handle callback from ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

This commit is contained in:
Milos Kozak 2019-08-03 20:01:55 +02:00
parent 81eeb66538
commit 93ecd43e2e
3 changed files with 42 additions and 38 deletions

View file

@ -335,7 +335,7 @@ public class MainActivity extends AppCompatActivity {
case AndroidPermission.CASE_LOCATION: case AndroidPermission.CASE_LOCATION:
case AndroidPermission.CASE_SMS: case AndroidPermission.CASE_SMS:
case AndroidPermission.CASE_BATTERY: case AndroidPermission.CASE_BATTERY:
case AndroidPermission.CASE_PHONESTATE: case AndroidPermission.CASE_PHONE_STATE:
break; break;
} }
} }

View file

@ -3,6 +3,8 @@ package info.nightscout.androidaps.setupwizard;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -223,4 +225,10 @@ public class SetupWizardActivity extends AppCompatActivity {
updateButtons(); updateButtons();
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AndroidPermission.CASE_BATTERY)
updateButtons();
}
} }

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.utils; package info.nightscout.androidaps.utils;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -26,15 +27,16 @@ public class AndroidPermission {
public static final int CASE_SMS = 0x2; public static final int CASE_SMS = 0x2;
public static final int CASE_LOCATION = 0x3; public static final int CASE_LOCATION = 0x3;
public static final int CASE_BATTERY = 0x4; 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 test = false;
boolean testBattery = false; boolean testBattery = false;
for (int i = 0; i < permission.length; i++) { for (String s : permission) {
test = test || (ContextCompat.checkSelfPermission(activity, permission[i]) != PackageManager.PERMISSION_GRANTED); test = test || (ContextCompat.checkSelfPermission(activity, s) != PackageManager.PERMISSION_GRANTED);
if (permission[i] == Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) { if (s.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
PowerManager powerManager = (PowerManager) activity.getSystemService(activity.POWER_SERVICE); PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
String packageName = activity.getPackageName(); String packageName = activity.getPackageName();
testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName); testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName);
} }
@ -46,7 +48,7 @@ public class AndroidPermission {
Intent i = new Intent(); Intent i = new Intent();
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + activity.getPackageName())); 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) { 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); PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
String packageName = context.getPackageName(); String packageName = context.getPackageName();
return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName);
|| !powerManager.isIgnoringBatteryOptimizations(packageName);
} }
return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED; return !selfCheck;
} }
public static synchronized void notifyForSMSPermissions(Activity activity) { public static synchronized void notifyForSMSPermissions(Activity activity) {
@ -80,7 +82,7 @@ public class AndroidPermission {
if (permissionNotGranted(activity, Manifest.permission.READ_PHONE_STATE)) { if (permissionNotGranted(activity, Manifest.permission.READ_PHONE_STATE)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT); NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT);
notification.action(MainApp.gs(R.string.request), () -> 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)); MainApp.bus().post(new EventNewNotification(notification));
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_PHONESTATE)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_PHONESTATE));
@ -89,7 +91,6 @@ public class AndroidPermission {
} }
public static synchronized void notifyForBatteryOptimizationPermission(Activity activity) { 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)) { 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); 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)); notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY));
@ -97,10 +98,8 @@ public class AndroidPermission {
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY));
} }
}
public static synchronized void notifyForStoragePermission(Activity activity) { public static synchronized void notifyForStoragePermission(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT); 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, notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
@ -109,10 +108,8 @@ public class AndroidPermission {
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE));
} }
}
public static synchronized void notifyForLocationPermissions(Activity activity) { public static synchronized void notifyForLocationPermissions(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) { if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT); 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)); notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION));
@ -121,4 +118,3 @@ public class AndroidPermission {
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION));
} }
} }
}