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

Commit 21ac5d4f authored by William Escande's avatar William Escande
Browse files

MapClientStateMachine: inject adapter service

Test: atest BluetoothInstrumentationTests
Flag: TEST_ONLY
Bug: 386715308
Change-Id: If57d1932750df3cfd141c58d69dee87cc04ca794
parent 11f082a7
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -173,9 +173,9 @@ public class MapClientService extends ProfileService {
        // connect.
        MceStateMachine mapStateMachine;
        if (mSmLooper != null) {
            mapStateMachine = new MceStateMachine(this, device, mSmLooper);
            mapStateMachine = new MceStateMachine(this, device, mAdapterService, mSmLooper);
        } else {
            mapStateMachine = new MceStateMachine(this, device);
            mapStateMachine = new MceStateMachine(this, device, mAdapterService);
        }
        mMapInstanceMap.put(device, mapStateMachine);
    }
+35 −28
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;
import static android.Manifest.permission.RECEIVE_SMS;

import static java.util.Objects.requireNonNull;

import android.app.Activity;
import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
@@ -112,11 +114,11 @@ class MceStateMachine extends StateMachine {
    // messaging app takes that responsibility.
    private static final Boolean SAVE_OUTBOUND_MESSAGES = true;
    @VisibleForTesting static final Duration DISCONNECT_TIMEOUT = Duration.ofSeconds(3);
    private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10);
    @VisibleForTesting static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10);
    private static final int MAX_MESSAGES = 20;
    private static final int MSG_CONNECT = 1;
    private static final int MSG_DISCONNECT = 2;
    static final int MSG_CONNECTING_TIMEOUT = 3;
    private static final int MSG_CONNECTING_TIMEOUT = 3;
    private static final int MSG_DISCONNECTING_TIMEOUT = 4;

    // Constants for SDP. Note that these values come from the native stack, but no centralized
@@ -147,22 +149,26 @@ class MceStateMachine extends StateMachine {
    private static final String SEND_MESSAGE_TYPE =
            "persist.bluetooth.pts.mapclient.sendmessagetype";

    private final State mDisconnected = new Disconnected();
    private final State mConnecting = new Connecting();
    private final State mConnected = new Connected();
    private final State mDisconnecting = new Disconnecting();

    private final HashMap<String, Bmessage> mSentMessageLog = new HashMap<>(MAX_MESSAGES);
    private final HashMap<Bmessage, PendingIntent> mSentReceiptRequested =
            new HashMap<>(MAX_MESSAGES);
    private final HashMap<Bmessage, PendingIntent> mDeliveryReceiptRequested =
            new HashMap<>(MAX_MESSAGES);

    private final BluetoothDevice mDevice;
    private final MapClientService mService;
    private final AdapterService mAdapterService;

    // Connectivity States
    private int mPreviousState = BluetoothProfile.STATE_DISCONNECTED;
    private int mMostRecentState = BluetoothProfile.STATE_DISCONNECTED;
    private State mDisconnected;
    private State mConnecting;
    private State mConnected;
    private State mDisconnecting;

    private final BluetoothDevice mDevice;
    private MapClientService mService;
    private MasClient mMasClient;
    private MapClientContent mDatabase;
    private HashMap<String, Bmessage> mSentMessageLog = new HashMap<>(MAX_MESSAGES);
    private HashMap<Bmessage, PendingIntent> mSentReceiptRequested = new HashMap<>(MAX_MESSAGES);
    private HashMap<Bmessage, PendingIntent> mDeliveryReceiptRequested =
            new HashMap<>(MAX_MESSAGES);

    private final Object mLock = new Object();

@@ -225,25 +231,33 @@ class MceStateMachine extends StateMachine {
    ConcurrentHashMap<String, MessageMetadata> mMessages =
            new ConcurrentHashMap<String, MessageMetadata>();

    MceStateMachine(MapClientService service, BluetoothDevice device) {
    MceStateMachine(
            MapClientService service, BluetoothDevice device, AdapterService adapterService) {
        super(TAG); // Create a state machine with its own separate thread
        mAdapterService = requireNonNull(adapterService);
        mService = service;
        mDevice = device;
        initStateMachine();
    }

    MceStateMachine(MapClientService service, BluetoothDevice device, Looper looper) {
        this(service, device, null, null, looper);
    MceStateMachine(
            MapClientService service,
            BluetoothDevice device,
            AdapterService adapterService,
            Looper looper) {
        this(service, device, adapterService, looper, null, null);
    }

    @VisibleForTesting
    MceStateMachine(
            MapClientService service,
            BluetoothDevice device,
            AdapterService adapterService,
            Looper looper,
            MasClient masClient,
            MapClientContent database,
            Looper looper) {
        super(TAG, looper);
            MapClientContent database) {
        super(TAG, requireNonNull(looper));
        mAdapterService = requireNonNull(adapterService);
        mService = service;
        mMasClient = masClient;
        mDevice = device;
@@ -254,10 +268,6 @@ class MceStateMachine extends StateMachine {
    private void initStateMachine() {
        mPreviousState = BluetoothProfile.STATE_DISCONNECTED;

        mDisconnected = new Disconnected();
        mConnecting = new Connecting();
        mDisconnecting = new Disconnecting();
        mConnected = new Connected();

        addState(mDisconnected);
        addState(mConnecting);
@@ -302,11 +312,8 @@ class MceStateMachine extends StateMachine {
        }
        setState(state);

        AdapterService adapterService = AdapterService.getAdapterService();
        if (adapterService != null) {
            adapterService.updateProfileConnectionAdapterProperties(
        mAdapterService.updateProfileConnectionAdapterProperties(
                mDevice, BluetoothProfile.MAP_CLIENT, state, prevState);
        }

        Intent intent = new Intent(BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
+6 −14
Original line number Diff line number Diff line
@@ -64,9 +64,7 @@ import androidx.test.filters.MediumTest;
import androidx.test.rule.ServiceTestRule;

import com.android.bluetooth.ObexAppParameters;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.flags.Flags;
import com.android.obex.HeaderSet;
import com.android.vcard.VCardConstants;
@@ -110,7 +108,6 @@ public class MapClientStateMachineTest {
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Mock private AdapterService mAdapterService;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private MapClientService mMockMapClientService;
    @Mock private MapClientContent mMockDatabase;
    @Mock private TelephonyManager mMockTelephonyManager;
@@ -152,7 +149,6 @@ public class MapClientStateMachineTest {

    private Bmessage mTestIncomingSmsBmessage;
    private Bmessage mTestIncomingMmsBmessage;
    boolean mIsAdapterServiceSet;
    boolean mIsMapClientServiceStarted;
    private MceStateMachine mMceStateMachine;
    private MockContentResolver mMockContentResolver;
@@ -207,11 +203,8 @@ public class MapClientStateMachineTest {

        mInOrder = inOrder(mMockMapClientService);

        TestUtils.setAdapterService(mAdapterService);
        mIsAdapterServiceSet = true;
        mMockContentProvider = new MockSmsContentProvider();
        mMockContentResolver = new MockContentResolver();
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
        mIsMapClientServiceStarted = true;
        mMockContentResolver.addProvider("sms", mMockContentProvider);
        mMockContentResolver.addProvider("mms", mMockContentProvider);
@@ -230,9 +223,10 @@ public class MapClientStateMachineTest {
                new MceStateMachine(
                        mMockMapClientService,
                        mTestDevice,
                        mAdapterService,
                        mLooper.getLooper(),
                        mMockMasClient,
                        mMockDatabase,
                        mLooper.getLooper());
                        mMockDatabase);
        mLooper.dispatchAll();

        int initialExpectedState = BluetoothProfile.STATE_CONNECTING;
@@ -268,9 +262,6 @@ public class MapClientStateMachineTest {
            mMceStateMachine.doQuit();
        }

        if (mIsAdapterServiceSet) {
            TestUtils.clearAdapterService(mAdapterService);
        }
        mTargetContext.unregisterReceiver(mSentDeliveryReceiver);
    }

@@ -856,8 +847,9 @@ public class MapClientStateMachineTest {
        // Note: There's no way to validate the BluetoothDevice#sdpSearch call
        mMceStateMachine.sendSdpResult(MceStateMachine.SDP_BUSY, null);

        // Timeout waiting for record
        sendAndDispatchMessage(MceStateMachine.MSG_CONNECTING_TIMEOUT);
        // Simulate timeout waiting for record
        mLooper.moveTimeForward(MceStateMachine.CONNECT_TIMEOUT.toMillis());
        mLooper.dispatchAll();

        // Verify we move into the disconnecting state
        verify(mMockMapClientService, times(2))