Merge "Set host's clipboard in a separate thread" am: 7a3a3a77d3

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2007913

Change-Id: I2bf469307acd968bf4faa789a9325173d6351c1d
This commit is contained in:
Roman Kiryanov
2022-03-07 19:28:31 +00:00
committed by Automerger Merge Worker

View File

@@ -54,8 +54,8 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
return bits; return bits;
} }
private boolean isPipeOpened() { private synchronized FileDescriptor getPipeFD() {
return mPipe != null; return mPipe;
} }
private synchronized boolean openPipe() { private synchronized boolean openPipe() {
@@ -107,14 +107,16 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
return msg; return msg;
} }
private void sendMessage(final byte[] msg) throws ErrnoException, InterruptedIOException { private static void sendMessage(
final FileDescriptor fd,
final byte[] msg) throws ErrnoException, InterruptedIOException {
final byte[] lengthBits = new byte[4]; final byte[] lengthBits = new byte[4];
final ByteBuffer bb = ByteBuffer.wrap(lengthBits); final ByteBuffer bb = ByteBuffer.wrap(lengthBits);
bb.order(ByteOrder.LITTLE_ENDIAN); bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(msg.length); bb.putInt(msg.length);
Os.write(mPipe, lengthBits, 0, lengthBits.length); Os.write(fd, lengthBits, 0, lengthBits.length);
Os.write(mPipe, msg, 0, msg.length); Os.write(fd, msg, 0, msg.length);
} }
EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) { EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) {
@@ -162,17 +164,22 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
} }
private void setHostClipboardImpl(final String value) { private void setHostClipboardImpl(final String value) {
final FileDescriptor pipeFD = getPipeFD();
if (pipeFD != null) {
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 {
if (isPipeOpened()) { sendMessage(pipeFD, value.getBytes());
sendMessage(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) {
} }
});
t.start();
}
} }
} }