/**
   * Allows the media keys to work when the keyguard is showing. The media keys should be of no
   * interest to the actual keyguard view(s), so intercepting them here should not be of any harm.
   *
   * @param event The key event
   * @return whether the event was consumed as a media key.
   */
  private boolean interceptMediaKey(KeyEvent event) {
    final int keyCode = event.getKeyCode();
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
      switch (keyCode) {
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
          /* Suppress PLAY/PAUSE toggle when phone is ringing or
           * in-call to avoid music playback */
          if (mTelephonyManager == null) {
            mTelephonyManager =
                (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
          }
          if (mTelephonyManager != null
              && mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
            return true; // suppress key event
          }
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
          {
            Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
            intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
            getContext().sendOrderedBroadcast(intent, null);
            return true;
          }

        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_VOLUME_MUTE:
          {
            if (KEYGUARD_MANAGES_VOLUME) {
              synchronized (this) {
                if (mAudioManager == null) {
                  mAudioManager =
                      (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
                }
              }
              // Volume buttons should only function for music.
              if (mAudioManager.isMusicActive()) {
                if (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
                  mAudioManager.toggleMute(AudioManager.STREAM_MUSIC);
                } else {
                  mAudioManager.adjustStreamVolume(
                      AudioManager.STREAM_MUSIC,
                      keyCode == KeyEvent.KEYCODE_VOLUME_UP
                          ? AudioManager.ADJUST_RAISE
                          : AudioManager.ADJUST_LOWER,
                      0);
                }
              }
              // Don't execute default volume behavior
              return true;
            } else {
              return false;
            }
          }
      }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
      switch (keyCode) {
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
          {
            Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
            intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
            getContext().sendOrderedBroadcast(intent, null);
            return true;
          }
      }
    }
    return false;
  }