OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2017 The Android Open Source Project | 2 * Copyright (C) 2017 The Android Open Source Project |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
5 * you may not use this file except in compliance with the License. | 5 * you may not use this file except in compliance with the License. |
6 * You may obtain a copy of the License at | 6 * You may obtain a copy of the License at |
7 * | 7 * |
8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * | 9 * |
10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
11 * distributed under the License is distributed on an "AS IS" BASIS, | 11 * distributed under the License is distributed on an "AS IS" BASIS, |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 * See the License for the specific language governing permissions and | 13 * See the License for the specific language governing permissions and |
14 * limitations under the License. | 14 * limitations under the License. |
15 */ | 15 */ |
16 package android.support.customtabs.browseractions; | 16 package android.support.customtabs.browseractions; |
17 | 17 |
18 import android.app.PendingIntent; | 18 import android.app.PendingIntent; |
19 import android.content.Context; | 19 import android.content.Context; |
20 import android.content.Intent; | 20 import android.content.Intent; |
21 import android.content.pm.PackageManager; | 21 import android.content.pm.PackageManager; |
22 import android.content.pm.ResolveInfo; | 22 import android.content.pm.ResolveInfo; |
23 import android.graphics.Bitmap; | |
23 import android.net.Uri; | 24 import android.net.Uri; |
24 import android.os.Build; | 25 import android.os.Build; |
25 import android.os.Bundle; | 26 import android.os.Bundle; |
26 import android.support.annotation.IntDef; | 27 import android.support.annotation.IntDef; |
27 import android.support.annotation.NonNull; | 28 import android.support.annotation.NonNull; |
29 import android.support.customtabs.CustomTabsClient; | |
28 import android.support.customtabs.CustomTabsIntent; | 30 import android.support.customtabs.CustomTabsIntent; |
29 import android.support.customtabs.CustomTabsSession; | 31 import android.support.customtabs.CustomTabsSession; |
30 import android.support.v4.content.ContextCompat; | 32 import android.support.v4.content.ContextCompat; |
33 import android.util.Log; | |
31 | 34 |
32 import java.lang.annotation.Retention; | 35 import java.lang.annotation.Retention; |
33 import java.lang.annotation.RetentionPolicy; | 36 import java.lang.annotation.RetentionPolicy; |
34 import java.util.ArrayList; | 37 import java.util.ArrayList; |
35 import java.util.List; | 38 import java.util.List; |
36 | 39 |
37 /** | 40 /** |
38 * Class holding the {@link Intent} and start bundle for a Browser Actions Activ ity. | 41 * Class holding the {@link Intent} and start bundle for a Browser Actions Activ ity. |
39 * | 42 * |
40 * <p> | 43 * <p> |
41 * <strong>Note:</strong> The constants below are public for the browser impleme ntation's benefit. | 44 * <strong>Note:</strong> The constants below are public for the browser impleme ntation's benefit. |
42 * You are strongly encouraged to use {@link BrowserActionsIntent.Builder}.</p> | 45 * You are strongly encouraged to use {@link BrowserActionsIntent.Builder}.</p> |
43 */ | 46 */ |
44 public class BrowserActionsIntent { | 47 public class BrowserActionsIntent { |
48 private final static String TAG = "BrowserActions"; | |
45 private final static String TEST_URL = "https://www.example.com"; | 49 private final static String TEST_URL = "https://www.example.com"; |
46 | 50 |
47 /** | 51 /** |
48 * Extra that specifies {@link PendingIntent} indicating which Application s ends the {@link | 52 * Extra that specifies {@link PendingIntent} indicating which Application s ends the {@link |
49 * BrowserActionsIntent}. | 53 * BrowserActionsIntent}. |
50 */ | 54 */ |
51 public final static String EXTRA_APP_ID = "android.support.customtabs.browse ractions.APP_ID"; | 55 public final static String EXTRA_APP_ID = "android.support.customtabs.browse ractions.APP_ID"; |
52 | 56 |
53 /** | 57 /** |
54 * Indicates that the user explicitly opted out of Browser Actions in the ca lling application. | 58 * Indicates that the user explicitly opted out of Browser Actions in the ca lling application. |
(...skipping 22 matching lines...) Expand all Loading... | |
77 */ | 81 */ |
78 public static final String EXTRA_TYPE = "android.support.customtabs.browsera ctions.extra.TYPE"; | 82 public static final String EXTRA_TYPE = "android.support.customtabs.browsera ctions.extra.TYPE"; |
79 | 83 |
80 /** | 84 /** |
81 * Extra that specifies List<Bundle> used for adding custom items to the Bro wser Actions menu. | 85 * Extra that specifies List<Bundle> used for adding custom items to the Bro wser Actions menu. |
82 */ | 86 */ |
83 public static final String EXTRA_MENU_ITEMS = | 87 public static final String EXTRA_MENU_ITEMS = |
84 "android.support.customtabs.browseractions.extra.MENU_ITEMS"; | 88 "android.support.customtabs.browseractions.extra.MENU_ITEMS"; |
85 | 89 |
86 /** | 90 /** |
91 * Extra that specifies the PendingIntent to be launched when a browser spec ified menu item is | |
92 * selected. The id of the chosen item will be notified through the data of its Intent. | |
93 */ | |
94 public static final String EXTRA_SELECTED_ACTION_PENDING_INTENT = | |
95 "android.support.customtabs.browseractions.extra.SELECTED_ACTION_PEN DING_INTENT"; | |
96 | |
97 /** | |
87 * The maximum allowed number of custom items. | 98 * The maximum allowed number of custom items. |
88 */ | 99 */ |
89 public static final int MAX_CUSTOM_ITEMS = 5; | 100 public static final int MAX_CUSTOM_ITEMS = 5; |
90 | 101 |
91 /** | 102 /** |
92 * Defines the types of url for Browser Actions menu. | 103 * Defines the types of url for Browser Actions menu. |
93 */ | 104 */ |
94 @IntDef({URL_TYPE_NONE, URL_TYPE_IMAGE, URL_TYPE_VIDEO, URL_TYPE_AUDIO, URL_ TYPE_FILE, | 105 @IntDef({URL_TYPE_NONE, URL_TYPE_IMAGE, URL_TYPE_VIDEO, URL_TYPE_AUDIO, URL_ TYPE_FILE, |
95 URL_TYPE_PLUGIN}) | 106 URL_TYPE_PLUGIN}) |
96 @Retention(RetentionPolicy.SOURCE) | 107 @Retention(RetentionPolicy.SOURCE) |
97 public @interface BrowserActionsUrlType {} | 108 public @interface BrowserActionsUrlType {} |
98 public static final int URL_TYPE_NONE = 0; | 109 public static final int URL_TYPE_NONE = 0; |
99 public static final int URL_TYPE_IMAGE = 1; | 110 public static final int URL_TYPE_IMAGE = 1; |
100 public static final int URL_TYPE_VIDEO = 2; | 111 public static final int URL_TYPE_VIDEO = 2; |
101 public static final int URL_TYPE_AUDIO = 3; | 112 public static final int URL_TYPE_AUDIO = 3; |
102 public static final int URL_TYPE_FILE = 4; | 113 public static final int URL_TYPE_FILE = 4; |
103 public static final int URL_TYPE_PLUGIN = 5; | 114 public static final int URL_TYPE_PLUGIN = 5; |
104 | 115 |
105 /** | 116 /** |
117 * Defines the the ids of the browser specified menu items in Browser Action s. | |
118 * TODO(ltian): A long term solution need, since other providers might have customized menus. | |
119 */ | |
120 @IntDef({ITEM_INVALID_ITEM, ITEM_OPEN_IN_NEW_TAB, ITEM_OPEN_IN_INCOGNITO, IT EM_DOWNLOAD, | |
121 ITEM_COPY, ITEM_SHARE}) | |
122 @Retention(RetentionPolicy.SOURCE) | |
123 public @interface BrowserActionsItemId {} | |
124 public static final int ITEM_INVALID_ITEM = -1; | |
125 public static final int ITEM_OPEN_IN_NEW_TAB = 0; | |
126 public static final int ITEM_OPEN_IN_INCOGNITO = 1; | |
127 public static final int ITEM_DOWNLOAD = 2; | |
128 public static final int ITEM_COPY = 3; | |
129 public static final int ITEM_SHARE = 4; | |
130 | |
131 /** | |
106 * An {@link Intent} used to start the Browser Actions Activity. | 132 * An {@link Intent} used to start the Browser Actions Activity. |
107 */ | 133 */ |
108 private final Intent mIntent; | 134 public final Intent mIntent; |
109 | 135 |
110 /** | 136 /** |
111 * Gets the Intent of {@link BrowserActionsIntent}. | 137 * Gets the Intent of {@link BrowserActionsIntent}. |
112 * @return the Intent of {@link BrowserActionsIntent}. | 138 * @return the Intent of {@link BrowserActionsIntent}. |
113 */ | 139 */ |
114 public Intent getIntent() { | 140 public Intent getIntent() { |
115 return mIntent; | 141 return mIntent; |
116 } | 142 } |
117 | 143 |
118 private BrowserActionsIntent(@NonNull Intent intent) { | 144 private BrowserActionsIntent(@NonNull Intent intent) { |
119 this.mIntent = intent; | 145 this.mIntent = intent; |
120 } | 146 } |
121 | 147 |
122 /** | 148 /** |
123 * Builder class for opening a Browser Actions context menu. | 149 * Builder class for opening a Browser Actions context menu. |
124 */ | 150 */ |
125 public static final class Builder { | 151 public static final class Builder { |
126 private final Intent mIntent = new Intent(BrowserActionsIntent.ACTION_BR OWSER_ACTIONS_OPEN); | 152 private final Intent mIntent = new Intent(BrowserActionsIntent.ACTION_BR OWSER_ACTIONS_OPEN); |
127 private Context mContext; | 153 private Context mContext; |
128 private Uri mUri; | 154 private Uri mUri; |
129 @BrowserActionsUrlType | 155 @BrowserActionsUrlType |
130 private int mType; | 156 private int mType; |
131 private ArrayList<Bundle> mMenuItems = null; | 157 private ArrayList<Bundle> mMenuItems = null; |
158 private PendingIntent mOnItemSelectedPendingIntent = null; | |
132 | 159 |
133 /** | 160 /** |
134 * Constructs a {@link BrowserActionsIntent.Builder} object associated w ith default setting | 161 * Constructs a {@link BrowserActionsIntent.Builder} object associated w ith default setting |
135 * for a selected url. | 162 * for a selected url. |
136 * @param context The context requesting the Browser Actions context men u. | 163 * @param context The context requesting the Browser Actions context men u. |
137 * @param uri The selected url for Browser Actions menu. | 164 * @param uri The selected url for Browser Actions menu. |
138 */ | 165 */ |
139 public Builder(Context context, Uri uri) { | 166 public Builder(Context context, Uri uri) { |
140 mContext = context; | 167 mContext = context; |
141 mUri = uri; | 168 mUri = uri; |
(...skipping 15 matching lines...) Expand all Loading... | |
157 * Only maximum {@link BrowserActionsIntent.MAX_CUSTOM_ITEMS} custom ite ms are allowed, | 184 * Only maximum {@link BrowserActionsIntent.MAX_CUSTOM_ITEMS} custom ite ms are allowed, |
158 * otherwise throws an {@link IllegalStateException}. | 185 * otherwise throws an {@link IllegalStateException}. |
159 * @param items The list of {@link BrowserActionItem} for custom items. | 186 * @param items The list of {@link BrowserActionItem} for custom items. |
160 */ | 187 */ |
161 public Builder setCustomItems(ArrayList<BrowserActionItem> items) { | 188 public Builder setCustomItems(ArrayList<BrowserActionItem> items) { |
162 if (items.size() >= MAX_CUSTOM_ITEMS) { | 189 if (items.size() >= MAX_CUSTOM_ITEMS) { |
163 throw new IllegalStateException( | 190 throw new IllegalStateException( |
164 "Exceeded maximum toolbar item count of " + MAX_CUSTOM_I TEMS); | 191 "Exceeded maximum toolbar item count of " + MAX_CUSTOM_I TEMS); |
165 } | 192 } |
166 for (int i = 0; i < items.size(); i++) { | 193 for (int i = 0; i < items.size(); i++) { |
167 mMenuItems.add(getBundleFromItem(items.get(i))); | 194 if (items.get(i).getTitle() == null) { |
195 throw new IllegalArgumentException("Custom item title is nul l"); | |
196 } else if (items.get(i).getAction() == null) { | |
197 throw new IllegalArgumentException("Custom item action is nu ll"); | |
198 } else { | |
199 mMenuItems.add(getBundleFromItem(items.get(i))); | |
200 } | |
168 } | 201 } |
169 return this; | 202 return this; |
170 } | 203 } |
171 | 204 |
172 /** | 205 /** |
206 * Set the PendingIntent to be launched when a a browser specified menu item is selected. | |
207 * @param onItemSelectedPendingIntent The PendingIntent to be launched. | |
208 */ | |
209 public Builder setOnItemSelectedAction(PendingIntent onItemSelectedPendi ngIntent) { | |
210 mOnItemSelectedPendingIntent = onItemSelectedPendingIntent; | |
211 return this; | |
212 } | |
213 | |
214 /** | |
173 * Populates a {@link Bundle} to hold a custom item for Browser Actions menu. | 215 * Populates a {@link Bundle} to hold a custom item for Browser Actions menu. |
174 * @param item A custom item for Browser Actions menu. | 216 * @param item A custom item for Browser Actions menu. |
175 * @return The Bundle of custom item. | 217 * @return The Bundle of custom item. |
176 */ | 218 */ |
177 private Bundle getBundleFromItem(BrowserActionItem item) { | 219 private Bundle getBundleFromItem(BrowserActionItem item) { |
178 Bundle bundle = new Bundle(); | 220 Bundle bundle = new Bundle(); |
179 bundle.putString(KEY_TITLE, item.getTitle()); | 221 bundle.putString(KEY_TITLE, item.getTitle()); |
180 bundle.putParcelable(KEY_ACTION, item.getAction()); | 222 bundle.putParcelable(KEY_ACTION, item.getAction()); |
181 if (item.getIcon() != null) bundle.putParcelable(KEY_ICON, item.getI con()); | 223 if (item.getIcon() != null) bundle.putParcelable(KEY_ICON, item.getI con()); |
182 return bundle; | 224 return bundle; |
183 } | 225 } |
184 | 226 |
185 /** | 227 /** |
186 * Combines all the options that have been set and returns a new {@link | 228 * Combines all the options that have been set and returns a new {@link |
187 * BrowserActionsIntent} object. | 229 * BrowserActionsIntent} object. |
188 */ | 230 */ |
189 public BrowserActionsIntent build() { | 231 public BrowserActionsIntent build() { |
190 mIntent.setData(mUri); | 232 mIntent.setData(mUri); |
191 mIntent.putExtra(EXTRA_TYPE, mType); | 233 mIntent.putExtra(EXTRA_TYPE, mType); |
192 mIntent.putParcelableArrayListExtra(EXTRA_MENU_ITEMS, mMenuItems); | 234 mIntent.putParcelableArrayListExtra(EXTRA_MENU_ITEMS, mMenuItems); |
193 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); | 235 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
194 mIntent.putExtra(EXTRA_APP_ID, pendingIntent); | 236 mIntent.putExtra(EXTRA_APP_ID, pendingIntent); |
237 mIntent.putExtra(EXTRA_SELECTED_ACTION_PENDING_INTENT, mOnItemSelect edPendingIntent); | |
195 return new BrowserActionsIntent(mIntent); | 238 return new BrowserActionsIntent(mIntent); |
196 } | 239 } |
197 } | 240 } |
198 | 241 |
199 /** | 242 /** |
200 * Open a Browser Actions menu with default settings. | 243 * Construct a BrowserActionsIntent with default settings and launch it to o pen a Browser Actions menu. |
201 * It first checks if any Browser Actions provider is available to create a context menu. | |
202 * If not, open a Browser Actions menu locally from support library. | |
203 * @param context The context requesting for a Browser Actions menu. | 244 * @param context The context requesting for a Browser Actions menu. |
204 * @param uri The url for Browser Actions menu. | 245 * @param uri The url for Browser Actions menu. |
205 */ | 246 */ |
206 public static void openBrowserAction(Context context, Uri uri) { | 247 public static void openBrowserAction(Context context, Uri uri) { |
207 if (hasBrowserActionsIntentHandler(context)) { | 248 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(context, uri).build(); |
208 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(conte xt, uri).build(); | 249 launchIntent(context, intent.getIntent()); |
209 ContextCompat.startActivity(context, intent.getIntent(), null); | |
210 } else { | |
211 openFallbackBrowserActionsMenu( | |
212 context, uri, URL_TYPE_NONE, new ArrayList<BrowserActionItem >()); | |
213 } | |
214 } | 250 } |
215 | 251 |
216 /** | 252 /** |
217 * Open a Browser Actions menu with custom items. | 253 * Construct a BrowserActionsIntent with custom settings and launch it to op en a Browser Actions menu. |
218 * It first checks if any Browser Actions provider is available to create a context menu. | |
219 * If not, open a Browser Actions menu locally from support library. | |
220 * @param context The context requesting for a Browser Actions menu. | 254 * @param context The context requesting for a Browser Actions menu. |
221 * @param uri The url for Browser Actions menu. | 255 * @param uri The url for Browser Actions menu. |
222 * @param type The type of the url for context menu to be opened. | 256 * @param type The type of the url for context menu to be opened. |
223 * @param items List of custom items to be added to Browser Actions menu. | 257 * @param items List of custom items to be added to Browser Actions menu. |
258 * @param pendingIntent The PendingIntent to be launched when a browser spec ified menu item is | |
259 * selected. The browser will perform the PendingIntent so this should only go to a {@link | |
260 * BroadcastReceiver}. | |
224 */ | 261 */ |
225 public static void openBrowserAction( | 262 public static void openBrowserAction(Context context, Uri uri, int type, |
226 Context context, Uri uri, int type, ArrayList<BrowserActionItem> ite ms) { | 263 ArrayList<BrowserActionItem> items, PendingIntent pendingIntent) { |
227 if (hasBrowserActionsIntentHandler(context)) { | 264 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(context, uri) |
228 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(conte xt, uri) | 265 .setUrlType(type) |
229 .setUrlType(type) | 266 .setCustomItems(items) |
230 .setCustomItems(items) | 267 .setOnItemSelectedAction(pendingIntent) |
231 .build(); | 268 .build(); |
232 ContextCompat.startActivity(context, intent.getIntent(), null); | 269 launchIntent(context, intent.getIntent()); |
233 } else { | |
234 openFallbackBrowserActionsMenu(context, uri, type, items); | |
235 } | |
236 } | 270 } |
237 | 271 |
238 /** | 272 /** |
239 * Check whether any Browser Actions provider is available to handle the {@l ink | 273 * Launch an Intent to open a Browser Actions menu. |
274 * It first checks if any Browser Actions provider is available to create th e menu. | |
275 * If the default Browser supports Browser Actions, menu will be opened by t he default Browser, | |
276 * otherwise show a intent picker. | |
277 * If not provider, a Browser Actions menu is opened locally from support li brary. | |
278 * @param context The context requesting for a Browser Actions menu. | |
279 * @param intent The {@link Intent} holds the setting for Browser Actions me nu. | |
280 */ | |
281 public static void launchIntent(Context context, Intent intent) { | |
282 List<ResolveInfo> handlers = getBrowserActionsIntentHandlers(context); | |
283 if (handlers == null || handlers.size() == 0) { | |
284 openFallbackBrowserActionsMenu(context, intent); | |
285 return; | |
286 } else if (handlers.size() == 1) { | |
287 intent.setPackage(handlers.get(0).activityInfo.packageName); | |
288 } else { | |
289 Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(TEST_UR L)); | |
290 PackageManager pm = context.getPackageManager(); | |
291 ResolveInfo defaultHandler = pm.resolveActivity(viewIntent, PackageM anager.MATCH_DEFAULT_ONLY); | |
Yusuf
2017/07/17 17:08:53
>100 lines?
ltian
2017/07/17 17:43:36
Yes, looks like the git cl upload does not check f
| |
292 if (defaultHandler != null) { | |
293 String defaultPackageName = defaultHandler.activityInfo.packageN ame; | |
294 for (int i = 0; i < handlers.size(); i++) { | |
295 if (defaultPackageName.equals(handlers.get(i).activityInfo.p ackageName)) { | |
296 intent.setPackage(defaultPackageName); | |
297 break; | |
298 } | |
299 } | |
300 } | |
301 } | |
302 ContextCompat.startActivity(context, intent, null); | |
303 } | |
304 | |
305 /** | |
306 * Returns a list of Browser Actions providers available to handle the {@lin k | |
240 * BrowserActionsIntent}. | 307 * BrowserActionsIntent}. |
241 * @param context The context requesting for a Browser Actions menu. | 308 * @param context The context requesting for a Browser Actions menu. |
242 * @return true If a Browser Actions provider is available handle the intent . | 309 * @return List of Browser Actions providers available to handle the intent. |
243 */ | 310 */ |
244 private static boolean hasBrowserActionsIntentHandler(Context context) { | 311 private static List<ResolveInfo> getBrowserActionsIntentHandlers(Context con text) { |
245 Intent intent = | 312 Intent intent = |
246 new Intent(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN, Uri .parse(TEST_URL)); | 313 new Intent(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN, Uri .parse(TEST_URL)); |
247 PackageManager pm = context.getPackageManager(); | 314 PackageManager pm = context.getPackageManager(); |
248 List<ResolveInfo> resolveInfoList = | 315 return pm.queryIntentActivities(intent, PackageManager.MATCH_ALL); |
249 pm.queryIntentActivities(intent, PackageManager.MATCH_ALL); | 316 } |
250 return resolveInfoList.size() > 0 ? true : false; | 317 |
318 private static void openFallbackBrowserActionsMenu( | |
319 Context context, BrowserActionsIntent browserActionsIntent) { | |
320 openFallbackBrowserActionsMenu(context, browserActionsIntent.getIntent() ); | |
321 } | |
322 | |
323 private static void openFallbackBrowserActionsMenu(Context context, Intent i ntent) { | |
324 Uri uri = intent.getData(); | |
325 int type = intent.getIntExtra(EXTRA_TYPE, URL_TYPE_NONE); | |
326 ArrayList<Bundle> bundles = intent.getParcelableArrayListExtra(EXTRA_MEN U_ITEMS); | |
327 List<BrowserActionItem> items = bundles != null ? parseBrowserActionItem s(bundles) : null; | |
328 openFallbackBrowserActionsMenu(context, uri, type, items); | |
251 } | 329 } |
252 | 330 |
253 /** | 331 /** |
254 * Open a Browser Actions menu from support library. | 332 * Open a Browser Actions menu from support library. |
255 * @param context The context requesting for a Browser Actions menu. | 333 * @param context The context requesting for a Browser Actions menu. |
256 * @param uri The url for Browser Actions menu. | 334 * @param uri The url for Browser Actions menu. |
257 * @param type The type of the url for context menu to be opened. | 335 * @param type The type of the url for context menu to be opened. |
258 * @param items List of custom items to add to Browser Actions menu. | 336 * @param items List of custom items to add to Browser Actions menu. |
259 */ | 337 */ |
260 private static void openFallbackBrowserActionsMenu( | 338 private static void openFallbackBrowserActionsMenu( |
261 Context context, Uri uri, int type, ArrayList<BrowserActionItem> ite ms) { | 339 Context context, Uri uri, int type, List<BrowserActionItem> items) { |
262 return; | 340 return; |
263 } | 341 } |
264 | 342 |
265 /** | 343 /** |
344 * Gets custom item list for browser action menu. | |
345 * @param bundles Data for custom items from {@link BrowserActionsIntent}. | |
346 * @return List of {@link BrowserActionItem} | |
347 */ | |
348 public static List<BrowserActionItem> parseBrowserActionItems(ArrayList<Bund le> bundles) { | |
349 List<BrowserActionItem> mActions = new ArrayList<>(); | |
350 for (int i = 0; i < bundles.size(); i++) { | |
351 Bundle bundle = bundles.get(i); | |
352 String title = bundle.getString(BrowserActionsIntent.KEY_TITLE); | |
353 PendingIntent action = bundle.getParcelable(BrowserActionsIntent.KEY _ACTION); | |
354 Bitmap icon = bundle.getParcelable(BrowserActionsIntent.KEY_ICON); | |
355 if (title != null && action != null) { | |
356 BrowserActionItem item = new BrowserActionItem(title, action); | |
357 if (icon != null) { | |
358 item.setIcon(icon); | |
359 } | |
360 mActions.add(item); | |
361 } else if (title != null) { | |
362 Log.e(TAG, "Missing action for item: " + i); | |
363 } else if (action != null) { | |
364 Log.e(TAG, "Missing title for item: " + i); | |
365 } else { | |
366 Log.e(TAG, "Missing title and action for item: " + i); | |
367 } | |
368 } | |
369 return mActions; | |
370 } | |
371 | |
372 /** | |
266 * Get the package name of the creator application. | 373 * Get the package name of the creator application. |
267 * @param intent The {@link BrowserActionsIntent}. | 374 * @param intent The {@link BrowserActionsIntent}. |
268 * @return The creator package name. | 375 * @return The creator package name. |
269 */ | 376 */ |
270 @SuppressWarnings("deprecation") | 377 @SuppressWarnings("deprecation") |
271 public static String getCreatorPackageName(Intent intent) { | 378 public static String getCreatorPackageName(Intent intent) { |
272 PendingIntent pendingIntent = intent.getParcelableExtra(BrowserActionsIn tent.EXTRA_APP_ID); | 379 PendingIntent pendingIntent = intent.getParcelableExtra(BrowserActionsIn tent.EXTRA_APP_ID); |
273 if (pendingIntent != null) { | 380 if (pendingIntent != null) { |
274 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | 381 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
275 return pendingIntent.getCreatorPackage(); | 382 return pendingIntent.getCreatorPackage(); |
276 } else { | 383 } else { |
277 return pendingIntent.getTargetPackage(); | 384 return pendingIntent.getTargetPackage(); |
278 } | 385 } |
279 } | 386 } |
280 return null; | 387 return null; |
281 } | 388 } |
282 } | 389 } |
OLD | NEW |