From 8b33c6c1087a183c7e8a7d45445eb0673b50e458 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 17 Oct 2019 22:21:47 +0200 Subject: [PATCH] Adapt copy-paste methods for Android 10 The methods getPrimaryClip() and setPrimaryClip() expect an additional parameter since Android 10. Fixes . --- .../scrcpy/wrappers/ClipboardManager.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java index 5cc71cd4bf..27dcb44357 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java @@ -3,6 +3,7 @@ import com.genymobile.scrcpy.Ln; import android.content.ClipData; +import android.os.Build; import android.os.IInterface; import java.lang.reflect.InvocationTargetException; @@ -11,6 +12,7 @@ public class ClipboardManager { private static final String PACKAGE_NAME = "com.android.shell"; + private static final int USER_ID = 0; private final IInterface manager; private Method getPrimaryClipMethod; @@ -23,7 +25,11 @@ public ClipboardManager(IInterface manager) { private Method getGetPrimaryClipMethod() { if (getPrimaryClipMethod == null) { try { - getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class); + } else { + getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class); + } } catch (NoSuchMethodException e) { Ln.e("Could not find method", e); } @@ -34,7 +40,12 @@ private Method getGetPrimaryClipMethod() { private Method getSetPrimaryClipMethod() { if (setPrimaryClipMethod == null) { try { - setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class); + } else { + setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, + String.class, int.class); + } } catch (NoSuchMethodException e) { Ln.e("Could not find method", e); } @@ -42,13 +53,30 @@ private Method getSetPrimaryClipMethod() { return setPrimaryClipMethod; } + private static ClipData getPrimaryClip(Method method, IInterface manager) throws InvocationTargetException, + IllegalAccessException { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + return (ClipData) method.invoke(manager, PACKAGE_NAME); + } + return (ClipData) method.invoke(manager, PACKAGE_NAME, USER_ID); + } + + private static void setPrimaryClip(Method method, IInterface manager, ClipData clipData) throws InvocationTargetException, + IllegalAccessException { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + method.invoke(manager, clipData, PACKAGE_NAME); + } else { + method.invoke(manager, clipData, PACKAGE_NAME, USER_ID); + } + } + public CharSequence getText() { Method method = getGetPrimaryClipMethod(); if (method == null) { return null; } try { - ClipData clipData = (ClipData) method.invoke(manager, PACKAGE_NAME); + ClipData clipData = getPrimaryClip(method, manager); if (clipData == null || clipData.getItemCount() == 0) { return null; } @@ -66,7 +94,7 @@ public void setText(CharSequence text) { } ClipData clipData = ClipData.newPlainText(null, text); try { - method.invoke(manager, clipData, PACKAGE_NAME); + setPrimaryClip(method, manager, clipData); } catch (InvocationTargetException | IllegalAccessException e) { Ln.e("Could not invoke " + method.getName(), e); }