List of usage examples for android.support.v4.content.pm ShortcutManagerCompat requestPinShortcut
public static boolean requestPinShortcut(@NonNull final Context context, @NonNull ShortcutInfoCompat shortcut, @Nullable final IntentSender callback)
From source file:org.mozilla.focus.shortcut.HomeScreen.java
/** * Create a shortcut via the AppCompat's shortcut manager. * <p>// ww w . j a v a 2 s .c o m * On Android versions up to 7 shortcut will be created via system broadcast internally. * <p> * On Android 8+ the user will have the ability to add the shortcut manually * or let the system place it automatically. */ private static void installShortCutViaManager(Context context, Bitmap bitmap, String url, String title, boolean blockingEnabled) { if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) { final IconCompat icon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? IconCompat.createWithAdaptiveBitmap(bitmap) : IconCompat.createWithBitmap(bitmap); final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, UUID.randomUUID().toString()).setShortLabel(title).setLongLabel(title).setIcon(icon) .setIntent(createShortcutIntent(context, url, blockingEnabled)).build(); ShortcutManagerCompat.requestPinShortcut(context, shortcut, null); } }
From source file:net.gsantner.opoc.util.ShareUtil.java
/** * Try to create a new desktop shortcut on the launcher. Add permissions: * <uses-permission android:name="android.permission.INSTALL_SHORTCUT" /> * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> * * @param intent The intent to be invoked on tap * @param iconRes Icon resource for the item * @param title Title of the item/*ww w. ja v a2 s . c o m*/ */ public void createLauncherDesktopShortcut(Intent intent, @DrawableRes int iconRes, String title) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (intent.getAction() == null) { intent.setAction(Intent.ACTION_VIEW); } ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(_context, Long.toString(new Random().nextLong())).setIntent(intent) .setIcon(IconCompat.createWithResource(_context, iconRes)).setShortLabel(title) .setLongLabel(title).build(); ShortcutManagerCompat.requestPinShortcut(_context, shortcut, null); }
From source file:org.deviceconnect.android.deviceplugin.demo.DemoSettingFragment.java
private void createShortcut() { Activity activity = getActivity();//from w w w . j a va 2 s. c o m if (activity == null) { return; } Context context = activity.getApplicationContext(); Intent shortcut = createDemoPageIntent(); ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(context, CAMERA_DEMO_SHORTCUT_ID) .setIcon(IconCompat.createWithResource(context, getShortcutIconResource(mDemoInstaller))) .setShortLabel(getShortcutShortLabel(mDemoInstaller)) .setLongLabel(getShortcutLongLabel(mDemoInstaller)).setIntent(shortcut); ComponentName mainActivity = getMainActivity(context); if (mainActivity != null) { builder.setActivity(mainActivity); } ShortcutInfoCompat info = builder.build(); boolean result = ShortcutManagerCompat.requestPinShortcut(context, info, null); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // OS 8???OS??????????? if (result) { showShurtcutResult(getString(R.string.demo_page_settings_button_create_shortcut_success)); } else { showShurtcutResult(getString(R.string.demo_page_settings_button_create_shortcut_error)); } } }
From source file:org.thoughtcrime.securesms.conversation.ConversationActivity.java
private void handleAddShortcut() { Log.i(TAG, "Creating home screen shortcut for recipient " + recipient.getAddress()); new AsyncTask<Void, Void, IconCompat>() { @Override//ww w .j a v a2 s . co m protected IconCompat doInBackground(Void... voids) { Context context = getApplicationContext(); IconCompat icon = null; if (recipient.getContactPhoto() != null) { try { Bitmap bitmap = BitmapFactory .decodeStream(recipient.getContactPhoto().openInputStream(context)); bitmap = BitmapUtil.createScaledBitmap(bitmap, 300, 300); icon = IconCompat.createWithAdaptiveBitmap(bitmap); } catch (IOException e) { Log.w(TAG, "Failed to decode contact photo during shortcut creation. Falling back to generic icon.", e); } } if (icon == null) { icon = IconCompat.createWithResource(context, recipient.isGroupRecipient() ? R.mipmap.ic_group_shortcut : R.mipmap.ic_person_shortcut); } return icon; } @Override protected void onPostExecute(IconCompat icon) { Context context = getApplicationContext(); String name = Optional.fromNullable(recipient.getName()) .or(Optional.fromNullable(recipient.getProfileName())).or(recipient.toShortString()); ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, recipient.getAddress().serialize() + '-' + System.currentTimeMillis()).setShortLabel(name) .setIcon(icon) .setIntent(ShortcutLauncherActivity.createIntent(context, recipient.getAddress())) .build(); if (ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null)) { Toast.makeText(context, getString(R.string.ConversationActivity_added_to_home_screen), Toast.LENGTH_LONG).show(); } } }.execute(); }
From source file:com.amaze.filemanager.fragments.MainFragment.java
private void addShortcut(LayoutElementParcelable path) { //Adding shortcut for MainActivity //on Home screen final Context ctx = getContext(); if (!ShortcutManagerCompat.isRequestPinShortcutSupported(ctx)) { Toast.makeText(getActivity(), getString(R.string.addshortcut_not_supported_by_launcher), Toast.LENGTH_SHORT).show(); return;// w w w . java 2 s . c o m } Intent shortcutIntent = new Intent(ctx, MainActivity.class); shortcutIntent.putExtra("path", path.desc); shortcutIntent.setAction(Intent.ACTION_MAIN); shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // Using file path as shortcut id. ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(ctx, path.desc) .setActivity(getMainActivity().getComponentName()) .setIcon(IconCompat.createWithResource(ctx, R.mipmap.ic_launcher)).setIntent(shortcutIntent) .setLongLabel(path.desc).setShortLabel(new File(path.desc).getName()).build(); ShortcutManagerCompat.requestPinShortcut(ctx, info, null); }