private void pinShortcutToHomeScreen(Context context) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager.isRequestPinShortcutSupported()) { ShortcutInfo pinShortcutInfo = createShortcutInfo(this); Intent resultIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, resultIntent, 0); shortcutManager.requestPinShortcut(pinShortcutInfo, pendingIntent.getIntentSender()); } else { Toast.makeText(context, R.string.device_does_not_support_shortcut_pinning, Toast.LENGTH_SHORT).show(); } }
/** * Update the dynamic shortcuts that are associated with this app. The given list of cards must * be sorted by popularity. The amount parameter indicates how much shortcuts should be made. * * @param cards A list of cards that's sorted by popularity. * @param amount Amount indicates the number n of cards for which a shortcut should be made from * the list. */ public void updateDynamicShortcuts(List<Card> cards, int amount) { if (cards.size() < amount) { amount = cards.size(); } this.cards = cards; List<ShortcutInfo> shortcuts = new ArrayList<>(); ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); for (int i = 0; i < amount; i++) { ShortcutInfo shortcut = new ShortcutInfo.Builder(context, cards.get(i).getName() + "-shortcut") .setShortLabel(cards.get(i).getName()) .setIcon(Icon.createWithResource(context, R.drawable.shortcut_store)) .setIntent(createCardShortcutIntent(cards.get(i))) .build(); shortcuts.add(shortcut); } shortcutManager.setDynamicShortcuts(shortcuts); }
@TargetApi(Build.VERSION_CODES.N_MR1) private void createShortcut(String label, String packageName, Bitmap icon, Intent intent) { ShortcutManager shortcutManager; shortcutManager = getSystemService(ShortcutManager.class); ShortcutInfo shortcut = new ShortcutInfo.Builder(this, packageName) .setShortLabel(label) .setLongLabel(label) .setIcon(Icon.createWithBitmap(icon)) .setIntent(intent) .setRank(0) .build(); List<ShortcutInfo> shortcutList = shortcutManager.getDynamicShortcuts(); int shortcutSize = shortcutList.size(); if (shortcutSize >= 5) { for (int i = 0; i < shortcutSize - 4; i++) { shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortcutList.get(0).getId())); } } shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut)); }
@TargetApi(Build.VERSION_CODES.N_MR1) private void createShortcut() { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); Intent intent = new Intent(this, SplashActivity.class); intent.setAction(ACTION_QUICK_START_OR_QUICK_STOP); intent.putExtra(EXTRA_COME_FROM_SHORTCUT, true); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcut_quick_switch") .setShortLabel(getString(R.string.shortcut_quick_switch)) .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)) .setIntent(intent) .build(); if (shortcutManager != null) { shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); } }
public ShortcutHelper createShortcutList(@NonNull List<Shortcut> shortcuts) { if (Build.VERSION.SDK_INT < 25) { return this; } mShortcutManager = mActivity.getSystemService(ShortcutManager.class); for (int i=0; i<shortcuts.size(); i++) { if (i < mShortcutManager.getMaxShortcutCountPerActivity()) { String shortcutId = shortcuts.get(i).getShortLabel().replaceAll("\\s+","").toLowerCase() + "_shortcut"; ShortcutInfo shortcut = new ShortcutInfo.Builder(mActivity, shortcutId) .setShortLabel(shortcuts.get(i).getShortLabel()) .setLongLabel(shortcuts.get(i).getLongLabel()) .setIcon(Icon.createWithResource(mActivity, shortcuts.get(i).getIconResource())) .setIntent(shortcuts.get(i).getIntent()) .build(); mShortcutInfos.add(shortcut); } } return this; }
private void addChoiceShortcuts() { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); if (mChoice.isFinish()) { shortcutManager.removeAllDynamicShortcuts(); return; } if (mChoice.choices == null || mChoice.choices.size() == 0) { return; } List<ShortcutInfo> choiceShortcuts = new ArrayList<>(); int rank = 1; for (Choice choice : mChoice.choices) { ShortcutInfo choiceShortcut = new ShortcutInfo.Builder(this, IdUtil.getRandomUniqueShortcutId()) .setShortLabel(choice.action) .setLongLabel(choice.action) .setDisabledMessage(getString(R.string.shortcut_disabled_message)) .setIcon(Icon.createWithBitmap(choice.getActionEmoji(this))) .setIntent(IntentUtil.choice(this, choice)) .setRank(rank) .build(); choiceShortcuts.add(choiceShortcut); rank++; } shortcutManager.setDynamicShortcuts(choiceShortcuts); }
public static int getNextRailNumber(ShortcutManager shortcutManager) { List<ShortcutInfo> shortcuts = shortcutManager.getPinnedShortcuts(); int newRailId = -1; for (ShortcutInfo shortcutInfo : shortcuts) { String id = shortcutInfo.getId(); if (isRailShortcut(id)) { int railId = getRailNumber(id); if (railId > newRailId) { newRailId = railId; } } } newRailId++; return newRailId; }
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; }
/** * Handle the incoming intent of shortcut * Returns true if shortcut was handled, false otherwise */ private boolean handleIntent() { String shortcutID; if (getIntent().getAction() != null && getIntent().getAction().equals(SHORTCUT_INTENT_STRING)) { shortcutID = SHORTCUT_ID; doToggle(); } else return false; /* * On Android 7.0 or below, bail out from now * This is because app shortcuts are not supported by default in those android versions * It however is supported in 3rd party launchers like nova launcher. * As android API guidelines suggest to reportShortcutUsed(), but that can be done only on API 25 */ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return true; ShortcutManager shortcutManager = this.getSystemService(ShortcutManager.class); shortcutManager.reportShortcutUsed(shortcutID); return true; }
/** * Shortucts features on android N * @param context */ public static void create(Context context) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); Icon pause = Icon.createWithResource(context, R.drawable.ic_shortcut_aw_ic_pause); ShortcutInfo pauses = new ShortcutInfo.Builder(context, Constants.PAUSE_SHORTCUTS) .setShortLabel("Pause") .setIcon(pause) .setIntent(shortcut(context,2)) .build(); Icon play = Icon.createWithResource(context, R.drawable.ic_shortcut_aw_ic_play); ShortcutInfo plays = new ShortcutInfo.Builder(context, Constants.PLAY_SHORTCUTS) .setShortLabel("Play") .setIcon(play) .setIntent(shortcut(context, 1)) .build(); ArrayList<ShortcutInfo> shortcuts = new ArrayList<>(); shortcuts.add(plays); shortcuts.add(pauses); shortcutManager.setDynamicShortcuts(shortcuts); } }
public static void updateShortcut(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { Log.d("Daedalus", "Updating shortcut"); //shortcut! String notice = context.getString(R.string.button_text_activate); boolean activate = true; ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (DaedalusVpnService.class.getName().equals(service.service.getClassName())) { notice = context.getString(R.string.button_text_deactivate); activate = false; } } 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_ACTIVATE : MainActivity.LAUNCH_ACTION_DEACTIVATE)) .build(); ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(SHORTCUT_SERVICE); shortcutManager.addDynamicShortcuts(Collections.singletonList(info)); } }
@TargetApi(25) public static void updateShortcuts(Activity parentActivity, ShortcutManager shortcutManager, int sizeOfForumFavArray) { ArrayList<ShortcutInfo> listOfShortcuts = new ArrayList<>(); int sizeOfShortcutArray = (sizeOfForumFavArray > 4 ? 4 : sizeOfForumFavArray); for (int i = 0; i < sizeOfShortcutArray; ++i) { String currentShortcutLink = PrefsManager.getStringWithSufix(PrefsManager.StringPref.Names.FORUM_FAV_LINK, String.valueOf(i)); String currentShortcutName = PrefsManager.getStringWithSufix(PrefsManager.StringPref.Names.FORUM_FAV_NAME, String.valueOf(i)); ShortcutInfo newShortcut = new ShortcutInfo.Builder(parentActivity, String.valueOf(i) + "_" + currentShortcutLink) .setShortLabel(currentShortcutName) .setLongLabel(currentShortcutName) .setIcon(Icon.createWithResource(parentActivity, R.mipmap.ic_shortcut_forum)) .setIntent(new Intent(MainActivity.ACTION_OPEN_LINK, Uri.parse(currentShortcutLink))).build(); listOfShortcuts.add(newShortcut); } try { shortcutManager.setDynamicShortcuts(listOfShortcuts); } catch (Exception e) { /* À ce qu'il parait ça peut crash "when the user is locked", je sais pas ce que ça * veut dire donc dans le doute je mets ça là. */ } }
private void setupShortcuts() { mShortcutManager = getSystemService(ShortcutManager.class); List<ShortcutInfo> infos = new ArrayList<>(); for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) { Intent intent = new Intent(this, MessageActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.putExtra("msg", "我和" + mAdapter.getItem(i) + "的对话"); ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i) .setShortLabel(mAdapter.getItem(i)) .setLongLabel("联系人:" + mAdapter.getItem(i)) .setIcon(Icon.createWithResource(this, R.drawable.icon)) .setIntent(intent) .build(); infos.add(info); // manager.addDynamicShortcuts(Arrays.asList(info)); } mShortcutManager.setDynamicShortcuts(infos); }
/** * 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; } }
@RequiresApi(api = Build.VERSION_CODES.N_MR1) private static void updataShortcuts(Context context, List<AppInfo> items) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); List<ShortcutInfo> shortcutInfoList = new ArrayList<>(); int max = shortcutManager.getMaxShortcutCountPerActivity(); for (int i = 0; i < max && i < items.size(); i++) { AppInfo appInfo = items.get(i); ShortcutInfo.Builder shortcut = new ShortcutInfo.Builder(context, appInfo.packageName); shortcut.setShortLabel(appInfo.appName); shortcut.setLongLabel(appInfo.appName); shortcut.setIcon( Icon.createWithBitmap(drawableToBitmap(LocalImageLoader.getDrawable(context, appInfo)))); Intent intent = new Intent(context, AppPermissionActivity.class); intent.putExtra(AppPermissionActivity.EXTRA_APP_PKGNAME, appInfo.packageName); intent.putExtra(AppPermissionActivity.EXTRA_APP_NAME, appInfo.appName); intent.setAction(Intent.ACTION_DEFAULT); shortcut.setIntent(intent); shortcutInfoList.add(shortcut.build()); } shortcutManager.setDynamicShortcuts(shortcutInfoList); }
/** * Remove the given card id from the app shortcuts, if such a * shortcut exists. */ @TargetApi(25) static void removeShortcut(Context context, int cardId) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); List<ShortcutInfo> list = shortcutManager.getDynamicShortcuts(); String shortcutId = Integer.toString(cardId); for(int index = 0; index < list.size(); index++) { if(list.get(index).getId().equals(shortcutId)) { list.remove(index); break; } } shortcutManager.setDynamicShortcuts(list); } }
@Override public void setShortcut(String did, String manufacturer, String deviceName) { //Home screen shortcut for favourite device if (Build.VERSION.SDK_INT < 25) return; ShortcutManager sM = getSystemService(ShortcutManager.class); sM.removeAllDynamicShortcuts(); Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.putExtra(Constants.EXTRA_DEVICE_ID, did); ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcut1") .setIntent(intent) .setLongLabel(manufacturer + " " + deviceName) .setShortLabel(deviceName) .setIcon(Icon.createWithResource(this, R.drawable.ic_device_placeholder)) .build(); sM.setDynamicShortcuts(Collections.singletonList(shortcut)); }
@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 = new Intent(context, PostNewDesignerNewsStory.class); intent.setAction(Intent.ACTION_VIEW); 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)); }
@RequiresApi(api = Build.VERSION_CODES.N_MR1) public static void addSearchShortcut(Context context, String searchWithPartyTitle) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); Intent openSearch = new Intent(context.getApplicationContext(), SearchActivity.class); openSearch.putExtra(ApplicationConstants.PARTY_NAME_EXTRA, searchWithPartyTitle); openSearch.setAction(Intent.ACTION_VIEW); ShortcutInfo shortcut = new ShortcutInfo.Builder(context, ApplicationConstants.SEARCH_SHORTCUT_ID) .setShortLabel("Search songs") .setLongLabel("Search for songs to add to the queue") .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_search)) .setIntent(openSearch) .build(); shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut)); }
/** * Adds a "New incognito tab" dynamic launcher shortcut. * @param context The context used to retrieve the system {@link ShortcutManager}. * @return True if addint the shortcut has succeeded. False if the call fails due to rate * limiting. See {@link ShortcutManager#addDynamicShortcuts}. */ @TargetApi(Build.VERSION_CODES.N_MR1) private static boolean addIncognitoLauncherShortcut(Context context) { Intent intent = new Intent(LauncherShortcutActivity.ACTION_OPEN_NEW_INCOGNITO_TAB); intent.setPackage(context.getPackageName()); intent.setClass(context, LauncherShortcutActivity.class); ShortcutInfo shortcut = new ShortcutInfo.Builder(context, DYNAMIC_OPEN_NEW_INCOGNITO_TAB_ID) .setShortLabel(context.getResources().getString( R.string.accessibility_tabstrip_incognito_identifier)) .setLongLabel( context.getResources().getString(R.string.menu_new_incognito_tab)) .setIcon(Icon.createWithResource(context, R.drawable.shortcut_incognito)) .setIntent(intent) .build(); ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); return shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut)); }
@Override @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) { settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { shortcutManager = getSystemService(ShortcutManager.class); } activity = this; PRNGFixes.apply(); // If user opens app with permission granted then revokes and returns, // prevent attempt to create password list fragment if (savedInstanceState != null && (!settings.getBoolean("git_external", false) || ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { savedInstanceState = null; } super.onCreate(savedInstanceState); setContentView(R.layout.activity_pwdstore); }
private void setLauncherShortcuts() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); if(shortcutManager.getDynamicShortcuts().size() == 0) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName(BuildConfig.APPLICATION_ID, TaskerQuickActionsActivity.class.getName()); intent.putExtra("launched-from-app", true); ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "quick_actions") .setShortLabel(getString(R.string.label_quick_actions)) .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon)) .setIntent(intent) .build(); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); } } }
public static void updateShortcuts(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); shortcutManager.removeAllDynamicShortcuts(); List<PreferredJourney> l; if ((l = PreferredStationsPreferences.getAllPreferredJourneys(context)).size() > 0) { shortcutManager.addDynamicShortcuts(setShortcuts(context, l.get(0))); } } }
private void enableShortcuts() { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManager manager = getSystemService(ShortcutManager.class); ShortcutInfo addPkg = new ShortcutInfo.Builder(this, "shortcut_add_package") .setLongLabel(getString(R.string.shortcut_label_add_package)) .setShortLabel(getString(R.string.shortcut_label_add_package)) .setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_add)) .setIntent( new Intent(OnboardingActivity.this, AddPackageActivity.class) .setAction(Intent.ACTION_VIEW) .addCategory(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION) ) .build(); ShortcutInfo scanCode = new ShortcutInfo.Builder(this, "shortcut_scan_code") .setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_filter_center_focus)) .setLongLabel(getString(R.string.shortcut_label_scan_code)) .setShortLabel(getString(R.string.shortcut_label_scan_code)) .setIntent( new Intent(OnboardingActivity.this, AddPackageActivity.class) .setAction("io.github.marktony.espresso.mvp.addpackage.AddPackageActivity") .addCategory(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION) ) .build(); ShortcutInfo search = new ShortcutInfo.Builder(this, "shortcut_search") .setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_search)) .setLongLabel(getString(R.string.shortcut_label_search)) .setShortLabel(getString(R.string.shortcut_label_search)) .setIntent( new Intent(OnboardingActivity.this, SearchActivity.class) .setAction(Intent.ACTION_VIEW) .addCategory(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION) ) .build(); manager.setDynamicShortcuts(Arrays.asList(addPkg, scanCode, search)); } }
private void addDynamicShortcut(Place place) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); String name = HTTextUtils.isEmpty(place.getName()) ? place.getAddress() : place.getName(); int count = 0; if (shortcutManager.getDynamicShortcuts() != null) { count = shortcutManager.getDynamicShortcuts().size(); } if (count > 2) { String id = shortcutManager.getDynamicShortcuts().get(0).getId(); List<String> shortcutIds = new ArrayList<>(); shortcutIds.add(id); shortcutManager.removeDynamicShortcuts(shortcutIds); } List<ShortcutInfo> shortcut = new ArrayList<>(); shortcut.add(0, new ShortcutInfo.Builder(this, place.getLocation().getLatLng().toString()) .setShortLabel(name) .setIcon(Icon.createWithResource(this, R.drawable.ic_marker_gray)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("share.location://hypertrack"))) .build()); shortcutManager.addDynamicShortcuts(shortcut); } } catch (Exception e) { Crashlytics.logException(e); } }
@TargetApi(26) private void createPinnedShortcut(Card card, Icon icon) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager.isRequestPinShortcutSupported()) { ShortcutInfo pin = new ShortcutInfo.Builder(context, card.getName() + "_pin") .setShortLabel(card.getName()) .setIntent(createCardShortcutIntent(card)) .setIcon(icon) .build(); shortcutManager.requestPinShortcut(pin, null); } }
@TargetApi(26) public void removePinnedShortcut(Card card) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager.isRequestPinShortcutSupported()) { List<String> toRemove = new ArrayList<>(); toRemove.add(card.getName() + "_pin"); shortcutManager.disableShortcuts(toRemove, context.getString(R.string.error_no_longer_in_library)); } }
private static void sendShortcutBroadcast(Context context, String shortcutId, Intent shortcutIntent, String title, Bitmap bitmap) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Icon icon = Icon.createWithBitmap(bitmap); ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, shortcutId) .setIcon(icon) .setShortLabel(title) .setIntent(shortcutIntent) .build(); ShortcutManager manager = context.getSystemService(ShortcutManager.class); if (manager != null) { manager.requestPinShortcut(shortcutInfo, null); } } else { final Context app = context.getApplicationContext(); Intent intent = new Intent(); intent.setAction(SHURTCUT_ACTION); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap); context.sendBroadcast(intent); Completable.complete() .observeOn(Injection.provideMainThreadScheduler()) .subscribe(() -> Toast.makeText(app, R.string.success, Toast.LENGTH_SHORT).show()); } }
public void go() { if (Build.VERSION.SDK_INT < 25) { return; } if (mShortcutManager==null) mShortcutManager = mActivity.getSystemService(ShortcutManager.class); try { if (mShortcutInfos!=null && mShortcutInfos.size()>0) mShortcutManager.setDynamicShortcuts(mShortcutInfos); } catch (Exception e) { e.printStackTrace(); } }
@Before public void setUp() { Shortbread.shortcutsSet = false; Shortbread.activityLifecycleCallbacksSet = false; MockitoAnnotations.initMocks(this); when(activity.getApplicationContext()).thenReturn(application); when(application.getApplicationContext()).thenReturn(application); when(application.getSystemService(ShortcutManager.class)).thenReturn(shortcutManager); when(activity.getIntent()).thenReturn(intent); }
private void saveBookmarks() { SharedPreferences.Editor editor = prefs.edit(); for (int i = 0; i < bookmarks.size(); i++) { editor.putInt(PREF_BOOKMARK + i, bookmarks.get(i)); } editor.putInt(PREF_BOOKMARKS_LENGTH, bookmarks.size()); editor.apply(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { Collections.sort(bookmarks); ShortcutManager manager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE); if (manager != null) { List<ShortcutInfo> shortcuts = new ArrayList<>(); for (int bpm : bookmarks) { shortcuts.add( new ShortcutInfo.Builder(this, String.valueOf(bpm)) .setShortLabel(getString(R.string.bpm, String.valueOf(bpm))) .setIcon(Icon.createWithResource(this, R.drawable.ic_note)) .setIntent(getBookmarkIntent(bpm)) .build() ); } manager.setDynamicShortcuts(shortcuts); } } updateBookmarks(true); }
private void addShowScenarioShortcut() { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); ShortcutInfo showScenarioShortcut = new ShortcutInfo.Builder(this, IdUtil.getRandomUniqueShortcutId()) .setShortLabel(getString(R.string.shortcut_title_show_scenario)) .setLongLabel(getString(R.string.shortcut_title_show_scenario)) .setDisabledMessage(getString(R.string.shortcut_disabled_message)) .setIcon(Icon.createWithBitmap(BitmapUtil.getShortcutIcon( this, ContextCompat.getColor(this, R.color.color_shortcut_background), R.drawable.ic_info_outline_black_24dp, ContextCompat.getColor(this, R.color.color_primary)))) .setIntent(IntentUtil.choice(this, mChoice)) .setRank(0) .build(); shortcutManager.addDynamicShortcuts(Collections.singletonList(showScenarioShortcut)); }
private void disableExistingShortcuts() { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); List<String> shortcutIds = new ArrayList<>(); for (ShortcutInfo shortcutInfo: shortcutManager.getDynamicShortcuts()) { shortcutIds.add(shortcutInfo.getId()); } shortcutManager.disableShortcuts(shortcutIds); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.removeAllDynamicShortcuts(); int newWallId = ShortcutsUtils.getNextRailNumber(shortcutManager); ShortcutInfo ballShortcut = ShortcutsUtils.createTrainShortcut(this); ShortcutInfo wallShortcut = ShortcutsUtils.createRailShortcut(this, newWallId); shortcutManager.setDynamicShortcuts(Arrays.asList(ballShortcut, wallShortcut)); setContentView(R.layout.activity_main); rootView = findViewById(R.id.activity_main_root); viewPager = (ViewPager) findViewById(R.id.activity_main_view_pager); pagerAdapter = new TutorialViewPagerAdapter(getFragmentManager()); viewPager.setAdapter(pagerAdapter); rootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finishActivity(); } }); }
@Override protected void doCreateView(View view) { setHasOptionsMenu(true); mAppListActivity = ((AppListActivity) getActivity()); new AppsPresenter(mAppListActivity, this); RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); mRecyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.addItemDecoration(new DividerItemDecoration(mAppListActivity, layoutManager.getOrientation())); mAppInfoDBController = new AppInfoDBController(mAppListActivity); Set<String> appPackageSet = new HashSet<>(); if (mAppListActivity.getIntent().getBooleanExtra(EXTRA_MANUAL_SHORTCUT, false)) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManager manager = mAppListActivity.getSystemService(ShortcutManager.class); for (ShortcutInfo shortcutInfo : manager.getDynamicShortcuts()) { appPackageSet.add(shortcutInfo.getId()); } } } else { List<AppInfo> appInfoSet = mAppInfoDBController.getDisableApps(AppInfoDBOpenHelper.TABLE_NAME_APP_INFO); for (AppInfo appInfo : appInfoSet) { appPackageSet.add(appInfo.getAppPackageName()); } } mAppListAdapter = new AppListAdapter(mAppListActivity, mRecyclerView, appPackageSet); mRecyclerView.setAdapter(mAppListAdapter); int tabCategory = getArguments().getInt(TAB_CATEGORY); mPresenter.getApps(tabCategory == 0 ? AppsRepository.APPS_FLAG_USER : AppsRepository.APPS_FLAG_SYSTEM); initListener(); }
public ShortcutsManager(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { mContext = context; mShortcutManager = context.getSystemService(ShortcutManager.class); mDBController = new AppInfoDBController(context); } }
@TargetApi(25) public void report(Contact contact) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class); shortcutManager.reportShortcutUsed(getShortcutId(contact)); } }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); if (data != null && data.getCount() > 0) { mEmptyView.setVisibility(View.GONE); } else { mEmptyView.setVisibility(View.VISIBLE); } if (VERSION.SDK_INT >= VERSION_CODES.N_MR1 && data != null) { data.moveToFirst(); List<ShortcutInfo> shortcuts = new ArrayList<>(); for (int i = 1; i < 5 && !data.isAfterLast(); ) { ChatListDataModel item = new ChatListDataModel(data); if (item.name != null && !item.name.isEmpty()) { Intent intent = new Intent(getContext(), ChatActivity.class); intent.putExtra("type", "contact_data_model"); intent.putExtra("data", item.getContactItemDataModel()); ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(getContext(), "id" + i) .setShortLabel(item.name) .setLongLabel(String.format("%s%s", getString(item.is_bot ? R.string.Bot_label : R.string.Contact_label), item.name)) .setIntent(intent) .setIcon(Icon.createWithResource(getContext(), R.drawable.empty_profile_pic)) .build(); shortcuts.add(shortcutInfo); i++; } data.moveToNext(); } ShortcutManager shortcutManager = getContext().getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(shortcuts); } }
/** * Removes the given channel as shortcut from the launcher icon. * Only call on api level >= 25. * * @param context to access system services * @param channelId id of the channel you want to remove from shorcut menu */ @TargetApi(25) public static void removeShortcutForChannel(Context context, String channelId) { ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); if (shortcutManager != null) { shortcutManager.removeDynamicShortcuts(Collections.singletonList(channelId)); } }