Java Code Examples for android.graphics.drawable.Icon#getType()

The following examples show how to use android.graphics.drawable.Icon#getType() . 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: TextClassification.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: ShortcutInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * @hide
 */
public static Icon validateIcon(Icon icon) {
    switch (icon.getType()) {
        case Icon.TYPE_RESOURCE:
        case Icon.TYPE_BITMAP:
        case Icon.TYPE_ADAPTIVE_BITMAP:
            break; // OK
        default:
            throw getInvalidIconException();
    }
    if (icon.hasTint()) {
        throw new IllegalArgumentException("Icons with tints are not supported");
    }

    return icon;
}
 
Example 3
Source File: NotificationListenerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/** Convert new-style Icons to legacy representations for pre-M clients. */
private void createLegacyIconExtras(Notification n) {
    Icon smallIcon = n.getSmallIcon();
    Icon largeIcon = n.getLargeIcon();
    if (smallIcon != null && smallIcon.getType() == Icon.TYPE_RESOURCE) {
        n.extras.putInt(Notification.EXTRA_SMALL_ICON, smallIcon.getResId());
        n.icon = smallIcon.getResId();
    }
    if (largeIcon != null) {
        Drawable d = largeIcon.loadDrawable(getContext());
        if (d != null && d instanceof BitmapDrawable) {
            final Bitmap largeIconBits = ((BitmapDrawable) d).getBitmap();
            n.extras.putParcelable(Notification.EXTRA_LARGE_ICON, largeIconBits);
            n.largeIcon = largeIconBits;
        }
    }
}
 
Example 4
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example 5
Source File: LauncherApps.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
    }
}
 
Example 6
Source File: RemoteViews.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static void visitIconUri(Icon icon, @NonNull Consumer<Uri> visitor) {
    if (icon != null && icon.getType() == Icon.TYPE_URI) {
        visitor.accept(icon.getUri());
    }
}