Chromium Code Reviews| Index: demos/src/main/java/org/chromium/customtabsdemos/AdvancedUISettingActivity.java |
| diff --git a/demos/src/main/java/org/chromium/customtabsdemos/AdvancedUISettingActivity.java b/demos/src/main/java/org/chromium/customtabsdemos/AdvancedUISettingActivity.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..51026b4c5ff0c02597345c0d479b1fea46b26621 |
| --- /dev/null |
| +++ b/demos/src/main/java/org/chromium/customtabsdemos/AdvancedUISettingActivity.java |
| @@ -0,0 +1,307 @@ |
| +// Copyright 2016 Google Inc. All Rights Reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +package org.chromium.customtabsdemos; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.graphics.Bitmap; |
| +import android.graphics.Color; |
| +import android.net.Uri; |
| +import android.os.Bundle; |
| +import android.provider.MediaStore; |
| +import android.support.v4.app.NavUtils; |
| +import android.support.v7.app.AppCompatActivity; |
| +import android.view.LayoutInflater; |
| +import android.view.MenuItem; |
| +import android.view.View; |
| +import android.widget.ArrayAdapter; |
| +import android.widget.Button; |
| +import android.widget.ImageView; |
| +import android.widget.LinearLayout; |
| +import android.widget.Spinner; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.customtabsclient.shared.CustomTabsHelper; |
| + |
| +import java.io.Serializable; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +/** |
| + * An activity for advanced UI settings. |
| + */ |
| +public class AdvancedUISettingActivity extends AppCompatActivity implements View.OnClickListener{ |
| + static final String KEY_PACKAGE = "packageName"; |
| + static final String KEY_COLOR = "color"; |
| + static final String KEY_ACTION_ITEMS = "actionBarItems"; |
| + static final String[] INTENTS_STRINGS = {"Send", "Select Contact", "Add Event", "Camera"}; |
| + |
| + private static final int PICK_IMAGE_REQUEST = 1; |
| + private Spinner mToolbarColorSpinner; |
| + private Spinner mPackageSpinner; |
| + private Button mSaveButton; |
| + private Button mAddActionBarItemButton; |
| + private List<String> mPackageNameList; |
| + private String mPackageName; |
| + private String mColor; |
| + private int mActionBarItemIndex; |
| + private ArrayList<ActionButtonParams> mActionBarItems; |
| + |
| + /** |
| + * Convert uri to bitmap. Put uri, instead of Bitmap in intent to save space, |
| + * so we have to convert it back. |
| + */ |
| + 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
|
| + try { |
| + return MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri); |
| + } catch (Exception e) { |
| + return null; |
| + } |
| + } |
| + |
| + @Override |
| + protected void onCreate(Bundle savedInstanceState) { |
| + super.onCreate(savedInstanceState); |
| + setContentView(R.layout.advanced_ui_setting); |
| + mToolbarColorSpinner = (Spinner) findViewById(R.id.spinner_color); |
| + mPackageSpinner = (Spinner) findViewById(R.id.spinner_package); |
| + mSaveButton = (Button) findViewById(R.id.save_setting); |
| + mAddActionBarItemButton = (Button) findViewById(R.id.add_actionbar_item); |
| + mPackageNameList = CustomTabsHelper.getAllPackagesSupportingCustomTabs(this); |
| + mPackageName = (String) getIntent().getSerializableExtra(KEY_PACKAGE); |
|
Ian Wen
2016/03/21 18:46:28
Again, like what I said in the previous patch, use
|
| + mColor = (String) getIntent().getSerializableExtra(KEY_COLOR); |
| + mActionBarItems = (ArrayList<ActionButtonParams>) getIntent() |
| + .getSerializableExtra(KEY_ACTION_ITEMS); |
| + mSaveButton.setOnClickListener(this); |
| + mAddActionBarItemButton.setOnClickListener(this); |
| + mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, |
| + android.R.layout.simple_spinner_dropdown_item, getColors())); |
| + mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, |
| + android.R.layout.simple_spinner_dropdown_item, mPackageNameList)); |
| + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| + selectUISetting(); |
| + } |
| + |
| + @Override |
| + public void onClick(View view) { |
| + switch (view.getId()) { |
| + case R.id.save_setting: |
| + sendUISettingToUIActivity(); |
| + finish(); |
| + break; |
| + case R.id.add_actionbar_item: |
| + ActionButtonParams actionB = createNewActionBarItem(); |
| + displayActionBarItem(actionB.description, actionB.intent, actionB.image); |
| + break; |
| + case R.id.item_delete: |
| + deleteItem(view); |
| + break; |
| + case R.id.select_file: |
| + mActionBarItemIndex = getActionItemIndex(view); |
| + selectFile(); |
| + break; |
| + } |
| + } |
| + |
| + @Override |
| + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| + super.onActivityResult(requestCode, resultCode, data); |
| + if ((requestCode == PICK_IMAGE_REQUEST)) { |
| + try { |
| + Uri uri = data.getData(); |
| + mActionBarItems.get(mActionBarItemIndex).setImage(uri.toString()); |
| + updateImage(mActionBarItemIndex, uri); |
| + } catch (Exception e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + } |
| + |
| + @Override |
| + public boolean onOptionsItemSelected(MenuItem item) { |
| + switch (item.getItemId()) { |
| + // Respond to the action bar's Up/Home button |
| + case android.R.id.home: |
| + finish(); |
| + return true; |
| + } |
| + return super.onOptionsItemSelected(item); |
| + } |
| + |
| + /** |
| + * @return All possible colors. |
| + */ |
| + private String[] getColors() { |
| + ArrayList<String> fieldList = new ArrayList<String>(); |
| + String colorName; |
| + for (int i = 0; i < Color.class.getFields().length; i ++) { |
| + colorName = Color.class.getFields()[i].getName(); |
| + // Make only the first character capitalized. |
| + fieldList.add(i, colorName.charAt(0) + colorName.substring(1).toLowerCase()); |
|
Ian Wen
2016/03/21 18:46:28
I would remove #151 and #152. Let's focus on more
|
| + } |
| + return fieldList.toArray(new String[fieldList.size()]); |
| + } |
| + |
| + private void updateImage(int index, Uri image) { |
| + LinearLayout item = (LinearLayout) ((LinearLayout) this.findViewById(R.id.actionbar_items)) |
| + .getChildAt(index); |
| + ((ImageView) item.findViewById(R.id.item_image)).setImageURI(image); |
| + } |
| + |
| + private void displayActionBarItem(String description, String intent, String image) { |
| + LayoutInflater inflater = (LayoutInflater)getBaseContext() |
| + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| + LinearLayout itemsList = (LinearLayout) findViewById(R.id.actionbar_items); |
| + View item = inflater.inflate(R.layout.display_action_bar_item_setting, null); |
| + item.findViewById(R.id.select_file).setOnClickListener(this); |
| + item.findViewById(R.id.item_delete).setOnClickListener(this); |
| + Spinner intents = ((Spinner) item.findViewById(R.id.item_intent)); |
| + intents.setAdapter(new ArrayAdapter<String>(this, |
| + android.R.layout.simple_spinner_dropdown_item, INTENTS_STRINGS)); |
| + |
| + ((TextView) item.findViewById(R.id.item_description)).setText(description); |
| + |
| + for (int index = 0, count = ((Spinner) item.findViewById(R.id.item_intent)).getCount(); |
| + index < count; ++index) |
| + { |
|
Ian Wen
2016/03/21 18:46:28
Code format incorrect! :( Please push #178 to #177
|
| + if (intents.getAdapter().getItem(index).equals(intent)) |
| + { |
| + intents.setSelection(index); |
| + } |
| + } |
| + ((ImageView) item.findViewById(R.id.item_image)).setImageURI( |
| + Uri.parse(image)); |
|
Ian Wen
2016/03/21 18:46:28
Push #185 to #184
|
| + |
| + itemsList.addView(item); |
| + } |
| + |
| + private ActionButtonParams createNewActionBarItem() { |
| + ActionButtonParams button = new ActionButtonParams(); |
| + mActionBarItems.add(button); |
| + return button; |
| + } |
| + |
| + /** |
| + * Displays action bar items. |
| + */ |
| + private void displayActionBarItems() { |
| + for (ActionButtonParams buttonParam : mActionBarItems) { |
| + displayActionBarItem(buttonParam.description, buttonParam.intent, buttonParam.image); |
| + } |
| + } |
| + |
| + private void selectFile() { |
| + Intent intent = new Intent(); |
| + intent.setType("image/*"); |
| + intent.setAction(Intent.ACTION_OPEN_DOCUMENT); |
| + startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); |
| + } |
| + |
| + private void deleteItem(View deleteButton) { |
| + LinearLayout lineLayout = (LinearLayout) deleteButton.getParent().getParent(); |
| + LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); |
| + LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); |
| + int index = getIndexOfLayout(itemLayout, itemsLayout); |
| + itemsLayout.removeView(itemLayout); |
| + mActionBarItems.remove(index); |
| + } |
| + |
| + private int getIndexOfLayout(LinearLayout item, LinearLayout items) { |
| + int kidsPos; |
| + for (kidsPos = 0; kidsPos < items.getChildCount(); kidsPos ++) { |
| + View view = items.getChildAt(kidsPos); |
| + if (view == item) { |
| + break; |
| + } |
| + } |
| + return kidsPos; |
| + } |
| + |
| + private int getActionItemIndex(View button) { |
| + LinearLayout lineLayout = (LinearLayout) button.getParent().getParent(); |
| + LinearLayout itemLayout = (LinearLayout) lineLayout.getParent(); |
| + LinearLayout itemsLayout = (LinearLayout) itemLayout.getParent(); |
| + return getIndexOfLayout(itemLayout, itemsLayout); |
| + } |
| + |
| + private void sendUISettingToUIActivity() { |
| + saveActionBarItems(); |
| + Intent intent = new Intent(); |
| + intent.putExtra(KEY_PACKAGE, mPackageSpinner.getSelectedItem().toString()); |
| + intent.putExtra(KEY_COLOR, mToolbarColorSpinner.getSelectedItem().toString()); |
| + intent.putExtra(KEY_ACTION_ITEMS, mActionBarItems); |
| + setResult(0, intent); |
| + } |
| + |
| + private void saveActionBarItems() { |
| + LinearLayout items = (LinearLayout) this.findViewById(R.id.actionbar_items); |
| + int index = 0; |
| + while (index < items.getChildCount()) { |
| + System.out.println("trying to save!!!"); |
| + LinearLayout item = (LinearLayout) items.getChildAt(index); |
| + System.out.println( |
| + ((TextView) item.findViewById(R.id.item_description)).getText().toString()); |
| + mActionBarItems.get(index).setDescription( |
| + ((TextView) item.findViewById(R.id.item_description)).getText().toString()); |
| + mActionBarItems.get(index).setIntent( |
| + ((Spinner) item.findViewById(R.id.item_intent)).getSelectedItem().toString()); |
| + index ++; |
| + } |
| + } |
| + |
| + /** |
| + * 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.
|
| + * If first time, select default UI setting from Custom UI Activity. |
| + */ |
| + private void selectUISetting() { |
| + for (int position = 0; position < mToolbarColorSpinner.getAdapter().getCount(); |
| + position ++) { |
| + if (mToolbarColorSpinner.getAdapter().getItem(position).equals(mColor)) { |
| + mToolbarColorSpinner.setSelection(position); |
| + break; |
| + } |
| + } |
| + for (int position = 0; position < mPackageSpinner.getAdapter().getCount(); position ++) { |
| + if (mPackageSpinner.getAdapter().getItem(position).equals(mPackageName)) { |
| + mPackageSpinner.setSelection(position); |
| + break; |
| + } |
| + } |
| + |
| + displayActionBarItems(); |
| + } |
| +} |
| + |
| +class ActionButtonParams implements Serializable{ |
| + // Default values for action button. |
| + String description = "Great Feature"; |
| + String intent = "Send"; |
| + String image = "android.resource://org.chromium.customtabsdemos/drawable/ic_action_name";; |
| + |
| + public ActionButtonParams setImage(String image) { |
| + this.image = image; |
|
Ian Wen
2016/03/21 18:46:28
Remove "this." Same for all other places.
|
| + return this; |
| + } |
| + |
| + public ActionButtonParams setIntent(String intent) { |
| + this.intent = intent; |
| + return this; |
| + } |
| + |
| + public ActionButtonParams setDescription(String description) { |
| + this.description = description; |
| + return this; |
| + } |
| +} |