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

Commit 4d64c093 authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Added an api to query ForcedAppStandby state

Adding an api which apps can query to check if the user has put them
into forced app standby.
An app may want to do this to manage expectations for any jobs or alarms
it schedules. It can also be used as an indication that the user noticed
unreasonable battery consumption attributed to the app.

Test: atest android.app.cts.ActivityManagerTest#testIsBackgroundRestricted

Bug: 73664209
Change-Id: I870f6c852c500769d3bf99d5a9ba3bf4eb1b65f5
parent f3537c20
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3882,6 +3882,7 @@ package android.app {
    method public android.app.PendingIntent getRunningServiceControlPanel(android.content.ComponentName) throws java.lang.SecurityException;
    method public deprecated java.util.List<android.app.ActivityManager.RunningServiceInfo> getRunningServices(int) throws java.lang.SecurityException;
    method public deprecated java.util.List<android.app.ActivityManager.RunningTaskInfo> getRunningTasks(int) throws java.lang.SecurityException;
    method public boolean isBackgroundRestricted();
    method public deprecated boolean isInLockTaskMode();
    method public boolean isLowRamDevice();
    method public static boolean isRunningInTestHarness();
+22 −0
Original line number Diff line number Diff line
@@ -3334,6 +3334,28 @@ public class ActivityManager {
        }
    }

    /**
     * Query whether the user has enabled background restrictions for this app.
     *
     * <p> The user may chose to do this, if they see that an app is consuming an unreasonable
     * amount of battery while in the background. </p>
     *
     * <p> If true, any work that the app tries to do will be aggressively restricted while it is in
     * the background. At a minimum, jobs and alarms will not execute and foreground services
     * cannot be started unless an app activity is in the foreground. </p>
     *
     * <p><b> Note that these restrictions stay in effect even when the device is charging.</b></p>
     *
     * @return true if user has enforced background restrictions for this app, false otherwise.
     */
    public boolean isBackgroundRestricted() {
        try {
            return getService().isBackgroundRestricted(mContext.getOpPackageName());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets the memory trim mode for a process and schedules a memory trim operation.
     *
+1 −1
Original line number Diff line number Diff line
@@ -614,7 +614,7 @@ interface IActivityManager {
    int sendIntentSender(in IIntentSender target, in IBinder whitelistToken, int code,
            in Intent intent, in String resolvedType, in IIntentReceiver finishedReceiver,
            in String requiredPermission, in Bundle options);

    boolean isBackgroundRestricted(in String packageName);

    // Start of N MR1 transactions
    void setVrThread(int tid);
+19 −0
Original line number Diff line number Diff line
@@ -12855,6 +12855,25 @@ public class ActivityManagerService extends IActivityManager.Stub
        return false;
    }
    @Override
    public boolean isBackgroundRestricted(String packageName) {
        final int callingUid = Binder.getCallingUid();
        final IPackageManager pm = AppGlobals.getPackageManager();
        try {
            final int packageUid = pm.getPackageUid(packageName, MATCH_DEBUG_TRIAGED_MISSING,
                    UserHandle.getUserId(callingUid));
            if (packageUid != callingUid) {
                throw new IllegalArgumentException("Uid " + callingUid
                        + " cannot query restriction state for package " + packageName);
            }
        } catch (RemoteException exc) {
            // Ignore.
        }
        final int mode = mAppOpsService.checkOperation(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND,
                callingUid, packageName);
        return (mode != AppOpsManager.MODE_ALLOWED);
    }
    @Override
    public void backgroundWhitelistUid(final int uid) {
        if (Binder.getCallingUid() != Process.SYSTEM_UID) {