Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 30e1657

Browse files
andrebanCommit bot
authored and
Commit bot
committed
Update demo with features from support lib 23.2.0
- Added demo for default share menu action - Added demo secondary toolbar color change - Added demo toolbar button BUG= Review URL: https://codereview.chromium.org/1750963002
1 parent 8ae46d2 commit 30e1657

11 files changed

+301
-185
lines changed

demos/build.gradle

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.1"
5+
buildToolsVersion "23.0.2"
66

77
defaultConfig {
88
applicationId "org.chromium.customtabsdemos"
9-
minSdkVersion 16
9+
minSdkVersion 15
1010
targetSdkVersion 23
1111
versionCode 1
1212
versionName "1.0"
@@ -21,8 +21,8 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:appcompat-v7:23.1.1'
25-
compile 'com.android.support:recyclerview-v7:23.1.1'
26-
compile 'com.android.support:customtabs:23.1.1'
24+
compile 'com.android.support:appcompat-v7:23.2.0'
25+
compile 'com.android.support:recyclerview-v7:23.2.0'
26+
compile 'com.android.support:customtabs:23.2.0'
2727
compile project(':shared')
2828
}

demos/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
android:name="android.support.PARENT_ACTIVITY"
7272
android:value=".DemoListActivity" />
7373
</activity>
74-
<receiver android:name=".ShareBroadcastReceiver" />
74+
<receiver android:name=".ActionBroadcastReceiver" />
7575
</application>
7676

7777
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.
14+
15+
package org.chromium.customtabsdemos;
16+
17+
import android.content.BroadcastReceiver;
18+
import android.content.Context;
19+
import android.content.Intent;
20+
import android.widget.Toast;
21+
22+
/**
23+
* A BroadcastReceiver that handles the Action Intent from the Custom Tab and shows the Url
24+
* in a Toast.
25+
*/
26+
public class ActionBroadcastReceiver extends BroadcastReceiver {
27+
public static final String KEY_ACTION_SOURCE = "org.chromium.customtabsdemos.ACTION_SOURCE";
28+
public static final int ACTION_ACTION_BUTTON = 1;
29+
public static final int ACTION_MENU_ITEM = 2;
30+
public static final int ACTION_TOOLBAR = 3;
31+
32+
@Override
33+
public void onReceive(Context context, Intent intent) {
34+
String url = intent.getDataString();
35+
if (url != null) {
36+
String toastText =
37+
getToastText(context, intent.getIntExtra(KEY_ACTION_SOURCE, -1), url);
38+
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show();
39+
}
40+
}
41+
42+
private String getToastText(Context context, int actionId, String url) {
43+
switch (actionId) {
44+
case ACTION_ACTION_BUTTON:
45+
return context.getString(R.string.action_button_toast_text, url);
46+
case ACTION_MENU_ITEM:
47+
return context.getString(R.string.menu_item_toast_text, url);
48+
case ACTION_TOOLBAR:
49+
return context.getString(R.string.toolbar_toast_text, url);
50+
default:
51+
return context.getString(R.string.unknown_toast_text, url);
52+
}
53+
}
54+
}

demos/src/main/java/org/chromium/customtabsdemos/CustomTabActivityHelper.java

-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
package org.chromium.customtabsdemos;
1616

1717
import android.app.Activity;
18-
import android.content.ComponentName;
1918
import android.net.Uri;
2019
import android.os.Bundle;
21-
import android.support.annotation.NonNull;
2220
import android.support.customtabs.CustomTabsClient;
2321
import android.support.customtabs.CustomTabsIntent;
2422
import android.support.customtabs.CustomTabsServiceConnection;
@@ -28,7 +26,6 @@
2826
import org.chromium.customtabsclient.shared.ServiceConnection;
2927
import org.chromium.customtabsclient.shared.ServiceConnectionCallback;
3028

31-
import java.lang.ref.WeakReference;
3229
import java.util.List;
3330

3431
/**

demos/src/main/java/org/chromium/customtabsdemos/CustomUIActivity.java

+48-14
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@
3232
*/
3333
public class CustomUIActivity extends AppCompatActivity implements View.OnClickListener {
3434
private static final String TAG = "CustChromeTabActivity";
35+
private static final int TOOLBAR_ITEM_ID = 1;
3536

3637
private EditText mUrlEditText;
3738
private EditText mCustomTabColorEditText;
39+
private EditText mCustomTabSecondaryColorEditText;
3840
private CheckBox mShowActionButtonCheckbox;
3941
private CheckBox mAddMenusCheckbox;
4042
private CheckBox mShowTitleCheckBox;
4143
private CheckBox mCustomBackButtonCheckBox;
4244
private CheckBox mAutoHideAppBarCheckbox;
45+
private CheckBox mAddDefaultShareCheckbox;
46+
private CheckBox mToolbarItemCheckbox;
4347
private CustomTabActivityHelper mCustomTabActivityHelper;
4448

4549
@Override
@@ -52,11 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {
5256

5357
mUrlEditText = (EditText) findViewById(R.id.url);
5458
mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_color);
59+
mCustomTabSecondaryColorEditText =
60+
(EditText) findViewById(R.id.custom_toolbar_secondary_color);
5561
mShowActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_action_button);
5662
mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus);
5763
mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title);
5864
mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_button);
5965
mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbox);
66+
mAddDefaultShareCheckbox = (CheckBox) findViewById(R.id.add_default_share);
67+
mToolbarItemCheckbox = (CheckBox) findViewById(R.id.add_toolbar_item);
6068
}
6169

6270
@Override
@@ -83,34 +91,58 @@ public void onClick(View v) {
8391
}
8492
}
8593

86-
private void openCustomTab() {
87-
String url = mUrlEditText.getText().toString();
88-
89-
int color = Color.BLUE;
94+
private int getColor(EditText editText) {
9095
try {
91-
color = Color.parseColor(mCustomTabColorEditText.getText().toString());
96+
return Color.parseColor(editText.getText().toString());
9297
} catch (NumberFormatException ex) {
93-
Log.i(TAG, "Unable to parse Color: " + mCustomTabColorEditText.getText());
98+
Log.i(TAG, "Unable to parse Color: " + editText.getText());
99+
return Color.LTGRAY;
94100
}
101+
}
102+
103+
private void openCustomTab() {
104+
String url = mUrlEditText.getText().toString();
105+
106+
int color = getColor(mCustomTabColorEditText);
107+
int secondaryColor = getColor(mCustomTabSecondaryColorEditText);
95108

96109
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
97110
intentBuilder.setToolbarColor(color);
111+
intentBuilder.setSecondaryToolbarColor(secondaryColor);
98112

99113
if (mShowActionButtonCheckbox.isChecked()) {
100-
//Generally you do not want to decode bitmaps in the UI thread.
101-
String shareLabel = getString(R.string.label_action_share);
114+
//Generally you do not want to decode bitmaps in the UI thread. Decoding it in the
115+
//UI thread to keep the example short.
116+
String actionLabel = getString(R.string.label_action);
102117
Bitmap icon = BitmapFactory.decodeResource(getResources(),
103118
android.R.drawable.ic_menu_share);
104-
PendingIntent pendingIntent = createPendingIntent();
105-
intentBuilder.setActionButton(icon, shareLabel, pendingIntent);
119+
PendingIntent pendingIntent =
120+
createPendingIntent(ActionBroadcastReceiver.ACTION_ACTION_BUTTON);
121+
intentBuilder.setActionButton(icon, actionLabel, pendingIntent);
106122
}
107123

108124
if (mAddMenusCheckbox.isChecked()) {
109125
String menuItemTitle = getString(R.string.menu_item_title);
110-
PendingIntent menuItemPendingIntent = createPendingIntent();
126+
PendingIntent menuItemPendingIntent =
127+
createPendingIntent(ActionBroadcastReceiver.ACTION_MENU_ITEM);
111128
intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent);
112129
}
113130

131+
if (mAddDefaultShareCheckbox.isChecked()) {
132+
intentBuilder.addDefaultShareMenuItem();
133+
}
134+
135+
if (mToolbarItemCheckbox.isChecked()) {
136+
//Generally you do not want to decode bitmaps in the UI thread. Decoding it in the
137+
//UI thread to keep the example short.
138+
String actionLabel = getString(R.string.label_action);
139+
Bitmap icon = BitmapFactory.decodeResource(getResources(),
140+
android.R.drawable.ic_menu_share);
141+
PendingIntent pendingIntent =
142+
createPendingIntent(ActionBroadcastReceiver.ACTION_TOOLBAR);
143+
intentBuilder.addToolbarItem(TOOLBAR_ITEM_ID, icon, actionLabel, pendingIntent);
144+
}
145+
114146
intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked());
115147

116148
if (mAutoHideAppBarCheckbox.isChecked()) {
@@ -130,9 +162,11 @@ private void openCustomTab() {
130162
this, intentBuilder.build(), Uri.parse(url), new WebviewFallback());
131163
}
132164

133-
private PendingIntent createPendingIntent() {
165+
private PendingIntent createPendingIntent(int actionSourceId) {
134166
Intent actionIntent = new Intent(
135-
this.getApplicationContext(), ShareBroadcastReceiver.class);
136-
return PendingIntent.getBroadcast(getApplicationContext(), 0, actionIntent, 0);
167+
this.getApplicationContext(), ActionBroadcastReceiver.class);
168+
actionIntent.putExtra(ActionBroadcastReceiver.KEY_ACTION_SOURCE, actionSourceId);
169+
return PendingIntent.getBroadcast(
170+
getApplicationContext(), actionSourceId, actionIntent, 0);
137171
}
138172
}

demos/src/main/java/org/chromium/customtabsdemos/ShareBroadcastReceiver.java

-41
This file was deleted.

0 commit comments

Comments
 (0)