aboutsummaryrefslogtreecommitdiff
path: root/android/src/org/linaro/glmark2/EditorActivity.java
blob: cae30a29394a077f4f7f7be4630127eaf2bf0195 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright © 2012 Linaro Limited
 *
 * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
 *
 * glmark2 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * glmark2.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *  Alexandros Frantzis
 */
package org.linaro.glmark2;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class EditorActivity extends Activity {
    public static final int DIALOG_SCENE_NAME_ID = 0;
    public static final int DIALOG_SCENE_OPTION_ID = 1;

    private EditorItemAdapter adapter;
    private ArrayList<SceneInfo> sceneInfoList;
    private String[] sceneNames;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor);

        /* Get information about the available scenes */
        sceneInfoList = getSceneInfoList();
        sceneNames = getSceneNames();

        /* Read information sent by the main activity */
        final int benchmarkPos = this.getIntent().getIntExtra("benchmark-pos", 0);
        String benchmarkText = getIntent().getStringExtra("benchmark-text");
        if (benchmarkText.isEmpty())
            benchmarkText = sceneNames[0];

        /* Set up the run button */
        Button runButton = (Button) findViewById(R.id.runButton);
        runButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(EditorActivity.this, Glmark2Activity.class);
                String args = "-b " + getBenchmarkDescriptionText();
                intent.putExtra("args", args);
                startActivity(intent);
            }
        });

        /* Set up the save button */
        Button button = (Button) findViewById(R.id.saveButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String newBenchmarkText = getBenchmarkDescriptionText();
                Intent intent = new Intent();
                intent.putExtra("benchmark-text", newBenchmarkText);
                intent.putExtra("benchmark-pos", benchmarkPos);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        /* Set up list view */
        ListView lv = (ListView) findViewById(R.id.editorListView);
        adapter = new EditorItemAdapter(this, R.layout.benchmark_item,
                                        getEditorItemList(benchmarkText));
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putInt("item-pos", position);
                /* Show the right dialog, depending on the clicked list position */
                if (position == 0)
                    showDialog(DIALOG_SCENE_NAME_ID, bundle);
                else
                    showDialog(DIALOG_SCENE_OPTION_ID, bundle);
            }
        });

        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parentView, View childView, int position, long id) {
                /* Reset the value of the long-clicked option */
                if (position > 0) {
                    EditorItem item = adapter.getItem(position);
                    item.value = null;
                    adapter.notifyDataSetChanged();
                }
                return true;
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle bundle) {
        final int itemPos = bundle.getInt("item-pos");
        Dialog dialog;
        final int finalId = id;

        switch (id) {
            case DIALOG_SCENE_NAME_ID:
                {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Pick a scene");
                builder.setItems(sceneNames, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        adapter.clear();
                        for (EditorItem ei: getEditorItemList(sceneNames[item]))
                            adapter.add(ei);
                        adapter.notifyDataSetChanged();
                        dismissDialog(DIALOG_SCENE_NAME_ID);
                    }
                });
                dialog = builder.create();
                }
                break;

            case DIALOG_SCENE_OPTION_ID:
                {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                final EditorItem item = adapter.getItem(itemPos);
                final EditText input = new EditText(this);
                if (item.value != null)
                    input.setText(item.value);

                input.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE ||
                            (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER &&
                             event.getAction() == KeyEvent.ACTION_UP))
                        {
                            item.value = v.getText().toString();
                            dismissDialog(DIALOG_SCENE_OPTION_ID);
                        }
                        return true;
                    }
                });
                builder.setTitle(item.option.name + ": " + item.option.description);
                dialog = builder.create();
                ((AlertDialog)dialog).setView(input, 15, 6, 15, 6);
                dialog.getWindow().setSoftInputMode(
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
                        WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
                        );

                }
                break;

            default:
                dialog = null;
                break;
        }

        if (dialog != null) {
            dialog.setOnDismissListener(new OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    removeDialog(finalId);
                }
            });
        }

        return dialog;
    }

    /**
     * Gets the value of an option.
     *
     * @param benchArray an array of option strings ("opt=val")
     * @param opt the options to get the value of
     *
     * @return the value or null
     */
    private String getOptionValue(String[] benchArray, String opt) {
        String ret = null;

        /* Search from the end to the beginning */
        for (int n = benchArray.length - 1; n >= 0; n--) {
            String s = benchArray[n].trim();
            if (s.startsWith(opt + "=")) {
                int i = s.indexOf('=');
                if (i >= 0 && i + 1 < s.length()) {
                    ret = s.substring(i + 1).trim();
                    break;
                }
            }
        }

        return ret;
    }

    /**
     * Gets the benchmark description string of the current editing state.
     *
     * @return the string
     */
    private String getBenchmarkDescriptionText() {
        String ret = "";

        for (int i = 0; i < adapter.getCount(); i++) {
            /* Convert each list item to a proper string representation */
            EditorItem item = adapter.getItem(i);
            String s = "";

            /*
             * Append "opt=" if this is an option item, except the
             * "__custom__" item.
             */
            if (item.option != null && item.value != null &&
                !item.option.name.equals("__custom__"))
            {
                s += item.option.name + "=";
            }

            /*
             * Append the item value if this is not "__custom__".
             */
            if (item.value != null && !item.value.equals("__custom__"))
                s += item.value;

            /*
             * Append ":" to the description string if needed.
             */
            if (!s.isEmpty() && !ret.isEmpty())
                ret += ":";

            /* Append the item representation */
            ret += s;
        }

        return ret;
    }

    /**
     * Creates an EditorItem list from a benchmark description string.
     *
     * @param benchDesc the benchmark description string
     *
     * @return the list
     */
    private ArrayList<EditorItem> getEditorItemList(String benchDesc) {
        String[] benchArray = benchDesc.split(":");
        String benchName = benchArray[0].trim();

        if (benchName.isEmpty())
            benchName = "__custom__";

        /* Find SceneInfo from name */
        SceneInfo sceneInfo = null;
        for (SceneInfo si: sceneInfoList) {
            if (si.name.equals(benchName)) {
                sceneInfo = si;
                break;
            }
        }

        /* If we couldn't find a matching SceneInfo, use __custom__ */
        if (sceneInfo == null) {
            for (SceneInfo si: sceneInfoList) {
                if (si.name.equals("__custom__")) {
                    sceneInfo = si;
                    break;
                }
            }
        }

        ArrayList<EditorItem> l = new ArrayList<EditorItem>();

        /* Append items to the list */
        if (!sceneInfo.name.equals("__custom__")) {
            /* Append scene name item */
            l.add(new EditorItem(null, sceneInfo.name));

            /* Append scene option items */
            for (SceneInfo.Option opt: sceneInfo.options)
                l.add(new EditorItem(opt, getOptionValue(benchArray, opt.name)));
        }
        else {
            String desc = new String(benchDesc);
            if (desc.startsWith("__custom__"))
                desc = "";

            /* Append scene name item */
            l.add(new EditorItem(null, sceneInfo.name));

            /* Append scene option items (only one for __custom__) */
            for (SceneInfo.Option opt: sceneInfo.options)
                l.add(new EditorItem(opt, desc));
        }

        return l;
    }

    /**
     * Gets a list of information about the available scenes.
     *
     * @return the list
     */
    private ArrayList<SceneInfo> getSceneInfoList() {
        ArrayList<SceneInfo> l = new ArrayList<SceneInfo>();
        SceneInfo customSceneInfo = new SceneInfo("__custom__");
        customSceneInfo.addOption("__custom__", "Custom benchmark string", "");

        for (Parcelable p: getIntent().getParcelableArrayExtra("scene-info"))
            l.add((SceneInfo)p);

        /* Add the "__custom__" SceneInfo */
        l.add(customSceneInfo);

        return l;
    }

    /**
     * Gets the array of scene names.
     *
     * @return the array
     */
    private String[] getSceneNames() {
        ArrayList<String> l = new ArrayList<String>();

        for (SceneInfo si: sceneInfoList) {
            if (!si.name.isEmpty())
                l.add(si.name);
        }

        String[] a = new String[0];
        return l.toArray(a);
    }


    static private class EditorItem {
        SceneInfo.Option option;

        public EditorItem(SceneInfo.Option o, String value) {
            this.option = o;
            this.value = value;
        }

        public String value;
    }

    /**
     * A ListView adapter that creates list item views from EditorItems
     */
    private class EditorItemAdapter extends ArrayAdapter<EditorItem> {
        static final int VIEW_TYPE_SCENE_NAME = 0;
        static final int VIEW_TYPE_SCENE_OPTION = 1;
        static final int VIEW_TYPE_COUNT = 2;

        public ArrayList<EditorItem> items;

        public EditorItemAdapter(Context context, int textViewResourceId,
                                 ArrayList<EditorItem> items)
        {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0)
                return VIEW_TYPE_SCENE_NAME;
            else
                return VIEW_TYPE_SCENE_OPTION;
        }

        @Override
        public int getViewTypeCount() {
            return VIEW_TYPE_COUNT;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == 0)
                return getViewScene(position, convertView);
            else
                return getViewOption(position, convertView);
        }

        private View getViewScene(int position, View convertView) {
            /* Get the view/widget to use */
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.benchmark_item, null);
            }

            EditorItem item = items.get(position);

            TextView title = (TextView) v.findViewById(R.id.title);
            TextView summary = (TextView) v.findViewById(R.id.summary);

            if (title != null)
                title.setText(item.value);
            if (summary != null)
                summary.setText("The scene to use");

            return v;
        }

        private View getViewOption(int position, View convertView) {
            /* Get the view/widget to use */
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.benchmark_item, null);
            }

            EditorItem item = items.get(position);

            TextView title = (TextView) v.findViewById(R.id.title);
            TextView summary = (TextView) v.findViewById(R.id.summary);
            boolean hasUserSetValue = item.value != null;
            String value = hasUserSetValue ? item.value : item.option.defaultValue;

            if (title != null) {
                /* If the option has been edited by the user show it with emphasis */
                SpannableString titleText = new SpannableString(item.option.name + " = " + value);
                ForegroundColorSpan span = new ForegroundColorSpan(hasUserSetValue ? Color.CYAN : Color.LTGRAY);
                titleText.setSpan(span, item.option.name.length() + " = ".length(), titleText.length(), 0);
                title.setText(titleText);
            }

            if (summary != null)
                summary.setText(item.option.description);

            return v;
        }
    }
}