• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 
17 package org.chromium.latency.walt;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.support.annotation.NonNull;
25 import android.support.v4.app.Fragment;
26 import android.text.method.ScrollingMovementMethod;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.TextView;
31 
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.Iterator;
35 
36 public class AutoRunFragment extends Fragment {
37 
38     static final String TEST_ACTION = "org.chromium.latency.walt.START_TEST";
39     static final String MODE_COLD = "Cold";
40 
41     private WaltDevice waltDevice;
42     private AudioTest toTearDown; // TODO: figure out a better way to destroy the engine
43     Handler handler = new Handler();
44 
45     private class AudioResultHandler implements ResultHandler {
46         private FileWriter fileWriter;
47 
AudioResultHandler(String fileName)48         AudioResultHandler(String fileName) throws IOException {
49             fileWriter = new FileWriter(fileName);
50         }
51 
52         @Override
onResult(Iterable[] results)53         public void onResult(Iterable[] results) {
54             if (results.length == 0) {
55                 logger.log("Can't write empty data!");
56                 return;
57             }
58             logger.log("Writing data file");
59 
60             Iterator its[] = new Iterator[results.length];
61 
62             for (int i = 0; i < results.length; i++) {
63                 its[i] = results[i].iterator();
64             }
65             try {
66                 while (its[0].hasNext()) {
67                     for (Iterator it : its) {
68                         if (it.hasNext()) {
69                             fileWriter.write(it.next().toString() + ",");
70                         }
71                     }
72                     fileWriter.write("\n");
73                 }
74             } catch (IOException e) {
75                 logger.log("Error writing output file: " + e.getMessage());
76             } finally {
77                 try {
78                     fileWriter.close();
79                 } catch (IOException e) {
80                     logger.log("Error closing output file: " + e.getMessage());
81                 }
82             }
83         }
84     }
85 
doTest(@onNull Bundle args)86     private void doTest(@NonNull Bundle args) {
87         final int reps = args.getInt("Reps", 10);
88         String fileName = args.getString("FileName", null);
89         ResultHandler r = null;
90         if (fileName != null) {
91             try {
92                 r = new AudioResultHandler(fileName);
93             } catch (IOException e) {
94                 logger.log("Unable to open output file " + e.getMessage());
95                 return;
96             }
97         }
98         final String mode = args.getString("Mode", "");
99         final ResultHandler resultHandler = r;
100         Runnable testRunnable = null;
101         switch (args.getString("TestType", "")) {
102             case "MidiIn": {
103                 testRunnable = new Runnable() {
104                     @Override
105                     public void run() {
106                         MidiTest midiTest = new MidiTest(getContext(), resultHandler);
107                         midiTest.setInputRepetitions(reps);
108                         midiTest.testMidiIn();
109                     }
110                 };
111                 break;
112             }
113             case "MidiOut": {
114                 testRunnable = new Runnable() {
115                     @Override
116                     public void run() {
117                         MidiTest midiTest = new MidiTest(getContext(), resultHandler);
118                         midiTest.setOutputRepetitions(reps);
119                         midiTest.testMidiOut();
120                     }
121                 };
122                 break;
123             }
124             case "AudioIn": {
125                 testRunnable = new Runnable() {
126                     @Override
127                     public void run() {
128                         AudioTest audioTest = new AudioTest(getContext(), resultHandler);
129                         audioTest.setRecordingRepetitions(reps);
130                         audioTest.setAudioMode(MODE_COLD.equals(mode) ?
131                                 AudioTest.AudioMode.COLD : AudioTest.AudioMode.CONTINUOUS);
132                         audioTest.beginRecordingMeasurement();
133                         toTearDown = audioTest;
134                     }
135                 };
136                 break;
137             }
138             case "AudioOut": {
139                 final int period = args.getInt("Period", -1);
140                 testRunnable = new Runnable() {
141                     @Override
142                     public void run() {
143                         AudioTest audioTest = new AudioTest(getContext(), resultHandler);
144                         audioTest.setPlaybackRepetitions(reps);
145                         audioTest.setAudioMode(MODE_COLD.equals(mode) ?
146                                 AudioTest.AudioMode.COLD : AudioTest.AudioMode.CONTINUOUS);
147                         if (period > 0) {
148                             audioTest.setPeriod(period);
149                         } else {
150                             audioTest.setPeriod(MODE_COLD.equals(mode) ?
151                                     AudioTest.COLD_TEST_PERIOD : AudioTest.CONTINUOUS_TEST_PERIOD);
152                         }
153                         audioTest.beginPlaybackMeasurement();
154                         toTearDown = audioTest;
155                     }
156                 };
157                 break;
158             }
159         }
160 
161         // Not sure we need the handler.post() here, but just in case.
162         final Runnable finalTestRunnable = testRunnable;
163         waltDevice.setConnectionStateListener(new WaltConnection.ConnectionStateListener() {
164             @Override
165             public void onConnect() {
166                 handler.post(finalTestRunnable);
167             }
168 
169             @Override
170             public void onDisconnect() {}
171         });
172 
173     }
174 
175     interface ResultHandler {
onResult(Iterable... r)176         void onResult(Iterable... r);
177     }
178 
179     @Override
onDestroyView()180     public void onDestroyView() {
181         if (toTearDown != null) {
182             toTearDown.teardown();
183         }
184         super.onDestroyView();
185     }
186 
187     private TextView txtLogAutoRun;
188     private SimpleLogger logger;
189 
AutoRunFragment()190     public AutoRunFragment() {
191         // Required empty public constructor
192     }
193 
194     private BroadcastReceiver logReceiver = new BroadcastReceiver() {
195         @Override
196         public void onReceive(Context context, Intent intent) {
197             String msg = intent.getStringExtra("message");
198             txtLogAutoRun.append("\n" + msg);
199         }
200     };
201 
202     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)203     public View onCreateView(LayoutInflater inflater, ViewGroup container,
204                              Bundle savedInstanceState) {
205         super.onCreate(savedInstanceState);
206         logger = SimpleLogger.getInstance(getContext());
207         waltDevice = WaltDevice.getInstance(getContext());
208 
209         View view = inflater.inflate(R.layout.fragment_auto_run, container, false);
210 
211         Bundle args = getArguments();
212         if (args != null) {
213             doTest(args);
214         }
215 
216         return view;
217     }
218 
219     @Override
onResume()220     public void onResume() {
221         super.onResume();
222         txtLogAutoRun = (TextView) getActivity().findViewById(R.id.txt_log_auto_run);
223         txtLogAutoRun.setMovementMethod(new ScrollingMovementMethod());
224         txtLogAutoRun.setText(logger.getLogText());
225         logger.registerReceiver(logReceiver);
226     }
227 
228     @Override
onPause()229     public void onPause() {
230         logger.unregisterReceiver(logReceiver);
231         super.onPause();
232     }
233 }
234