Home | History | Annotate | Download | only in com.example.android.midiscope
      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 ScopeLogger getScopeLogger() {
     43         return mScopeLogger;
     44     }
     45 
     46     public static void setScopeLogger(ScopeLogger logger) {
     47         if (logger != null) {
     48             // Receiver that prints the messages.
     49             LoggingReceiver loggingReceiver = new LoggingReceiver(logger);
     50             mDeviceFramer = new MidiFramer(loggingReceiver);
     51         }
     52         mScopeLogger = logger;
     53     }
     54 
     55     private static class MyReceiver extends MidiReceiver {
     56         @Override
     57         public void onSend(byte[] data, int offset, int count,
     58                 long timestamp) throws IOException {
     59             if (mScopeLogger != null) {
     60                 // Send raw data to be parsed into discrete messages.
     61                 mDeviceFramer.send(data, offset, count, timestamp);
     62             }
     63         }
     64     }
     65 
     66     /**
     67      * This will get called when clients connect or disconnect.
     68      * Log device information.
     69      */
     70     @Override
     71     public void onDeviceStatusChanged(MidiDeviceStatus status) {
     72         if (mScopeLogger != null) {
     73             if (status.isInputPortOpen(0)) {
     74                 mScopeLogger.log("=== connected ===");
     75                 String text = MidiPrinter.formatDeviceInfo(
     76                         status.getDeviceInfo());
     77                 mScopeLogger.log(text);
     78             } else {
     79                 mScopeLogger.log("--- disconnected ---");
     80             }
     81         }
     82     }
     83 }
     84