Java Code Examples for androidx.core.content.pm.ShortcutManagerCompat#requestPinShortcut()

The following examples show how to use androidx.core.content.pm.ShortcutManagerCompat#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: ConversationActivity.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static void addIconToHomeScreen(@NonNull Context context,
                                        @NonNull Bitmap bitmap,
                                        @NonNull Recipient recipient)
{
  IconCompat icon = IconCompat.createWithAdaptiveBitmap(bitmap);
  String     name = recipient.isLocalNumber() ? context.getString(R.string.note_to_self)
                                                : recipient.getDisplayName(context);

  ShortcutInfoCompat shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, recipient.getId().serialize() + '-' + System.currentTimeMillis())
                                                                .setShortLabel(name)
                                                                .setIcon(icon)
                                                                .setIntent(ShortcutLauncherActivity.createIntent(context, recipient.getId()))
                                                                .build();

  if (ShortcutManagerCompat.requestPinShortcut(context, shortcutInfoCompat, null)) {
    Toast.makeText(context, context.getString(R.string.ConversationActivity_added_to_home_screen), Toast.LENGTH_LONG).show();
  }

  bitmap.recycle();
}
 
Example 2
Source File: PhonkScriptHelper.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public static void addShortcut(Context c, String folder, String name) {
    Project p = new Project(folder, name);

    Intent.ShortcutIconResource icon;
    icon = Intent.ShortcutIconResource.fromContext(c, R.drawable.app_icon);

    if (ShortcutManagerCompat.isRequestPinShortcutSupported(c)) {
        Intent shortcutIntent = new Intent(c, AppRunnerActivity.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.putExtra(Project.NAME, p.getName());
        shortcutIntent.putExtra(Project.FOLDER, p.getFolder());
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(c, folder + "/" + name)
                .setIntent(shortcutIntent) // !!! intent's action must be set on oreo
                .setShortLabel(name)
                .setIcon(IconCompat.createWithResource(c, R.drawable.app_icon))
                .build();
        ShortcutManagerCompat.requestPinShortcut(c, shortcutInfo, null);
    }
}
 
Example 3
Source File: MainActivity.java    From c3nav-android with Apache License 2.0 6 votes vote down vote up
@JavascriptInterface
public void createShortcut(String url, String title) {
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.setData(Uri.parse(url));

    if (!ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
        Toast.makeText(MainActivity.this, R.string.shortcut_not_supported, Toast.LENGTH_LONG).show();
    }

    final ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), url)
            .setShortLabel(title)
            .setLongLabel(title)
            .setIcon(IconCompat.createWithResource(getApplicationContext(), R.mipmap.ic_launcher_36c3))
            .setIntent(shortcutIntent)
            .build();

    ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        Toast.makeText(MainActivity.this, R.string.shortcut_created, Toast.LENGTH_SHORT).show();
    }

}
 
Example 4
Source File: AppsListFragment.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onContextItemSelected(MenuItem item) {
	AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
	int index = info.position;
	AppItem appItem = adapter.getItem(index);
	switch (item.getItemId()) {
		case R.id.action_context_shortcut:
			Bitmap bitmap = BitmapFactory.decodeFile(appItem.getImagePathExt());
			Intent launchIntent = new Intent(Intent.ACTION_DEFAULT,
					Uri.parse(appItem.getPath()), getActivity(), ConfigActivity.class);
			ShortcutInfoCompat.Builder shortcutInfoCompatBuilder =
					new ShortcutInfoCompat.Builder(getActivity(), appItem.getTitle())
							.setIntent(launchIntent)
							.setShortLabel(appItem.getTitle());
			if (bitmap != null) {
				shortcutInfoCompatBuilder.setIcon(IconCompat.createWithBitmap(bitmap));
			} else {
				shortcutInfoCompatBuilder.setIcon(IconCompat.createWithResource(getActivity(), R.mipmap.ic_launcher));
			}
			ShortcutManagerCompat.requestPinShortcut(getActivity(), shortcutInfoCompatBuilder.build(), null);
			break;
		case R.id.action_context_rename:
			showRenameDialog(index);
			break;
		case R.id.action_context_settings:
			Config.startApp(getActivity(), appItem.getPath(), true);
			break;
		case R.id.action_context_delete:
			showDeleteDialog(index);
			break;
	}
	return super.onContextItemSelected(item);
}
 
Example 5
Source File: HomeScreen.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create a shortcut via the AppCompat's shortcut manager.
 * <p>
 * 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, boolean requestDesktop) {
    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, requestDesktop))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcut, null);
    }
}
 
Example 6
Source File: DemoSettingFragment.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
private void createShortcut() {
    Activity activity = getActivity();
    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));
        }
    }
}