Skip to content

Commit

Permalink
Attempt to fix #4392
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Nov 3, 2023
1 parent 446ea81 commit 65d6bdb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static FakeContext get() {
}

private FakeContext() {
super(null);
super(Workarounds.retrieveSystemContext());
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Workarounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.AudioRecord;
Expand All @@ -30,6 +31,11 @@ private Workarounds() {

public static void apply(boolean audio, boolean camera) {
Workarounds.prepareMainLooper();
try {
fillActivityThread();
} catch (Exception e) {
throw new AssertionError(e);
}

boolean mustFillAppInfo = false;
boolean mustFillBaseContext = false;
Expand Down Expand Up @@ -123,6 +129,17 @@ private static void fillAppInfo() {
ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.packageName = FakeContext.PACKAGE_NAME;

Application application = new Application() {
@Override
public String getOpPackageName() {
return FakeContext.PACKAGE_NAME;
}
};

Field initialApplicationField = activityThreadClass.getDeclaredField("mInitialApplication");
initialApplicationField.setAccessible(true);
initialApplicationField.set(activityThread, application);

// appBindData.appInfo = applicationInfo;
Field appInfoField = appBindDataClass.getDeclaredField("appInfo");
appInfoField.setAccessible(true);
Expand Down Expand Up @@ -306,4 +323,14 @@ public static AudioRecord createAudioRecord(int source, int sampleRate, int chan
throw new RuntimeException("Cannot create AudioRecord");
}
}

static Context retrieveSystemContext() {
try {
Method getSystemContextMethod = activityThreadClass.getDeclaredMethod("getSystemContext");
return (Context) getSystemContextMethod.invoke(activityThread);
} catch (Exception e) {
Ln.e("Cannot retrieve system context", e);
return null;
}
}
}

0 comments on commit 65d6bdb

Please sign in to comment.