Home | History | Annotate | Download | only in com.example.android.common.midi
      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.common.midi;
     18 
     19 import android.app.Activity;
     20 import android.media.midi.MidiDevice;
     21 import android.media.midi.MidiDeviceInfo;
     22 import android.media.midi.MidiManager;
     23 import android.media.midi.MidiOutputPort;
     24 import android.media.midi.MidiSender;
     25 import android.os.Handler;
     26 import android.os.Looper;
     27 import android.util.Log;
     28 
     29 import java.io.IOException;
     30 
     31 /**
     32  * Manages a Spinner for selecting a MidiOutputPort.
     33  */
     34 public class MidiOutputPortSelector extends MidiPortSelector {
     35     private MidiOutputPort mOutputPort;
     36     private MidiDispatcher mDispatcher = new MidiDispatcher();
     37     private MidiDevice mOpenDevice;
     38 
     39     /**
     40      * @param midiManager
     41      * @param activity
     42      * @param spinnerId ID from the layout resource
     43      */
     44     public MidiOutputPortSelector(MidiManager midiManager, Activity activity,
     45             int spinnerId) {
     46         super(midiManager, activity, spinnerId, MidiDeviceInfo.PortInfo.TYPE_OUTPUT);
     47     }
     48 
     49     @Override
     50     public void onPortSelected(final MidiPortWrapper wrapper) {
     51         Log.i(MidiConstants.TAG, "onPortSelected: " + wrapper);
     52         close();
     53 
     54         final MidiDeviceInfo info = wrapper.getDeviceInfo();
     55         if (info != null) {
     56             mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
     57 
     58                     @Override
     59                 public void onDeviceOpened(MidiDevice device) {
     60                     if (device == null) {
     61                         Log.e(MidiConstants.TAG, "could not open " + info);
     62                     } else {
     63                         mOpenDevice = device;
     64                         mOutputPort = device.openOutputPort(wrapper.getPortIndex());
     65                         if (mOutputPort == null) {
     66                             Log.e(MidiConstants.TAG,
     67                                     "could not open output port for " + info);
     68                             return;
     69                         }
     70                         mOutputPort.connect(mDispatcher);
     71                     }
     72                 }
     73             }, null);
     74             // Don't run the callback on the UI thread because openOutputPort might take a while.
     75         }
     76     }
     77 
     78     @Override
     79     public void onClose() {
     80         try {
     81             if (mOutputPort != null) {
     82                 mOutputPort.disconnect(mDispatcher);
     83             }
     84             mOutputPort = null;
     85             if (mOpenDevice != null) {
     86                 mOpenDevice.close();
     87             }
     88             mOpenDevice = null;
     89         } catch (IOException e) {
     90             Log.e(MidiConstants.TAG, "cleanup failed", e);
     91         }
     92     }
     93 
     94     /**
     95      * You can connect your MidiReceivers to this sender. The user will then select which output
     96      * port will send messages through this MidiSender.
     97      * @return a MidiSender that will send the messages from the selected port.
     98      */
     99     public MidiSender getSender() {
    100         return mDispatcher.getSender();
    101     }
    102 
    103 }
    104