|
29 | 29 | import com.sevtinge.hyperceiler.libhook.base.BaseHook; |
30 | 30 | import com.sevtinge.hyperceiler.libhook.callback.IMethodHook; |
31 | 31 |
|
32 | | -import java.lang.reflect.Method; |
33 | | -import java.lang.reflect.Modifier; |
| 32 | +import java.util.List; |
34 | 33 |
|
35 | 34 | import io.github.kyuubiran.ezxhelper.xposed.common.HookParam; |
| 35 | +import io.github.libxposed.api.XposedInterface; |
36 | 36 |
|
37 | 37 | /** |
38 | 38 | * 绕过打开应用商店时强制使用小米应用商店 |
|
41 | 41 | */ |
42 | 42 | public class BypassForceMiAppStore extends BaseHook { |
43 | 43 |
|
| 44 | + private static final String PREF_FORCE_CHOOSER = "system_framework_bypass_force_mi_appstore"; |
| 45 | + private static final String PREF_DETAIL_MINI = "system_framework_market_use_detailmini"; |
| 46 | + |
44 | 47 | @Override |
45 | 48 | public void init() { |
46 | 49 |
|
47 | 50 | try { |
48 | | - Class<?> cls = |
49 | | - findClass("com.android.server.wm.ActivityTaskManagerService"); |
50 | | - |
51 | | - for (Method method : cls.getDeclaredMethods()) { |
52 | | - if (!method.getName().equals("startActivity")) continue; |
53 | | - if (!Modifier.isPublic(method.getModifiers())) continue; |
54 | | - |
55 | | - Class<?>[] paramTypes = method.getParameterTypes(); |
56 | | - int intentIndex = -1; |
57 | | - for (int i = 0; i < paramTypes.length; i++) { |
58 | | - if (Intent.class.equals(paramTypes[i])) { |
59 | | - intentIndex = i; |
60 | | - break; |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - if (intentIndex == -1) continue; |
65 | | - |
66 | | - final int index = intentIndex; |
67 | | - hookMethod(method, new IMethodHook() { |
68 | | - @Override |
69 | | - public void before(HookParam param) { |
70 | | - try { |
71 | | - Intent intent = (Intent) param.getArgs()[index]; |
72 | | - if (intent == null) return; |
73 | | - |
74 | | - Uri data = intent.getData(); |
75 | | - if (!Intent.ACTION_VIEW.equals(intent.getAction()) || data == null) |
76 | | - return; |
| 51 | + Class<?> atmsClass = findClassIfExists("com.android.server.wm.ActivityTaskManagerService"); |
| 52 | + if (atmsClass == null) { |
| 53 | + XposedLog.w(TAG, getPackageName(), "Class not found: com.android.server.wm.ActivityTaskManagerService"); |
| 54 | + return; |
| 55 | + } |
77 | 56 |
|
78 | | - String uriStr = data.toString(); |
| 57 | + List<XposedInterface.HookHandle> handles = hookAllMethods(atmsClass, "startActivity", new IMethodHook() { |
| 58 | + @Override |
| 59 | + public void before(HookParam param) { |
| 60 | + try { |
| 61 | + Object[] args = param.getArgs(); |
| 62 | + int intentIndex = findIntentArgIndex(args); |
| 63 | + if (intentIndex < 0) return; |
79 | 64 |
|
80 | | - if (uriStr != null) { |
81 | | - // 始终替换 mimarket:// 为 market:// |
82 | | - uriStr = uriStr.replaceFirst("^mimarket://", "market://"); |
| 65 | + Intent intent = (Intent) args[intentIndex]; |
| 66 | + if (intent == null || !Intent.ACTION_VIEW.equals(intent.getAction())) return; |
83 | 67 |
|
84 | | - // 如果启用了 detailmini 选项,则替换 path 中的 details → details/detailmini |
85 | | - if (PrefsBridge.getBoolean("system_framework_market_use_detailmini")) { |
86 | | - uriStr = uriStr.replaceFirst("(?<=market://)(details)(?!/detailmini)", "details/detailmini"); |
87 | | - } |
| 68 | + final boolean forceChooserEnabled = PrefsBridge.getBoolean(PREF_FORCE_CHOOSER); |
| 69 | + final boolean detailMiniEnabled = PrefsBridge.getBoolean(PREF_DETAIL_MINI); |
| 70 | + if (!forceChooserEnabled && !detailMiniEnabled) return; |
88 | 71 |
|
89 | | - intent.setData(Uri.parse(uriStr)); |
90 | | - } |
| 72 | + Uri data = intent.getData(); |
| 73 | + if (data == null) return; |
91 | 74 |
|
92 | | - // 如果是 market://details?id=... |
93 | | - if ("market".equals(intent.getData().getScheme()) |
94 | | - && ("details".equals(intent.getData().getHost()) || "details/detailmini".equals(intent.getData().getHost())) |
95 | | - && intent.getData().getQueryParameter("id") != null |
96 | | - ) { |
| 75 | + Uri newData = data; |
| 76 | + String type = intent.getType(); |
97 | 77 |
|
98 | | - // 移除指定包名 |
99 | | - if (intent.getPackage() != null) { |
100 | | - XposedLog.d(TAG, getPackageName(), "Removed package=:" + intent.getPackage()); |
101 | | - intent.setPackage(null); |
102 | | - } |
| 78 | + // 注意:该 hook 属于系统层 startActivity 拦截,开关间存在联动影响,必须将改写范围收窄到 market 相关 URI。 |
| 79 | + if ("mimarket".equals(newData.getScheme())) { |
| 80 | + newData = newData.buildUpon().scheme("market").build(); |
| 81 | + } |
103 | 82 |
|
104 | | - //FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 会导致小米应用商店无法打开,原因未知 |
105 | | - intent.removeFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 83 | + // 如果启用了 detailmini,则将 market://details?id=... 改写为 market://details/detailmini?id=... |
| 84 | + String path = newData.getPath(); |
| 85 | + if (detailMiniEnabled |
| 86 | + && isMarketDetailsHost(newData) |
| 87 | + && (path == null || path.isEmpty() || "/".equals(path))) { |
| 88 | + newData = newData.buildUpon().path("/detailmini").build(); |
| 89 | + } |
106 | 90 |
|
107 | | - // 如果启用了 bypass_force_mi_appstore,则强制使用 chooser 打开应用商店 |
108 | | - if (PrefsBridge.getBoolean("system_framework_bypass_force_mi_appstore")) { |
109 | | - intent = Intent.createChooser(intent, null); |
| 91 | + // setData(...) 会清空 type,使用 setDataAndType(...) 保留 MIME type。 |
| 92 | + if (!newData.equals(data)) { |
| 93 | + if (type != null) { |
| 94 | + intent.setDataAndType(newData, type); |
| 95 | + } else { |
| 96 | + intent.setData(newData); |
| 97 | + } |
| 98 | + } |
110 | 99 |
|
111 | | - XposedLog.d(TAG, getPackageName(), "Forced chooser for market://details intent"); |
112 | | - } |
| 100 | + Uri finalData = intent.getData(); |
| 101 | + if (finalData == null) return; |
113 | 102 |
|
| 103 | + // 只处理 market://details[(/detailmini)]?id=...,避免影响其他 ACTION_VIEW intent。 |
| 104 | + if (isMarketDetailsIntent(finalData) && finalData.getQueryParameter("id") != null) { |
| 105 | + // 移除指定包名 |
| 106 | + if (intent.getPackage() != null) { |
| 107 | + XposedLog.d(TAG, getPackageName(), "Removed package=:" + intent.getPackage()); |
| 108 | + intent.setPackage(null); |
114 | 109 | } |
115 | | - param.getArgs()[index] = intent; |
116 | 110 |
|
117 | | - } catch (Throwable t) { |
118 | | - XposedLog.w(TAG, getPackageName(), "Error - " + Log.getStackTraceString(t)); |
| 111 | + // FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 会导致小米应用商店无法打开,原因未知 |
| 112 | + intent.removeFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
119 | 113 |
|
| 114 | + // 如果启用了 bypass_force_mi_appstore,则强制使用 chooser 打开应用商店 |
| 115 | + if (forceChooserEnabled) { |
| 116 | + intent = Intent.createChooser(intent, null); |
| 117 | + XposedLog.d(TAG, getPackageName(), "Forced chooser for market://details intent"); |
| 118 | + } |
120 | 119 | } |
| 120 | + |
| 121 | + args[intentIndex] = intent; |
| 122 | + } catch (Throwable t) { |
| 123 | + XposedLog.w(TAG, getPackageName(), "Error - " + Log.getStackTraceString(t)); |
121 | 124 | } |
122 | | - }); |
123 | | - XposedLog.d(TAG, getPackageName(), "Hooked method: " + method); |
124 | | - break; |
| 125 | + } |
| 126 | + }); |
| 127 | + if (handles == null || handles.isEmpty()) { |
| 128 | + XposedLog.w(TAG, getPackageName(), "No startActivity overload hooked"); |
| 129 | + } else { |
| 130 | + XposedLog.d(TAG, getPackageName(), "Hooked startActivity overloads: " + handles.size()); |
125 | 131 | } |
126 | 132 |
|
127 | 133 | } catch (Throwable t) { |
128 | 134 | XposedLog.w(TAG, getPackageName(), "Failed to hook - " + Log.getStackTraceString(t)); |
129 | 135 | } |
130 | 136 | } |
131 | 137 |
|
| 138 | + private static int findIntentArgIndex(Object[] args) { |
| 139 | + if (args == null) return -1; |
| 140 | + for (int i = 0; i < args.length; i++) { |
| 141 | + if (args[i] instanceof Intent) return i; |
| 142 | + } |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + private static boolean isMarketDetailsHost(Uri uri) { |
| 147 | + return uri != null |
| 148 | + && "market".equals(uri.getScheme()) |
| 149 | + && "details".equals(uri.getHost()); |
| 150 | + } |
| 151 | + |
| 152 | + private static boolean isMarketDetailsIntent(Uri uri) { |
| 153 | + if (!isMarketDetailsHost(uri)) return false; |
| 154 | + |
| 155 | + String path = uri.getPath(); |
| 156 | + return path == null || path.isEmpty() || "/".equals(path) || "/detailmini".equals(path); |
| 157 | + } |
132 | 158 |
|
133 | 159 | } |
0 commit comments