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

Commit 65db42e1 authored by Aritra Sen's avatar Aritra Sen
Browse files

Make Phone Policy changes on active device changes handled via direct calls...

Make Phone Policy changes on active device changes handled via direct calls routed via adapter service.

Move active device changes logic in adapter properties to handle it more cleanly.

Tag: #refactor
Bug: 296932947
Test: atest BluetoothInstrumentationTests
Change-Id: I998246f8da045921c018a5a89b355665b229050b
parent 95fcc49a
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -1068,9 +1068,7 @@ public class A2dpService extends ProfileService {
            mActiveDevice = device;
        }

        mAdapterService
                .getActiveDeviceManager()
                .profileActiveDeviceChanged(BluetoothProfile.A2DP, device);
        mAdapterService.handleActiveDeviceChange(BluetoothProfile.A2DP, device);
        mAdapterService.getSilenceDeviceManager().a2dpActiveDeviceChanged(device);

        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED,
+6 −0
Original line number Diff line number Diff line
@@ -6916,6 +6916,12 @@ public class AdapterService extends Service {
        mPhonePolicy.profileConnectionStateChanged(profile, device, fromState, toState);
    }

    /** Handle Bluetooth app state when active device changes for a given {@code profile}. */
    public void handleActiveDeviceChange(int profile, BluetoothDevice device) {
        mActiveDeviceManager.profileActiveDeviceChanged(profile, device);
        mPhonePolicy.profileActiveDeviceChanged(profile, device);
    }

    static int convertScanModeToHal(int mode) {
        switch (mode) {
            case BluetoothAdapter.SCAN_MODE_NONE:
+10 −37
Original line number Diff line number Diff line
@@ -19,12 +19,8 @@ package com.android.bluetooth.btservice;
import static com.android.bluetooth.Utils.isDualModeAudioEnabled;

import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.content.BroadcastReceiver;
@@ -91,7 +87,6 @@ class PhonePolicy {
    // 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_ADAPTER_STATE_TURNED_ON = 4;
    private static final int MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED = 5;
    private static final int MESSAGE_DEVICE_CONNECTED = 6;

    @VisibleForTesting static final String AUTO_CONNECT_PROFILES_PROPERTY =
@@ -129,6 +124,16 @@ class PhonePolicy {
        }
    }

    /**
     * Called when active state of audio profiles changed
     *
     * @param profile The Bluetooth profile of which active state changed
     * @param device The device currently activated. {@code null} if no A2DP device activated
     */
    public void profileActiveDeviceChanged(int profile, BluetoothDevice device) {
        mHandler.post(() -> processActiveDeviceChanged(device, profile));
    }

    // Broadcast receiver for all changes to states of various profiles
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
@@ -139,26 +144,6 @@ class PhonePolicy {
                return;
            }
            switch (action) {
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED,
                            BluetoothProfile.A2DP, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED,
                            BluetoothProfile.HEADSET, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED,
                            BluetoothProfile.HEARING_AID, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED,
                            BluetoothProfile.LE_AUDIO, -1, // No-op argument
                            intent).sendToTarget();
                    break;
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    // Only pass the message on if the adapter has actually changed state from
                    // non-ON to ON. NOTE: ON is the state depicting BREDR ON and not just BLE ON.
@@ -191,14 +176,6 @@ class PhonePolicy {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice activeDevice =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    processActiveDeviceChanged(activeDevice, msg.arg1);
                }
                break;

                case MESSAGE_CONNECT_OTHER_PROFILES: {
                    // Called when we try connect some profiles in processConnectOtherProfiles but
                    // we send a delayed message to try connecting the remaining profiles
@@ -229,10 +206,6 @@ class PhonePolicy {
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
        mAdapterService.registerReceiver(mReceiver, filter);
    }

+1 −3
Original line number Diff line number Diff line
@@ -724,9 +724,7 @@ public class HearingAidService extends ProfileService {
    }

    private void notifyActiveDeviceChanged() {
        mAdapterService
                .getActiveDeviceManager()
                .profileActiveDeviceChanged(BluetoothProfile.HEARING_AID, mActiveDevice);
        mAdapterService.handleActiveDeviceChange(BluetoothProfile.HEARING_AID, mActiveDevice);
        Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mActiveDevice);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
+1 −3
Original line number Diff line number Diff line
@@ -2135,9 +2135,7 @@ public class HeadsetService extends ProfileService {
    private void broadcastActiveDevice(BluetoothDevice device) {
        logD("broadcastActiveDevice: " + device);

        mAdapterService
                .getActiveDeviceManager()
                .profileActiveDeviceChanged(BluetoothProfile.HEADSET, device);
        mAdapterService.handleActiveDeviceChange(BluetoothProfile.HEADSET, device);
        mAdapterService.getSilenceDeviceManager().hfpActiveDeviceChanged(device);

        BluetoothStatsLog.write(
Loading