Chromium Code Reviews| 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", "Camera"}; | |
| 50 | |
| 51 private static final int PICK_IMAGE_REQUEST = 1; | |
| 52 private Spinner mToolbarColorSpinner; | |
| 53 private Spinner mPackageSpinner; | |
| 54 private Button mSaveButton; | |
| 55 private Button mAddActionBarItemButton; | |
| 56 private List<String> mPackageNameList; | |
| 57 private String mPackageName; | |
| 58 private String mColor; | |
| 59 private int mActionBarItemIndex; | |
| 60 private ArrayList<ActionButtonParams> mActionBarItems; | |
| 61 | |
| 62 /** | |
| 63 * Convert uri to bitmap. Put uri, instead of Bitmap in intent to save space , | |
| 64 * so we have to convert it back. | |
| 65 */ | |
| 66 static Bitmap uriToBitmap(Context context, Uri uri) { | |
|
Ian Wen
2016/03/21 18:46:28
1. Why is it a package method? If this method is o
| |
| 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 = CustomTabsHelper.getAllPackagesSupportingCustomTabs(t his); | |
| 83 mPackageName = (String) getIntent().getSerializableExtra(KEY_PACKAGE); | |
|
Ian Wen
2016/03/21 18:46:28
Again, like what I said in the previous patch, use
| |
| 84 mColor = (String) getIntent().getSerializableExtra(KEY_COLOR); | |
| 85 mActionBarItems = (ArrayList<ActionButtonParams>) getIntent() | |
| 86 .getSerializableExtra(KEY_ACTION_ITEMS); | |
| 87 mSaveButton.setOnClickListener(this); | |
| 88 mAddActionBarItemButton.setOnClickListener(this); | |
| 89 mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, | |
| 90 android.R.layout.simple_spinner_dropdown_item, getColors())); | |
| 91 mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, | |
| 92 android.R.layout.simple_spinner_dropdown_item, mPackageNameList) ); | |
| 93 getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
| 94 selectUISetting(); | |
| 95 } | |
| 96 | |
| 97 @Override | |
| 98 public void onClick(View view) { | |
| 99 switch (view.getId()) { | |
| 100 case R.id.save_setting: | |
| 101 sendUISettingToUIActivity(); | |
| 102 finish(); | |
| 103 break; | |
| 104 case R.id.add_actionbar_item: | |
| 105 ActionButtonParams actionB = createNewActionBarItem(); | |
| 106 displayActionBarItem(actionB.description, actionB.intent, action B.image); | |
| 107 break; | |
| 108 case R.id.item_delete: | |
| 109 deleteItem(view); | |
| 110 break; | |
| 111 case R.id.select_file: | |
| 112 mActionBarItemIndex = getActionItemIndex(view); | |
| 113 selectFile(); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 @Override | |
| 119 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| 120 super.onActivityResult(requestCode, resultCode, data); | |
| 121 if ((requestCode == PICK_IMAGE_REQUEST)) { | |
| 122 try { | |
| 123 Uri uri = data.getData(); | |
| 124 mActionBarItems.get(mActionBarItemIndex).setImage(uri.toString() ); | |
| 125 updateImage(mActionBarItemIndex, uri); | |
| 126 } catch (Exception e) { | |
| 127 e.printStackTrace(); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 @Override | |
| 133 public boolean onOptionsItemSelected(MenuItem item) { | |
| 134 switch (item.getItemId()) { | |
| 135 // Respond to the action bar's Up/Home button | |
| 136 case android.R.id.home: | |
| 137 finish(); | |
| 138 return true; | |
| 139 } | |
| 140 return super.onOptionsItemSelected(item); | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * @return All possible colors. | |
| 145 */ | |
| 146 private String[] getColors() { | |
| 147 ArrayList<String> fieldList = new ArrayList<String>(); | |
| 148 String colorName; | |
| 149 for (int i = 0; i < Color.class.getFields().length; i ++) { | |
| 150 colorName = Color.class.getFields()[i].getName(); | |
| 151 // Make only the first character capitalized. | |
| 152 fieldList.add(i, colorName.charAt(0) + colorName.substring(1).toLowe rCase()); | |
|
Ian Wen
2016/03/21 18:46:28
I would remove #151 and #152. Let's focus on more
| |
| 153 } | |
| 154 return fieldList.toArray(new String[fieldList.size()]); | |
| 155 } | |
| 156 | |
| 157 private void updateImage(int index, Uri image) { | |
| 158 LinearLayout item = (LinearLayout) ((LinearLayout) this.findViewById(R.i d.actionbar_items)) | |
| 159 .getChildAt(index); | |
| 160 ((ImageView) item.findViewById(R.id.item_image)).setImageURI(image); | |
| 161 } | |
| 162 | |
| 163 private void displayActionBarItem(String description, String intent, String image) { | |
| 164 LayoutInflater inflater = (LayoutInflater)getBaseContext() | |
| 165 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
| 166 LinearLayout itemsList = (LinearLayout) findViewById(R.id.actionbar_item s); | |
| 167 View item = inflater.inflate(R.layout.display_action_bar_item_setting, n ull); | |
| 168 item.findViewById(R.id.select_file).setOnClickListener(this); | |
| 169 item.findViewById(R.id.item_delete).setOnClickListener(this); | |
| 170 Spinner intents = ((Spinner) item.findViewById(R.id.item_intent)); | |
| 171 intents.setAdapter(new ArrayAdapter<String>(this, | |
| 172 android.R.layout.simple_spinner_dropdown_item, INTENTS_STRINGS)) ; | |
| 173 | |
| 174 ((TextView) item.findViewById(R.id.item_description)).setText(descriptio n); | |
| 175 | |
| 176 for (int index = 0, count = ((Spinner) item.findViewById(R.id.item_inten t)).getCount(); | |
| 177 index < count; ++index) | |
| 178 { | |
|
Ian Wen
2016/03/21 18:46:28
Code format incorrect! :( Please push #178 to #177
| |
| 179 if (intents.getAdapter().getItem(index).equals(intent)) | |
| 180 { | |
| 181 intents.setSelection(index); | |
| 182 } | |
| 183 } | |
| 184 ((ImageView) item.findViewById(R.id.item_image)).setImageURI( | |
| 185 Uri.parse(image)); | |
|
Ian Wen
2016/03/21 18:46:28
Push #185 to #184
| |
| 186 | |
| 187 itemsList.addView(item); | |
| 188 } | |
| 189 | |
| 190 private ActionButtonParams createNewActionBarItem() { | |
| 191 ActionButtonParams button = new ActionButtonParams(); | |
| 192 mActionBarItems.add(button); | |
| 193 return button; | |
| 194 } | |
| 195 | |
| 196 /** | |
| 197 * Displays action bar items. | |
| 198 */ | |
| 199 private void displayActionBarItems() { | |
| 200 for (ActionButtonParams buttonParam : mActionBarItems) { | |
| 201 displayActionBarItem(buttonParam.description, buttonParam.intent, bu ttonParam.image); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 private void selectFile() { | |
| 206 Intent intent = new Intent(); | |
| 207 intent.setType("image/*"); | |
| 208 intent.setAction(Intent.ACTION_OPEN_DOCUMENT); | |
| 209 startActivityForResult(Intent.createChooser(intent, "Select Picture"), P ICK_IMAGE_REQUEST); | |
| 210 } | |
| 211 | |
| 212 private void deleteItem(View deleteButton) { | |
| 213 LinearLayout lineLayout = (LinearLayout) deleteButton.getParent().getPar ent(); | |
| 214 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
| 215 LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); | |
| 216 int index = getIndexOfLayout(itemLayout, itemsLayout); | |
| 217 itemsLayout.removeView(itemLayout); | |
| 218 mActionBarItems.remove(index); | |
| 219 } | |
| 220 | |
| 221 private int getIndexOfLayout(LinearLayout item, LinearLayout items) { | |
| 222 int kidsPos; | |
| 223 for (kidsPos = 0; kidsPos < items.getChildCount(); kidsPos ++) { | |
| 224 View view = items.getChildAt(kidsPos); | |
| 225 if (view == item) { | |
| 226 break; | |
| 227 } | |
| 228 } | |
| 229 return kidsPos; | |
| 230 } | |
| 231 | |
| 232 private int getActionItemIndex(View button) { | |
| 233 LinearLayout lineLayout = (LinearLayout) button.getParent().getParent(); | |
| 234 LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); | |
| 235 LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); | |
| 236 return getIndexOfLayout(itemLayout, itemsLayout); | |
| 237 } | |
| 238 | |
| 239 private void sendUISettingToUIActivity() { | |
| 240 saveActionBarItems(); | |
| 241 Intent intent = new Intent(); | |
| 242 intent.putExtra(KEY_PACKAGE, mPackageSpinner.getSelectedItem().toString( )); | |
| 243 intent.putExtra(KEY_COLOR, mToolbarColorSpinner.getSelectedItem().toStri ng()); | |
| 244 intent.putExtra(KEY_ACTION_ITEMS, mActionBarItems); | |
| 245 setResult(0, intent); | |
| 246 } | |
| 247 | |
| 248 private void saveActionBarItems() { | |
| 249 LinearLayout items = (LinearLayout) this.findViewById(R.id.actionbar_ite ms); | |
| 250 int index = 0; | |
| 251 while (index < items.getChildCount()) { | |
| 252 System.out.println("trying to save!!!"); | |
| 253 LinearLayout item = (LinearLayout) items.getChildAt(index); | |
| 254 System.out.println( | |
| 255 ((TextView) item.findViewById(R.id.item_description)).getTex t().toString()); | |
| 256 mActionBarItems.get(index).setDescription( | |
| 257 ((TextView) item.findViewById(R.id.item_description)).getTex t().toString()); | |
| 258 mActionBarItems.get(index).setIntent( | |
| 259 ((Spinner) item.findViewById(R.id.item_intent)).getSelectedI tem().toString()); | |
| 260 index ++; | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 /** | |
| 265 * Select UI setting previously set by the user. | |
|
Ian Wen
2016/03/21 18:46:28
s/Select/Selects
Same for all javadocs you write.
| |
| 266 * If first time, select default UI setting from Custom UI Activity. | |
| 267 */ | |
| 268 private void selectUISetting() { | |
| 269 for (int position = 0; position < mToolbarColorSpinner.getAdapter().getC ount(); | |
| 270 position ++) { | |
| 271 if (mToolbarColorSpinner.getAdapter().getItem(position).equals(mColo r)) { | |
| 272 mToolbarColorSpinner.setSelection(position); | |
| 273 break; | |
| 274 } | |
| 275 } | |
| 276 for (int position = 0; position < mPackageSpinner.getAdapter().getCount( ); position ++) { | |
| 277 if (mPackageSpinner.getAdapter().getItem(position).equals(mPackageNa me)) { | |
| 278 mPackageSpinner.setSelection(position); | |
| 279 break; | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 displayActionBarItems(); | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 class ActionButtonParams implements Serializable{ | |
| 288 // Default values for action button. | |
| 289 String description = "Great Feature"; | |
| 290 String intent = "Send"; | |
| 291 String image = "android.resource://org.chromium.customtabsdemos/drawable/ic_ action_name";; | |
| 292 | |
| 293 public ActionButtonParams setImage(String image) { | |
| 294 this.image = image; | |
|
Ian Wen
2016/03/21 18:46:28
Remove "this." Same for all other places.
| |
| 295 return this; | |
| 296 } | |
| 297 | |
| 298 public ActionButtonParams setIntent(String intent) { | |
| 299 this.intent = intent; | |
| 300 return this; | |
| 301 } | |
| 302 | |
| 303 public ActionButtonParams setDescription(String description) { | |
| 304 this.description = description; | |
| 305 return this; | |
| 306 } | |
| 307 } | |
| OLD | NEW |