1package org.skia.viewer;
5import android.view.LayoutInflater;
9import android.widget.AdapterView;
10import android.widget.BaseAdapter;
11import android.widget.CompoundButton;
13import android.widget.LinearLayout;
18import org.json.JSONArray;
19import org.json.JSONException;
20import org.json.JSONObject;
22import java.util.ArrayList;
34public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelectedListener {
35 private static final String NAME =
"name";
36 private static final String VALUE =
"value";
37 private static final String OPTIONS =
"options";
38 private static final String BACKEND_STATE_NAME =
"Backend";
39 private static final String REFRESH_STATE_NAME =
"Refresh";
40 private static final String ON =
"ON";
41 private static final String OFF =
"OFF";
42 private static final int FILTER_LENGTH = 20;
45 private LinearLayout mLayout;
46 private JSONArray mStateJson;
49 mViewerActivity = viewerActivity;
51 mStateJson =
new JSONArray(
"[{\"name\": \"Please\", " +
52 "\"value\": \"Initialize\", \"options\": []}]");
53 }
catch (JSONException
e) {
60 mStateJson =
new JSONArray(stateJson);
61 if (mLayout !=
null) {
64 notifyDataSetChanged();
66 }
catch (JSONException
e) {
88 public View
getView(
int position, View convertView, ViewGroup parent) {
91 if (mLayout ==
null) {
92 mLayout =
new LinearLayout(mViewerActivity);
93 mLayout.setOrientation(LinearLayout.VERTICAL);
104 private void populateView(JSONObject item, View view)
throws JSONException {
105 LinearLayout itemView = (LinearLayout) view;
106 TextView nameText = (TextView) itemView.findViewById(
R.id.nameText);
107 TextView valueText = (TextView) itemView.findViewById(
R.id.valueText);
108 Spinner optionSpinner = (Spinner) itemView.findViewById(
R.id.optionSpinner);
110 String
value = item.getString(VALUE);
111 itemView.setTag(item.toString());
112 itemView.setTag(
R.integer.value_tag_key,
value);
114 nameText.setText(item.getString(NAME));
116 JSONArray
options = item.getJSONArray(OPTIONS);
118 valueText.setText(
value);
119 valueText.setVisibility(View.VISIBLE);
120 optionSpinner.setVisibility(View.GONE);
122 ArrayList<String> optionList =
new ArrayList<>();
123 String[] optionStrings =
new String[
options.length()];
124 for (
int j = 0; j <
options.length(); j++) {
125 optionList.add(
options.getString(j));
127 final OptionAdapter adapter =
new OptionAdapter(mViewerActivity,
128 android.R.layout.simple_spinner_dropdown_item, optionList, optionSpinner);
129 adapter.setCurrentOption(
value);
130 optionSpinner.setAdapter(adapter);
131 if (optionStrings.length >= FILTER_LENGTH) {
132 View existingView = itemView.getChildAt(1);
133 if (!(existingView instanceof EditText)) {
134 EditText filterText =
new EditText(mViewerActivity);
135 filterText.setHint(
"Filter");
136 itemView.addView(filterText, 1);
137 filterText.addTextChangedListener(
new TextWatcher() {
139 public void beforeTextChanged(CharSequence
s,
int start,
int cnt,
144 public void onTextChanged(CharSequence
s,
int start,
int before,
int cnt) {
148 public void afterTextChanged(Editable
s) {
149 adapter.getFilter().filter(
s.toString());
154 optionSpinner.setSelection(optionList.indexOf(
value));
155 optionSpinner.setOnItemSelectedListener(
this);
156 optionSpinner.setVisibility(View.VISIBLE);
157 valueText.setVisibility(View.GONE);
160 private View inflateItemView(JSONObject item)
throws JSONException {
161 LinearLayout itemView = (LinearLayout)
162 LayoutInflater.from(mViewerActivity).inflate(
R.layout.state_item,
null);
163 populateView(item, itemView);
167 private void updateDrawer() {
169 if (mStateJson.length() < mLayout.getChildCount()) {
171 mStateJson.length(), mLayout.getChildCount() - mStateJson.length());
173 for (
int i = 0;
i < mStateJson.length();
i++) {
174 JSONObject stateObject = mStateJson.getJSONObject(
i);
175 View childView = mLayout.getChildAt(
i);
176 if (childView !=
null) {
177 if (stateObject.toString().equals(childView.getTag())) {
180 populateView(stateObject, childView);
183 mLayout.addView(inflateItemView(stateObject),
i);
186 }
catch (JSONException
e) {
192 public void onItemSelected(AdapterView<?> parent, View view,
int position,
long id) {
196 View stateItem = (View) parent.getParent();
197 String stateName = ((TextView) stateItem.findViewById(
R.id.nameText)).getText().toString();
198 String stateValue = ((TextView) view).getText().toString();
199 if (!stateValue.equals(stateItem.getTag(
R.integer.value_tag_key))) {
200 stateItem.setTag(
null);
207 final Object oldValue = stateItem.getTag(
R.integer.value_tag_key);
208 if (stateName.equals(BACKEND_STATE_NAME)
209 && oldValue !=
null && !stateValue.equals(oldValue)) {
210 LinearLayout mainLayout = (LinearLayout) mViewerActivity.findViewById(
R.id.mainLayout);
211 mainLayout.removeAllViews();
212 SurfaceView surfaceView =
new SurfaceView(mViewerActivity);
213 surfaceView.setId(
R.id.surfaceView);
214 surfaceView.getHolder().addCallback(mViewerActivity);
215 surfaceView.setOnTouchListener(mViewerActivity);
216 mainLayout.addView(surfaceView);
Object getItem(int position)
StateAdapter(ViewerActivity viewerActivity)
void setState(String stateJson)
View getView(int position, View convertView, ViewGroup parent)
long getItemId(int position)
void onItemSelected(AdapterView<?> parent, View view, int position, long id)
void onNothingSelected(AdapterView<?> parent)
void onStateChanged(String stateName, String stateValue)