/** * Creates a normalized bitmap suitable for the all apps view. The bitmap is also visually * normalized with other icons and has enough spacing to add shadow. */ public static Bitmap createScaledBitmapWithoutShadow(Drawable icon, Context context, int iconAppTargetSdk) { RectF iconBounds = new RectF(); IconNormalizer normalizer; float scale = 1f; normalizer = IconNormalizer.getInstance(context); if (AndroidVersion.isAtLeastOreo() && iconAppTargetSdk >= Build.VERSION_CODES.O) { boolean[] outShape = new boolean[1]; AdaptiveIconDrawable dr = (AdaptiveIconDrawable) context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper).mutate(); dr.setBounds(0, 0, 1, 1); scale = normalizer.getScale(icon, iconBounds, dr.getIconMask(), outShape); if (AndroidVersion.isAtLeastOreo() && !outShape[0]) { Drawable wrappedIcon = wrapToAdaptiveIconDrawable(context, icon, scale); if (wrappedIcon != icon) { icon = wrappedIcon; scale = normalizer.getScale(icon, iconBounds, null, null); } } } scale = Math.min(scale, ShadowGenerator.getScaleForBounds(iconBounds)); return createIconBitmap(icon, context, scale); }
/** * If the platform is running O but the app is not providing AdaptiveIconDrawable, then * shrink the legacy icon and set it as foreground. Use color drawable as background to * create AdaptiveIconDrawable. */ private static Drawable wrapToAdaptiveIconDrawable(Context context, Drawable drawable, float scale) { if (!(AndroidVersion.isAtLeastOreo())) { return drawable; } try { if (!(drawable instanceof AdaptiveIconDrawable)) { AdaptiveIconDrawable iconWrapper = (AdaptiveIconDrawable) context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper).mutate(); FixedScaleDrawable fsd = ((FixedScaleDrawable) iconWrapper.getForeground()); fsd.setDrawable(drawable); fsd.setScale(scale); return (Drawable) iconWrapper; } } catch (Exception e) { return drawable; } return drawable; }
public Bitmap getDefaultBitmap(Drawable d) { if (d instanceof BitmapDrawable) { return ((BitmapDrawable) d).getBitmap(); } else if (Build.VERSION.SDK_INT >= 26) { if (d instanceof AdaptiveIconDrawable) { AdaptiveIconDrawable icon = ((AdaptiveIconDrawable)d); int w = icon.getIntrinsicWidth(); int h = icon.getIntrinsicHeight(); Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); icon.draw(canvas); return result; } } float density = context.getResources().getDisplayMetrics().density; int defaultWidth = (int)(48* density); int defaultHeight = (int)(48* density); return Bitmap.createBitmap(defaultWidth, defaultHeight, Bitmap.Config.ARGB_8888); }
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)); } }
/** * Returns a bitmap suitable for the all apps view. The icon is badged for {@param user}. * The bitmap is also visually normalized with other icons. */ public static Bitmap createBadgedIconBitmap( Drawable icon, UserHandle user, Context context, int iconAppTargetSdk) { IconNormalizer normalizer; float scale = 1f; normalizer = IconNormalizer.getInstance(context); if (AndroidVersion.isAtLeastOreo() && iconAppTargetSdk >= Build.VERSION_CODES.O) { boolean[] outShape = new boolean[1]; AdaptiveIconDrawable dr = (AdaptiveIconDrawable) context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper).mutate(); dr.setBounds(0, 0, 1, 1); scale = normalizer.getScale(icon, null, dr.getIconMask(), outShape); if (!outShape[0]){ Drawable wrappedIcon = wrapToAdaptiveIconDrawable(context, icon, scale); if (wrappedIcon != icon) { icon = wrappedIcon; scale = normalizer.getScale(icon, null, null, null); } } } Bitmap bitmap = createIconBitmap(icon, context, scale); if (AndroidVersion.isAtLeastOreo() && icon instanceof AdaptiveIconDrawable) { bitmap = ShadowGenerator.getInstance(context).recreateIcon(bitmap); } return badgeIconForUser(bitmap, user, context); }
/** * Returns a bitmap suitable for the all apps view. */ public static Bitmap createIconBitmap(Drawable icon, Context context) { float scale = 1f; if (AndroidVersion.isAtLeastOreo() && icon instanceof AdaptiveIconDrawable) { scale = ShadowGenerator.getScaleForBounds(new RectF(0, 0, 0, 0)); } Bitmap bitmap = createIconBitmap(icon, context, scale); if (AndroidVersion.isAtLeastOreo() && icon instanceof AdaptiveIconDrawable) { bitmap = ShadowGenerator.getInstance(context).recreateIcon(bitmap); } return bitmap; }
/** * Checks a possibility to create icon drawable from current app icon. * * @param context Current application context. * @return boolean True if it is possible to create a drawable from current app icon. */ private static boolean canCreateIconDrawable(Context context) { try { // Try to create icon drawable. Drawable drawable = AdaptiveIconDrawable.createFromStream( context.getResources().openRawResource(context.getApplicationInfo().icon), "applicationInfo.icon"); // If there was no crash, we still need to check for null. if (drawable != null) { return true; } } catch (Throwable ignored) { } return false; }
@Override public Drawable getLargeAppIcon() { Drawable icon = ResourcesCompat.getDrawableForDensity(mContext.getResources(), getIconId(), getLargerDisplayDensity(), mContext.getTheme()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && icon instanceof AdaptiveIconDrawable) { return ((AdaptiveIconDrawable) icon).getForeground(); } else { return icon; } }