Check we have battery optimisation with PowerManager (required in addition to REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission)
This commit is contained in:
parent
b3dde001d8
commit
246186f03f
1 changed files with 24 additions and 3 deletions
|
@ -3,8 +3,13 @@ package info.nightscout.androidaps.utils;
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
@ -25,22 +30,38 @@ public class AndroidPermission {
|
||||||
|
|
||||||
public static void askForPermission(Activity activity, String[] permission, Integer requestCode) {
|
public static void askForPermission(Activity activity, String[] permission, Integer requestCode) {
|
||||||
boolean test = false;
|
boolean test = false;
|
||||||
|
boolean testBattery = false;
|
||||||
for (int i = 0; i < permission.length; i++) {
|
for (int i = 0; i < permission.length; i++) {
|
||||||
test = test || (ContextCompat.checkSelfPermission(activity, permission[i]) != PackageManager.PERMISSION_GRANTED);
|
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);
|
||||||
|
String packageName = activity.getPackageName();
|
||||||
|
testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (test) {
|
if (test) {
|
||||||
ActivityCompat.requestPermissions(activity, permission, requestCode);
|
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.startActivity(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void askForPermission(Activity activity, String permission, Integer requestCode) {
|
public static void askForPermission(Activity activity, String permission, Integer requestCode) {
|
||||||
String[] permissions = {permission};
|
String[] permissions = {permission};
|
||||||
|
askForPermission(activity, permissions, requestCode);
|
||||||
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED)
|
|
||||||
ActivityCompat.requestPermissions(activity, permissions, requestCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean checkForPermission(Context context, String permission) {
|
public static boolean checkForPermission(Context context, String permission) {
|
||||||
|
if (permission == 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);
|
||||||
|
}
|
||||||
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue