1 /*
2  * Copyright (C) 2014 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.mobileer.miditools;
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     public final static String TAG = "MidiOutputPortConnectionSelector";
31     private MidiPortConnector mSynthConnector;
32     private MidiDeviceInfo mDestinationDeviceInfo;
33     private int mDestinationPortIndex;
34     private MidiPortWrapper mLastWrapper;
35     private MidiPortConnector.OnPortsConnectedListener mConnectedListener;
36 
37     /**
38      * Create a selector for connecting to the destination input port.
39      *
40      * @param midiManager
41      * @param activity
42      * @param spinnerId
43      * @param destinationDeviceInfo
44      * @param destinationPortIndex
45      */
MidiOutputPortConnectionSelector(MidiManager midiManager, Activity activity, int spinnerId, MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex)46     public MidiOutputPortConnectionSelector(MidiManager midiManager,
47             Activity activity, int spinnerId,
48             MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) {
49         super(midiManager, activity, spinnerId,
50                 MidiDeviceInfo.PortInfo.TYPE_OUTPUT);
51         mDestinationDeviceInfo = destinationDeviceInfo;
52         mDestinationPortIndex = destinationPortIndex;
53     }
54 
55     @Override
onPortSelected(final MidiPortWrapper wrapper)56     public void onPortSelected(final MidiPortWrapper wrapper) {
57         if(!wrapper.equals(mLastWrapper)) {
58             onClose();
59             if (wrapper.getDeviceInfo() != null) {
60                 mSynthConnector = new MidiPortConnector(mMidiManager);
61                 mSynthConnector.connectToDevicePort(wrapper.getDeviceInfo(),
62                         wrapper.getPortIndex(), mDestinationDeviceInfo,
63                         mDestinationPortIndex,
64                         // not safe on UI thread
65                         mConnectedListener, null);
66             }
67         }
68         mLastWrapper = wrapper;
69     }
70 
71     @Override
onClose()72     public void onClose() {
73         try {
74             if (mSynthConnector != null) {
75                 mSynthConnector.close();
76                 mSynthConnector = null;
77             }
78         } catch (IOException e) {
79             Log.e(MidiConstants.TAG, "Exception in closeSynthResources()", e);
80         }
81         super.onClose();
82     }
83 
84     /**
85      * @param connectedListener
86      */
setConnectedListener( MidiPortConnector.OnPortsConnectedListener connectedListener)87     public void setConnectedListener(
88             MidiPortConnector.OnPortsConnectedListener connectedListener) {
89         mConnectedListener = connectedListener;
90     }
91 }
92