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

Commit 3a288a1e authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[pm/archive] allow preinstalled launcher apps to unarchive even if they...

Merge "[pm/archive] allow preinstalled launcher apps to unarchive even if they are not default" into main
parents 00f16abc 181264cb
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -283,10 +283,7 @@ public class PackageArchiver {
            return START_CLASS_NOT_FOUND;
        }

        String currentLauncherPackageName = getCurrentLauncherPackageName(getParentUserId(userId));
        if ((currentLauncherPackageName == null || !TextUtils.equals(callerPackageName,
                currentLauncherPackageName)) && callingUid != Process.SHELL_UID) {
            // TODO(b/311619990): Remove dependency on SHELL_UID for testing
        if (!isCallerQualifiedForUnarchival(callerPackageName, callingUid, userId)) {
            Slog.e(TAG, TextUtils.formatSimple(
                    "callerPackageName: %s does not qualify for unarchival of package: " + "%s!",
                    callerPackageName, packageName));
@@ -335,6 +332,37 @@ public class PackageArchiver {
        return START_ABORTED;
    }

    private boolean isCallerQualifiedForUnarchival(String callerPackageName, int callingUid,
            int userId) {
        // TODO(b/311619990): Remove dependency on SHELL_UID for testing
        if (callingUid == Process.SHELL_UID) {
            return true;
        }
        String currentLauncherPackageName = getCurrentLauncherPackageName(getParentUserId(userId));
        if (currentLauncherPackageName != null && TextUtils.equals(
                callerPackageName, currentLauncherPackageName)) {
            return true;
        }
        Slog.w(TAG, TextUtils.formatSimple(
                "Requester of unarchival: %s is not the default launcher package: %s.",
                callerPackageName, currentLauncherPackageName));
        // When the default launcher is not set, or when the current caller is not the default
        // launcher, allow the caller to directly request unarchive if it is a launcher app
        // that is a pre-installed system app.
        final Computer snapshot = mPm.snapshotComputer();
        final PackageStateInternal ps = snapshot.getPackageStateInternal(callerPackageName);
        final boolean isSystem = ps != null && ps.isSystem();
        return isSystem && isLauncherApp(snapshot, callerPackageName, userId);
    }

    private boolean isLauncherApp(Computer snapshot, String packageName, int userId) {
        final Intent intent = snapshot.getHomeIntent();
        intent.setPackage(packageName);
        List<ResolveInfo> launcherActivities = snapshot.queryIntentActivitiesInternal(
                intent, null /* resolvedType */, 0 /* flags */, userId);
        return !launcherActivities.isEmpty();
    }

    // Profiles share their UI and default apps, so we have to get the profile parent before
    // fetching the default launcher.
    private int getParentUserId(int userId) {