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

Commit 4021e9e5 authored by Alice Kuo's avatar Alice Kuo
Browse files

As airplane mode turn on, keep BT on if LE audio profile connected

Bug: 207464971
Test: w/o LE audio device, and turn on/off airplane mode
Change-Id: I1865c5ff7e8c04a4188dc7c379223e48c8b29ad9
parent f7a480d8
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.internal.annotations.VisibleForTesting;
 * when Bluetooth is on and Bluetooth is in one of the following situations:
 *   1. Bluetooth A2DP is connected.
 *   2. Bluetooth Hearing Aid profile is connected.
 *   3. Bluetooth LE Audio is connected
 */
class BluetoothAirplaneModeListener {
    private static final String TAG = "BluetoothAirplaneModeListener";
@@ -132,7 +133,7 @@ class BluetoothAirplaneModeListener {
            return false;
        }
        if (!mAirplaneHelper.isBluetoothOn() || !mAirplaneHelper.isAirplaneModeOn()
                || !mAirplaneHelper.isA2dpOrHearingAidConnected()) {
                || !mAirplaneHelper.isMediaProfileConnected()) {
            return false;
        }
        return true;
+5 −3
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.app.BroadcastOptions;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProtoEnums;
import android.bluetooth.IBluetooth;
@@ -456,12 +457,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    }
                }
            } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(action)
                    || BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    || BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED.equals(action)
                    || BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED.equals(action)) {
                final int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE,
                        BluetoothProfile.STATE_CONNECTED);
                if (mHandler.hasMessages(MESSAGE_INIT_FLAGS_CHANGED)
                        && state == BluetoothProfile.STATE_DISCONNECTED
                        && !mBluetoothModeChangeHelper.isA2dpOrHearingAidConnected()) {
                        && !mBluetoothModeChangeHelper.isMediaProfileConnected()) {
                    Slog.i(TAG, "Device disconnected, reactivating pending flag changes");
                    onInitFlagsChanged();
                }
@@ -2291,7 +2293,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        Slog.d(TAG, "MESSAGE_INIT_FLAGS_CHANGED");
                    }
                    mHandler.removeMessages(MESSAGE_INIT_FLAGS_CHANGED);
                    if (mBluetoothModeChangeHelper.isA2dpOrHearingAidConnected()) {
                    if (mBluetoothModeChangeHelper.isMediaProfileConnected()) {
                        Slog.i(TAG, "Delaying MESSAGE_INIT_FLAGS_CHANGED by "
                                + DELAY_FOR_RETRY_INIT_FLAG_CHECK_MS
                                + " ms due to existing connections");
+19 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile.ServiceListener;
import android.content.Context;
@@ -37,6 +38,7 @@ import com.android.internal.annotations.VisibleForTesting;
public class BluetoothModeChangeHelper {
    private volatile BluetoothA2dp mA2dp;
    private volatile BluetoothHearingAid mHearingAid;
    private volatile BluetoothLeAudio mLeAudio;
    private final BluetoothAdapter mAdapter;
    private final Context mContext;

@@ -47,6 +49,7 @@ public class BluetoothModeChangeHelper {
        mAdapter.getProfileProxy(mContext, mProfileServiceListener, BluetoothProfile.A2DP);
        mAdapter.getProfileProxy(mContext, mProfileServiceListener,
                BluetoothProfile.HEARING_AID);
        mAdapter.getProfileProxy(mContext, mProfileServiceListener, BluetoothProfile.LE_AUDIO);
    }

    private final ServiceListener mProfileServiceListener = new ServiceListener() {
@@ -60,6 +63,9 @@ public class BluetoothModeChangeHelper {
                case BluetoothProfile.HEARING_AID:
                    mHearingAid = (BluetoothHearingAid) proxy;
                    break;
                case BluetoothProfile.LE_AUDIO:
                    mLeAudio = (BluetoothLeAudio) proxy;
                    break;
                default:
                    break;
            }
@@ -75,6 +81,9 @@ public class BluetoothModeChangeHelper {
                case BluetoothProfile.HEARING_AID:
                    mHearingAid = null;
                    break;
                case BluetoothProfile.LE_AUDIO:
                    mLeAudio = null;
                    break;
                default:
                    break;
            }
@@ -82,8 +91,8 @@ public class BluetoothModeChangeHelper {
    };

    @VisibleForTesting
    public boolean isA2dpOrHearingAidConnected() {
        return isA2dpConnected() || isHearingAidConnected();
    public boolean isMediaProfileConnected() {
        return isA2dpConnected() || isHearingAidConnected() || isLeAudioConnected();
    }

    @VisibleForTesting
@@ -142,4 +151,12 @@ public class BluetoothModeChangeHelper {
        }
        return hearingAid.getConnectedDevices().size() > 0;
    }

    private boolean isLeAudioConnected() {
        final BluetoothLeAudio leAudio = mLeAudio;
        if (leAudio == null) {
            return false;
        }
        return leAudio.getConnectedDevices().size() > 0;
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public class BluetoothAirplaneModeListenerTest {
        when(mHelper.isBluetoothOn()).thenReturn(true);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        when(mHelper.isA2dpOrHearingAidConnected()).thenReturn(true);
        when(mHelper.isMediaProfileConnected()).thenReturn(true);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        when(mHelper.isAirplaneModeOn()).thenReturn(true);
@@ -83,7 +83,7 @@ public class BluetoothAirplaneModeListenerTest {
    public void testHandleAirplaneModeChange_NotInvokeAirplaneModeChanged_NotPopToast() {
        mBluetoothAirplaneModeListener.mToastCount = BluetoothAirplaneModeListener.MAX_TOAST_COUNT;
        when(mHelper.isBluetoothOn()).thenReturn(true);
        when(mHelper.isA2dpOrHearingAidConnected()).thenReturn(true);
        when(mHelper.isMediaProfileConnected()).thenReturn(true);
        when(mHelper.isAirplaneModeOn()).thenReturn(true);
        mBluetoothAirplaneModeListener.handleAirplaneModeChange();

@@ -97,7 +97,7 @@ public class BluetoothAirplaneModeListenerTest {
    public void testHandleAirplaneModeChange_NotInvokeAirplaneModeChanged_PopToast() {
        mBluetoothAirplaneModeListener.mToastCount = 0;
        when(mHelper.isBluetoothOn()).thenReturn(true);
        when(mHelper.isA2dpOrHearingAidConnected()).thenReturn(true);
        when(mHelper.isMediaProfileConnected()).thenReturn(true);
        when(mHelper.isAirplaneModeOn()).thenReturn(true);
        mBluetoothAirplaneModeListener.handleAirplaneModeChange();