@@ -29,6 +29,7 @@ public enum InterruptionSource {
2929 private TelephonyCallback telephonyCallback ;
3030
3131 private AudioFocusChangeListener focusChangeListener ;
32+ private boolean interruptionActive = false ;
3233
3334 private final AudioManager .OnAudioFocusChangeListener audioSwitchFocusListener = focusChange -> {
3435 switch (focusChange ) {
@@ -82,6 +83,7 @@ public void setAudioFocusChangeListener(AudioFocusChangeListener listener) {
8283 }
8384
8485 public void startMonitoring () {
86+ interruptionActive = false ;
8587 if (monitorAudioFocus ) {
8688 if (AudioSwitchManager .instance != null ) {
8789 AudioSwitchManager .instance .setAudioFocusChangeListener (audioSwitchFocusListener );
@@ -100,6 +102,7 @@ public void stopMonitoring() {
100102 if (monitorAudioFocus && AudioSwitchManager .instance != null ) {
101103 AudioSwitchManager .instance .setAudioFocusChangeListener (null );
102104 }
105+ interruptionActive = false ;
103106
104107 if (monitorTelephony ) {
105108 unregisterTelephonyListener ();
@@ -200,7 +203,13 @@ private void handleInterruptionStart(String logMessage) {
200203 return ;
201204 }
202205
206+ if (interruptionActive ) {
207+ Log .d (TAG , "Ignoring duplicate interruption start: " + logMessage );
208+ return ;
209+ }
210+
203211 Log .d (TAG , logMessage );
212+ interruptionActive = true ;
204213 focusChangeListener .onInterruptionStart ();
205214 }
206215
@@ -209,7 +218,20 @@ private void handleInterruptionEnd(String logMessage) {
209218 return ;
210219 }
211220
221+ if (!interruptionActive ) {
222+ Log .d (TAG , "Ignoring interruption end with no active interruption: " + logMessage );
223+ return ;
224+ }
225+
212226 Log .d (TAG , logMessage );
227+ interruptionActive = false ;
213228 focusChangeListener .onInterruptionEnd ();
214229 }
230+
231+ public void notifyManualAudioFocusRegain () {
232+ if (!monitorAudioFocus ) {
233+ return ;
234+ }
235+ handleInterruptionEnd ("Audio focus manually requested" );
236+ }
215237}
0 commit comments