Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c660397 authored by Jing Ji's avatar Jing Ji
Browse files

Post notification on abusive background current drain

Tapping on the notification will bring up the individual abusive
app's battery settings page. It won't be re-posted after being
dismissed until next day, in order to avoid from spamming the user.

Bug: 200326767
Bug: 203105544
Test: FrameworksMockingServicesTests:BackgroundRestrictionTest
Test: Manual - adb shell am set-bg-abusive-uids & verify notification
Change-Id: I0b30014d748863c66c3845b5f310948a9493e302
parent b5f28a5e
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ public class SystemNotificationChannels {
    public static String DO_NOT_DISTURB = "DO_NOT_DISTURB";
    public static String ACCESSIBILITY_MAGNIFICATION = "ACCESSIBILITY_MAGNIFICATION";
    public static String ACCESSIBILITY_SECURITY_POLICY = "ACCESSIBILITY_SECURITY_POLICY";
    public static String ABUSIVE_BACKGROUND_APPS = "ABUSIVE_BACKGROUND_APPS";

    public static void createAll(Context context) {
        final NotificationManager nm = context.getSystemService(NotificationManager.class);
@@ -209,6 +210,12 @@ public class SystemNotificationChannels {
                NotificationManager.IMPORTANCE_LOW);
        channelsList.add(accessibilitySecurityPolicyChannel);

        final NotificationChannel abusiveBackgroundAppsChannel = new NotificationChannel(
                ABUSIVE_BACKGROUND_APPS,
                context.getString(R.string.notification_channel_abusive_bg_apps),
                NotificationManager.IMPORTANCE_LOW);
        channelsList.add(abusiveBackgroundAppsChannel);

        nm.createNotificationChannels(channelsList);
    }

+9 −0
Original line number Diff line number Diff line
@@ -6199,4 +6199,13 @@ ul.</string>
    <string name="ui_translation_accessibility_translated_text"><xliff:g id="message" example="Hello">%1$s</xliff:g> Translated.</string>
    <!-- Accessibility message announced to notify the user when the system has finished translating the content displayed on the screen to a different language after the user requested translation. [CHAR LIMIT=NONE] -->
    <string name="ui_translation_accessibility_translation_finished">Message translated from <xliff:g id="from_language" example="English">%1$s</xliff:g> to <xliff:g id="to_language" example="French">%2$s</xliff:g>.</string>

    <!-- Title for the notification channel notifying user of abusive background apps. [CHAR LIMIT=NONE] -->
    <string name="notification_channel_abusive_bg_apps">Background Activity</string>
    <!-- Title of notification indicating abusive background apps. [CHAR LIMIT=NONE] -->
    <string name="notification_title_abusive_bg_apps">Background Activity</string>
    <!-- Content of notification indicating abusive background apps. [CHAR LIMIT=NONE] -->
    <string name="notification_content_abusive_bg_apps">
        <xliff:g id="app" example="Gmail">%1$s</xliff:g> is running in the background and draining battery. Tap to review.
    </string>
</resources>
+4 −0
Original line number Diff line number Diff line
@@ -4669,4 +4669,8 @@
  <java-symbol type="string" name="config_deviceManagerUpdater" />

  <java-symbol type="string" name="config_deviceSpecificDeviceStatePolicyProvider" />

  <java-symbol type="string" name="notification_channel_abusive_bg_apps"/>
  <java-symbol type="string" name="notification_title_abusive_bg_apps"/>
  <java-symbol type="string" name="notification_content_abusive_bg_apps"/>
</resources>
+6 −0
Original line number Diff line number Diff line
@@ -362,5 +362,11 @@ message SystemMessage {
    // Notify the user that some accessibility service has view and control permissions.
    // package: android
    NOTE_A11Y_VIEW_AND_CONTROL_ACCESS = 1005;

    // Notify the user an abusive background app has been detected.
    // Package: android
    // Note: this is a base ID, multiple notifications will be posted for each
    // abusive apps, with notification ID based off this ID.
    NOTE_ABUSIVE_BG_APPS_BASE = 0xc1b2508; // 203105544
  }
}
+41 −0
Original line number Diff line number Diff line
@@ -336,6 +336,8 @@ final class ActivityManagerShellCommand extends ShellCommand {
                    return runGetIsolatedProcesses(pw);
                case "set-stop-user-on-switch":
                    return runSetStopUserOnSwitch(pw);
                case "set-bg-abusive-uids":
                    return runSetBgAbusiveUids(pw);
                default:
                    return handleDefaultCommands(cmd);
            }
@@ -3215,6 +3217,43 @@ final class ActivityManagerShellCommand extends ShellCommand {
        return 0;
    }

    // TODO(b/203105544) STOPSHIP - For debugging only, to be removed before shipping.
    private int runSetBgAbusiveUids(PrintWriter pw) throws RemoteException {
        final String arg = getNextArg();
        final AppBatteryTracker batteryTracker =
                mInternal.mAppRestrictionController.getAppStateTracker(AppBatteryTracker.class);
        if (batteryTracker == null) {
            getErrPrintWriter().println("Unable to get bg battery tracker");
            return -1;
        }
        if (arg == null) {
            batteryTracker.mDebugUidPercentages.clear();
            return 0;
        }
        String[] pairs = arg.split(",");
        int[] uids = new int[pairs.length];
        double[] values = new double[pairs.length];
        try {
            for (int i = 0; i < pairs.length; i++) {
                String[] pair = pairs[i].split("=");
                if (pair.length != 2) {
                    getErrPrintWriter().println("Malformed input");
                    return -1;
                }
                uids[i] = Integer.parseInt(pair[0]);
                values[i] = Double.parseDouble(pair[1]);
            }
        } catch (NumberFormatException e) {
            getErrPrintWriter().println("Malformed input");
            return -1;
        }
        batteryTracker.mDebugUidPercentages.clear();
        for (int i = 0; i < pairs.length; i++) {
            batteryTracker.mDebugUidPercentages.put(uids[i], values[i]);
        }
        return 0;
    }

    private Resources getResources(PrintWriter pw) throws RemoteException {
        // system resources does not contain all the device configuration, construct it manually.
        Configuration config = mInterface.getConfiguration();
@@ -3551,6 +3590,8 @@ final class ActivityManagerShellCommand extends ShellCommand {
            pw.println("         Sets whether the current user (and its profiles) should be stopped"
                    + " when switching to a different user.");
            pw.println("         Without arguments, it resets to the value defined by platform.");
            pw.println("  set-bg-abusive-uids [uid=percentage][,uid=percentage...]");
            pw.println("         Force setting the battery usage of the given UID.");
            pw.println();
            Intent.printIntentArgsHelp(pw, "");
        }
Loading