Skip to content
This repository was archived by the owner on Apr 27, 2021. It is now read-only.

Commit 9b1b6b6

Browse files
committed
explicitly check for custom tab intent
Change-Id: I7666c78d3acb209d0e4c01baef8f26eacddf25ff
1 parent 494a92e commit 9b1b6b6

File tree

2 files changed

+63
-54
lines changed

2 files changed

+63
-54
lines changed

app/src/main/java/com/example/android/customtabsbrowser/CustomTabController.java

+36-31
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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+
*/
1416
package com.example.android.customtabsbrowser;
1517

1618
import android.app.Activity;
@@ -48,7 +50,7 @@
4850
/**
4951
* Configures a custom tab. Extracts all configuration parameters from the activity's intent.
5052
* Clients need to provide a
51-
* {@link com.example.android.customtabsbrowser.CustomTabController.Callback} to handle different
53+
* {@link Callback} to handle different
5254
* configuration parameters.
5355
* <p/>
5456
* To properly support a custom tab lifecycle, clients need to:
@@ -63,8 +65,9 @@
6365
* @Override
6466
* protected void onStart() {
6567
* super.onStart();
66-
* if (mCustomTabsController.launch()) {
68+
* if (mCustomTabController.hasCustomTabIntent()) {
6769
* // we launch a custom tab
70+
* mCustomTabController.launch();
6871
* } else {
6972
* // we launch the browser
7073
* }
@@ -170,22 +173,16 @@ public CustomTabController(Activity activity, Callback callback) {
170173

171174
/**
172175
* Launches the custom tab. Should be called from {@link Activity#onStart()}
173-
*
174-
* @return if we launched a custom tab
175176
*/
176-
public boolean launch() {
177-
if (!isCustomTab()) {
178-
return false;
177+
public void launch() {
178+
if (!hasCustomTabIntent()) {
179+
return;
179180
}
180181
String url = mActivity.getIntent().getDataString();
181-
if (url == null) {
182-
return false;
183-
}
184182
mCallback.setUrl(url);
185183
updateToolbarColor();
186184
updateBackButtonIcon();
187185
updateToolbarAction();
188-
return true;
189186
}
190187

191188
/**
@@ -220,7 +217,7 @@ public void onTitleChange(String title) {
220217
* {@link Activity#onCreateOptionsMenu(Menu)}.
221218
*/
222219
public void updateMenu(Menu menu) {
223-
if (!isCustomTab()) {
220+
if (!hasCustomTabIntent()) {
224221
return;
225222
}
226223
List<Bundle> menuBundles = mActivity.getIntent()
@@ -245,17 +242,25 @@ public void updateMenu(Menu menu) {
245242
* {@link Activity#onOptionsItemSelected(MenuItem)}.
246243
*/
247244
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;
252248
}
253249
return false;
254250
}
255251

256-
private boolean isCustomTab() {
252+
/**
253+
* Returns true if the activity was started with a valid custom tab intent.
254+
*/
255+
public boolean hasCustomTabIntent() {
257256
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;
259264
}
260265

261266
private void updateToolbarAction() {

app/src/main/java/com/example/android/customtabsbrowser/MainActivity.java

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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+
*/
1416
package com.example.android.customtabsbrowser;
1517

1618
import android.content.Intent;
@@ -30,6 +32,9 @@
3032
import android.widget.TextView;
3133
import android.widget.Toast;
3234

35+
/**
36+
* This activity implements a custom tab provider using a Webview.
37+
*/
3338
public class MainActivity extends AppCompatActivity {
3439

3540
private static final String TAG = "MainActivity";
@@ -74,13 +79,10 @@ public void onReceivedTitle(WebView view, String title) {
7479
@Override
7580
protected void onStart() {
7681
super.onStart();
77-
if (mCustomTabController.launch()) {
78-
return;
82+
if (mCustomTabController.hasCustomTabIntent()) {
83+
mCustomTabController.launch();
7984
} else {
80-
Toast.makeText(this,
81-
"This is not the Custom Tab you are looking for.",
82-
Toast.LENGTH_LONG)
83-
.show();
85+
Toast.makeText(this, R.string.error_no_custom_tab, Toast.LENGTH_LONG).show();
8486
}
8587
}
8688

@@ -102,14 +104,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
102104
if (mCustomTabController.onOptionsItemSelected(item)) {
103105
return true;
104106
}
105-
switch (item.getItemId()) {
106-
case R.id.action_open_in_browser:
107-
startActivity(new Intent(Intent.ACTION_VIEW, getIntent().getData()));
108-
return true;
107+
if (R.id.action_open_in_browser == item.getItemId()) {
108+
startActivity(new Intent(Intent.ACTION_VIEW, getIntent().getData()));
109+
return true;
109110
}
110111
return super.onOptionsItemSelected(item);
111112
}
112113

114+
/**
115+
* Configures our webview-based browser tab based on the custom tab intent.
116+
*/
113117
private class CustomTabControllerCallback implements CustomTabController.Callback {
114118
@Override
115119
public void setTitle(String title) {

0 commit comments

Comments
 (0)