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 17 package com.example.android.midiscope; 18 19 import android.media.midi.MidiDeviceService; 20 import android.media.midi.MidiDeviceStatus; 21 import android.media.midi.MidiReceiver; 22 23 import com.example.android.common.midi.MidiFramer; 24 25 import java.io.IOException; 26 27 /** 28 * Virtual MIDI Device that logs messages to a ScopeLogger. 29 */ 30 31 public class MidiScope extends MidiDeviceService { 32 33 private static ScopeLogger mScopeLogger; 34 private MidiReceiver mInputReceiver = new MyReceiver(); 35 private static MidiFramer mDeviceFramer; 36 37 @Override 38 public MidiReceiver[] onGetInputPortReceivers() { 39 return new MidiReceiver[] { mInputReceiver }; 40 } 41 42 public static void setScopeLogger(ScopeLogger logger) { 43 if (logger != null) { 44 // Receiver that prints the messages. 45 LoggingReceiver loggingReceiver = new LoggingReceiver(logger); 46 mDeviceFramer = new MidiFramer(loggingReceiver); 47 } 48 mScopeLogger = logger; 49 } 50 51 private static class MyReceiver extends MidiReceiver { 52 @Override 53 public void onSend(byte[] data, int offset, int count, 54 long timestamp) throws IOException { 55 if (mScopeLogger != null) { 56 // Send raw data to be parsed into discrete messages. 57 mDeviceFramer.send(data, offset, count, timestamp); 58 } 59 } 60 } 61 62 /** 63 * This will get called when clients connect or disconnect. 64 * Log device information. 65 */ 66 @Override 67 public void onDeviceStatusChanged(MidiDeviceStatus status) { 68 if (mScopeLogger != null) { 69 if (status.isInputPortOpen(0)) { 70 mScopeLogger.log("=== connected ==="); 71 String text = MidiPrinter.formatDeviceInfo( 72 status.getDeviceInfo()); 73 mScopeLogger.log(text); 74 } else { 75 mScopeLogger.log("--- disconnected ---"); 76 } 77 } 78 } 79 } 80