Skip to content

Commit 49993d8

Browse files
committed
make post-debuggable apps shows in developer settings
1 parent 72e7e1d commit 49993d8

1 file changed

Lines changed: 65 additions & 15 deletions

File tree

app/src/main/java/tw/idv/palatis/xappdebug/xposed/HookMain.java

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,105 @@
11
package tw.idv.palatis.xappdebug.xposed;
22

3+
import static android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE;
4+
import static android.util.Log.getStackTraceString;
5+
import static de.robv.android.xposed.XposedBridge.hookAllMethods;
6+
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
7+
import static tw.idv.palatis.xappdebug.Constants.CONFIG_PATH_FORMAT;
8+
import static tw.idv.palatis.xappdebug.Constants.LOG_TAG;
9+
310
import android.annotation.SuppressLint;
11+
import android.content.pm.ApplicationInfo;
412
import android.content.pm.PackageInfo;
513
import android.os.StrictMode;
614
import android.os.UserHandle;
715

16+
import androidx.annotation.Keep;
17+
18+
import java.util.List;
819
import java.util.Locale;
920

10-
import androidx.annotation.Keep;
1121
import de.robv.android.xposed.IXposedHookLoadPackage;
1222
import de.robv.android.xposed.SELinuxHelper;
1323
import de.robv.android.xposed.XC_MethodHook;
1424
import de.robv.android.xposed.XposedBridge;
1525
import de.robv.android.xposed.callbacks.XC_LoadPackage;
1626

17-
import static android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE;
18-
import static android.util.Log.getStackTraceString;
19-
import static de.robv.android.xposed.XposedBridge.hookAllMethods;
20-
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
21-
import static tw.idv.palatis.xappdebug.Constants.CONFIG_PATH_FORMAT;
22-
import static tw.idv.palatis.xappdebug.Constants.LOG_TAG;
23-
2427
@Keep
2528
public class HookMain implements IXposedHookLoadPackage {
2629

2730
// taken from Zygote.java
2831
// https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/com/android/internal/os/Zygote.java
2932
private static final int DEBUG_ENABLE_JDWP = 1;
3033

34+
private static final String PACKAGE_MANAGER_SERVICE_CLASS = "com.android.server.pm.PackageManagerService";
35+
3136
@Override
3237
public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
3338
if (!"android".equals(lpparam.packageName))
3439
return;
3540

3641
findAndHookMethod(
37-
"com.android.server.pm.PackageManagerService",
42+
PACKAGE_MANAGER_SERVICE_CLASS,
3843
lpparam.classLoader,
3944
"getPackageInfo",
4045
String.class, int.class, int.class, /* packageName, flags, userId */
4146
new XC_MethodHook() {
4247
@Override
4348
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
4449
try {
45-
final PackageInfo packageInfo = (PackageInfo) param.getResult();
50+
final int userId = (int) param.args[2];
51+
final PackageInfo info = (PackageInfo) param.getResult();
52+
if (info == null)
53+
return;
54+
if (!isDebuggable(info.packageName, userId))
55+
return;
56+
info.applicationInfo.flags |= FLAG_DEBUGGABLE;
57+
} catch (Exception e) {
58+
XposedBridge.log(LOG_TAG + ": " + getStackTraceString(e));
59+
}
60+
}
61+
}
62+
);
4663

47-
if (packageInfo != null) {
48-
if (!isDebuggable(packageInfo.packageName, (int) param.args[2]))
49-
return;
64+
findAndHookMethod(
65+
PACKAGE_MANAGER_SERVICE_CLASS,
66+
lpparam.classLoader,
67+
"getApplicationInfo",
68+
String.class, int.class, int.class, /* packageName, flags, userId */
69+
new XC_MethodHook() {
70+
@Override
71+
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
72+
try {
73+
final int userId = (int) param.args[2];
74+
final ApplicationInfo appInfo = (ApplicationInfo) param.getResult();
75+
if (appInfo == null)
76+
return;
77+
if (!isDebuggable(appInfo.packageName, userId))
78+
return;
79+
appInfo.flags |= FLAG_DEBUGGABLE;
80+
} catch (Exception e) {
81+
XposedBridge.log(LOG_TAG + ": " + getStackTraceString(e));
82+
}
83+
}
84+
}
85+
);
5086

51-
packageInfo.applicationInfo.flags |= FLAG_DEBUGGABLE;
52-
param.setResult(packageInfo);
87+
findAndHookMethod(
88+
PACKAGE_MANAGER_SERVICE_CLASS,
89+
lpparam.classLoader,
90+
"getInstalledApplicationsListInternal",
91+
int.class, int.class, int.class, /* flags, userId, callingUid */
92+
new XC_MethodHook() {
93+
@Override
94+
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
95+
try {
96+
final int userId = (int) param.args[1];
97+
final List<ApplicationInfo> infos = (List<ApplicationInfo>) param.getResult();
98+
if (infos == null)
99+
return;
100+
for (ApplicationInfo info : infos) {
101+
if (isDebuggable(info.packageName, userId))
102+
info.flags |= FLAG_DEBUGGABLE;
53103
}
54104
} catch (Exception e) {
55105
XposedBridge.log(LOG_TAG + ": " + getStackTraceString(e));

0 commit comments

Comments
 (0)