Merge "MidiDevice: fix connectPorts for same Process" into nyc-dev

am: b2e4e98

* commit 'b2e4e98e7c2b54966b48945a0418187564d71085':
  MidiDevice: fix connectPorts for same Process

Change-Id: If2cd5fc8b7c4cd102be503ec6c99a76eee4801ba
This commit is contained in:
Phil Burk
2016-04-06 03:57:02 +00:00
committed by android-build-merger
3 changed files with 15 additions and 5 deletions

View File

@@ -28,7 +28,8 @@ interface IMidiDeviceServer
void closeDevice();
// connects the input port pfd to the specified output port
void connectPorts(IBinder token, in ParcelFileDescriptor pfd, int outputPortNumber);
// Returns the PID of the called process.
int connectPorts(IBinder token, in ParcelFileDescriptor pfd, int outputPortNumber);
MidiDeviceInfo getDeviceInfo();
void setDeviceInfo(in MidiDeviceInfo deviceInfo);

View File

@@ -19,6 +19,7 @@ package android.media.midi;
import android.os.Binder;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
@@ -181,9 +182,16 @@ public final class MidiDevice implements Closeable {
}
try {
IBinder token = new Binder();
mDeviceServer.connectPorts(token, pfd, outputPortNumber);
// close our copy of the file descriptor
IoUtils.closeQuietly(pfd);
int calleePid = mDeviceServer.connectPorts(token, pfd, outputPortNumber);
// If the service is a different Process then it will duplicate the pfd
// and we can safely close this one.
// But if the service is in the same Process then closing the pfd will
// kill the connection. So don't do that.
if (calleePid != Process.myPid()) {
// close our copy of the file descriptor
IoUtils.closeQuietly(pfd);
}
return new MidiConnection(token, inputPort);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in connectPorts");

View File

@@ -254,7 +254,7 @@ public final class MidiDeviceServer implements Closeable {
}
@Override
public void connectPorts(IBinder token, ParcelFileDescriptor pfd,
public int connectPorts(IBinder token, ParcelFileDescriptor pfd,
int outputPortNumber) {
MidiInputPort inputPort = new MidiInputPort(pfd, outputPortNumber);
MidiDispatcher dispatcher = mOutputPortDispatchers[outputPortNumber];
@@ -270,6 +270,7 @@ public final class MidiDeviceServer implements Closeable {
synchronized (mPortClients) {
mPortClients.put(token, client);
}
return Process.myPid(); // for caller to detect same process ID
}
@Override