aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools
diff options
context:
space:
mode:
authorGravatar liyuqian <liyuqian@google.com>2016-06-08 11:17:09 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-06-08 11:17:09 -0700
commit2a437e64593ab597cf66d48b91cab0ab2ed76382 (patch)
treef4dab6fb906482e7f6d0f55fc96b898f8ca7dfc3 /platform_tools
parentecbc12b1c1c72de0cf7bba4a3f6a7cce4f43bf41 (diff)
Provide filter when there are many options
Diffstat (limited to 'platform_tools')
-rw-r--r--platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/OptionAdapter.java64
-rw-r--r--platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java29
2 files changed, 88 insertions, 5 deletions
diff --git a/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/OptionAdapter.java b/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/OptionAdapter.java
new file mode 100644
index 0000000000..4fefb54d8d
--- /dev/null
+++ b/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/OptionAdapter.java
@@ -0,0 +1,64 @@
+package org.skia.viewer;
+
+import android.content.Context;
+import android.widget.ArrayAdapter;
+import android.widget.Filter;
+import android.widget.Spinner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class OptionAdapter extends ArrayAdapter<String> {
+ private String mCurrentOption;
+ private List<String> mOptions;
+ private Filter mFilter = null;
+ private Spinner mSpinner;
+
+ public OptionAdapter(Context context, int resource, List<String> options, Spinner spinner) {
+ super(context, resource);
+ addAll(options);
+ mOptions = options;
+ mSpinner = spinner;
+ }
+
+ public void setCurrentOption(String currentOption) {
+ this.mCurrentOption = currentOption;
+ }
+
+ private class OptionFilter extends Filter {
+
+ @Override
+ protected FilterResults performFiltering(CharSequence constraint) {
+ final String pattern = constraint.toString().toLowerCase();
+ ArrayList<String> filteredOptions = new ArrayList<>();
+ for(String option : mOptions) {
+ if (option.equals(mCurrentOption)
+ || option.toLowerCase().indexOf(pattern) > -1) {
+ filteredOptions.add(option);
+ }
+ }
+ FilterResults results = new FilterResults();
+ results.values = filteredOptions;
+ results.count = filteredOptions.size();
+ return results;
+ }
+
+ @Override
+ protected void publishResults(CharSequence constraint, FilterResults results) {
+ clear();
+ List<String> filteredOptions = (List<String>) results.values;
+ addAll(filteredOptions);
+ // We set the selection to the current option to avoid unwanted option selection change
+ mSpinner.setSelection(filteredOptions.indexOf(mCurrentOption));
+ notifyDataSetChanged();
+ }
+ }
+
+ @Override
+ public Filter getFilter() {
+ if (mFilter == null) {
+ mFilter = new OptionFilter();
+ }
+ return mFilter;
+ }
+}
diff --git a/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java b/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java
index 5ee68efc3c..ba5407984b 100644
--- a/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java
+++ b/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java
@@ -1,12 +1,14 @@
package org.skia.viewer;
+import android.text.Editable;
+import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
-import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
+import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
@@ -32,6 +34,7 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
static final String VALUE = "value";
static final String OPTIONS = "options";
private static final String BACKEND_STATE_NAME = "Backend";
+ private static final int FILTER_LENGTH = 20;
ViewerActivity mViewerActivity;
LinearLayout mLayout;
@@ -86,7 +89,7 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
}
private View inflateItemView(JSONObject item) throws JSONException {
- View itemView = LayoutInflater.from(mViewerActivity).inflate(R.layout.state_item, null);
+ LinearLayout itemView = (LinearLayout) LayoutInflater.from(mViewerActivity).inflate(R.layout.state_item, null);
TextView nameText = (TextView) itemView.findViewById(R.id.nameText);
TextView valueText = (TextView) itemView.findViewById(R.id.valueText);
Spinner optionSpinner = (Spinner) itemView.findViewById(R.id.optionSpinner);
@@ -97,15 +100,31 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
valueText.setText(value);
valueText.setVisibility(View.VISIBLE);
optionSpinner.setVisibility(View.GONE);
-
} else {
ArrayList<String> optionList = new ArrayList<>();
String[] optionStrings = new String[options.length()];
for (int j = 0; j < options.length(); j++) {
optionList.add(options.getString(j));
}
- optionSpinner.setAdapter(new ArrayAdapter<String>(mViewerActivity,
- android.R.layout.simple_spinner_dropdown_item, optionList));
+ final OptionAdapter adapter = new OptionAdapter(mViewerActivity,
+ android.R.layout.simple_spinner_dropdown_item, optionList, optionSpinner);
+ adapter.setCurrentOption(value);
+ optionSpinner.setAdapter(adapter);
+ if (optionStrings.length >= FILTER_LENGTH) {
+ EditText filterText = new EditText(mViewerActivity);
+ filterText.setHint("Filter");
+ itemView.addView(filterText, 1);
+ filterText.addTextChangedListener(new TextWatcher() {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int cnt, int after) {}
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int cnt) {}
+ @Override
+ public void afterTextChanged(Editable s) {
+ adapter.getFilter().filter(s.toString());
+ }
+ });
+ }
optionSpinner.setSelection(optionList.indexOf(value));
optionSpinner.setOnItemSelectedListener(this);
optionSpinner.setVisibility(View.VISIBLE);