OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 Google Inc. All Rights Reserved. | |
Ian Wen
2016/02/09 00:14:15
2016
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
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; | |
15 | |
16 import android.app.ActionBar; | |
17 import android.app.Dialog; | |
18 import android.content.Context; | |
19 import android.content.Intent; | |
20 import android.graphics.Bitmap; | |
21 import android.net.Uri; | |
22 import android.os.Bundle; | |
23 import android.provider.MediaStore; | |
24 import android.support.v7.app.AppCompatActivity; | |
25 import android.view.LayoutInflater; | |
26 import android.view.View; | |
27 import android.widget.ArrayAdapter; | |
28 import android.widget.Button; | |
29 import android.widget.EditText; | |
30 import android.widget.ImageView; | |
31 import android.widget.LinearLayout; | |
32 import android.widget.Spinner; | |
33 import android.widget.TextView; | |
34 import org.chromium.customtabsclient.shared.CustomTabsHelper; | |
35 import java.util.ArrayList; | |
36 import java.util.List; | |
37 | |
38 /** | |
39 * Advanced UI setting. | |
Ian Wen
2016/02/09 00:14:15
An activity for advanced UI settings.
| |
40 */ | |
41 public class AdvancedUISettingActivity extends AppCompatActivity implements View .OnClickListener{ | |
42 private Spinner mToolbarColorSpinner; | |
43 private Spinner mPackageSpinner; | |
44 private Button mSaveButton; | |
45 private Button mAddActionBarItemButton; | |
46 private List<String> packageNameList; | |
47 private String packageName; | |
48 private String color; | |
Ian Wen
2016/02/09 00:14:15
All memeber variables should start with an "m".
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
49 private Dialog addActionBarItemDialogue; | |
Ian Wen
2016/02/09 00:14:15
Use Dialog instead of Dialogue. Our names are long
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
50 | |
51 private int PICK_IMAGE_REQUEST = 1; | |
Ian Wen
2016/02/09 00:14:15
Make this a static final and move it before dynami
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
52 private Uri imageInAddItemDialogue; | |
Ian Wen
2016/02/09 00:14:15
This is a uri, not a dialog.
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
53 private ArrayList<String> actionBarItemsDescription; | |
54 private ArrayList<Uri> actionBarItemsImage; | |
55 private ArrayList<String> actionBarItemsIntent; | |
56 | |
57 @Override | |
58 protected void onCreate(Bundle savedInstanceState) { | |
59 super.onCreate(savedInstanceState); | |
60 setContentView(R.layout.advanced_ui_setting); | |
61 mToolbarColorSpinner = (Spinner) findViewById(R.id.spinner_color); | |
62 mPackageSpinner = (Spinner) findViewById(R.id.spinner_package); | |
63 mSaveButton = (Button) findViewById(R.id.save_setting); | |
64 mAddActionBarItemButton = (Button) findViewById(R.id.add_actionbar_item) ; | |
65 packageNameList = (List<String>) getIntent().getSerializableExtra("packa geNameList"); | |
Ian Wen
2016/02/09 00:14:15
Make these strings (eg "packageNameList") public c
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
66 packageName = getIntent().getStringExtra("packageName"); | |
67 color = getIntent().getStringExtra("color"); | |
68 actionBarItemsDescription = (ArrayList<String>) getIntent() | |
69 .getSerializableExtra("itemsDescription"); | |
70 actionBarItemsIntent = (ArrayList<String>) getIntent() | |
71 .getSerializableExtra("itemsIntent"); | |
72 actionBarItemsImage = (ArrayList<Uri>) getIntent() | |
73 .getSerializableExtra("itemsImage"); | |
74 mSaveButton.setOnClickListener(this); | |
75 mAddActionBarItemButton.setOnClickListener(this); | |
76 mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout. row, | |
77 R.id.item, | |
78 CustomTabsHelper.getColors())); | |
79 | |
80 mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.row, | |
Ian Wen
2016/02/09 00:14:15
Use android.R.layout.simple_spinner_dropdown_item
BigBossZhiling
2016/02/09 01:03:11
I tried R.layout.simple_spinner_dropdown_item, but
| |
81 R.id.item, packageNameList)); | |
82 | |
83 // Select UI setting previously set by the user. | |
84 // If first time, select default UI setting from Custom UI Activity. | |
85 selectUISetting(); | |
86 } | |
87 | |
88 @Override | |
89 public void onClick(View view) { | |
90 switch (view.getId()) { | |
91 case R.id.save_setting: | |
92 sendUISettingToUIActivity(); | |
93 finish(); | |
94 break; | |
95 case R.id.add_actionbar_item: | |
96 addActionBarItemDialogue(); | |
97 break; | |
98 case R.id.save_item: | |
99 saveActionBarItem(); | |
100 // Only add the last item to the UI. | |
101 displayActionBarItems(actionBarItemsDescription.size() - 1, | |
102 actionBarItemsDescription.size()); | |
103 addActionBarItemDialogue.dismiss(); | |
104 break; | |
105 case R.id.cancel: | |
106 addActionBarItemDialogue.dismiss(); | |
107 break; | |
108 case R.id.select_file: | |
109 selectFile(); | |
110 break; | |
111 case R.id.item_delete: | |
112 deleteItem(view); | |
113 break; | |
114 } | |
115 } | |
116 | |
117 @Override | |
118 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
119 super.onActivityResult(requestCode, resultCode, data); | |
120 if ((requestCode == PICK_IMAGE_REQUEST)) { | |
121 try { | |
122 Uri uri = data.getData(); | |
123 imageInAddItemDialogue = uri; | |
124 ImageView image = (ImageView) addActionBarItemDialogue | |
125 .findViewById(R.id.imageInDialogue); | |
126 image.setImageBitmap(uriToBitMap(this, imageInAddItemDialogue)); | |
127 image.requestLayout(); | |
128 } catch (Exception e) { | |
129 e.printStackTrace(); | |
130 } | |
131 } | |
132 } | |
133 | |
134 // Put uri, instead of Bitmap in intent to save space. | |
Ian Wen
2016/02/09 00:14:15
Switch to javadoc and use "Puts" instead of "put".
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
135 static Bitmap uriToBitMap(Context context, Uri uri) { | |
Ian Wen
2016/02/09 00:14:15
Static methods should be placed before dynamic met
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
136 try { | |
137 return MediaStore.Images.Media.getBitmap(context.getContentResolver( ), uri); | |
138 } catch (Exception e) { | |
139 return null; | |
140 } | |
141 } | |
142 | |
143 /** | |
144 * Display of items stored in the arraylist from index startIndex to endInde . | |
Ian Wen
2016/02/09 00:14:16
endInde. Typo.
Ian Wen
2016/02/09 00:14:16
Displays items......
BigBossZhiling
2016/02/09 01:03:11
Done.
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
145 * @param startIndex is the starting index. | |
146 * @param endIndex is the ending index. | |
147 */ | |
148 private void displayActionBarItems(int startIndex, int endIndex) { | |
149 LayoutInflater inflater = (LayoutInflater)getBaseContext() | |
150 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
151 LinearLayout setting = (LinearLayout) findViewById(R.id.advanced_ui_sett ing_linear_layout); | |
152 // Start position is the index in the layout to add action bar item | |
153 // Start position is the third last index. | |
154 int startPosition = setting.getChildCount() - 2; | |
155 for (int i = startIndex; i < endIndex; i++) { | |
156 View item = inflater.inflate(R.layout.display_action_bar_item_settin g, null); | |
157 ((TextView) item.findViewById(R.id.item_description)) | |
158 .setText("Description: " + actionBarItemsDescription.get(i)) ; | |
159 ((TextView) item.findViewById(R.id.item_intent)) | |
160 .setText("Intent: " + actionBarItemsIntent.get(i)); | |
161 ((TextView) item.findViewById(R.id.item_image_title)).setText("Image : "); | |
162 ImageView image = (ImageView) item.findViewById(R.id.item_image); | |
163 image.setImageBitmap(uriToBitMap(this, actionBarItemsImage.get(i))); | |
164 Button deleteButton = (Button) item.findViewById(R.id.item_delete); | |
165 deleteButton.setOnClickListener(this); | |
166 setting.addView(item, startPosition); | |
Ian Wen
2016/02/09 00:14:15
You can directly say addView, without giving it a
BigBossZhiling
2016/02/09 01:03:11
Yes I can, but I think I don't want to add the vie
| |
167 startPosition += 1; | |
168 | |
Ian Wen
2016/02/09 00:14:15
Remove empty line.
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
169 } | |
170 } | |
171 | |
172 private void selectFile() { | |
173 Intent intent = new Intent(); | |
174 intent.setType("image/*"); | |
175 intent.setAction(Intent.ACTION_OPEN_DOCUMENT); | |
176 startActivityForResult(Intent.createChooser(intent, "Select Picture"), P ICK_IMAGE_REQUEST); | |
177 } | |
178 | |
179 private void deleteItem(View deleteButton) { | |
180 LinearLayout buttonLayout = (LinearLayout) deleteButton.getParent(); | |
181 LinearLayout lineLayout = (LinearLayout) buttonLayout.getParent(); | |
182 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
183 LinearLayout settingLayout = (LinearLayout) itemLayout.getParent(); | |
184 int index = getIndexOfLayout(R.id.actiona_bar_item, itemLayout, settingL ayout); | |
185 settingLayout.removeView(itemLayout); | |
186 actionBarItemsDescription.remove(index); | |
187 actionBarItemsImage.remove(index); | |
188 actionBarItemsIntent.remove(index); | |
189 } | |
190 | |
191 private int getIndexOfLayout(int type, LinearLayout item, LinearLayout setti ng) { | |
192 int currentIndex = 0; | |
193 for (int kidsPos = 0; kidsPos < setting.getChildCount(); kidsPos ++) { | |
194 View view = setting.getChildAt(kidsPos); | |
195 // Check whether is the item we are searching for. | |
196 if (view.getId() == type && view != item) { | |
197 currentIndex += 1; | |
198 } | |
199 } | |
200 return currentIndex; | |
201 } | |
202 | |
203 private void addActionBarItemDialogue() { | |
Ian Wen
2016/02/09 00:14:15
This method shows a dialog, not "add". I would use
BigBossZhiling
2016/02/09 01:03:11
Done.
| |
204 addActionBarItemDialogue = new Dialog(this, R.style.DialogTheme); | |
205 addActionBarItemDialogue.setContentView(R.layout.dialogue_add_actionbar_ item); | |
206 addActionBarItemDialogue.setTitle("Add Action Bar Item"); | |
207 addActionBarItemDialogue.findViewById(R.id.save_item).setOnClickListener (this); | |
208 addActionBarItemDialogue.findViewById(R.id.cancel).setOnClickListener(th is); | |
209 addActionBarItemDialogue.findViewById(R.id.select_file).setOnClickListen er(this); | |
210 ((Spinner) addActionBarItemDialogue.findViewById(R.id.intent)) | |
211 .setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.it em, getIntents())); | |
212 imageInAddItemDialogue = Uri.parse( | |
213 "android.resource://org.chromium.customtabsdemos/drawable/ic_act ion_name"); | |
214 | |
215 addActionBarItemDialogue.show(); | |
216 } | |
217 | |
218 private String[] getIntents() { | |
219 return new String[] {"Send", "Select Contact", "Add Event", "Camera"}; | |
220 } | |
221 | |
222 private void sendUISettingToUIActivity() { | |
223 Intent intent = new Intent(); | |
224 intent.putExtra("packageName", mPackageSpinner.getSelectedItem().toStrin g()); | |
225 intent.putExtra("color", mToolbarColorSpinner.getSelectedItem().toString ()); | |
226 intent.putExtra("itemsDescription", actionBarItemsDescription); | |
227 intent.putExtra("itemsImage", actionBarItemsImage); | |
228 intent.putExtra("itemsIntent", actionBarItemsIntent); | |
229 setResult(0, intent); | |
230 } | |
231 | |
232 private void saveActionBarItem() { | |
233 actionBarItemsDescription.add(((EditText) addActionBarItemDialogue | |
234 .findViewById(R.id.description)).getText().toString()); | |
235 actionBarItemsImage.add(imageInAddItemDialogue); | |
236 actionBarItemsIntent.add(((Spinner) addActionBarItemDialogue | |
237 .findViewById(R.id.intent)).getSelectedItem().toString()); | |
238 } | |
239 | |
240 private void selectUISetting() { | |
241 for (int position = 0; position < mToolbarColorSpinner.getAdapter().getC ount(); | |
242 position ++) { | |
243 if (mToolbarColorSpinner.getAdapter().getItem(position).equals(color )) { | |
244 mToolbarColorSpinner.setSelection(position); | |
245 break; | |
246 } | |
247 } | |
248 for (int position = 0; position < mPackageSpinner.getAdapter().getCount( ); position ++) { | |
249 if (mPackageSpinner.getAdapter().getItem(position).equals(packageNam e)) { | |
250 mPackageSpinner.setSelection(position); | |
251 break; | |
252 } | |
253 } | |
254 | |
255 displayActionBarItems(0, actionBarItemsDescription.size()); | |
256 } | |
257 } | |
OLD | NEW |