Search in sources :

Example 86 with ShortcutInfo

use of android.content.pm.ShortcutInfo in project android_frameworks_base by ResurrectionRemix.

the class ShortcutManagerTest1 method testSaveAndLoadUser.

/**
     * Try save and load, also stop/start the user.
     */
public void testSaveAndLoadUser() {
    // First, create some shortcuts and save.
    runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
        final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
        final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getTestContext().getResources(), R.drawable.icon2));
        final ShortcutInfo si1 = makeShortcut("s1", "title1-1", makeComponent(ShortcutActivity.class), icon1, makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class, "key1", "val1", "nest", makeBundle("key", 123)), /* weight */
        10);
        final ShortcutInfo si2 = makeShortcut("s2", "title1-2", /* activity */
        null, icon2, makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class), /* weight */
        12);
        assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
        assertEquals(2, mManager.getRemainingCallCount());
    });
    runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
        final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
        final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getTestContext().getResources(), R.drawable.icon2));
        final ShortcutInfo si1 = makeShortcut("s1", "title2-1", makeComponent(ShortcutActivity.class), icon1, makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class, "key1", "val1", "nest", makeBundle("key", 123)), /* weight */
        10);
        final ShortcutInfo si2 = makeShortcut("s2", "title2-2", /* activity */
        null, icon2, makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class), /* weight */
        12);
        assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
        assertEquals(2, mManager.getRemainingCallCount());
    });
    mRunningUsers.put(USER_10, true);
    runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
        final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
        final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getTestContext().getResources(), R.drawable.icon2));
        final ShortcutInfo si1 = makeShortcut("s1", "title10-1-1", makeComponent(ShortcutActivity.class), icon1, makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class, "key1", "val1", "nest", makeBundle("key", 123)), /* weight */
        10);
        final ShortcutInfo si2 = makeShortcut("s2", "title10-1-2", /* activity */
        null, icon2, makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class), /* weight */
        12);
        assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
        assertEquals(2, mManager.getRemainingCallCount());
    });
    mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(new ComponentName("pkg1", "class"));
    // Restore.
    mService.saveDirtyInfo();
    initService();
    // Before the load, the map should be empty.
    assertEquals(0, mService.getShortcutsForTest().size());
    // this will pre-load the per-user info.
    mService.handleUnlockUser(UserHandle.USER_SYSTEM);
    // Now it's loaded.
    assertEquals(1, mService.getShortcutsForTest().size());
    runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
        assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(mManager.getDynamicShortcuts()))), "s1", "s2");
        assertEquals(2, mManager.getRemainingCallCount());
        assertEquals("title1-1", getCallerShortcut("s1").getTitle());
        assertEquals("title1-2", getCallerShortcut("s2").getTitle());
    });
    runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
        assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(mManager.getDynamicShortcuts()))), "s1", "s2");
        assertEquals(2, mManager.getRemainingCallCount());
        assertEquals("title2-1", getCallerShortcut("s1").getTitle());
        assertEquals("title2-2", getCallerShortcut("s2").getTitle());
    });
    assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).getLastKnownLauncher().getPackageName());
    // Start another user
    mService.handleUnlockUser(USER_10);
    // Now the size is 2.
    assertEquals(2, mService.getShortcutsForTest().size());
    runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
        assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(mManager.getDynamicShortcuts()))), "s1", "s2");
        assertEquals(2, mManager.getRemainingCallCount());
        assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
        assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
    });
    assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
    // Try stopping the user
    mService.handleCleanupUser(USER_10);
    // Now it's unloaded.
    assertEquals(1, mService.getShortcutsForTest().size());
// TODO Check all other fields
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) ComponentName(android.content.ComponentName) Icon(android.graphics.drawable.Icon)

Example 87 with ShortcutInfo

use of android.content.pm.ShortcutInfo in project android_frameworks_base by ResurrectionRemix.

the class ShortcutManagerTest2 method testShortcutInfoClone_minimum.

public void testShortcutInfoClone_minimum() {
    PersistableBundle pb = new PersistableBundle();
    pb.putInt("k", 1);
    ShortcutInfo sorig = new ShortcutInfo.Builder(getTestContext()).setId("id").setTitle("title").setIntent(makeIntent("action", ShortcutActivity.class)).build();
    ShortcutInfo si = sorig.clone(/* clone flags*/
    0);
    assertEquals(getTestContext().getPackageName(), si.getPackage());
    assertEquals("id", si.getId());
    assertEquals("title", si.getTitle());
    assertEquals("action", si.getIntent().getAction());
    assertEquals(null, si.getCategories());
    si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_CREATOR);
    assertEquals(getTestContext().getPackageName(), si.getPackage());
    assertEquals("id", si.getId());
    assertEquals("title", si.getTitle());
    assertEquals("action", si.getIntent().getAction());
    assertEquals(null, si.getCategories());
    si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
    assertEquals(getTestContext().getPackageName(), si.getPackage());
    assertEquals("id", si.getId());
    assertEquals("title", si.getTitle());
    assertEquals(null, si.getIntent());
    assertEquals(null, si.getCategories());
    si = sorig.clone(ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO);
    assertEquals(getTestContext().getPackageName(), si.getPackage());
    assertEquals("id", si.getId());
    assertEquals(null, si.getTitle());
    assertEquals(null, si.getIntent());
    assertEquals(null, si.getCategories());
}
Also used : PersistableBundle(android.os.PersistableBundle) ShortcutInfo(android.content.pm.ShortcutInfo)

Example 88 with ShortcutInfo

use of android.content.pm.ShortcutInfo in project android_frameworks_base by ResurrectionRemix.

the class ShortcutManagerTest2 method testThrottling_rewind.

public void testThrottling_rewind() {
    final ShortcutInfo si1 = makeShortcut("shortcut1");
    assertTrue(mManager.setDynamicShortcuts(list(si1)));
    assertEquals(2, mManager.getRemainingCallCount());
    assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
    // Clock reset!
    mInjectedCurrentTimeMillis = 12345;
    // Since the clock looks invalid, the counter shouldn't have reset.
    assertEquals(2, mManager.getRemainingCallCount());
    assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
    // Forward again.  Still haven't reset yet.
    mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
    assertEquals(2, mManager.getRemainingCallCount());
    assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
    // Now rewind -- this will reset the counters.
    mInjectedCurrentTimeMillis = START_TIME - 100000;
    assertEquals(3, mManager.getRemainingCallCount());
    assertTrue(mManager.setDynamicShortcuts(list(si1)));
    assertEquals(2, mManager.getRemainingCallCount());
    // Forward again, should be reset now.
    mInjectedCurrentTimeMillis += INTERVAL;
    assertEquals(3, mManager.getRemainingCallCount());
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo)

Example 89 with ShortcutInfo

use of android.content.pm.ShortcutInfo in project AndroidDevelop by 7449.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        return;
    }
    List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
    final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    ShortcutInfo blogShortcut = new ShortcutInfo.Builder(this, "blog").setShortLabel("my blog").setLongLabel("我的博客").setDisabledMessage("被删除了").setIcon(Icon.createWithResource(getApplicationContext(), R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://7449.github.io/"))).build();
    ShortcutInfo githubShortcut = new ShortcutInfo.Builder(this, "github").setShortLabel("my github").setLongLabel("我的github").setDisabledMessage("被删除了").setIcon(Icon.createWithResource(getApplicationContext(), R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/7449"))).build();
    Intent intent = new Intent(this, TestActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    ShortcutInfo newActivityShortcut = new ShortcutInfo.Builder(this, "Activity").setShortLabel("newActivity").setLongLabel("newActivity").setDisabledMessage("被删除了").setIcon(Icon.createWithResource(getApplicationContext(), R.mipmap.ic_launcher)).setIntent(intent).build();
    shortcutInfoList.add(blogShortcut);
    shortcutInfoList.add(githubShortcut);
    shortcutInfoList.add(newActivityShortcut);
    shortcutManager.setDynamicShortcuts(shortcutInfoList);
    findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
                List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
                for (int i = 0; i < dynamicShortcuts.size(); i++) {
                    ShortcutInfo shortcutInfo = dynamicShortcuts.get(i);
                    if (TextUtils.equals("github", shortcutInfo.getId())) {
                        //桌面的图标这个时候就变灰了
                        shortcutManager.disableShortcuts(Collections.singletonList(shortcutInfo.getId()));
                        shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortcutInfo.getId()));
                    }
                }
            }
        }
    });
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) ShortcutManager(android.content.pm.ShortcutManager) ArrayList(java.util.ArrayList) Intent(android.content.Intent) ArrayList(java.util.ArrayList) List(java.util.List) View(android.view.View)

Example 90 with ShortcutInfo

use of android.content.pm.ShortcutInfo in project android_frameworks_base by DirtyUnicorns.

the class ShortcutPackage method deleteOrDisableWithId.

@Nullable
private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable, boolean overrideImmutable) {
    final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
    if (oldShortcut == null || !oldShortcut.isEnabled()) {
        // Doesn't exist or already disabled.
        return null;
    }
    if (!overrideImmutable) {
        ensureNotImmutable(oldShortcut);
    }
    if (oldShortcut.isPinned()) {
        oldShortcut.setRank(0);
        oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
        if (disable) {
            oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
        }
        oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
        return oldShortcut;
    } else {
        deleteShortcutInner(shortcutId);
        return null;
    }
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) Nullable(android.annotation.Nullable)

Aggregations

ShortcutInfo (android.content.pm.ShortcutInfo)324 ComponentName (android.content.ComponentName)144 PersistableBundle (android.os.PersistableBundle)48 ArrayList (java.util.ArrayList)46 Intent (android.content.Intent)44 Icon (android.graphics.drawable.Icon)32 List (java.util.List)17 Matchers.anyString (org.mockito.Matchers.anyString)16 LocaleList (android.os.LocaleList)12 File (java.io.File)12 Nullable (android.annotation.Nullable)8 ShortcutQuery (android.content.pm.LauncherApps.ShortcutQuery)8 Resources (android.content.res.Resources)8 Bundle (android.os.Bundle)8 ArrayMap (android.util.ArrayMap)8 PackageWithUser (com.android.server.pm.ShortcutUser.PackageWithUser)8 ShortcutManagerTestUtils.makeBundle (com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle)8 IOException (java.io.IOException)8 ArraySet (android.util.ArraySet)5 Manifest.permission (android.Manifest.permission)4