1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.rs.blasbenchmark;
18
19import android.view.Menu;
20import android.view.MenuItem;
21import android.view.MenuInflater;
22import android.text.method.ScrollingMovementMethod;
23
24import android.app.Activity;
25import android.os.Bundle;
26import android.os.Handler;
27import android.graphics.Point;
28import android.widget.AdapterView;
29import android.widget.ArrayAdapter;
30import android.widget.ToggleButton;
31import android.widget.TextView;
32import android.widget.CompoundButton;
33import android.widget.ListView;
34import android.view.View;
35import java.util.ArrayList;
36import java.util.ListIterator;
37import android.util.Log;
38import android.content.Intent;
39
40import android.os.Environment;
41import java.io.BufferedWriter;
42import java.io.File;
43import java.io.FileWriter;
44import java.io.IOException;
45
46public class BlasControls extends Activity {
47    private final String TAG = "BLAS";
48    public final String RESULT_FILE = "blas_benchmark_result.csv";
49
50    private ListView mTestListView;
51    private TextView mResultView;
52
53    private ArrayAdapter<String> mTestListAdapter;
54    private ArrayList<String> mTestList = new ArrayList<String>();
55
56    private boolean mSettings[] = {false, false};
57    private static final int SETTING_LONG_RUN = 0;
58    private static final int SETTING_PAUSE = 1;
59
60    private float mResults[];
61    private String mInfo[];
62
63    @Override
64    public boolean onCreateOptionsMenu(Menu menu) {
65        // Inflate the menu items for use in the action bar
66        MenuInflater inflater = getMenuInflater();
67        inflater.inflate(R.menu.main_activity_actions, menu);
68
69        return super.onCreateOptionsMenu(menu);
70    }
71
72    void init() {
73
74        for (int i=0; i < BlasTestList.TestName.values().length; i++) {
75            mTestList.add(BlasTestList.TestName.values()[i].toString());
76        }
77
78        mTestListView = (ListView) findViewById(R.id.test_list);
79        mTestListAdapter = new ArrayAdapter(this,
80                android.R.layout.simple_list_item_activated_1,
81                mTestList);
82
83        mTestListView.setAdapter(mTestListAdapter);
84        mTestListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
85        mTestListAdapter.notifyDataSetChanged();
86
87        mResultView = (TextView) findViewById(R.id.results);
88        mResultView.setMovementMethod(new ScrollingMovementMethod());
89    }
90
91    @Override
92    protected void onCreate(Bundle savedInstanceState) {
93        super.onCreate(savedInstanceState);
94        setContentView(R.layout.controls);
95        init();
96    }
97
98    @Override
99    protected void onPause() {
100        super.onPause();
101    }
102
103    @Override
104    protected void onResume() {
105        super.onResume();
106    }
107
108    Intent makeBasicLaunchIntent() {
109        Intent intent = new Intent(this, BlasBenchmark.class);
110        intent.putExtra("enable long", mSettings[SETTING_LONG_RUN]);
111        intent.putExtra("enable pause", mSettings[SETTING_PAUSE]);
112        return intent;
113    }
114
115    public void btnRun(View v) {
116        BlasTestList.TestName t[] = BlasTestList.TestName.values();
117        int count = 0;
118        for (int i = 0; i < t.length; i++) {
119            if (mTestListView.isItemChecked(i)) {
120                count++;
121            }
122        }
123        if (count == 0) {
124            return;
125        }
126
127        int testList[] = new int[count];
128        count = 0;
129        for (int i = 0; i < t.length; i++) {
130            if (mTestListView.isItemChecked(i)) {
131                testList[count++] = i;
132            }
133        }
134
135        Intent intent = makeBasicLaunchIntent();
136        intent.putExtra("tests", testList);
137        startActivityForResult(intent, 0);
138    }
139
140    private void writeResults() {
141        // write result into a file
142        File externalStorage = Environment.getExternalStorageDirectory();
143        if (!externalStorage.canWrite()) {
144            Log.v(TAG, "sdcard is not writable");
145            return;
146        }
147        File resultFile = new File(externalStorage, RESULT_FILE);
148        resultFile.setWritable(true, false);
149        try {
150            BufferedWriter rsWriter = new BufferedWriter(new FileWriter(resultFile));
151            Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath());
152            java.text.DecimalFormat df = new java.text.DecimalFormat("######.####");
153
154            for (int ct=0; ct < BlasTestList.TestName.values().length; ct++) {
155                String t = BlasTestList.TestName.values()[ct].toString();
156                final float r = mResults[ct];
157                String s = new String("" + t + ", " + df.format(r));
158                rsWriter.write(s + "\n");
159            }
160            rsWriter.close();
161        } catch (IOException e) {
162            Log.v(TAG, "Unable to write result file " + e.getMessage());
163        }
164    }
165
166    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
167        if (requestCode == 0) {
168            if (resultCode == RESULT_OK) {
169                java.text.DecimalFormat df = new java.text.DecimalFormat("######.####");
170                mResults = new float[BlasTestList.TestName.values().length];
171                mInfo = new String[BlasTestList.TestName.values().length];
172
173                float r[] = data.getFloatArrayExtra("results");
174                String inf[] = data.getStringArrayExtra("testinfo");
175                int id[] = data.getIntArrayExtra("tests");
176
177                String mOutResult = "";
178                for (int ct=0; ct < id.length; ct++) {
179                    String t = inf[ct];
180                    String sl = BlasTestList.TestName.values()[id[ct]].toString() + ":   " + df.format(r[ct]) + "ms";
181                    String s = t + ":   " + df.format(r[ct]) + "ms";
182                    mTestList.set(id[ct], sl);
183                    mTestListAdapter.notifyDataSetChanged();
184                    mOutResult += s + '\n';
185                    mResults[id[ct]] = r[ct];
186                }
187
188                mResultView.setText(mOutResult);
189                writeResults();
190            }
191        }
192    }
193
194    public void btnSelAll(View v) {
195        BlasTestList.TestName t[] = BlasTestList.TestName.values();
196        for (int i=0; i < t.length; i++) {
197            mTestListView.setItemChecked(i, true);
198        }
199    }
200
201    public boolean onOptionsItemSelected(MenuItem item) {
202        // Handle presses on the action bar items
203        switch(item.getItemId()) {
204            case R.id.action_settings:
205                BlasSettings newFragment = new BlasSettings(mSettings);
206                newFragment.show(getFragmentManager(), "settings");
207                return true;
208            default:
209                return super.onOptionsItemSelected(item);
210        }
211    }
212
213    public void btnSelNone(View v) {
214        BlasTestList.TestName t[] = BlasTestList.TestName.values();
215        for (int i=0; i < t.length; i++) {
216            mTestListView.setItemChecked(i, false);
217        }
218    }
219
220    public void btnSettings(View v) {
221        BlasSettings newFragment = new BlasSettings(mSettings);
222        newFragment.show(getFragmentManager(), "settings");
223    }
224}
225