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

Commit f1fc93e2 authored by William Escande's avatar William Escande
Browse files

PhonePolicy: unify state change variable order

Sometimes it's (to, from), sometimes (from, to).
Trying to stick to one and only one

Test: atest BluetoothInstrumenationTests
Bug: 311772251
Flag: TEST_ONLY && refactor
Change-Id: Ie7d140cc4205ded7a7506b2d6e03ac834bf84ac5
parent 542bb2c1
Loading
Loading
Loading
Loading
+31 −45
Original line number Diff line number Diff line
@@ -16,6 +16,13 @@

package com.android.bluetooth.btservice;

import static android.bluetooth.BluetoothProfile.STATE_CONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_CONNECTING;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTING;
import static android.bluetooth.BluetoothProfile.getConnectionStateName;
import static android.bluetooth.BluetoothProfile.getProfileName;

import static com.android.bluetooth.Utils.isDualModeAudioEnabled;

import android.bluetooth.BluetoothAdapter;
@@ -106,18 +113,14 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {

    public void profileConnectionStateChanged(
            int profile, BluetoothDevice device, int fromState, int toState) {
        switch (profile) {
            case BluetoothProfile.A2DP:
            case BluetoothProfile.HEADSET:
            case BluetoothProfile.LE_AUDIO:
            case BluetoothProfile.CSIP_SET_COORDINATOR:
            case BluetoothProfile.VOLUME_CONTROL:
                mHandler.post(
                        () -> processProfileStateChanged(device, profile, toState, fromState));
                break;
            default:
                break;
        if (profile != BluetoothProfile.A2DP
                && profile != BluetoothProfile.HEADSET
                && profile != BluetoothProfile.LE_AUDIO
                && profile != BluetoothProfile.CSIP_SET_COORDINATOR
                && profile != BluetoothProfile.VOLUME_CONTROL) {
            return;
        }
        mHandler.post(() -> processProfileStateChanged(profile, device, fromState, toState));
    }

    /**
@@ -695,45 +698,28 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {
    }

    private void processProfileStateChanged(
            BluetoothDevice device, int profileId, int nextState, int prevState) {
        debugLog(
                "processProfileStateChanged, device="
                        + device
                        + ", profile="
                        + BluetoothProfile.getProfileName(profileId)
                        + ", "
                        + prevState
            int profile, BluetoothDevice device, int prevState, int nextState) {
        Log.d(
                TAG,
                ("processProfileStateChanged(" + getProfileName(profile) + ", " + device + "): ")
                        + getConnectionStateName(prevState)
                        + "->"
                        + nextState);
        if (((profileId == BluetoothProfile.A2DP)
                || (profileId == BluetoothProfile.HEADSET)
                || (profileId == BluetoothProfile.LE_AUDIO)
                || (profileId == BluetoothProfile.CSIP_SET_COORDINATOR)
                || (profileId == BluetoothProfile.VOLUME_CONTROL)
                || (profileId == BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT))) {
            if (nextState == BluetoothProfile.STATE_CONNECTED) {
                switch (profileId) {
                    case BluetoothProfile.A2DP:
                        mA2dpRetrySet.remove(device);
                        break;
                    case BluetoothProfile.HEADSET:
                        mHeadsetRetrySet.remove(device);
                        break;
                    case BluetoothProfile.CSIP_SET_COORDINATOR:
                        + getConnectionStateName(nextState));
        if (nextState == STATE_CONNECTED) {
            switch (profile) {
                case BluetoothProfile.A2DP -> mA2dpRetrySet.remove(device);
                case BluetoothProfile.HEADSET -> mHeadsetRetrySet.remove(device);
                case BluetoothProfile.CSIP_SET_COORDINATOR ->
                        handleLeAudioOnlyDeviceAfterCsipConnect(device);
                        break;
            }
            connectOtherProfile(device);
            }
            if (nextState == BluetoothProfile.STATE_DISCONNECTED) {
                if (prevState == BluetoothProfile.STATE_CONNECTING
                        || prevState == BluetoothProfile.STATE_DISCONNECTING) {
                    mDatabaseManager.setDisconnection(device, profileId);
        } else if (nextState == STATE_DISCONNECTED) {
            if (prevState == STATE_CONNECTING || prevState == STATE_DISCONNECTING) {
                mDatabaseManager.setDisconnection(device, profile);
            }
            handleAllProfilesDisconnected(device);
        }
    }
    }

    /**
     * Updates the last connection date in the connection order database for the newly active device
+17 −16
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.bluetooth.btservice;

import static android.bluetooth.BluetoothProfile.STATE_CONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTING;

import static com.android.bluetooth.TestUtils.getTestDevice;
import static com.android.bluetooth.TestUtils.waitForLooperToFinishScheduledTask;

@@ -266,8 +270,8 @@ public class PhonePolicyTest {
                mPhonePolicy.profileConnectionStateChanged(
                        BluetoothProfile.CSIP_SET_COORDINATOR,
                        dev,
                        BluetoothProfile.STATE_DISCONNECTED,
                        BluetoothProfile.STATE_CONNECTED);
                        STATE_DISCONNECTED,
                        STATE_CONNECTED);
                waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
            }
        }
@@ -568,8 +572,8 @@ public class PhonePolicyTest {
        mPhonePolicy.profileConnectionStateChanged(
                BluetoothProfile.CSIP_SET_COORDINATOR,
                firstDevice,
                BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTED);
                STATE_DISCONNECTED,
                STATE_CONNECTED);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify connection policy is set properly
@@ -601,8 +605,8 @@ public class PhonePolicyTest {
        mPhonePolicy.profileConnectionStateChanged(
                BluetoothProfile.CSIP_SET_COORDINATOR,
                secondDevice,
                BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTED);
                STATE_DISCONNECTED,
                STATE_CONNECTED);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify connection policy is set properly
@@ -687,8 +691,8 @@ public class PhonePolicyTest {
        mPhonePolicy.profileConnectionStateChanged(
                BluetoothProfile.CSIP_SET_COORDINATOR,
                firstDevice,
                BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTED);
                STATE_DISCONNECTED,
                STATE_CONNECTED);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify connection policy is set properly
@@ -717,8 +721,8 @@ public class PhonePolicyTest {
        mPhonePolicy.profileConnectionStateChanged(
                BluetoothProfile.CSIP_SET_COORDINATOR,
                secondDevice,
                BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTED);
                STATE_DISCONNECTED,
                STATE_CONNECTED);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify connection policy is set properly
@@ -821,10 +825,7 @@ public class PhonePolicyTest {
        when(mHeadsetService.getConnectionState(connectionOrder.get(1)))
                .thenReturn(BluetoothProfile.STATE_DISCONNECTED);
        mPhonePolicy.profileConnectionStateChanged(
                BluetoothProfile.A2DP,
                connectionOrder.get(1),
                BluetoothProfile.STATE_CONNECTED,
                BluetoothProfile.STATE_DISCONNECTED);
                BluetoothProfile.A2DP, connectionOrder.get(1), STATE_CONNECTED, STATE_DISCONNECTED);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify that we do not call setConnection, nor setDisconnection on disconnect
@@ -838,8 +839,8 @@ public class PhonePolicyTest {
        mPhonePolicy.profileConnectionStateChanged(
                BluetoothProfile.A2DP,
                connectionOrder.get(1),
                BluetoothProfile.STATE_DISCONNECTING,
                BluetoothProfile.STATE_DISCONNECTED);
                STATE_DISCONNECTING,
                STATE_DISCONNECTED);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify that we do not call setConnection, but instead setDisconnection on disconnect