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 package org.chromium.customtabsdemos; | |
Ian Wen
2016/02/10 02:36:06
Add one line before #14.
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
15 | |
16 import android.app.Dialog; | |
17 import android.content.Context; | |
18 import android.content.Intent; | |
19 import android.graphics.Bitmap; | |
20 import android.net.Uri; | |
21 import android.os.Bundle; | |
22 import android.provider.MediaStore; | |
23 import android.support.v7.app.AppCompatActivity; | |
24 import android.view.LayoutInflater; | |
25 import android.view.View; | |
26 import android.widget.ArrayAdapter; | |
27 import android.widget.Button; | |
28 import android.widget.EditText; | |
29 import android.widget.ImageView; | |
30 import android.widget.LinearLayout; | |
31 import android.widget.Spinner; | |
32 import android.widget.TextView; | |
33 import org.chromium.customtabsclient.shared.CustomTabsHelper; | |
34 import java.util.ArrayList; | |
35 import java.util.List; | |
36 | |
37 /** | |
38 * An activity for advanced UI settings. | |
39 */ | |
40 public class AdvancedUISettingActivity extends AppCompatActivity implements View .OnClickListener{ | |
41 public static final String KEY_PACKAGES_LIST = "packageNameList"; | |
42 public static final String KEY_PACKAGE = "packageName"; | |
43 public static final String KEY_COLOR = "color"; | |
44 public static final String KEY_DESCRIPTIONS = "itemsDescription"; | |
45 public static final String KEY_INTENTS = "itemsIntent"; | |
46 public static final String KEY_IMAGES = "itemsImage"; | |
47 | |
48 private static final int PICK_IMAGE_REQUEST = 1; | |
49 | |
50 private Spinner mToolbarColorSpinner; | |
51 private Spinner mPackageSpinner; | |
52 private Button mSaveButton; | |
53 private Button mAddActionBarItemButton; | |
54 private List<String> mPackageNameList; | |
55 private String mPackageName; | |
56 private String mColor; | |
57 private Dialog mAddActionBarItemDialog; | |
58 | |
59 private Uri mImage; | |
60 private ArrayList<String> mDescriptions; | |
Ian Wen
2016/02/10 02:36:06
Instead of holding three lists, make a static priv
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
61 private ArrayList<Uri> mImages; | |
62 private ArrayList<String> mIntents; | |
63 /** | |
64 * Puts uri, instead of Bitmap in intent to save space. | |
65 */ | |
66 static Bitmap uriToBitMap(Context context, Uri uri) { | |
67 try { | |
68 return MediaStore.Images.Media.getBitmap(context.getContentResolver( ), uri); | |
69 } catch (Exception e) { | |
70 return null; | |
71 } | |
72 } | |
73 | |
74 @Override | |
75 protected void onCreate(Bundle savedInstanceState) { | |
76 super.onCreate(savedInstanceState); | |
77 setContentView(R.layout.advanced_ui_setting); | |
78 mToolbarColorSpinner = (Spinner) findViewById(R.id.spinner_color); | |
79 mPackageSpinner = (Spinner) findViewById(R.id.spinner_package); | |
80 mSaveButton = (Button) findViewById(R.id.save_setting); | |
81 mAddActionBarItemButton = (Button) findViewById(R.id.add_actionbar_item) ; | |
82 mPackageNameList = (List<String>) getIntent().getSerializableExtra(KEY_P ACKAGES_LIST); | |
83 mPackageName = getIntent().getStringExtra(KEY_PACKAGE); | |
84 mColor = getIntent().getStringExtra(KEY_COLOR); | |
85 mDescriptions = (ArrayList<String>) getIntent() | |
Ian Wen
2016/02/10 02:36:06
#85 - #90 are not formatted correctly. Java line w
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
86 .getSerializableExtra(KEY_DESCRIPTIONS); | |
87 mIntents = (ArrayList<String>) getIntent() | |
88 .getSerializableExtra(KEY_INTENTS); | |
89 mImages = (ArrayList<Uri>) getIntent() | |
90 .getSerializableExtra(KEY_IMAGES); | |
91 mSaveButton.setOnClickListener(this); | |
92 mAddActionBarItemButton.setOnClickListener(this); | |
93 mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout. row, | |
94 R.id.item, | |
95 CustomTabsHelper.getColors())); | |
96 | |
Ian Wen
2016/02/10 02:36:06
Remove phantom empty lines.
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
97 mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.row, | |
98 R.id.item, mPackageNameList)); | |
99 | |
100 // Select UI setting previously set by the user. | |
Ian Wen
2016/02/10 02:36:06
Move the two comments to the javadoc of selectUISe
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
101 // If first time, select default UI setting from Custom UI Activity. | |
102 selectUISetting(); | |
103 } | |
104 | |
105 @Override | |
106 public void onClick(View view) { | |
107 switch (view.getId()) { | |
108 case R.id.save_setting: | |
Ian Wen
2016/02/10 02:36:06
Let's not mingle the onclick in dialog with onclic
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
109 sendUISettingToUIActivity(); | |
110 finish(); | |
111 break; | |
112 case R.id.add_actionbar_item: | |
113 showActionBarItemDialogue(); | |
Ian Wen
2016/02/10 02:36:06
In our codebase we never use the word "dialogue" (
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
114 break; | |
115 case R.id.save_item: | |
116 saveActionBarItem(); | |
117 // Only add the last item to the UI. | |
118 displayActionBarItems(mDescriptions.size() - 1, | |
119 mDescriptions.size()); | |
120 mAddActionBarItemDialog.dismiss(); | |
121 break; | |
122 case R.id.cancel: | |
123 mAddActionBarItemDialog.dismiss(); | |
124 break; | |
125 case R.id.select_file: | |
126 selectFile(); | |
127 break; | |
128 case R.id.item_delete: | |
129 deleteItem(view); | |
130 break; | |
131 } | |
132 } | |
133 | |
134 @Override | |
135 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
136 super.onActivityResult(requestCode, resultCode, data); | |
137 if ((requestCode == PICK_IMAGE_REQUEST)) { | |
138 try { | |
139 Uri uri = data.getData(); | |
140 mImage = uri; | |
141 ImageView image = (ImageView) mAddActionBarItemDialog | |
142 .findViewById(R.id.imageInDialogue); | |
143 image.setImageBitmap(uriToBitMap(this, mImage)); | |
144 image.requestLayout(); | |
145 } catch (Exception e) { | |
146 e.printStackTrace(); | |
147 } | |
148 } | |
149 } | |
150 | |
151 /** | |
152 * Displays items stored in the arraylist from index startIndex to endIndex. | |
153 * @param startIndex is the starting index. | |
154 * @param endIndex is the ending index. | |
155 */ | |
156 private void displayActionBarItems(int startIndex, int endIndex) { | |
Ian Wen
2016/02/10 02:36:06
The two parameters are not necessary. You can get
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
157 LayoutInflater inflater = (LayoutInflater)getBaseContext() | |
158 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
159 LinearLayout itemsList = (LinearLayout) findViewById(R.id.actionbar_item s); | |
160 | |
161 for (int i = startIndex; i < endIndex; i++) { | |
162 View item = inflater.inflate(R.layout.display_action_bar_item_settin g, null); | |
163 ((TextView) item.findViewById(R.id.item_description)) | |
164 .setText("Description: " + mDescriptions.get(i)); | |
165 ((TextView) item.findViewById(R.id.item_intent)) | |
166 .setText("Intent: " + mIntents.get(i)); | |
167 ((TextView) item.findViewById(R.id.item_image_title)).setText("Image : "); | |
168 ImageView image = (ImageView) item.findViewById(R.id.item_image); | |
169 image.setImageBitmap(uriToBitMap(this, mImages.get(i))); | |
170 Button deleteButton = (Button) item.findViewById(R.id.item_delete); | |
171 deleteButton.setOnClickListener(this); | |
172 itemsList.addView(item); | |
173 } | |
174 } | |
175 | |
176 private void selectFile() { | |
177 Intent intent = new Intent(); | |
178 intent.setType("image/*"); | |
179 intent.setAction(Intent.ACTION_OPEN_DOCUMENT); | |
180 startActivityForResult(Intent.createChooser(intent, "Select Picture"), P ICK_IMAGE_REQUEST); | |
181 } | |
182 | |
183 private void deleteItem(View deleteButton) { | |
184 LinearLayout lineLayout = (LinearLayout) deleteButton.getParent().getPar ent(); | |
185 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
186 LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); | |
187 int index = getIndexOfLayout(itemLayout, itemsLayout); | |
188 itemsLayout.removeView(itemLayout); | |
189 mDescriptions.remove(index); | |
190 mImages.remove(index); | |
191 mIntents.remove(index); | |
192 } | |
193 | |
194 private int getIndexOfLayout(LinearLayout item, LinearLayout setting) { | |
195 int kidsPos; | |
196 for (kidsPos = 0; kidsPos < setting.getChildCount(); kidsPos ++) { | |
197 View view = setting.getChildAt(kidsPos); | |
198 | |
Ian Wen
2016/02/10 02:36:06
Remove phantom empty lines.
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
199 if (view == item) { | |
200 break; | |
201 } | |
202 } | |
203 return kidsPos; | |
204 } | |
205 | |
206 private void showActionBarItemDialogue() { | |
Ian Wen
2016/02/10 02:36:06
Dialog.
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
207 mAddActionBarItemDialog = new Dialog(this, R.style.DialogTheme); | |
208 mAddActionBarItemDialog.setContentView(R.layout.dialogue_add_actionbar_i tem); | |
209 mAddActionBarItemDialog.setTitle("Add Action Bar Item"); | |
210 mAddActionBarItemDialog.findViewById(R.id.save_item).setOnClickListener( this); | |
211 mAddActionBarItemDialog.findViewById(R.id.cancel).setOnClickListener(thi s); | |
212 mAddActionBarItemDialog.findViewById(R.id.select_file).setOnClickListene r(this); | |
213 ((Spinner) mAddActionBarItemDialog.findViewById(R.id.intent)) | |
214 .setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.it em, getIntents())); | |
215 mImage = Uri.parse( | |
216 "android.resource://org.chromium.customtabsdemos/drawable/ic_act ion_name"); | |
217 | |
218 mAddActionBarItemDialog.show(); | |
219 } | |
220 | |
221 private String[] getIntents() { | |
222 return new String[] {"Send", "Select Contact", "Add Event", "Camera"}; | |
Ian Wen
2016/02/10 02:36:06
1. This method does not return "Intents"; it retur
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
223 } | |
224 | |
225 private void sendUISettingToUIActivity() { | |
226 Intent intent = new Intent(); | |
227 intent.putExtra(KEY_PACKAGE, mPackageSpinner.getSelectedItem().toString( )); | |
228 intent.putExtra(KEY_COLOR, mToolbarColorSpinner.getSelectedItem().toStri ng()); | |
229 intent.putExtra(KEY_DESCRIPTIONS, mDescriptions); | |
230 intent.putExtra(KEY_IMAGES, mImages); | |
231 intent.putExtra(KEY_INTENTS, mIntents); | |
232 setResult(0, intent); | |
Ian Wen
2016/02/10 02:36:06
You need to finish so that the parent activity can
BigBossZhiling
2016/02/17 05:18:09
Done.
| |
233 } | |
234 | |
235 private void saveActionBarItem() { | |
236 mDescriptions.add(((EditText) mAddActionBarItemDialog | |
237 .findViewById(R.id.description)).getText().toString()); | |
238 mImages.add(mImage); | |
239 mIntents.add(((Spinner) mAddActionBarItemDialog | |
240 .findViewById(R.id.intent)).getSelectedItem().toString()); | |
241 } | |
242 | |
243 private void selectUISetting() { | |
244 for (int position = 0; position < mToolbarColorSpinner.getAdapter().getC ount(); | |
245 position ++) { | |
246 if (mToolbarColorSpinner.getAdapter().getItem(position).equals(mColo r)) { | |
247 mToolbarColorSpinner.setSelection(position); | |
248 break; | |
249 } | |
250 } | |
251 for (int position = 0; position < mPackageSpinner.getAdapter().getCount( ); position ++) { | |
252 if (mPackageSpinner.getAdapter().getItem(position).equals(mPackageNa me)) { | |
253 mPackageSpinner.setSelection(position); | |
254 break; | |
255 } | |
256 } | |
257 | |
258 displayActionBarItems(0, mDescriptions.size()); | |
259 } | |
260 } | |
OLD | NEW |