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..095e4b7d1e56b1d47b0b46bd0e03bd2787b61847 |
| --- /dev/null |
| +++ b/demos/src/main/java/org/chromium/customtabsdemos/AdvancedUISettingActivity.java |
| @@ -0,0 +1,260 @@ |
| +// 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; |
|
Ian Wen
2016/02/10 02:36:06
Add one line before #14.
BigBossZhiling
2016/02/17 05:18:09
Done.
|
| + |
| +import android.app.Dialog; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.graphics.Bitmap; |
| +import android.net.Uri; |
| +import android.os.Bundle; |
| +import android.provider.MediaStore; |
| +import android.support.v7.app.AppCompatActivity; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.widget.ArrayAdapter; |
| +import android.widget.Button; |
| +import android.widget.EditText; |
| +import android.widget.ImageView; |
| +import android.widget.LinearLayout; |
| +import android.widget.Spinner; |
| +import android.widget.TextView; |
| +import org.chromium.customtabsclient.shared.CustomTabsHelper; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +/** |
| + * An activity for advanced UI settings. |
| + */ |
| +public class AdvancedUISettingActivity extends AppCompatActivity implements View.OnClickListener{ |
| + public static final String KEY_PACKAGES_LIST = "packageNameList"; |
| + public static final String KEY_PACKAGE = "packageName"; |
| + public static final String KEY_COLOR = "color"; |
| + public static final String KEY_DESCRIPTIONS = "itemsDescription"; |
| + public static final String KEY_INTENTS = "itemsIntent"; |
| + public static final String KEY_IMAGES = "itemsImage"; |
| + |
| + 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 Dialog mAddActionBarItemDialog; |
| + |
| + private Uri mImage; |
| + 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.
|
| + private ArrayList<Uri> mImages; |
| + private ArrayList<String> mIntents; |
| + /** |
| + * Puts uri, instead of Bitmap in intent to save space. |
| + */ |
| + static Bitmap uriToBitMap(Context context, Uri uri) { |
| + 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 = (List<String>) getIntent().getSerializableExtra(KEY_PACKAGES_LIST); |
| + mPackageName = getIntent().getStringExtra(KEY_PACKAGE); |
| + mColor = getIntent().getStringExtra(KEY_COLOR); |
| + 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.
|
| + .getSerializableExtra(KEY_DESCRIPTIONS); |
| + mIntents = (ArrayList<String>) getIntent() |
| + .getSerializableExtra(KEY_INTENTS); |
| + mImages = (ArrayList<Uri>) getIntent() |
| + .getSerializableExtra(KEY_IMAGES); |
| + mSaveButton.setOnClickListener(this); |
| + mAddActionBarItemButton.setOnClickListener(this); |
| + mToolbarColorSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.row, |
| + R.id.item, |
| + CustomTabsHelper.getColors())); |
| + |
|
Ian Wen
2016/02/10 02:36:06
Remove phantom empty lines.
BigBossZhiling
2016/02/17 05:18:09
Done.
|
| + mPackageSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.row, |
| + R.id.item, mPackageNameList)); |
| + |
| + // 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.
|
| + // If first time, select default UI setting from Custom UI Activity. |
| + selectUISetting(); |
| + } |
| + |
| + @Override |
| + public void onClick(View view) { |
| + switch (view.getId()) { |
| + 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.
|
| + sendUISettingToUIActivity(); |
| + finish(); |
| + break; |
| + case R.id.add_actionbar_item: |
| + 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.
|
| + break; |
| + case R.id.save_item: |
| + saveActionBarItem(); |
| + // Only add the last item to the UI. |
| + displayActionBarItems(mDescriptions.size() - 1, |
| + mDescriptions.size()); |
| + mAddActionBarItemDialog.dismiss(); |
| + break; |
| + case R.id.cancel: |
| + mAddActionBarItemDialog.dismiss(); |
| + break; |
| + case R.id.select_file: |
| + selectFile(); |
| + break; |
| + case R.id.item_delete: |
| + deleteItem(view); |
| + 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(); |
| + mImage = uri; |
| + ImageView image = (ImageView) mAddActionBarItemDialog |
| + .findViewById(R.id.imageInDialogue); |
| + image.setImageBitmap(uriToBitMap(this, mImage)); |
| + image.requestLayout(); |
| + } catch (Exception e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Displays items stored in the arraylist from index startIndex to endIndex. |
| + * @param startIndex is the starting index. |
| + * @param endIndex is the ending index. |
| + */ |
| + 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.
|
| + LayoutInflater inflater = (LayoutInflater)getBaseContext() |
| + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| + LinearLayout itemsList = (LinearLayout) findViewById(R.id.actionbar_items); |
| + |
| + for (int i = startIndex; i < endIndex; i++) { |
| + View item = inflater.inflate(R.layout.display_action_bar_item_setting, null); |
| + ((TextView) item.findViewById(R.id.item_description)) |
| + .setText("Description: " + mDescriptions.get(i)); |
| + ((TextView) item.findViewById(R.id.item_intent)) |
| + .setText("Intent: " + mIntents.get(i)); |
| + ((TextView) item.findViewById(R.id.item_image_title)).setText("Image: "); |
| + ImageView image = (ImageView) item.findViewById(R.id.item_image); |
| + image.setImageBitmap(uriToBitMap(this, mImages.get(i))); |
| + Button deleteButton = (Button) item.findViewById(R.id.item_delete); |
| + deleteButton.setOnClickListener(this); |
| + itemsList.addView(item); |
| + } |
| + } |
| + |
| + 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); |
| + mDescriptions.remove(index); |
| + mImages.remove(index); |
| + mIntents.remove(index); |
| + } |
| + |
| + private int getIndexOfLayout(LinearLayout item, LinearLayout setting) { |
| + int kidsPos; |
| + for (kidsPos = 0; kidsPos < setting.getChildCount(); kidsPos ++) { |
| + View view = setting.getChildAt(kidsPos); |
| + |
|
Ian Wen
2016/02/10 02:36:06
Remove phantom empty lines.
BigBossZhiling
2016/02/17 05:18:09
Done.
|
| + if (view == item) { |
| + break; |
| + } |
| + } |
| + return kidsPos; |
| + } |
| + |
| + private void showActionBarItemDialogue() { |
|
Ian Wen
2016/02/10 02:36:06
Dialog.
BigBossZhiling
2016/02/17 05:18:09
Done.
|
| + mAddActionBarItemDialog = new Dialog(this, R.style.DialogTheme); |
| + mAddActionBarItemDialog.setContentView(R.layout.dialogue_add_actionbar_item); |
| + mAddActionBarItemDialog.setTitle("Add Action Bar Item"); |
| + mAddActionBarItemDialog.findViewById(R.id.save_item).setOnClickListener(this); |
| + mAddActionBarItemDialog.findViewById(R.id.cancel).setOnClickListener(this); |
| + mAddActionBarItemDialog.findViewById(R.id.select_file).setOnClickListener(this); |
| + ((Spinner) mAddActionBarItemDialog.findViewById(R.id.intent)) |
| + .setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.item, getIntents())); |
| + mImage = Uri.parse( |
| + "android.resource://org.chromium.customtabsdemos/drawable/ic_action_name"); |
| + |
| + mAddActionBarItemDialog.show(); |
| + } |
| + |
| + private String[] getIntents() { |
| + 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.
|
| + } |
| + |
| + private void sendUISettingToUIActivity() { |
| + Intent intent = new Intent(); |
| + intent.putExtra(KEY_PACKAGE, mPackageSpinner.getSelectedItem().toString()); |
| + intent.putExtra(KEY_COLOR, mToolbarColorSpinner.getSelectedItem().toString()); |
| + intent.putExtra(KEY_DESCRIPTIONS, mDescriptions); |
| + intent.putExtra(KEY_IMAGES, mImages); |
| + intent.putExtra(KEY_INTENTS, mIntents); |
| + 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.
|
| + } |
| + |
| + private void saveActionBarItem() { |
| + mDescriptions.add(((EditText) mAddActionBarItemDialog |
| + .findViewById(R.id.description)).getText().toString()); |
| + mImages.add(mImage); |
| + mIntents.add(((Spinner) mAddActionBarItemDialog |
| + .findViewById(R.id.intent)).getSelectedItem().toString()); |
| + } |
| + |
| + 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(0, mDescriptions.size()); |
| + } |
| +} |