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.

Bug: 33340277
Change-Id: I42b78fd27fe9814c4f8e29afc8937c3b41f51e50
diff --git a/src/com/android/server/telecom/CallAudioRouteStateMachine.java b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
index f80239e..f88475a 100644
--- a/src/com/android/server/telecom/CallAudioRouteStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
@@ -62,6 +62,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;
 
@@ -164,6 +167,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
@@ -171,6 +187,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 f36a892..92906e2 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 com.android.internal.telephony.CallerInfoAsyncQuery;
 import com.android.server.telecom.AsyncRingtonePlayer;
@@ -169,6 +170,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 68eb0ed..d1c0325 100644
--- a/tests/src/com/android/server/telecom/tests/TelecomSystemTest.java
+++ b/tests/src/com/android/server/telecom/tests/TelecomSystemTest.java
@@ -180,6 +180,11 @@
         public int getCurrentInterruptionFilter() {
             return mInterruptionFilter;
         }
+
+        @Override
+        public String getInterruptionModeInitiator() {
+            return "com.android.server.telecom";
+        }
     }
 
     @Mock HeadsetMediaButton mHeadsetMediaButton;