// Path: android/WALT/app/src/main/java/org/chromium/latency/walt/Utils.java
// static int getIntPreference(Context context, @StringRes int keyId, int defaultValue) {
//     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
//     return preferences.getInt(context.getString(keyId), defaultValue);
// }


import static org.chromium.latency.walt.Utils.getIntPreference;
import android.annotation.TargetApi;
import android.content.Context;
import android.media.midi.MidiDevice;
import android.media.midi.MidiDeviceInfo;
import android.media.midi.MidiInputPort;
import android.media.midi.MidiManager;
import android.media.midi.MidiOutputPort;
import android.media.midi.MidiReceiver;
import android.os.Handler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
/*
 * 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;




@TargetApi(23)
class MidiTest extends BaseTest {

    private Handler handler = new Handler();

    private static final String TEENSY_MIDI_NAME = "Teensyduino Teensy MIDI";
    private static final byte[] noteMsg = {(byte) 0x90, (byte) 99, (byte) 0};

    private MidiManager midiManager;
    private MidiDevice midiDevice;
    // Output and Input here are with respect to the MIDI device, not the Android device.
    private MidiOutputPort midiOutputPort;
    private MidiInputPort midiInputPort;
    private boolean isConnecting = false;
    private long last_tWalt = 0;
    private long last_tSys = 0;
    private long last_tJava = 0;
    private int inputSyncAfterRepetitions = 100;
    private int outputSyncAfterRepetitions = 20; // TODO: implement periodic clock sync for output
    private int inputRepetitions;
    private int outputRepetitions;
    private int repetitionsDone;
    private ArrayList<Double> deltasToSys = new ArrayList<>();
    ArrayList<Double> deltasInputTotal = new ArrayList<>();
    ArrayList<Double> deltasOutputTotal = new ArrayList<>();

    private static final int noteDelay = 300;
    private static final int timeout = 1000;

    MidiTest(Context context) {
        super(context);
// Path: android/WALT/app/src/main/java/org/chromium/latency/walt/Utils.java
// static int getIntPreference(Context context, @StringRes int keyId, int defaultValue) {
//     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
//     return preferences.getInt(context.getString(keyId), defaultValue);
// }


// Path: android/WALT/app/src/main/java/org/chromium/latency/walt/MidiTest.java
import static org.chromium.latency.walt.Utils.getIntPreference;
import android.annotation.TargetApi;
import android.content.Context;
import android.media.midi.MidiDevice;
import android.media.midi.MidiDeviceInfo;
import android.media.midi.MidiInputPort;
import android.media.midi.MidiManager;
import android.media.midi.MidiOutputPort;
import android.media.midi.MidiReceiver;
import android.os.Handler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

/*
 * 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;




@TargetApi(23)
class MidiTest extends BaseTest {

    private Handler handler = new Handler();

    private static final String TEENSY_MIDI_NAME = "Teensyduino Teensy MIDI";
    private static final byte[] noteMsg = {(byte) 0x90, (byte) 99, (byte) 0};

    private MidiManager midiManager;
    private MidiDevice midiDevice;
    // Output and Input here are with respect to the MIDI device, not the Android device.
    private MidiOutputPort midiOutputPort;
    private MidiInputPort midiInputPort;
    private boolean isConnecting = false;
    private long last_tWalt = 0;
    private long last_tSys = 0;
    private long last_tJava = 0;
    private int inputSyncAfterRepetitions = 100;
    private int outputSyncAfterRepetitions = 20; // TODO: implement periodic clock sync for output
    private int inputRepetitions;
    private int outputRepetitions;
    private int repetitionsDone;
    private ArrayList<Double> deltasToSys = new ArrayList<>();
    ArrayList<Double> deltasInputTotal = new ArrayList<>();
    ArrayList<Double> deltasOutputTotal = new ArrayList<>();

    private static final int noteDelay = 300;
    private static final int timeout = 1000;

    MidiTest(Context context) {
        super(context);
        inputRepetitions = getIntPreference(context, R.string.preference_midi_in_reps, 100);