Add disabling battery optimizations to permissions screen

This commit is contained in:
Arjan Schrijver 2025-01-03 15:22:23 +01:00
parent 4ccf68af0a
commit 790e81a6f6
3 changed files with 35 additions and 1 deletions

View File

@ -38,6 +38,13 @@
<!-- Take wake locks (e.g. for time sync) --> <!-- Take wake locks (e.g. for time sync) -->
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<!--
Necessary for asking the user to disable battery optimizations.
GB falls under the acceptable use cases documented here:
https://developer.android.com/training/monitoring-device-state/doze-standby.html#exemption-cases
-->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<!-- Read loyalty cards from Catima --> <!-- Read loyalty cards from Catima -->
<uses-permission android:name="me.hackerchick.catima.READ_CARDS"/> <uses-permission android:name="me.hackerchick.catima.READ_CARDS"/>
<uses-permission android:name="me.hackerchick.catima.debug.READ_CARDS"/> <uses-permission android:name="me.hackerchick.catima.debug.READ_CARDS"/>

View File

@ -17,6 +17,7 @@
package nodomain.freeyourgadget.gadgetbridge.util; package nodomain.freeyourgadget.gadgetbridge.util;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
@ -28,6 +29,7 @@ import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Toast; import android.widget.Toast;
@ -52,11 +54,15 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
public class PermissionsUtils { public class PermissionsUtils {
private static final Logger LOG = LoggerFactory.getLogger(PermissionsUtils.class); private static final Logger LOG = LoggerFactory.getLogger(PermissionsUtils.class);
public static final String CUSTOM_PERM_IGNORE_BATT_OPTIM = "custom_perm_ignore_battery_optimization";
public static final String CUSTOM_PERM_NOTIFICATION_LISTENER = "custom_perm_notifications_listener"; public static final String CUSTOM_PERM_NOTIFICATION_LISTENER = "custom_perm_notifications_listener";
public static final String CUSTOM_PERM_NOTIFICATION_SERVICE = "custom_perm_notifications_service"; public static final String CUSTOM_PERM_NOTIFICATION_SERVICE = "custom_perm_notifications_service";
public static final String CUSTOM_PERM_DISPLAY_OVER = "custom_perm_display_over"; public static final String CUSTOM_PERM_DISPLAY_OVER = "custom_perm_display_over";
public static final List<String> specialPermissions = new ArrayList<String>() {{ public static final List<String> specialPermissions = new ArrayList<String>() {{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
add(CUSTOM_PERM_IGNORE_BATT_OPTIM);
}
add(CUSTOM_PERM_NOTIFICATION_LISTENER); add(CUSTOM_PERM_NOTIFICATION_LISTENER);
add(CUSTOM_PERM_NOTIFICATION_SERVICE); add(CUSTOM_PERM_NOTIFICATION_SERVICE);
add(CUSTOM_PERM_DISPLAY_OVER); add(CUSTOM_PERM_DISPLAY_OVER);
@ -68,6 +74,12 @@ public class PermissionsUtils {
public static ArrayList<PermissionDetails> getRequiredPermissionsList(Activity activity) { public static ArrayList<PermissionDetails> getRequiredPermissionsList(Activity activity) {
ArrayList<PermissionDetails> permissionsList = new ArrayList<>(); ArrayList<PermissionDetails> permissionsList = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permissionsList.add(new PermissionDetails(
CUSTOM_PERM_IGNORE_BATT_OPTIM,
activity.getString(R.string.permission_disable_doze_title),
activity.getString(R.string.permission_disable_doze_summary)));
}
permissionsList.add(new PermissionDetails( permissionsList.add(new PermissionDetails(
CUSTOM_PERM_NOTIFICATION_LISTENER, CUSTOM_PERM_NOTIFICATION_LISTENER,
activity.getString(R.string.menuitem_notifications), activity.getString(R.string.menuitem_notifications),
@ -189,6 +201,9 @@ public class PermissionsUtils {
return ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).isNotificationPolicyAccessGranted(); return ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).isNotificationPolicyAccessGranted();
} else if (permission.equals(CUSTOM_PERM_DISPLAY_OVER) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { } else if (permission.equals(CUSTOM_PERM_DISPLAY_OVER) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(context); return Settings.canDrawOverlays(context);
} else if (permission.equals(CUSTOM_PERM_IGNORE_BATT_OPTIM) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isIgnoringBatteryOptimizations(context.getApplicationContext().getPackageName());
} else { } else {
return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_DENIED; return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_DENIED;
} }
@ -205,7 +220,9 @@ public class PermissionsUtils {
} }
public static void requestPermission(Activity activity, String permission) { public static void requestPermission(Activity activity, String permission) {
if (permission.equals(CUSTOM_PERM_NOTIFICATION_LISTENER)) { if (permission.equals(CUSTOM_PERM_IGNORE_BATT_OPTIM)) {
showRequestIgnoreBatteryOptimizationDialog(activity);
} else if (permission.equals(CUSTOM_PERM_NOTIFICATION_LISTENER)) {
showNotifyListenerPermissionsDialog(activity); showNotifyListenerPermissionsDialog(activity);
} else if (permission.equals(CUSTOM_PERM_NOTIFICATION_SERVICE) && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)) { } else if (permission.equals(CUSTOM_PERM_NOTIFICATION_SERVICE) && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)) {
showNotifyPolicyPermissionsDialog(activity); showNotifyPolicyPermissionsDialog(activity);
@ -242,6 +259,14 @@ public class PermissionsUtils {
} }
} }
@SuppressLint("BatteryLife")
private static void showRequestIgnoreBatteryOptimizationDialog(Activity activity) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + activity.getApplicationContext().getPackageName()));
activity.startActivity(intent);
}
private static void showNotifyListenerPermissionsDialog(Activity activity) { private static void showNotifyListenerPermissionsDialog(Activity activity) {
new MaterialAlertDialogBuilder(activity) new MaterialAlertDialogBuilder(activity)
.setMessage(activity.getString(R.string.permission_notification_listener, .setMessage(activity.getString(R.string.permission_notification_listener,

View File

@ -3584,4 +3584,6 @@
<string name="battery_allow_pass_though_summary">When enabled, the battery can be charged while discharging</string> <string name="battery_allow_pass_though_summary">When enabled, the battery can be charged while discharging</string>
<string name="battery_allow_pass_through">Allow battery pass-through</string> <string name="battery_allow_pass_through">Allow battery pass-through</string>
<string name="canned_replies_not_empty">There should be at least one canned reply.</string> <string name="canned_replies_not_empty">There should be at least one canned reply.</string>
<string name="permission_disable_doze_title">Ignore battery optimizations</string>
<string name="permission_disable_doze_summary">Allows running in the background unhindered by Android\'s battery optimizations</string>
</resources> </resources>