OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 Google Inc. All Rights Reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (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 | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 package org.chromium.customtabsdemos; | |
16 | |
17 import android.content.Context; | |
18 import android.content.Intent; | |
19 import android.graphics.Bitmap; | |
20 import android.graphics.Color; | |
21 import android.net.Uri; | |
22 import android.os.Bundle; | |
23 import android.provider.MediaStore; | |
24 import android.support.v4.app.NavUtils; | |
25 import android.support.v7.app.AppCompatActivity; | |
26 import android.view.LayoutInflater; | |
27 import android.view.MenuItem; | |
28 import android.view.View; | |
29 import android.widget.ArrayAdapter; | |
30 import android.widget.Button; | |
31 import android.widget.ImageView; | |
32 import android.widget.LinearLayout; | |
33 import android.widget.Spinner; | |
34 import android.widget.TextView; | |
35 | |
36 import org.chromium.customtabsclient.shared.CustomTabsHelper; | |
37 | |
38 import java.io.Serializable; | |
39 import java.util.ArrayList; | |
40 import java.util.List; | |
41 | |
42 /** | |
43 * An activity for advanced UI settings. | |
44 */ | |
45 public class AdvancedUISettingActivity extends AppCompatActivity implements View .OnClickListener{ | |
46 static final String KEY_PACKAGE = "packageName"; | |
47 static final String KEY_COLOR = "color"; | |
48 static final String KEY_ACTION_ITEMS = "actionBarItems"; | |
49 static final String[] INTENTS_STRINGS = {"Send", "Select Contact", "Add Even t", | |
50 "Camera"}; | |
51 | |
52 private static final int PICK_IMAGE_REQUEST = 1; | |
53 | |
54 private Spinner mToolbarColorSpinner; | |
55 private Spinner mPackageSpinner; | |
56 private Button mSaveButton; | |
57 private Button mAddActionBarItemButton; | |
58 private List<String> mPackageNameList; | |
59 private String mPackageName; | |
60 private String mColor; | |
61 | |
62 private int mActionBarItemIndex; | |
63 private ArrayList<ActionButtonParams> mActionBarItems; | |
64 | |
65 /** | |
66 * @return All possible colors. | |
67 */ | |
68 static String[] getColors() { | |
Ian Wen
2016/03/16 01:35:30
These methods should be private
BigBossZhiling
2016/03/18 00:59:07
Done.
| |
69 ArrayList<String> fieldList = new ArrayList<String>(); | |
70 String colorName; | |
71 for (int i = 0; i < Color.class.getFields().length; i ++) { | |
72 colorName = Color.class.getFields()[i].getName(); | |
73 // Make only the first character capitalized. | |
74 fieldList.add(i, colorName.charAt(0) + colorName.substring(1).toLowe rCase()); | |
75 } | |
76 return fieldList.toArray(new String[fieldList.size()]); | |
77 } | |
78 | |
79 /** | |
80 * Convert color in string to int. | |
Ian Wen
2016/03/16 01:35:30
s/Convert/Converts. Same for all the javadocs you
BigBossZhiling
2016/03/18 00:59:07
Done.
| |
81 * @param color, which is color in string. | |
82 * @return | |
83 */ | |
84 static int stringToColor(String color) { | |
Ian Wen
2016/03/16 01:35:30
Remove this method and use Color.parse() instead.
BigBossZhiling
2016/03/18 00:59:07
Done.
| |
85 try { | |
86 return (int) Color.class.getField(color.toUpperCase()).get(new Color ()); | |
87 } catch (Exception e) { | |
88 return 0; // Transparent. | |
89 } | |
90 } | |
91 | |
92 /** | |
93 * Convert uri to bitmap. Put uri, instead of Bitmap in intent to save space , | |
94 * so we have to convert it back. | |
95 */ | |
96 static Bitmap uriToBitMap(Context context, Uri uri) { | |
Ian Wen
2016/03/16 01:35:30
s/BitMap/Bitmap, make it private
BigBossZhiling
2016/03/18 00:59:07
Done.
| |
97 try { | |
98 return MediaStore.Images.Media.getBitmap(context.getContentResolver( ), uri); | |
99 } catch (Exception e) { | |
100 return null; | |
101 } | |
102 } | |
103 | |
104 @Override | |
105 protected void onCreate(Bundle savedInstanceState) { | |
106 super.onCreate(savedInstanceState); | |
107 setContentView(R.layout.advanced_ui_setting); | |
108 mToolbarColorSpinner = (Spinner) findViewById(R.id.spinner_color); | |
109 mPackageSpinner = (Spinner) findViewById(R.id.spinner_package); | |
110 mSaveButton = (Button) findViewById(R.id.save_setting); | |
111 mAddActionBarItemButton = (Button) findViewById(R.id.add_actionbar_item) ; | |
112 mPackageNameList = CustomTabsHelper.getAllPackagesSupportingCustomTabs(t his); | |
113 mPackageName = (String) getIntent().getSerializableExtra(KEY_PACKAGE); | |
114 mColor = (String) getIntent().getSerializableExtra(KEY_COLOR); | |
115 mActionBarItems = (ArrayList<ActionButtonParams>) getIntent() | |
116 .getSerializableExtra(KEY_ACTION_ITEMS); | |
117 mSaveButton.setOnClickListener(this); | |
118 mAddActionBarItemButton.setOnClickListener(this); | |
119 mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, | |
120 android.R.layout.simple_spinner_dropdown_item, getColors())); | |
121 mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, | |
122 android.R.layout.simple_spinner_dropdown_item, mPackageNameList) ); | |
123 getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
124 selectUISetting(); | |
125 } | |
126 | |
127 @Override | |
128 public void onClick(View view) { | |
129 switch (view.getId()) { | |
130 case R.id.save_setting: | |
131 sendUISettingToUIActivity(); | |
132 finish(); | |
133 break; | |
134 case R.id.add_actionbar_item: | |
135 ActionButtonParams actionB = createNewActionBarItem(); | |
136 displayActionBarItem(actionB.description, actionB.intent, action B.image); | |
137 break; | |
138 case R.id.item_delete: | |
139 deleteItem(view); | |
140 break; | |
141 case R.id.select_file: | |
142 mActionBarItemIndex = getActionItemIndex(view); | |
143 selectFile(); | |
144 break; | |
145 } | |
146 } | |
147 | |
148 @Override | |
149 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
150 super.onActivityResult(requestCode, resultCode, data); | |
151 if ((requestCode == PICK_IMAGE_REQUEST)) { | |
152 try { | |
153 Uri uri = data.getData(); | |
154 mActionBarItems.get(mActionBarItemIndex).setImage(uri.toString() ); | |
155 updateImage(mActionBarItemIndex, uri); | |
156 } catch (Exception e) { | |
157 e.printStackTrace(); | |
158 } | |
159 } | |
160 } | |
161 | |
162 @Override | |
163 public boolean onOptionsItemSelected(MenuItem item) { | |
164 switch (item.getItemId()) { | |
165 // Respond to the action bar's Up/Home button | |
166 case android.R.id.home: | |
167 finish(); | |
168 return true; | |
169 } | |
170 return super.onOptionsItemSelected(item); | |
171 } | |
172 | |
173 private void updateImage(int index, Uri image) { | |
174 LinearLayout item = (LinearLayout) ((LinearLayout) this.findViewById(R.i d.actionbar_items)) | |
175 .getChildAt(index); | |
176 ((ImageView) item.findViewById(R.id.item_image)).setImageURI(image); | |
177 } | |
178 | |
179 private void displayActionBarItem(String description, String intent, String image) { | |
180 LayoutInflater inflater = (LayoutInflater)getBaseContext() | |
181 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
182 LinearLayout itemsList = (LinearLayout) findViewById(R.id.actionbar_item s); | |
183 View item = inflater.inflate(R.layout.display_action_bar_item_setting, n ull); | |
184 item.findViewById(R.id.select_file).setOnClickListener(this); | |
185 item.findViewById(R.id.item_delete).setOnClickListener(this); | |
186 Spinner intents = ((Spinner) item.findViewById(R.id.item_intent)); | |
187 intents.setAdapter(new ArrayAdapter<String>(this, | |
188 android.R.layout.simple_spinner_dropdown_item, INTENTS_STRINGS)) ; | |
189 | |
190 ((TextView) item.findViewById(R.id.item_description)).setText(descriptio n); | |
191 | |
192 for (int index = 0, count = ((Spinner) item.findViewById(R.id.item_inten t)).getCount(); | |
193 index < count; ++index) | |
194 { | |
195 if (intents.getAdapter().getItem(index).equals(intent)) | |
196 { | |
197 intents.setSelection(index); | |
198 } | |
199 } | |
200 ((ImageView) item.findViewById(R.id.item_image)).setImageURI( | |
201 Uri.parse(image)); | |
202 | |
203 itemsList.addView(item); | |
204 } | |
205 | |
206 private ActionButtonParams createNewActionBarItem() { | |
207 ActionButtonParams button = new ActionButtonParams(); | |
208 mActionBarItems.add(button); | |
209 return button; | |
210 } | |
211 | |
212 /** | |
213 * Displays action bar items. | |
214 */ | |
215 private void displayActionBarItems() { | |
216 for (ActionButtonParams buttonParam : mActionBarItems) { | |
217 displayActionBarItem(buttonParam.description, buttonParam.intent, bu ttonParam.image); | |
218 } | |
219 } | |
220 | |
221 private void selectFile() { | |
222 Intent intent = new Intent(); | |
223 intent.setType("image/*"); | |
224 intent.setAction(Intent.ACTION_OPEN_DOCUMENT); | |
225 startActivityForResult(Intent.createChooser(intent, "Select Picture"), P ICK_IMAGE_REQUEST); | |
226 } | |
227 | |
228 private void deleteItem(View deleteButton) { | |
229 LinearLayout lineLayout = (LinearLayout) deleteButton.getParent().getPar ent(); | |
230 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
231 LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); | |
232 int index = getIndexOfLayout(itemLayout, itemsLayout); | |
233 itemsLayout.removeView(itemLayout); | |
234 mActionBarItems.remove(index); | |
235 } | |
236 | |
237 private int getIndexOfLayout(LinearLayout item, LinearLayout items) { | |
238 int kidsPos; | |
239 for (kidsPos = 0; kidsPos < items.getChildCount(); kidsPos ++) { | |
240 View view = items.getChildAt(kidsPos); | |
241 if (view == item) { | |
242 break; | |
243 } | |
244 } | |
245 return kidsPos; | |
246 } | |
247 | |
248 private int getActionItemIndex(View button) { | |
249 LinearLayout lineLayout = (LinearLayout) button.getParent().getParent(); | |
250 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
251 LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); | |
252 return getIndexOfLayout(itemLayout, itemsLayout); | |
253 } | |
254 | |
255 private void sendUISettingToUIActivity() { | |
256 saveActionBarItems(); | |
257 Intent intent = new Intent(); | |
258 intent.putExtra(KEY_PACKAGE, mPackageSpinner.getSelectedItem().toString( )); | |
259 intent.putExtra(KEY_COLOR, mToolbarColorSpinner.getSelectedItem().toStri ng()); | |
260 intent.putExtra(KEY_ACTION_ITEMS, mActionBarItems); | |
261 setResult(0, intent); | |
262 } | |
263 | |
264 private void saveActionBarItems() { | |
265 LinearLayout items = (LinearLayout) this.findViewById(R.id.actionbar_ite ms); | |
266 int index = 0; | |
267 while (index < items.getChildCount()) { | |
268 System.out.println("trying to save!!!"); | |
269 LinearLayout item = (LinearLayout) items.getChildAt(index); | |
270 System.out.println( | |
271 ((TextView) item.findViewById(R.id.item_description)).getTex t().toString()); | |
272 mActionBarItems.get(index).setDescription( | |
273 ((TextView) item.findViewById(R.id.item_description)).getTex t().toString()); | |
274 mActionBarItems.get(index).setIntent( | |
275 ((Spinner) item.findViewById(R.id.item_intent)).getSelectedI tem().toString()); | |
276 index ++; | |
277 } | |
278 } | |
279 | |
280 /** | |
281 * Select UI setting previously set by the user. | |
282 * If first time, select default UI setting from Custom UI Activity. | |
283 */ | |
284 private void selectUISetting() { | |
285 for (int position = 0; position < mToolbarColorSpinner.getAdapter().getC ount(); | |
286 position ++) { | |
287 if (mToolbarColorSpinner.getAdapter().getItem(position).equals(mColo r)) { | |
288 mToolbarColorSpinner.setSelection(position); | |
289 break; | |
290 } | |
291 } | |
292 for (int position = 0; position < mPackageSpinner.getAdapter().getCount( ); position ++) { | |
293 if (mPackageSpinner.getAdapter().getItem(position).equals(mPackageNa me)) { | |
294 mPackageSpinner.setSelection(position); | |
295 break; | |
296 } | |
297 } | |
298 | |
299 displayActionBarItems(); | |
300 } | |
301 } | |
302 | |
303 class ActionButtonParams implements Serializable{ | |
Ian Wen
2016/03/16 01:35:30
1. Why is it package protected, not private?
2. Th
BigBossZhiling
2016/03/18 00:59:07
I think the reason why it is not private is that a
| |
304 // Default values for action button. | |
305 String description = "Great Feature"; | |
306 String intent = "Send"; | |
307 String image = "android.resource://org.chromium.customtabsdemos/drawable/ic_ action_name";; | |
308 | |
309 public ActionButtonParams setImage(String image) { | |
310 this.image = image; | |
311 return this; | |
312 } | |
313 | |
314 public ActionButtonParams setIntent(String intent) { | |
315 this.intent = intent; | |
316 return this; | |
317 } | |
318 | |
319 public ActionButtonParams setDescription(String description) { | |
320 this.description = description; | |
321 return this; | |
322 } | |
323 } | |
OLD | NEW |