Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 513f7d83 authored by Roman Kiryanov's avatar Roman Kiryanov Committed by Gerrit Code Review
Browse files

Merge "Cleanup in EmulatorClipboardMonitor (4)"

parents 306e3bb8 cfe2796f
Loading
Loading
Loading
Loading
+31 −22
Original line number Diff line number Diff line
@@ -169,27 +169,37 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {

    @Override
    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) {
            setHostClipboardImpl("");
        } else if (clip.getItemCount() > 0) {
            final CharSequence text = clip.getItemAt(0).getText();
            if (text != null) {
                setHostClipboardImpl(text.toString());
            return "";
        }

        if (clip.getItemCount() == 0) {
            return "";
        }

        final CharSequence text = clip.getItemAt(0).getText();
        if (text == null) {
            return "";
        }

    private void setHostClipboardImpl(final String value) {
        final FileDescriptor pipeFD = getPipeFD();
        return text.toString();
    }

        if (pipeFD != null) {
    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(pipeFD, value.getBytes());
                sendMessage(fd, value.getBytes());
            } catch (ErrnoException | InterruptedIOException e) {
                Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
            } catch (IllegalArgumentException e) {
@@ -197,7 +207,6 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
        });
        t.start();
    }
    }

    private static void readFully(final FileDescriptor fd,
                                  final byte[] buf, int offset, int size)