|
| 1 | +package com.sevtinge.hyperceiler.utils; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.DialogInterface; |
| 5 | + |
| 6 | +import dalvik.system.PathClassLoader; |
| 7 | +import de.robv.android.xposed.XposedHelpers; |
| 8 | + |
| 9 | +/** |
| 10 | + * 调用 Miui 的 MiuiDialog, |
| 11 | + * 仅 Xposed 代码可以调用 |
| 12 | + */ |
| 13 | +public class MiuiDialog { |
| 14 | + private static final String path = "miuix.appcompat.app.AlertDialog"; |
| 15 | + private static final String pathJar = "/system/framework/miuix.jar"; |
| 16 | + private Class<?> miuiClass = null; |
| 17 | + private Context context = null; |
| 18 | + private Object newInstance = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * 基本不可用,请传 ClassLoader |
| 22 | + */ |
| 23 | + public MiuiDialog(Context context) { |
| 24 | + this.context = context; |
| 25 | + miuiClass = findClass(new PathClassLoader(pathJar, ClassLoader.getSystemClassLoader())); |
| 26 | + newInstance = XposedHelpers.newInstance(miuiClass, context); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * 基本不可用,请传 ClassLoader |
| 31 | + */ |
| 32 | + public MiuiDialog(Context context, int value) { |
| 33 | + this.context = context; |
| 34 | + miuiClass = findClass(new PathClassLoader(pathJar, ClassLoader.getSystemClassLoader())); |
| 35 | + newInstance = XposedHelpers.newInstance(miuiClass, context, value); |
| 36 | + } |
| 37 | + |
| 38 | + public MiuiDialog(ClassLoader classLoader, Context context) { |
| 39 | + this.context = context; |
| 40 | + miuiClass = findClass(classLoader); |
| 41 | + newInstance = XposedHelpers.newInstance(miuiClass, context); |
| 42 | + } |
| 43 | + |
| 44 | + public MiuiDialog(ClassLoader classLoader, Context context, int value) { |
| 45 | + this.context = context; |
| 46 | + miuiClass = findClass(classLoader); |
| 47 | + newInstance = XposedHelpers.newInstance(miuiClass, context, value); |
| 48 | + } |
| 49 | + |
| 50 | + private Class<?> findClass(ClassLoader classLoader) { |
| 51 | + Class<?> clz = XposedHelpers.findClassIfExists(path, classLoader); |
| 52 | + if (clz == null) throw new RuntimeException("Failed to get miuix class!"); |
| 53 | + return clz; |
| 54 | + } |
| 55 | + |
| 56 | + public static class Builder { |
| 57 | + private Class<?> miuiClass = null; |
| 58 | + private Context context = null; |
| 59 | + private Object newInstance = null; |
| 60 | + private static final String pathBuilder = "miuix.appcompat.app.AlertDialog$Builder"; |
| 61 | + |
| 62 | + /** |
| 63 | + * 基本不可用,请传 ClassLoader |
| 64 | + */ |
| 65 | + public Builder(Context context) { |
| 66 | + this.context = context; |
| 67 | + miuiClass = findClass(new PathClassLoader(pathJar, ClassLoader.getSystemClassLoader())); |
| 68 | + newInstance = XposedHelpers.newInstance(miuiClass, context); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * 基本不可用,请传 ClassLoader |
| 73 | + */ |
| 74 | + public Builder(Context context, int value) { |
| 75 | + this.context = context; |
| 76 | + miuiClass = findClass(new PathClassLoader(pathJar, ClassLoader.getSystemClassLoader())); |
| 77 | + newInstance = XposedHelpers.newInstance(miuiClass, context, value); |
| 78 | + } |
| 79 | + |
| 80 | + public Builder(ClassLoader classLoader, Context context) { |
| 81 | + this.context = context; |
| 82 | + miuiClass = findClass(classLoader); |
| 83 | + newInstance = XposedHelpers.newInstance(miuiClass, context); |
| 84 | + } |
| 85 | + |
| 86 | + public Builder(ClassLoader classLoader, Context context, int value) { |
| 87 | + this.context = context; |
| 88 | + miuiClass = findClass(classLoader); |
| 89 | + newInstance = XposedHelpers.newInstance(miuiClass, context, value); |
| 90 | + } |
| 91 | + |
| 92 | + public Builder setTitle(int id) { |
| 93 | + newInstance = XposedHelpers.callMethod(newInstance, "setTitle", id); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public Builder setTitle(CharSequence charSequence) { |
| 98 | + newInstance = XposedHelpers.callMethod(newInstance, "setTitle", charSequence); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public Builder setHapticFeedbackEnabled(boolean z) { |
| 103 | + newInstance = XposedHelpers.callMethod(newInstance, "setHapticFeedbackEnabled", z); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + public Builder setMessage(int id) { |
| 108 | + newInstance = XposedHelpers.callMethod(newInstance, "setMessage", id); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + public Builder setMessage(CharSequence charSequence) { |
| 113 | + newInstance = XposedHelpers.callMethod(newInstance, "setMessage", charSequence); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + public Builder setNegativeButton(int id, DialogInterface.OnClickListener onClickListener) { |
| 118 | + newInstance = XposedHelpers.callMethod(newInstance, "setNegativeButton", id, onClickListener); |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + public Builder setNegativeButton(CharSequence charSequence, DialogInterface.OnClickListener onClickListener) { |
| 123 | + newInstance = XposedHelpers.callMethod(newInstance, "setNegativeButton", charSequence, onClickListener); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + public Builder setNeutralButton(int id, DialogInterface.OnClickListener onClickListener) { |
| 128 | + newInstance = XposedHelpers.callMethod(newInstance, "setNeutralButton", id, onClickListener); |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + public Builder setNeutralButton(CharSequence charSequence, DialogInterface.OnClickListener onClickListener) { |
| 133 | + newInstance = XposedHelpers.callMethod(newInstance, "setNeutralButton", onClickListener); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + public Builder setPositiveButton(int id, DialogInterface.OnClickListener onClickListener) { |
| 138 | + newInstance = XposedHelpers.callMethod(newInstance, "setPositiveButton", id, onClickListener); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public Builder setPositiveButton(CharSequence charSequence, DialogInterface.OnClickListener onClickListener) { |
| 143 | + newInstance = XposedHelpers.callMethod(newInstance, "setPositiveButton", onClickListener); |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public void show() { |
| 148 | + newInstance = XposedHelpers.callMethod(newInstance, "create"); |
| 149 | + XposedHelpers.callMethod(newInstance, "show"); |
| 150 | + } |
| 151 | + |
| 152 | + private Class<?> findClass(ClassLoader classLoader) { |
| 153 | + Class<?> clz = XposedHelpers.findClassIfExists(pathBuilder, classLoader); |
| 154 | + if (clz == null) throw new RuntimeException("Failed to get miuix$builder class!"); |
| 155 | + return clz; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments