Merge "Fixes various failing cts" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
aa3477c5a8
@@ -41,14 +41,18 @@ import java.util.function.Supplier;
|
||||
public class DefaultAppProvider {
|
||||
@NonNull
|
||||
private final Supplier<RoleManager> mRoleManagerSupplier;
|
||||
@NonNull
|
||||
private final Supplier<UserManagerInternal> mUserManagerInternalSupplier;
|
||||
|
||||
/**
|
||||
* Create a new instance of this class
|
||||
*
|
||||
* @param roleManagerSupplier the supplier for {@link RoleManager}
|
||||
*/
|
||||
public DefaultAppProvider(@NonNull Supplier<RoleManager> roleManagerSupplier) {
|
||||
public DefaultAppProvider(@NonNull Supplier<RoleManager> roleManagerSupplier,
|
||||
@NonNull Supplier<UserManagerInternal> userManagerInternalSupplier) {
|
||||
mRoleManagerSupplier = roleManagerSupplier;
|
||||
mUserManagerInternalSupplier = userManagerInternalSupplier;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +136,8 @@ public class DefaultAppProvider {
|
||||
*/
|
||||
@Nullable
|
||||
public String getDefaultHome(@NonNull int userId) {
|
||||
return getRoleHolder(RoleManager.ROLE_HOME, userId);
|
||||
return getRoleHolder(RoleManager.ROLE_HOME,
|
||||
mUserManagerInternalSupplier.get().getProfileParentId(userId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5761,8 +5761,8 @@ public class PackageManagerService extends IPackageManager.Stub
|
||||
(i, pm) -> new ViewCompiler(i.getInstallLock(), i.getInstaller()),
|
||||
(i, pm) -> (IncrementalManager)
|
||||
i.getContext().getSystemService(Context.INCREMENTAL_SERVICE),
|
||||
(i, pm) -> new DefaultAppProvider(() -> context.getSystemService(
|
||||
RoleManager.class)),
|
||||
(i, pm) -> new DefaultAppProvider(() -> context.getSystemService(RoleManager.class),
|
||||
() -> LocalServices.getService(UserManagerInternal.class)),
|
||||
(i, pm) -> new DisplayMetrics(),
|
||||
(i, pm) -> new PackageParser2(pm.mSeparateProcesses, pm.mOnlyCore,
|
||||
i.getDisplayMetrics(), pm.mCacheDir,
|
||||
|
||||
@@ -40,6 +40,7 @@ import android.content.IntentSender.SendIntentException;
|
||||
import android.content.LocusId;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ComponentInfo;
|
||||
import android.content.pm.IPackageManager;
|
||||
import android.content.pm.IShortcutService;
|
||||
import android.content.pm.LauncherApps;
|
||||
@@ -4619,6 +4620,9 @@ public class ShortcutService extends IShortcutService.Stub {
|
||||
case "verify-states": // hidden command to verify various internal states.
|
||||
handleVerifyStates();
|
||||
break;
|
||||
case "has-shortcut-access":
|
||||
handleHasShortcutAccess();
|
||||
break;
|
||||
default:
|
||||
return handleDefaultCommands(cmd);
|
||||
}
|
||||
@@ -4650,7 +4654,7 @@ public class ShortcutService extends IShortcutService.Stub {
|
||||
pw.println("[Deprecated] cmd shortcut get-default-launcher [--user USER_ID]");
|
||||
pw.println(" Show the default launcher");
|
||||
pw.println(" Note: This command is deprecated. Callers should query the default"
|
||||
+ " launcher directly from RoleManager instead.");
|
||||
+ " launcher from RoleManager instead.");
|
||||
pw.println();
|
||||
pw.println("cmd shortcut unload-user [--user USER_ID]");
|
||||
pw.println(" Unload a user from the memory");
|
||||
@@ -4662,6 +4666,10 @@ public class ShortcutService extends IShortcutService.Stub {
|
||||
pw.println("cmd shortcut get-shortcuts [--user USER_ID] [--flags FLAGS] PACKAGE");
|
||||
pw.println(" Show the shortcuts for a package that match the given flags");
|
||||
pw.println();
|
||||
pw.println("cmd shortcut has-shortcut-access [--user USER_ID] PACKAGE");
|
||||
pw.println(" Prints \"true\" if the package can access shortcuts,"
|
||||
+ " \"false\" otherwise");
|
||||
pw.println();
|
||||
}
|
||||
|
||||
private void handleResetThrottling() throws CommandException {
|
||||
@@ -4706,11 +4714,24 @@ public class ShortcutService extends IShortcutService.Stub {
|
||||
private void handleGetDefaultLauncher() throws CommandException {
|
||||
synchronized (mLock) {
|
||||
parseOptionsLocked(/* takeUser =*/ true);
|
||||
|
||||
final String defaultLauncher = getDefaultLauncher(mUserId);
|
||||
if (defaultLauncher == null) {
|
||||
throw new CommandException(
|
||||
"Failed to get the default launcher for user " + mUserId);
|
||||
}
|
||||
|
||||
// Get the class name of the component from PM to keep the old behaviour.
|
||||
final List<ResolveInfo> allHomeCandidates = new ArrayList<>();
|
||||
// Default launcher from package manager.
|
||||
final ComponentName defaultLauncher = mPackageManagerInternal
|
||||
.getHomeActivitiesAsUser(allHomeCandidates, getParentOrSelfUserId(mUserId));
|
||||
getOutPrintWriter().println("Launcher: " + defaultLauncher);
|
||||
mPackageManagerInternal.getHomeActivitiesAsUser(allHomeCandidates,
|
||||
getParentOrSelfUserId(mUserId));
|
||||
for (ResolveInfo ri : allHomeCandidates) {
|
||||
final ComponentInfo ci = ri.getComponentInfo();
|
||||
if (ci.packageName.equals(defaultLauncher)) {
|
||||
getOutPrintWriter().println("Launcher: " + ci.getComponentName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4761,6 +4782,16 @@ public class ShortcutService extends IShortcutService.Stub {
|
||||
throw new CommandException(th.getMessage() + "\n" + Log.getStackTraceString(th));
|
||||
}
|
||||
}
|
||||
|
||||
private void handleHasShortcutAccess() throws CommandException {
|
||||
synchronized (mLock) {
|
||||
parseOptionsLocked(/* takeUser =*/ true);
|
||||
final String packageName = getNextArgRequired();
|
||||
|
||||
boolean shortcutAccess = hasShortcutHostPermissionInner(packageName, mUserId);
|
||||
getOutPrintWriter().println(Boolean.toString(shortcutAccess));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// === Unit test support ===
|
||||
|
||||
@@ -221,11 +221,13 @@ public class ShortcutManagerTest7 extends BaseShortcutManagerTest {
|
||||
|
||||
// This command is deprecated. Will remove the test later.
|
||||
public void testLauncherCommands() throws Exception {
|
||||
prepareGetRoleHoldersAsUser(getSystemLauncher().activityInfo.packageName, USER_0);
|
||||
prepareGetHomeActivitiesAsUser(
|
||||
/* preferred */ getSystemLauncher().activityInfo.getComponentName(),
|
||||
list(getSystemLauncher(), getFallbackLauncher()),
|
||||
USER_0);
|
||||
|
||||
prepareGetRoleHoldersAsUser(CALLING_PACKAGE_2, USER_10);
|
||||
prepareGetHomeActivitiesAsUser(
|
||||
/* preferred */ cn(CALLING_PACKAGE_2, "name"),
|
||||
list(getSystemLauncher(), getFallbackLauncher(),
|
||||
@@ -247,6 +249,7 @@ public class ShortcutManagerTest7 extends BaseShortcutManagerTest {
|
||||
"Launcher: ComponentInfo{com.android.test.2/name}");
|
||||
|
||||
// Change user-0's launcher.
|
||||
prepareGetRoleHoldersAsUser(CALLING_PACKAGE_1, USER_0);
|
||||
prepareGetHomeActivitiesAsUser(
|
||||
/* preferred */ cn(CALLING_PACKAGE_1, "name"),
|
||||
list(ri(CALLING_PACKAGE_1, "name", false, 0)),
|
||||
|
||||
@@ -22,5 +22,9 @@ java_library {
|
||||
"android.test.runner.stubs",
|
||||
],
|
||||
|
||||
static_libs: [
|
||||
"compatibility-device-util-axt",
|
||||
],
|
||||
|
||||
sdk_version: "test_current",
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.android.server.pm.shortcutmanagertest;
|
||||
|
||||
import static com.android.compatibility.common.util.SystemUtil.callWithShellPermissionIdentity;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
@@ -34,6 +36,7 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.app.Instrumentation;
|
||||
import android.app.role.RoleManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.LocusId;
|
||||
@@ -50,6 +53,7 @@ import android.os.Parcel;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.PersistableBundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.test.MoreAsserts;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -217,30 +221,60 @@ public class ShortcutManagerTestUtils {
|
||||
return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
|
||||
}
|
||||
|
||||
public static String getDefaultLauncher(Instrumentation instrumentation) {
|
||||
final String PREFIX = "Launcher: ComponentInfo{";
|
||||
final String POSTFIX = "}";
|
||||
final List<String> result = runShortcutCommandForSuccess(
|
||||
instrumentation, "get-default-launcher --user "
|
||||
+ instrumentation.getContext().getUserId());
|
||||
for (String s : result) {
|
||||
if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
|
||||
return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
|
||||
private static UserHandle getParentUser(Context context) {
|
||||
final UserHandle user = context.getUser();
|
||||
final UserManager userManager = context.getSystemService(UserManager.class);
|
||||
if (!userManager.isManagedProfile(user.getIdentifier())) {
|
||||
return user;
|
||||
}
|
||||
|
||||
final List<UserHandle> profiles = userManager.getUserProfiles();
|
||||
for (UserHandle handle : profiles) {
|
||||
if (!userManager.isManagedProfile(handle.getIdentifier())) {
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
fail("Default launcher not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
|
||||
runCommand(instrumentation, "cmd package set-home-activity --user "
|
||||
+ instrumentation.getContext().getUserId() + " " + component,
|
||||
result -> result.contains("Success"));
|
||||
public static String getDefaultLauncher(Instrumentation instrumentation) throws Exception {
|
||||
final Context context = instrumentation.getContext();
|
||||
final RoleManager roleManager = context.getSystemService(RoleManager.class);
|
||||
final UserHandle user = getParentUser(context);
|
||||
List<String> roleHolders = callWithShellPermissionIdentity(
|
||||
() -> roleManager.getRoleHoldersAsUser(RoleManager.ROLE_HOME, user));
|
||||
if (roleHolders.size() == 1) {
|
||||
return roleHolders.get(0);
|
||||
}
|
||||
fail("Failed to get the default launcher for user " + context.getUserId());
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setDefaultLauncher(Instrumentation instrumentation, String packageName) {
|
||||
runCommandForNoOutput(instrumentation, "cmd role add-role-holder --user "
|
||||
+ instrumentation.getContext().getUserId() + " " + RoleManager.ROLE_HOME + " "
|
||||
+ packageName + " 0");
|
||||
waitUntil("Failed to get shortcut access",
|
||||
() -> hasShortcutAccess(instrumentation, packageName), 20);
|
||||
}
|
||||
|
||||
public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
|
||||
setDefaultLauncher(instrumentation, packageContext.getPackageName()
|
||||
+ "/android.content.pm.cts.shortcutmanager.packages.Launcher");
|
||||
setDefaultLauncher(instrumentation, packageContext.getPackageName());
|
||||
}
|
||||
|
||||
public static boolean hasShortcutAccess(Instrumentation instrumentation, String packageName) {
|
||||
final List<String> result = runShortcutCommandForSuccess(instrumentation,
|
||||
"has-shortcut-access --user " + instrumentation.getContext().getUserId()
|
||||
+ " " + packageName);
|
||||
for (String s : result) {
|
||||
if (s.startsWith("true")) {
|
||||
return true;
|
||||
} else if (s.startsWith("false")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
fail("Failed to check shortcut access");
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void overrideConfig(Instrumentation instrumentation, String config) {
|
||||
|
||||
Reference in New Issue
Block a user