From 450a25c3b847c3eea1a2c607efe17b52b891643d Mon Sep 17 00:00:00 2001 From: Brian Quinion Date: Wed, 21 Aug 2019 17:38:01 +0100 Subject: [PATCH 1/4] Allow for Battery Optimization failing on some platforms --- .../setupwizard/SetupWizardActivity.java | 2 +- .../androidaps/utils/AndroidPermission.java | 36 +++++++++++++++---- app/src/main/res/values/strings.xml | 2 ++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java index 3064228fd3..f998b343a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java @@ -226,7 +226,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { } @Override - protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == AndroidPermission.CASE_BATTERY) updateButtons(); 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 7963ae0545..42f24dff69 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java @@ -3,7 +3,9 @@ package info.nightscout.androidaps.utils; import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; @@ -11,6 +13,7 @@ import android.os.Build; import android.os.PowerManager; import android.provider.Settings; +import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -45,10 +48,26 @@ public class AndroidPermission { ActivityCompat.requestPermissions(activity, permission, requestCode); } if (testBattery) { - Intent i = new Intent(); - i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - i.setData(Uri.parse("package:" + activity.getPackageName())); - activity.startActivityForResult(i, CASE_BATTERY); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + try { + Intent i = new Intent(); + i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + i.setData(Uri.parse("package:" + activity.getPackageName())); + activity.startActivityForResult(i, CASE_BATTERY); + } catch (ActivityNotFoundException e) { + SP.putBoolean(R.string.key_permission_battery_optimization_failed, true); + + AlertDialog.Builder alert = new AlertDialog.Builder(activity); + alert.setMessage(R.string.alert_dialog_permission_battery_optimization_failed); + alert.setPositiveButton(R.string.ok, null); + alert.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + activity.recreate(); + } + }); + alert.show(); + } } } @@ -60,9 +79,12 @@ public class AndroidPermission { public static boolean permissionNotGranted(Context context, String permission) { boolean selfCheck = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; if (permission.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) { - PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - String packageName = context.getPackageName(); - selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M + && !SP.getBoolean(R.string.key_permission_battery_optimization_failed, false)) { + PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + String packageName = context.getPackageName(); + selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName); + } } return !selfCheck; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2029c7023d..6a77b5acf7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -38,6 +38,8 @@ DanaR Bluetooth device Always use basal absolute values Please reboot your phone or restart AndroidAPS from the System Settings \notherwise Android APS will not have logging (important to track and verify that the algorithms are working correctly)! + key_permission_battery_optimization_failed + This device does not appear to support battery optimization whitelisting - you may experience performance issues. Some buttons to quickly access common features Enter advanced log book entries. From d0335e56fa37ea3d7e4589d96840fedc30387df7 Mon Sep 17 00:00:00 2001 From: Brian Quinion Date: Wed, 21 Aug 2019 17:45:31 +0100 Subject: [PATCH 2/4] Allow for Battery Optimization failing on some platforms --- .../nightscout/androidaps/setupwizard/SetupWizardActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java index f998b343a2..3064228fd3 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java @@ -226,7 +226,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { } @Override - public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == AndroidPermission.CASE_BATTERY) updateButtons(); From 75be27485cf41aedf7a469a303dc07244fa3555d Mon Sep 17 00:00:00 2001 From: Brian Quinion Date: Thu, 22 Aug 2019 10:29:13 +0100 Subject: [PATCH 3/4] Typo in battery optimization code --- .../java/info/nightscout/androidaps/utils/AndroidPermission.java | 1 + 1 file changed, 1 insertion(+) 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 42f24dff69..3a9d8909b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java @@ -67,6 +67,7 @@ public class AndroidPermission { } }); alert.show(); + } } } } From 792a1d7ddde38e4bf17794b5a07d52a6daba6e9d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 24 Aug 2019 18:58:17 +0200 Subject: [PATCH 4/4] code cleanup --- .../androidaps/utils/AndroidPermission.java | 37 ++++++------------- app/src/main/res/values/strings.xml | 1 - 2 files changed, 12 insertions(+), 26 deletions(-) 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 3a9d8909b3..11d584bb87 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java @@ -5,7 +5,6 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; @@ -13,7 +12,6 @@ import android.os.Build; import android.os.PowerManager; import android.provider.Settings; -import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -32,6 +30,8 @@ public class AndroidPermission { public static final int CASE_BATTERY = 0x4; public static final int CASE_PHONE_STATE = 0x5; + private static boolean permission_battery_optimization_failed = false; + @SuppressLint("BatteryLife") private static void askForPermission(Activity activity, String[] permission, Integer requestCode) { boolean test = false; @@ -48,27 +48,15 @@ public class AndroidPermission { ActivityCompat.requestPermissions(activity, permission, requestCode); } if (testBattery) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - try { - Intent i = new Intent(); - i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - i.setData(Uri.parse("package:" + activity.getPackageName())); - activity.startActivityForResult(i, CASE_BATTERY); - } catch (ActivityNotFoundException e) { - SP.putBoolean(R.string.key_permission_battery_optimization_failed, true); - - AlertDialog.Builder alert = new AlertDialog.Builder(activity); - alert.setMessage(R.string.alert_dialog_permission_battery_optimization_failed); - alert.setPositiveButton(R.string.ok, null); - alert.setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - activity.recreate(); - } - }); - alert.show(); - } - } + try { + Intent i = new Intent(); + i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + i.setData(Uri.parse("package:" + activity.getPackageName())); + activity.startActivityForResult(i, CASE_BATTERY); + } catch (ActivityNotFoundException e) { + permission_battery_optimization_failed = true; + OKDialog.show(activity, MainApp.gs(R.string.permission), MainApp.gs(R.string.alert_dialog_permission_battery_optimization_failed), activity::recreate); + } } } @@ -80,8 +68,7 @@ public class AndroidPermission { public static boolean permissionNotGranted(Context context, String permission) { boolean selfCheck = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; if (permission.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M - && !SP.getBoolean(R.string.key_permission_battery_optimization_failed, false)) { + if (!permission_battery_optimization_failed) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); String packageName = context.getPackageName(); selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6a77b5acf7..2bb2753b66 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -38,7 +38,6 @@ DanaR Bluetooth device Always use basal absolute values Please reboot your phone or restart AndroidAPS from the System Settings \notherwise Android APS will not have logging (important to track and verify that the algorithms are working correctly)! - key_permission_battery_optimization_failed This device does not appear to support battery optimization whitelisting - you may experience performance issues. Some buttons to quickly access common features