From 64cacec12b48bdc025e93b00249cbbc6ab5e384b Mon Sep 17 00:00:00 2001 From: atrost Date: Thu, 3 Oct 2019 15:21:24 +0100 Subject: [PATCH] Add UID APIs to PlatformCompat. Needed for scoped storage (to be used from MediaProvider). IsChangeEnabled returns true if there are no installed packages for the required UID, or if the change is enabled for ALL of the installed packages associated with the provided UID. Bug: 138275545 Test: Test app Change-Id: I6785f57e4fcfb92ed7529f4da0a6db6ffe8b3ad4 --- .../internal/compat/IPlatformCompat.aidl | 40 ++++++++++++++++--- .../android/server/compat/PlatformCompat.java | 28 ++++++++++--- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/core/java/com/android/internal/compat/IPlatformCompat.aidl b/core/java/com/android/internal/compat/IPlatformCompat.aidl index e415b41459ab5..4d8378a345999 100644 --- a/core/java/com/android/internal/compat/IPlatformCompat.aidl +++ b/core/java/com/android/internal/compat/IPlatformCompat.aidl @@ -43,11 +43,6 @@ interface IPlatformCompat /** * Reports that a compatibility change is affecting an app process now. * - *

Same as {@link #reportChange(long, ApplicationInfo)}, except it receives a package name - * instead of an {@link ApplicationInfo} - * object, and finds an app info object based on the package name. Returns {@code true} if - * there is no installed package by that name. - * *

Note: for changes that are gated using {@link #isChangeEnabled(long, String)}, * you do not need to call this API directly. The change will be reported for you. * @@ -56,6 +51,17 @@ interface IPlatformCompat */ void reportChangeByPackageName(long changeId, in String packageName); + /** + * Reports that a compatibility change is affecting an app process now. + * + *

Note: for changes that are gated using {@link #isChangeEnabled(long, int)}, + * you do not need to call this API directly. The change will be reported for you. + * + * @param changeId The ID of the compatibility change taking effect. + * @param uid The UID of the app in question. + */ + void reportChangeByUid(long changeId, int uid); + /** * Query if a given compatibility change is enabled for an app process. This method should * be called when implementing functionality on behalf of the affected app. @@ -95,4 +101,28 @@ interface IPlatformCompat * @return {@code true} if the change is enabled for the current app. */ boolean isChangeEnabledByPackageName(long changeId, in String packageName); + + /** + * Query if a given compatibility change is enabled for an app process. This method should + * be called when implementing functionality on behalf of the affected app. + * + *

Same as {@link #isChangeEnabled(long, ApplicationInfo)}, except it receives a uid + * instead of an {@link ApplicationInfo} object, and finds an app info object based on the + * uid (or objects if there's more than one package associated with the UID). + * Returns {@code true} if there are no installed packages for the required UID, or if the + * change is enabled for ALL of the installed packages associated with the provided UID. Please + * use a more specific API if you want a different behaviour for multi-package UIDs. + * + *

If this method returns {@code true}, the calling code should implement the compatibility + * change, resulting in differing behaviour compared to earlier releases. If this method + * returns {@code false}, the calling code should behave as it did in earlier releases. + * + *

It will also report the change as {@link #reportChange(long, int)} would, so there is + * no need to call that method directly. + * + * @param changeId The ID of the compatibility change in question. + * @param uid The UID of the app in question. + * @return {@code true} if the change is enabled for the current app. + */ + boolean isChangeEnabledByUid(long changeId, int uid); } \ No newline at end of file diff --git a/services/core/java/com/android/server/compat/PlatformCompat.java b/services/core/java/com/android/server/compat/PlatformCompat.java index 8e09d0e5958e7..852b26d1ff07d 100644 --- a/services/core/java/com/android/server/compat/PlatformCompat.java +++ b/services/core/java/com/android/server/compat/PlatformCompat.java @@ -47,7 +47,8 @@ public class PlatformCompat extends IPlatformCompat.Stub { @Override public void reportChange(long changeId, ApplicationInfo appInfo) { - reportChange(changeId, appInfo, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED); + reportChange(changeId, appInfo.uid, + StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED); } @Override @@ -59,14 +60,19 @@ public class PlatformCompat extends IPlatformCompat.Stub { reportChange(changeId, appInfo); } + @Override + public void reportChangeByUid(long changeId, int uid) { + reportChange(changeId, uid, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED); + } + @Override public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) { if (CompatConfig.get().isChangeEnabled(changeId, appInfo)) { - reportChange(changeId, appInfo, + reportChange(changeId, appInfo.uid, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED); return true; } - reportChange(changeId, appInfo, + reportChange(changeId, appInfo.uid, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED); return false; } @@ -80,6 +86,19 @@ public class PlatformCompat extends IPlatformCompat.Stub { return isChangeEnabled(changeId, appInfo); } + @Override + public boolean isChangeEnabledByUid(long changeId, int uid) { + String[] packages = mContext.getPackageManager().getPackagesForUid(uid); + if (packages == null || packages.length == 0) { + return true; + } + boolean enabled = true; + for (String packageName : packages) { + enabled = enabled && isChangeEnabledByPackageName(changeId, packageName); + } + return enabled; + } + @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, "platform_compat", pw)) return; @@ -95,8 +114,7 @@ public class PlatformCompat extends IPlatformCompat.Stub { return null; } - private void reportChange(long changeId, ApplicationInfo appInfo, int state) { - int uid = appInfo.uid; + private void reportChange(long changeId, int uid, int state) { mChangeReporter.reportChange(uid, changeId, state); } }