From 5b345c5ce355f43112cbb058436a777b4fa95293 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 13 Dec 2019 13:12:36 +0100 Subject: [PATCH] migrate units from last profile --- .../nightscout/androidaps/MainActivity.java | 15 +-------- .../info/nightscout/androidaps/MainApp.java | 31 +++++++++++++++++++ .../androidaps/utils/AndroidPermission.java | 4 ++- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index ca6345b337..c0ebb847b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -92,8 +92,6 @@ public class MainActivity extends NoSplashAppCompatActivity { // initialize screen wake lock processPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on)); - doMigrations(); - final ViewPager viewPager = findViewById(R.id.pager); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override @@ -139,7 +137,7 @@ public class MainActivity extends NoSplashAppCompatActivity { .subscribe(this::processPreferenceChange, FabricPrivacy::logException) ); - if (!SP.getBoolean(R.string.key_setupwizard_processed, false) || !SP.contains(R.string.key_units)) { + if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) { Intent intent = new Intent(this, SetupWizardActivity.class); startActivity(intent); } @@ -233,17 +231,6 @@ public class MainActivity extends NoSplashAppCompatActivity { } } - private void doMigrations() { - - // guarantee that the unreachable threshold is at least 30 and of type String - // Added in 1.57 at 21.01.2018 - int unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30); - SP.remove(R.string.key_pump_unreachable_threshold); - if (unreachable_threshold < 30) unreachable_threshold = 30; - SP.putString(R.string.key_pump_unreachable_threshold, Integer.toString(unreachable_threshold)); - } - - @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 4802655d3a..5173377b33 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -15,6 +15,7 @@ import com.j256.ormlite.android.apptools.OpenHelperManager; import net.danlew.android.joda.JodaTimeAndroid; +import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,6 +23,7 @@ import java.io.File; import java.util.ArrayList; import info.nightscout.androidaps.data.ConstraintChecker; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; @@ -32,6 +34,7 @@ import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.constraints.dstHelper.DstHelperPlugin; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin; @@ -90,6 +93,7 @@ import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver; import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.LocaleHelper; +import info.nightscout.androidaps.utils.SP; import io.fabric.sdk.android.Fabric; import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; @@ -243,8 +247,35 @@ public class MainApp extends Application { startKeepAliveService(); }).start(); } + + doMigrations(); } + private void doMigrations() { + + // guarantee that the unreachable threshold is at least 30 and of type String + // Added in 1.57 at 21.01.2018 + int unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30); + SP.remove(R.string.key_pump_unreachable_threshold); + if (unreachable_threshold < 30) unreachable_threshold = 30; + SP.putString(R.string.key_pump_unreachable_threshold, Integer.toString(unreachable_threshold)); + + // 2.5 -> 2.6 + if (!SP.contains(R.string.key_units)) { + String newUnits = Constants.MGDL; + Profile p = ProfileFunctions.getInstance().getProfile(); + if (p != null && p.getData() != null && p.getData().has("units")) { + try { + newUnits = p.getData().getString("units"); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + } + SP.putString(R.string.key_units, newUnits); + } + } + + private void registerLocalBroadcastReceiver() { lbm = LocalBroadcastManager.getInstance(this); lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT)); diff --git a/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java b/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java index 9f7986cb97..fdee5417a3 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java @@ -17,11 +17,13 @@ import androidx.core.content.ContextCompat; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.bus.RxBus; 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.overview.notifications.NotificationWithAction; +import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; public class AndroidPermission { @@ -79,7 +81,7 @@ public class AndroidPermission { } public static synchronized void notifyForSMSPermissions(Activity activity) { - if (SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)) { + if (SmsCommunicatorPlugin.INSTANCE.isEnabled(PluginType.GENERAL)) { if (permissionNotGranted(activity, Manifest.permission.RECEIVE_SMS)) { NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_SMS, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.URGENT); notification.action(R.string.request, () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS,