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

Commit d497814f authored by Leslie Watkins's avatar Leslie Watkins
Browse files

DO NOT MERGE Track SCO state in HfpClientDeviceBlock, and set SCO

state of both Connections and Conferences whenever they're created,
and when onAudioStateChanged is invoked.

Tag: #stability
Bug: 191206390
Bug: 190404030
Test: atest BluetoothInstrumentationTests
Change-Id: I256a6409c83f45db03052f3f7ee9da8aafb5b817
parent 713029c7
Loading
Loading
Loading
Loading
+11 −2
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@ import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService;
import com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService;
import com.android.internal.annotations.VisibleForTesting;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
@@ -513,7 +514,9 @@ public class HeadsetClientService extends ProfileService {
        return sHeadsetClientService;
        return sHeadsetClientService;
    }
    }


    private static synchronized void setHeadsetClientService(HeadsetClientService instance) {
    /** Set a {@link HeadsetClientService} instance. */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public static synchronized void setHeadsetClientService(HeadsetClientService instance) {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "setHeadsetClientService(): set to: " + instance);
            Log.d(TAG, "setHeadsetClientService(): set to: " + instance);
        }
        }
@@ -681,7 +684,13 @@ public class HeadsetClientService extends ProfileService {
        return true;
        return true;
    }
    }


    int getAudioState(BluetoothDevice device) {
    /**
     * Gets audio state of the connection with {@code device}.
     *
     * <p>Can be one of {@link STATE_AUDIO_CONNECTED}, {@link STATE_AUDIO_CONNECTING}, or
     * {@link STATE_AUDIO_DISCONNECTED}.
     */
    public int getAudioState(BluetoothDevice device) {
        HeadsetClientStateMachine sm = getStateMachine(device);
        HeadsetClientStateMachine sm = getStateMachine(device);
        if (sm == null) {
        if (sm == null) {
            Log.e(TAG, "Cannot allocate SM for device " + device);
            Log.e(TAG, "Cannot allocate SM for device " + device);
+0 −15
Original line number Original line Diff line number Diff line
@@ -33,7 +33,6 @@ public class HfpClientConnection extends Connection {
    private static final String TAG = "HfpClientConnection";
    private static final String TAG = "HfpClientConnection";
    private static final boolean DBG = false;
    private static final boolean DBG = false;


    private static final String KEY_SCO_STATE = "com.android.bluetooth.hfpclient.SCO_STATE";
    private static final String EVENT_SCO_CONNECT = "com.android.bluetooth.hfpclient.SCO_CONNECT";
    private static final String EVENT_SCO_CONNECT = "com.android.bluetooth.hfpclient.SCO_CONNECT";
    private static final String EVENT_SCO_DISCONNECT =
    private static final String EVENT_SCO_DISCONNECT =
             "com.android.bluetooth.hfpclient.SCO_DISCONNECT";
             "com.android.bluetooth.hfpclient.SCO_DISCONNECT";
@@ -64,7 +63,6 @@ public class HfpClientConnection extends Connection {
        }
        }


        mCurrentCall = call;
        mCurrentCall = call;
        setScoState(BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED);
        handleCallChanged();
        handleCallChanged();
        finishInitializing();
        finishInitializing();
    }
    }
@@ -299,19 +297,6 @@ public class HfpClientConnection extends Connection {
        }
        }
    }
    }


    /**
     * Notify this connection of changes in the SCO state so we can update our call details
     */
    public void onScoStateChanged(int newState, int oldState) {
        setScoState(newState);
    }

    private void setScoState(int state) {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_SCO_STATE, state);
        setExtras(bundle);
    }

    @Override
    @Override
    public boolean equals(Object o) {
    public boolean equals(Object o) {
        if (!(o instanceof HfpClientConnection)) {
        if (!(o instanceof HfpClientConnection)) {
+35 −4
Original line number Original line Diff line number Diff line
@@ -27,19 +27,23 @@ import android.telecom.PhoneAccount;
import android.telecom.TelecomManager;
import android.telecom.TelecomManager;
import android.util.Log;
import android.util.Log;


import com.android.bluetooth.hfpclient.HeadsetClientService;

import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Map;
import java.util.UUID;
import java.util.UUID;


// Helper class that manages the call handling for one device. HfpClientConnectionService holdes a
// Helper class that manages the call handling for one device. HfpClientConnectionService holds a
// list of such blocks and routes traffic from the UI.
// list of such blocks and routes traffic from the UI.
//
//
// Lifecycle of a Device Block is managed entirely by the Service which creates it. In essence it
// Lifecycle of a Device Block is managed entirely by the Service which creates it. In essence it
// has only the active state otherwise the block should be GCed.
// has only the active state otherwise the block should be GCed.
public class HfpClientDeviceBlock {
public class HfpClientDeviceBlock {
    private final String mTAG;
    private static final String KEY_SCO_STATE = "com.android.bluetooth.hfpclient.SCO_STATE";
    private static final boolean DBG = false;
    private static final boolean DBG = false;

    private final String mTAG;
    private final Context mContext;
    private final Context mContext;
    private final BluetoothDevice mDevice;
    private final BluetoothDevice mDevice;
    private final PhoneAccount mPhoneAccount;
    private final PhoneAccount mPhoneAccount;
@@ -47,8 +51,8 @@ public class HfpClientDeviceBlock {
    private final TelecomManager mTelecomManager;
    private final TelecomManager mTelecomManager;
    private final HfpClientConnectionService mConnServ;
    private final HfpClientConnectionService mConnServ;
    private HfpClientConference mConference;
    private HfpClientConference mConference;

    private BluetoothHeadsetClient mHeadsetProfile;
    private BluetoothHeadsetClient mHeadsetProfile;
    private Bundle mScoState;


    HfpClientDeviceBlock(HfpClientConnectionService connServ, BluetoothDevice device,
    HfpClientDeviceBlock(HfpClientConnectionService connServ, BluetoothDevice device,
            BluetoothHeadsetClient headsetProfile) {
            BluetoothHeadsetClient headsetProfile) {
@@ -64,6 +68,10 @@ public class HfpClientDeviceBlock {
        mTelecomManager.enablePhoneAccount(mPhoneAccount.getAccountHandle(), true);
        mTelecomManager.enablePhoneAccount(mPhoneAccount.getAccountHandle(), true);
        mTelecomManager.setUserSelectedOutgoingPhoneAccount(mPhoneAccount.getAccountHandle());
        mTelecomManager.setUserSelectedOutgoingPhoneAccount(mPhoneAccount.getAccountHandle());
        mHeadsetProfile = headsetProfile;
        mHeadsetProfile = headsetProfile;
        mScoState = getScoStateFromDevice(device);
        if (DBG) {
            Log.d(mTAG, "SCO state = " + mScoState);
        }


        // Read the current calls and add them to telecom if already present
        // Read the current calls and add them to telecom if already present
        if (mHeadsetProfile != null) {
        if (mHeadsetProfile != null) {
@@ -110,8 +118,13 @@ public class HfpClientDeviceBlock {
        if (DBG) {
        if (DBG) {
            Log.d(mTAG, "Call audio state changed " + oldState + " -> " + newState);
            Log.d(mTAG, "Call audio state changed " + oldState + " -> " + newState);
        }
        }
        mScoState.putInt(KEY_SCO_STATE, newState);

        for (HfpClientConnection connection : mConnections.values()) {
        for (HfpClientConnection connection : mConnections.values()) {
            connection.onScoStateChanged(newState, oldState);
            connection.setExtras(mScoState);
        }
        if (mConference != null) {
            mConference.setExtras(mScoState);
        }
        }
    }
    }


@@ -132,6 +145,7 @@ public class HfpClientDeviceBlock {
        if (mConference == null) {
        if (mConference == null) {
            mConference = new HfpClientConference(mPhoneAccount.getAccountHandle(), mDevice,
            mConference = new HfpClientConference(mPhoneAccount.getAccountHandle(), mDevice,
                    mHeadsetProfile);
                    mHeadsetProfile);
            mConference.setExtras(mScoState);
        }
        }


        if (connection1.getConference() == null) {
        if (connection1.getConference() == null) {
@@ -263,6 +277,10 @@ public class HfpClientDeviceBlock {
        } else {
        } else {
            connection = new HfpClientConnection(mConnServ, mDevice, mHeadsetProfile, number);
            connection = new HfpClientConnection(mConnServ, mDevice, mHeadsetProfile, number);
        }
        }
        connection.setExtras(mScoState);
        if (DBG) {
            Log.d(mTAG, "Connection extras = " + connection.getExtras().toString());
        }


        if (connection.getState() != Connection.STATE_DISCONNECTED) {
        if (connection.getState() != Connection.STATE_DISCONNECTED) {
            mConnections.put(connection.getUUID(), connection);
            mConnections.put(connection.getUUID(), connection);
@@ -301,6 +319,7 @@ public class HfpClientDeviceBlock {
                if (mConference == null) {
                if (mConference == null) {
                    mConference = new HfpClientConference(mPhoneAccount.getAccountHandle(), mDevice,
                    mConference = new HfpClientConference(mPhoneAccount.getAccountHandle(), mDevice,
                            mHeadsetProfile);
                            mHeadsetProfile);
                    mConference.setExtras(mScoState);
                }
                }
                if (mConference.addConnection(otherConn)) {
                if (mConference.addConnection(otherConn)) {
                    if (DBG) {
                    if (DBG) {
@@ -330,4 +349,16 @@ public class HfpClientDeviceBlock {
        }
        }
    }
    }


    private Bundle getScoStateFromDevice(BluetoothDevice device) {
        Bundle bundle = new Bundle();

        HeadsetClientService headsetClientService = HeadsetClientService.getHeadsetClientService();
        if (headsetClientService == null) {
            return bundle;
        }

        bundle.putInt(KEY_SCO_STATE, headsetClientService.getAudioState(device));

        return bundle;
    }
}
}