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

Commit 8277e922 authored by Aritra Sen's avatar Aritra Sen
Browse files

Handle PhonePolicy changes on ACL Connect directly via calls into adapter...

Handle PhonePolicy changes on ACL Connect directly via calls into adapter service instead of Broadcasts.

Tag: #refactor
Bug: 296932947
Test: atest BluetoothInstrumentationTests
Change-Id: Ib20ebbc44537f0134d6a89735bbb352a1edc9672
parent 4cbe221f
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -6899,6 +6899,11 @@ public class AdapterService extends Service {
        }
    }

    /** Update PhonePolicy when new {@link BluetoothDevice} creates an ACL connection. */
    public void updatePhonePolicyOnAclConnect(BluetoothDevice device) {
        mPhonePolicy.handleAclConnected(device);
    }

    /**
     * Notify GATT of a Bluetooth profile's connection state change for a given {@link
     * BluetoothProfile}.
+2 −32
Original line number Diff line number Diff line
@@ -72,7 +72,6 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {

    // Message types for the handler (internal messages generated by intents or timeouts)
    private static final int MESSAGE_CONNECT_OTHER_PROFILES = 3;
    private static final int MESSAGE_DEVICE_CONNECTED = 6;

    @VisibleForTesting static final String AUTO_CONNECT_PROFILES_PROPERTY =
            "bluetooth.auto_connect_profiles.enabled";
@@ -129,29 +128,8 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
        mHandler.post(() -> processActiveDeviceChanged(device, profile));
    }

    // Broadcast receiver for all changes to states of various profiles
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == null) {
                errorLog("Received intent with null action");
                return;
            }
            switch (action) {
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    mHandler.obtainMessage(MESSAGE_DEVICE_CONNECTED, intent).sendToTarget();
                    break;
                default:
                    Log.e(TAG, "Received unexpected intent, action=" + action);
                    break;
            }
        }
    };

    @VisibleForTesting
    BroadcastReceiver getBroadcastReceiver() {
        return mReceiver;
    public void handleAclConnected(BluetoothDevice device) {
        mHandler.post(() -> processDeviceConnected(device));
    }

    // Handler to handoff intents to class thread
@@ -171,12 +149,6 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                    mConnectOtherProfilesDeviceSet.remove(device);
                    break;
                }
                case MESSAGE_DEVICE_CONNECTED:
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    processDeviceConnected(device);
                    break;
            }
        }
    }
@@ -190,12 +162,10 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        mAdapterService.registerReceiver(mReceiver, filter);
    }

    protected void cleanup() {
        mAdapterService.unregisterBluetoothStateCallback(this);
        mAdapterService.unregisterReceiver(mReceiver);
        resetStates();
    }

+1 −0
Original line number Diff line number Diff line
@@ -1107,6 +1107,7 @@ public class RemoteDevices {
            if (batteryService != null && transportLinkType == BluetoothDevice.TRANSPORT_LE) {
                batteryService.connectIfPossible(device);
            }
            mAdapterService.updatePhonePolicyOnAclConnect(device);
            SecurityLog.writeEvent(SecurityLog.TAG_BLUETOOTH_CONNECTION,
                    Utils.getLoggableAddress(device), /* success */ 1, /* reason */ "");
            debugLog(
+4 −14
Original line number Diff line number Diff line
@@ -856,14 +856,8 @@ public class PhonePolicyTest {
        when(mHeadsetService.getConnectionState(bondedDevices[0])).thenReturn(
                BluetoothProfile.STATE_DISCONNECTED);

        // We send a connection successful for one profile since the re-connect *only* works if we
        // have already connected successfully over one of the profiles
        Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bondedDevices[0]);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED);
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        mPhonePolicy.handleAclConnected(bondedDevices[0]);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Check that we don't get any calls to reconnect
        verify(mA2dpService, after(CONNECT_OTHER_PROFILES_TIMEOUT_WAIT_MILLIS).never()).connect(
@@ -920,12 +914,8 @@ public class PhonePolicyTest {

        // We send a connection successful for one profile since the re-connect *only* works if we
        // have already connected successfully over one of the profiles
        Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bondedDevices[0]);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED);
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        mPhonePolicy.handleAclConnected(bondedDevices[0]);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Check that we don't get any calls to reconnect
        verify(mA2dpService, after(CONNECT_OTHER_PROFILES_TIMEOUT_WAIT_MILLIS).never()).connect(