OLD | NEW |
1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 14 matching lines...) Expand all Loading... |
25 import android.util.Log; | 25 import android.util.Log; |
26 import android.view.View; | 26 import android.view.View; |
27 import android.widget.CheckBox; | 27 import android.widget.CheckBox; |
28 import android.widget.EditText; | 28 import android.widget.EditText; |
29 | 29 |
30 /** | 30 /** |
31 * Opens Chrome Custom Tabs with a customized UI. | 31 * Opens Chrome Custom Tabs with a customized UI. |
32 */ | 32 */ |
33 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL
istener { | 33 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL
istener { |
34 private static final String TAG = "CustChromeTabActivity"; | 34 private static final String TAG = "CustChromeTabActivity"; |
| 35 private static final int TOOLBAR_ITEM_ID = 1; |
35 | 36 |
36 private EditText mUrlEditText; | 37 private EditText mUrlEditText; |
37 private EditText mCustomTabColorEditText; | 38 private EditText mCustomTabColorEditText; |
| 39 private EditText mCustomTabSecondaryColorEditText; |
38 private CheckBox mShowActionButtonCheckbox; | 40 private CheckBox mShowActionButtonCheckbox; |
39 private CheckBox mAddMenusCheckbox; | 41 private CheckBox mAddMenusCheckbox; |
40 private CheckBox mShowTitleCheckBox; | 42 private CheckBox mShowTitleCheckBox; |
41 private CheckBox mCustomBackButtonCheckBox; | 43 private CheckBox mCustomBackButtonCheckBox; |
42 private CheckBox mAutoHideAppBarCheckbox; | 44 private CheckBox mAutoHideAppBarCheckbox; |
| 45 private CheckBox mAddDefaultShareCheckbox; |
| 46 private CheckBox mToolbarItemCheckbox; |
43 private CustomTabActivityHelper mCustomTabActivityHelper; | 47 private CustomTabActivityHelper mCustomTabActivityHelper; |
44 | 48 |
45 @Override | 49 @Override |
46 protected void onCreate(Bundle savedInstanceState) { | 50 protected void onCreate(Bundle savedInstanceState) { |
47 super.onCreate(savedInstanceState); | 51 super.onCreate(savedInstanceState); |
48 setContentView(R.layout.activity_custom_ui); | 52 setContentView(R.layout.activity_custom_ui); |
49 | 53 |
50 mCustomTabActivityHelper = new CustomTabActivityHelper(); | 54 mCustomTabActivityHelper = new CustomTabActivityHelper(); |
51 findViewById(R.id.start_custom_tab).setOnClickListener(this); | 55 findViewById(R.id.start_custom_tab).setOnClickListener(this); |
52 | 56 |
53 mUrlEditText = (EditText) findViewById(R.id.url); | 57 mUrlEditText = (EditText) findViewById(R.id.url); |
54 mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_co
lor); | 58 mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_co
lor); |
| 59 mCustomTabSecondaryColorEditText = |
| 60 (EditText) findViewById(R.id.custom_toolbar_secondary_color); |
55 mShowActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_act
ion_button); | 61 mShowActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_act
ion_button); |
56 mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); | 62 mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); |
57 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); | 63 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); |
58 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but
ton); | 64 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but
ton); |
59 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo
x); | 65 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo
x); |
| 66 mAddDefaultShareCheckbox = (CheckBox) findViewById(R.id.add_default_shar
e); |
| 67 mToolbarItemCheckbox = (CheckBox) findViewById(R.id.add_toolbar_item); |
60 } | 68 } |
61 | 69 |
62 @Override | 70 @Override |
63 protected void onStart() { | 71 protected void onStart() { |
64 super.onStart(); | 72 super.onStart(); |
65 mCustomTabActivityHelper.bindCustomTabsService(this); | 73 mCustomTabActivityHelper.bindCustomTabsService(this); |
66 } | 74 } |
67 | 75 |
68 @Override | 76 @Override |
69 protected void onStop() { | 77 protected void onStop() { |
70 super.onStop(); | 78 super.onStop(); |
71 mCustomTabActivityHelper.unbindCustomTabsService(this); | 79 mCustomTabActivityHelper.unbindCustomTabsService(this); |
72 } | 80 } |
73 | 81 |
74 @Override | 82 @Override |
75 public void onClick(View v) { | 83 public void onClick(View v) { |
76 int viewId = v.getId(); | 84 int viewId = v.getId(); |
77 switch (viewId) { | 85 switch (viewId) { |
78 case R.id.start_custom_tab: | 86 case R.id.start_custom_tab: |
79 openCustomTab(); | 87 openCustomTab(); |
80 break; | 88 break; |
81 default: | 89 default: |
82 //Unknown View Clicked | 90 //Unknown View Clicked |
83 } | 91 } |
84 } | 92 } |
85 | 93 |
| 94 private int getColor(EditText editText) { |
| 95 try { |
| 96 return Color.parseColor(editText.getText().toString()); |
| 97 } catch (NumberFormatException ex) { |
| 98 Log.i(TAG, "Unable to parse Color: " + editText.getText()); |
| 99 return Color.LTGRAY; |
| 100 } |
| 101 } |
| 102 |
86 private void openCustomTab() { | 103 private void openCustomTab() { |
87 String url = mUrlEditText.getText().toString(); | 104 String url = mUrlEditText.getText().toString(); |
88 | 105 |
89 int color = Color.BLUE; | 106 int color = getColor(mCustomTabColorEditText); |
90 try { | 107 int secondaryColor = getColor(mCustomTabSecondaryColorEditText); |
91 color = Color.parseColor(mCustomTabColorEditText.getText().toString(
)); | |
92 } catch (NumberFormatException ex) { | |
93 Log.i(TAG, "Unable to parse Color: " + mCustomTabColorEditText.getTe
xt()); | |
94 } | |
95 | 108 |
96 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); | 109 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); |
97 intentBuilder.setToolbarColor(color); | 110 intentBuilder.setToolbarColor(color); |
| 111 intentBuilder.setSecondaryToolbarColor(secondaryColor); |
98 | 112 |
99 if (mShowActionButtonCheckbox.isChecked()) { | 113 if (mShowActionButtonCheckbox.isChecked()) { |
100 //Generally you do not want to decode bitmaps in the UI thread. | 114 //Generally you do not want to decode bitmaps in the UI thread. Deco
ding it in the |
101 String shareLabel = getString(R.string.label_action_share); | 115 //UI thread to keep the example short. |
| 116 String actionLabel = getString(R.string.label_action); |
102 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 117 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
103 android.R.drawable.ic_menu_share); | 118 android.R.drawable.ic_menu_share); |
104 PendingIntent pendingIntent = createPendingIntent(); | 119 PendingIntent pendingIntent = |
105 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); | 120 createPendingIntent(ActionBroadcastReceiver.ACTION_ACTION_BU
TTON); |
| 121 intentBuilder.setActionButton(icon, actionLabel, pendingIntent); |
106 } | 122 } |
107 | 123 |
108 if (mAddMenusCheckbox.isChecked()) { | 124 if (mAddMenusCheckbox.isChecked()) { |
109 String menuItemTitle = getString(R.string.menu_item_title); | 125 String menuItemTitle = getString(R.string.menu_item_title); |
110 PendingIntent menuItemPendingIntent = createPendingIntent(); | 126 PendingIntent menuItemPendingIntent = |
| 127 createPendingIntent(ActionBroadcastReceiver.ACTION_MENU_ITEM
); |
111 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); | 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. Deco
ding 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, pen
dingIntent); |
| 144 } |
| 145 |
114 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); | 146 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
115 | 147 |
116 if (mAutoHideAppBarCheckbox.isChecked()) { | 148 if (mAutoHideAppBarCheckbox.isChecked()) { |
117 intentBuilder.enableUrlBarHiding(); | 149 intentBuilder.enableUrlBarHiding(); |
118 } | 150 } |
119 | 151 |
120 if (mCustomBackButtonCheckBox.isChecked()) { | 152 if (mCustomBackButtonCheckBox.isChecked()) { |
121 intentBuilder.setCloseButtonIcon( | 153 intentBuilder.setCloseButtonIcon( |
122 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); | 154 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); |
123 } | 155 } |
124 | 156 |
125 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli
de_out_left); | 157 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli
de_out_left); |
126 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, | 158 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, |
127 android.R.anim.slide_out_right); | 159 android.R.anim.slide_out_right); |
128 | 160 |
129 CustomTabActivityHelper.openCustomTab( | 161 CustomTabActivityHelper.openCustomTab( |
130 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback
()); | 162 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback
()); |
131 } | 163 } |
132 | 164 |
133 private PendingIntent createPendingIntent() { | 165 private PendingIntent createPendingIntent(int actionSourceId) { |
134 Intent actionIntent = new Intent( | 166 Intent actionIntent = new Intent( |
135 this.getApplicationContext(), ShareBroadcastReceiver.class); | 167 this.getApplicationContext(), ActionBroadcastReceiver.class); |
136 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte
nt, 0); | 168 actionIntent.putExtra(ActionBroadcastReceiver.KEY_ACTION_SOURCE, actionS
ourceId); |
| 169 return PendingIntent.getBroadcast( |
| 170 getApplicationContext(), actionSourceId, actionIntent, 0); |
137 } | 171 } |
138 } | 172 } |
OLD | NEW |