aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java
blob: f82a7b66cd1b5fd2556966ed37f8570ca61a7ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/*
    The navigation drawer requires ListView, so we implemented this BaseAdapter for that ListView.
    However, the ListView does not provide good support for updating just a single child view.
    For example, a frequently changed child view such as FPS state will reset the spinner of
    all other child views; although I didn't change other child views and directly return
    the convertView in BaseAdapter.getView(int position, View convertView, ViewGroup parent).

    Therefore, our adapter only returns one LinearLayout for the ListView.
    Within that LinearLayout, we maintain views ourselves so we can efficiently update its children.
 */
public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelectedListener {
    private static final String NAME = "name";
    private static final String VALUE = "value";
    private static final String OPTIONS = "options";
    private static final String BACKEND_STATE_NAME = "Backend";
    private static final String FPS_STATE_NAME = "FPS";
    private static final String REFRESH_STATE_NAME = "Refresh";
    private static final String ON = "ON";
    private static final String OFF = "OFF";
    private static final int FILTER_LENGTH = 20;

    private ViewerActivity mViewerActivity;
    private LinearLayout mLayout;
    private JSONArray mStateJson;
    private TextView mFPSFloatText;

    public StateAdapter(ViewerActivity viewerActivity) {
        mViewerActivity = viewerActivity;
        mFPSFloatText = (TextView) viewerActivity.findViewById(R.id.fpsFloatText);
        try {
            mStateJson = new JSONArray("[{\"name\": \"Please\", " +
                    "\"value\": \"Initialize\", \"options\": []}]");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void setState(String stateJson) {
        try {
            mStateJson = new JSONArray(stateJson);
            if (mLayout != null) {
                updateDrawer();
            } else {
                notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // The first list item is the mLayout that contains a list of state items
    // The second list item is the toggle for float FPS
    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (position) {
            case 0: {
                if (mLayout == null) {
                    mLayout = new LinearLayout(mViewerActivity);
                    mLayout.setOrientation(LinearLayout.VERTICAL);
                    updateDrawer();
                }
                return mLayout;
            }
            case 1: {
                View view = LayoutInflater.from(mViewerActivity).inflate(R.layout.fps_toggle, null);
                Switch theSwitch = (Switch) view.findViewById(R.id.theSwitch);
                theSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        mFPSFloatText.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
                        // Quickly set the bool fRefresh in native Viewer app for continuous refresh
                        mViewerActivity.onStateChanged(REFRESH_STATE_NAME, isChecked ? ON : OFF);
                    }
                });
                return view;
            }
            default: {
                return null;
            }
        }
    }

    private void populateView(JSONObject item, View view) throws JSONException {
        LinearLayout itemView = (LinearLayout) view;
        TextView nameText = (TextView) itemView.findViewById(R.id.nameText);
        TextView valueText = (TextView) itemView.findViewById(R.id.valueText);
        Spinner optionSpinner = (Spinner) itemView.findViewById(R.id.optionSpinner);

        String value = item.getString(VALUE);
        itemView.setTag(item.toString()); // To save unnecessary view update
        itemView.setTag(R.integer.value_tag_key, value);

        nameText.setText(item.getString(NAME));

        if (nameText.getText().equals(FPS_STATE_NAME) && mFPSFloatText != null) {
            mFPSFloatText.setText(value);
            // Don't show FPS in the drawer. We'll show it in the float text.
            itemView.setVisibility(View.GONE);
        }

        JSONArray options = item.getJSONArray(OPTIONS);
        if (options.length() == 0) {
            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));
            }
            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) {
                View existingView = itemView.getChildAt(1);
                if (!(existingView instanceof EditText)) {
                    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);
            valueText.setVisibility(View.GONE);
        }
    }
    private View inflateItemView(JSONObject item) throws JSONException {
        LinearLayout itemView = (LinearLayout)
                LayoutInflater.from(mViewerActivity).inflate(R.layout.state_item, null);
        populateView(item, itemView);
        return itemView;
    }

    private void updateDrawer() {
        try {
            if (mStateJson.length() < mLayout.getChildCount()) {
                mLayout.removeViews(
                        mStateJson.length(), mLayout.getChildCount() - mStateJson.length());
            }
            for (int i = 0; i < mStateJson.length(); i++) {
                JSONObject stateObject = mStateJson.getJSONObject(i);
                View childView = mLayout.getChildAt(i);
                if (childView != null) {
                    if (stateObject.toString().equals(childView.getTag())) {
                        continue; // No update, reuse the old view and skip the remaining step
                    } else {
                        populateView(stateObject, childView);
                    }
                } else {
                    mLayout.addView(inflateItemView(stateObject), i);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (view == null) {
            return;
        }
        View stateItem = (View) parent.getParent();
        String stateName = ((TextView) stateItem.findViewById(R.id.nameText)).getText().toString();
        String stateValue = ((TextView) view).getText().toString();
        if (!stateValue.equals(stateItem.getTag(R.integer.value_tag_key))) {
            stateItem.setTag(null); // Reset the tag to let updateDrawer update this item view.
            mViewerActivity.onStateChanged(stateName, stateValue);
        }

        // Due to the current Android limitation, we're required to recreate the SurfaceView for
        // switching to/from the Raster backend.
        // (Although we can switch between GPU backend without recreating the SurfaceView.)
        final Object oldValue = stateItem.getTag(R.integer.value_tag_key);
        if (stateName.equals(BACKEND_STATE_NAME)
                && oldValue != null && !stateValue.equals(oldValue)) {
            LinearLayout mainLayout = (LinearLayout) mViewerActivity.findViewById(R.id.mainLayout);
            mainLayout.removeAllViews();
            SurfaceView surfaceView = new SurfaceView(mViewerActivity);
            surfaceView.setId(R.id.surfaceView);
            surfaceView.getHolder().addCallback(mViewerActivity);
            surfaceView.setOnTouchListener(mViewerActivity);
            mainLayout.addView(surfaceView);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // do nothing
    }
}