From 0d4157357a047bbd323a2068edbcd64cf72c8a28 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 16 Jun 2023 23:59:52 +0200 Subject: [PATCH] Use system context as base context DONOTMERGE: it causes #994 on Xiaomi devices This allows to make Context.getPackageManager() work. Fixes #4015 Refs Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com> --- .../com/genymobile/scrcpy/FakeContext.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/FakeContext.java b/server/src/main/java/com/genymobile/scrcpy/FakeContext.java index 738203dedc..ea34857ac1 100644 --- a/server/src/main/java/com/genymobile/scrcpy/FakeContext.java +++ b/server/src/main/java/com/genymobile/scrcpy/FakeContext.java @@ -1,11 +1,16 @@ package com.genymobile.scrcpy; +import com.genymobile.scrcpy.wrappers.ActivityThread; + 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.Method; + public final class FakeContext extends ContextWrapper { public static final String PACKAGE_NAME = "com.android.shell"; @@ -13,12 +18,25 @@ public final class FakeContext extends ContextWrapper { private static final FakeContext INSTANCE = new FakeContext(); + private static Context retrieveSystemContext() { + try { + Class activityThreadClass = ActivityThread.getActivityThreadClass(); + Object activityThread = ActivityThread.getActivityThread(); + + Method getSystemContextMethod = activityThreadClass.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