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

Commit d9664625 authored by Mehdi Alizadeh's avatar Mehdi Alizadeh Committed by Android (Google) Code Review
Browse files

Merge "Adds a new adb command to get shortcuts from shortcut service"

parents d1ce559f feb46be8
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -1535,6 +1535,27 @@ class ShortcutPackage extends ShortcutPackageItem {
        pw.println(")");
    }

    public void dumpShortcuts(@NonNull PrintWriter pw, int matchFlags) {
        final boolean matchDynamic = (matchFlags & ShortcutManager.FLAG_MATCH_DYNAMIC) != 0;
        final boolean matchPinned = (matchFlags & ShortcutManager.FLAG_MATCH_PINNED) != 0;
        final boolean matchManifest = (matchFlags & ShortcutManager.FLAG_MATCH_MANIFEST) != 0;
        final boolean matchCached = (matchFlags & ShortcutManager.FLAG_MATCH_CACHED) != 0;

        final int shortcutFlags = (matchDynamic ? ShortcutInfo.FLAG_DYNAMIC : 0)
                | (matchPinned ? ShortcutInfo.FLAG_PINNED : 0)
                | (matchManifest ? ShortcutInfo.FLAG_MANIFEST : 0)
                | (matchCached ? ShortcutInfo.FLAG_CACHED_ALL : 0);

        final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
        final int size = shortcuts.size();
        for (int i = 0; i < size; i++) {
            final ShortcutInfo si = shortcuts.valueAt(i);
            if ((si.getFlags() & shortcutFlags) != 0) {
                pw.println(si.toDumpString(""));
            }
        }
    }

    @Override
    public JSONObject dumpCheckin(boolean clear) throws JSONException {
        final JSONObject result = super.dumpCheckin(clear);
+31 −0
Original line number Diff line number Diff line
@@ -4556,6 +4556,10 @@ public class ShortcutService extends IShortcutService.Stub {

        private int mUserId = UserHandle.USER_SYSTEM;

        private int mShortcutMatchFlags = ShortcutManager.FLAG_MATCH_CACHED
                | ShortcutManager.FLAG_MATCH_DYNAMIC | ShortcutManager.FLAG_MATCH_MANIFEST
                | ShortcutManager.FLAG_MATCH_PINNED;

        private void parseOptionsLocked(boolean takeUser)
                throws CommandException {
            String opt;
@@ -4571,6 +4575,9 @@ public class ShortcutService extends IShortcutService.Stub {
                            break;
                        }
                        // fallthrough
                    case "--flags":
                        mShortcutMatchFlags = Integer.parseInt(getNextArgRequired());
                        break;
                    default:
                        throw new CommandException("Unknown option: " + opt);
                }
@@ -4606,6 +4613,9 @@ public class ShortcutService extends IShortcutService.Stub {
                    case "clear-shortcuts":
                        handleClearShortcuts();
                        break;
                    case "get-shortcuts":
                        handleGetShortcuts();
                        break;
                    case "verify-states": // hidden command to verify various internal states.
                        handleVerifyStates();
                        break;
@@ -4649,6 +4659,9 @@ public class ShortcutService extends IShortcutService.Stub {
            pw.println("cmd shortcut clear-shortcuts [--user USER_ID] PACKAGE");
            pw.println("    Remove all shortcuts from a package, including pinned shortcuts");
            pw.println();
            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();
        }

        private void handleResetThrottling() throws CommandException {
@@ -4723,6 +4736,24 @@ public class ShortcutService extends IShortcutService.Stub {
            }
        }

        private void handleGetShortcuts() throws CommandException {
            synchronized (mLock) {
                parseOptionsLocked(/* takeUser =*/ true);
                final String packageName = getNextArgRequired();

                Slog.i(TAG, "cmd: handleGetShortcuts: user=" + mUserId + ", flags="
                        + mShortcutMatchFlags + ", package=" + packageName);

                final ShortcutUser user = ShortcutService.this.getUserShortcutsLocked(mUserId);
                final ShortcutPackage p = user.getPackageShortcutsIfExists(packageName);
                if (p == null) {
                    return;
                }

                p.dumpShortcuts(getOutPrintWriter(), mShortcutMatchFlags);
            }
        }

        private void handleVerifyStates() throws CommandException {
            try {
                verifyStatesForce(); // This will throw when there's an issue.
+72 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.pm;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.array;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertContains;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertHaveIds;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertSuccess;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
@@ -25,6 +26,8 @@ import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils
import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resultContains;

import android.content.ComponentName;
import android.content.pm.LauncherApps;
import android.content.pm.ShortcutManager;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.Process;
@@ -47,6 +50,9 @@ import java.util.concurrent.atomic.AtomicInteger;
 */
@SmallTest
public class ShortcutManagerTest7 extends BaseShortcutManagerTest {

    private static final int CACHE_OWNER = LauncherApps.FLAG_CACHE_NOTIFICATION_SHORTCUTS;

    private List<String> callShellCommand(String... args) throws IOException, RemoteException {

        // For reset to work, the current time needs to be incrementing.
@@ -323,6 +329,72 @@ public class ShortcutManagerTest7 extends BaseShortcutManagerTest {
        });
    }

    public void testGetShortcuts() throws Exception {

        mRunningUsers.put(USER_10, true);

        // Add two manifests and two dynamics.
        addManifestShortcutResource(
                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
                R.xml.shortcut_2);
        updatePackageVersion(CALLING_PACKAGE_1, 1);
        mService.mPackageMonitor.onReceive(getTestContext(),
                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));

        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
            assertTrue(mManager.addDynamicShortcuts(list(
                    makeLongLivedShortcut("s1"), makeShortcut("s2"))));
        });
        runWithCaller(LAUNCHER_1, USER_10, () -> {
            mInjectCheckAccessShortcutsPermission = true;
            mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_10,
                    CACHE_OWNER);
            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
        });

        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
            assertWith(getCallerShortcuts())
                    .haveIds("ms1", "ms2", "s1", "s2")
                    .areAllEnabled()

                    .selectPinned()
                    .haveIds("ms2", "s2");
        });


        mRunningUsers.put(USER_10, true);
        mUnlockedUsers.put(USER_10, true);

        mInjectedCallingUid = Process.SHELL_UID;

        assertHaveIds(callShellCommand("get-shortcuts", "--user", "10", "--flags",
                Integer.toString(ShortcutManager.FLAG_MATCH_CACHED), CALLING_PACKAGE_1),
                "s1");

        assertHaveIds(callShellCommand("get-shortcuts", "--user", "10", "--flags",
                Integer.toString(ShortcutManager.FLAG_MATCH_DYNAMIC), CALLING_PACKAGE_1),
                "s1", "s2");

        assertHaveIds(callShellCommand("get-shortcuts", "--user", "10", "--flags",
                Integer.toString(ShortcutManager.FLAG_MATCH_MANIFEST), CALLING_PACKAGE_1),
                "ms1", "ms2");

        assertHaveIds(callShellCommand("get-shortcuts", "--user", "10", "--flags",
                Integer.toString(ShortcutManager.FLAG_MATCH_PINNED), CALLING_PACKAGE_1),
                "ms2", "s2");

        assertHaveIds(callShellCommand("get-shortcuts", "--user", "10", "--flags",
                Integer.toString(ShortcutManager.FLAG_MATCH_DYNAMIC
                        | ShortcutManager.FLAG_MATCH_PINNED), CALLING_PACKAGE_1),
                "ms2", "s1", "s2");

        assertHaveIds(callShellCommand("get-shortcuts", "--user", "10", "--flags",
                Integer.toString(ShortcutManager.FLAG_MATCH_MANIFEST
                        | ShortcutManager.FLAG_MATCH_CACHED), CALLING_PACKAGE_1),
                "ms1", "ms2", "s1");

    }

    public void testDumpsysArgs() {
        checkDumpsysArgs(null, true, false, false);
        checkDumpsysArgs(array("-u"), true, true, false);
+24 −0
Original line number Diff line number Diff line
@@ -136,6 +136,20 @@ public class ShortcutManagerTestUtils {
        return sb.toString();
    }

    public static List<String> extractShortcutIds(List<String> result) {
        final String prefix = "ShortcutInfo {id=";
        final String postfix = ", ";

        List<String> ids = new ArrayList<>();
        for (String line : result) {
            if (line.contains(prefix)) {
                ids.add(line.substring(
                        line.indexOf(prefix) + prefix.length(), line.indexOf(postfix)));
            }
        }
        return ids;
    }

    public static boolean resultContains(List<String> result, String expected) {
        for (String line : result) {
            if (line.contains(expected)) {
@@ -160,6 +174,16 @@ public class ShortcutManagerTestUtils {
        return result;
    }

    public static List<String> assertHaveIds(List<String> result, String... expectedIds) {
        assertSuccess(result);

        final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
        final SortedSet<String> actual = new TreeSet<>(extractShortcutIds(result));
        assertEquals(expected, actual);

        return result;
    }

    public static List<String> runCommand(Instrumentation instrumentation, String command) {
        return runCommand(instrumentation, command, null);
    }