diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt index d550f3b247..709e290e8d 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt @@ -28,9 +28,9 @@ import com.google.android.material.tabs.TabLayoutMediator import com.joanzapata.iconify.Iconify import com.joanzapata.iconify.fonts.FontAwesomeModule import dev.doubledot.doki.ui.DokiActivity -import info.nightscout.androidaps.activities.ProfileHelperActivity import info.nightscout.androidaps.activities.NoSplashAppCompatActivity import info.nightscout.androidaps.activities.PreferencesActivity +import info.nightscout.androidaps.activities.ProfileHelperActivity import info.nightscout.androidaps.activities.SingleFragmentActivity import info.nightscout.androidaps.activities.StatsActivity import info.nightscout.androidaps.events.EventAppExit @@ -71,6 +71,7 @@ import javax.inject.Inject import kotlin.system.exitProcess class MainActivity : NoSplashAppCompatActivity() { + private val disposable = CompositeDisposable() @Inject lateinit var aapsLogger: AAPSLogger @@ -136,16 +137,17 @@ class MainActivity : NoSplashAppCompatActivity() { if (it.recreate) recreate() else setupViews() setWakeLock() - }, fabricPrivacy::logException ) + }, fabricPrivacy::logException) ) disposable.add(rxBus .toObservable(EventPreferenceChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ processPreferenceChange(it) }, fabricPrivacy::logException ) + .subscribe({ processPreferenceChange(it) }, fabricPrivacy::logException) ) if (!sp.getBoolean(R.string.key_setupwizard_processed, false) && !isRunningRealPumpTest()) { - val intent = Intent(this, SetupWizardActivity::class.java) - startActivity(intent) + protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { + startActivity(Intent(this, SetupWizardActivity::class.java)) + }) } androidPermission.notifyForStoragePermission(this) androidPermission.notifyForBatteryOptimizationPermission(this) @@ -238,7 +240,7 @@ class MainActivity : NoSplashAppCompatActivity() { if (permissions.isNotEmpty()) { if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) { when (requestCode) { - AndroidPermission.CASE_STORAGE -> //show dialog after permission is granted + AndroidPermission.CASE_STORAGE -> //show dialog after permission is granted OKDialog.show(this, "", resourceHelper.gs(R.string.alert_dialog_storage_permission_text)) AndroidPermission.CASE_LOCATION, AndroidPermission.CASE_SMS, AndroidPermission.CASE_BATTERY, AndroidPermission.CASE_PHONE_STATE, AndroidPermission.CASE_SYSTEM_WINDOW -> { @@ -273,7 +275,7 @@ class MainActivity : NoSplashAppCompatActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.nav_preferences -> { + R.id.nav_preferences -> { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { val i = Intent(this, PreferencesActivity::class.java) i.putExtra("id", -1) @@ -282,17 +284,19 @@ class MainActivity : NoSplashAppCompatActivity() { return true } - R.id.nav_historybrowser -> { + R.id.nav_historybrowser -> { startActivity(Intent(this, HistoryBrowseActivity::class.java)) return true } - R.id.nav_setupwizard -> { - startActivity(Intent(this, SetupWizardActivity::class.java)) + R.id.nav_setupwizard -> { + protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { + startActivity(Intent(this, SetupWizardActivity::class.java)) + }) return true } - R.id.nav_about -> { + R.id.nav_about -> { var message = "Build: ${BuildConfig.BUILDVERSION}\n" message += "Flavor: ${BuildConfig.FLAVOR}${BuildConfig.BUILD_TYPE}\n" message += "${resourceHelper.gs(R.string.configbuilder_nightscoutversion_label)} ${nsSettingsStatus.nightscoutVersionName}" @@ -313,7 +317,7 @@ class MainActivity : NoSplashAppCompatActivity() { return true } - R.id.nav_exit -> { + R.id.nav_exit -> { aapsLogger.debug(LTag.CORE, "Exiting") rxBus.send(EventAppExit()) finish() @@ -336,12 +340,12 @@ class MainActivity : NoSplashAppCompatActivity() { return true } */ - R.id.nav_defaultprofile -> { + R.id.nav_defaultprofile -> { startActivity(Intent(this, ProfileHelperActivity::class.java)) return true } - R.id.nav_stats -> { + R.id.nav_stats -> { startActivity(Intent(this, StatsActivity::class.java)) return true } diff --git a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt index b3f3024de6..16a870ce61 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt @@ -7,9 +7,6 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener import android.os.Bundle import androidx.annotation.XmlRes import androidx.preference.* -import dagger.android.AndroidInjector -import dagger.android.DispatchingAndroidInjector -import dagger.android.HasAndroidInjector import dagger.android.support.AndroidSupportInjection import info.nightscout.androidaps.Config import info.nightscout.androidaps.R @@ -58,7 +55,7 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import javax.inject.Inject -class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeListener, HasAndroidInjector { +class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeListener { private var pluginId = -1 private var filter = "" @@ -103,11 +100,6 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang @Inject lateinit var nsSettingStatus: NSSettingsStatus @Inject lateinit var openHumansUploader: OpenHumansUploader - // TODO why? - @Inject lateinit var androidInjector: DispatchingAndroidInjector - - override fun androidInjector(): AndroidInjector = androidInjector - override fun onAttach(context: Context) { AndroidSupportInjection.inject(this) super.onAttach(context) @@ -259,7 +251,7 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang } } - private fun addPreferencesFromResource(@XmlRes preferencesResId: Int, key: String?, enabled: Boolean) { + private fun addPreferencesFromResource(@Suppress("SameParameterValue") @XmlRes preferencesResId: Int, key: String?, enabled: Boolean) { if (enabled) addPreferencesFromResource(preferencesResId, key) } @@ -426,7 +418,7 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang return super.onPreferenceTreeClick(preference) } - public fun setFilter(filter: String) { + fun setFilter(filter: String) { this.filter = filter updateFilterVisibility(filter, preferenceScreen) } diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt index 42deb79742..f817821057 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt @@ -10,7 +10,6 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.utils.locale.LocaleHelper import info.nightscout.androidaps.utils.resources.ResourceHelper import kotlinx.android.synthetic.main.activity_preferences.* -import kotlinx.android.synthetic.main.local_insight_status_item.view.* import javax.inject.Inject class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback { @@ -33,8 +32,8 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa override fun afterTextChanged(s: Editable) {} }) - pref_filter_clear.setOnClickListener() { - pref_filter.setText(""); + pref_filter_clear.setOnClickListener { + pref_filter.setText("") } title = resourceHelper.gs(R.string.nav_preferences) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index a4b96354fd..af8f4e3125 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -22,6 +22,7 @@ import javax.inject.Singleton; import dagger.Lazy; import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.BuildConfig; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; @@ -55,10 +56,10 @@ import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui; import info.nightscout.androidaps.plugins.aps.loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker; +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.wear.ActionStringHandler; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; @@ -95,9 +96,8 @@ public class LoopPlugin extends PluginBase implements LoopInterface { private final FabricPrivacy fabricPrivacy; private final NSUpload nsUpload; private final HardLimits hardLimits; - private Notification notification; - private CompositeDisposable disposable = new CompositeDisposable(); + private final CompositeDisposable disposable = new CompositeDisposable(); private static final String CHANNEL_ID = "AndroidAPS-Openloop"; @@ -126,6 +126,7 @@ public class LoopPlugin extends PluginBase implements LoopInterface { AAPSLogger aapsLogger, RxBusWrapper rxBus, SP sp, + Config config, ConstraintChecker constraintChecker, ResourceHelper resourceHelper, ProfileFunction profileFunction, @@ -147,6 +148,7 @@ public class LoopPlugin extends PluginBase implements LoopInterface { .pluginName(R.string.loop) .shortName(R.string.loop_shortname) .preferencesId(R.xml.pref_loop) + .enableByDefault(config.getAPS()) .description(R.string.description_loop), aapsLogger, resourceHelper, injector ); @@ -415,7 +417,7 @@ public class LoopPlugin extends PluginBase implements LoopInterface { resultAfterConstraints.smb = 0; } - if (lastRun != null) { + if (lastRun != null && lastRun.getConstraintsProcessed() != null) { prevCarbsreq = lastRun.getConstraintsProcessed().carbsReq; } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.kt b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.kt index eb6e39d0ac..755a033576 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.kt +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.kt @@ -2,19 +2,15 @@ package info.nightscout.androidaps.setupwizard import android.Manifest import android.content.Context -import android.content.Intent import androidx.appcompat.app.AppCompatActivity import dagger.android.HasAndroidInjector import info.nightscout.androidaps.Config import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R -import info.nightscout.androidaps.activities.PreferencesActivity import info.nightscout.androidaps.dialogs.ProfileSwitchDialog -import info.nightscout.androidaps.events.EventConfigBuilderChange import info.nightscout.androidaps.events.EventPumpStatusChanged import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.CommandQueueProvider -import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin @@ -37,8 +33,6 @@ import info.nightscout.androidaps.setupwizard.events.EventSWUpdate import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.CryptoUtil import info.nightscout.androidaps.utils.extensions.isRunningTest -import info.nightscout.androidaps.utils.locale.LocaleHelper.update -import info.nightscout.androidaps.utils.protection.ProtectionCheck import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import java.util.* @@ -61,7 +55,6 @@ class SWDefinition @Inject constructor( private val loopPlugin: LoopPlugin, private val nsClientPlugin: NSClientPlugin, private val nsProfilePlugin: NSProfilePlugin, - private val protectionCheck: ProtectionCheck, private val importExportPrefs: ImportExportPrefs, private val androidPermission: AndroidPermission, private val cryptoUtil: CryptoUtil, @@ -83,16 +76,6 @@ class SWDefinition @Inject constructor( private val screenSetupWizard = SWScreen(injector, R.string.nav_setupwizard) .add(SWInfotext(injector) .label(R.string.welcometosetupwizard)) - private val screenLanguage = SWScreen(injector, R.string.language) - .skippable(false) - .add(SWRadioButton(injector) - .option(R.array.languagesArray, R.array.languagesValues) - .preferenceId(R.string.key_language).label(R.string.language) - .comment(R.string.setupwizard_language_prompt)) - .validator(SWValidator { - update(context) - sp.contains(R.string.key_language) - }) private val screenEula = SWScreen(injector, R.string.end_user_license_agreement) .skippable(false) .add(SWInfotext(injector) @@ -100,20 +83,20 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.end_user_license_agreement_i_understand) - .visibility(SWValidator { !sp.getBoolean(R.string.key_i_understand, false) }) - .action(Runnable { + .visibility { !sp.getBoolean(R.string.key_i_understand, false) } + .action { sp.putBoolean(R.string.key_i_understand, true) rxBus.send(EventSWUpdate(false)) - })) - .visibility(SWValidator { !sp.getBoolean(R.string.key_i_understand, false) }) - .validator(SWValidator { sp.getBoolean(R.string.key_i_understand, false) }) + }) + .visibility { !sp.getBoolean(R.string.key_i_understand, false) } + .validator { sp.getBoolean(R.string.key_i_understand, false) } private val screenUnits = SWScreen(injector, R.string.units) .skippable(false) .add(SWRadioButton(injector) .option(R.array.unitsArray, R.array.unitsValues) .preferenceId(R.string.key_units).label(R.string.units) .comment(R.string.setupwizard_units_prompt)) - .validator(SWValidator { sp.contains(R.string.key_units) }) + .validator { sp.contains(R.string.key_units) } private val displaySettings = SWScreen(injector, R.string.wear_display_settings) .skippable(false) .add(SWEditNumberWithUnits(injector, Constants.LOWMARK * Constants.MGDL_TO_MMOLL, 3.0, 8.0) @@ -134,10 +117,10 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.askforpermission) - .visibility(SWValidator { androidPermission.permissionNotGranted(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) }) - .action(Runnable { androidPermission.askForPermission(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, AndroidPermission.CASE_BATTERY) })) - .visibility(SWValidator { androidPermission.permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) }) - .validator(SWValidator { !androidPermission.permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) }) + .visibility { androidPermission.permissionNotGranted(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) } + .action { androidPermission.askForPermission(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, AndroidPermission.CASE_BATTERY) }) + .visibility { androidPermission.permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) } + .validator { !androidPermission.permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) } private val screenPermissionBt = SWScreen(injector, R.string.permission) .skippable(false) .add(SWInfotext(injector) @@ -145,10 +128,10 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.askforpermission) - .visibility(SWValidator { androidPermission.permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION) }) - .action(Runnable { androidPermission.askForPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION, AndroidPermission.CASE_LOCATION) })) - .visibility(SWValidator { androidPermission.permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION) }) - .validator(SWValidator { !androidPermission.permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION) }) + .visibility { androidPermission.permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION) } + .action { androidPermission.askForPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION, AndroidPermission.CASE_LOCATION) }) + .visibility { androidPermission.permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION) } + .validator { !androidPermission.permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION) } private val screenPermissionStore = SWScreen(injector, R.string.permission) .skippable(false) .add(SWInfotext(injector) @@ -156,18 +139,18 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.askforpermission) - .visibility(SWValidator { androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) }) - .action(Runnable { androidPermission.askForPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, AndroidPermission.CASE_STORAGE) })) - .visibility(SWValidator { androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) }) - .validator(SWValidator { !androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) }) + .visibility { androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) } + .action { androidPermission.askForPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, AndroidPermission.CASE_STORAGE) }) + .visibility { androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) } + .validator { !androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) } private val screenImport = SWScreen(injector, R.string.nav_import) .add(SWInfotext(injector) .label(R.string.storedsettingsfound)) .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.nav_import) - .action(Runnable { importExportPrefs.importSharedPreferences(activity) })) - .visibility(SWValidator { importExportPrefs.prefsFileExists() && !androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) }) + .action { importExportPrefs.importSharedPreferences(activity) }) + .visibility { importExportPrefs.prefsFileExists() && !androidPermission.permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) } private val screenNsClient = SWScreen(injector, R.string.nsclientinternal_title) .skippable(true) .add(SWInfotext(injector) @@ -175,22 +158,18 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.enable_nsclient) - .action(Runnable { - nsClientPlugin.setPluginEnabled(PluginType.GENERAL, true) - nsClientPlugin.setFragmentVisible(PluginType.GENERAL, true) - configBuilderPlugin.processOnEnabledCategoryChanged(nsClientPlugin, PluginType.GENERAL) - configBuilderPlugin.storeSettings("SetupWizard") - rxBus.send(EventConfigBuilderChange()) + .action { + configBuilderPlugin.performPluginSwitch(nsClientPlugin, true, PluginType.GENERAL) rxBus.send(EventSWUpdate(true)) - }) - .visibility(SWValidator { !nsClientPlugin.isEnabled(PluginType.GENERAL) })) + } + .visibility { !nsClientPlugin.isEnabled(PluginType.GENERAL) }) .add(SWEditUrl(injector) .preferenceId(R.string.key_nsclientinternal_url) .updateDelay(5) .label(R.string.nsclientinternal_url_title) .comment(R.string.nsclientinternal_url_dialogmessage)) .add(SWEditString(injector) - .validator(SWTextValidator { text: String -> text.length >= 12 }) + .validator { text: String -> text.length >= 12 } .preferenceId(R.string.key_nsclientinternal_api_secret) .updateDelay(5) .label(R.string.nsclientinternal_secret_dialogtitle) @@ -200,9 +179,8 @@ class SWDefinition @Inject constructor( .label(R.string.status) .initialStatus(nsClientPlugin.status) ) - .add(SWBreak(injector)) - .validator(SWValidator { nsClientPlugin.nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth }) - .visibility(SWValidator { !(nsClientPlugin.nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth) }) + .validator { nsClientPlugin.nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth } + .visibility { !(nsClientPlugin.nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth) } private val screenPatientName = SWScreen(injector, R.string.patient_name) .skippable(true) .add(SWInfotext(injector) @@ -219,7 +197,7 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWInfotext(injector) .label(R.string.master_password_summary)) - .validator(SWValidator { !cryptoUtil.checkPassword("", sp.getString(R.string.key_master_password, "")) }) + .validator { !cryptoUtil.checkPassword("", sp.getString(R.string.key_master_password, "")) } private val screenAge = SWScreen(injector, R.string.patientage) .skippable(false) .add(SWBreak(injector)) @@ -228,51 +206,28 @@ class SWDefinition @Inject constructor( .preferenceId(R.string.key_age) .label(R.string.patientage) .comment(R.string.patientage_summary)) - .validator(SWValidator { sp.contains(R.string.key_age) }) + .validator { sp.contains(R.string.key_age) } private val screenInsulin = SWScreen(injector, R.string.configbuilder_insulin) .skippable(false) - .add(SWPlugin(injector) + .add(SWPlugin(injector, this) .option(PluginType.INSULIN, R.string.configbuilder_insulin_description) .makeVisible(false) .label(R.string.configbuilder_insulin)) .add(SWBreak(injector)) .add(SWInfotext(injector) .label(R.string.diawarning)) - .add(SWBreak(injector)) - .add(SWButton(injector) - .text(R.string.insulinsourcesetup) - .action(Runnable { - val plugin = activePlugin.activeInsulin as PluginBase - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { - val i = Intent(activity, PreferencesActivity::class.java) - i.putExtra("id", plugin.preferencesId) - activity.startActivity(i) - }, null) - }) - .visibility(SWValidator { (activePlugin.activeInsulin as PluginBase).preferencesId > 0 })) private val screenBgSource = SWScreen(injector, R.string.configbuilder_bgsource) .skippable(false) - .add(SWPlugin(injector) + .add(SWPlugin(injector, this) .option(PluginType.BGSOURCE, R.string.configbuilder_bgsource_description) .label(R.string.configbuilder_bgsource)) .add(SWBreak(injector)) - .add(SWButton(injector) - .text(R.string.bgsourcesetup) - .action(Runnable { - val plugin = activePlugin.activeBgSource as PluginBase - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { - val i = Intent(activity, PreferencesActivity::class.java) - i.putExtra("id", plugin.preferencesId) - activity.startActivity(i) - }, null) - }) - .visibility(SWValidator { (activePlugin.activeBgSource as PluginBase).preferencesId > 0 })) private val screenProfile = SWScreen(injector, R.string.configbuilder_profile) .skippable(false) .add(SWInfotext(injector) .label(R.string.setupwizard_profile_description)) .add(SWBreak(injector)) - .add(SWPlugin(injector) + .add(SWPlugin(injector, this) .option(PluginType.PROFILE, R.string.configbuilder_profile_description) .label(R.string.configbuilder_profile)) private val screenNsProfile = SWScreen(injector, R.string.nsprofile) @@ -281,69 +236,54 @@ class SWDefinition @Inject constructor( .label(R.string.adjustprofileinns)) .add(SWFragment(injector, this) .add(NSProfileFragment())) - .validator(SWValidator { nsProfilePlugin.profile != null && nsProfilePlugin.profile!!.getDefaultProfile() != null && nsProfilePlugin.profile!!.getDefaultProfile()!!.isValid("StartupWizard") }) - .visibility(SWValidator { nsProfilePlugin.isEnabled(PluginType.PROFILE) }) + .validator { nsProfilePlugin.profile != null && nsProfilePlugin.profile!!.getDefaultProfile() != null && nsProfilePlugin.profile!!.getDefaultProfile()!!.isValid("StartupWizard") } + .visibility { nsProfilePlugin.isEnabled(PluginType.PROFILE) } private val screenLocalProfile = SWScreen(injector, R.string.localprofile) .skippable(false) .add(SWFragment(injector, this) .add(LocalProfileFragment())) - .validator(SWValidator { localProfilePlugin.profile?.getDefaultProfile()?.isValid("StartupWizard") == true }) - .visibility(SWValidator { localProfilePlugin.isEnabled(PluginType.PROFILE) }) + .validator { localProfilePlugin.profile?.getDefaultProfile()?.isValid("StartupWizard") == true } + .visibility { localProfilePlugin.isEnabled(PluginType.PROFILE) } private val screenProfileSwitch = SWScreen(injector, R.string.careportal_profileswitch) .skippable(false) .add(SWInfotext(injector) .label(R.string.profileswitch_ismissing)) .add(SWButton(injector) .text(R.string.doprofileswitch) - .action(Runnable { ProfileSwitchDialog().show(activity.supportFragmentManager, "SetupWizard") })) - .validator(SWValidator { profileFunction.getProfile() != null }) - .visibility(SWValidator { profileFunction.getProfile() == null }) + .action { ProfileSwitchDialog().show(activity.supportFragmentManager, "SetupWizard") }) + .validator { profileFunction.getProfile() != null } + .visibility { profileFunction.getProfile() == null } private val screenPump = SWScreen(injector, R.string.configbuilder_pump) .skippable(false) - .add(SWPlugin(injector) + .add(SWPlugin(injector, this) .option(PluginType.PUMP, R.string.configbuilder_pump_description) .label(R.string.configbuilder_pump)) .add(SWBreak(injector)) - .add(SWButton(injector) - .text(R.string.pumpsetup) - .action(Runnable { - val plugin = activePlugin.activePump as PluginBase - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { - val i = Intent(activity, PreferencesActivity::class.java) - i.putExtra("id", plugin.preferencesId) - activity.startActivity(i) - }, null) - }) - .visibility(SWValidator { (activePlugin.activePump as PluginBase).preferencesId > 0 })) .add(SWInfotext(injector) .label(R.string.setupwizard_pump_pump_not_initialized) - .visibility(SWValidator { !isPumpInitialized() })) + .visibility { !isPumpInitialized() }) .add( // Omnipod only SWInfotext(injector) .label(R.string.setupwizard_pump_waiting_for_riley_link_connection) - .visibility(SWValidator { + .visibility { val activePump = activePlugin.activePump activePump is OmnipodPumpPlugin && !activePump.isRileyLinkReady - })) + }) .add( // Omnipod only SWEventListener(injector, EventRileyLinkDeviceStatusChange::class.java) .label(R.string.setupwizard_pump_riley_link_status) - .visibility(SWValidator { - activePlugin.activePump is OmnipodPumpPlugin - })) + .visibility { activePlugin.activePump is OmnipodPumpPlugin }) .add(SWButton(injector) .text(R.string.readstatus) - .action(Runnable { commandQueue.readStatus("Clicked connect to pump", null) }) - .visibility(SWValidator { + .action { commandQueue.readStatus("Clicked connect to pump", null) } + .visibility { // Hide for Omnipod, because as we don't require a Pod to be paired in the setup wizard, // Getting the status might not be possible activePlugin.activePump !is OmnipodPumpPlugin - })) + }) .add(SWEventListener(injector, EventPumpStatusChanged::class.java) - .visibility(SWValidator { activePlugin.activePump !is OmnipodPumpPlugin })) - .validator(SWValidator { - isPumpInitialized() - }) + .visibility { activePlugin.activePump !is OmnipodPumpPlugin }) + .validator { isPumpInitialized() } private fun isPumpInitialized(): Boolean { val activePump = activePlugin.activePump @@ -358,31 +298,20 @@ class SWDefinition @Inject constructor( .add(SWInfotext(injector) .label(R.string.setupwizard_aps_description)) .add(SWBreak(injector)) + .add(SWPlugin(injector, this) + .option(PluginType.APS, R.string.configbuilder_aps_description) + .label(R.string.configbuilder_aps)) + .add(SWBreak(injector)) .add(SWHtmlLink(injector) .label("https://openaps.readthedocs.io/en/latest/")) .add(SWBreak(injector)) - .add(SWPlugin(injector) - .option(PluginType.APS, R.string.configbuilder_aps_description) - .label(R.string.configbuilder_aps)) - .add(SWButton(injector) - .text(R.string.apssetup) - .action(Runnable { - val plugin = activePlugin.activeAPS as PluginBase - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { - val i = Intent(activity, PreferencesActivity::class.java) - i.putExtra("id", plugin.preferencesId) - activity.startActivity(i) - }, null) - }) - .visibility(SWValidator { (activePlugin.activeAPS as PluginBase).preferencesId > 0 })) - .visibility(SWValidator { config.APS }) private val screenApsMode = SWScreen(injector, R.string.apsmode_title) .skippable(false) .add(SWRadioButton(injector) .option(R.array.aps_modeArray, R.array.aps_modeValues) .preferenceId(R.string.key_aps_mode).label(R.string.apsmode_title) .comment(R.string.setupwizard_preferred_aps_mode)) - .validator(SWValidator { sp.contains(R.string.key_aps_mode) }) + .validator { sp.contains(R.string.key_aps_mode) } private val screenLoop = SWScreen(injector, R.string.configbuilder_loop) .skippable(false) .add(SWInfotext(injector) @@ -390,17 +319,13 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWButton(injector) .text(R.string.enableloop) - .action(Runnable { - loopPlugin.setPluginEnabled(PluginType.LOOP, true) - loopPlugin.setFragmentVisible(PluginType.LOOP, true) - configBuilderPlugin.processOnEnabledCategoryChanged(loopPlugin, PluginType.LOOP) - configBuilderPlugin.storeSettings("SetupWizard") - rxBus.send(EventConfigBuilderChange()) + .action { + configBuilderPlugin.performPluginSwitch(loopPlugin, true, PluginType.LOOP) rxBus.send(EventSWUpdate(true)) - }) - .visibility(SWValidator { !loopPlugin.isEnabled(PluginType.LOOP) })) - .validator(SWValidator { loopPlugin.isEnabled(PluginType.LOOP) }) - .visibility(SWValidator { !loopPlugin.isEnabled(PluginType.LOOP) && config.APS }) + } + .visibility { !loopPlugin.isEnabled(PluginType.LOOP) }) + .validator { loopPlugin.isEnabled(PluginType.LOOP) } + .visibility { !loopPlugin.isEnabled(PluginType.LOOP) && config.APS } private val screenSensitivity = SWScreen(injector, R.string.configbuilder_sensitivity) .skippable(false) .add(SWInfotext(injector) @@ -408,21 +333,9 @@ class SWDefinition @Inject constructor( .add(SWHtmlLink(injector) .label(R.string.setupwizard_sensitivity_url)) .add(SWBreak(injector)) - .add(SWPlugin(injector) + .add(SWPlugin(injector, this) .option(PluginType.SENSITIVITY, R.string.configbuilder_sensitivity_description) .label(R.string.configbuilder_sensitivity)) - .add(SWBreak(injector)) - .add(SWButton(injector) - .text(R.string.sensitivitysetup) - .action(Runnable { - val plugin = activePlugin.activeSensitivity as PluginBase - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { - val i = Intent(activity, PreferencesActivity::class.java) - i.putExtra("id", plugin.preferencesId) - activity.startActivity(i) - }, null) - }) - .visibility(SWValidator { (activePlugin.activeSensitivity as PluginBase).preferencesId > 0 })) private val getScreenObjectives = SWScreen(injector, R.string.objectives) .skippable(false) .add(SWInfotext(injector) @@ -430,8 +343,8 @@ class SWDefinition @Inject constructor( .add(SWBreak(injector)) .add(SWFragment(injector, this) .add(ObjectivesFragment())) - .validator(SWValidator { objectivesPlugin.objectives[ObjectivesPlugin.FIRST_OBJECTIVE].isStarted }) - .visibility(SWValidator { !objectivesPlugin.objectives[ObjectivesPlugin.FIRST_OBJECTIVE].isStarted && config.APS }) + .validator { objectivesPlugin.objectives[ObjectivesPlugin.FIRST_OBJECTIVE].isStarted } + .visibility { !objectivesPlugin.objectives[ObjectivesPlugin.FIRST_OBJECTIVE].isStarted && config.APS } private fun swDefinitionFull() { // List all the screens here add(screenSetupWizard) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.kt b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.kt index 5d8c1f0f93..60702978e9 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.setupwizard.elements +import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.LinearLayout @@ -7,15 +8,17 @@ import android.widget.RadioButton import android.widget.RadioGroup import android.widget.TextView import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.activities.MyPreferenceFragment import info.nightscout.androidaps.events.EventConfigBuilderChange import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.PluginStore +import info.nightscout.androidaps.setupwizard.SWDefinition import info.nightscout.androidaps.setupwizard.events.EventSWUpdate import javax.inject.Inject -class SWPlugin(injector: HasAndroidInjector) : SWItem(injector, Type.PLUGIN) { +class SWPlugin(injector: HasAndroidInjector, val definition: SWDefinition) : SWItem(injector, Type.PLUGIN) { @Inject lateinit var pluginStore: PluginStore @Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin @@ -25,6 +28,8 @@ class SWPlugin(injector: HasAndroidInjector) : SWItem(injector, Type.PLUGIN) { private var pluginDescription = 0 private var makeVisible = true + private var fragment: MyPreferenceFragment? = null + fun option(pType: PluginType, pluginDescription: Int): SWPlugin { this.pType = pType this.pluginDescription = pluginDescription @@ -37,26 +42,30 @@ class SWPlugin(injector: HasAndroidInjector) : SWItem(injector, Type.PLUGIN) { } override fun generateDialog(layout: LinearLayout) { + var selectedPlugin: PluginBase? = null val context = layout.context radioGroup = RadioGroup(context) radioGroup?.clearCheck() val pluginsInCategory = pluginStore.getSpecificPluginsList(pType!!) radioGroup?.orientation = LinearLayout.VERTICAL radioGroup?.visibility = View.VISIBLE - val pdesc = TextView(context) - pdesc.setText(pluginDescription) + val pDesc = TextView(context) + pDesc.setText(pluginDescription) var params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) params.setMargins(0, 0, 0, 40) - pdesc.layoutParams = params - layout.addView(pdesc) + pDesc.layoutParams = params + layout.addView(pDesc) for (i in pluginsInCategory.indices) { - val rdbtn = RadioButton(context) + val rdBtn = RadioButton(context) val p = pluginsInCategory[i] - rdbtn.id = View.generateViewId() - rdbtn.text = p.name - if (p.isEnabled(pType!!)) rdbtn.isChecked = true - rdbtn.tag = p - radioGroup?.addView(rdbtn) + rdBtn.id = View.generateViewId() + rdBtn.text = p.name + if (p.isEnabled(pType!!)) { + rdBtn.isChecked = true + selectedPlugin = p + } + rdBtn.tag = p + radioGroup?.addView(rdBtn) params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) params.setMargins(80, 0, 0, 0) val desc = TextView(context) @@ -73,8 +82,27 @@ class SWPlugin(injector: HasAndroidInjector) : SWItem(injector, Type.PLUGIN) { configBuilderPlugin.storeSettings("SetupWizard") rxBus.send(EventConfigBuilderChange()) rxBus.send(EventSWUpdate(false)) + addConfiguration(layout, plugin) } layout.addView(radioGroup) + selectedPlugin?.let { addConfiguration(layout, it) } super.generateDialog(layout) } + + fun addConfiguration(layout: LinearLayout, plugin: PluginBase) { + if (plugin.preferencesId != -1) { + fragment = MyPreferenceFragment() + fragment?.arguments = Bundle().also { it.putInt("id", plugin.preferencesId) } + definition.activity.supportFragmentManager.beginTransaction().run { + replace(layout.id, fragment!!) + commit() + } + } else { + definition.activity.supportFragmentManager.beginTransaction().run { + if (fragment != null) remove(fragment!!) + fragment = null + commit() + } + } + } } \ No newline at end of file