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 Original line 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
     * Notify GATT of a Bluetooth profile's connection state change for a given {@link
     * BluetoothProfile}.
     * BluetoothProfile}.
+2 −32
Original line number Original line 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)
    // 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_CONNECT_OTHER_PROFILES = 3;
    private static final int MESSAGE_DEVICE_CONNECTED = 6;


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


    // Broadcast receiver for all changes to states of various profiles
    public void handleAclConnected(BluetoothDevice device) {
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        mHandler.post(() -> processDeviceConnected(device));
        @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;
    }
    }


    // Handler to handoff intents to class thread
    // Handler to handoff intents to class thread
@@ -171,12 +149,6 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                    mConnectOtherProfilesDeviceSet.remove(device);
                    mConnectOtherProfilesDeviceSet.remove(device);
                    break;
                    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();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        mAdapterService.registerReceiver(mReceiver, filter);
    }
    }


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


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


        // We send a connection successful for one profile since the re-connect *only* works if we
        mPhonePolicy.handleAclConnected(bondedDevices[0]);
        // have already connected successfully over one of the profiles
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        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);


        // Check that we don't get any calls to reconnect
        // Check that we don't get any calls to reconnect
        verify(mA2dpService, after(CONNECT_OTHER_PROFILES_TIMEOUT_WAIT_MILLIS).never()).connect(
        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
        // 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
        // have already connected successfully over one of the profiles
        Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        mPhonePolicy.handleAclConnected(bondedDevices[0]);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bondedDevices[0]);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        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);


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