Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6d828503 authored by jasonwshsu's avatar jasonwshsu
Browse files

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
parent ab631004
Loading
Loading
Loading
Loading
+24 −8
Original line number Diff line number Diff line
@@ -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;
+18 −0
Original line number Diff line number Diff line
@@ -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();