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

Commit e646b287 authored by Nadav Bar's avatar Nadav Bar
Browse files

Filter app-ops from for foreground notifications

This fixes inconsistency of showing the app ops indicators in the status bar
and foreground notifications.
Specifically, the issue that were seen is that the microphone indicator was also shown for system apps
without a launcher that have the recording pre-granted.

Bug: 129263222.
Change-Id: I6f6faf1552d3721f6fc34c650b5601e90d172db6
Test: Manually.
parent 1f9256fa
Loading
Loading
Loading
Loading
+45 −9
Original line number Diff line number Diff line
@@ -204,23 +204,58 @@ public class AppOpsControllerImpl implements AppOpsController,
    }

    /**
     * Does the app-op item refer to a user sensitive permission. Only user sensitive permission
     * should be shown to the user by default.
     * Does the app-op code refer to a user sensitive permission for the specified user id
     * and package. Only user sensitive permission should be shown to the user by default.
     *
     * @param item The item
     * @param appOpCode The code of the app-op.
     * @param uid The uid of the user.
     * @param packageName The name of the package.
     *
     * @return {@code true} iff the app-op item is user sensitive
     */
    private boolean isUserSensitive(AppOpItem item) {
        String permission = AppOpsManager.opToPermission(item.getCode());
    private boolean isUserSensitive(int appOpCode, int uid, String packageName) {
        String permission = AppOpsManager.opToPermission(appOpCode);
        if (permission == null) {
            return false;
        }
        int permFlags = mContext.getPackageManager().getPermissionFlags(permission,
                item.getPackageName(), UserHandle.getUserHandleForUid(item.getUid()));
                packageName, UserHandle.getUserHandleForUid(uid));
        return (permFlags & PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0;
    }

    /**
     * Does the app-op item refer to an operation that should be shown to the user.
     * Only specficic ops (like SYSTEM_ALERT_WINDOW) or ops that refer to user sensitive
     * permission should be shown to the user by default.
     *
     * @param item The item
     *
     * @return {@code true} iff the app-op item should be shown to the user
     */
    private boolean isUserVisible(AppOpItem item) {
        return isUserVisible(item.getCode(), item.getUid(), item.getPackageName());
    }


    /**
     * Does the app-op, uid and package name, refer to an operation that should be shown to the
     * user. Only specficic ops (like {@link AppOpsManager.OP_SYSTEM_ALERT_WINDOW}) or
     * ops that refer to user sensitive permission should be shown to the user by default.
     *
     * @param item The item
     *
     * @return {@code true} iff the app-op for should be shown to the user
     */
    private boolean isUserVisible(int appOpCode, int uid, String packageName) {
        // currently OP_SYSTEM_ALERT_WINDOW does not correspond to a platform permission
        // which may be user senstive, so for now always show it to the user.
        if (appOpCode == AppOpsManager.OP_SYSTEM_ALERT_WINDOW) {
            return true;
        }

        return isUserSensitive(appOpCode, uid, packageName);
    }

    /**
     * Returns a copy of the list containing all the active AppOps that the controller tracks.
     *
@@ -245,7 +280,7 @@ public class AppOpsControllerImpl implements AppOpsController,
            for (int i = 0; i < numActiveItems; i++) {
                AppOpItem item = mActiveItems.get(i);
                if ((userId == UserHandle.USER_ALL || UserHandle.getUserId(item.getUid()) == userId)
                        && isUserSensitive(item)) {
                        && isUserVisible(item)) {
                    list.add(item);
                }
            }
@@ -255,7 +290,7 @@ public class AppOpsControllerImpl implements AppOpsController,
            for (int i = 0; i < numNotedItems; i++) {
                AppOpItem item = mNotedItems.get(i);
                if ((userId == UserHandle.USER_ALL || UserHandle.getUserId(item.getUid()) == userId)
                        && isUserSensitive(item)) {
                        && isUserVisible(item)) {
                    list.add(item);
                }
            }
@@ -281,7 +316,8 @@ public class AppOpsControllerImpl implements AppOpsController,
    }

    private void notifySuscribers(int code, int uid, String packageName, boolean active) {
        if (mCallbacksByCode.containsKey(code)) {
        if (mCallbacksByCode.containsKey(code)
                && isUserVisible(code, uid, packageName)) {
            for (Callback cb: mCallbacksByCode.get(code)) {
                cb.onActiveStateChanged(code, uid, packageName, active);
            }