MidiDevice.java revision 35110d1ed72ee7fe687125831b4dd91b87b515ee
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
17package android.media.midi;
18
19import android.content.Context;
20import android.content.ServiceConnection;
21import android.os.ParcelFileDescriptor;
22import android.os.RemoteException;
23import android.util.Log;
24
25import java.io.Closeable;
26import java.io.IOException;
27
28/**
29 * This class is used for sending and receiving data to and from an MIDI device
30 * Instances of this class are created by {@link MidiManager#openDevice}.
31 *
32 * CANDIDATE FOR PUBLIC API
33 * @hide
34 */
35public final class MidiDevice implements Closeable {
36    private static final String TAG = "MidiDevice";
37
38    private final MidiDeviceInfo mDeviceInfo;
39    private final IMidiDeviceServer mServer;
40    private Context mContext;
41    private ServiceConnection mServiceConnection;
42
43    /* package */ MidiDevice(MidiDeviceInfo deviceInfo, IMidiDeviceServer server) {
44        mDeviceInfo = deviceInfo;
45        mServer = server;
46        mContext = null;
47        mServiceConnection = null;
48    }
49
50    /* package */ MidiDevice(MidiDeviceInfo deviceInfo, IMidiDeviceServer server,
51            Context context, ServiceConnection serviceConnection) {
52        mDeviceInfo = deviceInfo;
53        mServer = server;
54        mContext = context;
55        mServiceConnection = serviceConnection;
56    }
57
58    /**
59     * Returns a {@link MidiDeviceInfo} object, which describes this device.
60     *
61     * @return the {@link MidiDeviceInfo} object
62     */
63    public MidiDeviceInfo getInfo() {
64        return mDeviceInfo;
65    }
66
67    /**
68     * Called to open a {@link MidiInputPort} for the specified port number.
69     *
70     * @param portNumber the number of the input port to open
71     * @return the {@link MidiInputPort}
72     */
73    public MidiInputPort openInputPort(int portNumber) {
74        try {
75            ParcelFileDescriptor pfd = mServer.openInputPort(portNumber);
76            if (pfd == null) {
77                return null;
78            }
79            return new MidiInputPort(pfd, portNumber);
80        } catch (RemoteException e) {
81            Log.e(TAG, "RemoteException in openInputPort");
82            return null;
83        }
84    }
85
86    /**
87     * Called to open a {@link MidiOutputPort} for the specified port number.
88     *
89     * @param portNumber the number of the output port to open
90     * @return the {@link MidiOutputPort}
91     */
92    public MidiOutputPort openOutputPort(int portNumber) {
93        try {
94            ParcelFileDescriptor pfd = mServer.openOutputPort(portNumber);
95            if (pfd == null) {
96                return null;
97            }
98            return new MidiOutputPort(pfd, portNumber);
99        } catch (RemoteException e) {
100            Log.e(TAG, "RemoteException in openOutputPort");
101            return null;
102        }
103    }
104
105    @Override
106    public void close() throws IOException {
107        if (mContext != null && mServiceConnection != null) {
108            mContext.unbindService(mServiceConnection);
109            mContext = null;
110            mServiceConnection = null;
111        }
112    }
113
114    @Override
115    public String toString() {
116        return ("MidiDevice: " + mDeviceInfo.toString());
117    }
118}
119