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

Commit 922f6b52 authored by Jason Hsu's avatar Jason Hsu Committed by Android (Google) Code Review
Browse files

Merge "Fix test fail for HearingDevicePhoneCallNotificationController" into main

parents ca00f39d 6d828503
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();