diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt index 5b6923442f..23e7ec93c9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt @@ -361,25 +361,16 @@ class ImportExportPrefs @Inject constructor( override fun exportUserEntriesXml(activity: FragmentActivity, listEntries: Single>) { val entries = listEntries.blockingGet() + log.debug("XXXXX " + entries.size) prefFileList.ensureExportDirExists() - val legacyFile = prefFileList.legacyFile() val newFile = prefFileList.newExportXmlFile() -/* + log.debug("XXXXX " + classicPrefsFormat.returnXml(entries)) + askToConfirmExport(activity, newFile) { password -> try { - val entries: MutableMap = mutableMapOf() - for ((key, value) in sp.getAll()) { - entries[key] = value.toString() - } - - val prefs = Prefs(entries, prepareMetadata(activity)) - - if (BuildConfig.DEBUG && buildHelper.isEngineeringMode()) { - classicPrefsFormat.savePreferences(legacyFile, prefs) - } - encryptedPrefsFormat.savePreferences(newFile, prefs, password) - - ToastUtils.okToast(activity, resourceHelper.gs(R.string.exported)) + classicPrefsFormat.saveXml(newFile, entries) + log.debug("XXXXX " + newFile.isHidden + " " + newFile.absolutePath) + ToastUtils.okToast(activity, resourceHelper.gs(R.string.ue_exported)) } catch (e: FileNotFoundException) { ToastUtils.errorToast(activity, resourceHelper.gs(R.string.filenotfound) + " " + newFile) log.error(LTag.CORE, "Unhandled exception", e) @@ -400,6 +391,5 @@ class ImportExportPrefs @Inject constructor( } } - */ } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1484ce8709..1248d37520 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -179,6 +179,7 @@ Nightscout version: Missing %1$d g Preferences exported + User Entries exported Export settings to Import settings from Settings imported diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt index 1a86f59479..a94c50668c 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt @@ -31,6 +31,7 @@ class PrefFileListProvider @Inject constructor( private val path = File(Environment.getExternalStorageDirectory().toString()) private val aapsPath = File(path, "AAPS" + File.separator + "preferences") + private val xmlPath = File(path, "AAPS" + File.separator + "xml") private const val IMPORT_AGE_NOT_YET_OLD_DAYS = 60 } @@ -93,6 +94,9 @@ class PrefFileListProvider @Inject constructor( if (!aapsPath.exists()) { aapsPath.mkdirs() } + if (!xmlPath.exists()) { + xmlPath.mkdirs() + } } fun newExportFile(): File { @@ -102,7 +106,7 @@ class PrefFileListProvider @Inject constructor( fun newExportXmlFile(): File { val timeLocal = LocalDateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd'_'HHmmss")) - return File(aapsPath, timeLocal + "_UserEntry.xml") + return File(xmlPath, timeLocal + "_UserEntry.xml") } // check metadata for known issues, change their status and add info with explanations diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/ClassicPrefsFormat.kt b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/ClassicPrefsFormat.kt index 8123be8d98..3ab55fed9c 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/ClassicPrefsFormat.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/ClassicPrefsFormat.kt @@ -2,6 +2,13 @@ package info.nightscout.androidaps.plugins.general.maintenance.formats import info.nightscout.androidaps.Constants import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.database.entities.UserEntry +import info.nightscout.androidaps.database.entities.UserEntry.* +import info.nightscout.androidaps.interfaces.ProfileFunction +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.DecimalFormatter +import info.nightscout.androidaps.utils.Translator +import info.nightscout.androidaps.utils.extensions.stringId import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.storage.Storage import java.io.File @@ -13,6 +20,9 @@ import javax.inject.Singleton @Singleton class ClassicPrefsFormat @Inject constructor( private var resourceHelper: ResourceHelper, + private var dateUtil: DateUtil, + private var translator: Translator, + private var profileFunction: ProfileFunction, private var storage: Storage ) : PrefsFormat { @@ -67,4 +77,61 @@ class ClassicPrefsFormat @Inject constructor( return metadata } + fun saveXml(file: File, userEntries: List) { + try { + val contents = userEntries.joinToString("\n") { entry -> + if (entry.values.size > 0) { + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "," + + dateUtil.dateAndTimeAndSecondsString(entry.utcOffset) + "," + + resourceHelper.gs(entry.action.stringId()) + "," + + "\""+ entry.s.replace("\"", "\\\"") + "\"," + + entry.values.joinToString(",") { value -> valueWithUnitToStringXml(value) } + "\n" + } else { + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "," + + dateUtil.dateAndTimeAndSecondsString(entry.utcOffset) + "," + + resourceHelper.gs(entry.action.stringId()) + "," + + "\""+ entry.s.replace("\"", "\\\"") + "\",,\n" + } + } + storage.putFileContents(file, contents) + } catch (e: FileNotFoundException) { + throw PrefFileNotFoundError(file.absolutePath) + } catch (e: IOException) { + throw PrefIOError(file.absolutePath) + } + } + + fun returnXml(userEntries: List): String { + + return userEntries.joinToString("\n") { entry -> + if (entry.values.size > 0) { + entry.values.joinToString("\n") { value -> + "\"" + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "\"," + + "\"" + dateUtil.timeString(entry.utcOffset) + "\"," + + "\"" + resourceHelper.gs(entry.action.stringId()) + "\"," + + if (entry.s != "") {"\""+ entry.s.replace("\"", "\\\"") + "\"," } else { "," } + + valueWithUnitToStringXml(value) } + } else { + "\"" + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "\"," + + "\"" + dateUtil.timeString(entry.utcOffset) + "\"," + + "\"" + resourceHelper.gs(entry.action.stringId()) + "\"," + + if (entry.s != "") {"\""+ entry.s.replace("\"", "\\\"") + "\"," } else { ",," } + } + } + } + + fun valueWithUnitToStringXml(v: ValueWithUnit): String { + return when (v.unit) { + Units.Timestamp -> "\"" + dateUtil.dateAndTimeAndSecondsString(v.lValue) + "\",\"" + resourceHelper.gs(R.string.date) + "\"" + Units.CPEvent -> "\"" + translator.translate(v.sValue) + "\"," + Units.R_String -> "\"" + resourceHelper.gs(v.iValue) + "\"," + Units.Mg_Dl -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.Mg_Dl.stringId()) + "\"" else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + ",\"" + resourceHelper.gs(Units.Mmol_L.stringId()) + "\"" + Units.Mmol_L -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + ",\"" + resourceHelper.gs(Units.Mg_Dl.stringId()) + "\"" else DecimalFormatter.to1Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.Mmol_L.stringId()) + "\"" + Units.G -> v.iValue.toString() + ",\"" + resourceHelper.gs(Units.G.stringId()) + "\"" + Units.U_H -> DecimalFormatter.to2Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.U_H.stringId()) + "\"" + else -> if (v.sValue != "") {"\""+ v.sValue.replace("\"", "\\\"") + if (!v.unit.stringId().equals(0)) "\",\"" + resourceHelper.gs(v.unit.stringId()) + "\"" else "\","} + else if (v.dValue != 0.0 || v.iValue != 0) { v.value().toString() + if (!v.unit.stringId().equals(0)) ",\"" + resourceHelper.gs(v.unit.stringId()) + "\"" else "," } + else "," + } + } } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/PrefsFormat.kt b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/PrefsFormat.kt index 42cfa18f1e..3a1b754194 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/PrefsFormat.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/formats/PrefsFormat.kt @@ -5,6 +5,7 @@ import android.os.Parcelable import androidx.annotation.DrawableRes import androidx.annotation.StringRes import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.database.entities.UserEntry import kotlinx.parcelize.Parcelize import java.io.File diff --git a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt index a110d68827..5aadcceff9 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.utils.extensions import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.UserEntry.* fun Action.stringId(): Int { @@ -228,3 +229,8 @@ fun Units.stringId(): Int { else -> 0 } } + +fun UserEntry.toXml(): String { + val content = action.name + "," + values.joinToString(",") {value -> value.toXml()} + return "" +} diff --git a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt index 16e8503f59..668da47006 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt @@ -136,6 +136,13 @@ data class UserEntry( if (!iValue.equals(0)) return iValue return lValue } + + fun toXml() : String { + if (sValue != "") return "\""+ sValue.replace("\"", "\\\"") + "\"," + unit.text + if (!dValue.equals(0.0)) return dValue.toString() + "," + unit.text + if (!iValue.equals(0)) return return iValue.toString() + "," + unit.text + return return lValue.toString() + "," + unit.text + } } enum class Units(val text: String) { @SerializedName("None") None (""), @@ -169,4 +176,5 @@ data class UserEntry( Pump, Aaps } + } \ No newline at end of file