title: Phone Listener logic
body: <p>I have a phone listener with 2 options, ringing and idle.</p>
  
  <p><strong>What I am trying to achieve:</strong></p>
  
  <ol>
  <li><p>If do not disturb is not already on.
  The phone rings.
  Do not disturb comes on while the phone rings.
  The phone stops ringing.
  Do not disturb comes off.</p></li>
  <li><p>If do not disturb is already on. Nothing happens.</p></li>
  </ol>
  
  <p><strong>What is actually happening:</strong> </p>
  
  <ol>
  <li>Works fine.</li>
  <li>When the phone stops ringing, do not disturb comes off.</li>
  </ol>
  
  <p><strong>My code sample:</strong></p>
  
  <pre><code>switch (state) {
       case CALL_STATE_RINGING:
  
                      assert notificationManager != null;
                      if (notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY &amp;&amp;
                              notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_ALARMS &amp;&amp;
                              notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_UNKNOWN &amp;&amp;
                              notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_NONE) {
  
      notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_NONE);
  
                      }
                      break;
  
        case CALL_STATE_IDLE:
              if (lastState == TelephonyManager.CALL_STATE_RINGING) {
                          assert notificationManager != null;
                          notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_ALL);
                      }
  </code></pre>
  
  <p><strong>What I tried:</strong>
  I tried creating a boolean "isActivated" in my first Switch case and passing it to my second Switch case. If isActivated is true, then set interruption filter to All. Else, do nothing. But this had no effect.</p>
  
  <p>I would be grateful for some pointers as I don't understand where my logic is wrong. </p>
  
  <p>EDIT: I have moved the check to see if Do Not Disturb is on before the Phone Listener kicks in, so that nothing happens if DND is already on. This seems to make sense. However, results are inconsistent. Most of the time INTERRUPTION_FILTER_ALL kicks in when the state goes idle, and occasionally it doesn't.</p>
  
  <p>Here is my listener code:</p>
  
  <pre><code>    @Override
  public void onReceive(final Context context, Intent intent) {
  
      final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      assert notificationManager != null;
      dnDisturbOn = notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_PRIORITY ||
              notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_ALARMS ||
              notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_UNKNOWN ||
              notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_NONE;
  
      if (!dnDisturbOn) {
  
          TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  
          if (telephony != null) {
              telephony.listen(new PhoneStateListener() {
                  @Override
                  public void onCallStateChanged(int state, String number) {
                      super.onCallStateChanged(state, number);
  
                      testNumber(context, state, number);
                  }
              }, PhoneStateListener.LISTEN_CALL_STATE);
          }
      }
  }
  </code></pre>
  
  <p>Is there any reason why the code as it is should impact on the consistency of the method "testNumber"?</p>