Java Code Examples for android.content.pm.ShortcutManager#requestPinShortcut()

The following examples show how to use android.content.pm.ShortcutManager#requestPinShortcut() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: U.java    From Taskbar with Apache License 2.0 6 votes vote down vote up
public static void pinAppShortcut(Context context) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);

        if(mShortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, "freeform_mode").build();

            mShortcutManager.requestPinShortcut(pinShortcutInfo, null);
        } else
            showToastLong(context, R.string.tb_pin_shortcut_not_supported);
    } else {
        Intent intent = ShortcutUtils.getShortcutIntent(context);
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        intent.putExtra("duplicate", false);

        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo defaultLauncher = context.getPackageManager().resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);

        intent.setPackage(defaultLauncher.activityInfo.packageName);
        context.sendBroadcast(intent);

        showToast(context, R.string.tb_shortcut_created);
    }
}
 
Example 2
Source File: Utility.java    From Shelter with Do What The F*ck You Want To Public License 5 votes vote down vote up
public static void createLauncherShortcut(Context context, Intent launchIntent, Icon icon, String id, String label) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);

        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo info = new ShortcutInfo.Builder(context, id)
                    .setIntent(launchIntent)
                    .setIcon(icon)
                    .setShortLabel(label)
                    .setLongLabel(label)
                    .build();
            Intent addIntent = shortcutManager.createShortcutResultIntent(info);
            shortcutManager.requestPinShortcut(info,
                    PendingIntent.getBroadcast(context, 0, addIntent, 0).getIntentSender());
        } else {
            // TODO: Maybe implement this for launchers without pin shortcut support?
            // TODO: Should be the same with the fallback for Android < O
            // for now just show unsupported
            Toast.makeText(context, context.getString(R.string.unsupported_launcher), Toast.LENGTH_LONG).show();
        }
    } else {
        Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, drawableToBitmap(icon.loadDrawable(context)));
        context.sendBroadcast(shortcutIntent);
        Toast.makeText(context, R.string.shortcut_create_success, Toast.LENGTH_SHORT).show();
    }
}
 
Example 3
Source File: ShortCut8_0Activity.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
public static void testShortCut(Context context) {
    //(若考虑兼容性可用ShortcutManagerCompat)
    ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
    boolean requestPinShortcutSupported = shortcutManager.isRequestPinShortcutSupported();
    Log.d("realmo", "启动器是否支持固定快捷方式: "+requestPinShortcutSupported);

    if (requestPinShortcutSupported) {

        Intent shortcutInfoIntent = new Intent(context, ShortCutActvivity.class);

        shortcutInfoIntent.setAction(Intent.ACTION_VIEW);

        ShortcutInfo info = new ShortcutInfo.Builder(context, "tzw")
                .setIcon(Icon.createWithResource(context, R.mipmap.momo))
                .setShortLabel("O系统短")
                .setLongLabel("O系统长")
                .setIntent(shortcutInfoIntent)
                .build();

        //当添加快捷方式的确认弹框弹出来时,将被回调CallBackReceiver里面的onReceive方法
        PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, CallBackReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

        shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());

    }
}
 
Example 4
Source File: LauncherIconCreator.java    From ActivityLauncher with ISC License 5 votes vote down vote up
@TargetApi(26)
private static void doCreateShortcut(Context context, String appName, Drawable draw, Intent intent) {
    ShortcutManager shortcutManager = Objects.requireNonNull(context.getSystemService(ShortcutManager.class));

    if (shortcutManager.isRequestPinShortcutSupported()) {
        Bitmap bitmap = getBitmapFromDrawable(draw);
        intent.setAction(Intent.ACTION_CREATE_SHORTCUT);


        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, appName)
                .setShortLabel(appName)
                .setLongLabel(appName)
                .setIcon(Icon.createWithBitmap(bitmap))
                .setIntent(intent)
                .build();

        shortcutManager.requestPinShortcut(shortcutInfo, null);
    } else {
        new AlertDialog.Builder(context)
                .setTitle(context.getText(R.string.error_creating_shortcut))
                .setMessage(context.getText(R.string.error_verbose_pin_shortcut))
                .setPositiveButton(context.getText(android.R.string.ok), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Just close dialog don't do anything
                        dialog.cancel();
                    }
                })
                .show();
    }
}
 
Example 5
Source File: LauncherShortcutUtils.java    From FreezeYou with Apache License 2.0 4 votes vote down vote up
static void requestCreateShortCut(String title, Intent intent, Drawable icon, String id, Context context, Bitmap bm) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        requestCreateShortCutOldApi(title, intent, icon, context, bm);
    } else {
        ShortcutManager mShortcutManager =
                context.getSystemService(ShortcutManager.class);
        if (mShortcutManager != null) {
            if (mShortcutManager.isRequestPinShortcutSupported()) {
                ShortcutInfo.Builder shortcutInfoBuilder =
                        new ShortcutInfo.Builder(context, id);
                shortcutInfoBuilder.setIcon(
                        Icon.createWithBitmap(bm == null ? getBitmapFromDrawable(icon) : bm));
                shortcutInfoBuilder.setIntent(intent);
                shortcutInfoBuilder.setShortLabel(title);
                shortcutInfoBuilder.setLongLabel(title);

                ShortcutInfo pinShortcutInfo = shortcutInfoBuilder.build();
                // Create the PendingIntent object only if your app needs to be notified
                // that the user allowed the shortcut to be pinned. Note that, if the
                // pinning operation fails, your app isn't notified. We assume here that the
                // app has implemented a method called createShortcutResultIntent() that
                // returns a broadcast intent.
                Intent pinnedShortcutCallbackIntent =
                        mShortcutManager.createShortcutResultIntent(pinShortcutInfo);

                // Configure the intent so that your app's broadcast receiver gets
                // the callback successfully.
                PendingIntent successCallback = PendingIntent.getBroadcast(context, id.hashCode(),
                        pinnedShortcutCallbackIntent, 0);

                mShortcutManager.requestPinShortcut(pinShortcutInfo,
                        successCallback.getIntentSender());
                showToast(context, R.string.requested);
            } else {
                requestCreateShortCutOldApi(title, intent, icon, context, bm);
            }
        } else {
            requestCreateShortCutOldApi(title, intent, icon, context, bm);
        }
    }
}
 
Example 6
Source File: DynamicShortcutManager.java    From volume_control_android with MIT License 4 votes vote down vote up
public static boolean installPinnedShortcut(Activity activity, SoundProfile soundProfile) {
    ShortcutInfo info = createShortcutInfo(activity, soundProfile);
    ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
    return shortcutManager.requestPinShortcut(info, null);
}
 
Example 7
Source File: BaseNoteFragment.java    From nextcloud-notes with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Main-Menu-Handler
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_cancel:
            if (originalNote == null) {
                db.deleteNoteAndSync(ssoAccount, note.getId());
            } else {
                db.updateNoteAndSync(ssoAccount, localAccount, originalNote, null, null);
            }
            listener.close();
            return true;
        case R.id.menu_delete:
            db.deleteNoteAndSync(ssoAccount, note.getId());
            listener.close();
            return true;
        case R.id.menu_favorite:
            db.toggleFavorite(ssoAccount, note, null);
            listener.onNoteUpdated(note);
            prepareFavoriteOption(item);
            return true;
        case R.id.menu_category:
            showCategorySelector();
            return true;
        case R.id.menu_title:
            showEditTitleDialog();
            return true;
        case R.id.menu_move:
            AccountPickerDialogFragment.newInstance(note.getAccountId()).show(requireActivity().getSupportFragmentManager(), BaseNoteFragment.class.getSimpleName());
            return true;
        case R.id.menu_share:
            ShareUtil.openShareDialog(requireContext(), note.getTitle(), note.getContent());
            return false;
        case MENU_ID_PIN:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ShortcutManager shortcutManager = requireActivity().getSystemService(ShortcutManager.class);

                if (shortcutManager != null) {
                    if (shortcutManager.isRequestPinShortcutSupported()) {
                        Intent intent = new Intent(getActivity(), EditNoteActivity.class);
                        intent.putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getId());
                        intent.setAction(ACTION_SHORTCUT);

                        ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(getActivity(), note.getId() + "")
                                .setShortLabel(note.getTitle())
                                .setIcon(Icon.createWithResource(requireActivity().getApplicationContext(), note.isFavorite() ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_grey_ccc_24dp))
                                .setIntent(intent)
                                .build();

                        Intent pinnedShortcutCallbackIntent =
                                shortcutManager.createShortcutResultIntent(pinShortcutInfo);

                        PendingIntent successCallback = PendingIntent.getBroadcast(getActivity(), /* request code */ 0,
                                pinnedShortcutCallbackIntent, /* flags */ 0);

                        shortcutManager.requestPinShortcut(pinShortcutInfo,
                                successCallback.getIntentSender());
                    } else {
                        Log.i(TAG, "RequestPinShortcut is not supported");
                    }
                } else {
                    Log.e(TAG, "ShortcutManager is null");
                }
            }

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}