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

Unified Diff: customtabs/src/android/support/customtabs/browseraction/BrowserActionIntent.java

Issue 2787723002: [Android] Add public API for Browser Action in support library (Closed)
Patch Set: Created 3 years, 9 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: customtabs/src/android/support/customtabs/browseraction/BrowserActionIntent.java
diff --git a/customtabs/src/android/support/customtabs/browseraction/BrowserActionIntent.java b/customtabs/src/android/support/customtabs/browseraction/BrowserActionIntent.java
new file mode 100644
index 0000000000000000000000000000000000000000..40c5e617fe11bc1e5b7c15452748a0ac45ef2d72
--- /dev/null
+++ b/customtabs/src/android/support/customtabs/browseraction/BrowserActionIntent.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.support.customtabs.browseraction;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+import android.support.annotation.IntDef;
+import android.support.annotation.NonNull;
+import android.support.customtabs.CustomTabsIntent;
+import android.support.customtabs.CustomTabsSession;
+import android.support.v4.content.ContextCompat;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.ArrayList;
+
+/**
+ * Class holding the {@link Intent} and start bundle for a Browser Action Activity.
+ *
+ * <p>
+ * <strong>Note:</strong> The constants below are public for the browser implementation's benefit.
+ * You are strongly encouraged to use {@link BrowserActionIntent.Builder}.</p>
+ */
+public class BrowserActionIntent {
+ private final static String TEST_URL = "https://www.google.com";
Yusuf 2017/04/03 18:00:48 Since this is not a VIEW intent, but a custom ACTI
ltian 2017/04/05 01:44:28 I find we still need this to set data because in A
+
+ /**
+ * Indicates that the user explicitly opted out of Browser Action in the calling application.
+ */
+ public static final String ACTION_BROWSER_ACTION_OPEN =
+ "android.support.customtabs.browser_action_open";
+
+ /**
+ * Extra bitmap that specifies the icon of a custom item shown in the browser action menu.
+ */
+ public static final String KEY_ICON = "android.support.browseraction.ICON";
+
+ /**
+ * Extra string that specifies the title of a custom item shown in the browser action menu.
+ */
+ public static final String KEY_TITLE = "android.support.browseraction.TITLE";
+
+ /**
+ * Extra PendingIntent to be launched when a custom item is selected.
+ */
+ public static final String KEY_ACTION = "android.support.browseraction.ACTION";
Yusuf 2017/04/03 18:00:48 this should follow the packagename android.support
ltian 2017/04/05 01:44:28 Done.
+
+ /**
+ * Extra that specifies {@link BrowserActionType} type of url for the browser action menu.
+ */
+ public static final String EXTRA_TYPE = "android.support.browseraction.extra.TYPE";
+
+ /**
+ * Extra that specifies List<Bundle> used for adding custom items to the browser acton menu.
+ */
+ public static final String EXTRA_MENU_ITEMS = "android.support.browseraction.extra.MENU_ITEMS";
+
+ /**
+ * The maximum allowed number of custom items.
+ */
+ private static final int MAX_CUSTOM_ITEMS = 5;
+
+ /**
+ * Defines the types of url for browser action menu.
+ */
+ @IntDef({MEDIA_TYPE_NONE, MEDIA_TYPE_IMAGE, MEDIA_TYPE_VIDEO, MEDIA_TYPE_AUDIO, MEDIA_TYPE_FILE,
+ MEDIA_TYPE_PLUGIN})
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface BrowserActionType {}
+ public static final int MEDIA_TYPE_NONE = 0;
+ public static final int MEDIA_TYPE_IMAGE = 1;
+ public static final int MEDIA_TYPE_VIDEO = 2;
+ public static final int MEDIA_TYPE_AUDIO = 3;
+ public static final int MEDIA_TYPE_FILE = 4;
+ public static final int MEDIA_TYPE_PLUGIN = 5;
+
+ /**
+ * An {@link Intent} used to start the Browser Action Activity.
+ */
+ @NonNull
Yusuf 2017/04/03 18:00:48 We should have @NonNull in the constructor rather
ltian 2017/04/05 01:44:28 Done.
+ public final Intent intent;
+
+ private BrowserActionIntent(Intent intent) {
+ this.intent = intent;
+ }
+
+ /**
+ * Builder class for {@link BrowserActionIntent} objects.
+ */
+ public static final class Builder {
+ private final Intent mIntent = new Intent(BrowserActionIntent.ACTION_BROWSER_ACTION_OPEN);
+ @BrowserActionType
+ private int mType;
+ private ArrayList<Bundle> mMenuItems = null;
+
+ /**
+ * Constructs a {@link BrowserActionIntent.Builder} object associated with default setting.
+ */
+ public Builder() {
+ mType = MEDIA_TYPE_NONE;
+ mMenuItems = new ArrayList<>();
+ }
+
+ /**
+ * Sets the type of Browser Action context menu.
Yusuf 2017/04/03 18:00:48 The terminology is a bit confusing here. We say ME
ltian 2017/04/05 01:44:28 Done.
+ * @param type {@link BrowserActionType}.
+ */
+ public Builder setUrlType(@BrowserActionType int type) {
+ mType = type;
+ return this;
+ }
+
+ /**
+ * Sets the custom items list.
+ * @param items The list of {@link BrowserActionItem} for custom items.
+ */
+ public Builder setCustomItems(ArrayList<BrowserActionItem> items) {
+ if (items.size() >= MAX_CUSTOM_ITEMS) {
+ throw new IllegalStateException(
+ "Exceeded maximum toolbar item count of " + MAX_CUSTOM_ITEMS);
+ }
+ for (int i = 0; i < items.size(); i++) {
+ mMenuItems.add(getBundleFromItem(items.get(i)));
+ }
+ return this;
+ }
+
+ /**
+ * Populates a {@link Bundle} to hold a custom item for browser action menu.
+ * @param item A custom item for browser action.
+ * @return The Bundle of custom item.
+ */
+ private Bundle getBundleFromItem(BrowserActionItem item) {
+ Bundle bundle = new Bundle();
+ bundle.putString(KEY_TITLE, item.getTitle());
+ bundle.putParcelable(KEY_ACTION, item.getAction());
+ if (item.getIcon() != null) {
+ bundle.putParcelable(KEY_ICON, item.getIcon());
+ }
+ return bundle;
+ }
+
+ /**
+ * Combines all the options that have been set and returns a new {@link BrowserActionIntent}
+ * object.
+ */
+ public BrowserActionIntent build() {
+ mIntent.putExtra(EXTRA_TYPE, mType);
+ mIntent.putParcelableArrayListExtra(EXTRA_MENU_ITEMS, mMenuItems);
+ return new BrowserActionIntent(mIntent);
+ }
+ }
+
+ /**
+ * Method to show a browser action menu.
+ * @param context The source Context.
+ * @param url The URL to load in the Custom Tab.
Yusuf 2017/04/03 18:00:48 Update comment.
ltian 2017/04/05 01:44:28 Done.
+ */
+ public void openBrowserAction(Context context, Uri url) {
+ intent.setData(url);
+ ContextCompat.startActivity(context, intent, null);
+ }
+
+ /**
+ * Method to create a lightweight Intent to check whether Chrome is available for browser action
Yusuf 2017/04/03 18:00:48 no mention of Chrome
ltian 2017/04/05 01:44:28 Done.
+ * request.
+ * @return The Intent to test availability of Chrome for handling browser action request.
+ */
+ public static Intent getLightWeightBrowserActionIntent() {
+ return new Intent(BrowserActionIntent.ACTION_BROWSER_ACTION_OPEN, Uri.parse(TEST_URL));
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698