Skip to content
  •  
  •  
  •  
116 changes: 108 additions & 8 deletions app/src/main/kotlin/app/aaps/MainApp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.aaps

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.content.Intent
import android.content.IntentFilter
Expand All @@ -24,15 +25,20 @@ import app.aaps.core.interfaces.configuration.Config
import app.aaps.core.interfaces.configuration.ConfigBuilder
import app.aaps.core.interfaces.db.PersistenceLayer
import app.aaps.core.interfaces.di.ApplicationScope
import app.aaps.core.interfaces.insulin.InsulinType
import app.aaps.core.interfaces.logging.AAPSLogger
import app.aaps.core.interfaces.logging.LTag
import app.aaps.core.interfaces.plugin.ActivePlugin
import app.aaps.core.interfaces.plugin.PluginBase
import app.aaps.core.interfaces.profile.EffectiveProfile
import app.aaps.core.interfaces.profile.LocalProfileManager
import app.aaps.core.interfaces.profile.ProfileFunction
import app.aaps.core.interfaces.profile.ProfileUtil
import app.aaps.core.interfaces.resources.ResourceHelper
import app.aaps.core.interfaces.sharedPreferences.SP
import app.aaps.core.interfaces.ui.UiInteraction
import app.aaps.core.interfaces.utils.DateUtil
import app.aaps.core.interfaces.utils.HardLimits
import app.aaps.core.interfaces.utils.SafeParse
import app.aaps.core.interfaces.utils.fabric.FabricPrivacy
import app.aaps.core.interfaces.versionChecker.VersionCheckerUtils
Expand All @@ -41,15 +47,16 @@ import app.aaps.core.keys.BooleanNonKey
import app.aaps.core.keys.IntKey
import app.aaps.core.keys.LongComposedKey
import app.aaps.core.keys.ProfileComposedBooleanKey
import app.aaps.core.keys.ProfileComposedDoubleKey
import app.aaps.core.keys.ProfileComposedStringKey
import app.aaps.core.keys.StringKey
import app.aaps.core.keys.StringNonKey
import app.aaps.core.keys.UnitDoubleKey
import app.aaps.core.keys.interfaces.Preferences
import app.aaps.core.objects.profile.ProfileSealed
import app.aaps.core.ui.extensions.runOnUiThread
import app.aaps.core.ui.locale.LocaleHelper
import app.aaps.core.utils.JsonHelper
import app.aaps.database.AppRepository
import app.aaps.database.persistence.CompatDBHelper
import app.aaps.di.AppComponent
import app.aaps.di.DaggerAppComponent
Expand Down Expand Up @@ -115,8 +122,14 @@ class MainApp : DaggerApplication() {
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var profileUtil: ProfileUtil
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var repository: AppRepository
@Inject lateinit var hardLimits: HardLimits
@Inject lateinit var activePlugin: ActivePlugin
@Inject lateinit var localProfileManager: LocalProfileManager
@Inject @ApplicationScope lateinit var appScope: CoroutineScope
lateinit var appComponent: AppComponent
lateinit var insulinLabel: String
var insulinPeakTime: Long = 0L

private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
private lateinit var refreshWidget: Runnable
Expand All @@ -142,6 +155,9 @@ class MainApp : DaggerApplication() {
pluginStore.plugins = plugins
configBuilder.initialize()

// Do necessary data migrations (plugins must be initialized already)
dataMigrations()

// Do initializations in another thread
scope.launch { doInit() }
}
Expand Down Expand Up @@ -367,13 +383,9 @@ class MainApp : DaggerApplication() {
sp.remove(key)
}
if (key.startsWith(Constants.LOCAL_PROFILE + "_") && key.endsWith("_dia")) {
val number = key.split("_")[1]
if (value is String)
preferences.put(ProfileComposedDoubleKey.LocalProfileNumberedDia, SafeParse.stringToInt(number), value = SafeParse.stringToDouble(value))
else if (value is Float)
preferences.put(ProfileComposedDoubleKey.LocalProfileNumberedDia, SafeParse.stringToInt(number), value = value.toDouble())
else
preferences.put(ProfileComposedDoubleKey.LocalProfileNumberedDia, SafeParse.stringToInt(number), value = value as Double)
sp.remove(key)
}
if (key.startsWith(Constants.LOCAL_PROFILE + "_dia_")) {
sp.remove(key)
}
}
Expand Down Expand Up @@ -404,6 +416,46 @@ class MainApp : DaggerApplication() {

// Migrate temp target presets from old preference keys to JSON array
migrateTempTargetPresets()

// Get Insulin plugin information for database migration
insulinLabel = rh.get().gs(
when {
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefRapidActingPlugin", false) -> InsulinType.OREF_RAPID_ACTING.label
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefUltraRapidActingPlugin", false) -> InsulinType.OREF_ULTRA_RAPID_ACTING.label
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefFreePeakPlugin", false) -> InsulinType.OREF_FREE_PEAK.label
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinLyumjevPlugin", false) -> InsulinType.OREF_LYUMJEV.label
else -> InsulinType.OREF_RAPID_ACTING.label
}
)
insulinPeakTime = when {
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefRapidActingPlugin", false) -> InsulinType.OREF_RAPID_ACTING.insulinPeakTime
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefUltraRapidActingPlugin", false) -> InsulinType.OREF_ULTRA_RAPID_ACTING.insulinPeakTime
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefFreePeakPlugin", false) -> (sp.getInt("insulin_oref_peak", 75) * 60 * 1000).toLong()
sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinLyumjevPlugin", false) -> InsulinType.OREF_LYUMJEV.insulinPeakTime
else -> InsulinType.OREF_RAPID_ACTING.insulinPeakTime
}
// Migrate Insulin Plugins
if (sp.getBoolean("ConfigBuilder_INSULIN_InsulinOrefRapidActingPlugin_Enabled", false) || sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefRapidActingPlugin", false) ||
sp.getBoolean("ConfigBuilder_INSULIN_InsulinOrefUltraRapidActingPlugin_Enabled", false) || sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefUltraRapidActingPlugin", false) ||
sp.getBoolean("ConfigBuilder_INSULIN_InsulinOrefFreePeakPlugin_Enabled", false) || sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinOrefFreePeakPlugin", false) ||
sp.getBoolean("ConfigBuilder_INSULIN_InsulinLyumjevPlugin_Enabled", false) || sp.getBoolean("ConfigBuilder_Enabled_INSULIN_InsulinLyumjevPlugin", false)) {
sp.remove("ConfigBuilder_INSULIN_InsulinOrefRapidActingPlugin_Enabled")
sp.remove("ConfigBuilder_INSULIN_InsulinOrefRapidActingPlugin_Visible")
sp.remove("ConfigBuilder_Enabled_INSULIN_InsulinOrefRapidActingPlugin")
sp.remove("ConfigBuilder_INSULIN_InsulinOrefUltraRapidActingPlugin_Enabled")
sp.remove("ConfigBuilder_INSULIN_InsulinOrefUltraRapidActingPlugin_Visible")
sp.remove("ConfigBuilder_Enabled_INSULIN_InsulinOrefUltraRapidActingPlugin")
sp.remove("ConfigBuilder_INSULIN_InsulinOrefFreePeakPlugin_Enabled")
sp.remove("ConfigBuilder_INSULIN_InsulinOrefFreePeakPlugin_Visible")
sp.remove("ConfigBuilder_Enabled_INSULIN_InsulinOrefFreePeakPlugin")
sp.remove("ConfigBuilder_INSULIN_InsulinLyumjevPlugin_Enabled")
sp.remove("ConfigBuilder_INSULIN_InsulinLyumjevPlugin_Visible")
sp.remove("ConfigBuilder_Enabled_INSULIN_InsulinLyumjevPlugin")
sp.remove("insulin_oref_peak")
}
sp.putBoolean("ConfigBuilder_Enabled_INSULIN_InsulinPlugin", true)
// TODO Migrate insulin configurations

}

/**
Expand Down Expand Up @@ -486,6 +538,54 @@ class MainApp : DaggerApplication() {
// Removal will be done when legacy UI is completely removed in the future
}


@SuppressLint("CheckResult")
fun dataMigrations() {
// Migrate to database 32 (ICfg)
// Grab default value first
val dia = (profileFunction.getProfile() as ProfileSealed.EPS?)?.profileName?.let { profileName ->
localProfileManager.profile?.getSpecificProfile(profileName)?.iCfg?.dia
}
val insulinEndTime = ((dia ?: hardLimits.maxDia()) * 3600 * 1000).toLong()

val concentration = 1.0
runBlocking {
for (ps in persistenceLayer.getProfileSwitches()) {
if (ps.iCfg.insulinEndTime == -1L) {
// record not migrated yet
ps.iCfg.insulinLabel = insulinLabel
ps.iCfg.insulinEndTime = insulinEndTime
ps.iCfg.insulinPeakTime = insulinPeakTime
ps.iCfg.concentration = concentration
persistenceLayer.insertOrUpdateProfileSwitch(ps, Action.START_AAPS, Sources.Aaps, listValues = listOf())
aapsLogger.debug(LTag.CORE, "Migrating to 32: $ps")
}
}
for (eps in persistenceLayer.getEffectiveProfileSwitches()) {
if (eps.iCfg.insulinEndTime == -1L) {
// record not migrated yet
eps.iCfg.insulinLabel = insulinLabel
eps.iCfg.insulinEndTime = insulinEndTime
eps.iCfg.insulinPeakTime = insulinPeakTime
eps.iCfg.concentration = concentration
persistenceLayer.insertOrUpdateEffectiveProfileSwitch(eps)
aapsLogger.debug(LTag.CORE, "Migrating to 32: $eps")
}
}
for (bolus in persistenceLayer.getBoluses()) {
if (bolus.iCfg.insulinEndTime == -1L) {
// record not migrated yet
bolus.iCfg.insulinLabel = insulinLabel
bolus.iCfg.insulinEndTime = insulinEndTime
bolus.iCfg.insulinPeakTime = insulinPeakTime
bolus.iCfg.concentration = concentration
persistenceLayer.insertOrUpdateBolus(bolus, Action.START_AAPS, Sources.Aaps)
aapsLogger.debug(LTag.CORE, "Migrating to 32: $bolus")
}
}
}
}

override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
appComponent = DaggerAppComponent
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang
activePlugin.getSpecificPluginsList(PluginType.LOOP).forEach { addPreferencesIfEnabled(it, rootKey, config.APS) }
addPreferencesIfEnabled(activePlugin.activeAPS as PluginBase, rootKey, config.APS)
addPreferencesIfEnabled(activePlugin.activeSensitivity as PluginBase, rootKey)
addPreferencesIfEnabled(activePlugin.activePump as PluginBase, rootKey)
addPreferencesIfEnabled(activePlugin.activePumpInternal as PluginBase, rootKey)
addPumpScreen(rootKey)
addPreferencesIfEnabled(activePlugin.activeInsulin as PluginBase, rootKey)
activePlugin.getSpecificPluginsList(PluginType.SYNC).forEach { addPreferencesIfEnabled(it, rootKey) }
Expand Down
25 changes: 2 additions & 23 deletions app/src/main/kotlin/app/aaps/di/PluginsListModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import app.aaps.plugins.constraints.safety.SafetyPlugin
import app.aaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin
import app.aaps.plugins.constraints.storage.StorageConstraintPlugin
import app.aaps.plugins.constraints.versionChecker.VersionCheckerPlugin
import app.aaps.plugins.insulin.InsulinLyumjevPlugin
import app.aaps.plugins.insulin.InsulinOrefFreePeakPlugin
import app.aaps.plugins.insulin.InsulinOrefRapidActingPlugin
import app.aaps.plugins.insulin.InsulinOrefUltraRapidActingPlugin
import app.aaps.plugins.insulin.InsulinPlugin
import app.aaps.plugins.main.general.actions.ActionsPlugin
import app.aaps.plugins.main.general.food.FoodPlugin
import app.aaps.plugins.main.general.overview.OverviewPlugin
Expand Down Expand Up @@ -107,25 +104,7 @@ abstract class PluginsListModule {
@AllConfigs
@IntoMap
@IntKey(30)
abstract fun bindInsulinOrefRapidActingPlugin(plugin: InsulinOrefRapidActingPlugin): PluginBase

@Binds
@AllConfigs
@IntoMap
@IntKey(40)
abstract fun bindInsulinOrefUltraRapidActingPlugin(plugin: InsulinOrefUltraRapidActingPlugin): PluginBase

@Binds
@AllConfigs
@IntoMap
@IntKey(42)
abstract fun bindInsulinLyumjevPlugin(plugin: InsulinLyumjevPlugin): PluginBase

@Binds
@AllConfigs
@IntoMap
@IntKey(50)
abstract fun bindInsulinOrefFreePeakPlugin(plugin: InsulinOrefFreePeakPlugin): PluginBase
abstract fun bindInsulinPlugin(plugin: InsulinPlugin): PluginBase

@Binds
@AllConfigs
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/app/aaps/implementations/ConfigImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ConfigImpl @Inject constructor(
private var ignoreNightscoutV3Errors: Boolean? = null
private var doNotSendSmsOnProfileChange: Boolean? = null
private var enableAutotune: Boolean? = null
private var enableInsulinConcentration: Boolean? = null
private var disableLeakCanary: Boolean? = null

override fun isEngineeringModeOrRelease(): Boolean = if (!APS) true else isEngineeringMode() || !isDev()
Expand All @@ -54,5 +55,6 @@ class ConfigImpl @Inject constructor(
override fun ignoreNightscoutV3Errors(): Boolean = ignoreNightscoutV3Errors ?: (fileListProvider.get().ensureExtraDirExists()?.findFile("ignore_nightscout_v3_errors") != null).also { ignoreNightscoutV3Errors = it }
override fun doNotSendSmsOnProfileChange(): Boolean = doNotSendSmsOnProfileChange ?: (fileListProvider.get().ensureExtraDirExists()?.findFile("do_not_send_sms_on_profile_change") != null).also { doNotSendSmsOnProfileChange = it }
override fun enableAutotune(): Boolean = enableAutotune ?: (fileListProvider.get().ensureExtraDirExists()?.findFile("enable_autotune") != null).also { enableAutotune = it }
override fun enableInsulinConcentration(): Boolean = enableInsulinConcentration ?: (fileListProvider.get().ensureExtraDirExists()?.findFile("enable_insulin_concentration") != null).also { enableInsulinConcentration = it }
override fun disableLeakCanary(): Boolean = disableLeakCanary ?: (fileListProvider.get().ensureExtraDirExists()?.findFile("disable_leakcanary") != null).also { disableLeakCanary = it }
}
35 changes: 33 additions & 2 deletions app/src/main/kotlin/app/aaps/implementations/UiInteractionImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import app.aaps.MainActivity
import app.aaps.activities.HistoryBrowseActivity
import app.aaps.activities.MyPreferenceFragment
import app.aaps.activities.PreferencesActivity
import app.aaps.core.data.model.ICfg
import app.aaps.core.interfaces.notifications.Notification
import app.aaps.core.interfaces.nsclient.NSAlarm
import app.aaps.core.interfaces.protection.ProtectionCheck
import app.aaps.core.interfaces.rx.bus.RxBus
import app.aaps.core.interfaces.rx.events.EventDismissNotification
import app.aaps.core.interfaces.rx.events.EventNewNotification
import app.aaps.core.interfaces.ui.UiInteraction
import app.aaps.core.objects.extensions.toJson
import app.aaps.core.keys.interfaces.Preferences
import app.aaps.core.ui.toast.ToastUtils
import app.aaps.plugins.configuration.activities.SingleFragmentActivity
import app.aaps.plugins.main.general.overview.notifications.NotificationWithAction
import app.aaps.ui.activities.BolusProgressHelperActivity
import app.aaps.ui.activities.ConcentrationActivity
import app.aaps.ui.activities.ErrorActivity
import app.aaps.ui.activities.ProfileViewerActivity
import app.aaps.ui.activities.QuickWizardListActivity
Expand All @@ -32,9 +35,11 @@ import app.aaps.ui.dialogs.BolusProgressDialog
import app.aaps.ui.dialogs.CalibrationDialog
import app.aaps.ui.dialogs.CarbsDialog
import app.aaps.ui.dialogs.CareDialog
import app.aaps.ui.dialogs.ConcentrationDialog
import app.aaps.ui.dialogs.ExtendedBolusDialog
import app.aaps.ui.dialogs.FillDialog
import app.aaps.ui.dialogs.InsulinDialog
import app.aaps.ui.dialogs.InsulinSwitchDialog
import app.aaps.ui.dialogs.LoopDialog
import app.aaps.ui.dialogs.ProfileSwitchDialog
import app.aaps.ui.dialogs.SiteRotationDialog
Expand Down Expand Up @@ -67,6 +72,7 @@ class UiInteractionImpl @Inject constructor(
override val historyBrowseActivity: Class<*> = HistoryBrowseActivity::class.java
override val errorHelperActivity: Class<*> = ErrorActivity::class.java
override val bolusProgressHelperActivity: Class<*> = BolusProgressHelperActivity::class.java
override val concentrationActivity: Class<*> = ConcentrationActivity::class.java
override val singleFragmentActivity: Class<*> = SingleFragmentActivity::class.java
override val preferencesActivity: Class<*> = PreferencesActivity::class.java
override val myPreferenceFragment: Class<*> = MyPreferenceFragment::class.java
Expand All @@ -84,6 +90,10 @@ class UiInteractionImpl @Inject constructor(
context.startActivity(i)
}

override fun runInsulinConfirmation() {
context.startActivity(Intent(context, concentrationActivity).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
}

override fun updateWidget(context: Context, from: String) {
Widget.updateWidget(context, from)
}
Expand All @@ -104,9 +114,14 @@ class UiInteractionImpl @Inject constructor(
.show(fragmentManager, "LoopDialog")
}

override fun runProfileSwitchDialog(fragmentManager: FragmentManager, profileName: String?) {
override fun runProfileSwitchDialog(fragmentManager: FragmentManager, profileName: String?, iCfg: ICfg?) {
ProfileSwitchDialog()
.also { it.arguments = Bundle().also { bundle -> bundle.putString("profileName", profileName) } }
.also {
it.arguments = Bundle().also { bundle ->
bundle.putString("profileName", profileName)
iCfg?.let { bundle.putString("iCfg", it.toJson().toString()) }
}
}
.show(fragmentManager, "ProfileSwitchDialog")
}

Expand All @@ -125,11 +140,27 @@ class UiInteractionImpl @Inject constructor(
.show(fragmentManager, "InsulinDialog")
}

override fun runInsulinSwitchDialog(fragmentManager: FragmentManager, concentration: Double?, iCfg: ICfg?) {
InsulinSwitchDialog()
.also {
it.arguments = Bundle().also { bundle ->
iCfg?.let { bundle.putString("iCfg", it.toJson().toString()) }
concentration?.let { bundle.putDouble("concentration", it) }
}
}
.show(fragmentManager, "InsulinSwitchDialog")
}

override fun runCalibrationDialog(fragmentManager: FragmentManager) {
CalibrationDialog()
.show(fragmentManager, "CalibrationDialog")
}

override fun runConcentrationDialog(fragmentManager: FragmentManager) {
ConcentrationDialog()
.show(fragmentManager, "ConcentrationDialog")
}

override fun runCarbsDialog(fragmentManager: FragmentManager) {
CarbsDialog()
.show(fragmentManager, "CarbsDialog")
Expand Down
Loading
Loading