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

Commit 7b6a5805 authored by Rahul Sabnis's avatar Rahul Sabnis
Browse files

PhonePolicy no longer relies on broadcast for service discovery

Bug: 278014948
Test: atest PhonePolicyTest
Change-Id: I85579412d991f639ea826502ff49fcc02d905e65
parent 3693e36e
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -7062,6 +7062,23 @@ public class AdapterService extends Service {
        return true;
    }

    /**
     * Sends service discovery UUIDs internally within the stack. This is meant to remove internal
     * dependencies on the broadcast {@link BluetoothDevice#ACTION_UUID}.
     *
     * @param device is the remote device whose UUIDs have been discovered
     * @param uuids are the services supported on the remote device
     */
    void sendUuidsInternal(BluetoothDevice device, ParcelUuid[] uuids) {
        Log.i(TAG, "sendUuidsInternal: Received service discovery UUIDs for device " + device);
        if (DBG) {
            for (int i = 0; i < uuids.length; i++) {
                Log.d(TAG, "index=" + i + "uuid=" + uuids[i]);
            }
        }
        mPhonePolicy.onUuidsDiscovered(device, uuids);
    }

    static native void classInitNative();

    native boolean initNative(boolean startRestricted, boolean isCommonCriteriaMode,
+16 −23
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.os.Parcelable;
import android.os.SystemProperties;
import android.util.Log;

@@ -92,7 +91,6 @@ class PhonePolicy {

    // Message types for the handler (internal messages generated by intents or timeouts)
    private static final int MESSAGE_PROFILE_CONNECTION_STATE_CHANGED = 1;
    private static final int MESSAGE_PROFILE_INIT_PRIORITIES = 2;
    private static final int MESSAGE_CONNECT_OTHER_PROFILES = 3;
    private static final int MESSAGE_ADAPTER_STATE_TURNED_ON = 4;
    private static final int MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED = 5;
@@ -176,9 +174,6 @@ class PhonePolicy {
                        mHandler.obtainMessage(MESSAGE_ADAPTER_STATE_TURNED_ON).sendToTarget();
                    }
                    break;
                case BluetoothDevice.ACTION_UUID:
                    mHandler.obtainMessage(MESSAGE_PROFILE_INIT_PRIORITIES, intent).sendToTarget();
                    break;
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    mHandler.obtainMessage(MESSAGE_DEVICE_CONNECTED, intent).sendToTarget();
                    break;
@@ -203,23 +198,6 @@ class PhonePolicy {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_PROFILE_INIT_PRIORITIES: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
                    debugLog("Received ACTION_UUID for device " + device);
                    if (uuids != null) {
                        ParcelUuid[] uuidsToSend = new ParcelUuid[uuids.length];
                        for (int i = 0; i < uuidsToSend.length; i++) {
                            uuidsToSend[i] = (ParcelUuid) uuids[i];
                            debugLog("index=" + i + "uuid=" + uuidsToSend[i]);
                        }
                        processInitProfilePriorities(device, uuidsToSend);
                    }
                }
                break;

                case MESSAGE_PROFILE_CONNECTION_STATE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
@@ -271,7 +249,6 @@ class PhonePolicy {
        filter.addAction(BluetoothCsipSetCoordinator.ACTION_CSIS_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothVolumeControl.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_UUID);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
@@ -844,6 +821,22 @@ class PhonePolicy {
        }
    }

    /**
     * Direct call prior to sending out {@link BluetoothDevice#ACTION_UUID}. This indicates that
     * service discovery is complete and passes the UUIDs directly to PhonePolicy.
     *
     * @param device is the remote device whose services have been discovered
     * @param uuids are the services supported by the remote device
     */
    void onUuidsDiscovered(BluetoothDevice device, ParcelUuid[] uuids) {
        debugLog("onUuidsDiscovered: discovered services for device " + device);
        if (uuids != null) {
            processInitProfilePriorities(device, uuids);
        } else {
            warnLog("onUuidsDiscovered: uuids is null for device " + device);
        }
    }

    private static void debugLog(String msg) {
        if (DBG) {
            Log.i(TAG, msg);
+5 −1
Original line number Diff line number Diff line
@@ -696,9 +696,13 @@ final class RemoteDevices {
    }

    private void sendUuidIntent(BluetoothDevice device, DeviceProperties prop) {
        // Send uuids within the stack before the broadcast is sent out
        ParcelUuid[] uuids = prop == null ? null : prop.getUuids();
        mAdapterService.sendUuidsInternal(device, uuids);

        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null ? null : prop.getUuids());
        intent.putExtra(BluetoothDevice.EXTRA_UUID, uuids);
        Utils.sendBroadcast(mAdapterService, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());

+5 −18
Original line number Diff line number Diff line
@@ -133,13 +133,10 @@ public class PhonePolicyTest {
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);

        // Inject an event for UUIDs updated for a remote device with only HFP enabled
        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        ParcelUuid[] uuids = new ParcelUuid[2];
        uuids[0] = BluetoothUuid.HFP;
        uuids[1] = BluetoothUuid.A2DP_SINK;
        intent.putExtra(BluetoothDevice.EXTRA_UUID, uuids);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        mPhonePolicy.onUuidsDiscovered(device, uuids);

        // Check that the priorities of the devices for preferred profiles are set to ON
        verify(mDatabaseManager, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
@@ -167,13 +164,10 @@ public class PhonePolicyTest {
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);

        // Inject an event for UUIDs updated for a remote device with only HFP enabled
        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        ParcelUuid[] uuids = new ParcelUuid[2];
        uuids[0] = BluetoothUuid.HFP;
        uuids[1] = BluetoothUuid.A2DP_SINK;
        intent.putExtra(BluetoothDevice.EXTRA_UUID, uuids);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        mPhonePolicy.onUuidsDiscovered(device, uuids);

        // Check auto connect
        verify(mA2dpService, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
@@ -249,14 +243,11 @@ public class PhonePolicyTest {
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);

        // Inject an event for UUIDs updated for a remote device with only HFP enabled
        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        ParcelUuid[] uuids = new ParcelUuid[3];
        uuids[0] = BluetoothUuid.HFP;
        uuids[1] = BluetoothUuid.A2DP_SINK;
        uuids[2] = BluetoothUuid.LE_AUDIO;
        intent.putExtra(BluetoothDevice.EXTRA_UUID, uuids);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        mPhonePolicy.onUuidsDiscovered(device, uuids);
    }

    /**
@@ -890,12 +881,8 @@ public class PhonePolicyTest {
        when(mA2dpService.getConnectionPolicy(device))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);

        // Inject an event for UUIDs updated for a remote device with only HFP enabled
        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);

        // Put no UUIDs
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        // Inject an event for UUIDs updated for a remote device with no supported services
        mPhonePolicy.onUuidsDiscovered(device, null);

        // Check that we do not crash and not call any setPriority methods
        verify(mHeadsetService,
+4 −1
Original line number Diff line number Diff line
@@ -87,13 +87,16 @@ public class RemoteDevicesTest {

    @Test
    public void testSendUuidIntent() {
        doNothing().when(mAdapterService).sendUuidsInternal(any(), any());

        // Verify that a handler message is sent by the method call
        mRemoteDevices.updateUuids(mDevice1);
        Message msg = mTestLooperManager.next();
        Assert.assertNotNull(msg);

        // Verify that executing that message results in a broadcast intent
        // Verify that executing that message results in a direct call and broadcast intent
        mTestLooperManager.execute(msg);
        verify(mAdapterService).sendUuidsInternal(any(), any());
        verify(mAdapterService).sendBroadcast(any(), anyString(), any());
        verifyNoMoreInteractions(mAdapterService);
    }