From e743bda5dec3c81ed6292c06b97b0c9f41464907 Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Fri, 9 Sep 2016 11:56:48 -0700 Subject: [PATCH] Fix Zen mode for different notification usage types ZenModeHelper was only considering a subset of all notification usage types. The code was also iterating over the usage values, expecting them to be contiguous, which will break with the addition of new usage values. The update consists in: - defining in AudioAttributes an array of expected usage types - defining the suppression behavior for each usage type - have ZenModeHelper query the behavior for each usage in order to apply the corresponding muting behavior. Bug 29009099 Change-Id: If884dda6297f125c60c5775c757df4f782e22e53 --- media/java/android/media/AudioAttributes.java | 61 +++++++++++++++++++ .../server/notification/ZenModeHelper.java | 14 +++-- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/media/java/android/media/AudioAttributes.java b/media/java/android/media/AudioAttributes.java index 5286f8fa5ad31..89709ee6b95a1 100644 --- a/media/java/android/media/AudioAttributes.java +++ b/media/java/android/media/AudioAttributes.java @@ -24,6 +24,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; import android.util.Log; +import android.util.SparseIntArray; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -169,6 +170,66 @@ public final class AudioAttributes implements Parcelable { */ public final static int USAGE_VIRTUAL_SOURCE = 15; + /** + * IMPORTANT: when adding new usage types, add them to SDK_USAGES and update SUPPRESSIBLE_USAGES + * if applicable. + */ + + /** + * @hide + * Denotes a usage for notifications that do not expect immediate intervention from the user, + * will be muted when the Zen mode disables notifications + * @see #SUPPRESSIBLE_USAGES + */ + public final static int SUPPRESSIBLE_NOTIFICATION = 1; + /** + * @hide + * Denotes a usage for notifications that do expect immediate intervention from the user, + * will be muted when the Zen mode disables calls + * @see #SUPPRESSIBLE_USAGES + */ + public final static int SUPPRESSIBLE_CALL = 2; + + /** + * @hide + * Array of all usage types for calls and notifications to assign the suppression behavior, + * used by the Zen mode restrictions. + * @see com.android.server.notification.ZenModeHelper + */ + public static final SparseIntArray SUPPRESSIBLE_USAGES; + + static { + SUPPRESSIBLE_USAGES = new SparseIntArray(); + SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION, SUPPRESSIBLE_NOTIFICATION); + SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_RINGTONE, SUPPRESSIBLE_CALL); + SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_REQUEST,SUPPRESSIBLE_CALL); + SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_INSTANT,SUPPRESSIBLE_NOTIFICATION); + SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_DELAYED,SUPPRESSIBLE_NOTIFICATION); + SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_EVENT, SUPPRESSIBLE_NOTIFICATION); + } + + /** + * @hide + * Array of all usage types exposed in the SDK that applications can use. + */ + public final static int[] SDK_USAGES = { + USAGE_UNKNOWN, + USAGE_MEDIA, + USAGE_VOICE_COMMUNICATION, + USAGE_VOICE_COMMUNICATION_SIGNALLING, + USAGE_ALARM, + USAGE_NOTIFICATION, + USAGE_NOTIFICATION_RINGTONE, + USAGE_NOTIFICATION_COMMUNICATION_REQUEST, + USAGE_NOTIFICATION_COMMUNICATION_INSTANT, + USAGE_NOTIFICATION_COMMUNICATION_DELAYED, + USAGE_NOTIFICATION_EVENT, + USAGE_ASSISTANCE_ACCESSIBILITY, + USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, + USAGE_ASSISTANCE_SONIFICATION, + USAGE_GAME + }; + /** * Flag defining a behavior where the audibility of the sound will be ensured by the system. */ diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java index c22bfb321a5ca..afd42ea67c0f0 100644 --- a/services/core/java/com/android/server/notification/ZenModeHelper.java +++ b/services/core/java/com/android/server/notification/ZenModeHelper.java @@ -35,6 +35,7 @@ import android.content.pm.ServiceInfo; import android.content.res.Resources; import android.content.res.XmlResourceParser; import android.database.ContentObserver; +import android.media.AudioAttributes; import android.media.AudioManager; import android.media.AudioManagerInternal; import android.media.AudioSystem; @@ -736,13 +737,14 @@ public class ZenModeHelper { // total silence restrictions final boolean muteEverything = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; - for (int i = USAGE_UNKNOWN; i <= USAGE_VIRTUAL_SOURCE; i++) { - if (i == USAGE_NOTIFICATION) { - applyRestrictions(muteNotifications || muteEverything, i); - } else if (i == USAGE_NOTIFICATION_RINGTONE) { - applyRestrictions(muteCalls || muteEverything, i); + for (int usage : AudioAttributes.SDK_USAGES) { + final int suppressionBehavior = AudioAttributes.SUPPRESSIBLE_USAGES.get(usage); + if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_NOTIFICATION) { + applyRestrictions(muteNotifications || muteEverything, usage); + } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_CALL) { + applyRestrictions(muteCalls || muteEverything, usage); } else { - applyRestrictions(muteEverything, i); + applyRestrictions(muteEverything, usage); } } }