Skip to content

Commit

Permalink
Use system context as base context
Browse files Browse the repository at this point in the history
DONOTMERGE: it causes #994 on Xiaomi devices

This allows to make Context.getPackageManager() work.

Fixes #4015 <#4015>
Refs <#4015 (comment)>

Co-authored-by: Simon Chan <[email protected]>
  • Loading branch information
rom1v and yume-chan committed Jun 16, 2023
1 parent 95e61e2 commit 0d41573
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/FakeContext.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
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";
public static final int ROOT_UID = 0; // Like android.os.Process.ROOT_UID, but before API 29

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
Expand Down

0 comments on commit 0d41573

Please sign in to comment.