private static IconCompat generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) { // Get and tint foreground and background drawables Drawable vectorDrawable = Util.getTintedVectorDrawable(context, iconId, foregroundColor); Drawable backgroundDrawable = Util.getTintedVectorDrawable(context, R.drawable.ic_app_shortcut_background, backgroundColor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AdaptiveIconDrawable adaptiveIconDrawable = new AdaptiveIconDrawable(backgroundDrawable, vectorDrawable); return IconCompat.createWithAdaptiveBitmap(drawableToBitmap(adaptiveIconDrawable)); } else { // Squash the two drawables together LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable}); // Return as an Icon return IconCompat.createWithBitmap(drawableToBitmap(layerDrawable)); } }
public void placeShortcut(Station station) { // credit: https://medium.com/@BladeCoder/using-support-library-26-0-0-you-can-do-bb75911e01e8 if (ShortcutManagerCompat.isRequestPinShortcutSupported(mContext)) { ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(mContext, station.getStationName()) .setShortLabel(station.getStationName()) .setLongLabel(station.getStationName()) .setIcon(IconCompat.createWithBitmap(createShortcutIcon(station))) .setIntent(createShortcutIntent(station)) .build(); ShortcutManagerCompat.requestPinShortcut(mContext, shortcut, null); Toast.makeText(mContext, mContext.getString(R.string.toastmessage_shortcut_created), Toast.LENGTH_LONG).show(); } else { Toast.makeText(mContext, mContext.getString(R.string.toastmessage_shortcut_not_created), Toast.LENGTH_LONG).show(); } }
/** * 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) { 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); } }
private void createShortcut(){ Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); intent1.setAction(Intent.ACTION_VIEW); intent1.setData(Uri.parse(mSearchView.getText().toString())); intent1.putExtra("duplicate", false); ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(MainActivity.this, webViewTitle) .setShortLabel(webViewTitle) .setIcon(IconCompat.createWithBitmap(favoriteIcon)) .setIntent(intent1) .build(); ShortcutManagerCompat.requestPinShortcut(MainActivity.this, pinShortcutInfo, null); }
@Override public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) { EditText shortcutNameEditText = dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext); Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); intent1.setAction(Intent.ACTION_VIEW); intent1.setData(Uri.parse(mSearchView.getText().toString())); intent1.putExtra("duplicate", false); ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(MainActivity.this, webViewTitle) .setShortLabel(shortcutNameEditText.getText().toString()) .setIcon(IconCompat.createWithBitmap(StaticUtils.getCircleBitmap(favoriteIcon))) .setIntent(intent1) .build(); ShortcutManagerCompat.requestPinShortcut(MainActivity.this, pinShortcutInfo, null); }
private static IconCompat generateDefaultThemedIcon(Context context, int iconId) { // Return an Icon of iconId with default colors return generateThemedIcon(context, iconId, context.getColor(R.color.app_shortcut_default_foreground), context.getColor(R.color.app_shortcut_default_background) ); }
private static IconCompat generateUserThemedIcon(Context context, int iconId) { // Get background color from context's theme final TypedValue typedColorBackground = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.colorBackground, typedColorBackground, true); // Return an Icon of iconId with those colors return generateThemedIcon(context, iconId, ThemeStore.primaryColor(context), typedColorBackground.data ); }
private boolean createShortcutWith(String id, String label, IconCompat icon) { ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getContext(), id) .setIntent(intent) .setShortLabel(label) .setIcon(icon) .build(); return ShortcutManagerCompat.requestPinShortcut(getContext(), shortcut, startHomeScreen()); }
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; } 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); }
IconCompat createIconFor(Drawable drawable) { return IconCompat.createWithBitmap(createBitmapFor(drawable)); }
private IconCompat createSimpleIcon() { return IconCompat.createWithResource(getContext(), R.mipmap.ic_launcher_bookmark); }