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.app.Dialog; | |
18 import android.content.Context; | |
19 import android.content.Intent; | |
20 import android.graphics.Bitmap; | |
21 import android.graphics.Color; | |
22 import android.net.Uri; | |
23 import android.os.Bundle; | |
24 import android.provider.MediaStore; | |
25 import android.support.v7.app.AppCompatActivity; | |
26 import android.view.LayoutInflater; | |
27 import android.view.View; | |
28 import android.widget.ArrayAdapter; | |
29 import android.widget.Button; | |
30 import android.widget.EditText; | |
31 import android.widget.ImageView; | |
32 import android.widget.LinearLayout; | |
33 import android.widget.Spinner; | |
34 import android.widget.TextView; | |
35 | |
36 import java.io.Serializable; | |
37 import java.util.ArrayList; | |
38 import java.util.List; | |
39 | |
40 /** | |
41 * An activity for advanced UI settings. | |
42 */ | |
43 public class AdvancedUISettingActivity extends AppCompatActivity implements View .OnClickListener{ | |
44 public static final String KEY_PACKAGES_LIST = "packageNameList"; | |
45 public static final String KEY_PACKAGE = "packageName"; | |
46 public static final String KEY_COLOR = "color"; | |
47 public static final String KEY_ACTION_ITEMS = "actionBarItems"; | |
48 public static final String[] INTENTS_STRINGS = {"Send", "Select Contact", "A dd Event", | |
49 "Camera"}; | |
50 | |
51 private static final int PICK_IMAGE_REQUEST = 1; | |
52 | |
53 private Spinner mToolbarColorSpinner; | |
54 private Spinner mPackageSpinner; | |
55 private Button mSaveButton; | |
56 private Button mAddActionBarItemButton; | |
57 private List<String> mPackageNameList; | |
58 private String mPackageName; | |
59 private String mColor; | |
60 private Dialog mAddActionBarItemDialog; | |
61 | |
62 private Uri mImage; | |
63 private ArrayList<ActionButtonParams> mActionBarItems; | |
64 | |
65 /** | |
66 * @return All possible colors. | |
67 */ | |
68 public static String[] getColors() { | |
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 public static int stringToColor(String color) { | |
80 try { | |
81 return (int) Color.class.getField(color.toUpperCase()).get(new Color ()); | |
82 } catch (Exception e) { | |
83 return 0; // Transparent. | |
84 } | |
85 } | |
86 | |
87 /** | |
88 * Puts uri, instead of Bitmap in intent to save space. | |
89 */ | |
90 static Bitmap uriToBitMap(Context context, Uri uri) { | |
91 try { | |
92 return MediaStore.Images.Media.getBitmap(context.getContentResolver( ), uri); | |
93 } catch (Exception e) { | |
94 return null; | |
95 } | |
96 } | |
97 | |
98 @Override | |
99 protected void onCreate(Bundle savedInstanceState) { | |
100 super.onCreate(savedInstanceState); | |
101 setContentView(R.layout.advanced_ui_setting); | |
102 mToolbarColorSpinner = (Spinner) findViewById(R.id.spinner_color); | |
103 mPackageSpinner = (Spinner) findViewById(R.id.spinner_package); | |
104 mSaveButton = (Button) findViewById(R.id.save_setting); | |
105 mAddActionBarItemButton = (Button) findViewById(R.id.add_actionbar_item) ; | |
106 mPackageNameList = CustomUIActivity.allSupportingPackageNames; | |
107 mPackageName = CustomUIActivity.packageName; | |
108 mColor = CustomUIActivity.toolbarColor; | |
109 mActionBarItems = CustomUIActivity.mActionBarItems; | |
110 mSaveButton.setOnClickListener(this); | |
111 mAddActionBarItemButton.setOnClickListener(this); | |
112 mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, | |
113 android.R.layout.simple_spinner_dropdown_item, getColors())); | |
114 mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, | |
115 android.R.layout.simple_spinner_dropdown_item, mPackageNameList) ); | |
116 | |
117 selectUISetting(); | |
118 } | |
119 | |
120 @Override | |
121 public void onClick(View view) { | |
122 switch (view.getId()) { | |
123 case R.id.save_setting: | |
124 sendUISettingToUIActivity(); | |
125 finish(); | |
126 break; | |
127 case R.id.add_actionbar_item: | |
128 showAddActionBarItemDialog(); | |
129 break; | |
130 case R.id.item_delete: | |
131 deleteItem(view); | |
132 break; | |
133 } | |
134 } | |
135 | |
136 public void onClickDialog(View view) { | |
137 switch (view.getId()) { | |
138 case R.id.save_item: | |
139 saveActionBarItem(); | |
140 // Only add the last item to the UI. | |
141 displayActionBarItems(); | |
142 mAddActionBarItemDialog.dismiss(); | |
143 break; | |
144 case R.id.cancel: | |
145 mAddActionBarItemDialog.dismiss(); | |
146 break; | |
147 case R.id.select_file: | |
148 selectFile(); | |
149 break; | |
150 } | |
151 } | |
152 | |
153 @Override | |
154 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
155 super.onActivityResult(requestCode, resultCode, data); | |
156 if ((requestCode == PICK_IMAGE_REQUEST)) { | |
157 try { | |
158 Uri uri = data.getData(); | |
159 mImage = uri; | |
160 ImageView image = (ImageView) mAddActionBarItemDialog | |
161 .findViewById(R.id.imageInDialog); | |
162 image.setImageBitmap(uriToBitMap(this, mImage)); | |
163 image.requestLayout(); | |
164 } catch (Exception e) { | |
165 e.printStackTrace(); | |
166 } | |
167 } | |
168 } | |
169 | |
170 /** | |
171 * Displays action bar items. | |
172 */ | |
173 private void displayActionBarItems() { | |
174 LayoutInflater inflater = (LayoutInflater)getBaseContext() | |
175 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
176 LinearLayout itemsList = (LinearLayout) findViewById(R.id.actionbar_item s); | |
177 itemsList.removeAllViews(); | |
178 for (int i = 0; i < mActionBarItems.size(); i++) { | |
179 View item = inflater.inflate(R.layout.display_action_bar_item_settin g, null); | |
180 ((TextView) item.findViewById(R.id.item_description)) | |
181 .setText("Description: " + mActionBarItems.get(i).descriptio n); | |
182 ((TextView) item.findViewById(R.id.item_intent)) | |
183 .setText("Intent: " + mActionBarItems.get(i).intent); | |
184 ((TextView) item.findViewById(R.id.item_image_title)).setText("Image : "); | |
185 ImageView image = (ImageView) item.findViewById(R.id.item_image); | |
186 image.setImageBitmap(uriToBitMap(this, Uri.parse(mActionBarItems.get (i).image))); | |
187 Button deleteButton = (Button) item.findViewById(R.id.item_delete); | |
188 deleteButton.setOnClickListener(this); | |
189 itemsList.addView(item); | |
190 } | |
191 } | |
192 | |
193 private void selectFile() { | |
194 Intent intent = new Intent(); | |
195 intent.setType("image/*"); | |
196 intent.setAction(Intent.ACTION_OPEN_DOCUMENT); | |
197 startActivityForResult(Intent.createChooser(intent, "Select Picture"), P ICK_IMAGE_REQUEST); | |
198 } | |
199 | |
200 private void deleteItem(View deleteButton) { | |
201 LinearLayout lineLayout = (LinearLayout) deleteButton.getParent().getPar ent(); | |
202 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
203 LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); | |
204 int index = getIndexOfLayout(itemLayout, itemsLayout); | |
205 itemsLayout.removeView(itemLayout); | |
206 mActionBarItems.remove(index); | |
207 } | |
208 | |
209 private int getIndexOfLayout(LinearLayout item, LinearLayout setting) { | |
210 int kidsPos; | |
211 for (kidsPos = 0; kidsPos < setting.getChildCount(); kidsPos ++) { | |
212 View view = setting.getChildAt(kidsPos); | |
213 if (view == item) { | |
214 break; | |
215 } | |
216 } | |
217 return kidsPos; | |
218 } | |
219 | |
220 private void showAddActionBarItemDialog() { | |
221 mAddActionBarItemDialog = new Dialog(this, R.style.DialogTheme); | |
222 mAddActionBarItemDialog.setContentView(R.layout.dialog_add_actionbar_ite m); | |
223 mAddActionBarItemDialog.setTitle("Add Action Bar Item"); | |
224 ((Spinner) mAddActionBarItemDialog.findViewById(R.id.intent)) | |
225 .setAdapter(new ArrayAdapter<String>(this, | |
226 android.R.layout.simple_spinner_dropdown_item, INTENTS_S TRINGS)); | |
227 mImage = Uri.parse( | |
228 "android.resource://org.chromium.customtabsdemos/drawable/ic_act ion_name"); | |
229 | |
230 mAddActionBarItemDialog.show(); | |
231 } | |
232 | |
233 private void sendUISettingToUIActivity() { | |
234 Intent intent = new Intent(); | |
235 CustomUIActivity.packageName = mPackageSpinner.getSelectedItem().toStrin g(); | |
236 CustomUIActivity.toolbarColor = mToolbarColorSpinner.getSelectedItem().t oString(); | |
237 CustomUIActivity.mActionBarItems = mActionBarItems; | |
238 setResult(0, intent); | |
239 } | |
240 | |
241 private void saveActionBarItem() { | |
242 ActionButtonParams actionItem = new ActionButtonParams(); | |
243 actionItem.description = ((EditText) mAddActionBarItemDialog | |
244 .findViewById(R.id.description)).getText().toString(); | |
245 actionItem.image = mImage.toString(); | |
246 actionItem.intent = ((Spinner) mAddActionBarItemDialog | |
247 .findViewById(R.id.intent)).getSelectedItem().toString(); | |
248 mActionBarItems.add(actionItem); | |
249 } | |
250 | |
251 /** | |
252 * Select UI setting previously set by the user. | |
253 * If first time, select default UI setting from Custom UI Activity. | |
254 */ | |
255 private void selectUISetting() { | |
256 for (int position = 0; position < mToolbarColorSpinner.getAdapter().getC ount(); | |
257 position ++) { | |
258 if (mToolbarColorSpinner.getAdapter().getItem(position).equals(mColo r)) { | |
259 mToolbarColorSpinner.setSelection(position); | |
260 break; | |
261 } | |
262 } | |
263 for (int position = 0; position < mPackageSpinner.getAdapter().getCount( ); position ++) { | |
264 if (mPackageSpinner.getAdapter().getItem(position).equals(mPackageNa me)) { | |
265 mPackageSpinner.setSelection(position); | |
266 break; | |
267 } | |
268 } | |
269 | |
270 displayActionBarItems(); | |
271 } | |
272 } | |
273 | |
274 class ActionButtonParams implements Serializable{ | |
Ian Wen
2016/03/16 01:35:30
1. indentation is wrong.
2. This class has to be a
| |
275 String description; | |
276 String intent; | |
277 String image; | |
278 } | |
OLD | NEW |