diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 360898ee00..2c8a56bdd7 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -335,7 +335,7 @@ public class MainActivity extends AppCompatActivity { case AndroidPermission.CASE_LOCATION: case AndroidPermission.CASE_SMS: case AndroidPermission.CASE_BATTERY: - case AndroidPermission.CASE_PHONESTATE: + case AndroidPermission.CASE_PHONE_STATE: break; } } 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 777463583d..3e2c638363 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java @@ -3,6 +3,8 @@ package info.nightscout.androidaps.setupwizard; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; + +import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; @@ -223,4 +225,10 @@ public class SetupWizardActivity extends AppCompatActivity { updateButtons(); } + @Override + protected 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 f7b50a0f70..7963ae0545 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.utils; import android.Manifest; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -26,15 +27,16 @@ public class AndroidPermission { public static final int CASE_SMS = 0x2; public static final int CASE_LOCATION = 0x3; public static final int CASE_BATTERY = 0x4; - public static final int CASE_PHONESTATE = 0x5; + public static final int CASE_PHONE_STATE = 0x5; - public static void askForPermission(Activity activity, String[] permission, Integer requestCode) { + @SuppressLint("BatteryLife") + private static void askForPermission(Activity activity, String[] permission, Integer requestCode) { boolean test = false; boolean testBattery = false; - for (int i = 0; i < permission.length; i++) { - test = test || (ContextCompat.checkSelfPermission(activity, permission[i]) != PackageManager.PERMISSION_GRANTED); - if (permission[i] == Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) { - PowerManager powerManager = (PowerManager) activity.getSystemService(activity.POWER_SERVICE); + for (String s : permission) { + test = test || (ContextCompat.checkSelfPermission(activity, s) != PackageManager.PERMISSION_GRANTED); + if (s.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) { + PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE); String packageName = activity.getPackageName(); testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName); } @@ -46,7 +48,7 @@ public class AndroidPermission { Intent i = new Intent(); i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); i.setData(Uri.parse("package:" + activity.getPackageName())); - activity.startActivity(i); + activity.startActivityForResult(i, CASE_BATTERY); } } @@ -56,13 +58,13 @@ public class AndroidPermission { } public static boolean permissionNotGranted(Context context, String permission) { - if (permission == Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) { + 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(); - return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED - || !powerManager.isIgnoringBatteryOptimizations(packageName); + selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName); } - return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED; + return !selfCheck; } public static synchronized void notifyForSMSPermissions(Activity activity) { @@ -80,7 +82,7 @@ public class AndroidPermission { if (permissionNotGranted(activity, Manifest.permission.READ_PHONE_STATE)) { NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT); notification.action(MainApp.gs(R.string.request), () -> - AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AndroidPermission.CASE_PHONESTATE)); + AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AndroidPermission.CASE_PHONE_STATE)); MainApp.bus().post(new EventNewNotification(notification)); } else MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_PHONESTATE)); @@ -89,36 +91,30 @@ public class AndroidPermission { } public static synchronized void notifyForBatteryOptimizationPermission(Activity activity) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) { - NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_BATTERY, String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)), Notification.URGENT); - notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY)); - MainApp.bus().post(new EventNewNotification(notification)); - } else - MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY)); - } + if (permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) { + NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_BATTERY, String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)), Notification.URGENT); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY)); } public static synchronized void notifyForStoragePermission(Activity activity) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { - NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT); - notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, - Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE)); - MainApp.bus().post(new EventNewNotification(notification)); - } else - MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE)); - } + if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE)); } public static synchronized void notifyForLocationPermissions(Activity activity) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) { - NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT); - notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION)); - MainApp.bus().post(new EventNewNotification(notification)); - } else - MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION)); - } + if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) { + NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION)); } }