-
Notifications
You must be signed in to change notification settings - Fork 1
feat: adding package lister with app name and versions #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.mobilenext.devicekit; | ||
|
|
||
| import android.content.pm.ApplicationInfo; | ||
| import android.content.pm.PackageInfo; | ||
| import android.content.res.AssetManager; | ||
| import android.content.res.Configuration; | ||
| import android.content.res.Resources; | ||
| import android.os.IBinder; | ||
| import android.util.DisplayMetrics; | ||
|
|
||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Standalone entry point for listing installed packages via app_process. | ||
| * | ||
| * Usage: | ||
| * adb shell CLASSPATH=/path/to/classes.dex app_process / com.mobilenext.devicekit.PackageLister | ||
| * | ||
| * Outputs a JSON array of {packageName, appName, version} to stdout. | ||
| */ | ||
| public class PackageLister { | ||
|
|
||
| public static void main(String[] args) { | ||
| try { | ||
| // Get the IPackageManager binder via ServiceManager (hidden API) | ||
| Class<?> serviceManager = Class.forName("android.os.ServiceManager"); | ||
| Method getService = serviceManager.getMethod("getService", String.class); | ||
| IBinder binder = (IBinder) getService.invoke(null, "package"); | ||
|
|
||
| // Get IPackageManager.Stub.asInterface(binder) | ||
| Class<?> ipmStub = Class.forName("android.content.pm.IPackageManager$Stub"); | ||
| Method asInterface = ipmStub.getMethod("asInterface", IBinder.class); | ||
| Object ipm = asInterface.invoke(null, binder); | ||
|
|
||
| // Call getInstalledPackages(flags, userId) | ||
| Method getInstalledPackages = ipm.getClass().getMethod("getInstalledPackages", long.class, int.class); | ||
| Object parceledList = getInstalledPackages.invoke(ipm, 0L, 0); | ||
|
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: The declared signatures of android.content.pm.IPackageManager#getInstalledPackages differ between AOSP current master and android10-release. In current AOSP master (refs/heads/main), the relevant method is: ParceledListSlice<PackageInfo> getInstalledPackages( Citations:
🏁 Script executed: # Get the file content around the specified lines to understand context
head -60 "app/src/main/java/com/mobilenext/devicekit/PackageLister.java" | tail -40Repository: mobile-next/devicekit-android Length of output: 2090 Add fallback for different Lines 40–41 assume the method signature Implement a try-fallback chain to attempt the master signature first, then fall back to the older 🤖 Prompt for AI Agents |
||
|
|
||
| // ParceledListSlice.getList() | ||
| Method getList = parceledList.getClass().getMethod("getList"); | ||
| @SuppressWarnings("unchecked") | ||
| List<PackageInfo> packages = (List<PackageInfo>) getList.invoke(parceledList); | ||
|
|
||
| JSONArray result = new JSONArray(); | ||
|
|
||
| for (PackageInfo pkg : packages) { | ||
| String packageName = pkg.packageName; | ||
| String versionName = pkg.versionName != null ? pkg.versionName : ""; | ||
| String displayName = packageName; | ||
|
|
||
| ApplicationInfo appInfo = pkg.applicationInfo; | ||
| if (appInfo != null) { | ||
| if (appInfo.nonLocalizedLabel != null) { | ||
| displayName = appInfo.nonLocalizedLabel.toString(); | ||
| } else if (appInfo.labelRes != 0 && appInfo.sourceDir != null) { | ||
| try { | ||
| AssetManager assets = AssetManager.class.getDeclaredConstructor().newInstance(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Close Prompt for AI agents |
||
| Method addAssetPath = AssetManager.class.getMethod("addAssetPath", String.class); | ||
| addAssetPath.invoke(assets, appInfo.sourceDir); | ||
| Resources res = new Resources(assets, new DisplayMetrics(), new Configuration()); | ||
| String label = res.getString(appInfo.labelRes); | ||
| if (label != null && !label.isEmpty()) { | ||
| displayName = label; | ||
| } | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| JSONObject entry = new JSONObject(); | ||
| entry.put("packageName", packageName); | ||
| entry.put("appName", displayName); | ||
| entry.put("version", versionName); | ||
| result.put(entry); | ||
| } | ||
|
|
||
| System.out.println(result.toString()); | ||
| } catch (Exception e) { | ||
| System.err.println("Error: " + e.getMessage()); | ||
| e.printStackTrace(System.err); | ||
| System.exit(1); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Avoid hard-coding userId=0 when querying installed packages; it can return the wrong package set on multi-user devices.
Prompt for AI agents