Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/customtabs/BottombarItemParams.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/BottombarItemParams.java b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/BottombarItemParams.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7c2a70e536015ad9a436a7f0544ca5de526a4e19 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/BottombarItemParams.java |
| @@ -0,0 +1,139 @@ |
| +// 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.graphics.Bitmap; |
| +import android.os.Bundle; |
| +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 java.util.ArrayList; |
| +import java.util.HashSet; |
| +import java.util.List; |
| +import java.util.Set; |
| + |
| +/** |
| + * A collection of data associated with an item on the bottombar. |
| + */ |
| +public class BottombarItemParams extends ButtonParams { |
|
Ian Wen
2016/01/02 14:20:39
This should have been package protected. Fixed loc
|
| + |
| + private int mId; |
| + |
| + private BottombarItemParams(int id, Bitmap icon, String description, |
| + PendingIntent pendingIntent) { |
| + super(icon, description, pendingIntent); |
| + mId = id; |
| + } |
| + |
| + /** |
| + * @return The id of this item, specified by the client. |
| + */ |
| + int getId() { |
| + return mId; |
| + } |
| + |
| + /** |
| + * Inflates an {@link ImageButton} using data provided by the item params. |
| + * @param parent The parent that the inflated {@link ImageButton} will be added to. |
| + * @return The inflated {@link ImageButton} |
| + */ |
| + ImageButton toImageButton(Context context, ViewGroup parent) { |
| + 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 botton 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 the given {@link Intent} sent by the client. If there are multiple items with the same |
| + * id, only the first item will be displayed. |
| + * @return The parsed {@link BottombarItemParams}s. Returns an empty list if input is invalid. |
| + */ |
| + static List<BottombarItemParams> fromIntent(Context context, Intent intent) { |
| + List<BottombarItemParams> items = new ArrayList<>(); |
| + List<Bundle> bottomBundles = IntentUtils.getParcelableArrayListExtra(intent, |
| + CustomTabsIntent.EXTRA_BOTTOM_BAR_ITEMS); |
| + |
| + Set<Integer> ids = new HashSet<>(); |
| + if (bottomBundles != null) { |
| + for (Bundle bundle : bottomBundles) { |
| + BottombarItemParams params = BottombarItemParams.fromBundle(context, bundle); |
| + 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()); |
| + items.add(params); |
| + } |
| + } |
| + return items; |
| + } |
| + |
| + /** |
| + * Parses an {@link BottombarItemParams} from a button bundle sent by clients. |
| + * @param bundle A bundle contained in {@link CustomTabsIntent#EXTRA_BOTTOM_BAR_ITEMS} |
| + * @return The parsed {@link ButtonParams}. Return null if input is invalid. |
| + */ |
| + private static BottombarItemParams fromBundle(Context context, Bundle bundle) { |
| + if (bundle == null) return null; |
| + |
| + int id = 0; |
| + if (bundle.containsKey(CustomTabsIntent.KEY_ID)) { |
| + id = IntentUtils.safeGetInt(bundle, CustomTabsIntent.KEY_ID, 0); |
| + } else { |
| + Log.e(TAG, "Invalid bottom bar item: id not specified in bundle!"); |
| + return null; |
| + } |
| + |
| + Bitmap bitmap = tryParseBitmapFromBundle(context, bundle); |
| + if (bitmap == null) { |
| + Log.e(TAG, "Invalid bottom bar item: bitmap not present in bundle!"); |
| + return null; |
| + } |
| + |
| + String description = tryParseDescriptionFromBundle(bundle); |
| + if (TextUtils.isEmpty(description)) { |
| + Log.e(TAG, "Invalid bottom bar item: content description not present in bundle!"); |
| + bitmap.recycle(); |
| + return null; |
| + } |
| + |
| + // PendingIntent is optional when customizing the bottombar. |
| + PendingIntent pi = IntentUtils.safeGetParcelable(bundle, |
| + CustomTabsIntent.KEY_PENDING_INTENT); |
| + return new BottombarItemParams(id, bitmap, description, pi); |
| + } |
| + |
| +} |