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

Commit 1f1de70f authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Exposing canSuspendPackage as SystemApi

Exposing canSuspendPackage so apps with SUSPEND_APPS can query whether a
package is considered too critical to suspend by the system.

Test: atest GtsSuspendAppsTestCases

Fixes: 117968270
Change-Id: I9c316dae7e7a7259d28e271c3901465244d6c93d
parent 3c91545b
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -1205,6 +1205,7 @@ package android.content.pm {
  public abstract class PackageManager {
  public abstract class PackageManager {
    method public abstract void addOnPermissionsChangeListener(android.content.pm.PackageManager.OnPermissionsChangedListener);
    method public abstract void addOnPermissionsChangeListener(android.content.pm.PackageManager.OnPermissionsChangedListener);
    method public abstract boolean arePermissionsIndividuallyControlled();
    method public abstract boolean arePermissionsIndividuallyControlled();
    method public boolean canSuspendPackage(java.lang.String);
    method public abstract java.util.List<android.content.IntentFilter> getAllIntentFilters(java.lang.String);
    method public abstract java.util.List<android.content.IntentFilter> getAllIntentFilters(java.lang.String);
    method public android.content.pm.dex.ArtManager getArtManager();
    method public android.content.pm.dex.ArtManager getArtManager();
    method public abstract java.lang.String getDefaultBrowserPackageNameAsUser(int);
    method public abstract java.lang.String getDefaultBrowserPackageNameAsUser(int);
+9 −0
Original line number Original line Diff line number Diff line
@@ -2276,6 +2276,15 @@ public class ApplicationPackageManager extends PackageManager {
        }
        }
    }
    }


    @Override
    public boolean canSuspendPackage(String packageName) {
        try {
            return mPM.canSuspendPackageForUser(packageName, mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    @Override
    @Override
    public Bundle getSuspendedPackageAppExtras() {
    public Bundle getSuspendedPackageAppExtras() {
        final PersistableBundle extras;
        final PersistableBundle extras;
+2 −0
Original line number Original line Diff line number Diff line
@@ -276,6 +276,8 @@ interface IPackageManager {
            in PersistableBundle appExtras, in PersistableBundle launcherExtras,
            in PersistableBundle appExtras, in PersistableBundle launcherExtras,
            in SuspendDialogInfo dialogInfo, String callingPackage, int userId);
            in SuspendDialogInfo dialogInfo, String callingPackage, int userId);


    boolean canSuspendPackageForUser(String packageName, int userId);

    boolean isPackageSuspendedForUser(String packageName, int userId);
    boolean isPackageSuspendedForUser(String packageName, int userId);


    PersistableBundle getSuspendedPackageAppExtras(String packageName, int userId);
    PersistableBundle getSuspendedPackageAppExtras(String packageName, int userId);
+24 −0
Original line number Original line Diff line number Diff line
@@ -5796,6 +5796,30 @@ public abstract class PackageManager {
        throw new UnsupportedOperationException("setPackagesSuspended not implemented");
        throw new UnsupportedOperationException("setPackagesSuspended not implemented");
    }
    }


    /**
     * Returns whether or not a given package can be suspended via a call to {@link
     * #setPackagesSuspended(String[], boolean, PersistableBundle, PersistableBundle,
     * SuspendDialogInfo) setPackagesSuspended}. The platform prevents suspending certain critical
     * packages to keep the device in a functioning state, e.g. the default dialer.
     * Apps need to hold {@link Manifest.permission#SUSPEND_APPS SUSPEND_APPS} to call this api.
     *
     * <p>
     * Note that this set of critical packages can change with time, so <em>a value of {@code true}
     * returned by this api does not guarantee that a following call to {@link
     * #setPackagesSuspended(String[], boolean, PersistableBundle, PersistableBundle,
     * SuspendDialogInfo) setPackagesSuspended} for the same package will succeed</em>, especially
     * if considerable time elapsed between the two calls.
     *
     * @param packageName The package to check.
     * @return {@code true} if the given package can be suspended, {@code false} otherwise.
     * @hide
     */
    @SystemApi
    @RequiresPermission(Manifest.permission.SUSPEND_APPS)
    public boolean canSuspendPackage(@NonNull String packageName) {
        throw new UnsupportedOperationException("canSuspendPackage not implemented");
    }

    /**
    /**
     * @see #setPackagesSuspended(String[], boolean, PersistableBundle, PersistableBundle, String)
     * @see #setPackagesSuspended(String[], boolean, PersistableBundle, PersistableBundle, String)
     * @param packageName The name of the package to get the suspended status of.
     * @param packageName The name of the package to get the suspended status of.
+20 −1
Original line number Original line Diff line number Diff line
@@ -12928,6 +12928,25 @@ public class PackageManagerService extends IPackageManager.Stub
        }
        }
    }
    }
    @Override
    public boolean canSuspendPackageForUser(String packageName, int userId) {
        mContext.enforceCallingOrSelfPermission(Manifest.permission.SUSPEND_APPS,
                "canSuspendPackageForUser");
        final int callingUid = Binder.getCallingUid();
        if (UserHandle.getUserId(callingUid) != userId) {
            throw new SecurityException("Calling uid " + callingUid
                    + " cannot query canSuspendPackageForUser for user " + userId);
        }
        final long identity = Binder.clearCallingIdentity();
        try {
            synchronized (mPackages) {
                return canSuspendPackageForUserLocked(packageName, userId);
            }
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }
    @GuardedBy("mPackages")
    @GuardedBy("mPackages")
    private boolean canSuspendPackageForUserLocked(String packageName, int userId) {
    private boolean canSuspendPackageForUserLocked(String packageName, int userId) {
        if (isPackageDeviceAdmin(packageName, userId)) {
        if (isPackageDeviceAdmin(packageName, userId)) {
@@ -12991,7 +13010,7 @@ public class PackageManagerService extends IPackageManager.Stub
        }
        }
        if (PLATFORM_PACKAGE_NAME.equals(packageName)) {
        if (PLATFORM_PACKAGE_NAME.equals(packageName)) {
            Slog.w(TAG, "Cannot suspend package: " + packageName);
            Slog.w(TAG, "Cannot suspend the platform package: " + packageName);
            return false;
            return false;
        }
        }