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.MidiDeviceInfo; 21 import android.media.midi.MidiManager; 22 import android.util.Log; 23 24 import java.io.IOException; 25 26 /** 27 * Select an output port and connect it to a destination input port. 28 */ 29 public class MidiOutputPortConnectionSelector extends MidiPortSelector { 30 31 private MidiPortConnector mSynthConnector; 32 private MidiDeviceInfo mDestinationDeviceInfo; 33 private int mDestinationPortIndex; 34 private MidiPortConnector.OnPortsConnectedListener mConnectedListener; 35 36 /** 37 * @param midiManager 38 * @param activity 39 * @param spinnerId 40 * @param type 41 */ MidiOutputPortConnectionSelector(MidiManager midiManager, Activity activity, int spinnerId, MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex)42 public MidiOutputPortConnectionSelector(MidiManager midiManager, 43 Activity activity, int spinnerId, 44 MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) { 45 super(midiManager, activity, spinnerId, 46 MidiDeviceInfo.PortInfo.TYPE_OUTPUT); 47 mDestinationDeviceInfo = destinationDeviceInfo; 48 mDestinationPortIndex = destinationPortIndex; 49 } 50 51 @Override onPortSelected(final MidiPortWrapper wrapper)52 public void onPortSelected(final MidiPortWrapper wrapper) { 53 Log.i(MidiConstants.TAG, "connectPortToSynth: " + wrapper); 54 onClose(); 55 if (wrapper.getDeviceInfo() != null) { 56 mSynthConnector = new MidiPortConnector(mMidiManager); 57 mSynthConnector.connectToDevicePort(wrapper.getDeviceInfo(), 58 wrapper.getPortIndex(), mDestinationDeviceInfo, 59 mDestinationPortIndex, 60 // not safe on UI thread 61 mConnectedListener, null); 62 } 63 } 64 65 @Override onClose()66 public void onClose() { 67 try { 68 if (mSynthConnector != null) { 69 mSynthConnector.close(); 70 mSynthConnector = null; 71 } 72 } catch (IOException e) { 73 Log.e(MidiConstants.TAG, "Exception in closeSynthResources()", e); 74 } 75 } 76 77 /** 78 * @param myPortsConnectedListener 79 */ setConnectedListener( MidiPortConnector.OnPortsConnectedListener connectedListener)80 public void setConnectedListener( 81 MidiPortConnector.OnPortsConnectedListener connectedListener) { 82 mConnectedListener = connectedListener; 83 } 84 } 85