Loading services/accessibility/java/com/android/server/accessibility/HearingDevicePhoneCallNotificationController.java +24 −8 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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) { Loading @@ -172,10 +169,8 @@ public class HearingDevicePhoneCallNotificationController { if (mHearingDevice != null) { showNotificationIfNeeded(); } else { mAudioManager.addOnCommunicationDeviceChangedListener( mCommDeviceChangedExecutor, addOnCommunicationDeviceChangedListenerIfNeeded(mCommDeviceChangedExecutor, mCommDeviceChangedListener); mIsCommDeviceChangedRegistered = true; } } else { mHearingDevice = getSupportedInputHearingDeviceInfo( Loading @@ -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; Loading services/tests/servicestests/src/com/android/server/accessibility/HearingDevicePhoneCallNotificationControllerTest.java +18 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); Loading Loading
services/accessibility/java/com/android/server/accessibility/HearingDevicePhoneCallNotificationController.java +24 −8 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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) { Loading @@ -172,10 +169,8 @@ public class HearingDevicePhoneCallNotificationController { if (mHearingDevice != null) { showNotificationIfNeeded(); } else { mAudioManager.addOnCommunicationDeviceChangedListener( mCommDeviceChangedExecutor, addOnCommunicationDeviceChangedListenerIfNeeded(mCommDeviceChangedExecutor, mCommDeviceChangedListener); mIsCommDeviceChangedRegistered = true; } } else { mHearingDevice = getSupportedInputHearingDeviceInfo( Loading @@ -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; Loading
services/tests/servicestests/src/com/android/server/accessibility/HearingDevicePhoneCallNotificationControllerTest.java +18 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); Loading