Cleanup in EmulatorClipboardMonitor (4)
Bug: 231345789 Test: presubmit Signed-off-by: Roman Kiryanov <rkir@google.com> Change-Id: I9ba57f4347e50f8462cec347e5608ec4d1eb0b30 Merged-In: I9ba57f4347e50f8462cec347e5608ec4d1eb0b30
This commit is contained in:
@@ -169,27 +169,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) {
|
||||||
@@ -197,7 +207,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