1
- // Copyright 2015 Google Inc. All Rights Reserved.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
1
+ /*
2
+ * Copyright (C) 2015 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (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
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
14
16
package com .example .android .customtabsbrowser ;
15
17
16
18
import android .app .Activity ;
48
50
/**
49
51
* Configures a custom tab. Extracts all configuration parameters from the activity's intent.
50
52
* Clients need to provide a
51
- * {@link com.example.android.customtabsbrowser.CustomTabController. Callback} to handle different
53
+ * {@link Callback} to handle different
52
54
* configuration parameters.
53
55
* <p/>
54
56
* To properly support a custom tab lifecycle, clients need to:
63
65
* @Override
64
66
* protected void onStart() {
65
67
* super.onStart();
66
- * if (mCustomTabsController.launch ()) {
68
+ * if (mCustomTabController.hasCustomTabIntent ()) {
67
69
* // we launch a custom tab
70
+ * mCustomTabController.launch();
68
71
* } else {
69
72
* // we launch the browser
70
73
* }
@@ -170,22 +173,16 @@ public CustomTabController(Activity activity, Callback callback) {
170
173
171
174
/**
172
175
* Launches the custom tab. Should be called from {@link Activity#onStart()}
173
- *
174
- * @return if we launched a custom tab
175
176
*/
176
- public boolean launch () {
177
- if (!isCustomTab ()) {
178
- return false ;
177
+ public void launch () {
178
+ if (!hasCustomTabIntent ()) {
179
+ return ;
179
180
}
180
181
String url = mActivity .getIntent ().getDataString ();
181
- if (url == null ) {
182
- return false ;
183
- }
184
182
mCallback .setUrl (url );
185
183
updateToolbarColor ();
186
184
updateBackButtonIcon ();
187
185
updateToolbarAction ();
188
- return true ;
189
186
}
190
187
191
188
/**
@@ -220,7 +217,7 @@ public void onTitleChange(String title) {
220
217
* {@link Activity#onCreateOptionsMenu(Menu)}.
221
218
*/
222
219
public void updateMenu (Menu menu ) {
223
- if (!isCustomTab ()) {
220
+ if (!hasCustomTabIntent ()) {
224
221
return ;
225
222
}
226
223
List <Bundle > menuBundles = mActivity .getIntent ()
@@ -245,17 +242,25 @@ public void updateMenu(Menu menu) {
245
242
* {@link Activity#onOptionsItemSelected(MenuItem)}.
246
243
*/
247
244
public boolean onOptionsItemSelected (MenuItem item ) {
248
- switch (item .getItemId ()) {
249
- case android .R .id .home :
250
- finish ();
251
- return true ;
245
+ if (android .R .id .home == item .getItemId ()) {
246
+ mActivity .finish ();
247
+ return true ;
252
248
}
253
249
return false ;
254
250
}
255
251
256
- private boolean isCustomTab () {
252
+ /**
253
+ * Returns true if the activity was started with a valid custom tab intent.
254
+ */
255
+ public boolean hasCustomTabIntent () {
257
256
Bundle extras = mActivity .getIntent ().getExtras ();
258
- return extras != null && extras .containsKey (EXTRA_SESSION );
257
+ if (extras == null ) {
258
+ return false ;
259
+ }
260
+ if (!extras .containsKey (EXTRA_SESSION )) {
261
+ return false ;
262
+ }
263
+ return mActivity .getIntent ().getDataString () != null ;
259
264
}
260
265
261
266
private void updateToolbarAction () {
0 commit comments