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, |
(...skipping 14 matching lines...) Expand all Loading... | |
25 import android.util.Log; | 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 /** | 30 /** |
31 * Opens Chrome Custom Tabs with a customized UI. | 31 * Opens Chrome Custom Tabs with a customized UI. |
32 */ | 32 */ |
33 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL istener { | 33 public class CustomUIActivity extends AppCompatActivity implements View.OnClickL istener { |
34 private static final String TAG = "CustChromeTabActivity"; | 34 private static final String TAG = "CustChromeTabActivity"; |
35 private static final int TOOLBAR_ITEM_ID = 1; | |
35 | 36 |
36 private EditText mUrlEditText; | 37 private EditText mUrlEditText; |
37 private EditText mCustomTabColorEditText; | 38 private EditText mCustomTabColorEditText; |
39 private EditText mCustomTabSecondaryColorEditText; | |
38 private CheckBox mShowActionButtonCheckbox; | 40 private CheckBox mShowActionButtonCheckbox; |
39 private CheckBox mAddMenusCheckbox; | 41 private CheckBox mAddMenusCheckbox; |
40 private CheckBox mShowTitleCheckBox; | 42 private CheckBox mShowTitleCheckBox; |
41 private CheckBox mCustomBackButtonCheckBox; | 43 private CheckBox mCustomBackButtonCheckBox; |
42 private CheckBox mAutoHideAppBarCheckbox; | 44 private CheckBox mAutoHideAppBarCheckbox; |
45 private CheckBox mAddDefaultShareCheckbox; | |
46 private CheckBox mToolbarItemCheckbox; | |
43 private CustomTabActivityHelper mCustomTabActivityHelper; | 47 private CustomTabActivityHelper mCustomTabActivityHelper; |
44 | 48 |
45 @Override | 49 @Override |
46 protected void onCreate(Bundle savedInstanceState) { | 50 protected void onCreate(Bundle savedInstanceState) { |
47 super.onCreate(savedInstanceState); | 51 super.onCreate(savedInstanceState); |
48 setContentView(R.layout.activity_custom_ui); | 52 setContentView(R.layout.activity_custom_ui); |
49 | 53 |
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 |
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 mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_co lor); |
59 mCustomTabSecondaryColorEditText = | |
60 (EditText) findViewById(R.id.custom_toolbar_secondary_color); | |
55 mShowActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_act ion_button); | 61 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 mAddDefaultShareCheckbox = (CheckBox) findViewById(R.id.add_default_shar e); | |
67 mToolbarItemCheckbox = (CheckBox) findViewById(R.id.add_toolbar_item); | |
60 } | 68 } |
61 | 69 |
62 @Override | 70 @Override |
63 protected void onStart() { | 71 protected void onStart() { |
64 super.onStart(); | 72 super.onStart(); |
65 mCustomTabActivityHelper.bindCustomTabsService(this); | 73 mCustomTabActivityHelper.bindCustomTabsService(this); |
66 } | 74 } |
67 | 75 |
68 @Override | 76 @Override |
69 protected void onStop() { | 77 protected void onStop() { |
70 super.onStop(); | 78 super.onStop(); |
71 mCustomTabActivityHelper.unbindCustomTabsService(this); | 79 mCustomTabActivityHelper.unbindCustomTabsService(this); |
72 } | 80 } |
73 | 81 |
74 @Override | 82 @Override |
75 public void onClick(View v) { | 83 public void onClick(View v) { |
76 int viewId = v.getId(); | 84 int viewId = v.getId(); |
77 switch (viewId) { | 85 switch (viewId) { |
78 case R.id.start_custom_tab: | 86 case R.id.start_custom_tab: |
79 openCustomTab(); | 87 openCustomTab(); |
80 break; | 88 break; |
81 default: | 89 default: |
82 //Unknown View Clicked | 90 //Unknown View Clicked |
83 } | 91 } |
84 } | 92 } |
85 | 93 |
94 private int getColor(EditText editText) { | |
95 try { | |
96 return Color.parseColor(editText.getText().toString()); | |
97 } catch (NumberFormatException ex) { | |
98 Log.i(TAG, "Unable to parse Color: " + editText.getText()); | |
99 return Color.LTGRAY; | |
100 } | |
101 } | |
102 | |
86 private void openCustomTab() { | 103 private void openCustomTab() { |
87 String url = mUrlEditText.getText().toString(); | 104 String url = mUrlEditText.getText().toString(); |
88 | 105 |
89 int color = Color.BLUE; | 106 int color = getColor(mCustomTabColorEditText); |
90 try { | 107 int secondaryColor = getColor(mCustomTabSecondaryColorEditText); |
91 color = Color.parseColor(mCustomTabColorEditText.getText().toString( )); | |
92 } catch (NumberFormatException ex) { | |
93 Log.i(TAG, "Unable to parse Color: " + mCustomTabColorEditText.getTe xt()); | |
94 } | |
95 | 108 |
96 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); | 109 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); |
97 intentBuilder.setToolbarColor(color); | 110 intentBuilder.setToolbarColor(color); |
111 intentBuilder.setSecondaryToolbarColor(secondaryColor); | |
98 | 112 |
99 if (mShowActionButtonCheckbox.isChecked()) { | 113 if (mShowActionButtonCheckbox.isChecked()) { |
100 //Generally you do not want to decode bitmaps in the UI thread. | 114 //Generally you do not want to decode bitmaps in the UI thread. |
pasko
2016/03/01 14:28:28
is this a TODO? Why is it OK to decode bitmaps in
andreban
2016/03/01 15:03:03
Done.
| |
101 String shareLabel = getString(R.string.label_action_share); | 115 String actionLabel = getString(R.string.label_action); |
102 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 116 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
103 android.R.drawable.ic_menu_share); | 117 android.R.drawable.ic_menu_share); |
104 PendingIntent pendingIntent = createPendingIntent(); | 118 PendingIntent pendingIntent = |
105 intentBuilder.setActionButton(icon, shareLabel, pendingIntent); | 119 createPendingIntent(ActionBroadcastReceiver.ACTION_ACTION_BU TTON); |
120 intentBuilder.setActionButton(icon, actionLabel, pendingIntent); | |
106 } | 121 } |
107 | 122 |
108 if (mAddMenusCheckbox.isChecked()) { | 123 if (mAddMenusCheckbox.isChecked()) { |
109 String menuItemTitle = getString(R.string.menu_item_title); | 124 String menuItemTitle = getString(R.string.menu_item_title); |
110 PendingIntent menuItemPendingIntent = createPendingIntent(); | 125 PendingIntent menuItemPendingIntent = |
126 createPendingIntent(ActionBroadcastReceiver.ACTION_MENU_ITEM ); | |
111 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); | 127 intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); |
112 } | 128 } |
113 | 129 |
130 if (mAddDefaultShareCheckbox.isChecked()) { | |
131 intentBuilder.addDefaultShareMenuItem(); | |
132 } | |
133 | |
134 if (mToolbarItemCheckbox.isChecked()) { | |
135 //Generally you do not want to decode bitmaps in the UI thread. | |
136 String actionLabel = getString(R.string.label_action); | |
137 Bitmap icon = BitmapFactory.decodeResource(getResources(), | |
138 android.R.drawable.ic_menu_share); | |
139 PendingIntent pendingIntent = | |
140 createPendingIntent(ActionBroadcastReceiver.ACTION_TOOLBAR); | |
141 intentBuilder.addToolbarItem(TOOLBAR_ITEM_ID, icon, actionLabel, pen dingIntent); | |
142 } | |
143 | |
114 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); | 144 intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
115 | 145 |
116 if (mAutoHideAppBarCheckbox.isChecked()) { | 146 if (mAutoHideAppBarCheckbox.isChecked()) { |
117 intentBuilder.enableUrlBarHiding(); | 147 intentBuilder.enableUrlBarHiding(); |
118 } | 148 } |
119 | 149 |
120 if (mCustomBackButtonCheckBox.isChecked()) { | 150 if (mCustomBackButtonCheckBox.isChecked()) { |
121 intentBuilder.setCloseButtonIcon( | 151 intentBuilder.setCloseButtonIcon( |
122 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a rrow_back)); | 152 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a rrow_back)); |
123 } | 153 } |
124 | 154 |
125 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli de_out_left); | 155 intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.sli de_out_left); |
126 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, | 156 intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, |
127 android.R.anim.slide_out_right); | 157 android.R.anim.slide_out_right); |
128 | 158 |
129 CustomTabActivityHelper.openCustomTab( | 159 CustomTabActivityHelper.openCustomTab( |
130 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback ()); | 160 this, intentBuilder.build(), Uri.parse(url), new WebviewFallback ()); |
131 } | 161 } |
132 | 162 |
133 private PendingIntent createPendingIntent() { | 163 private PendingIntent createPendingIntent(int actionSourceId) { |
134 Intent actionIntent = new Intent( | 164 Intent actionIntent = new Intent( |
135 this.getApplicationContext(), ShareBroadcastReceiver.class); | 165 this.getApplicationContext(), ActionBroadcastReceiver.class); |
136 return PendingIntent.getBroadcast(getApplicationContext(), 0, actionInte nt, 0); | 166 actionIntent.putExtra(ActionBroadcastReceiver.KEY_ACTION_SOURCE, actionS ourceId); |
167 return PendingIntent.getBroadcast( | |
168 getApplicationContext(), actionSourceId, actionIntent, 0); | |
137 } | 169 } |
138 } | 170 } |
OLD | NEW |