Expose APK path from Java

This commit is contained in:
Romain Vimont 2022-10-02 16:35:50 +02:00
parent 2bc1f59b5b
commit 64b70724e0
3 changed files with 64 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import com.genymobile.scrcpy.wrappers.SurfaceControl;
import com.genymobile.scrcpy.wrappers.WindowManager;
import android.content.IOnPrimaryClipChangedListener;
import android.content.pm.ApplicationInfo;
import android.graphics.Rect;
import android.os.Build;
import android.os.IBinder;
@ -21,6 +22,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
public final class Device {
private static final String SCRCPY_PACKAGE_NAME = "com.genymobile.scrcpy";
public static final int POWER_MODE_OFF = SurfaceControl.POWER_MODE_OFF;
public static final int POWER_MODE_NORMAL = SurfaceControl.POWER_MODE_NORMAL;
@ -319,4 +322,12 @@ public final class Device {
public static Settings getSettings() {
return SETTINGS;
}
public static String getApkPath() {
ApplicationInfo info = SERVICE_MANAGER.getPackageManager().getApplicationInfo(SCRCPY_PACKAGE_NAME);
if (info == null) {
return null;
}
return info.sourceDir;
}
}

View file

@ -0,0 +1,36 @@
package com.genymobile.scrcpy.wrappers;
import com.genymobile.scrcpy.Ln;
import android.content.pm.ApplicationInfo;
import android.os.IInterface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class PackageManager {
private IInterface manager;
private Method getApplicationInfoMethod;
public PackageManager(IInterface manager) {
this.manager = manager;
}
private Method getGetApplicationInfoMethod() throws NoSuchMethodException {
if (getApplicationInfoMethod == null) {
getApplicationInfoMethod = manager.getClass().getDeclaredMethod("getApplicationInfo", String.class, int.class, int.class);
}
return getApplicationInfoMethod;
}
public ApplicationInfo getApplicationInfo(String packageName) {
try {
Method method = getGetApplicationInfoMethod();
return (ApplicationInfo) method.invoke(manager, packageName, 0, ServiceManager.USER_ID);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
Ln.e("Cannot get application info", e);
return null;
}
}
}

View file

@ -22,6 +22,7 @@ public final class ServiceManager {
private StatusBarManager statusBarManager;
private ClipboardManager clipboardManager;
private ActivityManager activityManager;
private PackageManager packageManager;
public ServiceManager() {
try {
@ -119,4 +120,20 @@ public final class ServiceManager {
return activityManager;
}
public PackageManager getPackageManager() {
if (packageManager == null) {
try {
//IInterface manager = getService("package", "android.content.pm.IPackageManager");
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
Method getPackageManager = activityThreadClass.getDeclaredMethod("getPackageManager");
IInterface manager = (IInterface) getPackageManager.invoke(null);
return new PackageManager(manager);
} catch (Exception e) {
throw new AssertionError(e);
}
}
return packageManager;
}
}