🐛 fix android 13 clipboard managger NoSuchMethodException

This commit is contained in:
WrBug 2023-07-17 18:40:25 +08:00
parent cec31a6f67
commit ce817df606

View file

@ -22,10 +22,11 @@ public class ClipboardManager {
private Method getGetPrimaryClipMethod() throws NoSuchMethodException { private Method getGetPrimaryClipMethod() throws NoSuchMethodException {
if (getPrimaryClipMethod == null) { if (getPrimaryClipMethod == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { for (Method method : manager.getClass().getMethods()) {
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class); if (method.getName().equals("getPrimaryClip")) {
} else { getPrimaryClipMethod = method;
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class); break;
}
} }
} }
return getPrimaryClipMethod; return getPrimaryClipMethod;
@ -33,29 +34,50 @@ public class ClipboardManager {
private Method getSetPrimaryClipMethod() throws NoSuchMethodException { private Method getSetPrimaryClipMethod() throws NoSuchMethodException {
if (setPrimaryClipMethod == null) { if (setPrimaryClipMethod == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { for (Method method : manager.getClass().getMethods()) {
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class); if (method.getName().equals("setPrimaryClip")) {
} else { setPrimaryClipMethod = method;
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class); break;
}
} }
} }
return setPrimaryClipMethod; return setPrimaryClipMethod;
} }
private static ClipData getPrimaryClip(Method method, IInterface manager) throws InvocationTargetException, IllegalAccessException { private Method getAddPrimaryClipChangedListener() throws NoSuchMethodException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (addPrimaryClipChangedListener == null) {
return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME); for (Method method : manager.getClass().getMethods()) {
if (method.getName().equals("addPrimaryClipChangedListener")) {
addPrimaryClipChangedListener = method;
break;
} }
return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID); }
}
return addPrimaryClipChangedListener;
} }
private static void setPrimaryClip(Method method, IInterface manager, ClipData clipData) private static ClipData getPrimaryClip(Method method, IInterface manager) throws InvocationTargetException, IllegalAccessException {
throws InvocationTargetException, IllegalAccessException { Class<?>[] parameterTypes = method.getParameterTypes();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (parameterTypes.length == 1 && parameterTypes[0] == String.class) {
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME); return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME);
} else {
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
} }
if (parameterTypes.length == 2 && parameterTypes[0] == String.class && parameterTypes[1] == int.class) {
return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
}
return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME, null, ServiceManager.USER_ID);
}
private static void setPrimaryClip(Method method, IInterface manager, ClipData clipData) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 2 && parameterTypes[0] == ClipData.class && parameterTypes[1] == String.class) {
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME);
return;
}
if (parameterTypes.length == 3 && parameterTypes[0] == ClipData.class && parameterTypes[1] == String.class && parameterTypes[2] == int.class) {
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
return;
}
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME, null, ServiceManager.USER_ID);
} }
public CharSequence getText() { public CharSequence getText() {
@ -84,26 +106,17 @@ public class ClipboardManager {
} }
} }
private static void addPrimaryClipChangedListener(Method method, IInterface manager, IOnPrimaryClipChangedListener listener) private static void addPrimaryClipChangedListener(Method method, IInterface manager, IOnPrimaryClipChangedListener listener) throws InvocationTargetException, IllegalAccessException {
throws InvocationTargetException, IllegalAccessException { Class<?>[] parameterTypes = method.getParameterTypes();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (parameterTypes.length == 2 && parameterTypes[0] == IOnPrimaryClipChangedListener.class && parameterTypes[1] == String.class) {
method.invoke(manager, listener, ServiceManager.PACKAGE_NAME); method.invoke(manager, listener, ServiceManager.PACKAGE_NAME);
} else { return;
}
if (parameterTypes.length == 3 && parameterTypes[0] == IOnPrimaryClipChangedListener.class && parameterTypes[1] == String.class && parameterTypes[2] == int.class) {
method.invoke(manager, listener, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID); method.invoke(manager, listener, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
return;
} }
} method.invoke(manager, listener, ServiceManager.PACKAGE_NAME, null, ServiceManager.USER_ID);
private Method getAddPrimaryClipChangedListener() throws NoSuchMethodException {
if (addPrimaryClipChangedListener == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
addPrimaryClipChangedListener = manager.getClass()
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class);
} else {
addPrimaryClipChangedListener = manager.getClass()
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, int.class);
}
}
return addPrimaryClipChangedListener;
} }
public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) { public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {