This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 636
/
CustomTabsIntent.java
297 lines (265 loc) · 11.1 KB
/
CustomTabsIntent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (C) 2015 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;
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.BundleCompat;
import java.util.ArrayList;
/**
* Class holding the {@link Intent} and start bundle for a Custom Tabs Activity.
*
* <p>
* <strong>Note:</strong> The constants below are public for the browser implementation's benefit.
* You are strongly encouraged to use {@link CustomTabsIntent.Builder}.</p>
*/
public final class CustomTabsIntent {
/**
* Extra used to match the session. This has to be included in the intent to open in
* a custom tab. This is the same IBinder that gets passed to ICustomTabsService#newSession.
* Null if there is no need to match any service side sessions with the intent.
*/
public static final String EXTRA_SESSION = "android.support.customtabs.extra.SESSION";
/**
* Extra that changes the background color for the toolbar. colorRes is an int that specifies a
* {@link Color}, not a resource id.
*/
public static final String EXTRA_TOOLBAR_COLOR =
"android.support.customtabs.extra.TOOLBAR_COLOR";
/**
* Extra bitmap that specifies the icon of the back button on the toolbar. If the client chooses
* not to customize it, a default close button will be used.
*/
public static final String EXTRA_CLOSE_BUTTON_ICON =
"android.support.customtabs.extra.CLOSE_BUTTON_ICON";
/**
* Extra (int) that specifies state for showing the page title. Default is {@link #NO_TITLE}.
*/
public static final String EXTRA_TITLE_VISIBILITY_STATE =
"android.support.customtabs.extra.TITLE_VISIBILITY";
/**
* Don't show any title. Shows only the domain.
*/
public static final int NO_TITLE = 0;
/**
* Shows the page title and the domain.
*/
public static final int SHOW_PAGE_TITLE = 1;
/**
* Bundle used for adding a custom action button to the custom tab toolbar. The client should
* provide a description, an icon {@link Bitmap} and a {@link PendingIntent} for the button.
* All three keys must be present.
*/
public static final String EXTRA_ACTION_BUTTON_BUNDLE =
"android.support.customtabs.extra.ACTION_BUTTON_BUNDLE";
/**
* Key that specifies the {@link Bitmap} to be used as the image source for the action button.
* The icon should't be more than 24dp in height (No padding needed. The button itself will be
* 48dp in height) and have a width/height ratio of less than 2.
*/
public static final String KEY_ICON = "android.support.customtabs.customaction.ICON";
/**
* Key that specifies the content description for the custom action button.
*/
public static final String KEY_DESCRIPTION =
"android.support.customtabs.customaction.DESCRIPTION";
/**
* Key that specifies the PendingIntent to launch when the action button or menu item was
* clicked. The custom tab will be calling {@link PendingIntent#send()} on clicks after adding
* the url as data. The client app can call {@link Intent#getDataString()} to get the url.
*/
public static final String KEY_PENDING_INTENT =
"android.support.customtabs.customaction.PENDING_INTENT";
/**
* Use an {@code ArrayList<Bundle>} for specifying menu related params. There should be a
* separate {@link Bundle} for each custom menu item.
*/
public static final String EXTRA_MENU_ITEMS = "android.support.customtabs.extra.MENU_ITEMS";
/**
* Key for specifying the title of a menu item.
*/
public static final String KEY_MENU_ITEM_TITLE =
"android.support.customtabs.customaction.MENU_ITEM_TITLE";
/**
* Bundle constructed out of {@link ActivityOptions} that will be running when the
* {@link Activity} that holds the custom tab gets finished. A similar ActivityOptions
* for creation should be constructed and given to the startActivity() call that
* launches the custom tab.
*/
public static final String EXTRA_EXIT_ANIMATION_BUNDLE =
"android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE";
/**
* An {@link Intent} used to start the Custom Tabs Activity.
*/
@NonNull public final Intent intent;
/**
* A {@link Bundle} containing the start animation for the Custom Tabs Activity.
*/
@Nullable public final Bundle startAnimationBundle;
/**
* Convenience method to launch a Custom Tabs Activity.
* @param context The source Activity.
* @param url The URL to load in the Custom Tab.
*/
public void launchUrl(Activity context, Uri url) {
intent.setData(url);
if (startAnimationBundle != null){
context.startActivity(intent, startAnimationBundle);
} else {
context.startActivity(intent);
}
}
private CustomTabsIntent(Intent intent, Bundle startAnimationBundle) {
this.intent = intent;
this.startAnimationBundle = startAnimationBundle;
}
/**
* Builder class for {@link CustomTabsIntent} objects.
*/
public static final class Builder {
private final Intent mIntent = new Intent(Intent.ACTION_VIEW);
private ArrayList<Bundle> mMenuItems = null;
private Bundle mStartAnimationBundle = null;
/**
* Creates a {@link CustomTabsIntent.Builder} object associated with no
* {@link CustomTabsSession}.
*/
public Builder() {
this(null);
}
/**
* Creates a {@link CustomTabsIntent.Builder} object associated with a given
* {@link CustomTabsSession}.
*
* Guarantees that the {@link Intent} will be sent to the same component as the one the
* session is associated with.
*
* @param session The session to associate this Builder with.
*/
public Builder(@Nullable CustomTabsSession session) {
if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
Bundle bundle = new Bundle();
BundleCompat.putBinder(
bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
mIntent.putExtras(bundle);
}
/**
* Sets the toolbar color.
*
* @param color {@link Color}
*/
public Builder setToolbarColor(@ColorInt int color) {
mIntent.putExtra(EXTRA_TOOLBAR_COLOR, color);
return this;
}
/**
* Sets the Close button icon for the custom tab.
*
* @param icon The icon {@link Bitmap}
*/
public Builder setCloseButtonIcon(@NonNull Bitmap icon) {
mIntent.putExtra(EXTRA_CLOSE_BUTTON_ICON, icon);
return this;
}
/**
* Sets whether the title should be shown in the custom tab.
*
* @param showTitle Whether the title should be shown.
*/
public Builder setShowTitle(boolean showTitle) {
mIntent.putExtra(EXTRA_TITLE_VISIBILITY_STATE,
showTitle ? SHOW_PAGE_TITLE : NO_TITLE);
return this;
}
/**
* Adds a menu item.
*
* @param label Menu label.
* @param pendingIntent Pending intent delivered when the menu item is clicked.
*/
public Builder addMenuItem(@NonNull String label, @NonNull PendingIntent pendingIntent) {
if (mMenuItems == null) mMenuItems = new ArrayList<>();
Bundle bundle = new Bundle();
bundle.putString(KEY_MENU_ITEM_TITLE, label);
bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent);
mMenuItems.add(bundle);
return this;
}
/**
* Set the action button.
*
* @param icon The icon.
* @param description The description for the button. To be used for accessibility.
* @param pendingIntent pending intent delivered when the button is clicked.
*/
public Builder setActionButton(@NonNull Bitmap icon,
@NonNull String description, @NonNull PendingIntent pendingIntent) {
Bundle bundle = new Bundle();
bundle.putParcelable(KEY_ICON, icon);
bundle.putString(KEY_DESCRIPTION, description);
bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent);
mIntent.putExtra(EXTRA_ACTION_BUTTON_BUNDLE, bundle);
return this;
}
/**
* Sets the start animations,
*
* @param context Application context.
* @param enterResId Resource ID of the "enter" animation for the browser.
* @param exitResId Resource ID of the "exit" animation for the application.
*/
public Builder setStartAnimations(
@NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) {
mStartAnimationBundle =
ActivityOptions.makeCustomAnimation(context, enterResId, exitResId).toBundle();
return this;
}
/**
* Sets the exit animations,
*
* @param context Application context.
* @param enterResId Resource ID of the "enter" animation for the application.
* @param exitResId Resource ID of the "exit" animation for the browser.
*/
public Builder setExitAnimations(
@NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) {
Bundle bundle =
ActivityOptions.makeCustomAnimation(context, enterResId, exitResId).toBundle();
mIntent.putExtra(EXTRA_EXIT_ANIMATION_BUNDLE, bundle);
return this;
}
/**
* Combines all the options that have been set and returns a new {@link CustomTabsIntent}
* object.
*/
public CustomTabsIntent build() {
if (mMenuItems != null) {
mIntent.putParcelableArrayListExtra(CustomTabsIntent.EXTRA_MENU_ITEMS, mMenuItems);
}
return new CustomTabsIntent(mIntent, mStartAnimationBundle);
}
}
}