summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Roman Kiryanov <rkir@google.com> 2022-03-03 21:33:44 -0800
committer Roman Kiryanov <rkir@google.com> 2022-05-12 11:13:54 -0700
commit0a11904976a913cdef2c1669fb3b95b7bdaa16cf (patch)
treed9428e243fa59f5388bfb9e9456bb43cc4033a83
parent935e7d4ca23f1e39b0928388038888167c70354f (diff)
Set host's clipboard in a separate thread
It is not recommended to do IO in the UI thread. Bug: 219611030 Bug: 232284244 Test: boot AOSP, check if clipboard works Signed-off-by: Roman Kiryanov <rkir@google.com> Change-Id: Iaf445ae3e6e009b86ccf4d1783dffeebc165bb62 Merged-In: Iaf445ae3e6e009b86ccf4d1783dffeebc165bb62
-rw-r--r--services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java37
1 files changed, 22 insertions, 15 deletions
diff --git a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java
index 11c451e01d4c..28c7cad3b184 100644
--- a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java
+++ b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java
@@ -54,8 +54,8 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
return bits;
}
- private boolean isPipeOpened() {
- return mPipe != null;
+ private synchronized FileDescriptor getPipeFD() {
+ return mPipe;
}
private synchronized boolean openPipe() {
@@ -107,14 +107,16 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
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 ByteBuffer bb = ByteBuffer.wrap(lengthBits);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(msg.length);
- Os.write(mPipe, lengthBits, 0, lengthBits.length);
- Os.write(mPipe, msg, 0, msg.length);
+ Os.write(fd, lengthBits, 0, lengthBits.length);
+ Os.write(fd, msg, 0, msg.length);
}
EmulatorClipboardMonitor(final Consumer<ClipData> setAndroidClipboard) {
@@ -162,17 +164,22 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
}
private void setHostClipboardImpl(final String value) {
- if (LOG_CLIBOARD_ACCESS) {
- Slog.i(TAG, "Setting the host clipboard to '" + value + "'");
- }
+ final FileDescriptor pipeFD = getPipeFD();
- try {
- if (isPipeOpened()) {
- sendMessage(value.getBytes());
- }
- } catch (ErrnoException | InterruptedIOException e) {
- Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
- } catch (IllegalArgumentException e) {
+ if (pipeFD != null) {
+ Thread t = new Thread(() -> {
+ if (LOG_CLIBOARD_ACCESS) {
+ Slog.i(TAG, "Setting the host clipboard to '" + value + "'");
+ }
+
+ try {
+ sendMessage(pipeFD, value.getBytes());
+ } catch (ErrnoException | InterruptedIOException e) {
+ Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
+ } catch (IllegalArgumentException e) {
+ }
+ });
+ t.start();
}
}
}