Adapt copy-paste methods for Android 10

The methods getPrimaryClip() and setPrimaryClip() expect an additional
parameter since Android 10.

Fixes <https://github.com/Genymobile/scrcpy/issues/796>.
This commit is contained in:
Romain Vimont 2019-10-17 22:21:47 +02:00
parent 5b7a0cd8e9
commit 8b33c6c108

View file

@ -3,6 +3,7 @@ package com.genymobile.scrcpy.wrappers;
import com.genymobile.scrcpy.Ln; import com.genymobile.scrcpy.Ln;
import android.content.ClipData; import android.content.ClipData;
import android.os.Build;
import android.os.IInterface; import android.os.IInterface;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -11,6 +12,7 @@ import java.lang.reflect.Method;
public class ClipboardManager { public class ClipboardManager {
private static final String PACKAGE_NAME = "com.android.shell"; private static final String PACKAGE_NAME = "com.android.shell";
private static final int USER_ID = 0;
private final IInterface manager; private final IInterface manager;
private Method getPrimaryClipMethod; private Method getPrimaryClipMethod;
@ -23,7 +25,11 @@ public class ClipboardManager {
private Method getGetPrimaryClipMethod() { private Method getGetPrimaryClipMethod() {
if (getPrimaryClipMethod == null) { if (getPrimaryClipMethod == null) {
try { 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) { } catch (NoSuchMethodException e) {
Ln.e("Could not find method", e); Ln.e("Could not find method", e);
} }
@ -34,7 +40,12 @@ public class ClipboardManager {
private Method getSetPrimaryClipMethod() { private Method getSetPrimaryClipMethod() {
if (setPrimaryClipMethod == null) { if (setPrimaryClipMethod == null) {
try { 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) { } catch (NoSuchMethodException e) {
Ln.e("Could not find method", e); Ln.e("Could not find method", e);
} }
@ -42,13 +53,30 @@ public class ClipboardManager {
return setPrimaryClipMethod; 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() { public CharSequence getText() {
Method method = getGetPrimaryClipMethod(); Method method = getGetPrimaryClipMethod();
if (method == null) { if (method == null) {
return null; return null;
} }
try { try {
ClipData clipData = (ClipData) method.invoke(manager, PACKAGE_NAME); ClipData clipData = getPrimaryClip(method, manager);
if (clipData == null || clipData.getItemCount() == 0) { if (clipData == null || clipData.getItemCount() == 0) {
return null; return null;
} }
@ -66,7 +94,7 @@ public class ClipboardManager {
} }
ClipData clipData = ClipData.newPlainText(null, text); ClipData clipData = ClipData.newPlainText(null, text);
try { try {
method.invoke(manager, clipData, PACKAGE_NAME); setPrimaryClip(method, manager, clipData);
} catch (InvocationTargetException | IllegalAccessException e) { } catch (InvocationTargetException | IllegalAccessException e) {
Ln.e("Could not invoke " + method.getName(), e); Ln.e("Could not invoke " + method.getName(), e);
} }