Skip to content

Commit

Permalink
cleanup: usage of PACKAGE_NAME + PACKAGE_SHELL
Browse files Browse the repository at this point in the history
  • Loading branch information
RiggiG committed Jun 30, 2023
1 parent 87ca353 commit dae2c20
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
16 changes: 4 additions & 12 deletions server/src/main/java/com/genymobile/scrcpy/FakeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

public final class FakeContext extends MutableContextWrapper {

public static final String PACKAGE_NAME = Os.getuid() == 1000 ? "android" : "com.android.shell";
public static final String PACKAGE_SHELL = "com.android.shell";
public static final int ROOT_UID = 0; // Like android.os.Process.ROOT_UID, but before API 29

Expand All @@ -24,29 +25,20 @@ private FakeContext() {

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

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

public static String getPackageNameStatic() {
if (Os.getuid() == 1000) {
return "android";
}
return PACKAGE_SHELL;
}

@TargetApi(Build.VERSION_CODES.S)
@Override
public AttributionSource getAttributionSource() {
AttributionSource.Builder builder = new AttributionSource.Builder(Process.SHELL_UID);
builder.setPackageName(PACKAGE_SHELL);
if (Os.getuid() == 1000) {
builder.setPackageName("android");
}
builder.setPackageName(PACKAGE_NAME);
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static void fillAppInfo() {
Object appBindData = appBindDataConstructor.newInstance();

ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.packageName = FakeContext.getPackageNameStatic();
applicationInfo.packageName = FakeContext.PACKAGE_NAME;

// appBindData.appInfo = applicationInfo;
Field appInfoField = appBindDataClass.getDeclaredField("appInfo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public int startActivityAsUserWithFeature(Intent intent) {
return (int) method.invoke(
/* this */ manager,
/* caller */ null,
/* callingPackage */ FakeContext.getPackageNameStatic(),
/* callingPackage */ FakeContext.PACKAGE_NAME,
/* callingFeatureId */ null,
/* intent */ intent,
/* resolvedType */ null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,37 @@ private Method getSetPrimaryClipMethod() throws NoSuchMethodException {
private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager)
throws InvocationTargetException, IllegalAccessException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic());
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME);
}

switch (methodVersion) {
case 0:
return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic(), FakeContext.ROOT_UID);
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
case 1:
return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID);
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
case 2:
return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID, 0);
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0);
default:
return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic(), FakeContext.ROOT_UID, null);
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID, null);
}
}

private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData)
throws InvocationTargetException, IllegalAccessException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, clipData, FakeContext.getPackageNameStatic());
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME);
return;
}

switch (methodVersion) {
case 0:
method.invoke(manager, clipData, FakeContext.getPackageNameStatic(), FakeContext.ROOT_UID);
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
break;
case 1:
method.invoke(manager, clipData, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID);
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
break;
default:
method.invoke(manager, clipData, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID, 0);
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0);
break;
}
}
Expand Down Expand Up @@ -141,19 +141,19 @@ public boolean setText(CharSequence text) {
private static void addPrimaryClipChangedListener(Method method, int methodVersion, IInterface manager,
IOnPrimaryClipChangedListener listener) throws InvocationTargetException, IllegalAccessException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, listener, FakeContext.getPackageNameStatic());
method.invoke(manager, listener, FakeContext.PACKAGE_NAME);
return;
}

switch (methodVersion) {
case 0:
method.invoke(manager, listener, FakeContext.getPackageNameStatic(), FakeContext.ROOT_UID);
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
break;
case 1:
method.invoke(manager, listener, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID);
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
break;
default:
method.invoke(manager, listener, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID, 0);
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ private Bundle call(String callMethod, String arg, Bundle extras)
} else {
switch (callMethodVersion) {
case 1:
args = new Object[]{FakeContext.getPackageNameStatic(), null, "settings", callMethod, arg, extras};
args = new Object[]{FakeContext.PACKAGE_NAME, null, "settings", callMethod, arg, extras};
break;
case 2:
args = new Object[]{FakeContext.getPackageNameStatic(), "settings", callMethod, arg, extras};
args = new Object[]{FakeContext.PACKAGE_NAME, "settings", callMethod, arg, extras};
break;
default:
args = new Object[]{FakeContext.getPackageNameStatic(), callMethod, arg, extras};
args = new Object[]{FakeContext.PACKAGE_NAME, callMethod, arg, extras};
break;
}
}
Expand Down

0 comments on commit dae2c20

Please sign in to comment.