OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package android.support.customtabs; | |
6 | |
7 import android.app.Activity; | |
8 import android.app.ActivityOptions; | |
9 import android.app.PendingIntent; | |
10 import android.content.Intent; | |
11 import android.graphics.Bitmap; | |
12 import android.graphics.Color; | |
13 import android.net.Uri; | |
14 import android.os.Build; | |
15 import android.os.Bundle; | |
16 import android.os.IBinder; | |
17 | |
18 import java.lang.reflect.InvocationTargetException; | |
19 import java.lang.reflect.Method; | |
20 | |
21 /** | |
22 * Constants and utilities that will be used for low level control on customizin
g the UI and | |
23 * functionality of a tab. | |
24 */ | |
25 public class CustomTabsIntent { | |
26 | |
27 /** | |
28 * Extra used to match the session. This has to be included in the intent to
open in | |
29 * a custom tab. This is the same IBinder that gets passed to ICustomTabsSer
vice#newSession. | |
30 * Null if there is no need to match any service side sessions with the inte
nt. | |
31 */ | |
32 public static final String EXTRA_SESSION = "android.support.customtabs.extra
.SESSION"; | |
33 | |
34 /** | |
35 * Extra that changes the background color for the toolbar. colorRes is an i
nt that specifies a | |
36 * {@link Color}, not a resource id. | |
37 */ | |
38 public static final String EXTRA_TOOLBAR_COLOR = | |
39 "android.support.customtabs.extra.TOOLBAR_COLOR"; | |
40 | |
41 /** | |
42 * Boolean extra that enables the url bar to hide as the user scrolls down t
he page | |
43 */ | |
44 public static final String EXTRA_ENABLE_URLBAR_HIDING = | |
45 "android.support.customtabs.extra.ENABLE_URLBAR_HIDING"; | |
46 | |
47 /** | |
48 * Bundle used for adding a custom action button to the custom tab toolbar.
The client can | |
49 * provide an icon {@link Bitmap} and a {@link PendingIntent} for the button
. | |
50 */ | |
51 public static final String EXTRA_ACTION_BUTTON_BUNDLE = | |
52 "android.support.customtabs.extra.ACTION_BUTTON_BUNDLE"; | |
53 | |
54 /** | |
55 * Key that specifies the {@link Bitmap} to be used as the image source for
the action button. | |
56 */ | |
57 public static final String KEY_ICON = "android.support.customtabs.customacti
on.ICON"; | |
58 | |
59 /** | |
60 * Key that specifies the content description for the custom action button. | |
61 */ | |
62 public static final String KEY_DESCRIPTION = | |
63 "android.support.customtabs.customaction.DESCRIPTION"; | |
64 | |
65 /** | |
66 * Key that specifies the PendingIntent to launch when the action button or
menu item was | |
67 * clicked. The custom tab will be calling {@link PendingIntent#send()} on c
licks after adding | |
68 * the url as data. The client app can call {@link Intent#getDataString()} t
o get the url. | |
69 */ | |
70 public static final String KEY_PENDING_INTENT = | |
71 "android.support.customtabs.customaction.PENDING_INTENT"; | |
72 | |
73 /** | |
74 * Use an {@code ArrayList<Bundle>} for specifying menu related params. Ther
e should be a | |
75 * separate {@link Bundle} for each custom menu item. | |
76 */ | |
77 public static final String EXTRA_MENU_ITEMS = "android.support.customtabs.ex
tra.MENU_ITEMS"; | |
78 | |
79 /** | |
80 * Key for specifying the title of a menu item. | |
81 */ | |
82 public static final String KEY_MENU_ITEM_TITLE = | |
83 "android.support.customtabs.customaction.MENU_ITEM_TITLE"; | |
84 | |
85 /** | |
86 * Bundle constructed out of {@link ActivityOptions} that will be running wh
en the | |
87 * {@link Activity} that holds the custom tab gets finished. A similar Activ
ityOptions | |
88 * for creation should be constructed and given to the startActivity() call
that | |
89 * launches the custom tab. | |
90 */ | |
91 public static final String EXTRA_EXIT_ANIMATION_BUNDLE = | |
92 "android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE"; | |
93 | |
94 /** | |
95 * Convenience method to create a VIEW intent without a session for the give
n package. | |
96 * @param packageName The package name to set in the intent. | |
97 * @param data The data {@link Uri} to be used in the intent. | |
98 * @return The intent with the given package, data and the right
session extra. | |
99 */ | |
100 public static Intent getViewIntentWithNoSession(String packageName, Uri data
) { | |
101 Intent intent = new Intent(Intent.ACTION_VIEW, data); | |
102 intent.setPackage(packageName); | |
103 Bundle extras = new Bundle(); | |
104 if (!safePutBinder(extras, EXTRA_SESSION, null)) return null; | |
105 intent.putExtras(extras); | |
106 return intent; | |
107 } | |
108 | |
109 /** | |
110 * A convenience method to handle putting an {@link IBinder} inside a {@link
Bundle} for all | |
111 * Android version. | |
112 * @param bundle The bundle to insert the {@link IBinder}. | |
113 * @param key The key to use while putting the {@link IBinder}. | |
114 * @param binder The {@link IBinder} to put. | |
115 * @return Whether the operation was successful. | |
116 */ | |
117 static boolean safePutBinder(Bundle bundle, String key, IBinder binder) { | |
118 try { | |
119 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | |
120 bundle.putBinder(key, binder); | |
121 } else { | |
122 Method putBinderMethod = | |
123 Bundle.class.getMethod("putIBinder", String.class, IBind
er.class); | |
124 putBinderMethod.invoke(bundle, key, binder); | |
125 } | |
126 } catch (InvocationTargetException | IllegalAccessException | IllegalArg
umentException | |
127 | NoSuchMethodException e) { | |
128 return false; | |
129 } | |
130 return true; | |
131 } | |
132 } | |
OLD | NEW |