From c8e64680685d3563ce1785b8b6c78fe0b4f8309d Mon Sep 17 00:00:00 2001 From: Vologhat <47892605+Vologhat@users.noreply.github.com> Date: Tue, 27 Sep 2022 20:49:01 +0300 Subject: [PATCH] fix xiaomi shit (maybe) --- .../vtosters/lite/deviceinfo/OEMDetector.java | 34 +- .../vtosters/lite/utils/ReflectionUtils.java | 394 ++++++------------ 2 files changed, 144 insertions(+), 284 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/deviceinfo/OEMDetector.java b/app/src/main/java/ru/vtosters/lite/deviceinfo/OEMDetector.java index 98ed62320..b35f66a31 100644 --- a/app/src/main/java/ru/vtosters/lite/deviceinfo/OEMDetector.java +++ b/app/src/main/java/ru/vtosters/lite/deviceinfo/OEMDetector.java @@ -2,17 +2,15 @@ package ru.vtosters.lite.deviceinfo; import android.os.Build; import android.text.TextUtils; -import android.util.Log; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.lang.reflect.Field; +import java.lang.reflect.Method; import ru.vtosters.lite.utils.ReflectionUtils; public class OEMDetector { private static Field semPlatformIntField; + private static Method getPropMtd; private static Boolean isOneUi; private static int oneUiMajorVersion; @@ -21,7 +19,8 @@ public class OEMDetector { static { try { semPlatformIntField = ReflectionUtils.getDeclaredField(Build.VERSION.class, "SEM_PLATFORM_INT"); - } catch (NoSuchFieldException e) { + getPropMtd = ReflectionUtils.getDeclaredMethod(ReflectionUtils.getClass("android.os.SystemProperties"), "get", String.class); + } catch (Throwable e) { e.printStackTrace(); } } @@ -97,27 +96,12 @@ public class OEMDetector { return getSystemProperty("ro.build.version.incremental"); } - private static String getSystemProperty(String str) { - BufferedReader bufferedReader; + private static String getSystemProperty(String prop) { try { - bufferedReader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("getprop " + str).getInputStream()), 1024); - try { - try { - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - var rl = bufferedReader.readLine(); - - Log.d("OEMDetector", str + " " + rl); - - return rl; - } catch (IOException ignored) { - } - } catch (Throwable ignored) { + return (String) getPropMtd.invoke(null, prop); + } catch (Throwable e) { + e.printStackTrace(); + return null; } - return null; } - } \ No newline at end of file diff --git a/app/src/main/java/ru/vtosters/lite/utils/ReflectionUtils.java b/app/src/main/java/ru/vtosters/lite/utils/ReflectionUtils.java index 8a4181b91..999769697 100644 --- a/app/src/main/java/ru/vtosters/lite/utils/ReflectionUtils.java +++ b/app/src/main/java/ru/vtosters/lite/utils/ReflectionUtils.java @@ -11,46 +11,83 @@ public class ReflectionUtils { * Gets a class with specific name. * Supports smali type (Lpackage/Class;) */ - public static Class getClass(String name) throws ClassNotFoundException { + public static Class getClass(String name) + throws ClassNotFoundException { if (name.contains(";")) name = name.substring(1, name.length() - 1).replace("/", "."); return Class.forName(name); } - /** - * Gets a declared field with specific name. - **/ - public static Field getDeclaredField(Class clz, String fieldName) throws NoSuchFieldException { + public static Field getDeclaredField(Class clz, String fieldName) + throws NoSuchFieldException { Field field = clz.getDeclaredField(fieldName); field.setAccessible(true); return field; } - public static Field getDeclaredField(String className, String fieldName) throws NoSuchFieldException, ClassNotFoundException { - Field field = getClass(className).getDeclaredField(fieldName); - field.setAccessible(true); - return field; + public static Field getDeclaredField(String className, String fieldName) + throws NoSuchFieldException, ClassNotFoundException { + return getDeclaredField(getClass(className), fieldName); } - /** - * Gets a field with specific name. - **/ - public static Field getField(Class clz, String fieldName) throws NoSuchFieldException { + public static Field getDeclaredFieldRecursive(Class clz, String fieldName) + throws NoSuchFieldException { + Field field = null; + Class clazz = clz; + while (clazz != null) { + try { + field = getDeclaredField(clazz, fieldName); + break; + } catch (NoSuchFieldException e) { + clazz = clz.getSuperclass(); + } + } + if (field != null) + return field; + throw new NoSuchFieldException(fieldName); + } + + public static Field getDeclaredFieldRecursive(String className, String fieldName) + throws ClassNotFoundException, NoSuchFieldException { + return getDeclaredFieldRecursive(getClass(className), fieldName); + } + + public static Field getField(Class clz, String fieldName) + throws NoSuchFieldException { Field field = clz.getField(fieldName); field.setAccessible(true); return field; } - public static Field getField(String className, String fieldName) throws NoSuchFieldException, ClassNotFoundException { - Field field = getClass(className).getField(fieldName); - field.setAccessible(true); - return field; + public static Field getField(String className, String fieldName) + throws NoSuchFieldException, ClassNotFoundException { + return getField(getClass(className), fieldName); } - /** - * Gets a non-static field value - */ - public static T getObjectField(Class clz, String fieldName, Object instance) throws NoSuchFieldException, IllegalAccessException { + public static Field getFieldRecursive(Class clz, String fieldName) + throws NoSuchFieldException { + Field field = null; + Class clazz = clz; + while (clazz != null) { + try { + field = getField(clazz, fieldName); + break; + } catch (NoSuchFieldException e) { + clazz = clz.getSuperclass(); + } + } + if (field != null) + return field; + throw new NoSuchFieldException(fieldName); + } + + public static Field getFieldRecursive(String className, String fieldName) + throws ClassNotFoundException, NoSuchFieldException { + return getFieldRecursive(getClass(className), fieldName); + } + + public static T getObjectField(Class clz, String fieldName, Object instance) + throws NoSuchFieldException, IllegalAccessException { Field field; try { field = getField(clz, fieldName); @@ -60,43 +97,50 @@ public class ReflectionUtils { return (T) field.get(instance); } - public static T getObjectField(String className, String fieldName, Object instance) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { - Field field; - try { - field = getField(className, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(className, fieldName); - } - return (T) field.get(instance); + public static T getObjectField(String className, String fieldName, Object instance) + throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { + return getObjectField(getClass(className), fieldName, instance); } - /** - * Sets a value for non-static field - */ - public static void setObjectField(Class clz, String fieldName, Object instance, Object value) throws NoSuchFieldException, IllegalAccessException { + public static void setObjectField(Class clz, String fieldName, Object instance, Object value) + throws NoSuchFieldException, IllegalAccessException { Field field; try { field = getField(clz, fieldName); } catch (NoSuchFieldException e) { field = getDeclaredField(clz, fieldName); } + unfinalField(field); field.set(instance, value); } - public static void setObjectField(String className, String fieldName, Object instance, Object value) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { - Field field; - try { - field = getField(className, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(className, fieldName); - } - field.set(instance, value); + public static void setObjectField(String className, String fieldName, Object instance, Object value) + throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { + setObjectField(getClass(className), fieldName, instance, value); } - /** - * Unlocks modifying of field - */ - private static void unlockField(Field field) throws NoSuchFieldException { + public static T getObjectStaticField(Class clz, String fieldName) + throws NoSuchFieldException, IllegalAccessException { + return getObjectField(clz, fieldName, null); + } + + public static T getObjectStaticField(String className, String fieldName) + throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { + return getObjectField(getClass(className), fieldName, null); + } + + public static void setObjectStaticField(Class clz, String fieldName, Object value) + throws NoSuchFieldException, IllegalAccessException { + setObjectField(clz, fieldName, null, value); + } + + public static void setObjectStaticField(String className, String fieldName, Object value) + throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { + setObjectStaticField(getClass(className), fieldName, value); + } + + public static void unfinalField(Field field) throws NoSuchFieldException { + if (!Modifier.isFinal(field.getModifiers())) return; Field accessFlagsFld = getDeclaredField(Field.class, "accessFlags"); try { int accessFlags = accessFlagsFld.getInt(field); @@ -106,178 +150,60 @@ public class ReflectionUtils { } } - /** - * Unlocks modifying of non-static field and sets a value for it - */ - public static void setObjectFinalField(Class clz, String fieldName, Object instance, Object value) throws NoSuchFieldException, IllegalAccessException { - Field field; - try { - field = getField(clz, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(clz, fieldName); - } - - if (!Modifier.isFinal(field.getModifiers())) return; - - unlockField(field); - field.set(instance, value); - } - - public static void setObjectFinalField(String className, String fieldName, Object instance, Object value) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { - Field field; - try { - field = getField(className, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(className, fieldName); - } - - if (!Modifier.isFinal(field.getModifiers())) return; - - unlockField(field); - field.set(instance, value); - } - - /** - * Sets a value for static field - */ - public static void setObjectStaticField(Class clz, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { - Field field; - try { - field = getField(clz, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(clz, fieldName); - } - field.set(null, value); - } - - public static void setObjectStaticField(String className, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { - Field field; - try { - field = getField(className, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(className, fieldName); - } - field.set(null, value); - } - - /** - * Unlocks modifying of non-static field and sets a value for it - */ - public static void setObjectFinalStaticField(Class clz, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { - Field field; - try { - field = getField(clz, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(clz, fieldName); - } - - if (!Modifier.isFinal(field.getModifiers())) return; - - unlockField(field); - field.set(null, value); - } - - public static void setObjectFinalStaticField(String className, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { - Field field; - try { - field = getField(className, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(className, fieldName); - } - - if (!Modifier.isFinal(field.getModifiers())) return; - - unlockField(field); - field.set(null, value); - } - - /** - * Gets a static field value - */ - public static T getObjectField(Class clz, String fieldName) throws NoSuchFieldException, IllegalAccessException { - Field field; - try { - field = getField(clz, fieldName); - } catch (NoSuchFieldException e) { - field = getDeclaredField(clz, fieldName); - } - return (T) field.get(null); - } - - /** - * Gets a method with specific name. - **/ - public static Method getMethod(Class clz, String methodName, Class... parameterTypes) throws NoSuchMethodException { + public static Method getMethod(Class clz, String methodName, Class... parameterTypes) + throws NoSuchMethodException { Method method = clz.getMethod(methodName, parameterTypes); method.setAccessible(true); return method; } - public static Method getMethod(String className, String methodName, Class... parameterTypes) throws ClassNotFoundException, NoSuchMethodException { - Method method = getClass(className).getMethod(methodName, parameterTypes); - method.setAccessible(true); - return method; + public static Method getMethod(String className, String methodName, Class... parameterTypes) + throws ClassNotFoundException, NoSuchMethodException { + return getMethod(getClass(className), methodName, parameterTypes); } - public static Method getMethod(Class clz, String methodName, Object... parameters) throws NoSuchMethodException { + public static Method getMethod(Class clz, String methodName, Object... parameters) + throws NoSuchMethodException { Class[] parameterTypes = new Class[parameters.length]; for (int i = 0; i < parameters.length; i++) { parameterTypes[i] = parameters[i].getClass(); } - Method method = getMethod(clz, methodName, parameterTypes); - method.setAccessible(true); - return method; + return getMethod(clz, methodName, parameterTypes); } - public static Method getMethod(String className, String methodName, Object... parameters) throws ClassNotFoundException, NoSuchMethodException { - Class[] parameterTypes = new Class[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - parameterTypes[i] = parameters[i].getClass(); - } - Method method = getMethod(className, methodName, parameterTypes); - method.setAccessible(true); - return method; + public static Method getMethod(String className, String methodName, Object... parameters) + throws ClassNotFoundException, NoSuchMethodException { + return getMethod(getClass(className), methodName, parameters); } - /** - * Gets a declared method with specific name. - **/ - public static Method getDeclaredMethod(Class clz, String methodName, Class... parameterTypes) throws NoSuchMethodException { + public static Method getDeclaredMethod(Class clz, String methodName, Class... parameterTypes) + throws NoSuchMethodException { Method method = clz.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } - public static Method getDeclaredMethod(String className, String methodName, Class... parameterTypes) throws ClassNotFoundException, NoSuchMethodException { - Method method = getClass(className).getDeclaredMethod(methodName, parameterTypes); - method.setAccessible(true); - return method; + public static Method getDeclaredMethod(String className, String methodName, Class... parameterTypes) + throws ClassNotFoundException, NoSuchMethodException { + return getDeclaredMethod(getClass(className), methodName, parameterTypes); } - public static Method getDeclaredMethod(Class clz, String methodName, Object... parameters) throws NoSuchMethodException { + public static Method getDeclaredMethod(Class clz, String methodName, Object... parameters) + throws NoSuchMethodException { Class[] parameterTypes = new Class[parameters.length]; for (int i = 0; i < parameters.length; i++) { parameterTypes[i] = parameters[i].getClass(); } - Method method = getDeclaredMethod(clz, methodName, parameterTypes); - method.setAccessible(true); - return method; + return getDeclaredMethod(clz, methodName, parameterTypes); } - public static Method getDeclaredMethod(String className, String methodName, Object... parameters) throws ClassNotFoundException, NoSuchMethodException { - Class[] parameterTypes = new Class[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - parameterTypes[i] = parameters[i].getClass(); - } - Method method = getDeclaredMethod(className, methodName, parameterTypes); - method.setAccessible(true); - return method; + public static Method getDeclaredMethod(String className, String methodName, Object... parameters) + throws ClassNotFoundException, NoSuchMethodException { + return getDeclaredMethod(getClass(className), methodName, parameters); } - /** - * Invokes a non-static method - */ - public static T invokeMethod(Class clz, String methodName, Object instance, Class[] parameterTypes, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public static T invokeMethod(Class clz, String methodName, Object instance, Class[] parameterTypes, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method; try { method = getMethod(clz, methodName, parameterTypes); @@ -287,92 +213,42 @@ public class ReflectionUtils { return (T) method.invoke(instance, parameters); } - public static T invokeMethod(String className, String methodName, Object instance, Class[] parameterTypes, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { - Method method; - try { - method = getMethod(className, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(className, methodName, parameters); - } - return (T) method.invoke(instance, parameters); + public static T invokeMethod(String className, String methodName, Object instance, Class[] parameterTypes, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + return invokeMethod(getClass(className), methodName, instance, parameterTypes, parameters); } - public static T invokeMethod(Class clz, String methodName, Object instance, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public static T invokeMethod(Class clz, String methodName, Object instance, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class[] parameterTypes = new Class[parameters.length]; for (int i = 0; i < parameters.length; i++) { parameterTypes[i] = parameters[i].getClass(); } - Method method; - try { - method = getMethod(clz, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(clz, methodName, parameters); - } - return (T) method.invoke(instance, parameters); + return invokeMethod(clz, methodName, instance, parameterTypes, parameters); } - public static T invokeMethod(String className, String methodName, Object instance, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { - Class[] parameterTypes = new Class[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - parameterTypes[i] = parameters[i].getClass(); - } - Method method; - try { - method = getMethod(className, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(className, methodName, parameters); - } - return (T) method.invoke(instance, parameters); + public static T invokeMethod(String className, String methodName, Object instance, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + return invokeMethod(getClass(className), methodName, instance, parameters); } - /** - * Invokes a static method - */ - public static T invokeStaticMethod(Class clz, String methodName, Class[] parameterTypes, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Method method; - try { - method = getMethod(clz, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(clz, methodName, parameters); - } - return (T) method.invoke(null, parameters); + public static T invokeStaticMethod(Class clz, String methodName, Class[] parameterTypes, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + return invokeMethod(clz, methodName, null, parameterTypes, parameters); } - public static T invokeStaticMethod(String className, String methodName, Class[] parameterTypes, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { - Method method; - try { - method = getMethod(className, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(className, methodName, parameters); - } - return (T) method.invoke(null, parameters); + public static T invokeStaticMethod(String className, String methodName, Class[] parameterTypes, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + return invokeStaticMethod(getClass(className), methodName, parameterTypes, parameters); } - public static T invokeStaticMethod(Class clz, String methodName, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Class[] parameterTypes = new Class[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - parameterTypes[i] = parameters[i].getClass(); - } - Method method; - try { - method = getMethod(clz, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(clz, methodName, parameters); - } - return (T) method.invoke(null, parameters); + public static T invokeStaticMethod(Class clz, String methodName, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + return invokeMethod(clz, methodName, null, parameters); } - public static T invokeStaticMethod(String className, String methodName, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { - Class[] parameterTypes = new Class[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - parameterTypes[i] = parameters[i].getClass(); - } - Method method; - try { - method = getMethod(className, methodName, parameterTypes); - } catch (NoSuchMethodException e) { - method = getDeclaredMethod(className, methodName, parameters); - } - return (T) method.invoke(null, parameters); + public static T invokeStaticMethod(String className, String methodName, Object... parameters) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + return invokeMethod(getClass(className), methodName, null, parameters); } -} +} \ No newline at end of file