Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2bd738b18fe0f872c63c48024a2fb9bb75a7a3cc |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java |
| @@ -0,0 +1,245 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.customtabs; |
| + |
| +import android.app.PendingIntent; |
| +import android.app.PendingIntent.CanceledException; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.content.res.Resources; |
| +import android.graphics.Bitmap; |
| +import android.graphics.drawable.BitmapDrawable; |
| +import android.graphics.drawable.Drawable; |
| +import android.os.Bundle; |
| +import android.support.annotation.NonNull; |
| +import android.support.customtabs.CustomTabsIntent; |
| +import android.text.TextUtils; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.ImageButton; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.util.IntentUtils; |
| +import org.chromium.chrome.browser.widget.TintedDrawable; |
| + |
| +import java.util.ArrayList; |
| +import java.util.HashSet; |
| +import java.util.List; |
| +import java.util.Set; |
| + |
| +/** |
| + * Container for all parameters related to creating a customizable button. |
| + */ |
| +class CustomButtonParams { |
| + protected static final String TAG = "CustomTabs"; |
| + |
| + private int mId; |
| + protected Bitmap mIcon; |
|
Yusuf
2016/01/06 23:02:55
protected static final
protected final
protected
Ian Wen
2016/01/07 03:09:50
Done. Replaced protected with private and put fina
|
| + protected String mDescription; |
| + protected final PendingIntent mPendingIntent; |
| + private boolean mShouldTint; |
| + private boolean mOnToolbar; |
| + |
| + private CustomButtonParams(int id, @NonNull Bitmap icon, @NonNull String description, |
| + PendingIntent pendingIntent, boolean tinted, boolean onToolbar) { |
| + mId = id; |
| + mIcon = icon; |
| + mDescription = description; |
| + mPendingIntent = pendingIntent; |
| + mShouldTint = tinted; |
| + mOnToolbar = onToolbar; |
| + } |
| + |
| + /** |
| + * Replaces the current icon and description with new ones. |
| + */ |
| + void update(@NonNull Bitmap icon, @NonNull String description) { |
| + mIcon = icon; |
| + mDescription = description; |
| + } |
| + |
| + /** |
| + * @return Whether this button should be shown on the toolbar. |
| + */ |
| + boolean showOnToolbar() { |
| + return mOnToolbar; |
| + } |
| + |
| + /** |
| + * @return The id associated with this button. The custom button on the toolbar always uses |
| + * {@link CustomTabsIntent#TOOLBAR_ACTION_BUTTON_ID} as id. |
| + */ |
| + int getId() { |
| + return mId; |
| + } |
| + |
| + /** |
| + * @return The drawable for the customized button. If the button should be tinted, returns a |
| + * {@link TintedDrawable} |
| + */ |
| + Drawable getIcon(Resources res) { |
| + if (mShouldTint) return TintedDrawable.constructTintedDrawable(res, mIcon); |
|
Yusuf
2016/01/06 23:02:55
if there is an else, there is always braces.
Ian Wen
2016/01/07 03:09:50
Done.
|
| + else return new BitmapDrawable(res, mIcon); |
| + } |
| + |
| + /** |
| + * @return The content description for the customized button. |
| + */ |
| + String getDescription() { |
| + return mDescription; |
| + } |
| + |
| + /** |
| + * @return The {@link PendingIntent} that will be sent when user clicks the customized button. |
| + */ |
| + PendingIntent getPendingIntent() { |
| + return mPendingIntent; |
| + } |
| + |
| + /** |
| + * Converts the params to an {@link ImageButton} on the bottom bar. |
| + * @param parent The parent that the inflated {@link ImageButton}. |
| + * @return The inflated {@link ImageButton}. Returns null if this button is shown in toolbar. |
| + */ |
| + ImageButton toBottomBarButton(Context context, ViewGroup parent) { |
| + if (mOnToolbar) return null; |
| + |
| + ImageButton button = (ImageButton) LayoutInflater.from(context) |
| + .inflate(R.layout.custom_tabs_bottombar_item, parent, false); |
| + button.setImageBitmap(mIcon); |
| + button.setContentDescription(mDescription); |
| + if (mPendingIntent == null) { |
| + button.setEnabled(false); |
| + } else { |
| + // TODO(ianwen): add UMA for button clicking. |
| + button.setOnClickListener(new View.OnClickListener() { |
| + @Override |
| + public void onClick(View v) { |
| + try { |
| + mPendingIntent.send(); |
| + } catch (CanceledException e) { |
| + Log.e(TAG, "CanceledException while sending pending intent in custom tab"); |
| + } |
| + } |
| + }); |
| + } |
| + return button; |
| + } |
| + |
| + /** |
| + * Parses a list of {@link CustomButtonParams} from the intent sent by clients. |
| + * @param intent The intent sent by the client. |
| + * @return A list of parsed {@link CustomButtonParams}. Return an empty list if input is invalid |
| + */ |
| + static List<CustomButtonParams> fromIntent(Context context, Intent intent) { |
| + List<CustomButtonParams> paramsList = new ArrayList<>(1); |
| + if (intent == null) return paramsList; |
| + |
| + Bundle singleBundle = IntentUtils.safeGetBundleExtra(intent, |
| + CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); |
| + ArrayList<Bundle> bundleList = IntentUtils.getParcelableArrayListExtra(intent, |
| + CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); |
| + boolean tinted = IntentUtils.safeGetBooleanExtra(intent, |
| + CustomTabsIntent.EXTRA_TINT_ACTION_BUTTON, false); |
| + if (singleBundle != null) { |
| + CustomButtonParams params = fromBundle(context, singleBundle, tinted, false); |
| + paramsList.add(params); |
| + return paramsList; |
| + } else if (bundleList != null) { |
|
Yusuf
2016/01/06 23:02:55
what if both are non null? This seems to be ignori
Ian Wen
2016/01/07 03:09:50
If both are null this method returns an empty arra
|
| + Set<Integer> ids = new HashSet<>(); |
| + for (Bundle bundle : bundleList) { |
| + CustomButtonParams params = fromBundle(context, bundle, tinted, true); |
| + if (params == null) { |
| + continue; |
| + } else if (ids.contains(params.getId())) { |
| + Log.e(TAG, "Bottom bar items contain duplicate id: " + params.getId()); |
| + continue; |
| + } |
| + ids.add(params.getId()); |
| + paramsList.add(params); |
| + } |
| + return paramsList; |
| + } |
| + return paramsList; |
| + } |
| + |
| + /** |
| + * Parses params out of a bundle. Note if a custom button contains a bitmap that does not fit |
| + * into toolbar, it will be put to bottom bar. |
| + * @param fromList Whether the bundle is contained in a list or it is the single bundle that |
| + * directly comes from the intent. |
| + */ |
| + private static CustomButtonParams fromBundle(Context context, Bundle bundle, boolean tinted, |
| + boolean fromList) { |
| + if (bundle == null) return null; |
| + |
| + if (fromList && !bundle.containsKey(CustomTabsIntent.KEY_ID)) return null; |
| + int id = IntentUtils.safeGetInt(bundle, CustomTabsIntent.KEY_ID, |
| + CustomTabsIntent.TOOLBAR_ACTION_BUTTON_ID); |
| + |
| + Bitmap bitmap = tryParseBitmapFromBundle(bundle); |
| + if (bitmap == null) { |
| + Log.e(TAG, "Invalid action button: bitmap not present in bundle!"); |
| + return null; |
| + } |
| + |
| + String description = tryParseDescriptionFromBundle(bundle); |
| + if (TextUtils.isEmpty(description)) { |
| + Log.e(TAG, "Invalid action button: content description not present in bundle!"); |
| + bitmap.recycle(); |
| + return null; |
| + } |
| + |
| + boolean onToolbar = id == CustomTabsIntent.TOOLBAR_ACTION_BUTTON_ID; |
| + if (onToolbar && !checkCustomButtonIconWithinBounds(context, bitmap)) { |
| + onToolbar = false; |
| + Log.w(TAG, "Button's icon not suitable for toolbar, putting it to bottom bar instead." |
| + + "See: https://developer.android.com/reference/android/support/customtabs/" |
| + + "CustomTabsIntent.html#KEY_ICON"); |
| + } |
| + |
| + PendingIntent pi = IntentUtils.safeGetParcelable(bundle, |
| + CustomTabsIntent.KEY_PENDING_INTENT); |
| + // PendingIntent is a must for buttons on the toolbar, but it's optional for bottombar. |
| + if (onToolbar && pi == null) { |
| + Log.w(TAG, "Invalid action button on toolbar: pending intent not present in bundle!"); |
| + bitmap.recycle(); |
| + return null; |
| + } |
| + |
| + return new CustomButtonParams(id, bitmap, description, pi, tinted, onToolbar); |
| + } |
| + |
| + /** |
| + * @return The bitmap contained in the given {@link Bundle}. Will return null if input is |
| + * invalid. |
| + */ |
| + static Bitmap tryParseBitmapFromBundle(Bundle bundle) { |
| + if (bundle == null) return null; |
| + Bitmap bitmap = IntentUtils.safeGetParcelable(bundle, CustomTabsIntent.KEY_ICON); |
| + if (bitmap == null) return null; |
| + return bitmap; |
| + } |
| + |
| + /** |
| + * @return The content description contained in the given {@link Bundle}. Will return null if |
| + * input is invalid. |
| + */ |
| + static String tryParseDescriptionFromBundle(Bundle bundle) { |
| + String description = IntentUtils.safeGetString(bundle, CustomTabsIntent.KEY_DESCRIPTION); |
| + if (TextUtils.isEmpty(description)) return null; |
| + return description; |
| + } |
| + |
| + private static boolean checkCustomButtonIconWithinBounds(Context context, Bitmap bitmap) { |
| + int height = context.getResources().getDimensionPixelSize(R.dimen.toolbar_icon_height); |
| + if (bitmap.getHeight() < height) return false; |
| + int scaledWidth = bitmap.getWidth() / bitmap.getHeight() * height; |
| + if (scaledWidth > 2 * height) return false; |
| + return true; |
| + } |
| +} |