Skip to content

Commit 23d841c

Browse files
committed
refactor: use dynamic intent lookup and unify interceptor logic
1 parent 268303d commit 23d841c

1 file changed

Lines changed: 22 additions & 64 deletions

File tree

  • app/src/main/java/com/yureitzk/nophotopickerapi

app/src/main/java/com/yureitzk/nophotopickerapi/MainHook.kt

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -58,47 +58,17 @@ class MainHook : IXposedHookLoadPackage {
5858
}
5959
}
6060
}
61-
private fun hookInstrumentation(lpparam: XC_LoadPackage.LoadPackageParam) {
62-
try {
63-
XposedHelpers.findAndHookMethod(
64-
android.app.Instrumentation::class.java,
65-
"execStartActivity",
66-
android.content.Context::class.java,
67-
android.os.IBinder::class.java,
68-
android.os.IBinder::class.java,
69-
Activity::class.java,
70-
Intent::class.java,
71-
Int::class.javaPrimitiveType,
72-
android.os.Bundle::class.java,
73-
object : XC_MethodHook() {
74-
override fun beforeHookedMethod(param: MethodHookParam) {
75-
val intent = param.args[4] as? Intent ?: return
76-
if (isPhotoPickerIntent(intent)) {
77-
logIntentDetails(intent, "Instrumentation.execStartActivity")
78-
param.args[4] = buildDocumentPickerIntent(intent)
79-
}
80-
}
81-
}
82-
)
83-
Log.d(TAG, "Hooked Instrumentation for ${lpparam.packageName}")
84-
} catch (t: Throwable) {
85-
XposedBridge.log("$TAG: Failed to hook Instrumentation: ${t.message}")
86-
}
87-
}
8861

8962
private fun createIntentInterceptor(source: String): XC_MethodHook {
9063
return object : XC_MethodHook() {
9164
override fun beforeHookedMethod(param: MethodHookParam) {
92-
val args = param.args
65+
val args = param.args ?: return
9366
for (i in args.indices) {
9467
if (args[i] is Intent) {
9568
val intent = args[i] as Intent
96-
9769
if (isPhotoPickerIntent(intent)) {
98-
logIntentDetails(intent, "$source.startActivity")
99-
70+
logIntentDetails(intent, source)
10071
val newIntent = buildDocumentPickerIntent(intent)
101-
10272
args[i] = newIntent
10373

10474
if (i + 1 < args.size && (args[i + 1] == null || args[i + 1] is String)) {
@@ -113,42 +83,30 @@ class MainHook : IXposedHookLoadPackage {
11383
}
11484
}
11585

116-
private fun hookActivity(lpparam: XC_LoadPackage.LoadPackageParam) {
86+
private fun hookInstrumentation(lpparam: XC_LoadPackage.LoadPackageParam) {
11787
try {
118-
XposedHelpers.findAndHookMethod(
119-
Activity::class.java,
120-
"startActivity",
121-
Intent::class.java,
122-
object : XC_MethodHook() {
123-
override fun beforeHookedMethod(param: MethodHookParam) {
124-
val intent = param.args[0] as? Intent ?: return
125-
126-
if (intent.hasExtra(FLAG)) return
127-
128-
if (isPhotoPickerIntent(intent)) {
129-
logIntentDetails(intent, "App.Activity.startActivity")
130-
param.args[0] = buildDocumentPickerIntent(intent)
131-
}
132-
}
133-
}
88+
XposedBridge.hookAllMethods(
89+
android.app.Instrumentation::class.java,
90+
"execStartActivity",
91+
createIntentInterceptor("Instrumentation.execStartActivity")
13492
)
93+
Log.d(TAG, "Hooked Instrumentation for ${lpparam.packageName}")
94+
} catch (t: Throwable) {
95+
XposedBridge.log("$TAG: Failed to hook Instrumentation: ${t.message}")
96+
}
97+
}
13598

136-
XposedHelpers.findAndHookMethod(
137-
Activity::class.java,
138-
"startActivityForResult",
139-
Intent::class.java,
140-
Int::class.javaPrimitiveType,
141-
object : XC_MethodHook() {
142-
override fun beforeHookedMethod(param: MethodHookParam) {
143-
val intent = param.args[0] as? Intent ?: return
144-
if (isPhotoPickerIntent(intent)) {
145-
logIntentDetails(intent, "App.startActivityForResult")
146-
param.args[0] = buildDocumentPickerIntent(intent)
147-
}
148-
}
149-
}
150-
)
99+
private fun hookActivity(lpparam: XC_LoadPackage.LoadPackageParam) {
100+
try {
101+
val activityMethods = listOf("startActivity", "startActivityForResult")
151102

103+
for (methodName in activityMethods) {
104+
XposedBridge.hookAllMethods(
105+
Activity::class.java,
106+
methodName,
107+
createIntentInterceptor("App.Activity.$methodName")
108+
)
109+
}
152110
Log.d(TAG, "Successfully hooked Activity methods for ${lpparam.packageName}")
153111
} catch (t: Throwable) {
154112
XposedBridge.log("$TAG: Failed to hook Activity: ${t.message}")

0 commit comments

Comments
 (0)