Merge changes from topics "presubmit-am-9b6163649ab04af9ae8747b484e284d0", "presubmit-am-f3bd2f18f3ff4c72b82bb741b56d0b5e" into sc-v2-dev
* changes: Cleanup in EmulatorClipboardMonitor (4) Cleanup in EmulatorClipboardMonitor (3)
This commit is contained in:
committed by
Android (Google) Code Review
commit
84efddcaaf
@@ -82,11 +82,8 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openPipe() throws InterruptedException {
|
private static FileDescriptor openPipe() throws InterruptedException {
|
||||||
FileDescriptor fd = getPipeFD();
|
FileDescriptor fd = openPipeImpl();
|
||||||
|
|
||||||
if (fd == null) {
|
|
||||||
fd = openPipeImpl();
|
|
||||||
|
|
||||||
// There's no guarantee that QEMU pipes will be ready at the moment
|
// There's no guarantee that QEMU pipes will be ready at the moment
|
||||||
// this method is invoked. We simply try to get the pipe open and
|
// this method is invoked. We simply try to get the pipe open and
|
||||||
@@ -95,32 +92,21 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
fd = openPipeImpl();
|
fd = openPipeImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
setPipeFD(fd);
|
private static byte[] receiveMessage(final FileDescriptor fd) throws ErrnoException,
|
||||||
}
|
InterruptedIOException, EOFException {
|
||||||
|
|
||||||
private synchronized void closePipe() {
|
|
||||||
try {
|
|
||||||
final FileDescriptor fd = mPipe;
|
|
||||||
mPipe = null;
|
|
||||||
if (fd != null) {
|
|
||||||
Os.close(fd);
|
|
||||||
}
|
|
||||||
} catch (ErrnoException ignore) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] receiveMessage() throws ErrnoException, InterruptedIOException, EOFException {
|
|
||||||
final byte[] lengthBits = new byte[4];
|
final byte[] lengthBits = new byte[4];
|
||||||
readFully(mPipe, lengthBits, 0, lengthBits.length);
|
readFully(fd, lengthBits, 0, lengthBits.length);
|
||||||
|
|
||||||
final ByteBuffer bb = ByteBuffer.wrap(lengthBits);
|
final ByteBuffer bb = ByteBuffer.wrap(lengthBits);
|
||||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
final int msgLen = bb.getInt();
|
final int msgLen = bb.getInt();
|
||||||
|
|
||||||
final byte[] msg = new byte[msgLen];
|
final byte[] msg = new byte[msgLen];
|
||||||
readFully(mPipe, msg, 0, msg.length);
|
readFully(fd, msg, 0, msg.length);
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
@@ -139,11 +125,16 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
|
|
||||||
EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) {
|
EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) {
|
||||||
this.mHostMonitorThread = new Thread(() -> {
|
this.mHostMonitorThread = new Thread(() -> {
|
||||||
|
FileDescriptor fd = null;
|
||||||
|
|
||||||
while (!Thread.interrupted()) {
|
while (!Thread.interrupted()) {
|
||||||
try {
|
try {
|
||||||
openPipe();
|
if (fd == null) {
|
||||||
|
fd = openPipe();
|
||||||
|
setPipeFD(fd);
|
||||||
|
}
|
||||||
|
|
||||||
final byte[] receivedData = receiveMessage();
|
final byte[] receivedData = receiveMessage(fd);
|
||||||
|
|
||||||
final String str = new String(receivedData);
|
final String str = new String(receivedData);
|
||||||
final ClipData clip = new ClipData("host clipboard",
|
final ClipData clip = new ClipData("host clipboard",
|
||||||
@@ -154,9 +145,17 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
Slog.i(TAG, "Setting the guest clipboard to '" + str + "'");
|
Slog.i(TAG, "Setting the guest clipboard to '" + str + "'");
|
||||||
}
|
}
|
||||||
setAndroidClipboard.accept(clip);
|
setAndroidClipboard.accept(clip);
|
||||||
} catch (ErrnoException | EOFException | InterruptedIOException e) {
|
} catch (ErrnoException | EOFException | InterruptedIOException
|
||||||
closePipe();
|
| InterruptedException e) {
|
||||||
} catch (InterruptedException | IllegalArgumentException e) {
|
setPipeFD(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Os.close(fd);
|
||||||
|
} catch (ErrnoException e2) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -166,27 +165,37 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(final @Nullable ClipData clip) {
|
public void accept(final @Nullable ClipData clip) {
|
||||||
|
final FileDescriptor fd = getPipeFD();
|
||||||
|
if (fd != null) {
|
||||||
|
setHostClipboard(fd, getClipString(clip));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getClipString(final @Nullable ClipData clip) {
|
||||||
if (clip == null) {
|
if (clip == null) {
|
||||||
setHostClipboardImpl("");
|
return "";
|
||||||
} else if (clip.getItemCount() > 0) {
|
}
|
||||||
|
|
||||||
|
if (clip.getItemCount() == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
final CharSequence text = clip.getItemAt(0).getText();
|
final CharSequence text = clip.getItemAt(0).getText();
|
||||||
if (text != null) {
|
if (text == null) {
|
||||||
setHostClipboardImpl(text.toString());
|
return "";
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setHostClipboardImpl(final String value) {
|
return text.toString();
|
||||||
final FileDescriptor pipeFD = getPipeFD();
|
}
|
||||||
|
|
||||||
if (pipeFD != null) {
|
private static void setHostClipboard(final FileDescriptor fd, final String value) {
|
||||||
Thread t = new Thread(() -> {
|
Thread t = new Thread(() -> {
|
||||||
if (LOG_CLIBOARD_ACCESS) {
|
if (LOG_CLIBOARD_ACCESS) {
|
||||||
Slog.i(TAG, "Setting the host clipboard to '" + value + "'");
|
Slog.i(TAG, "Setting the host clipboard to '" + value + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
sendMessage(pipeFD, value.getBytes());
|
sendMessage(fd, value.getBytes());
|
||||||
} catch (ErrnoException | InterruptedIOException e) {
|
} catch (ErrnoException | InterruptedIOException e) {
|
||||||
Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
|
Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@@ -194,7 +203,6 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void readFully(final FileDescriptor fd,
|
private static void readFully(final FileDescriptor fd,
|
||||||
final byte[] buf, int offset, int size)
|
final byte[] buf, int offset, int size)
|
||||||
|
|||||||
Reference in New Issue
Block a user