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.Notification; |
16 import android.app.PendingIntent; | 17 import android.app.PendingIntent; |
17 import android.content.Intent; | 18 import android.content.Intent; |
18 import android.graphics.Bitmap; | 19 import android.graphics.Bitmap; |
19 import android.graphics.BitmapFactory; | 20 import android.graphics.BitmapFactory; |
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 static String toolbarColor; |
| 40 static String packageName; |
| 41 static ArrayList<String> allSupportingPackageNames; |
| 42 static ArrayList<ActionButtonParams> mActionBarItems; |
35 | 43 |
36 private EditText mUrlEditText; | 44 private EditText mUrlEditText; |
37 private EditText mCustomTabColorEditText; | 45 private CheckBox mAddActionButtonCheckbox; |
38 private CheckBox mShowActionButtonCheckbox; | |
39 private CheckBox mAddMenusCheckbox; | 46 private CheckBox mAddMenusCheckbox; |
40 private CheckBox mShowTitleCheckBox; | 47 private CheckBox mShowTitleCheckBox; |
41 private CheckBox mCustomBackButtonCheckBox; | 48 private CheckBox mCustomBackButtonCheckBox; |
42 private CheckBox mAutoHideAppBarCheckbox; | 49 private CheckBox mAutoHideAppBarCheckbox; |
43 private CustomTabActivityHelper mCustomTabActivityHelper; | 50 private CustomTabActivityHelper mCustomTabActivityHelper; |
44 | 51 |
| 52 private int advancedUISettintRequestCode; |
45 @Override | 53 @Override |
46 protected void onCreate(Bundle savedInstanceState) { | 54 protected void onCreate(Bundle savedInstanceState) { |
47 super.onCreate(savedInstanceState); | 55 super.onCreate(savedInstanceState); |
48 setContentView(R.layout.activity_custom_ui); | 56 setContentView(R.layout.activity_custom_ui); |
49 | |
50 mCustomTabActivityHelper = new CustomTabActivityHelper(); | 57 mCustomTabActivityHelper = new CustomTabActivityHelper(); |
51 findViewById(R.id.start_custom_tab).setOnClickListener(this); | 58 findViewById(R.id.start_custom_tab).setOnClickListener(this); |
52 | 59 findViewById(R.id.advanced_ui_setting).setOnClickListener(this); |
53 mUrlEditText = (EditText) findViewById(R.id.url); | 60 mUrlEditText = (EditText) findViewById(R.id.url); |
54 mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_co
lor); | 61 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); | 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 advancedUISettintRequestCode = 999; |
| 67 |
| 68 makeDefaultSetting(); |
60 } | 69 } |
61 | 70 |
62 @Override | 71 @Override |
63 protected void onStart() { | 72 protected void onStart() { |
64 super.onStart(); | 73 super.onStart(); |
65 mCustomTabActivityHelper.bindCustomTabsService(this); | 74 mCustomTabActivityHelper.bindCustomTabsService(this); |
66 } | 75 } |
67 | 76 |
68 @Override | 77 @Override |
69 protected void onStop() { | 78 protected void onStop() { |
70 super.onStop(); | 79 super.onStop(); |
71 mCustomTabActivityHelper.unbindCustomTabsService(this); | 80 mCustomTabActivityHelper.unbindCustomTabsService(this); |
72 } | 81 } |
73 | 82 |
74 @Override | 83 @Override |
75 public void onClick(View v) { | 84 public void onClick(View v) { |
76 int viewId = v.getId(); | 85 int viewId = v.getId(); |
77 switch (viewId) { | 86 switch (viewId) { |
78 case R.id.start_custom_tab: | 87 case R.id.start_custom_tab: |
79 openCustomTab(); | 88 openCustomTab(); |
80 break; | 89 break; |
| 90 case R.id.advanced_ui_setting: |
| 91 openUISetting(); |
| 92 break; |
81 default: | 93 default: |
82 //Unknown View Clicked | 94 //Unknown View Clicked |
83 } | 95 } |
84 } | 96 } |
85 | 97 |
| 98 private void makeDefaultSetting() { |
| 99 toolbarColor = "Red"; |
| 100 allSupportingPackageNames = CustomTabsHelper.getAllPackagesSupportingCus
tomTabs(this); |
| 101 packageName = CustomTabsHelper.getPackageNameToUse(this, allSupportingPa
ckageNames); |
| 102 mActionBarItems = new ArrayList<ActionButtonParams>(); |
| 103 } |
| 104 |
| 105 private void openUISetting() { |
| 106 Intent intent = new Intent(this, AdvancedUISettingActivity.class); |
| 107 startActivityForResult(intent, advancedUISettintRequestCode); |
| 108 } |
| 109 |
| 110 private void addActionButtons(CustomTabsIntent.Builder intentBuilder) { |
| 111 for (int i = 0; i < mActionBarItems.size(); i++) { |
| 112 intentBuilder.addActionBarItem(i, AdvancedUISettingActivity.uriToBit
Map(this, |
| 113 Uri.parse(mActionBarItems.get(i).image)), |
| 114 mActionBarItems.get(i).description, |
| 115 createPendingIntent(mActionBarItems.get(i).intent)); |
| 116 } |
| 117 } |
| 118 |
| 119 private PendingIntent createPendingIntent(String intent) { |
| 120 switch (intent) { |
| 121 case "Send": |
| 122 Intent sendIntent = new Intent(); |
| 123 sendIntent.setAction(Intent.ACTION_SEND); |
| 124 sendIntent.setType("text/plain"); |
| 125 return PendingIntent.getActivity(this, 0, sendIntent, 0); |
| 126 case "Select Contact": |
| 127 Intent contact = new Intent(Intent.ACTION_PICK); |
| 128 contact.setType(ContactsContract.Contacts.CONTENT_TYPE); |
| 129 return PendingIntent.getActivity(this, 0, contact, 0); |
| 130 case "Add Event": |
| 131 Intent event = new Intent(Intent.ACTION_INSERT) |
| 132 .setData(CalendarContract.Events.CONTENT_URI) |
| 133 .putExtra(CalendarContract.Events.TITLE, "Great Event") |
| 134 .putExtra(CalendarContract.Events.EVENT_LOCATION, "Googl
eplex 43") |
| 135 .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, "toda
y") |
| 136 .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, "tomorr
ow"); |
| 137 return PendingIntent.getActivity(this, 0, event, 0); |
| 138 case "Camera": |
| 139 return PendingIntent.getActivity(this, 0, new Intent(this, Camer
a.class), |
| 140 PendingIntent.FLAG_UPDATE_CURRENT); |
| 141 default: |
| 142 return null; |
| 143 } |
| 144 } |
| 145 |
86 private void openCustomTab() { | 146 private void openCustomTab() { |
87 String url = mUrlEditText.getText().toString(); | 147 String url = mUrlEditText.getText().toString(); |
88 | 148 |
89 int color = Color.BLUE; | 149 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 | 150 |
96 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); | 151 intentBuilder.setToolbarColor(AdvancedUISettingActivity.stringToColor(to
olbarColor)); |
97 intentBuilder.setToolbarColor(color); | 152 intentBuilder.addDefaultShareMenuItem(); |
98 | 153 |
99 if (mShowActionButtonCheckbox.isChecked()) { | 154 if (mAddActionButtonCheckbox.isChecked()) { |
100 //Generally you do not want to decode bitmaps in the UI thread. | 155 //Generally you do not want to decode bitmaps in the UI thread. |
101 String shareLabel = getString(R.string.label_action_share); | 156 String shareLabel = getString(R.string.label_action_share); |
102 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 157 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
103 android.R.drawable.ic_menu_share); | 158 android.R.drawable.ic_menu_share); |
104 PendingIntent pendingIntent = createPendingIntent(); | 159 PendingIntent pendingIntent = createPendingIntent(); |
105 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); | 160 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); |
106 } | 161 } |
107 | 162 |
108 if (mAddMenusCheckbox.isChecked()) { | 163 if (mAddMenusCheckbox.isChecked()) { |
109 String menuItemTitle = getString(R.string.menu_item_title); | 164 String menuItemTitle = getString(R.string.menu_item_title); |
110 PendingIntent menuItemPendingIntent = createPendingIntent(); | 165 PendingIntent menuItemPendingIntent = createPendingIntent(); |
111 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); | 166 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); |
112 } | 167 } |
113 | 168 |
114 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); | 169 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
115 | 170 |
| 171 addActionButtons(intentBuilder); |
| 172 |
116 if (mAutoHideAppBarCheckbox.isChecked()) { | 173 if (mAutoHideAppBarCheckbox.isChecked()) { |
117 intentBuilder.enableUrlBarHiding(); | 174 intentBuilder.enableUrlBarHiding(); |
118 } | 175 } |
119 | 176 |
120 if (mCustomBackButtonCheckBox.isChecked()) { | 177 if (mCustomBackButtonCheckBox.isChecked()) { |
121 intentBuilder.setCloseButtonIcon( | 178 intentBuilder.setCloseButtonIcon( |
122 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); | 179 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); |
123 } | 180 } |
124 | 181 |
125 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli
de_out_left); | 182 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli
de_out_left); |
126 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, | 183 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, |
127 android.R.anim.slide_out_right); | 184 android.R.anim.slide_out_right); |
128 | 185 |
129 CustomTabActivityHelper.openCustomTab( | 186 CustomTabActivityHelper.openCustomTab( |
130 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback
()); | 187 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback
(), |
| 188 packageName); |
131 } | 189 } |
132 | 190 |
133 private PendingIntent createPendingIntent() { | 191 private PendingIntent createPendingIntent() { |
134 Intent actionIntent = new Intent( | 192 Intent actionIntent = new Intent( |
135 this.getApplicationContext(), ShareBroadcastReceiver.class); | 193 this.getApplicationContext(), ShareBroadcastReceiver.class); |
136 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte
nt, 0); | 194 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte
nt, 0); |
137 } | 195 } |
138 } | 196 } |
OLD | NEW |