• 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.support.v4.app.Fragment;
24 import android.text.method.ScrollingMovementMethod;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 
30 import java.util.Locale;
31 
32 public class MidiFragment extends Fragment
33         implements View.OnClickListener, BaseTest.TestStateListener {
34 
35     private SimpleLogger logger;
36     private TextView textView;
37     private View startMidiInButton;
38     private View startMidiOutButton;
39     private HistogramChart latencyChart;
40     private MidiTest midiTest;
41 
MidiFragment()42     public MidiFragment() {
43         // Required empty public constructor
44     }
45 
46     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)47     public View onCreateView(LayoutInflater inflater, ViewGroup container,
48                              Bundle savedInstanceState) {
49         logger = SimpleLogger.getInstance(getContext());
50         midiTest = new MidiTest(getActivity());
51         midiTest.setTestStateListener(this);
52 
53         final View view = inflater.inflate(R.layout.fragment_midi, container, false);
54         textView = (TextView) view.findViewById(R.id.txt_box_midi);
55         startMidiInButton = view.findViewById(R.id.button_start_midi_in);
56         startMidiOutButton = view.findViewById(R.id.button_start_midi_out);
57         latencyChart = (HistogramChart) view.findViewById(R.id.latency_chart);
58         textView.setMovementMethod(new ScrollingMovementMethod());
59         return view;
60     }
61 
62     @Override
onResume()63     public void onResume() {
64         super.onResume();
65 
66         // Register this fragment class as the listener for some button clicks
67         startMidiInButton.setOnClickListener(this);
68         startMidiOutButton.setOnClickListener(this);
69 
70         textView.setText(logger.getLogText());
71         logger.registerReceiver(logReceiver);
72     }
73 
74     @Override
onPause()75     public void onPause() {
76         logger.unregisterReceiver(logReceiver);
77         super.onPause();
78     }
79 
80     @Override
onClick(View v)81     public void onClick(View v) {
82         switch (v.getId()) {
83             case R.id.button_start_midi_in:
84                 disableButtons();
85                 latencyChart.setVisibility(View.VISIBLE);
86                 latencyChart.clearData();
87                 latencyChart.setLegendEnabled(false);
88                 latencyChart.getBarChart().getDescription().setText("MIDI Input Latency [ms]");
89                 midiTest.testMidiIn();
90                 break;
91             case R.id.button_start_midi_out:
92                 disableButtons();
93                 latencyChart.setVisibility(View.VISIBLE);
94                 latencyChart.clearData();
95                 latencyChart.setLegendEnabled(false);
96                 latencyChart.getBarChart().getDescription().setText("MIDI Output Latency [ms]");
97                 midiTest.testMidiOut();
98                 break;
99         }
100     }
101 
disableButtons()102     private void disableButtons() {
103         startMidiInButton.setEnabled(false);
104         startMidiOutButton.setEnabled(false);
105     }
106 
107     private BroadcastReceiver logReceiver = new BroadcastReceiver() {
108         @Override
109         public void onReceive(Context context, Intent intent) {
110             String msg = intent.getStringExtra("message");
111             textView.append(msg + "\n");
112         }
113     };
114 
115     @Override
onTestStopped()116     public void onTestStopped() {
117         if (!midiTest.deltasOutputTotal.isEmpty()) {
118             latencyChart.setLegendEnabled(true);
119             latencyChart.setLabel(String.format(
120                     Locale.US, "Median=%.1f ms", Utils.median(midiTest.deltasOutputTotal)));
121         } else if (!midiTest.deltasInputTotal.isEmpty()) {
122             latencyChart.setLegendEnabled(true);
123             latencyChart.setLabel(String.format(
124                     Locale.US, "Median=%.1f ms", Utils.median(midiTest.deltasInputTotal)));
125         }
126         LogUploader.uploadIfAutoEnabled(getContext());
127         startMidiInButton.setEnabled(true);
128         startMidiOutButton.setEnabled(true);
129     }
130 
131     @Override
onTestStoppedWithError()132     public void onTestStoppedWithError() {
133         onTestStopped();
134         latencyChart.setVisibility(View.GONE);
135     }
136 
137     @Override
onTestPartialResult(double value)138     public void onTestPartialResult(double value) {
139         latencyChart.addEntry(value);
140     }
141 
hasMidi(Context context)142     public static boolean hasMidi(Context context) {
143         return context.getPackageManager().
144                 hasSystemFeature("android.software.midi");
145     }
146 }
147