Merge "MidiDevice: do not open ports on closed device" into nyc-dev
This commit is contained in:
@@ -42,6 +42,7 @@ public final class MidiDevice implements Closeable {
|
|||||||
private final IMidiManager mMidiManager;
|
private final IMidiManager mMidiManager;
|
||||||
private final IBinder mClientToken;
|
private final IBinder mClientToken;
|
||||||
private final IBinder mDeviceToken;
|
private final IBinder mDeviceToken;
|
||||||
|
private boolean mIsDeviceClosed;
|
||||||
|
|
||||||
private final CloseGuard mGuard = CloseGuard.get();
|
private final CloseGuard mGuard = CloseGuard.get();
|
||||||
|
|
||||||
@@ -123,6 +124,9 @@ public final class MidiDevice implements Closeable {
|
|||||||
* or null in case of failure.
|
* or null in case of failure.
|
||||||
*/
|
*/
|
||||||
public MidiInputPort openInputPort(int portNumber) {
|
public MidiInputPort openInputPort(int portNumber) {
|
||||||
|
if (mIsDeviceClosed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
IBinder token = new Binder();
|
IBinder token = new Binder();
|
||||||
ParcelFileDescriptor pfd = mDeviceServer.openInputPort(token, portNumber);
|
ParcelFileDescriptor pfd = mDeviceServer.openInputPort(token, portNumber);
|
||||||
@@ -146,6 +150,9 @@ public final class MidiDevice implements Closeable {
|
|||||||
* or null in case of failure.
|
* or null in case of failure.
|
||||||
*/
|
*/
|
||||||
public MidiOutputPort openOutputPort(int portNumber) {
|
public MidiOutputPort openOutputPort(int portNumber) {
|
||||||
|
if (mIsDeviceClosed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
IBinder token = new Binder();
|
IBinder token = new Binder();
|
||||||
ParcelFileDescriptor pfd = mDeviceServer.openOutputPort(token, portNumber);
|
ParcelFileDescriptor pfd = mDeviceServer.openOutputPort(token, portNumber);
|
||||||
@@ -175,12 +182,15 @@ public final class MidiDevice implements Closeable {
|
|||||||
if (outputPortNumber < 0 || outputPortNumber >= mDeviceInfo.getOutputPortCount()) {
|
if (outputPortNumber < 0 || outputPortNumber >= mDeviceInfo.getOutputPortCount()) {
|
||||||
throw new IllegalArgumentException("outputPortNumber out of range");
|
throw new IllegalArgumentException("outputPortNumber out of range");
|
||||||
}
|
}
|
||||||
|
if (mIsDeviceClosed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
ParcelFileDescriptor pfd = inputPort.claimFileDescriptor();
|
ParcelFileDescriptor pfd = inputPort.claimFileDescriptor();
|
||||||
if (pfd == null) {
|
if (pfd == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
IBinder token = new Binder();
|
IBinder token = new Binder();
|
||||||
int calleePid = mDeviceServer.connectPorts(token, pfd, outputPortNumber);
|
int calleePid = mDeviceServer.connectPorts(token, pfd, outputPortNumber);
|
||||||
// If the service is a different Process then it will duplicate the pfd
|
// If the service is a different Process then it will duplicate the pfd
|
||||||
@@ -202,11 +212,14 @@ public final class MidiDevice implements Closeable {
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
synchronized (mGuard) {
|
synchronized (mGuard) {
|
||||||
mGuard.close();
|
if (!mIsDeviceClosed) {
|
||||||
try {
|
mGuard.close();
|
||||||
mMidiManager.closeDevice(mClientToken, mDeviceToken);
|
mIsDeviceClosed = true;
|
||||||
} catch (RemoteException e) {
|
try {
|
||||||
Log.e(TAG, "RemoteException in closeDevice");
|
mMidiManager.closeDevice(mClientToken, mDeviceToken);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
Log.e(TAG, "RemoteException in closeDevice");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user