systemctx

This commit is contained in:
Romain Vimont 2023-06-16 11:40:54 +02:00
parent 6414613113
commit ce8d3ac6d1

View file

@ -2,10 +2,14 @@ package com.genymobile.scrcpy;
import android.annotation.TargetApi;
import android.content.AttributionSource;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.os.Process;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public final class FakeContext extends ContextWrapper {
public static final String PACKAGE_NAME = "com.android.shell";
@ -13,12 +17,27 @@ public final class FakeContext extends ContextWrapper {
private static final FakeContext INSTANCE = new FakeContext();
private static Context retrieveSystemContext() {
try {
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
Constructor<?> activityThreadConstructor = activityThreadClass.getDeclaredConstructor();
activityThreadConstructor.setAccessible(true);
Object activityThread = activityThreadConstructor.newInstance();
Method getSystemContextMethod = activityThread.getClass().getDeclaredMethod("getSystemContext");
return (Context) getSystemContextMethod.invoke(activityThread);
} catch (Exception e) {
Ln.e("Cannot retrieve system context", e);
return null;
}
}
public static FakeContext get() {
return INSTANCE;
}
private FakeContext() {
super(null);
super(retrieveSystemContext());
}
@Override