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; | 20 import android.graphics.Color; |
21 import android.net.Uri; | 21 import android.net.Uri; |
22 import android.os.Bundle; | 22 import android.os.Bundle; |
23 import android.provider.CalendarContract; | |
24 import android.provider.ContactsContract; | |
23 import android.support.customtabs.CustomTabsIntent; | 25 import android.support.customtabs.CustomTabsIntent; |
24 import android.support.v7.app.AppCompatActivity; | 26 import android.support.v7.app.AppCompatActivity; |
25 import android.util.Log; | |
26 import android.view.View; | 27 import android.view.View; |
27 import android.widget.CheckBox; | 28 import android.widget.CheckBox; |
28 import android.widget.EditText; | 29 import android.widget.EditText; |
29 | 30 |
31 import org.chromium.customtabsclient.shared.CustomTabsHelper; | |
32 | |
33 import java.util.ArrayList; | |
34 | |
30 /** | 35 /** |
31 * Opens Chrome Custom Tabs with a customized UI. | 36 * Opens Chrome Custom Tabs with a customized UI. |
32 */ | 37 */ |
33 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL istener { | 38 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL istener { |
34 private static final String TAG = "CustChromeTabActivity"; | 39 private String mToolbarColor; |
35 | 40 private String mPackageName; |
41 private ArrayList<ActionButtonParams> mActionBarItems; | |
36 private EditText mUrlEditText; | 42 private EditText mUrlEditText; |
37 private EditText mCustomTabColorEditText; | 43 private CheckBox mAddActionButtonCheckbox; |
38 private CheckBox mShowActionButtonCheckbox; | |
39 private CheckBox mAddMenusCheckbox; | 44 private CheckBox mAddMenusCheckbox; |
40 private CheckBox mShowTitleCheckBox; | 45 private CheckBox mShowTitleCheckBox; |
41 private CheckBox mCustomBackButtonCheckBox; | 46 private CheckBox mCustomBackButtonCheckBox; |
42 private CheckBox mAutoHideAppBarCheckbox; | 47 private CheckBox mAutoHideAppBarCheckbox; |
43 private CustomTabActivityHelper mCustomTabActivityHelper; | 48 private CustomTabActivityHelper mCustomTabActivityHelper; |
44 | 49 |
50 private int ADVANCED_SETTING_REQUEST_CODE; | |
Ian Wen
2016/03/21 18:46:28
Constants should be static final, and should be pu
| |
45 @Override | 51 @Override |
46 protected void onCreate(Bundle savedInstanceState) { | 52 protected void onCreate(Bundle savedInstanceState) { |
47 super.onCreate(savedInstanceState); | 53 super.onCreate(savedInstanceState); |
48 setContentView(R.layout.activity_custom_ui); | 54 setContentView(R.layout.activity_custom_ui); |
49 | |
50 mCustomTabActivityHelper = new CustomTabActivityHelper(); | 55 mCustomTabActivityHelper = new CustomTabActivityHelper(); |
51 findViewById(R.id.start_custom_tab).setOnClickListener(this); | 56 findViewById(R.id.start_custom_tab).setOnClickListener(this); |
52 | 57 findViewById(R.id.advanced_ui_setting).setOnClickListener(this); |
53 mUrlEditText = (EditText) findViewById(R.id.url); | 58 mUrlEditText = (EditText) findViewById(R.id.url); |
54 mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_co lor); | 59 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); | 60 mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); |
57 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); | 61 mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); |
58 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but ton); | 62 mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_but ton); |
59 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo x); | 63 mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbo x); |
64 ADVANCED_SETTING_REQUEST_CODE = 999; | |
65 makeDefaultSetting(); | |
60 } | 66 } |
61 | 67 |
62 @Override | 68 @Override |
63 protected void onStart() { | 69 protected void onStart() { |
64 super.onStart(); | 70 super.onStart(); |
65 mCustomTabActivityHelper.bindCustomTabsService(this); | 71 mCustomTabActivityHelper.bindCustomTabsService(this); |
66 } | 72 } |
67 | 73 |
68 @Override | 74 @Override |
69 protected void onStop() { | 75 protected void onStop() { |
70 super.onStop(); | 76 super.onStop(); |
71 mCustomTabActivityHelper.unbindCustomTabsService(this); | 77 mCustomTabActivityHelper.unbindCustomTabsService(this); |
72 } | 78 } |
73 | 79 |
74 @Override | 80 @Override |
81 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
82 super.onActivityResult(requestCode, resultCode, data); | |
83 if ((requestCode == ADVANCED_SETTING_REQUEST_CODE)) { | |
84 try { | |
85 mActionBarItems = (ArrayList) data.getSerializableExtra | |
86 (AdvancedUISettingActivity.KEY_ACTION_ITEMS); | |
87 mToolbarColor = data.getStringExtra | |
88 (AdvancedUISettingActivity.KEY_COLOR); | |
89 mPackageName = data.getStringExtra | |
90 (AdvancedUISettingActivity.KEY_PACKAGE); | |
91 } catch (Exception e) { | |
92 e.printStackTrace(); | |
93 } | |
94 } | |
95 } | |
96 | |
97 @Override | |
75 public void onClick(View v) { | 98 public void onClick(View v) { |
76 int viewId = v.getId(); | 99 int viewId = v.getId(); |
77 switch (viewId) { | 100 switch (viewId) { |
78 case R.id.start_custom_tab: | 101 case R.id.start_custom_tab: |
79 openCustomTab(); | 102 openCustomTab(); |
80 break; | 103 break; |
104 case R.id.advanced_ui_setting: | |
105 openUISetting(); | |
106 break; | |
81 default: | 107 default: |
82 //Unknown View Clicked | 108 //Unknown View Clicked |
83 } | 109 } |
84 } | 110 } |
85 | 111 |
112 private void makeDefaultSetting() { | |
113 mToolbarColor = "Red"; | |
114 mPackageName = CustomTabsHelper.getPackageNameToUse(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, ADVANCED_SETTING_REQUEST_CODE); | |
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 aActivity.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(Color.parseColor(mToolbarColor)); |
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 |