下面列出了怎么用android.graphics.drawable.AdaptiveIconDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Retrieves {@link InputStream} of image by URI (image is located in drawable resources of application).
*
* @param imageUri Image URI
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
* DisplayImageOptions.extraForDownloader(Object)}; can be null
* @return {@link InputStream} of image
*/
protected InputStream getStreamFromDrawable(String imageUri, Object extra) {
String drawableIdString = Scheme.DRAWABLE.crop(imageUri);
int drawableId = Integer.parseInt(drawableIdString);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Drawable drawable = context.getResources().getDrawable(drawableId, context.getTheme());
if (drawable instanceof AdaptiveIconDrawable) {
Bitmap iconBitmap = new AdaptiveIcon()
.setDrawable((AdaptiveIconDrawable) drawable)
.setPath(Data.AdaptiveIconShape)
.setSize(272)
.render();
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
iconBitmap.compress(CompressFormat.PNG, 0, byteOutputStream);
byte[] bitmapData = byteOutputStream.toByteArray();
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bitmapData);
return byteInputStream;
}
}
return context.getResources().openRawResource(drawableId);
}
@Nullable
private static Drawable maybeLoadDrawable(Icon icon) {
if (icon == null) {
return null;
}
switch (icon.getType()) {
case Icon.TYPE_BITMAP:
return new BitmapDrawable(Resources.getSystem(), icon.getBitmap());
case Icon.TYPE_ADAPTIVE_BITMAP:
return new AdaptiveIconDrawable(null,
new BitmapDrawable(Resources.getSystem(), icon.getBitmap()));
case Icon.TYPE_DATA:
return new BitmapDrawable(
Resources.getSystem(),
BitmapFactory.decodeByteArray(
icon.getDataBytes(), icon.getDataOffset(), icon.getDataLength()));
}
return null;
}
private static IconCompat generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) {
// Get and tint foreground and background drawables
Drawable vectorDrawable = ImageUtil.getTintedVectorDrawable(context, iconId, foregroundColor);
Drawable backgroundDrawable = ImageUtil.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(ImageUtil.createBitmap(adaptiveIconDrawable));
} else {
// Squash the two drawables together
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});
// Return as an Icon
return IconCompat.createWithBitmap(ImageUtil.createBitmap(layerDrawable));
}
}
/**
* 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;
}
/**
* 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.
*/
@TargetApi(16)
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;
}
public Bitmap getDefaultBitmap(Drawable d) {
if (d instanceof BitmapDrawable) {
return ((BitmapDrawable) d).getBitmap();
} else if ((Build.VERSION.SDK_INT >= 26)
&& (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, w, h);
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);
}
/**
* Retrieve an icon for a component name, applying user preferences
* such as shaded adaptive icons and icon pack.
*
* @param activity Activity where LauncherApps service can be retrieved.
* @param componentName Component name of the activity.
* @param user The numerical representation of the user.
* @param shouldHide Whether we are even needed at all.
*
* @return Drawable of the icon.
*/
public static Drawable getIcon(Activity activity, String componentName, long user, boolean shouldHide) {
Drawable icon = null;
if (!shouldHide) {
icon = LauncherIconHelper.getIconDrawable(activity, componentName, user);
if (PreferenceHelper.appTheme().equals("light")
&& PreferenceHelper.shadeAdaptiveIcon()
&& (Utils.atLeastOreo()
&& icon instanceof AdaptiveIconDrawable)) {
icon = LauncherIconHelper.drawAdaptiveShadow(icon);
}
}
return icon;
}
/**
* Get a {@link Bitmap} out of a {@link Drawable}
*/
public Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof VectorDrawableCompat
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
|| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} else if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
}
return bitmap;
}
private static IconCompat generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) {
// Get and tint foreground and background drawables
Drawable vectorDrawable = ImageUtil.getTintedVectorDrawable(context, iconId, foregroundColor);
Drawable backgroundDrawable = ImageUtil.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(ImageUtil.createBitmap(adaptiveIconDrawable));
} else {
// Squash the two drawables together
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});
// Return as an Icon
return IconCompat.createWithBitmap(ImageUtil.createBitmap(layerDrawable));
}
}
/**
* Get a {@link Bitmap} out of a {@link Drawable}
*/
public Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof VectorDrawableCompat
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
|| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} else if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
}
return bitmap;
}
/**
* Get a {@link Bitmap} out of a {@link Drawable}
*/
public Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof VectorDrawableCompat
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
|| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} else if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
}
return bitmap;
}
void onBindView() {
ResourceProvider provider = providerList.get(getAdapterPosition());
container.setOnClickListener(v -> listener.onItemClicked(provider, getAdapterPosition()));
Drawable drawable = provider.getProviderIcon();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& drawable instanceof AdaptiveIconDrawable) {
icon.setIcon(new AdaptiveIcon(
((AdaptiveIconDrawable) drawable).getForeground(),
((AdaptiveIconDrawable) drawable).getBackground(),
0.5
));
icon.setPath(AdaptiveIconView.PATH_CIRCLE);
} else {
icon.setIcon(new AdaptiveIcon(drawable, null, 1));
}
title.setText(provider.getProviderName());
previewButton.setOnClickListener(v ->
IntentHelper.startPreviewIconActivity(activity, provider.getPackageName()));
}
/**
* Retrieve an icon for a component name, applying user preferences
* such as shaded adaptive icons and icon pack.
*
* @param activity Activity where LauncherApps service can be retrieved.
* @param componentName Component name of the activity.
* @param user The numerical representation of the user.
* @param shouldHide Whether we are even needed at all.
*
* @return Drawable of the icon.
*/
public static Drawable getIcon(Activity activity, String componentName, long user, boolean shouldHide) {
Drawable icon = null;
if (!shouldHide) {
icon = LauncherIconHelper.getIconDrawable(activity, componentName, user);
if (PreferenceHelper.appTheme().equals("light")
&& PreferenceHelper.shadeAdaptiveIcon()
&& (Utils.atLeastOreo()
&& icon instanceof AdaptiveIconDrawable)) {
icon = LauncherIconHelper.drawAdaptiveShadow(icon);
}
}
return icon;
}
private static IconCompat generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) {
// Get and tint foreground and background drawables
Drawable vectorDrawable = ImageUtil.getTintedVectorDrawable(context, iconId, foregroundColor);
Drawable backgroundDrawable = ImageUtil.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(ImageUtil.createBitmap(adaptiveIconDrawable));
} else {
// Squash the two drawables together
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});
// Return as an Icon
return IconCompat.createWithBitmap(ImageUtil.createBitmap(layerDrawable));
}
}
/**
* Get a {@link Bitmap} out of a {@link Drawable}
*/
public Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof VectorDrawableCompat
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
|| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} else if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
}
return bitmap;
}
private static Bitmap getRightIcon(Drawable drawable) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof AdaptiveIconDrawable) {
return new AdaptiveIcon()
.setDrawable((AdaptiveIconDrawable) drawable)
.setSize(272)
.render();
}
}
return null;
}
public static Bitmap getRightIcon(Drawable drawable) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof AdaptiveIconDrawable) {
return new AdaptiveIcon()
.setDrawable((AdaptiveIconDrawable) drawable)
.render();
}
}
return null;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mIconName = savedInstanceState.getString(NAME);
mIconId = savedInstanceState.getInt(ID);
}
if (!getActivity().getResources().getBoolean(R.bool.show_icon_name)) {
boolean iconNameReplacer = getActivity().getResources().getBoolean(
R.bool.enable_icon_name_replacer);
mIconName = IconsHelper.replaceName(getActivity(), iconNameReplacer, mIconName);
}
mName.setText(mIconName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Drawable drawable = getActivity().getDrawable(mIconId);
if (drawable instanceof AdaptiveIconDrawable) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
new AdaptiveIcon()
.setDrawable((AdaptiveIconDrawable) drawable)
.setPath(Preferences.get(getActivity()).getIconShape())
.setSize(272)
.render()
.compress(Bitmap.CompressFormat.PNG, 100, stream);
Glide.with(this)
.load(stream.toByteArray())
.into(mIcon);
return;
}
}
ImageLoader.getInstance().displayImage("drawable://" + mIconId, mIcon,
ImageConfig.getDefaultImageOptions(false));
}
public Drawable wrapIconDrawableWithShadow(Drawable drawable) {
if (!(drawable instanceof AdaptiveIconDrawable)) {
return drawable;
}
Bitmap shadow = getShadowBitmap((AdaptiveIconDrawable) drawable);
return new ShadowDrawable(shadow, drawable);
}
/**
* 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;
}
@RequiresApi(O) static Icon createAdaptiveIcon(final AdaptiveIconDrawable drawable) {
final int width = drawable.getIntrinsicWidth() * 3 / 2, height = drawable.getIntrinsicHeight() * 3 / 2,
start = drawable.getIntrinsicWidth() / 4, top = drawable.getIntrinsicHeight() / 4;
drawable.setBounds(start, top, width - start, height - top);
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
return Icon.createWithAdaptiveBitmap(bitmap);
}
@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;
}
}
public Image (Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (drawable instanceof AdaptiveIconDrawable) {
drawable = adaptiveIconToDrawable((AdaptiveIconDrawable) drawable);
}
}
this.drawable = drawable;
}
@RequiresApi(Build.VERSION_CODES.O)
private static Drawable adaptiveIconToDrawable(AdaptiveIconDrawable adaptive) {
final Bitmap bitmap = Bitmap.createBitmap(adaptive.getIntrinsicWidth(), adaptive.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
adaptive.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
adaptive.draw(canvas);
return new BitmapDrawable(bitmap);
}
void saveIconAndFixUpShortcutLocked(ShortcutInfo shortcut) {
if (shortcut.hasIconFile() || shortcut.hasIconResource()) {
return;
}
final long token = injectClearCallingIdentity();
try {
// Clear icon info on the shortcut.
removeIconLocked(shortcut);
final Icon icon = shortcut.getIcon();
if (icon == null) {
return; // has no icon
}
int maxIconDimension = mMaxIconDimension;
Bitmap bitmap;
try {
switch (icon.getType()) {
case Icon.TYPE_RESOURCE: {
injectValidateIconResPackage(shortcut, icon);
shortcut.setIconResourceId(icon.getResId());
shortcut.addFlags(ShortcutInfo.FLAG_HAS_ICON_RES);
return;
}
case Icon.TYPE_BITMAP:
bitmap = icon.getBitmap(); // Don't recycle in this case.
break;
case Icon.TYPE_ADAPTIVE_BITMAP: {
bitmap = icon.getBitmap(); // Don't recycle in this case.
maxIconDimension *= (1 + 2 * AdaptiveIconDrawable.getExtraInsetFraction());
break;
}
default:
// This shouldn't happen because we've already validated the icon, but
// just in case.
throw ShortcutInfo.getInvalidIconException();
}
mShortcutBitmapSaver.saveBitmapLocked(shortcut,
maxIconDimension, mIconPersistFormat, mIconPersistQuality);
} finally {
// Once saved, we won't use the original icon information, so null it out.
shortcut.clearIcon();
}
} finally {
injectRestoreCallingIdentity(token);
}
}
/**
* Returns the icon for this shortcut, without any badging for the profile.
*
* <p>The calling launcher application must be allowed to access the shortcut information,
* as defined in {@link #hasShortcutHostPermission()}.
*
* @param density The preferred density of the icon, zero for default density. Use
* density DPI values from {@link DisplayMetrics}.
*
* @return The drawable associated with the shortcut.
* @throws IllegalStateException when the user is locked, or when the {@code user} user
* is locked or not running.
*
* @see ShortcutManager
* @see #getShortcutBadgedIconDrawable(ShortcutInfo, int)
* @see DisplayMetrics
*/
public Drawable getShortcutIconDrawable(@NonNull ShortcutInfo shortcut, int density) {
if (shortcut.hasIconFile()) {
final ParcelFileDescriptor pfd = getShortcutIconFd(shortcut);
if (pfd == null) {
return null;
}
try {
final Bitmap bmp = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
if (bmp != null) {
BitmapDrawable dr = new BitmapDrawable(mContext.getResources(), bmp);
if (shortcut.hasAdaptiveBitmap()) {
return new AdaptiveIconDrawable(null, dr);
} else {
return dr;
}
}
return null;
} finally {
try {
pfd.close();
} catch (IOException ignore) {
}
}
} else if (shortcut.hasIconResource()) {
return loadDrawableResourceFromPackage(shortcut.getPackage(),
shortcut.getIconResourceId(), shortcut.getUserHandle(), density);
} else if (shortcut.getIcon() != null) {
// This happens if a shortcut is pending-approval.
final Icon icon = shortcut.getIcon();
switch (icon.getType()) {
case Icon.TYPE_RESOURCE: {
return loadDrawableResourceFromPackage(shortcut.getPackage(),
icon.getResId(), shortcut.getUserHandle(), density);
}
case Icon.TYPE_BITMAP:
case Icon.TYPE_ADAPTIVE_BITMAP: {
return icon.loadDrawable(mContext);
}
default:
return null; // Shouldn't happen though.
}
} else {
return null; // Has no icon.
}
}