Skip to content

Commit

Permalink
Adapt copy-paste methods for Android 10
Browse files Browse the repository at this point in the history
The methods getPrimaryClip() and setPrimaryClip() expect an additional
parameter since Android 10.

Fixes <#796>.
  • Loading branch information
rom1v committed Oct 17, 2019
1 parent 5b7a0cd commit 8b33c6c
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -34,21 +40,43 @@ 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);
}
}
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;
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 8b33c6c

Please sign in to comment.