@Implementation protected boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) { List<ShortcutInfo> existingShortcutsToUpdate = new ArrayList<>(); for (ShortcutInfo shortcutInfo : shortcutInfoList) { if (dynamicShortcuts.containsKey(shortcutInfo.getId()) || activePinnedShortcuts.containsKey(shortcutInfo.getId()) || disabledPinnedShortcuts.containsKey(shortcutInfo.getId())) { existingShortcutsToUpdate.add(shortcutInfo); } } return addDynamicShortcuts(existingShortcutsToUpdate); } }
@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"); }
@Override public int compare(ShortcutInfo o1, ShortcutInfo o2) { long r = o1.getLastChangedTimestamp() - o2.getLastChangedTimestamp(); return r == 0 ? 0 : (r > 0 ? 1 : -1); } });
@Implementation protected boolean addDynamicShortcuts(List<ShortcutInfo> shortcutInfoList) { for (ShortcutInfo shortcutInfo : shortcutInfoList) { if (activePinnedShortcuts.containsKey(shortcutInfo.getId())) { ShortcutInfo previousShortcut = activePinnedShortcuts.get(shortcutInfo.getId()); if (!previousShortcut.isImmutable()) { activePinnedShortcuts.put(shortcutInfo.getId(), shortcutInfo); } } else if (disabledPinnedShortcuts.containsKey(shortcutInfo.getId())) { ShortcutInfo previousShortcut = disabledPinnedShortcuts.get(shortcutInfo.getId()); if (!previousShortcut.isImmutable()) { disabledPinnedShortcuts.put(shortcutInfo.getId(), shortcutInfo); } } else if (dynamicShortcuts.containsKey(shortcutInfo.getId())) { ShortcutInfo previousShortcut = dynamicShortcuts.get(shortcutInfo.getId()); if (!previousShortcut.isImmutable()) { dynamicShortcuts.put(shortcutInfo.getId(), shortcutInfo); } } else { dynamicShortcuts.put(shortcutInfo.getId(), shortcutInfo); } } return true; }
assert shortcutInfo != null; Log.d(TAG, "Shortcut: " + shortcutInfo.getPackage() + " " + shortcutInfo.getId()); String id = ShortcutsPojo.SCHEME + ShortcutsPojo.OREO_PREFIX + shortcutInfo.getId(); ShortcutsPojo pojo = new ShortcutsPojo(id, shortcutInfo.getPackage(), shortcutInfo.getId(), drawableToBitmap(iconDrawable)); if (shortcutInfo.getShortLabel() != null) { pojo.setName(shortcutInfo.getShortLabel().toString()); } else if (shortcutInfo.getLongLabel() != null) { pojo.setName(shortcutInfo.getLongLabel().toString()); } else { Log.d(TAG, "Invalid shortcut " + pojo.id + ", ignoring");
if(list.get(index).getId().equals(shortcutId)) Intent shortcutIntent = prevShortcut.getIntent(); ShortcutInfo updatedShortcut = new ShortcutInfo.Builder(context, prevShortcut.getId()) .setShortLabel(prevShortcut.getShortLabel()) .setLongLabel(prevShortcut.getLongLabel()) .setIntent(shortcutIntent) .setIcon(Icon.createWithResource(context, R.drawable.circle))
private void addShortcutToActionPopup(final LauncherApps launcherApps, final ShortcutInfo shortcutInfo) { if (Build.VERSION.SDK_INT>=25) { if (shortcutInfo != null && shortcutInfo.getActivity() != null) { //Log.d(TAG, shortcutInfo.getShortLabel() + " " + shortcutInfo.getActivity().getClassName()); if (shortcutInfo.isEnabled()) { String label = ""; if (shortcutInfo.getShortLabel() != null) label += shortcutInfo.getShortLabel(); if (shortcutInfo.getLongLabel() != null && !label.contentEquals(shortcutInfo.getLongLabel())) label = shortcutInfo.getLongLabel() + ""; Drawable icon = launcherApps.getShortcutIconDrawable(shortcutInfo, DisplayMetrics.DENSITY_DEFAULT); addActionMenuItem(label.trim(), icon, new Runnable() { @Override public void run() { if (Build.VERSION.SDK_INT >= 25) { try { launcherApps.startShortcut(shortcutInfo, null, null); } catch (Exception e) { Log.e(TAG, "Couldn't Launch shortcut", e); } } dismissActionPopup(); } }); } } } }
@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; } }
try { String shortcutsText = shortcutInfoList.get(j).getShortLabel().toString(); String packageName = shortcutInfoList.get(j).getActivity().getPackageName(); String className = shortcutInfoList.get(j).getActivity().getClassName(); Bitmap shortcutsImageBadgedBitmap = Utils.convertDrawableToBitmap(shortcutsImageBadged); int rank = shortcutInfoList.get(j).getRank();
public static List<RailInfo> getRails(ShortcutManager shortcutManager) { List<ShortcutInfo> shortcuts = shortcutManager.getPinnedShortcuts(); List<RailInfo> rails = new ArrayList<>(); for (ShortcutInfo shortcutInfo : shortcuts) { String id = shortcutInfo.getId(); if (isRailShortcut(id)) { PersistableBundle extras = shortcutInfo.getExtras(); if (extras != null) { int[] posArray = extras.getIntArray(RailActionActivity.RAIL_RECT_KEY); int rotation = extras.getInt(RailActionActivity.RAIL_ROTATION_KEY); assert posArray != null; rails.add(new RailInfo(posArray[0], posArray[1], rotation)); } } } return rails; } }
@Override public CharSequence getLabel() { return mInfo.getShortLabel(); }
public int getRank() { return mShortcutInfo.getRank(); }
@TargetApi(Build.VERSION_CODES.O) private void doOreoLaunch(Context context, View v) { final LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); assert launcherApps != null; // Only the default launcher is allowed to start shortcuts if (!launcherApps.hasShortcutHostPermission()) { Toast.makeText(context, context.getString(R.string.shortcuts_no_host_permission), Toast.LENGTH_LONG).show(); return; } LauncherApps.ShortcutQuery query = new LauncherApps.ShortcutQuery(); query.setPackage(shortcutPojo.packageName); query.setShortcutIds(Collections.singletonList(shortcutPojo.getOreoId())); query.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED); List<UserHandle> userHandles = launcherApps.getProfiles(); // Find the correct UserHandle, and launch the shortcut. for (UserHandle userHandle : userHandles) { List<ShortcutInfo> shortcuts = launcherApps.getShortcuts(query, userHandle); if (shortcuts != null && shortcuts.size() > 0 && shortcuts.get(0).isEnabled()) { launcherApps.startShortcut(shortcuts.get(0), v.getClipBounds(), null); return; } } // Application removed? Invalid shortcut? Shortcut to an app on an unmounted SD card? Toast.makeText(context, R.string.application_not_found, Toast.LENGTH_LONG).show(); }
PinShortcutRequestActivityInfo(PinItemRequestCompat request, Context context) { super(new ComponentName(request.getShortcutInfo().getPackage(), DUMMY_COMPONENT_CLASS), request.getShortcutInfo().getUserHandle()); mRequest = request; mInfo = request.getShortcutInfo(); mContext = context; }
public ComponentName getActivity() { return mShortcutInfo.getActivity(); }
public String getPackage() { return mShortcutInfo.getPackage(); }
@TargetApi(Build.VERSION_CODES.O) private void acceptShortcut(LauncherApps launcherApps, LauncherApps.PinItemRequest request) { ShortcutReceiver shrecv = GlobState.getShortcutReceiver(this); if (shrecv == null) { return; } ShortcutInfo si = request.getShortcutInfo(); if (si == null) { return; } Drawable iconDrawable = launcherApps.getShortcutIconDrawable(si, 0); Bitmap icon = null; if (iconDrawable != null) { icon = IconsHandler.drawableToBitmap(iconDrawable); } String label = null; if (si.getShortLabel() != null) { label = si.getShortLabel().toString(); CharSequence longlabel = si.getLongLabel(); if (longlabel != null) { if (longlabel.toString().startsWith(label)) { label = longlabel.toString(); } else { label += " " + longlabel; } } } shrecv.addOreoLink(this, si.getId(), si.getPackage(), label, icon); request.accept(); }
private static ShortcutInfo createShortcut(String id, boolean isImmutable) { ShortcutInfo shortcut = mock(ShortcutInfo.class); when(shortcut.getId()).thenReturn(id); when(shortcut.isImmutable()).thenReturn(isImmutable); return shortcut; } }
public CharSequence getShortLabel() { return mShortcutInfo.getShortLabel(); }