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

Commit b80b9560 authored by Hai Zhang's avatar Hai Zhang
Browse files

Fix migration for home role.

Fixes: 141703336
Fixes: 141840533
Test: Upgrade from P->Q with this fix and a custom launcher as
      default, and it was preserved.
Test: Factory reset Q with this fix and the device default launcher is
      used.
Change-Id: I2d31f7128881f6d8173fda7cf3b64517c2a28153
parent 3019d9c8
Loading
Loading
Loading
Loading
+29 −4
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.UserIdInt;
import android.app.role.RoleManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ResolveInfo;
@@ -34,9 +35,9 @@ import com.android.internal.util.CollectionUtils;
import com.android.server.LocalServices;
import com.android.server.role.RoleManagerService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Logic to retrieve the various legacy(pre-Q) equivalents of role holders.
@@ -125,9 +126,21 @@ public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHolder
            }
            case RoleManager.ROLE_HOME: {
                PackageManager packageManager = mContext.getPackageManager();
                List<ResolveInfo> resolveInfos = new ArrayList<>();
                ComponentName componentName = packageManager.getHomeActivities(resolveInfos);
                String packageName = componentName != null ? componentName.getPackageName() : null;
                String packageName;
                if (packageManager.isDeviceUpgrading()) {
                    ResolveInfo resolveInfo = packageManager.resolveActivityAsUser(
                            new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME),
                            PackageManager.MATCH_DEFAULT_ONLY
                                    | PackageManager.MATCH_DIRECT_BOOT_AWARE
                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId);
                    packageName = resolveInfo != null && resolveInfo.activityInfo != null
                            ? resolveInfo.activityInfo.packageName : null;
                    if (packageName != null && isSettingsApplication(packageName, userId)) {
                        packageName = null;
                    }
                } else {
                    packageName = null;
                }
                return CollectionUtils.singletonOrEmpty(packageName);
            }
            case RoleManager.ROLE_EMERGENCY: {
@@ -142,4 +155,16 @@ public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHolder
            }
        }
    }

    private boolean isSettingsApplication(@NonNull String packageName, @UserIdInt int userId) {
        PackageManager packageManager = mContext.getPackageManager();
        ResolveInfo resolveInfo = packageManager.resolveActivityAsUser(new Intent(
                Settings.ACTION_SETTINGS), PackageManager.MATCH_DEFAULT_ONLY
                | PackageManager.MATCH_DIRECT_BOOT_AWARE
                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId);
        if (resolveInfo == null || resolveInfo.activityInfo == null) {
            return false;
        }
        return Objects.equals(packageName, resolveInfo.activityInfo.packageName);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -269,6 +269,7 @@ public class RoleManagerService extends SystemService implements RoleUserState.C
        maybeMigrateRole(RoleManager.ROLE_DIALER, userId);
        maybeMigrateRole(RoleManager.ROLE_SMS, userId);
        maybeMigrateRole(RoleManager.ROLE_EMERGENCY, userId);
        maybeMigrateRole(RoleManager.ROLE_HOME, userId);

        // Some package state has changed, so grant default roles again.
        Slog.i(LOG_TAG, "Granting default roles...");