Fix issue where the dnd mode remains on after a call terminates.

There is a potential race condition when handling the
ACTION_INTERRUPTION_FILTER_CHANGED broadcast.  Since we're setting the
interruption mode ourselves, we also get this broadcast.  Most of the time
the broadcast is processed first and everything works.  However, it looks
like there are cases where the broadcast receiver processes AFTER we have
updated mAreNotificationSuppressed to true.  Thus, when we try to turn
off DND later on we assume that the user had initiated the request to turn
off DND and don't bother to do so.

The fix is to check who initiated the manual DND rule.  If its telecom,
we can ignore the broadcast.

Test: Manual
Bug: 33340277
Merged-In: I42b78fd27fe9814c4f8e29afc8937c3b41f51e50
Change-Id: I42b78fd27fe9814c4f8e29afc8937c3b41f51e50
diff --git a/src/com/android/server/telecom/CallAudioRouteStateMachine.java b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
index 8f3aefe..63bb6ab 100644
--- a/src/com/android/server/telecom/CallAudioRouteStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
@@ -65,6 +65,9 @@
  * mIsMuted: a boolean indicating whether the audio is muted
  */
 public class CallAudioRouteStateMachine extends StateMachine {
+    private static final String TELECOM_PACKAGE =
+            CallAudioRouteStateMachine.class.getPackage().getName();
+
     /** Direct the audio stream through the device's earpiece. */
     public static final int ROUTE_EARPIECE      = CallAudioState.ROUTE_EARPIECE;
 
@@ -167,6 +170,19 @@
                 String action = intent.getAction();
 
                 if (action.equals(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)) {
+                    // We get an this broadcast any time the notification filter is changed, even if
+                    // we are the initiator of the change.
+                    // So, we'll look at who the initiator of the manual zen rule is in the
+                    // notification manager.  If its us, then we can just exit now.
+                    String initiator =
+                            mInterruptionFilterProxy.getInterruptionModeInitiator();
+
+                    if (TELECOM_PACKAGE.equals(initiator)) {
+                        // We are the initiator of this change, so ignore it.
+                        Log.i(this, "interruptionFilterChanged - ignoring own change");
+                        return;
+                    }
+
                     if (mAreNotificationSuppressed) {
                         // If we've already set the interruption filter, and the user changes it to
                         // something other than INTERRUPTION_FILTER_ALARMS, assume we will no longer
@@ -174,6 +190,8 @@
                         mAreNotificationSuppressed =
                                 mInterruptionFilterProxy.getCurrentInterruptionFilter()
                                         == NotificationManager.INTERRUPTION_FILTER_ALARMS;
+                        Log.i(this, "interruptionFilterChanged - changing to %b",
+                                mAreNotificationSuppressed);
                     }
                 }
             } finally {
diff --git a/src/com/android/server/telecom/InterruptionFilterProxy.java b/src/com/android/server/telecom/InterruptionFilterProxy.java
index 434c341..b659de8 100644
--- a/src/com/android/server/telecom/InterruptionFilterProxy.java
+++ b/src/com/android/server/telecom/InterruptionFilterProxy.java
@@ -24,4 +24,5 @@
 public interface InterruptionFilterProxy {
     void setInterruptionFilter(int interruptionFilter);
     int getCurrentInterruptionFilter();
+    String getInterruptionModeInitiator();
 }
diff --git a/src/com/android/server/telecom/components/TelecomService.java b/src/com/android/server/telecom/components/TelecomService.java
index ea1db36..a066b6c 100644
--- a/src/com/android/server/telecom/components/TelecomService.java
+++ b/src/com/android/server/telecom/components/TelecomService.java
@@ -26,6 +26,7 @@
 import android.os.IBinder;
 import android.os.PowerManager;
 import android.os.ServiceManager;
+import android.service.notification.ZenModeConfig;
 import android.telecom.Log;
 
 import com.android.internal.telephony.CallerInfoAsyncQuery;
@@ -170,6 +171,15 @@
                                 public int getCurrentInterruptionFilter() {
                                     return notificationManager.getCurrentInterruptionFilter();
                                 }
+
+                                @Override
+                                public String getInterruptionModeInitiator() {
+                                    ZenModeConfig config = notificationManager.getZenModeConfig();
+                                    if (config.manualRule != null) {
+                                        return config.manualRule.enabler;
+                                    }
+                                    return null;
+                                }
                             }
                     ));
         }
diff --git a/tests/src/com/android/server/telecom/tests/TelecomSystemTest.java b/tests/src/com/android/server/telecom/tests/TelecomSystemTest.java
index f590e8c..78d0fec 100644
--- a/tests/src/com/android/server/telecom/tests/TelecomSystemTest.java
+++ b/tests/src/com/android/server/telecom/tests/TelecomSystemTest.java
@@ -181,6 +181,11 @@
         public int getCurrentInterruptionFilter() {
             return mInterruptionFilter;
         }
+
+        @Override
+        public String getInterruptionModeInitiator() {
+            return "com.android.server.telecom";
+        }
     }
     @Mock HeadsetMediaButton mHeadsetMediaButton;
     @Mock ProximitySensorManager mProximitySensorManager;