Java Code Examples for androidx.core.graphics.drawable.IconCompat#createWithBitmap()

The following examples show how to use androidx.core.graphics.drawable.IconCompat#createWithBitmap() . 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: AppShortcutIconGenerator.java    From Music-Player with GNU General Public License v3.0 6 votes vote down vote up
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));
    }
}
 
Example 2
Source File: AppShortcutIconGenerator.java    From VinylMusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
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));
    }
}
 
Example 3
Source File: AppShortcutIconGenerator.java    From Phonograph with GNU General Public License v3.0 6 votes vote down vote up
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));
    }
}
 
Example 4
Source File: AvatarUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@WorkerThread
public static IconCompat getIconForNotification(@NonNull Context context, @NonNull Recipient recipient) {
  try {
    return IconCompat.createWithBitmap(request(GlideApp.with(context).asBitmap(), context, recipient).submit().get());
  } catch (ExecutionException | InterruptedException e) {
    return null;
  }
}
 
Example 5
Source File: NotificationBuilderImplP.java    From FCM-for-Mojo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public NotificationCompat.Style createStyle(Context context, Chat chat) {
    MessagingStyle style = new MessagingStyle(new Person.Builder()
            .setName(context.getString(R.string.you))
            .setIcon(IconCompat.createWithBitmap(UserIcon.requestIcon(context, User.getSelf().getUid(), Chat.ChatType.FRIEND)))
            .build());
    style.setConversationTitle(chat.getName());
    style.setGroupConversation(chat.isGroup());

    for (int i = chat.getMessages().size() - NOTIFICATION_MAX_MESSAGES, count = 0; i < chat.getMessages().size() && count <= 8; i++, count++) {
        if (i < 0) {
            continue;
        }

        Message message = chat.getMessages().get(i);
        User sender = message.getSenderUser();

        IconCompat icon = null;
        Bitmap bitmap = UserIcon.getIcon(context, sender.getUid(), Chat.ChatType.FRIEND);
        if (bitmap != null) {
            icon = IconCompat.createWithBitmap(bitmap);
        }

        Person person = null;
        if (message.getSenderUser() != User.getSelf()) {
            person = new Person.Builder()
                    .setKey(sender.getName())
                    .setName(sender.getName())
                    .setIcon(icon)
                    .build();
        }

        style.addMessage(message.getContent(context), message.getTimestamp(), person);
    }

    style.setSummaryText(context.getString(R.string.notification_messages, chat.getMessages().getSize()));

    return style;
}
 
Example 6
Source File: HomeScreen.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 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, boolean requestDesktop) {
    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, requestDesktop))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcut, null);
    }
}
 
Example 7
Source File: Shortcuts.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
private static ShortcutInfoCompat.Builder getShortcut(Context context, String email, String name, Uri avatar) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean identicons = prefs.getBoolean("identicons", false);
    boolean circular = prefs.getBoolean("circular", true);

    Intent intent = new Intent(context, ActivityMain.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.setAction(Intent.ACTION_SEND);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setData(Uri.parse("mailto:" + email));

    Bitmap bitmap = null;
    if (avatar != null &&
            Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
        // Create icon from bitmap because launcher might not have contacts permission
        InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
                context.getContentResolver(), avatar);
        bitmap = BitmapFactory.decodeStream(is);
    }

    boolean identicon = false;
    if (bitmap == null) {
        int dp = Helper.dp2pixels(context, 96);
        if (identicons) {
            identicon = true;
            bitmap = ImageHelper.generateIdenticon(email, dp, 5, context);
        } else
            bitmap = ImageHelper.generateLetterIcon(email, name, dp, context);
    }

    bitmap = ImageHelper.makeCircular(bitmap,
            circular && !identicon ? null : Helper.dp2pixels(context, 3));

    IconCompat icon = IconCompat.createWithBitmap(bitmap);
    String id = (name == null ? email : "\"" + name + "\" <" + email + ">");
    Set<String> categories = new HashSet<>(Arrays.asList("eu.faircode.email.TEXT_SHARE_TARGET"));
    ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(context, id)
            .setIcon(icon)
            .setShortLabel(name == null ? email : name)
            .setLongLabel(name == null ? email : name)
            .setCategories(categories)
            .setIntent(intent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        Person.Builder person = new Person.Builder()
                .setIcon(icon)
                .setName(name == null ? email : name)
                .setImportant(true);
        if (avatar != null)
            person.setUri(avatar.toString());
        builder.setPerson(person.build());
    }

    return builder;
}