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

Commit 16d90af8 authored by Matías Hernández's avatar Matías Hernández
Browse files

Properly distinguish origin of Zen Changes

Add entry points to NotificationManager and INotificationManager so that Settings and SystemUI can tag Zen operations as "fromUser" (apps cannot do this, except for setAutomaticZenRulke via the Condition.source field). Also update SystemUI callers to provide "fromUser == true" where it makes sense (which is almost everywhere).

This effectively separates the "System" and "User" origins (the former being reserved to changes that are not originated from the user, such as schedule-based rules starting or ending).

Bug: 308670715
Flag: ACONFIG android.app.modes_api DEVELOPMENT
Test: atest NotificationManagerServiceTest ZenModeHelperTest DefaultDeviceEffectsApplierTest
Change-Id: If1e74e7989697c3126fd50636347488bfe3cda51
parent 918378f0
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ interface INotificationManager
    void requestInterruptionFilterFromListener(in INotificationListener token, int interruptionFilter);
    int getInterruptionFilterFromListener(in INotificationListener token);
    void setOnNotificationPostedTrimFromListener(in INotificationListener token, int trim);
    void setInterruptionFilter(String pkg, int interruptionFilter);
    void setInterruptionFilter(String pkg, int interruptionFilter, boolean fromUser);

    void updateNotificationChannelGroupFromPrivilegedListener(in INotificationListener token, String pkg, in UserHandle user, in NotificationChannelGroup group);
    void updateNotificationChannelFromPrivilegedListener(in INotificationListener token, String pkg, in UserHandle user, in NotificationChannel channel);
@@ -205,11 +205,11 @@ interface INotificationManager
    @UnsupportedAppUsage
    ZenModeConfig getZenModeConfig();
    NotificationManager.Policy getConsolidatedNotificationPolicy();
    oneway void setZenMode(int mode, in Uri conditionId, String reason);
    oneway void setZenMode(int mode, in Uri conditionId, String reason, boolean fromUser);
    oneway void notifyConditions(String pkg, in IConditionProvider provider, in Condition[] conditions);
    boolean isNotificationPolicyAccessGranted(String pkg);
    NotificationManager.Policy getNotificationPolicy(String pkg);
    void setNotificationPolicy(String pkg, in NotificationManager.Policy policy);
    void setNotificationPolicy(String pkg, in NotificationManager.Policy policy, boolean fromUser);
    boolean isNotificationPolicyAccessGrantedForPackage(String pkg);
    void setNotificationPolicyAccessGranted(String pkg, boolean granted);
    void setNotificationPolicyAccessGrantedForUser(String pkg, int userId, boolean granted);
@@ -217,12 +217,12 @@ interface INotificationManager
    Map<String, AutomaticZenRule> getAutomaticZenRules();
    // TODO: b/310620812 - Remove getZenRules() when MODES_API is inlined.
    List<ZenModeConfig.ZenRule> getZenRules();
    String addAutomaticZenRule(in AutomaticZenRule automaticZenRule, String pkg);
    boolean updateAutomaticZenRule(String id, in AutomaticZenRule automaticZenRule);
    boolean removeAutomaticZenRule(String id);
    boolean removeAutomaticZenRules(String packageName);
    String addAutomaticZenRule(in AutomaticZenRule automaticZenRule, String pkg, boolean fromUser);
    boolean updateAutomaticZenRule(String id, in AutomaticZenRule automaticZenRule, boolean fromUser);
    boolean removeAutomaticZenRule(String id, boolean fromUser);
    boolean removeAutomaticZenRules(String packageName, boolean fromUser);
    int getRuleInstanceCount(in ComponentName owner);
    void setAutomaticZenRuleState(String id, in Condition condition);
    void setAutomaticZenRuleState(String id, in Condition condition, boolean fromUser);

    byte[] getBackupPayload(int user);
    void applyRestore(in byte[] payload, int user);
+59 −8
Original line number Diff line number Diff line
@@ -1184,14 +1184,20 @@ public class NotificationManager {
     */
    @UnsupportedAppUsage
    public void setZenMode(int mode, Uri conditionId, String reason) {
        setZenMode(mode, conditionId, reason, /* fromUser= */ false);
    }

    /** @hide */
    public void setZenMode(int mode, Uri conditionId, String reason, boolean fromUser) {
        INotificationManager service = getService();
        try {
            service.setZenMode(mode, conditionId, reason);
            service.setZenMode(mode, conditionId, reason, fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }


    /**
     * @hide
     */
@@ -1325,9 +1331,15 @@ public class NotificationManager {
     * @return The id of the newly created rule; null if the rule could not be created.
     */
    public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
        return addAutomaticZenRule(automaticZenRule, /* fromUser= */ false);
    }

    /** @hide */
    public String addAutomaticZenRule(AutomaticZenRule automaticZenRule, boolean fromUser) {
        INotificationManager service = getService();
        try {
            return service.addAutomaticZenRule(automaticZenRule, mContext.getPackageName());
            return service.addAutomaticZenRule(automaticZenRule,
                    mContext.getPackageName(), fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -1347,9 +1359,15 @@ public class NotificationManager {
     * @return Whether the rule was successfully updated.
     */
    public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
        return updateAutomaticZenRule(id, automaticZenRule, /* fromUser= */ false);
    }

    /** @hide */
    public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule,
            boolean fromUser) {
        INotificationManager service = getService();
        try {
            return service.updateAutomaticZenRule(id, automaticZenRule);
            return service.updateAutomaticZenRule(id, automaticZenRule, fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -1367,9 +1385,20 @@ public class NotificationManager {
     * @param condition The new state of this rule
     */
    public void setAutomaticZenRuleState(@NonNull String id, @NonNull Condition condition) {
        if (Flags.modesApi()) {
            setAutomaticZenRuleState(id, condition,
                    /* fromUser= */ condition.source == Condition.SOURCE_USER_ACTION);
        } else {
            setAutomaticZenRuleState(id, condition, /* fromUser= */ false);
        }
    }

    /** @hide */
    public void setAutomaticZenRuleState(@NonNull String id, @NonNull Condition condition,
            boolean fromUser) {
        INotificationManager service = getService();
        try {
            service.setAutomaticZenRuleState(id, condition);
            service.setAutomaticZenRuleState(id, condition, fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -1388,9 +1417,14 @@ public class NotificationManager {
     * @return Whether the rule was successfully deleted.
     */
    public boolean removeAutomaticZenRule(String id) {
        return removeAutomaticZenRule(id, /* fromUser= */ false);
    }

    /** @hide */
    public boolean removeAutomaticZenRule(String id, boolean fromUser) {
        INotificationManager service = getService();
        try {
            return service.removeAutomaticZenRule(id);
            return service.removeAutomaticZenRule(id, fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -1402,9 +1436,14 @@ public class NotificationManager {
     * @hide
     */
    public boolean removeAutomaticZenRules(String packageName) {
        return removeAutomaticZenRules(packageName, /* fromUser= */ false);
    }

    /** @hide */
    public boolean removeAutomaticZenRules(String packageName, boolean fromUser) {
        INotificationManager service = getService();
        try {
            return service.removeAutomaticZenRules(packageName);
            return service.removeAutomaticZenRules(packageName, fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -1685,10 +1724,15 @@ public class NotificationManager {
     */
    // TODO(b/309457271): Update documentation with VANILLA_ICE_CREAM behavior.
    public void setNotificationPolicy(@NonNull Policy policy) {
        setNotificationPolicy(policy, /* fromUser= */ false);
    }

    /** @hide */
    public void setNotificationPolicy(@NonNull Policy policy, boolean fromUser) {
        checkRequired("policy", policy);
        INotificationManager service = getService();
        try {
            service.setNotificationPolicy(mContext.getOpPackageName(), policy);
            service.setNotificationPolicy(mContext.getOpPackageName(), policy, fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -2685,9 +2729,16 @@ public class NotificationManager {
     */
    // TODO(b/309457271): Update documentation with VANILLA_ICE_CREAM behavior.
    public final void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
        setInterruptionFilter(interruptionFilter, /* fromUser= */ false);
    }

    /** @hide */
    public final void setInterruptionFilter(@InterruptionFilter int interruptionFilter,
            boolean fromUser) {
        final INotificationManager service = getService();
        try {
            service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter);
            service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter,
                    fromUser);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+11 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Flags;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
@@ -143,10 +144,17 @@ public class EnableZenModeDialog {
                                    Slog.d(TAG, "Invalid manual condition: " + tag.condition);
                                }
                                // always triggers priority-only dnd with chosen condition
                                if (Flags.modesApi()) {
                                    mNotificationManager.setZenMode(
                                            Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
                                            getRealConditionId(tag.condition), TAG,
                                            /* fromUser= */ true);
                                } else {
                                    mNotificationManager.setZenMode(
                                            Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
                                            getRealConditionId(tag.condition), TAG);
                                }
                            }
                        });

        if (mCancelIsNeutral) {
+6 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.statusbar.policy;

import android.app.AlarmManager;
import android.app.Flags;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
@@ -191,8 +192,12 @@ public class ZenModeControllerImpl implements ZenModeController, Dumpable {

    @Override
    public void setZen(int zen, Uri conditionId, String reason) {
        if (Flags.modesApi()) {
            mNoMan.setZenMode(zen, conditionId, reason, /* fromUser= */ true);
        } else {
            mNoMan.setZenMode(zen, conditionId, reason);
        }
    }

    @Override
    public boolean isZenAvailable() {
+1 −1
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ public class ConditionProviders extends ManagedServices {
            if (pkgList != null && (pkgList.length > 0)) {
                for (String pkgName : pkgList) {
                    try {
                        inm.removeAutomaticZenRules(pkgName);
                        inm.removeAutomaticZenRules(pkgName, /* fromUser= */ false);
                        inm.setNotificationPolicyAccessGranted(pkgName, false);
                    } catch (Exception e) {
                        Slog.e(TAG, "Failed to clean up rules for " + pkgName, e);
Loading