Example usage for android.support.v4.graphics.drawable IconCompat createWithAdaptiveBitmap

List of usage examples for android.support.v4.graphics.drawable IconCompat createWithAdaptiveBitmap

Introduction

In this page you can find the example usage for android.support.v4.graphics.drawable IconCompat createWithAdaptiveBitmap.

Prototype

public static IconCompat createWithAdaptiveBitmap(Bitmap bits) 

Source Link

Document

Create an Icon pointing to a bitmap in memory that follows the icon design guideline defined by android.graphics.drawable.AdaptiveIconDrawable .

Usage

From source file:org.mozilla.focus.shortcut.HomeScreen.java

/**
 * Create a shortcut via the AppCompat's shortcut manager.
 * <p>/*from w  w w .  ja v  a  2  s  . c  o  m*/
 * 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) {
    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)).build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcut, null);
    }
}

From source file:com.android.contacts.ShortcutIntentBuilder.java

private void createContactShortcutIntent(Uri contactUri, String contentType, String displayName,
        String lookupKey, byte[] bitmapData) {
    Intent intent = null;/*www. ja  v  a 2 s .com*/
    if (TextUtils.isEmpty(displayName)) {
        displayName = mContext.getResources().getString(R.string.missing_name);
    }
    if (BuildCompat.isAtLeastO()) {
        final long contactId = ContentUris.parseId(contactUri);
        final ShortcutManager sm = (ShortcutManager) mContext.getSystemService(Context.SHORTCUT_SERVICE);
        final DynamicShortcuts dynamicShortcuts = new DynamicShortcuts(mContext);
        final ShortcutInfo shortcutInfo = dynamicShortcuts.getQuickContactShortcutInfo(contactId, lookupKey,
                displayName);
        if (shortcutInfo != null) {
            intent = sm.createShortcutResultIntent(shortcutInfo);
        }
    }
    final Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);

    final Intent shortcutIntent = ImplicitIntentsUtil.getIntentForQuickContactLauncherShortcut(mContext,
            contactUri);

    intent = intent == null ? new Intent() : intent;

    final Bitmap icon = generateQuickContactIcon(drawable);
    if (BuildCompat.isAtLeastO()) {
        final IconCompat compatIcon = IconCompat.createWithAdaptiveBitmap(icon);
        compatIcon.addToShortcutIntent(intent, null, mContext);
    } else {
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
    }
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, displayName);

    mListener.onShortcutIntentCreated(contactUri, intent);
}

From source file:com.android.contacts.ShortcutIntentBuilder.java

private void createPhoneNumberShortcutIntent(Uri uri, String displayName, String lookupKey, byte[] bitmapData,
        String phoneNumber, int phoneType, String phoneLabel, String shortcutAction) {
    final Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);
    final Bitmap icon;
    final Uri phoneUri;
    final String shortcutName;
    if (TextUtils.isEmpty(displayName)) {
        displayName = mContext.getResources().getString(R.string.missing_name);
    }//from   w  ww. j  a  v  a 2s  .  c o  m

    if (Intent.ACTION_CALL.equals(shortcutAction)) {
        // Make the URI a direct tel: URI so that it will always continue to work
        phoneUri = Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null);
        icon = generatePhoneNumberIcon(drawable, phoneType, phoneLabel,
                R.drawable.quantum_ic_phone_vd_theme_24);
        shortcutName = mContext.getResources().getString(R.string.call_by_shortcut, displayName);
    } else {
        phoneUri = Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phoneNumber, null);
        icon = generatePhoneNumberIcon(drawable, phoneType, phoneLabel,
                R.drawable.quantum_ic_message_vd_theme_24);
        shortcutName = mContext.getResources().getString(R.string.sms_by_shortcut, displayName);
    }

    final Intent shortcutIntent = new Intent(shortcutAction, phoneUri);
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent intent = null;
    IconCompat compatAdaptiveIcon = null;
    if (BuildCompat.isAtLeastO()) {
        compatAdaptiveIcon = IconCompat.createWithAdaptiveBitmap(icon);
        final ShortcutManager sm = (ShortcutManager) mContext.getSystemService(Context.SHORTCUT_SERVICE);
        final String id = shortcutAction + lookupKey + phoneUri.toString().hashCode();
        final DynamicShortcuts dynamicShortcuts = new DynamicShortcuts(mContext);
        final ShortcutInfo shortcutInfo = dynamicShortcuts.getActionShortcutInfo(id, displayName,
                shortcutIntent, compatAdaptiveIcon.toIcon());
        if (shortcutInfo != null) {
            intent = sm.createShortcutResultIntent(shortcutInfo);
        }
    }

    intent = intent == null ? new Intent() : intent;
    // This will be non-null in O and above.
    if (compatAdaptiveIcon != null) {
        compatAdaptiveIcon.addToShortcutIntent(intent, null, mContext);
    } else {
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
    }
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);

    mListener.onShortcutIntentCreated(uri, intent);
}

From source file:org.thoughtcrime.securesms.conversation.ConversationActivity.java

private void handleAddShortcut() {
    Log.i(TAG, "Creating home screen shortcut for recipient " + recipient.getAddress());

    new AsyncTask<Void, Void, IconCompat>() {

        @Override//from ww  w .  java 2 s.co  m
        protected IconCompat doInBackground(Void... voids) {
            Context context = getApplicationContext();
            IconCompat icon = null;

            if (recipient.getContactPhoto() != null) {
                try {
                    Bitmap bitmap = BitmapFactory
                            .decodeStream(recipient.getContactPhoto().openInputStream(context));
                    bitmap = BitmapUtil.createScaledBitmap(bitmap, 300, 300);
                    icon = IconCompat.createWithAdaptiveBitmap(bitmap);
                } catch (IOException e) {
                    Log.w(TAG,
                            "Failed to decode contact photo during shortcut creation. Falling back to generic icon.",
                            e);
                }
            }

            if (icon == null) {
                icon = IconCompat.createWithResource(context,
                        recipient.isGroupRecipient() ? R.mipmap.ic_group_shortcut
                                : R.mipmap.ic_person_shortcut);
            }

            return icon;
        }

        @Override
        protected void onPostExecute(IconCompat icon) {
            Context context = getApplicationContext();
            String name = Optional.fromNullable(recipient.getName())
                    .or(Optional.fromNullable(recipient.getProfileName())).or(recipient.toShortString());

            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context,
                    recipient.getAddress().serialize() + '-' + System.currentTimeMillis()).setShortLabel(name)
                            .setIcon(icon)
                            .setIntent(ShortcutLauncherActivity.createIntent(context, recipient.getAddress()))
                            .build();

            if (ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null)) {
                Toast.makeText(context, getString(R.string.ConversationActivity_added_to_home_screen),
                        Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}