aboutsummaryrefslogtreecommitdiff
path: root/android/WALT/app/src/main/java/org/chromium/latency/walt/AutoRunFragment.java
blob: f2f2a7fb60fe1989aa1805b65296d85bfce68371 (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.chromium.latency.walt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

public class AutoRunFragment extends Fragment {

    static final String TEST_ACTION = "org.chromium.latency.walt.START_TEST";
    static final String MODE_COLD = "Cold";

    private WaltDevice waltDevice;
    private AudioTest toTearDown; // TODO: figure out a better way to destroy the engine
    Handler handler = new Handler();

    private class AudioResultHandler implements ResultHandler {
        private FileWriter fileWriter;

        AudioResultHandler(String fileName) throws IOException {
            fileWriter = new FileWriter(fileName);
        }

        @Override
        public void onResult(Iterable[] results) {
            if (results.length == 0) {
                logger.log("Can't write empty data!");
                return;
            }
            logger.log("Writing data file");

            Iterator its[] = new Iterator[results.length];

            for (int i = 0; i < results.length; i++) {
                its[i] = results[i].iterator();
            }
            try {
                while (its[0].hasNext()) {
                    for (Iterator it : its) {
                        if (it.hasNext()) {
                            fileWriter.write(it.next().toString() + ",");
                        }
                    }
                    fileWriter.write("\n");
                }
            } catch (IOException e) {
                logger.log("Error writing output file: " + e.getMessage());
            } finally {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    logger.log("Error closing output file: " + e.getMessage());
                }
            }
        }
    }

    private void doTest(@NonNull Bundle args) {
        final int reps = args.getInt("Reps", 10);
        String fileName = args.getString("FileName", null);
        ResultHandler r = null;
        if (fileName != null) {
            try {
                r = new AudioResultHandler(fileName);
            } catch (IOException e) {
                logger.log("Unable to open output file " + e.getMessage());
                return;
            }
        }
        final String mode = args.getString("Mode", "");
        final ResultHandler resultHandler = r;
        Runnable testRunnable = null;
        switch (args.getString("TestType", "")) {
            case "MidiIn": {
                testRunnable = new Runnable() {
                    @Override
                    public void run() {
                        MidiTest midiTest = new MidiTest(getContext(), resultHandler);
                        midiTest.setInputRepetitions(reps);
                        midiTest.testMidiIn();
                    }
                };
                break;
            }
            case "MidiOut": {
                testRunnable = new Runnable() {
                    @Override
                    public void run() {
                        MidiTest midiTest = new MidiTest(getContext(), resultHandler);
                        midiTest.setOutputRepetitions(reps);
                        midiTest.testMidiOut();
                    }
                };
                break;
            }
            case "AudioIn": {
                testRunnable = new Runnable() {
                    @Override
                    public void run() {
                        AudioTest audioTest = new AudioTest(getContext(), resultHandler);
                        audioTest.setRecordingRepetitions(reps);
                        audioTest.setAudioMode(MODE_COLD.equals(mode) ?
                                AudioTest.AudioMode.COLD : AudioTest.AudioMode.CONTINUOUS);
                        audioTest.beginRecordingMeasurement();
                        toTearDown = audioTest;
                    }
                };
                break;
            }
            case "AudioOut": {
                final int period = args.getInt("Period", -1);
                testRunnable = new Runnable() {
                    @Override
                    public void run() {
                        AudioTest audioTest = new AudioTest(getContext(), resultHandler);
                        audioTest.setPlaybackRepetitions(reps);
                        audioTest.setAudioMode(MODE_COLD.equals(mode) ?
                                AudioTest.AudioMode.COLD : AudioTest.AudioMode.CONTINUOUS);
                        if (period > 0) {
                            audioTest.setPeriod(period);
                        } else {
                            audioTest.setPeriod(MODE_COLD.equals(mode) ?
                                    AudioTest.COLD_TEST_PERIOD : AudioTest.CONTINUOUS_TEST_PERIOD);
                        }
                        audioTest.beginPlaybackMeasurement();
                        toTearDown = audioTest;
                    }
                };
                break;
            }
        }

        // Not sure we need the handler.post() here, but just in case.
        final Runnable finalTestRunnable = testRunnable;
        waltDevice.setConnectionStateListener(new WaltConnection.ConnectionStateListener() {
            @Override
            public void onConnect() {
                handler.post(finalTestRunnable);
            }

            @Override
            public void onDisconnect() {}
        });

    }

    interface ResultHandler {
        void onResult(Iterable... r);
    }

    @Override
    public void onDestroyView() {
        if (toTearDown != null) {
            toTearDown.teardown();
        }
        super.onDestroyView();
    }

    private TextView txtLogAutoRun;
    private SimpleLogger logger;

    public AutoRunFragment() {
        // Required empty public constructor
    }

    private BroadcastReceiver logReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("message");
            txtLogAutoRun.append("\n" + msg);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        logger = SimpleLogger.getInstance(getContext());
        waltDevice = WaltDevice.getInstance(getContext());

        View view = inflater.inflate(R.layout.fragment_auto_run, container, false);

        Bundle args = getArguments();
        if (args != null) {
            doTest(args);
        }

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        txtLogAutoRun = (TextView) getActivity().findViewById(R.id.txt_log_auto_run);
        txtLogAutoRun.setMovementMethod(new ScrollingMovementMethod());
        txtLogAutoRun.setText(logger.getLogText());
        logger.registerReceiver(logReceiver);
    }

    @Override
    public void onPause() {
        logger.unregisterReceiver(logReceiver);
        super.onPause();
    }
}