Skip to content

Commit

Permalink
Use FakeContext for Application instance
Browse files Browse the repository at this point in the history
This will expose the correct package name and UID to the application
context.

PR #3757 <#3757>
  • Loading branch information
rom1v committed Mar 3, 2023
1 parent 013bf96 commit 484f38b
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions server/src/main/java/com/genymobile/scrcpy/Workarounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Instrumentation;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.os.Looper;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public final class Workarounds {
private Workarounds() {
Expand Down Expand Up @@ -62,11 +60,10 @@ public static void fillAppInfo() {
mBoundApplicationField.setAccessible(true);
mBoundApplicationField.set(activityThread, appBindData);

// Context ctx = activityThread.getSystemContext();
Method getSystemContextMethod = activityThreadClass.getDeclaredMethod("getSystemContext");
Context ctx = (Context) getSystemContextMethod.invoke(activityThread);

Application app = Instrumentation.newApplication(Application.class, ctx);
Application app = Application.class.newInstance();
Field baseField = ContextWrapper.class.getDeclaredField("mBase");
baseField.setAccessible(true);
baseField.set(app, FakeContext.get());

// activityThread.mInitialApplication = app;
Field mInitialApplicationField = activityThreadClass.getDeclaredField("mInitialApplication");
Expand Down

0 comments on commit 484f38b

Please sign in to comment.