Allow for Battery Optimization failing on some platforms

This commit is contained in:
Brian Quinion 2019-08-21 17:38:01 +01:00
parent 2f3a2116fb
commit 450a25c3b8
3 changed files with 32 additions and 8 deletions

View file

@ -226,7 +226,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity {
} }
@Override @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); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AndroidPermission.CASE_BATTERY) if (requestCode == AndroidPermission.CASE_BATTERY)
updateButtons(); updateButtons();

View file

@ -3,7 +3,9 @@ package info.nightscout.androidaps.utils;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
@ -11,6 +13,7 @@ import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -45,10 +48,26 @@ public class AndroidPermission {
ActivityCompat.requestPermissions(activity, permission, requestCode); ActivityCompat.requestPermissions(activity, permission, requestCode);
} }
if (testBattery) { if (testBattery) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
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.startActivityForResult(i, CASE_BATTERY); 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,10 +79,13 @@ public class AndroidPermission {
public static boolean permissionNotGranted(Context context, String permission) { public static boolean permissionNotGranted(Context context, String permission) {
boolean selfCheck = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; boolean selfCheck = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
if (permission.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) { 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)) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
String packageName = context.getPackageName(); String packageName = context.getPackageName();
selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName); selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName);
} }
}
return !selfCheck; return !selfCheck;
} }

View file

@ -38,6 +38,8 @@
<string name="danar_bt_name_title">DanaR Bluetooth device</string> <string name="danar_bt_name_title">DanaR Bluetooth device</string>
<string name="ns_sync_use_absolute_title">Always use basal absolute values</string> <string name="ns_sync_use_absolute_title">Always use basal absolute values</string>
<string name="alert_dialog_storage_permission_text">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)!</string> <string name="alert_dialog_storage_permission_text">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)!</string>
<string name="key_permission_battery_optimization_failed" translatable="false">key_permission_battery_optimization_failed</string>
<string name="alert_dialog_permission_battery_optimization_failed">This device does not appear to support battery optimization whitelisting - you may experience performance issues.</string>
<string name="description_actions">Some buttons to quickly access common features</string> <string name="description_actions">Some buttons to quickly access common features</string>
<string name="description_careportal">Enter advanced log book entries.</string> <string name="description_careportal">Enter advanced log book entries.</string>