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

Commit 959c54f4 authored by Winson's avatar Winson
Browse files

Reset default browser on installExistingForUser

Previously the preferred browser was only reset on the initial
install of a package. So if a different user already had the browser
installed, adding it to another user would not reset their default
browser.

In general the post install steps should be split up into
postFirstInstall and postInstallForUser, where the former calls the
latter, but that's a much larger change.

This doesn't fix an unfortunate bug where uninstalling a browser
and re-installing it without re-querying for the preferred browser
will clear the browser role, but not wipe the preferred Activity from
settings. Fixing this requires a much larger refactor, and is probably
not worth it given the assumption that users don't re-install apps
expecting it to reset the browser.

Bug: 152898545

Test: manual install browser to personal profile and then to work profile,
    verify disambiguation Activity pops up

Change-Id: Ie02b45c28c705bdcfb5bf89b9e7a33b4850cf0f7
parent f847d228
Loading
Loading
Loading
Loading
+43 −28
Original line number Diff line number Diff line
@@ -1566,13 +1566,17 @@ public class PackageManagerService extends IPackageManager.Stub
    // Recordkeeping of restore-after-install operations that are currently in flight
    // between the Package Manager and the Backup Manager
    static class PostInstallData {
        @Nullable
        public final InstallArgs args;
        @NonNull
        public final PackageInstalledInfo res;
        @Nullable
        public final Runnable mPostInstallRunnable;
        PostInstallData(InstallArgs _a, PackageInstalledInfo _r, Runnable postInstallRunnable) {
            args = _a;
            res = _r;
        PostInstallData(@Nullable InstallArgs args, @NonNull PackageInstalledInfo res,
                @Nullable Runnable postInstallRunnable) {
            this.args = args;
            this.res = res;
            mPostInstallRunnable = postInstallRunnable;
        }
    }
@@ -1714,7 +1718,7 @@ public class PackageManagerService extends IPackageManager.Stub
                    if (data != null && data.mPostInstallRunnable != null) {
                        data.mPostInstallRunnable.run();
                    } else if (data != null) {
                    } else if (data != null && data.args != null) {
                        InstallArgs args = data.args;
                        PackageInstalledInfo parentRes = data.res;
@@ -2306,27 +2310,8 @@ public class PackageManagerService extends IPackageManager.Stub
            // Work that needs to happen on first install within each user
            if (firstUserIds != null && firstUserIds.length > 0) {
                for (int userId : firstUserIds) {
                    // If this app is a browser and it's newly-installed for some
                    // users, clear any default-browser state in those users. The
                    // app's nature doesn't depend on the user, so we can just check
                    // its browser nature in any user and generalize.
                    if (packageIsBrowser(packageName, userId)) {
                        // If this browser is restored from user's backup, do not clear
                        // default-browser state for this user
                        if (pkgSetting.getInstallReason(userId)
                                != PackageManager.INSTALL_REASON_DEVICE_RESTORE) {
                            mPermissionManager.setDefaultBrowser(null, true, true, userId);
                        }
                    }
                    // We may also need to apply pending (restored) runtime permission grants
                    // within these users.
                    mPermissionManager.restoreDelayedRuntimePermissions(packageName,
                            UserHandle.of(userId));
                    // Persistent preferred activity might have came into effect due to this
                    // install.
                    updateDefaultHomeNotLocked(userId);
                    clearRolesAndRestorePermissionsForNewUserInstall(packageName,
                            pkgSetting.getInstallReason(userId), userId);
                }
            }
@@ -13122,9 +13107,15 @@ public class PackageManagerService extends IPackageManager.Stub
                        createPackageInstalledInfo(PackageManager.INSTALL_SUCCEEDED);
                res.pkg = pkgSetting.pkg;
                res.newUsers = new int[]{ userId };
                PostInstallData postInstallData = intentSender == null ? null :
                        new PostInstallData(null, res, () -> onRestoreComplete(res.returnCode,
                              mContext, intentSender));
                PostInstallData postInstallData =
                        new PostInstallData(null, res, () -> {
                            clearRolesAndRestorePermissionsForNewUserInstall(packageName,
                                    pkgSetting.getInstallReason(userId), userId);
                            if (intentSender != null) {
                                onRestoreComplete(res.returnCode, mContext, intentSender);
                            }
                        });
                restoreAndPostInstall(userId, res, postInstallData);
            }
        } finally {
@@ -19749,6 +19740,30 @@ public class PackageManagerService extends IPackageManager.Stub
        }
    }
    private void clearRolesAndRestorePermissionsForNewUserInstall(String packageName,
            int installReason, @UserIdInt int userId) {
        // If this app is a browser and it's newly-installed for some
        // users, clear any default-browser state in those users. The
        // app's nature doesn't depend on the user, so we can just check
        // its browser nature in any user and generalize.
        if (packageIsBrowser(packageName, userId)) {
            // If this browser is restored from user's backup, do not clear
            // default-browser state for this user
            if (installReason != PackageManager.INSTALL_REASON_DEVICE_RESTORE) {
                mPermissionManager.setDefaultBrowser(null, true, true, userId);
            }
        }
        // We may also need to apply pending (restored) runtime permission grants
        // within these users.
        mPermissionManager.restoreDelayedRuntimePermissions(packageName,
                UserHandle.of(userId));
        // Persistent preferred activity might have came into effect due to this
        // install.
        updateDefaultHomeNotLocked(userId);
    }
    @Override
    public void resetApplicationPreferences(int userId) {
        mContext.enforceCallingOrSelfPermission(