Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/customtabs/ActionButtonParams.java

Issue 1291083004: [Custom Tabs]Add API for updating action button in service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: make updateToolbarUrl return boolean Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/customtabs/ActionButtonParams.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/ActionButtonParams.java b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/ActionButtonParams.java
new file mode 100644
index 0000000000000000000000000000000000000000..9d27bc50f000b9cd9d0e8fd9f7c8d09600b02312
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/ActionButtonParams.java
@@ -0,0 +1,89 @@
+// 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.content.Context;
+import android.graphics.Bitmap;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.customtabs.CustomTabsIntent;
+import android.text.TextUtils;
+
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.util.IntentUtils;
+
+/**
+ * Container for all parameters related to creating a custom action button.
+ */
+/* package */ class ActionButtonParams {
+ final Bitmap mIcon;
Yusuf 2015/08/21 00:07:36 see below. I think we should use a single static w
Ian Wen 2015/08/21 17:01:23 Done.
+ final String mDescription;
+ final PendingIntent mPendingIntent;
+
+ public ActionButtonParams(@NonNull Bitmap icon, @NonNull String description,
Yusuf 2015/08/21 00:07:36 why not have a class method with update(Bitmap, St
Ian Wen 2015/08/21 17:01:23 Good idea. I was constrained by keeping them final
+ @NonNull PendingIntent pendingIntent) {
+ mIcon = icon;
+ mDescription = description;
+ mPendingIntent = pendingIntent;
+ }
+
+ /**
+ * Parses an {@link ActionButtonParams} from an action button bundle sent by clients.
+ * @param bundle The action button button specified by
Yusuf 2015/08/21 00:07:36 action button bundle
Ian Wen 2015/08/21 17:01:23 Done.
+ * {@link CustomTabsIntent#EXTRA_ACTION_BUTTON_BUNDLE}
+ * @return The parsed {@link ActionButtonParams}. Return null if input is invalid.
+ */
+ static ActionButtonParams fromBundle(Context context, Bundle bundle) {
+ if (bundle == null) return null;
+
+ Bitmap bitmap = tryParseBitmapFromBundle(context, bundle);
+ if (bitmap == null) return null;
+
+ String description = tryParseDescriptionFromBundle(bundle);
+ if (TextUtils.isEmpty(description)) {
+ bitmap.recycle();
+ return null;
+ }
+
+ PendingIntent pi = IntentUtils.safeGetParcelable(bundle,
+ CustomTabsIntent.KEY_PENDING_INTENT);
+ if (pi == null) return null;
+ return new ActionButtonParams(bitmap, description, pi);
+ }
+
+ /**
+ * @return The bitmap contained in the given {@link Bundle}. Will return null if input is
+ * invalid.
+ */
+ static Bitmap tryParseBitmapFromBundle(Context context, Bundle bundle) {
Benoit L 2015/08/21 11:31:14 It seems that you are not using the tryParse* meth
Ian Wen 2015/08/21 17:01:23 I wish I could do what you suggested (had the same
Benoit L 2015/08/21 17:06:27 Alright, that's fine. Thank you for explaining the
+ if (bundle == null) return null;
+ Bitmap bitmap = IntentUtils.safeGetParcelable(bundle, CustomTabsIntent.KEY_ICON);
+ if (bitmap == null) return null;
+ if (!checkCustomButtonIconWithinBounds(context, bitmap)) {
+ bitmap.recycle();
+ 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;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698