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

Commit 33e3c435 authored by Roman Kiryanov's avatar Roman Kiryanov Committed by Automerger Merge Worker
Browse files

Merge "Set host's clipboard in a separate thread" am: 7a3a3a77 am: 19929dff am: a4873fa8

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

Change-Id: I814ee6aff7f3dce121f999c801e0dcc97808d56b
parents e89ba039 a4873fa8
Loading
Loading
Loading
Loading
+22 −15
Original line number Diff line number Diff line
@@ -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) {
        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 {
            if (isPipeOpened()) {
                sendMessage(value.getBytes());
            }
                    sendMessage(pipeFD, value.getBytes());
                } catch (ErrnoException | InterruptedIOException e) {
                    Slog.e(TAG, "Failed to set host clipboard " + e.getMessage());
                } catch (IllegalArgumentException e) {
                }
            });
            t.start();
        }
    }
}