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++) { |
194 assert items.get(i).getTitle() != null; | |
Yusuf
2017/07/14 05:35:34
assert would not help with production builds so th
ltian
2017/07/14 22:15:02
Done.
| |
195 assert items.get(i).getAction() != null; | |
167 mMenuItems.add(getBundleFromItem(items.get(i))); | 196 mMenuItems.add(getBundleFromItem(items.get(i))); |
168 } | 197 } |
169 return this; | 198 return this; |
170 } | 199 } |
171 | 200 |
172 /** | 201 /** |
202 * Set the PendingIntent to be launched when a a browser specified menu item is selected. | |
203 * @param onItemSelectedPendingIntent The PendingIntent to be launched. | |
204 */ | |
205 public Builder setSelectedAction(PendingIntent onItemSelectedPendingInte nt) { | |
Yusuf
2017/07/14 05:35:35
setOnItemSelectedAction
ltian
2017/07/14 22:15:02
Done.
| |
206 mOnItemSelectedPendingIntent = onItemSelectedPendingIntent; | |
207 return this; | |
208 } | |
209 | |
210 /** | |
173 * Populates a {@link Bundle} to hold a custom item for Browser Actions menu. | 211 * Populates a {@link Bundle} to hold a custom item for Browser Actions menu. |
174 * @param item A custom item for Browser Actions menu. | 212 * @param item A custom item for Browser Actions menu. |
175 * @return The Bundle of custom item. | 213 * @return The Bundle of custom item. |
176 */ | 214 */ |
177 private Bundle getBundleFromItem(BrowserActionItem item) { | 215 private Bundle getBundleFromItem(BrowserActionItem item) { |
178 Bundle bundle = new Bundle(); | 216 Bundle bundle = new Bundle(); |
179 bundle.putString(KEY_TITLE, item.getTitle()); | 217 bundle.putString(KEY_TITLE, item.getTitle()); |
180 bundle.putParcelable(KEY_ACTION, item.getAction()); | 218 bundle.putParcelable(KEY_ACTION, item.getAction()); |
181 if (item.getIcon() != null) bundle.putParcelable(KEY_ICON, item.getI con()); | 219 if (item.getIcon() != null) bundle.putParcelable(KEY_ICON, item.getI con()); |
182 return bundle; | 220 return bundle; |
183 } | 221 } |
184 | 222 |
185 /** | 223 /** |
186 * Combines all the options that have been set and returns a new {@link | 224 * Combines all the options that have been set and returns a new {@link |
187 * BrowserActionsIntent} object. | 225 * BrowserActionsIntent} object. |
188 */ | 226 */ |
189 public BrowserActionsIntent build() { | 227 public BrowserActionsIntent build() { |
190 mIntent.setData(mUri); | 228 mIntent.setData(mUri); |
191 mIntent.putExtra(EXTRA_TYPE, mType); | 229 mIntent.putExtra(EXTRA_TYPE, mType); |
192 mIntent.putParcelableArrayListExtra(EXTRA_MENU_ITEMS, mMenuItems); | 230 mIntent.putParcelableArrayListExtra(EXTRA_MENU_ITEMS, mMenuItems); |
193 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); | 231 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
194 mIntent.putExtra(EXTRA_APP_ID, pendingIntent); | 232 mIntent.putExtra(EXTRA_APP_ID, pendingIntent); |
233 mIntent.putExtra(EXTRA_SELECTED_ACTION_PENDING_INTENT, mOnItemSelect edPendingIntent); | |
195 return new BrowserActionsIntent(mIntent); | 234 return new BrowserActionsIntent(mIntent); |
196 } | 235 } |
197 } | 236 } |
198 | 237 |
199 /** | 238 /** |
200 * Open a Browser Actions menu with default settings. | 239 * 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. | 240 * @param context The context requesting for a Browser Actions menu. |
204 * @param uri The url for Browser Actions menu. | 241 * @param uri The url for Browser Actions menu. |
205 */ | 242 */ |
206 public static void openBrowserAction(Context context, Uri uri) { | 243 public static void openBrowserAction(Context context, Uri uri) { |
207 if (hasBrowserActionsIntentHandler(context)) { | 244 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(context, uri).build(); |
208 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(conte xt, uri).build(); | 245 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 } | 246 } |
215 | 247 |
216 /** | 248 /** |
217 * Open a Browser Actions menu with custom items. | 249 * 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. | 250 * @param context The context requesting for a Browser Actions menu. |
221 * @param uri The url for Browser Actions menu. | 251 * @param uri The url for Browser Actions menu. |
222 * @param type The type of the url for context menu to be opened. | 252 * @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. | 253 * @param items List of custom items to be added to Browser Actions menu. |
254 * @param pendingIntent The PendingIntent to be launched when a browser spec ified menu item is | |
255 * selected. The browser will perform the PendingIntent so this should only go to a {@link | |
256 * BroadcastReceiver}. | |
224 */ | 257 */ |
225 public static void openBrowserAction( | 258 public static void openBrowserAction(Context context, Uri uri, int type, |
226 Context context, Uri uri, int type, ArrayList<BrowserActionItem> ite ms) { | 259 ArrayList<BrowserActionItem> items, PendingIntent pendingIntent) { |
227 if (hasBrowserActionsIntentHandler(context)) { | 260 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(context, uri) |
228 BrowserActionsIntent intent = new BrowserActionsIntent.Builder(conte xt, uri) | 261 .setUrlType(type) |
229 .setUrlType(type) | 262 .setCustomItems(items) |
230 .setCustomItems(items) | 263 .setSelectedAction(pendingIntent) |
231 .build(); | 264 .build(); |
232 ContextCompat.startActivity(context, intent.getIntent(), null); | 265 launchIntent(context, intent.getIntent()); |
233 } else { | |
234 openFallbackBrowserActionsMenu(context, uri, type, items); | |
235 } | |
236 } | 266 } |
237 | 267 |
238 /** | 268 /** |
239 * Check whether any Browser Actions provider is available to handle the {@l ink | 269 * Launch an Intent to open a Browser Actions menu. |
270 * It first checks if any Browser Actions provider is available to create th e menu. | |
271 * If the default Browser supports Browser Actions, menu will be opened by t he default Browser, | |
272 * otherwise show a intent picker. | |
273 * If not provider, a Browser Actions menu is opened locally from support li brary. | |
274 * @param context The context requesting for a Browser Actions menu. | |
275 * @param intent The {@link Intent} holds the setting for Browser Actions me nu. | |
276 */ | |
277 public static void launchIntent(Context context, Intent intent) { | |
278 List<ResolveInfo> handlers = getBrowserActionsIntentHandlers(context); | |
279 if (handlers == null || handlers.size() == 0) { | |
280 openFallbackBrowserActionsMenu(context, intent); | |
281 return; | |
282 } else if (handlers.size() == 1) { | |
283 intent.setPackage(handlers.get(0).activityInfo.packageName); | |
284 } else { | |
285 Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(TEST_UR L)); | |
286 ResolveInfo defaultHandler = CustomTabsClient.getDefaultHandlerForIn tent(context.getPackageManager(), viewIntent); | |
Yusuf
2017/07/14 05:35:34
indentation wrong here?
ltian
2017/07/14 22:15:02
After revert it back, there should be no problem.
| |
287 if (defaultHandler != null) { | |
288 String defaultPackageName = defaultHandler.activityInfo.packageN ame; | |
289 for (int i = 0; i < handlers.size(); i++) { | |
290 if (defaultPackageName.equals(handlers.get(i).activityInfo.p ackageName)) { | |
291 intent.setPackage(defaultPackageName); | |
292 break; | |
293 } | |
294 } | |
295 } | |
296 } | |
297 ContextCompat.startActivity(context, intent, null); | |
298 } | |
299 | |
300 /** | |
301 * Returns a list of Browser Actions providers available to handle the {@lin k | |
240 * BrowserActionsIntent}. | 302 * BrowserActionsIntent}. |
241 * @param context The context requesting for a Browser Actions menu. | 303 * @param context The context requesting for a Browser Actions menu. |
242 * @return true If a Browser Actions provider is available handle the intent . | 304 * @return List of Browser Actions providers available to handle the intent. |
243 */ | 305 */ |
244 private static boolean hasBrowserActionsIntentHandler(Context context) { | 306 private static List<ResolveInfo> getBrowserActionsIntentHandlers(Context con text) { |
245 Intent intent = | 307 Intent intent = |
246 new Intent(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN, Uri .parse(TEST_URL)); | 308 new Intent(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN, Uri .parse(TEST_URL)); |
247 PackageManager pm = context.getPackageManager(); | 309 PackageManager pm = context.getPackageManager(); |
248 List<ResolveInfo> resolveInfoList = | 310 return pm.queryIntentActivities(intent, PackageManager.MATCH_ALL); |
249 pm.queryIntentActivities(intent, PackageManager.MATCH_ALL); | 311 } |
250 return resolveInfoList.size() > 0 ? true : false; | 312 |
313 private static void openFallbackBrowserActionsMenu( | |
314 Context context, BrowserActionsIntent browserActionsIntent) { | |
315 openFallbackBrowserActionsMenu(context, browserActionsIntent.getIntent() ); | |
316 } | |
317 | |
318 private static void openFallbackBrowserActionsMenu(Context context, Intent i ntent) { | |
319 Uri uri = intent.getData(); | |
320 int type = intent.getIntExtra(EXTRA_TYPE, URL_TYPE_NONE); | |
321 ArrayList<Bundle> bundles = intent.getParcelableArrayListExtra(EXTRA_MEN U_ITEMS); | |
322 List<BrowserActionItem> items = bundles != null ? parseBrowserActionItem s(bundles) : null; | |
323 openFallbackBrowserActionsMenu(context, uri, type, items); | |
251 } | 324 } |
252 | 325 |
253 /** | 326 /** |
254 * Open a Browser Actions menu from support library. | 327 * Open a Browser Actions menu from support library. |
255 * @param context The context requesting for a Browser Actions menu. | 328 * @param context The context requesting for a Browser Actions menu. |
256 * @param uri The url for Browser Actions menu. | 329 * @param uri The url for Browser Actions menu. |
257 * @param type The type of the url for context menu to be opened. | 330 * @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. | 331 * @param items List of custom items to add to Browser Actions menu. |
259 */ | 332 */ |
260 private static void openFallbackBrowserActionsMenu( | 333 private static void openFallbackBrowserActionsMenu( |
261 Context context, Uri uri, int type, ArrayList<BrowserActionItem> ite ms) { | 334 Context context, Uri uri, int type, List<BrowserActionItem> items) { |
262 return; | 335 return; |
263 } | 336 } |
264 | 337 |
265 /** | 338 /** |
339 * Gets custom item list for browser action menu. | |
340 * @param bundles Data for custom items from {@link BrowserActionsIntent}. | |
341 * @return List of {@link BrowserActionItem} | |
342 */ | |
343 public static List<BrowserActionItem> parseBrowserActionItems(ArrayList<Bund le> bundles) { | |
344 List<BrowserActionItem> mActions = new ArrayList<>(); | |
345 for (int i = 0; i < bundles.size(); i++) { | |
346 Bundle bundle = bundles.get(i); | |
347 String title = bundle.getString(BrowserActionsIntent.KEY_TITLE); | |
348 PendingIntent action = bundle.getParcelable(BrowserActionsIntent.KEY _ACTION); | |
349 Bitmap icon = bundle.getParcelable(BrowserActionsIntent.KEY_ICON); | |
350 if (title != null && action != null) { | |
351 BrowserActionItem item = new BrowserActionItem(title, action); | |
352 if (icon != null) { | |
353 item.setIcon(icon); | |
354 } | |
355 mActions.add(item); | |
356 } else if (title != null) { | |
357 Log.e(TAG, "Missing action for item: " + i); | |
Yusuf
2017/07/14 05:35:34
These are still here?
ltian
2017/07/14 22:15:02
The reason I still leave these checks here is beca
Yusuf
2017/07/17 17:08:52
Then again, we should be throwing IllegalArgumentE
| |
358 } else if (action != null) { | |
359 Log.e(TAG, "Missing title for item: " + i); | |
360 } else { | |
361 Log.e(TAG, "Missing title and action for item: " + i); | |
362 } | |
363 } | |
364 return mActions; | |
365 } | |
366 | |
367 /** | |
266 * Get the package name of the creator application. | 368 * Get the package name of the creator application. |
267 * @param intent The {@link BrowserActionsIntent}. | 369 * @param intent The {@link BrowserActionsIntent}. |
268 * @return The creator package name. | 370 * @return The creator package name. |
269 */ | 371 */ |
270 @SuppressWarnings("deprecation") | 372 @SuppressWarnings("deprecation") |
271 public static String getCreatorPackageName(Intent intent) { | 373 public static String getCreatorPackageName(Intent intent) { |
272 PendingIntent pendingIntent = intent.getParcelableExtra(BrowserActionsIn tent.EXTRA_APP_ID); | 374 PendingIntent pendingIntent = intent.getParcelableExtra(BrowserActionsIn tent.EXTRA_APP_ID); |
273 if (pendingIntent != null) { | 375 if (pendingIntent != null) { |
274 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | 376 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
275 return pendingIntent.getCreatorPackage(); | 377 return pendingIntent.getCreatorPackage(); |
276 } else { | 378 } else { |
277 return pendingIntent.getTargetPackage(); | 379 return pendingIntent.getTargetPackage(); |
278 } | 380 } |
279 } | 381 } |
280 return null; | 382 return null; |
281 } | 383 } |
282 } | 384 } |
OLD | NEW |