@TargetApi(Build.VERSION_CODES.N_MR1) public static void enablePostShortcut(@NonNull Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return; ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); Intent intent = ActivityHelper.intentTo(Activities.DesignerNews.PostStory.INSTANCE); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); ShortcutInfo postShortcut = new ShortcutInfo.Builder(context, POST_SHORTCUT_ID) .setShortLabel(context.getString(R.string.shortcut_post_short_label)) .setLongLabel(context.getString(R.string.shortcut_post_long_label)) .setDisabledMessage(context.getString(R.string.shortcut_post_disabled)) .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_post)) .setIntent(intent) .build(); shortcutManager.addDynamicShortcuts(Collections.singletonList(postShortcut)); }
@TargetApi(Build.VERSION_CODES.N_MR1) private static boolean createShortcutAboveN(Context context, ShortcutInfo likeShortcut) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager == null) { return false; } try { int max = shortcutManager.getMaxShortcutCountPerActivity(); List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts(); if (dynamicShortcuts.size() >= max) { Collections.sort(dynamicShortcuts, new Comparator<ShortcutInfo>() { @Override public int compare(ShortcutInfo o1, ShortcutInfo o2) { long r = o1.getLastChangedTimestamp() - o2.getLastChangedTimestamp(); return r == 0 ? 0 : (r > 0 ? 1 : -1); } }); ShortcutInfo remove = dynamicShortcuts.remove(0);// remove old. shortcutManager.removeDynamicShortcuts(Collections.singletonList(remove.getId())); } shortcutManager.addDynamicShortcuts(Collections.singletonList(likeShortcut)); return true; } catch (Throwable e) { return false; } }
@Test public void testShortcutWithIdenticalIdGetsUpdated() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1"); when(shortcut1.getLongLabel()).thenReturn("original"); ShortcutInfo shortcut2 = createShortcut("id1"); when(shortcut2.getLongLabel()).thenReturn("updated"); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut1)); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(1); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut2)); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(1); assertThat(shortcutManager.getDynamicShortcuts().get(0).getLongLabel()).isEqualTo("updated"); }
@Test public void testDynamicShortcuts_immutableShortcutDoesntGetUpdated() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1", true /* isImmutable */); when(shortcut1.getLongLabel()).thenReturn("original"); ShortcutInfo shortcut2 = createShortcut("id1", true /* isImmutable */); when(shortcut2.getLongLabel()).thenReturn("updated"); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut1)); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(1); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut2)); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(1); assertThat(shortcutManager.getDynamicShortcuts().get(0).getLongLabel()).isEqualTo("original"); }
@Test public void testUpdateShortcut_dynamic() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1"); when(shortcut1.getLongLabel()).thenReturn("original"); ShortcutInfo shortcutUpdated = createShortcut("id1"); when(shortcutUpdated.getLongLabel()).thenReturn("updated"); shortcutManager.addDynamicShortcuts( ImmutableList.of(shortcut1)); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcut1); shortcutManager.updateShortcuts(ImmutableList.of(shortcutUpdated)); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcutUpdated); }
@Test public void testDynamicShortcuts_twoAdded() throws Exception { shortcutManager.addDynamicShortcuts( ImmutableList.of(createShortcut("id1"), createShortcut("id2"))); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(2); }
@Test public void testDynamicShortcuts_duplicateGetsDeduped() throws Exception { shortcutManager.addDynamicShortcuts( ImmutableList.of(createShortcut("id1"), createShortcut("id1"))); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(1); }
@Test public void testRemoveDynamicShortcuts() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1"); ShortcutInfo shortcut2 = createShortcut("id2"); shortcutManager.addDynamicShortcuts( ImmutableList.of(shortcut1, shortcut2)); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(2); shortcutManager.removeDynamicShortcuts(ImmutableList.of("id1")); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcut2); }
@Test public void testUpdateShortcutsOnlyUpdatesExistingShortcuts() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1"); when(shortcut1.getLongLabel()).thenReturn("original"); ShortcutInfo shortcutUpdated = createShortcut("id1"); when(shortcutUpdated.getLongLabel()).thenReturn("updated"); ShortcutInfo shortcut2 = createShortcut("id2"); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut1)); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcut1); shortcutManager.updateShortcuts(ImmutableList.of(shortcutUpdated, shortcut2)); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcutUpdated); assertThat(shortcutManager.getDynamicShortcuts().get(0).getLongLabel()).isEqualTo("updated"); }
@Test @Config(minSdk = Build.VERSION_CODES.O) public void testPinningExistingDynamicShortcut() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1"); ShortcutInfo shortcut2 = createShortcut("id2"); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut1, shortcut2)); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(2); shortcutManager.requestPinShortcut(shortcut1, null /* resultIntent */); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcut2); assertThat(shortcutManager.getPinnedShortcuts()).containsExactly(shortcut1); }
@Test public void testRemoveAllDynamicShortcuts() throws Exception { shortcutManager.addDynamicShortcuts( ImmutableList.of(createShortcut("id1"), createShortcut("id2"))); assertThat(shortcutManager.getDynamicShortcuts()).hasSize(2); shortcutManager.removeAllDynamicShortcuts(); assertThat(shortcutManager.getDynamicShortcuts()).isEmpty(); }
@Test public void testSetDynamicShortcutsClearOutOldList() throws Exception { ShortcutInfo shortcut1 = createShortcut("id1"); ShortcutInfo shortcut2 = createShortcut("id2"); ShortcutInfo shortcut3 = createShortcut("id3"); ShortcutInfo shortcut4 = createShortcut("id4"); shortcutManager.addDynamicShortcuts(ImmutableList.of(shortcut1, shortcut2)); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcut1, shortcut2); shortcutManager.setDynamicShortcuts(ImmutableList.of(shortcut3, shortcut4)); assertThat(shortcutManager.getDynamicShortcuts()).containsExactly(shortcut3, shortcut4); }
public static void updateShortcut(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { Log.d("Daedalus", "Updating shortcut"); boolean activate = DaedalusVpnService.isActivated(); String notice = activate ? context.getString(R.string.button_text_deactivate) : context.getString(R.string.button_text_activate); ShortcutInfo info = new ShortcutInfo.Builder(context, Daedalus.SHORTCUT_ID_ACTIVATE) .setLongLabel(notice) .setShortLabel(notice) .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher)) .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_VIEW) .putExtra(MainActivity.LAUNCH_ACTION, activate ? MainActivity.LAUNCH_ACTION_DEACTIVATE : MainActivity.LAUNCH_ACTION_ACTIVATE)) .build(); ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(SHORTCUT_SERVICE); shortcutManager.addDynamicShortcuts(Collections.singletonList(info)); } }
@TargetApi(Build.VERSION_CODES.N_MR1) private static boolean createShortcutAboveN(Context context, ShortcutInfo likeShortcut) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager == null) { return false; } try { int max = shortcutManager.getMaxShortcutCountPerActivity(); List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts(); if (dynamicShortcuts.size() >= max) { dynamicShortcuts.sort((o1, o2) -> { long r = o1.getLastChangedTimestamp() - o2.getLastChangedTimestamp(); return r == 0 ? 0 : (r > 0 ? 1 : -1); }); ShortcutInfo remove = dynamicShortcuts.remove(0);// remove old. shortcutManager.removeDynamicShortcuts(Collections.singletonList(remove.getId())); } shortcutManager.addDynamicShortcuts(Collections.singletonList(likeShortcut)); return true; } catch (Throwable e) { return false; } }
shortcutManager.addDynamicShortcuts(Collections.singletonList(railShortcut)); finish();
/** * Adds the given channel as shortcut to the launcher icon. * Only call on api level >= 25. * * @param context to access system services * @param channel channel to create a shortcut for * @return true if the channel could be added */ @TargetApi(25) public static boolean addShortcutForChannel(Context context, ChannelModel channel) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager == null || shortcutManager.getDynamicShortcuts().size() >= 4) { return false; } ShortcutInfo shortcut = new ShortcutInfo.Builder(context, channel.getId()) .setShortLabel(channel.getName()) .setLongLabel(channel.getName()) .setIcon(Icon.createWithResource(context, channel.getDrawableId())) .setIntent(ChannelDetailActivity.getStartIntent(context, channel.getId())) .build(); try { return shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut)); } catch (IllegalArgumentException e) { // too many shortcuts return false; } }