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

Commit 3f6240bc authored by Mehdi Alizadeh's avatar Mehdi Alizadeh
Browse files

Fixes various failing cts

This CL fixed various failing CTS cases by:
1. Adding a new shell command in ShortCutService to verify the shortcut access of a package
2. For managed users, use the HOME role holder of the parent user instead
3. Using RoleManager to set and get the default launcher in utility methods

Bug: 159814579
Bug: 168424506
Bug: 176498610
Bug: 176706886
Bug: 166655637

Test: atest ShortcutManagerTest1 to ShortcutManagerTest11
      atest CtsShortcutHostTestCases CtsShortcutManagerTestCases
      atest com.android.cts.devicepolicy.MixedManagedProfileOwnerTest
      atest android.content.pm.cts.PackageManagerTest
      atest android.content.pm.cts.LauncherAppsTest
Change-Id: I9b0156c62c47f848400b2cd85548a1b8d246a68c
parent 38eaf0db
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -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));
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -6051,8 +6051,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,
+36 −5
Original line number Diff line number Diff line
@@ -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 ===
+3 −0
Original line number Diff line number Diff line
@@ -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)),
+4 −0
Original line number Diff line number Diff line
@@ -22,5 +22,9 @@ java_library {
        "android.test.runner.stubs",
    ],

    static_libs: [
        "compatibility-device-util-axt",
    ],

    sdk_version: "test_current",
}
Loading