Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 64cacec1 authored by atrost's avatar atrost
Browse files

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
parent 9ff2a6a0
Loading
Loading
Loading
Loading
+35 −5
Original line number Diff line number Diff line
@@ -43,11 +43,6 @@ interface IPlatformCompat
    /**
     * Reports that a compatibility change is affecting an app process now.
     *
     * <p>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.
     *
     * <p>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.
     *
     * <p>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.
     *
     * <p>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.
     *
     * <p>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.
     *
     * <p>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
+23 −5
Original line number Diff line number Diff line
@@ -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);
    }
}