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, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 package org.chromium.customtabsdemos; | 14 package org.chromium.customtabsdemos; |
15 | 15 |
16 import android.app.PendingIntent; | 16 import android.app.PendingIntent; |
17 import android.content.Intent; | 17 import android.content.Intent; |
18 import android.graphics.Bitmap; | 18 import android.graphics.Bitmap; |
19 import android.graphics.BitmapFactory; | 19 import android.graphics.BitmapFactory; |
20 import android.graphics.Color; | |
21 import android.net.Uri; | 20 import android.net.Uri; |
22 import android.os.Bundle; | 21 import android.os.Bundle; |
22 import android.provider.CalendarContract; | |
23 import android.provider.ContactsContract; | |
23 import android.support.customtabs.CustomTabsIntent; | 24 import android.support.customtabs.CustomTabsIntent; |
24 import android.support.v7.app.AppCompatActivity; | 25 import android.support.v7.app.AppCompatActivity; |
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 import org.chromium.customtabsclient.shared.CustomTabsHelper; | |
31 | |
32 import java.util.ArrayList; | |
33 | |
30 /** | 34 /** |
31 * Opens Chrome Custom Tabs with a customized UI. | 35 * Opens Chrome Custom Tabs with a customized UI. |
32 */ | 36 */ |
33 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL istener { | 37 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL istener { |
34 private static final String TAG = "CustChromeTabActivity"; | 38 private String mToolbarColor; |
35 | 39 private String mPackageName; |
40 private ArrayList<ActionButtonParams> mActionBarItems; | |
36 private EditText mUrlEditText; | 41 private EditText mUrlEditText; |
37 private EditText mCustomTabColorEditText; | 42 private CheckBox mAddActionButtonCheckbox; |
38 private CheckBox mShowActionButtonCheckbox; | |
39 private CheckBox mAddMenusCheckbox; | 43 private CheckBox mAddMenusCheckbox; |
40 private CheckBox mShowTitleCheckBox; | 44 private CheckBox mShowTitleCheckBox; |
41 private CheckBox mCustomBackButtonCheckBox; | 45 private CheckBox mCustomBackButtonCheckBox; |
42 private CheckBox mAutoHideAppBarCheckbox; | 46 private CheckBox mAutoHideAppBarCheckbox; |
43 private CustomTabActivityHelper mCustomTabActivityHelper; | 47 private CustomTabActivityHelper mCustomTabActivityHelper; |
44 | 48 |
49 private int advancedUISettintRequestCode; | |
Ian Wen
2016/03/16 01:35:30
Member variables should start with an "m". Also th
BigBossZhiling
2016/03/18 00:59:07
Done.
| |
45 @Override | 50 @Override |
46 protected void onCreate(Bundle savedInstanceState) { | 51 protected void onCreate(Bundle savedInstanceState) { |
47 super.onCreate(savedInstanceState); | 52 super.onCreate(savedInstanceState); |
48 setContentView(R.layout.activity_custom_ui); | 53 setContentView(R.layout.activity_custom_ui); |
49 | |
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 findViewById(R.id.advanced_ui_setting).setOnClickListener(this); |
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 mAddActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_acti on_button); |
55 mShowActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_act ion_button); | |
56 mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); | 59 mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); |
57 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); | 60 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); |
58 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but ton); | 61 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but ton); |
59 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo x); | 62 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo x); |
63 advancedUISettintRequestCode = 999; | |
64 makeDefaultSetting(); | |
60 } | 65 } |
61 | 66 |
62 @Override | 67 @Override |
63 protected void onStart() { | 68 protected void onStart() { |
64 super.onStart(); | 69 super.onStart(); |
65 mCustomTabActivityHelper.bindCustomTabsService(this); | 70 mCustomTabActivityHelper.bindCustomTabsService(this); |
66 } | 71 } |
67 | 72 |
68 @Override | 73 @Override |
69 protected void onStop() { | 74 protected void onStop() { |
70 super.onStop(); | 75 super.onStop(); |
71 mCustomTabActivityHelper.unbindCustomTabsService(this); | 76 mCustomTabActivityHelper.unbindCustomTabsService(this); |
72 } | 77 } |
73 | 78 |
74 @Override | 79 @Override |
80 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
81 super.onActivityResult(requestCode, resultCode, data); | |
82 if ((requestCode == advancedUISettintRequestCode)) { | |
83 try { | |
84 mActionBarItems = (ArrayList) data.getSerializableExtra | |
85 (AdvancedUISettingActivity.KEY_ACTION_ITEMS); | |
86 mToolbarColor = (String) data.getSerializableExtra | |
Ian Wen
2016/03/16 01:35:30
Why is it SerializableExtra?
You should use getSt
BigBossZhiling
2016/03/18 00:59:07
Done.
| |
87 (AdvancedUISettingActivity.KEY_COLOR); | |
88 mPackageName = (String) data.getSerializableExtra | |
89 (AdvancedUISettingActivity.KEY_PACKAGE); | |
90 } catch (Exception e) { | |
91 e.printStackTrace(); | |
92 } | |
93 } | |
94 } | |
95 | |
96 @Override | |
75 public void onClick(View v) { | 97 public void onClick(View v) { |
76 int viewId = v.getId(); | 98 int viewId = v.getId(); |
77 switch (viewId) { | 99 switch (viewId) { |
78 case R.id.start_custom_tab: | 100 case R.id.start_custom_tab: |
79 openCustomTab(); | 101 openCustomTab(); |
80 break; | 102 break; |
103 case R.id.advanced_ui_setting: | |
104 openUISetting(); | |
105 break; | |
81 default: | 106 default: |
82 //Unknown View Clicked | 107 //Unknown View Clicked |
83 } | 108 } |
84 } | 109 } |
85 | 110 |
111 private void makeDefaultSetting() { | |
112 mToolbarColor = "Red"; | |
113 mPackageName = CustomTabsHelper.getPackageNameToUse(this, | |
114 CustomTabsHelper.getAllPackagesSupportingCustomTabs(this)); | |
115 mActionBarItems = new ArrayList<ActionButtonParams>(); | |
116 } | |
117 | |
118 private void openUISetting() { | |
119 Intent intent = new Intent(this, AdvancedUISettingActivity.class); | |
120 intent.putExtra(AdvancedUISettingActivity.KEY_PACKAGE, mPackageName); | |
121 intent.putExtra(AdvancedUISettingActivity.KEY_COLOR, mToolbarColor); | |
122 intent.putExtra(AdvancedUISettingActivity.KEY_ACTION_ITEMS, mActionBarIt ems); | |
123 startActivityForResult(intent, advancedUISettintRequestCode); | |
124 } | |
125 | |
126 private void addActionButtons(CustomTabsIntent.Builder intentBuilder) { | |
127 for (int i = 0; i < mActionBarItems.size(); i++) { | |
128 intentBuilder.addActionBarItem(i, AdvancedUISettingActivity.uriToBit Map(this, | |
129 Uri.parse(mActionBarItems.get(i).image)), | |
130 mActionBarItems.get(i).description, | |
131 createPendingIntent(mActionBarItems.get(i).intent)); | |
132 } | |
133 } | |
134 | |
135 private PendingIntent createPendingIntent(String intent) { | |
136 switch (intent) { | |
137 case "Send": | |
138 Intent sendIntent = new Intent(); | |
139 sendIntent.setAction(Intent.ACTION_SEND); | |
140 sendIntent.setType("text/plain"); | |
141 return PendingIntent.getActivity(this, 0, sendIntent, 0); | |
142 case "Select Contact": | |
143 Intent contact = new Intent(Intent.ACTION_PICK); | |
144 contact.setType(ContactsContract.Contacts.CONTENT_TYPE); | |
145 return PendingIntent.getActivity(this, 0, contact, 0); | |
146 case "Add Event": | |
147 Intent event = new Intent(Intent.ACTION_INSERT) | |
148 .setData(CalendarContract.Events.CONTENT_URI) | |
149 .putExtra(CalendarContract.Events.TITLE, "Great Event") | |
150 .putExtra(CalendarContract.Events.EVENT_LOCATION, "Googl eplex 43") | |
151 .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, "toda y") | |
152 .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, "tomorr ow"); | |
153 return PendingIntent.getActivity(this, 0, event, 0); | |
154 case "Camera": | |
155 return PendingIntent.getActivity(this, 0, new Intent(this, Camer a.class), | |
156 PendingIntent.FLAG_UPDATE_CURRENT); | |
157 default: | |
158 return null; | |
159 } | |
160 } | |
161 | |
86 private void openCustomTab() { | 162 private void openCustomTab() { |
87 String url = mUrlEditText.getText().toString(); | 163 String url = mUrlEditText.getText().toString(); |
88 | 164 |
89 int color = Color.BLUE; | 165 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); |
90 try { | |
91 color = Color.parseColor(mCustomTabColorEditText.getText().toString( )); | |
92 } catch (NumberFormatException ex) { | |
93 Log.i(TAG, "Unable to parse Color: " + mCustomTabColorEditText.getTe xt()); | |
94 } | |
95 | 166 |
96 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); | 167 intentBuilder.setToolbarColor(AdvancedUISettingActivity.stringToColor(mT oolbarColor)); |
97 intentBuilder.setToolbarColor(color); | 168 intentBuilder.addDefaultShareMenuItem(); |
98 | 169 |
99 if (mShowActionButtonCheckbox.isChecked()) { | 170 if (mAddActionButtonCheckbox.isChecked()) { |
100 //Generally you do not want to decode bitmaps in the UI thread. | 171 //Generally you do not want to decode bitmaps in the UI thread. |
101 String shareLabel = getString(R.string.label_action_share); | 172 String shareLabel = getString(R.string.label_action_share); |
102 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 173 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
103 android.R.drawable.ic_menu_share); | 174 android.R.drawable.ic_menu_share); |
104 PendingIntent pendingIntent = createPendingIntent(); | 175 PendingIntent pendingIntent = createPendingIntent(); |
105 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); | 176 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); |
106 } | 177 } |
107 | 178 |
108 if (mAddMenusCheckbox.isChecked()) { | 179 if (mAddMenusCheckbox.isChecked()) { |
109 String menuItemTitle = getString(R.string.menu_item_title); | 180 String menuItemTitle = getString(R.string.menu_item_title); |
110 PendingIntent menuItemPendingIntent = createPendingIntent(); | 181 PendingIntent menuItemPendingIntent = createPendingIntent(); |
111 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); | 182 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); |
112 } | 183 } |
113 | 184 |
114 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); | 185 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
115 | 186 |
187 addActionButtons(intentBuilder); | |
188 | |
116 if (mAutoHideAppBarCheckbox.isChecked()) { | 189 if (mAutoHideAppBarCheckbox.isChecked()) { |
117 intentBuilder.enableUrlBarHiding(); | 190 intentBuilder.enableUrlBarHiding(); |
118 } | 191 } |
119 | 192 |
120 if (mCustomBackButtonCheckBox.isChecked()) { | 193 if (mCustomBackButtonCheckBox.isChecked()) { |
121 intentBuilder.setCloseButtonIcon( | 194 intentBuilder.setCloseButtonIcon( |
122 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a rrow_back)); | 195 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a rrow_back)); |
123 } | 196 } |
124 | 197 |
125 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli de_out_left); | 198 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli de_out_left); |
126 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, | 199 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, |
127 android.R.anim.slide_out_right); | 200 android.R.anim.slide_out_right); |
128 | 201 |
129 CustomTabActivityHelper.openCustomTab( | 202 CustomTabActivityHelper.openCustomTab( |
130 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback ()); | 203 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback (), |
204 mPackageName); | |
131 } | 205 } |
132 | 206 |
133 private PendingIntent createPendingIntent() { | 207 private PendingIntent createPendingIntent() { |
134 Intent actionIntent = new Intent( | 208 Intent actionIntent = new Intent( |
135 this.getApplicationContext(), ShareBroadcastReceiver.class); | 209 this.getApplicationContext(), ShareBroadcastReceiver.class); |
136 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte nt, 0); | 210 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte nt, 0); |
137 } | 211 } |
138 } | 212 } |
OLD | NEW |