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("packageName"); |
| 106 toolbarColor = data.getStringExtra("color"); |
| 107 itemsDescriptions = (ArrayList<String>) data.getSerializableExtra("i
temsDescription"); |
| 108 itemsImage = (ArrayList<Uri>) data.getSerializableExtra("itemsImage"
); |
| 109 itemsIntent = (ArrayList<String>) data.getSerializableExtra("itemsIn
tent"); |
| 110 } else { |
| 111 |
| 112 } |
| 113 } |
| 114 |
| 115 private void makeDefaultSetting() { |
| 116 toolbarColor = "Red"; |
| 117 allSupportingPackageNames = CustomTabsHelper.getAllPackagesSupportingCus
tomTabs(this); |
| 118 packageName = CustomTabsHelper.getPackageNameToUse(this, allSupportingPa
ckageNames); |
| 119 itemsIntent = new ArrayList<String>(); |
| 120 itemsDescriptions = new ArrayList<String>(); |
| 121 itemsImage = new ArrayList<Uri>(); |
| 122 } |
| 123 |
| 124 private void openUISetting() { |
| 125 Intent intent = new Intent(this, AdvancedUISettingActivity.class); |
| 126 intent.putExtra("packageNameList", allSupportingPackageNames); |
| 127 intent.putExtra("packageName", packageName); |
| 128 intent.putExtra("color", toolbarColor); |
| 129 intent.putExtra("itemsDescription", itemsDescriptions); |
| 130 intent.putExtra("itemsImage", itemsImage); |
| 131 intent.putExtra("itemsIntent", itemsIntent); |
| 132 startActivityForResult(intent, advancedUISettintRequestCode); |
| 133 } |
| 134 |
| 135 private void addActionButtons(CustomTabsIntent.Builder intentBuilder) { |
| 136 for (int i = 0; i < itemsDescriptions.size(); i++) { |
| 137 intentBuilder.addActionBarItem(i, |
| 138 AdvancedUISettingActivity.uriToBitMap(this, itemsImage.get(i
)), |
| 139 itemsDescriptions.get(i), |
| 140 createPendingIntent(itemsIntent.get(i))); |
| 141 } |
| 142 } |
| 143 |
| 144 private PendingIntent createPendingIntent(String intent) { |
| 145 switch (intent) { |
| 146 case "Send": |
| 147 Intent sendIntent = new Intent(); |
| 148 sendIntent.setAction(Intent.ACTION_SEND); |
| 149 sendIntent.setType("text/plain"); |
| 150 return PendingIntent.getActivity(this, 0, sendIntent, 0); |
| 151 case "Select Contact": |
| 152 Intent contact = new Intent(Intent.ACTION_PICK); |
| 153 contact.setType(ContactsContract.Contacts.CONTENT_TYPE); |
| 154 return PendingIntent.getActivity(this, 0, contact, 0); |
| 155 case "Add Event": |
| 156 Intent event = new Intent(Intent.ACTION_INSERT) |
| 157 .setData(CalendarContract.Events.CONTENT_URI) |
| 158 .putExtra(CalendarContract.Events.TITLE, "Great Event") |
| 159 .putExtra(CalendarContract.Events.EVENT_LOCATION, "Googl
eplex 43") |
| 160 .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, "toda
y") |
| 161 .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, "tomorr
ow"); |
| 162 return PendingIntent.getActivity(this, 0, event, 0); |
| 163 case "Camera": |
| 164 return PendingIntent.getActivity(this, 0, new Intent(this, Camer
a.class), |
| 165 PendingIntent.FLAG_UPDATE_CURRENT); |
| 166 default: |
| 167 return null; |
| 168 } |
| 169 } |
| 170 |
86 private void openCustomTab() { | 171 private void openCustomTab() { |
87 String url = mUrlEditText.getText().toString(); | 172 String url = mUrlEditText.getText().toString(); |
88 | 173 |
89 int color = Color.BLUE; | 174 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 | 175 |
96 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); | 176 intentBuilder.setToolbarColor(CustomTabsHelper.stringToColor(toolbarColo
r)); |
97 intentBuilder.setToolbarColor(color); | 177 intentBuilder.addDefaultShareMenuItem(); |
98 | 178 |
99 if (mShowActionButtonCheckbox.isChecked()) { | 179 if (mAddActionButtonCheckbox.isChecked()) { |
100 //Generally you do not want to decode bitmaps in the UI thread. | 180 //Generally you do not want to decode bitmaps in the UI thread. |
101 String shareLabel = getString(R.string.label_action_share); | 181 String shareLabel = getString(R.string.label_action_share); |
102 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 182 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
103 android.R.drawable.ic_menu_share); | 183 android.R.drawable.ic_menu_share); |
104 PendingIntent pendingIntent = createPendingIntent(); | 184 PendingIntent pendingIntent = createPendingIntent(); |
105 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); | 185 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); |
106 } | 186 } |
107 | 187 |
108 if (mAddMenusCheckbox.isChecked()) { | 188 if (mAddMenusCheckbox.isChecked()) { |
109 String menuItemTitle = getString(R.string.menu_item_title); | 189 String menuItemTitle = getString(R.string.menu_item_title); |
110 PendingIntent menuItemPendingIntent = createPendingIntent(); | 190 PendingIntent menuItemPendingIntent = createPendingIntent(); |
111 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); | 191 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); |
112 } | 192 } |
113 | 193 |
114 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); | 194 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
115 | 195 |
| 196 addActionButtons(intentBuilder); |
| 197 |
116 if (mAutoHideAppBarCheckbox.isChecked()) { | 198 if (mAutoHideAppBarCheckbox.isChecked()) { |
117 intentBuilder.enableUrlBarHiding(); | 199 intentBuilder.enableUrlBarHiding(); |
118 } | 200 } |
119 | 201 |
120 if (mCustomBackButtonCheckBox.isChecked()) { | 202 if (mCustomBackButtonCheckBox.isChecked()) { |
121 intentBuilder.setCloseButtonIcon( | 203 intentBuilder.setCloseButtonIcon( |
122 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); | 204 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); |
123 } | 205 } |
124 | 206 |
125 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli
de_out_left); | 207 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli
de_out_left); |
126 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, | 208 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, |
127 android.R.anim.slide_out_right); | 209 android.R.anim.slide_out_right); |
128 | 210 |
129 CustomTabActivityHelper.openCustomTab( | 211 CustomTabActivityHelper.openCustomTab( |
130 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback
()); | 212 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback
(), |
| 213 packageName); |
131 } | 214 } |
132 | 215 |
133 private PendingIntent createPendingIntent() { | 216 private PendingIntent createPendingIntent() { |
134 Intent actionIntent = new Intent( | 217 Intent actionIntent = new Intent( |
135 this.getApplicationContext(), ShareBroadcastReceiver.class); | 218 this.getApplicationContext(), ShareBroadcastReceiver.class); |
136 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte
nt, 0); | 219 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte
nt, 0); |
137 } | 220 } |
138 } | 221 } |
OLD | NEW |