32
32
*/
33
33
public class CustomUIActivity extends AppCompatActivity implements View .OnClickListener {
34
34
private static final String TAG = "CustChromeTabActivity" ;
35
+ private static final int TOOLBAR_ITEM_ID = 1 ;
35
36
36
37
private EditText mUrlEditText ;
37
38
private EditText mCustomTabColorEditText ;
39
+ private EditText mCustomTabSecondaryColorEditText ;
38
40
private CheckBox mShowActionButtonCheckbox ;
39
41
private CheckBox mAddMenusCheckbox ;
40
42
private CheckBox mShowTitleCheckBox ;
41
43
private CheckBox mCustomBackButtonCheckBox ;
42
44
private CheckBox mAutoHideAppBarCheckbox ;
45
+ private CheckBox mAddDefaultShareCheckbox ;
46
+ private CheckBox mToolbarItemCheckbox ;
43
47
private CustomTabActivityHelper mCustomTabActivityHelper ;
44
48
45
49
@ Override
@@ -52,11 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {
52
56
53
57
mUrlEditText = (EditText ) findViewById (R .id .url );
54
58
mCustomTabColorEditText = (EditText ) findViewById (R .id .custom_toolbar_color );
59
+ mCustomTabSecondaryColorEditText =
60
+ (EditText ) findViewById (R .id .custom_toolbar_secondary_color );
55
61
mShowActionButtonCheckbox = (CheckBox ) findViewById (R .id .custom_show_action_button );
56
62
mAddMenusCheckbox = (CheckBox ) findViewById (R .id .custom_add_menus );
57
63
mShowTitleCheckBox = (CheckBox ) findViewById (R .id .show_title );
58
64
mCustomBackButtonCheckBox = (CheckBox ) findViewById (R .id .custom_back_button );
59
65
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 );
60
68
}
61
69
62
70
@ Override
@@ -83,34 +91,58 @@ public void onClick(View v) {
83
91
}
84
92
}
85
93
86
- private void openCustomTab () {
87
- String url = mUrlEditText .getText ().toString ();
88
-
89
- int color = Color .BLUE ;
94
+ private int getColor (EditText editText ) {
90
95
try {
91
- color = Color .parseColor (mCustomTabColorEditText .getText ().toString ());
96
+ return Color .parseColor (editText .getText ().toString ());
92
97
} 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 ;
94
100
}
101
+ }
102
+
103
+ private void openCustomTab () {
104
+ String url = mUrlEditText .getText ().toString ();
105
+
106
+ int color = getColor (mCustomTabColorEditText );
107
+ int secondaryColor = getColor (mCustomTabSecondaryColorEditText );
95
108
96
109
CustomTabsIntent .Builder intentBuilder = new CustomTabsIntent .Builder ();
97
110
intentBuilder .setToolbarColor (color );
111
+ intentBuilder .setSecondaryToolbarColor (secondaryColor );
98
112
99
113
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 );
102
117
Bitmap icon = BitmapFactory .decodeResource (getResources (),
103
118
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 );
106
122
}
107
123
108
124
if (mAddMenusCheckbox .isChecked ()) {
109
125
String menuItemTitle = getString (R .string .menu_item_title );
110
- PendingIntent menuItemPendingIntent = createPendingIntent ();
126
+ PendingIntent menuItemPendingIntent =
127
+ createPendingIntent (ActionBroadcastReceiver .ACTION_MENU_ITEM );
111
128
intentBuilder .addMenuItem (menuItemTitle , menuItemPendingIntent );
112
129
}
113
130
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
+
114
146
intentBuilder .setShowTitle (mShowTitleCheckBox .isChecked ());
115
147
116
148
if (mAutoHideAppBarCheckbox .isChecked ()) {
@@ -130,9 +162,11 @@ private void openCustomTab() {
130
162
this , intentBuilder .build (), Uri .parse (url ), new WebviewFallback ());
131
163
}
132
164
133
- private PendingIntent createPendingIntent () {
165
+ private PendingIntent createPendingIntent (int actionSourceId ) {
134
166
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 );
137
171
}
138
172
}
0 commit comments