| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.customtabs; | |
| 6 | |
| 7 import android.app.PendingIntent; | |
| 8 import android.content.Context; | |
| 9 import android.content.res.Resources; | |
| 10 import android.graphics.Bitmap; | |
| 11 import android.graphics.drawable.BitmapDrawable; | |
| 12 import android.graphics.drawable.Drawable; | |
| 13 import android.os.Bundle; | |
| 14 import android.support.annotation.NonNull; | |
| 15 import android.support.customtabs.CustomTabsIntent; | |
| 16 import android.text.TextUtils; | |
| 17 | |
| 18 import org.chromium.base.Log; | |
| 19 import org.chromium.chrome.R; | |
| 20 import org.chromium.chrome.browser.util.IntentUtils; | |
| 21 import org.chromium.chrome.browser.widget.TintedDrawable; | |
| 22 | |
| 23 /** | |
| 24 * Container for all parameters related to creating a custom action button. | |
| 25 */ | |
| 26 /* package */ class ActionButtonParams { | |
| 27 private static final String TAG = "CustomTabs"; | |
| 28 | |
| 29 private Bitmap mIcon; | |
| 30 private String mDescription; | |
| 31 private final PendingIntent mPendingIntent; | |
| 32 private boolean mShouldTint; | |
| 33 | |
| 34 private ActionButtonParams(@NonNull Bitmap icon, @NonNull String description
, | |
| 35 @NonNull PendingIntent pendingIntent) { | |
| 36 mIcon = icon; | |
| 37 mDescription = description; | |
| 38 mPendingIntent = pendingIntent; | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * Replaces the current icon and description with new ones. | |
| 43 */ | |
| 44 void update(@NonNull Bitmap icon, @NonNull String description) { | |
| 45 mIcon = icon; | |
| 46 mDescription = description; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Sets whether the action button icon should be tinted. | |
| 51 */ | |
| 52 void setTinted(boolean shouldTint) { | |
| 53 mShouldTint = shouldTint; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * @return The drawable for the action button. Will be a {@link TintedDrawab
le} if in the VIEW | |
| 58 * intent the client required to tint it. | |
| 59 */ | |
| 60 Drawable getIcon(Resources res) { | |
| 61 Drawable drawable = null; | |
| 62 if (mShouldTint) { | |
| 63 drawable = TintedDrawable.constructTintedDrawable(res, mIcon); | |
| 64 } else { | |
| 65 drawable = new BitmapDrawable(res, mIcon); | |
| 66 } | |
| 67 return drawable; | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * @return The content description for the custom action button. | |
| 72 */ | |
| 73 String getDescription() { | |
| 74 return mDescription; | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * @return The {@link PendingIntent} that will be sent when the user clicks t
he action button. | |
| 79 */ | |
| 80 PendingIntent getPendingIntent() { | |
| 81 return mPendingIntent; | |
| 82 } | |
| 83 | |
| 84 /** | |
| 85 * Parses an {@link ActionButtonParams} from an action button bundle sent by
clients. | |
| 86 * @param bundle The action button bundle specified by | |
| 87 * {@link CustomTabsIntent#EXTRA_ACTION_BUTTON_BUNDLE} | |
| 88 * @return The parsed {@link ActionButtonParams}. Return null if input is in
valid. | |
| 89 */ | |
| 90 static ActionButtonParams fromBundle(Context context, Bundle bundle) { | |
| 91 if (bundle == null) return null; | |
| 92 | |
| 93 Bitmap bitmap = tryParseBitmapFromBundle(context, bundle); | |
| 94 if (bitmap == null) return null; | |
| 95 | |
| 96 String description = tryParseDescriptionFromBundle(bundle); | |
| 97 if (TextUtils.isEmpty(description)) { | |
| 98 bitmap.recycle(); | |
| 99 return null; | |
| 100 } | |
| 101 | |
| 102 PendingIntent pi = IntentUtils.safeGetParcelable(bundle, | |
| 103 CustomTabsIntent.KEY_PENDING_INTENT); | |
| 104 if (pi == null) return null; | |
| 105 return new ActionButtonParams(bitmap, description, pi); | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * @return The bitmap contained in the given {@link Bundle}. Will return nul
l if input is | |
| 110 * invalid. | |
| 111 */ | |
| 112 static Bitmap tryParseBitmapFromBundle(Context context, Bundle bundle) { | |
| 113 if (bundle == null) return null; | |
| 114 Bitmap bitmap = IntentUtils.safeGetParcelable(bundle, CustomTabsIntent.K
EY_ICON); | |
| 115 if (bitmap == null) return null; | |
| 116 if (!checkCustomButtonIconWithinBounds(context, bitmap)) { | |
| 117 Log.w(TAG, "Action button's icon size not acceptable. Please refer t
o " | |
| 118 + "https://developer.android.com/reference/android/support/c
ustomtabs/" | |
| 119 + "CustomTabsIntent.html#KEY_ICON"); | |
| 120 bitmap.recycle(); | |
| 121 return null; | |
| 122 } | |
| 123 return bitmap; | |
| 124 } | |
| 125 | |
| 126 /** | |
| 127 * @return The content description contained in the given {@link Bundle}. Wi
ll return null if | |
| 128 * input is invalid. | |
| 129 */ | |
| 130 static String tryParseDescriptionFromBundle(Bundle bundle) { | |
| 131 String description = IntentUtils.safeGetString(bundle, CustomTabsIntent.
KEY_DESCRIPTION); | |
| 132 if (TextUtils.isEmpty(description)) return null; | |
| 133 return description; | |
| 134 } | |
| 135 | |
| 136 private static boolean checkCustomButtonIconWithinBounds(Context context, Bi
tmap bitmap) { | |
| 137 int height = context.getResources().getDimensionPixelSize(R.dimen.toolba
r_icon_height); | |
| 138 if (bitmap.getHeight() < height) return false; | |
| 139 int scaledWidth = bitmap.getWidth() / bitmap.getHeight() * height; | |
| 140 if (scaledWidth > 2 * height) return false; | |
| 141 return true; | |
| 142 } | |
| 143 } | |
| OLD | NEW |