From dacdc2e487ba004bc91ea495569ddcf194756434 Mon Sep 17 00:00:00 2001 From: RiggiG <44820045+RiggiG@users.noreply.github.com> Date: Fri, 30 Jun 2023 18:21:15 -0400 Subject: [PATCH] cleanup: usage of PACKAGE_NAME + PACKAGE_SHELL --- .../com/genymobile/scrcpy/FakeContext.java | 16 +++-------- .../com/genymobile/scrcpy/Workarounds.java | 2 +- .../scrcpy/wrappers/ActivityManager.java | 2 +- .../scrcpy/wrappers/ClipboardManager.java | 28 +++++++++---------- .../scrcpy/wrappers/ContentProvider.java | 6 ++-- 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/FakeContext.java b/server/src/main/java/com/genymobile/scrcpy/FakeContext.java index ba12917691..aaed560ca6 100644 --- a/server/src/main/java/com/genymobile/scrcpy/FakeContext.java +++ b/server/src/main/java/com/genymobile/scrcpy/FakeContext.java @@ -10,6 +10,7 @@ public final class FakeContext extends ContextWrapper { + 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 @@ -25,29 +26,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(); } diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java index 40ef8acb3d..448e7099fa 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java +++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java @@ -128,7 +128,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"); diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java index 591471f3d9..75115618ba 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java @@ -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, diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java index afc5ba85f5..73eac6f54c 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java @@ -82,40 +82,40 @@ 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); case 3: - return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic(), FakeContext.ROOT_UID, null); + return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID, null); default: // The last boolean parameter is "userOperate" - return (ClipData) method.invoke(manager, FakeContext.getPackageNameStatic(), null, FakeContext.ROOT_UID, 0, true); + return (ClipData) method.invoke(manager, FakeContext.FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0, true); } } 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; } } @@ -149,19 +149,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; } } diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java index 93bb065b60..8171988e90 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java @@ -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; } }