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

Commit a0241d2d authored by jasonwshsu's avatar jasonwshsu
Browse files

Fix to not show mic input notification when communication device is other bluetooth device

Root Cause: We use getAvailableCommunicationDevices to show mic input notification, but did not
consider the case that other BT (car in this case) will coexist with hearing device.

Solution: Change to listen to the CommunicationDeviceChanged to show mic input notification when
communication device for now is hearing device.

Bug: 394070235
Test: atest HearingDevicePhoneCallNotificationControllerTest
Flag: com.android.server.accessibility.hearing_input_change_when_comm_device
Change-Id: I4ae002e407ee8f8e95dc6e22c49295585978067a
parent 8ddbaf6a
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -346,3 +346,13 @@ flag {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
    name: "hearing_input_change_when_comm_device"
    namespace: "accessibility"
    description: "Listen to the CommunicationDeviceChanged to show hearing device input notification."
    bug: "394070235"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}
+43 −6
Original line number Diff line number Diff line
@@ -65,9 +65,9 @@ public class HearingDevicePhoneCallNotificationController {
    private final Executor mCallbackExecutor;

    public HearingDevicePhoneCallNotificationController(@NonNull Context context) {
        mTelephonyListener = new CallStateListener(context);
        mTelephonyManager = context.getSystemService(TelephonyManager.class);
        mCallbackExecutor = Executors.newSingleThreadExecutor();
        mTelephonyListener = new CallStateListener(context, mCallbackExecutor);
    }

    @VisibleForTesting
@@ -109,14 +109,29 @@ public class HearingDevicePhoneCallNotificationController {
                AudioDeviceAttributes.ROLE_INPUT, AudioDeviceInfo.TYPE_BUILTIN_MIC, "");

        private final Context mContext;
        private final Executor mCommDeviceChangedExecutor;
        private final AudioManager.OnCommunicationDeviceChangedListener mCommDeviceChangedListener;
        private NotificationManager mNotificationManager;
        private AudioManager mAudioManager;
        private BroadcastReceiver mHearingDeviceActionReceiver;
        private BluetoothDevice mHearingDevice;
        private boolean mIsCommDeviceChangedRegistered = false;
        private boolean mIsNotificationShown = false;

        CallStateListener(@NonNull Context context) {
        CallStateListener(@NonNull Context context, @NonNull Executor executor) {
            mContext = context;
            mCommDeviceChangedExecutor = executor;
            mCommDeviceChangedListener = device -> {
                if (device == null) {
                    return;
                }
                mHearingDevice = getSupportedInputHearingDeviceInfo(List.of(device));
                if (mHearingDevice != null) {
                    showNotificationIfNeeded();
                } else {
                    dismissNotificationIfNeeded();
                }
            };
        }

        @Override
@@ -134,6 +149,11 @@ public class HearingDevicePhoneCallNotificationController {
            }

            if (state == TelephonyManager.CALL_STATE_IDLE) {
                if (mIsCommDeviceChangedRegistered) {
                    mIsCommDeviceChangedRegistered = false;
                    mAudioManager.removeOnCommunicationDeviceChangedListener(
                            mCommDeviceChangedListener);
                }
                dismissNotificationIfNeeded();

                if (mHearingDevice != null) {
@@ -143,6 +163,18 @@ public class HearingDevicePhoneCallNotificationController {
                mHearingDevice = null;
            }
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                if (com.android.server.accessibility.Flags.hearingInputChangeWhenCommDevice()) {
                    AudioDeviceInfo commDevice = mAudioManager.getCommunicationDevice();
                    mHearingDevice = getSupportedInputHearingDeviceInfo(List.of(commDevice));
                    if (mHearingDevice != null) {
                        showNotificationIfNeeded();
                    } else {
                        mAudioManager.addOnCommunicationDeviceChangedListener(
                                mCommDeviceChangedExecutor,
                                mCommDeviceChangedListener);
                        mIsCommDeviceChangedRegistered = true;
                    }
                } else {
                    mHearingDevice = getSupportedInputHearingDeviceInfo(
                            mAudioManager.getAvailableCommunicationDevices());
                    if (mHearingDevice != null) {
@@ -150,6 +182,7 @@ public class HearingDevicePhoneCallNotificationController {
                    }
                }
            }
        }

        private void showNotificationIfNeeded() {
            if (mIsNotificationShown) {
@@ -264,6 +297,10 @@ public class HearingDevicePhoneCallNotificationController {
                            PendingIntent.FLAG_IMMUTABLE);
                }
                case ACTION_BLUETOOTH_DEVICE_DETAILS -> {
                    if (mHearingDevice == null) {
                        return null;
                    }

                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_BLUETOOTH_ADDRESS, mHearingDevice.getAddress());
                    intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, bundle);
+1 −1
Original line number Diff line number Diff line
@@ -171,7 +171,7 @@ public class HearingDevicePhoneCallNotificationControllerTest {
            HearingDevicePhoneCallNotificationController.CallStateListener {

        TestCallStateListener(@NonNull Context context) {
            super(context);
            super(context, context.getMainExecutor());
        }

        @Override