Merge "Cleanup in EmulatorClipboardMonitor (4)" am: 513f7d83b0

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

Change-Id: Ic13560f1b78f73dcddfd46c10c45d7084841a377
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Roman Kiryanov
2022-05-12 19:48:49 +00:00
committed by Automerger Merge Worker

View File

@@ -169,34 +169,43 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
@Override
public void accept(final @Nullable ClipData clip) {
if (clip == null) {
setHostClipboardImpl("");
} else if (clip.getItemCount() > 0) {
final CharSequence text = clip.getItemAt(0).getText();
if (text != null) {
setHostClipboardImpl(text.toString());
}
final FileDescriptor fd = getPipeFD();
if (fd != null) {
setHostClipboard(fd, getClipString(clip));
}
}
private void setHostClipboardImpl(final String value) {
final FileDescriptor pipeFD = getPipeFD();
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();
private String getClipString(final @Nullable ClipData clip) {
if (clip == null) {
return "";
}
if (clip.getItemCount() == 0) {
return "";
}
final CharSequence text = clip.getItemAt(0).getText();
if (text == null) {
return "";
}
return text.toString();
}
private static void setHostClipboard(final FileDescriptor fd, final String value) {
Thread t = new Thread(() -> {
if (LOG_CLIBOARD_ACCESS) {
Slog.i(TAG, "Setting the host clipboard to '" + value + "'");
}
try {
sendMessage(fd, value.getBytes());
} catch (ErrnoException | InterruptedIOException e) {
Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
} catch (IllegalArgumentException e) {
}
});
t.start();
}
private static void readFully(final FileDescriptor fd,