diff options
author | 2025-03-13 05:52:12 +0800 | |
---|---|---|
committer | 2025-03-14 05:23:53 +0800 | |
commit | 6d828503251c96ca8cc33e6f60c48ea8801870c0 (patch) | |
tree | 9c4030796cd97a9b4bd2ff98aded63deb63797d6 | |
parent | ab631004d0ab58d48bb2abcbba0acaefb939dd47 (diff) |
Fix test fail for HearingDevicePhoneCallNotificationController
Root Cause: attempt to call addOnCommunicationDeviceChangedListeneron a previously registered listener, and fail on pts-bot:HFP/AG#HFP/AG/ECS/BV-03-I CtsTelecomTestCases CtsTelephonyTestCases
Solution: Add protection to not register it multiple times
Bug: 402755697
Bug: 402749422
Test: atest HearingDevicePhoneCallNotificationControllerTest
Test: atest pts-bot:HFP/AG#HFP/AG/ECS/BV-03-I CtsTelecomTestCases CtsTelephonyTestCases
Flag: com.android.server.accessibility.hearing_input_change_when_comm_device
Change-Id: I51dd57f5578b6672d91356273930371f088fcdd5
2 files changed, 42 insertions, 8 deletions
diff --git a/services/accessibility/java/com/android/server/accessibility/HearingDevicePhoneCallNotificationController.java b/services/accessibility/java/com/android/server/accessibility/HearingDevicePhoneCallNotificationController.java index 94cef418b6c8..edcf5748a8fc 100644 --- a/services/accessibility/java/com/android/server/accessibility/HearingDevicePhoneCallNotificationController.java +++ b/services/accessibility/java/com/android/server/accessibility/HearingDevicePhoneCallNotificationController.java @@ -17,6 +17,7 @@ package com.android.server.accessibility; import android.Manifest; +import android.annotation.CallbackExecutor; import android.annotation.NonNull; import android.annotation.SuppressLint; import android.app.Notification; @@ -149,11 +150,7 @@ public class HearingDevicePhoneCallNotificationController { } if (state == TelephonyManager.CALL_STATE_IDLE) { - if (mIsCommDeviceChangedRegistered) { - mIsCommDeviceChangedRegistered = false; - mAudioManager.removeOnCommunicationDeviceChangedListener( - mCommDeviceChangedListener); - } + removeOnCommunicationDeviceChangedListenerIfNeeded(mCommDeviceChangedListener); dismissNotificationIfNeeded(); if (mHearingDevice != null) { @@ -172,10 +169,8 @@ public class HearingDevicePhoneCallNotificationController { if (mHearingDevice != null) { showNotificationIfNeeded(); } else { - mAudioManager.addOnCommunicationDeviceChangedListener( - mCommDeviceChangedExecutor, + addOnCommunicationDeviceChangedListenerIfNeeded(mCommDeviceChangedExecutor, mCommDeviceChangedListener); - mIsCommDeviceChangedRegistered = true; } } else { mHearingDevice = getSupportedInputHearingDeviceInfo( @@ -187,6 +182,27 @@ public class HearingDevicePhoneCallNotificationController { } } + private void addOnCommunicationDeviceChangedListenerIfNeeded( + @NonNull @CallbackExecutor Executor executor, + @NonNull AudioManager.OnCommunicationDeviceChangedListener listener) { + if (mIsCommDeviceChangedRegistered) { + return; + } + + mIsCommDeviceChangedRegistered = true; + mAudioManager.addOnCommunicationDeviceChangedListener(executor, listener); + } + + private void removeOnCommunicationDeviceChangedListenerIfNeeded( + @NonNull AudioManager.OnCommunicationDeviceChangedListener listener) { + if (!mIsCommDeviceChangedRegistered) { + return; + } + + mAudioManager.removeOnCommunicationDeviceChangedListener(listener); + mIsCommDeviceChangedRegistered = false; + } + private void showNotificationIfNeeded() { if (mIsNotificationShown) { return; diff --git a/services/tests/servicestests/src/com/android/server/accessibility/HearingDevicePhoneCallNotificationControllerTest.java b/services/tests/servicestests/src/com/android/server/accessibility/HearingDevicePhoneCallNotificationControllerTest.java index 3565244d90b3..33529c3f4375 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/HearingDevicePhoneCallNotificationControllerTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/HearingDevicePhoneCallNotificationControllerTest.java @@ -25,6 +25,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -207,6 +208,23 @@ public class HearingDevicePhoneCallNotificationControllerTest { eq(SystemMessageProto.SystemMessage.NOTE_HEARING_DEVICE_INPUT_SWITCH), any()); } + @Test + @EnableFlags(Flags.FLAG_HEARING_INPUT_CHANGE_WHEN_COMM_DEVICE) + public void onCallStateChanged_offHookMultiple_addListenerOnlyOneTime() { + AudioDeviceInfo a2dpDeviceInfo = createAudioDeviceInfo(TEST_ADDRESS, + AudioManager.DEVICE_OUT_BLUETOOTH_A2DP); + when(mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).thenReturn( + new AudioDeviceInfo[]{a2dpDeviceInfo}); + when(mAudioManager.getCommunicationDevice()).thenReturn(a2dpDeviceInfo); + + mTestCallStateListener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK); + mTestCallStateListener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK); + + verify(mAudioManager, times(1)).addOnCommunicationDeviceChangedListener( + any(Executor.class), + any(AudioManager.OnCommunicationDeviceChangedListener.class)); + } + private AudioDeviceInfo createAudioDeviceInfo(String address, int type) { AudioDevicePort audioDevicePort = mock(AudioDevicePort.class); doReturn(type).when(audioDevicePort).type(); |