Java Code Examples for android.content.pm.ShortcutInfo#Builder

The following examples show how to use android.content.pm.ShortcutInfo#Builder . 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: Helper.java    From AppOpsX with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: ShortcutHelper.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
private ShortcutInfo.Builder setSiteInformation(ShortcutInfo.Builder b, Uri uri) {
    // TODO Get the actual site <title> and use it.
    // TODO Set the current locale to accept-language to get localized title.
    b.setShortLabel(uri.getHost());
    b.setLongLabel(uri.toString());

    Bitmap bmp = fetchFavicon(uri);
    if (bmp != null) {
        b.setIcon(Icon.createWithBitmap(bmp));
    } else {
        b.setIcon(Icon.createWithResource(mContext, R.drawable.link));
    }

    return b;
}
 
Example 3
Source File: ShortcutHelper.java    From android-AppShortcuts with Apache License 2.0 5 votes vote down vote up
private ShortcutInfo.Builder setSiteInformation(ShortcutInfo.Builder b, Uri uri) {
    // TODO Get the actual site <title> and use it.
    // TODO Set the current locale to accept-language to get localized title.
    b.setShortLabel(uri.getHost());
    b.setLongLabel(uri.toString());

    Bitmap bmp = fetchFavicon(uri);
    if (bmp != null) {
        b.setIcon(Icon.createWithBitmap(bmp));
    } else {
        b.setIcon(Icon.createWithResource(mContext, R.drawable.link));
    }

    return b;
}
 
Example 4
Source File: MainActivity.java    From c3nav-android with Apache License 2.0 5 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.N_MR1)
public ShortcutInfo getShortcutInfo(@NonNull String id, @NonNull String shortLabel, String longLabel, @NonNull Icon icon, @NonNull String action, Uri data) {
    ShortcutInfo.Builder shortcutInfoBuilder = new ShortcutInfo.Builder(getApplicationContext(), action)
            .setShortLabel(shortLabel)
            .setIcon(icon)
            .setIntent(getShortcutIntet(action, null));
    if (longLabel != null) shortcutInfoBuilder.setLongLabel(longLabel);
    return shortcutInfoBuilder.build();
}
 
Example 5
Source File: ShortcutHelper.java    From user-interface-samples with Apache License 2.0 4 votes vote down vote up
private ShortcutInfo.Builder setExtras(ShortcutInfo.Builder b) {
    final PersistableBundle extras = new PersistableBundle();
    extras.putLong(EXTRA_LAST_REFRESH, System.currentTimeMillis());
    b.setExtras(extras);
    return b;
}
 
Example 6
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 7
Source File: ShortcutHelper.java    From android-AppShortcuts with Apache License 2.0 4 votes vote down vote up
private ShortcutInfo.Builder setExtras(ShortcutInfo.Builder b) {
    final PersistableBundle extras = new PersistableBundle();
    extras.putLong(EXTRA_LAST_REFRESH, System.currentTimeMillis());
    b.setExtras(extras);
    return b;
}
 
Example 8
Source File: ShortcutHelper.java    From user-interface-samples with Apache License 2.0 3 votes vote down vote up
private ShortcutInfo createShortcutForUrl(String urlAsString) {
    Log.i(TAG, "createShortcutForUrl: " + urlAsString);

    final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mContext, urlAsString);

    final Uri uri = Uri.parse(urlAsString);
    b.setIntent(new Intent(Intent.ACTION_VIEW, uri));

    setSiteInformation(b, uri);
    setExtras(b);

    return b.build();
}
 
Example 9
Source File: ShortcutHelper.java    From android-AppShortcuts with Apache License 2.0 3 votes vote down vote up
private ShortcutInfo createShortcutForUrl(String urlAsString) {
    Log.i(TAG, "createShortcutForUrl: " + urlAsString);

    final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mContext, urlAsString);

    final Uri uri = Uri.parse(urlAsString);
    b.setIntent(new Intent(Intent.ACTION_VIEW, uri));

    setSiteInformation(b, uri);
    setExtras(b);

    return b.build();
}