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 static final String TAG = "CustChromeTabActivity"; |
35 | 39 |
36 private EditText mUrlEditText; | 40 private EditText mUrlEditText; |
37 private EditText mCustomTabColorEditText; | 41 private CheckBox mAddActionButtonCheckbox; |
38 private CheckBox mShowActionButtonCheckbox; | |
39 private CheckBox mAddMenusCheckbox; | 42 private CheckBox mAddMenusCheckbox; |
40 private CheckBox mShowTitleCheckBox; | 43 private CheckBox mShowTitleCheckBox; |
41 private CheckBox mCustomBackButtonCheckBox; | 44 private CheckBox mCustomBackButtonCheckBox; |
42 private CheckBox mAutoHideAppBarCheckbox; | 45 private CheckBox mAutoHideAppBarCheckbox; |
43 private CustomTabActivityHelper mCustomTabActivityHelper; | 46 private CustomTabActivityHelper mCustomTabActivityHelper; |
44 | 47 |
48 private String toolbarColor; | |
49 private String packageName; | |
50 private ArrayList<String> itemsDescriptions; | |
51 private ArrayList<Uri> itemsImage; | |
52 private ArrayList<String> allSupportingPackageNames; | |
53 private ArrayList<String> itemsIntent; | |
54 | |
55 private int advancedUISettintRequestCode; | |
45 @Override | 56 @Override |
46 protected void onCreate(Bundle savedInstanceState) { | 57 protected void onCreate(Bundle savedInstanceState) { |
47 super.onCreate(savedInstanceState); | 58 super.onCreate(savedInstanceState); |
48 setContentView(R.layout.activity_custom_ui); | 59 setContentView(R.layout.activity_custom_ui); |
49 | |
50 mCustomTabActivityHelper = new CustomTabActivityHelper(); | 60 mCustomTabActivityHelper = new CustomTabActivityHelper(); |
51 findViewById(R.id.start_custom_tab).setOnClickListener(this); | 61 findViewById(R.id.start_custom_tab).setOnClickListener(this); |
52 | 62 findViewById(R.id.advanced_ui_setting).setOnClickListener(this); |
53 mUrlEditText = (EditText) findViewById(R.id.url); | 63 mUrlEditText = (EditText) findViewById(R.id.url); |
54 mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_co lor); | 64 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); | 65 mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); |
57 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); | 66 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); |
58 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but ton); | 67 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but ton); |
59 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo x); | 68 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo x); |
69 advancedUISettintRequestCode = 999; | |
70 | |
71 makeDefaultSetting(); | |
60 } | 72 } |
61 | 73 |
62 @Override | 74 @Override |
63 protected void onStart() { | 75 protected void onStart() { |
64 super.onStart(); | 76 super.onStart(); |
65 mCustomTabActivityHelper.bindCustomTabsService(this); | 77 mCustomTabActivityHelper.bindCustomTabsService(this); |
66 } | 78 } |
67 | 79 |
68 @Override | 80 @Override |
69 protected void onStop() { | 81 protected void onStop() { |
70 super.onStop(); | 82 super.onStop(); |
71 mCustomTabActivityHelper.unbindCustomTabsService(this); | 83 mCustomTabActivityHelper.unbindCustomTabsService(this); |
72 } | 84 } |
73 | 85 |
74 @Override | 86 @Override |
75 public void onClick(View v) { | 87 public void onClick(View v) { |
76 int viewId = v.getId(); | 88 int viewId = v.getId(); |
77 switch (viewId) { | 89 switch (viewId) { |
78 case R.id.start_custom_tab: | 90 case R.id.start_custom_tab: |
79 openCustomTab(); | 91 openCustomTab(); |
80 break; | 92 break; |
93 case R.id.advanced_ui_setting: | |
94 openUISetting(); | |
95 break; | |
81 default: | 96 default: |
82 //Unknown View Clicked | 97 //Unknown View Clicked |
83 } | 98 } |
84 } | 99 } |
85 | 100 |
101 @Override | |
102 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
103 super.onActivityResult(requestCode, resultCode, data); | |
104 if (requestCode == advancedUISettintRequestCode && data != null) { | |
105 packageName = data.getStringExtra(AdvancedUISettingActivity.KEY_PACK AGE); | |
106 toolbarColor = data.getStringExtra(AdvancedUISettingActivity.KEY_COL OR); | |
107 itemsDescriptions = (ArrayList<String>) data.getSerializableExtra( | |
108 AdvancedUISettingActivity.KEY_DESCRIPTIONS); | |
109 itemsImage = (ArrayList<Uri>) data.getSerializableExtra( | |
110 AdvancedUISettingActivity.KEY_IMAGES); | |
111 itemsIntent = (ArrayList<String>) data.getSerializableExtra( | |
112 AdvancedUISettingActivity.KEY_INTENTS); | |
113 } else { | |
114 | |
115 } | |
116 } | |
117 | |
118 private void makeDefaultSetting() { | |
119 toolbarColor = "Red"; | |
120 allSupportingPackageNames = CustomTabsHelper.getAllPackagesSupportingCus tomTabs(this); | |
121 packageName = CustomTabsHelper.getPackageNameToUse(this, allSupportingPa ckageNames); | |
122 itemsIntent = new ArrayList<String>(); | |
123 itemsDescriptions = new ArrayList<String>(); | |
124 itemsImage = new ArrayList<Uri>(); | |
125 } | |
126 | |
127 private void openUISetting() { | |
128 Intent intent = new Intent(this, AdvancedUISettingActivity.class); | |
129 intent.putExtra(AdvancedUISettingActivity.KEY_PACKAGES_LIST, allSupporti ngPackageNames); | |
Ian Wen
2016/02/10 02:36:06
you don't need to pass the list to the advanced ac
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
130 intent.putExtra(AdvancedUISettingActivity.KEY_PACKAGE, packageName); | |
131 intent.putExtra(AdvancedUISettingActivity.KEY_COLOR, toolbarColor); | |
132 intent.putExtra(AdvancedUISettingActivity.KEY_DESCRIPTIONS, itemsDescrip tions); | |
133 intent.putExtra(AdvancedUISettingActivity.KEY_IMAGES, itemsImage); | |
134 intent.putExtra(AdvancedUISettingActivity.KEY_INTENTS, itemsIntent); | |
135 startActivityForResult(intent, advancedUISettintRequestCode); | |
136 } | |
137 | |
138 private void addActionButtons(CustomTabsIntent.Builder intentBuilder) { | |
139 for (int i = 0; i < itemsDescriptions.size(); i++) { | |
140 intentBuilder.addActionBarItem(i, | |
141 AdvancedUISettingActivity.uriToBitMap(this, itemsImage.get(i )), | |
142 itemsDescriptions.get(i), | |
143 createPendingIntent(itemsIntent.get(i))); | |
144 } | |
145 } | |
146 | |
147 private PendingIntent createPendingIntent(String intent) { | |
148 switch (intent) { | |
149 case "Send": | |
150 Intent sendIntent = new Intent(); | |
151 sendIntent.setAction(Intent.ACTION_SEND); | |
152 sendIntent.setType("text/plain"); | |
153 return PendingIntent.getActivity(this, 0, sendIntent, 0); | |
154 case "Select Contact": | |
155 Intent contact = new Intent(Intent.ACTION_PICK); | |
156 contact.setType(ContactsContract.Contacts.CONTENT_TYPE); | |
157 return PendingIntent.getActivity(this, 0, contact, 0); | |
158 case "Add Event": | |
159 Intent event = new Intent(Intent.ACTION_INSERT) | |
160 .setData(CalendarContract.Events.CONTENT_URI) | |
161 .putExtra(CalendarContract.Events.TITLE, "Great Event") | |
162 .putExtra(CalendarContract.Events.EVENT_LOCATION, "Googl eplex 43") | |
163 .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, "toda y") | |
164 .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, "tomorr ow"); | |
165 return PendingIntent.getActivity(this, 0, event, 0); | |
166 case "Camera": | |
167 return PendingIntent.getActivity(this, 0, new Intent(this, Camer a.class), | |
168 PendingIntent.FLAG_UPDATE_CURRENT); | |
169 default: | |
170 return null; | |
171 } | |
172 } | |
173 | |
86 private void openCustomTab() { | 174 private void openCustomTab() { |
87 String url = mUrlEditText.getText().toString(); | 175 String url = mUrlEditText.getText().toString(); |
88 | 176 |
89 int color = Color.BLUE; | 177 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 | 178 |
96 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); | 179 intentBuilder.setToolbarColor(CustomTabsHelper.stringToColor(toolbarColo r)); |
97 intentBuilder.setToolbarColor(color); | 180 intentBuilder.addDefaultShareMenuItem(); |
98 | 181 |
99 if (mShowActionButtonCheckbox.isChecked()) { | 182 if (mAddActionButtonCheckbox.isChecked()) { |
100 //Generally you do not want to decode bitmaps in the UI thread. | 183 //Generally you do not want to decode bitmaps in the UI thread. |
101 String shareLabel = getString(R.string.label_action_share); | 184 String shareLabel = getString(R.string.label_action_share); |
102 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 185 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
103 android.R.drawable.ic_menu_share); | 186 android.R.drawable.ic_menu_share); |
104 PendingIntent pendingIntent = createPendingIntent(); | 187 PendingIntent pendingIntent = createPendingIntent(); |
105 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); | 188 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); |
106 } | 189 } |
107 | 190 |
108 if (mAddMenusCheckbox.isChecked()) { | 191 if (mAddMenusCheckbox.isChecked()) { |
109 String menuItemTitle = getString(R.string.menu_item_title); | 192 String menuItemTitle = getString(R.string.menu_item_title); |
110 PendingIntent menuItemPendingIntent = createPendingIntent(); | 193 PendingIntent menuItemPendingIntent = createPendingIntent(); |
111 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); | 194 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); |
112 } | 195 } |
113 | 196 |
114 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); | 197 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
115 | 198 |
199 addActionButtons(intentBuilder); | |
200 | |
116 if (mAutoHideAppBarCheckbox.isChecked()) { | 201 if (mAutoHideAppBarCheckbox.isChecked()) { |
117 intentBuilder.enableUrlBarHiding(); | 202 intentBuilder.enableUrlBarHiding(); |
118 } | 203 } |
119 | 204 |
120 if (mCustomBackButtonCheckBox.isChecked()) { | 205 if (mCustomBackButtonCheckBox.isChecked()) { |
121 intentBuilder.setCloseButtonIcon( | 206 intentBuilder.setCloseButtonIcon( |
122 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a rrow_back)); | 207 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a rrow_back)); |
123 } | 208 } |
124 | 209 |
125 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli de_out_left); | 210 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli de_out_left); |
126 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, | 211 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, |
127 android.R.anim.slide_out_right); | 212 android.R.anim.slide_out_right); |
128 | 213 |
129 CustomTabActivityHelper.openCustomTab( | 214 CustomTabActivityHelper.openCustomTab( |
130 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback ()); | 215 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback (), |
216 packageName); | |
131 } | 217 } |
132 | 218 |
133 private PendingIntent createPendingIntent() { | 219 private PendingIntent createPendingIntent() { |
134 Intent actionIntent = new Intent( | 220 Intent actionIntent = new Intent( |
135 this.getApplicationContext(), ShareBroadcastReceiver.class); | 221 this.getApplicationContext(), ShareBroadcastReceiver.class); |
136 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte nt, 0); | 222 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte nt, 0); |
137 } | 223 } |
138 } | 224 } |
OLD | NEW |