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

The following examples show how to use android.graphics.drawable.Icon#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 Orin with GNU General Public License v3.0 5 votes vote down vote up
private static Icon 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);

    // Squash the two drawables together
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});

    // Return as an Icon
    return Icon.createWithBitmap(drawableToBitmap(layerDrawable));
}
 
Example 2
Source File: AppShortcutIconGenerator.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
private static Icon 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);

    // Squash the two drawables together
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});

    // Return as an Icon
    return Icon.createWithBitmap(drawableToBitmap(layerDrawable));
}
 
Example 3
Source File: NotificationBuilderBase.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Adds an action to {@code builder} using a {@code Bitmap} if a bitmap is provided and the API
 * level is high enough, otherwise a resource id is used.
 */
@SuppressWarnings("deprecation") // For addAction(int, CharSequence, PendingIntent)
@TargetApi(Build.VERSION_CODES.M) // For the Icon class.
protected static void addActionToBuilder(Notification.Builder builder, Action action) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && action.iconBitmap != null) {
        Icon icon = Icon.createWithBitmap(action.iconBitmap);
        builder.addAction(
                new Notification.Action.Builder(icon, action.title, action.intent).build());
    } else {
        builder.addAction(action.iconId, action.title, action.intent);
    }
}
 
Example 4
Source File: ShortcutsManager.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
@NonNull
private static Icon getIcon(ResourceProvider provider, WeatherCode code, boolean daytime) {
    return Icon.createWithBitmap(
            drawableToBitmap(
                    ResourceHelper.getShortcutsIcon(provider, code, daytime)
            )
    );
}
 
Example 5
Source File: NotificationBuilderBase.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // For Notification.Action.Builder
@SuppressWarnings("deprecation") // For Builder(int, CharSequence, PendingIntent)
private static Notification.Action.Builder getActionBuilder(Action action) {
    Notification.Action.Builder actionBuilder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && action.iconBitmap != null) {
        // Icon was added in Android M.
        Icon icon = Icon.createWithBitmap(action.iconBitmap);
        actionBuilder = new Notification.Action.Builder(icon, action.title, action.intent);
    } else {
        actionBuilder =
                new Notification.Action.Builder(action.iconId, action.title, action.intent);
    }
    return actionBuilder;
}
 
Example 6
Source File: ContactChooserTargetService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
    final ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
    if (!EventReceiver.hasEnabledAccounts(this)) {
        return chooserTargets;
    }
    final Intent intent = new Intent(this, XmppConnectionService.class);
    intent.setAction("contact_chooser");
    Compatibility.startService(this, intent);
    bindService(intent, this, Context.BIND_AUTO_CREATE);
    try {
        waitForService();
        final ArrayList<Conversation> conversations = new ArrayList<>();
        if (!mXmppConnectionService.areMessagesInitialized()) {
            return chooserTargets;
        }

        mXmppConnectionService.populateWithOrderedConversations(conversations, textOnly(matchedFilter));
        final ComponentName componentName = new ComponentName(this, ConversationsActivity.class);
        final int pixel = AvatarService.getSystemUiAvatarSize(this);
        for (Conversation conversation : conversations) {
            if (conversation.sentMessagesCount() == 0) {
                continue;
            }
            final String name = conversation.getName().toString();
            final Icon icon = Icon.createWithBitmap(mXmppConnectionService.getAvatarService().get(conversation, pixel));
            final float score = 1 - (1.0f / MAX_TARGETS) * chooserTargets.size();
            final Bundle extras = new Bundle();
            extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
            chooserTargets.add(new ChooserTarget(name, icon, score, componentName, extras));
            if (chooserTargets.size() >= MAX_TARGETS) {
                break;
            }
        }
    } catch (InterruptedException e) {
    }
    unbindService(this);
    return chooserTargets;
}
 
Example 7
Source File: NotificationBuilderBase.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // For Notification.Action.Builder
@SuppressWarnings("deprecation") // For Builder(int, CharSequence, PendingIntent)
private static Notification.Action.Builder getActionBuilder(Action action) {
    Notification.Action.Builder actionBuilder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && action.iconBitmap != null) {
        // Icon was added in Android M.
        Icon icon = Icon.createWithBitmap(action.iconBitmap);
        actionBuilder = new Notification.Action.Builder(icon, action.title, action.intent);
    } else {
        actionBuilder =
                new Notification.Action.Builder(action.iconId, action.title, action.intent);
    }
    return actionBuilder;
}
 
Example 8
Source File: ShortcutHelper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private static ShortcutInfo buildShortcut(Context context, Site s) {
    int tintColor = ThemeHelper.getColor(context, R.color.secondary_light);
    Bitmap siteBitmap = getBitmapFromVectorDrawable(context, s.getIco());
    siteBitmap = tintBitmap(siteBitmap, tintColor);
    Icon siteIcon = Icon.createWithBitmap(siteBitmap);

    Intent siteIntent = UnlockActivity.wrapIntent(context, s);

    return new ShortcutInfo.Builder(context, s.getDescription().toLowerCase())
            .setShortLabel(s.getDescription().toLowerCase())
            .setLongLabel("Open " + s.getDescription().toLowerCase())
            .setIcon(siteIcon)
            .setIntent(siteIntent)
            .build();
}
 
Example 9
Source File: LinphoneShortcutManager.java    From linphone-android with GNU General Public License v3.0 5 votes vote down vote up
public ShortcutInfo createChatRoomShortcutInfo(
        LinphoneContact contact, String chatRoomAddress) {
    if (contact == null) return null;

    Bitmap bm = null;
    if (contact.getThumbnailUri() != null) {
        bm = ImageUtils.getRoundBitmapFromUri(mContext, contact.getThumbnailUri());
    }
    Icon icon =
            bm == null
                    ? Icon.createWithResource(mContext, R.drawable.avatar)
                    : Icon.createWithBitmap(bm);

    try {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(mContext, ChatActivity.class);
        intent.addFlags(
                Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra("RemoteSipUri", chatRoomAddress);

        return new ShortcutInfo.Builder(mContext, chatRoomAddress)
                .setShortLabel(contact.getFullName())
                .setIcon(icon)
                .setCategories(mCategories)
                .setIntent(intent)
                .build();
    } catch (Exception e) {
        Log.e("[Shortcuts Manager] ShortcutInfo.Builder exception: " + e);
    }

    return null;
}
 
Example 10
Source File: LinphoneShortcutManager.java    From linphone-android with GNU General Public License v3.0 5 votes vote down vote up
public ShortcutInfo createContactShortcutInfo(LinphoneContact contact) {
    if (contact == null) return null;

    Bitmap bm = null;
    if (contact.getThumbnailUri() != null) {
        bm = ImageUtils.getRoundBitmapFromUri(mContext, contact.getThumbnailUri());
    }
    Icon icon =
            bm == null
                    ? Icon.createWithResource(mContext, R.drawable.avatar)
                    : Icon.createWithBitmap(bm);

    try {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(mContext, ContactsActivity.class);
        intent.addFlags(
                Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra("ContactId", contact.getContactId());

        return new ShortcutInfo.Builder(mContext, contact.getContactId())
                .setShortLabel(contact.getFullName())
                .setIcon(icon)
                .setCategories(mCategories)
                .setIntent(intent)
                .build();
    } catch (Exception e) {
        Log.e("[Shortcuts Manager] ShortcutInfo.Builder exception: " + e);
    }

    return null;
}
 
Example 11
Source File: TgChooserTargetService.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private Icon createSavedMessagesIcon() {
    try {
        final Bitmap bitmap = Bitmap.createBitmap(AndroidUtilities.dp(56f), AndroidUtilities.dp(56f), Bitmap.Config.ARGB_8888);
        final AvatarDrawable avatarDrawable = new AvatarDrawable();
        avatarDrawable.setAvatarType(AvatarDrawable.AVATAR_TYPE_SAVED);
        avatarDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        avatarDrawable.draw(new Canvas(bitmap));
        return Icon.createWithBitmap(bitmap);
    } catch (Throwable e) {
        FileLog.e(e);
    }
    return null;
}
 
Example 12
Source File: ContactChooserTargetService.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
    final ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
    if (!EventReceiver.hasEnabledAccounts(this)) {
        return chooserTargets;
    }
    final Intent intent = new Intent(this, XmppConnectionService.class);
    intent.setAction("contact_chooser");
    Compatibility.startService(this, intent);
    bindService(intent, this, Context.BIND_AUTO_CREATE);
    try {
        waitForService();
        final ArrayList<Conversation> conversations = new ArrayList<>();
        if (!mXmppConnectionService.areMessagesInitialized()) {
            return chooserTargets;
        }
        
        mXmppConnectionService.populateWithOrderedConversations(conversations, textOnly(matchedFilter));
        final ComponentName componentName = new ComponentName(this, ConversationsActivity.class);
        final int pixel = AvatarService.getSystemUiAvatarSize(this);
        for (Conversation conversation : conversations) {
            if (conversation.sentMessagesCount() == 0) {
                continue;
            }
            final String name = conversation.getName().toString();
            final Icon icon = Icon.createWithBitmap(mXmppConnectionService.getAvatarService().get(conversation, pixel));
            final float score = 1 - (1.0f / MAX_TARGETS) * chooserTargets.size();
            final Bundle extras = new Bundle();
            extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
            chooserTargets.add(new ChooserTarget(name, icon, score, componentName, extras));
            if (chooserTargets.size() >= MAX_TARGETS) {
                break;
            }
        }
    } catch (InterruptedException e) {
    }
    unbindService(this);
    return chooserTargets;
}
 
Example 13
Source File: TgChooserTargetService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private Icon createSavedMessagesIcon() {
    try {
        final Bitmap bitmap = Bitmap.createBitmap(AndroidUtilities.dp(56f), AndroidUtilities.dp(56f), Bitmap.Config.ARGB_8888);
        final AvatarDrawable avatarDrawable = new AvatarDrawable();
        avatarDrawable.setAvatarType(AvatarDrawable.AVATAR_TYPE_SAVED);
        avatarDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        avatarDrawable.draw(new Canvas(bitmap));
        return Icon.createWithBitmap(bitmap);
    } catch (Throwable e) {
        FileLog.e(e);
    }
    return null;
}
 
Example 14
Source File: ApiTwentyEightPlus.java    From linphone-android with GNU General Public License v3.0 4 votes vote down vote up
public static Notification createMessageNotification(
        Context context, Notifiable notif, Bitmap contactIcon, PendingIntent intent) {

    Person me = new Person.Builder().setName(notif.getMyself()).build();
    Notification.MessagingStyle style = new Notification.MessagingStyle(me);
    for (NotifiableMessage message : notif.getMessages()) {
        Icon userIcon = null;
        if (message.getSenderBitmap() != null) {
            userIcon = Icon.createWithBitmap(message.getSenderBitmap());
        }

        Person.Builder builder = new Person.Builder().setName(message.getSender());
        if (userIcon != null) {
            builder.setIcon(userIcon);
        }
        Person user = builder.build();

        Notification.MessagingStyle.Message msg =
                new Notification.MessagingStyle.Message(
                        message.getMessage(), message.getTime(), user);
        if (message.getFilePath() != null)
            msg.setData(message.getFileMime(), message.getFilePath());
        style.addMessage(msg);
    }
    if (notif.isGroup()) {
        style.setConversationTitle(notif.getGroupTitle());
    }
    style.setGroupConversation(notif.isGroup());

    return new Notification.Builder(
                    context, context.getString(R.string.notification_channel_id))
            .setSmallIcon(R.drawable.topbar_chat_notification)
            .setAutoCancel(true)
            .setContentIntent(intent)
            .setDefaults(
                    Notification.DEFAULT_SOUND
                            | Notification.DEFAULT_VIBRATE
                            | Notification.DEFAULT_LIGHTS)
            .setLargeIcon(contactIcon)
            .setCategory(Notification.CATEGORY_MESSAGE)
            .setGroup(CHAT_NOTIFICATIONS_GROUP)
            .setVisibility(Notification.VISIBILITY_PRIVATE)
            .setPriority(Notification.PRIORITY_HIGH)
            .setNumber(notif.getMessages().size())
            .setWhen(System.currentTimeMillis())
            .setShowWhen(true)
            .setColor(context.getColor(R.color.notification_led_color))
            .setStyle(style)
            .addAction(Compatibility.getReplyMessageAction(context, notif))
            .addAction(Compatibility.getMarkMessageAsReadAction(context, notif))
            .build();
}