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
This commit is contained in:
Hai Zhang
2019-10-01 13:48:35 -07:00
parent 3019d9c8d7
commit b80b95600e
2 changed files with 30 additions and 4 deletions

View File

@@ -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);
}
}

View File

@@ -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...");