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

Commit c06001fd authored by Aritra Sen's avatar Aritra Sen
Browse files

Make Phone Policy changes on connection state changes handled via direct calls...

Make Phone Policy changes on connection state changes handled via direct calls routed via adapter service.

Tag: #refactor
Bug: 296932947
Test: atest BluetoothInstrumentationTests
Change-Id: Ib4ad2f08d047e4a63bc01ecdd467b72f1bb78218
parent bb6c315e
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -1282,6 +1282,8 @@ public class A2dpService extends ProfileService {
        }
        }
        mAdapterService.notifyProfileConnectionStateChangeToGatt(
        mAdapterService.notifyProfileConnectionStateChangeToGatt(
                BluetoothProfile.A2DP, fromState, toState);
                BluetoothProfile.A2DP, fromState, toState);
        mAdapterService.handleProfileConnectionStateChange(
                BluetoothProfile.A2DP, device, fromState, toState);
        mAdapterService
        mAdapterService
                .getActiveDeviceManager()
                .getActiveDeviceManager()
                .profileConnectionStateChanged(BluetoothProfile.A2DP, device, fromState, toState);
                .profileConnectionStateChanged(BluetoothProfile.A2DP, device, fromState, toState);
+11 −0
Original line number Original line Diff line number Diff line
@@ -6857,6 +6857,17 @@ public class AdapterService extends Service {
        mGattService.notifyProfileConnectionStateChange(profile, fromState, toState);
        mGattService.notifyProfileConnectionStateChange(profile, fromState, toState);
    }
    }


    /**
     * Handle Bluetooth app state when connection state changes for a given {@code profile}.
     *
     * <p>Currently this function is limited to handling Phone policy but the eventual goal is to
     * move all connection logic here.
     */
    public void handleProfileConnectionStateChange(
            int profile, BluetoothDevice device, int fromState, int toState) {
        mPhonePolicy.profileConnectionStateChanged(profile, device, fromState, toState);
    }

    static int convertScanModeToHal(int mode) {
    static int convertScanModeToHal(int mode) {
        switch (mode) {
        switch (mode) {
            case BluetoothAdapter.SCAN_MODE_NONE:
            case BluetoothAdapter.SCAN_MODE_NONE:
+16 −43
Original line number Original line Diff line number Diff line
@@ -21,14 +21,12 @@ import static com.android.bluetooth.Utils.isDualModeAudioEnabled;
import android.annotation.RequiresPermission;
import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.BluetoothVolumeControl;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
@@ -91,7 +89,6 @@ class PhonePolicy {
    private static final String TAG = "BluetoothPhonePolicy";
    private static final String TAG = "BluetoothPhonePolicy";


    // 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_PROFILE_CONNECTION_STATE_CHANGED = 1;
    private static final int MESSAGE_CONNECT_OTHER_PROFILES = 3;
    private static final int MESSAGE_CONNECT_OTHER_PROFILES = 3;
    private static final int MESSAGE_ADAPTER_STATE_TURNED_ON = 4;
    private static final int MESSAGE_ADAPTER_STATE_TURNED_ON = 4;
    private static final int MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED = 5;
    private static final int MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED = 5;
@@ -116,6 +113,22 @@ class PhonePolicy {
    private final HashSet<BluetoothDevice> mConnectOtherProfilesDeviceSet = new HashSet<>();
    private final HashSet<BluetoothDevice> mConnectOtherProfilesDeviceSet = new HashSet<>();
    @VisibleForTesting boolean mAutoConnectProfilesSupported;
    @VisibleForTesting boolean mAutoConnectProfilesSupported;


    public void profileConnectionStateChanged(
            int profile, BluetoothDevice device, int fromState, int toState) {
        switch (profile) {
            case BluetoothProfile.A2DP:
            case BluetoothProfile.HEADSET:
            case BluetoothProfile.LE_AUDIO:
            case BluetoothProfile.CSIP_SET_COORDINATOR:
            case BluetoothProfile.VOLUME_CONTROL:
                mHandler.post(
                        () -> processProfileStateChanged(device, profile, toState, fromState));
                break;
            default:
                break;
        }
    }

    // Broadcast receiver for all changes to states of various profiles
    // Broadcast receiver for all changes to states of various profiles
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        @Override
@@ -126,31 +139,6 @@ class PhonePolicy {
                return;
                return;
            }
            }
            switch (action) {
            switch (action) {
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                            BluetoothProfile.HEADSET, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                            BluetoothProfile.A2DP, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothCsipSetCoordinator.ACTION_CSIS_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                            BluetoothProfile.CSIP_SET_COORDINATOR, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                            BluetoothProfile.LE_AUDIO, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothVolumeControl.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                            BluetoothProfile.VOLUME_CONTROL, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED,
                    mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED,
                            BluetoothProfile.A2DP, -1, // No-op argument
                            BluetoothProfile.A2DP, -1, // No-op argument
@@ -203,16 +191,6 @@ class PhonePolicy {
        @Override
        @Override
        public void handleMessage(Message msg) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            switch (msg.what) {
                case MESSAGE_PROFILE_CONNECTION_STATE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
                    int nextState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
                    processProfileStateChanged(device, msg.arg1, nextState, prevState);
                }
                break;

                case MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED: {
                case MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice activeDevice =
                    BluetoothDevice activeDevice =
@@ -249,11 +227,6 @@ class PhonePolicy {
    protected void start() {
    protected void start() {
        IntentFilter filter = new IntentFilter();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED);
        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_ACL_CONNECTED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
+2 −0
Original line number Original line Diff line number Diff line
@@ -1056,6 +1056,8 @@ public class CsipSetCoordinatorService extends ProfileService {
            mGroupIdToConnectedDevices.get(groupId).add(device);
            mGroupIdToConnectedDevices.get(groupId).add(device);
            disableCsipIfNeeded(groupId);
            disableCsipIfNeeded(groupId);
        }
        }
        mAdapterService.handleProfileConnectionStateChange(
                BluetoothProfile.CSIP_SET_COORDINATOR, device, fromState, toState);
    }
    }


    /**
    /**
+2 −0
Original line number Original line Diff line number Diff line
@@ -2007,6 +2007,8 @@ public class HeadsetService extends ProfileService {
                .handleHeadsetConnectionStateChanged(device, fromState, toState);
                .handleHeadsetConnectionStateChanged(device, fromState, toState);
        mAdapterService.notifyProfileConnectionStateChangeToGatt(
        mAdapterService.notifyProfileConnectionStateChangeToGatt(
                BluetoothProfile.HEADSET, fromState, toState);
                BluetoothProfile.HEADSET, fromState, toState);
        mAdapterService.handleProfileConnectionStateChange(
                BluetoothProfile.HEADSET, device, fromState, toState);
    }
    }


    /**
    /**
Loading