Skip to content

Commit

Permalink
Add a fake Android Context
Browse files Browse the repository at this point in the history
Since scrcpy-server is not an Android application (it's a java
executable), it has no Context.

Some features will require a Context instance to get the package name
and the UID. Add a FakeContext for this purpose.

PR #3757 <#3757>

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
  • Loading branch information
yume-chan and rom1v committed Mar 3, 2023
1 parent e068fe4 commit b5ae5bf
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/FakeContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.genymobile.scrcpy;

import android.annotation.TargetApi;
import android.content.AttributionSource;
import android.content.ContextWrapper;
import android.os.Build;
import android.os.Process;

public final class FakeContext extends ContextWrapper {

public static final String PACKAGE_NAME = "com.android.shell";

private static final FakeContext INSTANCE = new FakeContext();

public static FakeContext get() {
return INSTANCE;
}

private FakeContext() {
super(null);
}

@Override
public String getPackageName() {
return PACKAGE_NAME;
}

@Override
public String getOpPackageName() {
return PACKAGE_NAME;
}

@TargetApi(Build.VERSION_CODES.S)
@Override
public AttributionSource getAttributionSource() {
AttributionSource.Builder builder = new AttributionSource.Builder(Process.SHELL_UID);
builder.setPackageName(PACKAGE_NAME);
return builder.build();
}
}

0 comments on commit b5ae5bf

Please sign in to comment.