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

Commit bdeeb88a authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I59017504,I9eef4745,I5d701c9b into main

* changes:
  Stop using BluetoothA2dp and BluetoothHeadset Broadcasts for CONNECTION_STATE_CHANGED in SilenceDeviceManager.
  Stop using HFP active device changed broadcast for SilenceDeviceManager.
  Stop using A2DP active device changed for SilenceDeviceManager.
parents c55a5242 ea13f47d
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -1080,14 +1080,17 @@ public class A2dpService extends ProfileService {
            mActiveDevice = device;
        }

        mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device);
        mAdapterService.getSilenceDeviceManager().a2dpActiveDeviceChanged(device);

        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED,
                BluetoothProfile.A2DP, mAdapterService.obfuscateAddress(device),
                mAdapterService.getMetricId(device));

        Intent intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
    }
@@ -1285,6 +1288,9 @@ public class A2dpService extends ProfileService {
        mAdapterService
                .getActiveDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
        mAdapterService
                .getSilenceDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
    }

    /**
+4 −0
Original line number Diff line number Diff line
@@ -758,6 +758,10 @@ public class AdapterService extends Service {
        return mActiveDeviceManager;
    }

    public SilenceDeviceManager getSilenceDeviceManager() {
        return mSilenceDeviceManager;
    }

    private boolean initMetricsLogger() {
        if (mMetricsLogger != null) {
            return false;
+70 −92
Original line number Diff line number Diff line
@@ -19,14 +19,9 @@ package com.android.bluetooth.btservice;
import static android.Manifest.permission.BLUETOOTH_CONNECT;

import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@@ -76,43 +71,52 @@ public class SilenceDeviceManager {
    private static final int MSG_SILENCE_DEVICE_STATE_CHANGED = 1;
    private static final int MSG_A2DP_CONNECTION_STATE_CHANGED = 10;
    private static final int MSG_HFP_CONNECTION_STATE_CHANGED = 11;
    private static final int MSG_A2DP_ACTIVE_DEIVCE_CHANGED = 20;
    private static final int MSG_A2DP_ACTIVE_DEVICE_CHANGED = 20;
    private static final int MSG_HFP_ACTIVE_DEVICE_CHANGED = 21;
    private static final int ENABLE_SILENCE = 0;
    private static final int DISABLE_SILENCE = 1;

    // Broadcast receiver for all changes
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == null) {
                Log.e(TAG, "Received intent with null action");
                return;
    /**
     * Called when A2DP connection state changed by A2dpService
     *
     * @param device The device of which connection state was changed
     * @param fromState The previous connection state of the device
     * @param toState The new connection state of the device
     */
    public void a2dpConnectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        mHandler.obtainMessage(MSG_A2DP_CONNECTION_STATE_CHANGED, fromState, toState, device)
                .sendToTarget();
    }
            switch (action) {
                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MSG_A2DP_CONNECTION_STATE_CHANGED,
                                           intent).sendToTarget();
                    break;
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED,
                                           intent).sendToTarget();
                    break;
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEIVCE_CHANGED,
                                           intent).sendToTarget();
                    break;
                case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MSG_HFP_ACTIVE_DEVICE_CHANGED,
                        intent).sendToTarget();
                    break;
                default:
                    Log.e(TAG, "Received unexpected intent, action=" + action);
                    break;

    /**
     * Called when A2DP active device changed by A2dpService
     *
     * @param device The device currently activated. {@code null} if no A2DP device activated
     */
    public void a2dpActiveDeviceChanged(BluetoothDevice device) {
        mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEVICE_CHANGED, device).sendToTarget();
    }

    /**
     * Called when HFP connection state changed by HeadsetService
     *
     * @param device The device of which connection state was changed
     * @param fromState The previous connection state of the device
     * @param toState The new connection state of the device
     */
    public void hfpConnectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED, fromState, toState, device)
                .sendToTarget();
    }

    /**
     * Called when HFP active device is changed by HeadsetService
     *
     * @param device The device currently activated. {@code null} if no HFP device activated
     */
    public void hfpActiveDeviceChanged(BluetoothDevice device) {
        mHandler.obtainMessage(MSG_HFP_ACTIVE_DEVICE_CHANGED, device).sendToTarget();
    }
    };

    class SilenceDeviceManagerHandler extends Handler {
        SilenceDeviceManagerHandler(Looper looper) {
@@ -132,12 +136,10 @@ public class SilenceDeviceManager {
                }
                break;

                case MSG_A2DP_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);
                case MSG_A2DP_CONNECTION_STATE_CHANGED:
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    int prevState = msg.arg1;
                    int nextState = msg.arg2;

                    if (nextState == BluetoothProfile.STATE_CONNECTED) {
                        // enter connected state
@@ -153,62 +155,51 @@ public class SilenceDeviceManager {
                            mSilenceDevices.remove(device);
                        }
                    }
                }
                    break;

                case MSG_HFP_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);
                case MSG_HFP_CONNECTION_STATE_CHANGED:
                    BluetoothDevice bluetoothDevice = (BluetoothDevice) msg.obj;
                    int prev = msg.arg1;
                    int next = msg.arg2;

                    if (nextState == BluetoothProfile.STATE_CONNECTED) {
                    if (next == BluetoothProfile.STATE_CONNECTED) {
                        // enter connected state
                        addConnectedDevice(device, BluetoothProfile.HEADSET);
                        if (!mSilenceDevices.containsKey(device)) {
                            mSilenceDevices.put(device, false);
                        addConnectedDevice(bluetoothDevice, BluetoothProfile.HEADSET);
                        if (!mSilenceDevices.containsKey(bluetoothDevice)) {
                            mSilenceDevices.put(bluetoothDevice, false);
                        }
                    } else if (prevState == BluetoothProfile.STATE_CONNECTED) {
                    } else if (prev == BluetoothProfile.STATE_CONNECTED) {
                        // exiting from connected state
                        removeConnectedDevice(device, BluetoothProfile.HEADSET);
                        if (!isBluetoothAudioConnected(device)) {
                            handleSilenceDeviceStateChanged(device, false);
                            mSilenceDevices.remove(device);
                        }
                        removeConnectedDevice(bluetoothDevice, BluetoothProfile.HEADSET);
                        if (!isBluetoothAudioConnected(bluetoothDevice)) {
                            handleSilenceDeviceStateChanged(bluetoothDevice, false);
                            mSilenceDevices.remove(bluetoothDevice);
                        }
                    }
                    break;

                case MSG_A2DP_ACTIVE_DEIVCE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice a2dpActiveDevice =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                case MSG_A2DP_ACTIVE_DEVICE_CHANGED:
                    BluetoothDevice a2dpActiveDevice = (BluetoothDevice) msg.obj;
                    if (getSilenceMode(a2dpActiveDevice)) {
                        // Resume the device from silence mode.
                        setSilenceMode(a2dpActiveDevice, false);
                    }
                }
                    break;

                case MSG_HFP_ACTIVE_DEVICE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice hfpActiveDevice =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                case MSG_HFP_ACTIVE_DEVICE_CHANGED:
                    BluetoothDevice hfpActiveDevice = (BluetoothDevice) msg.obj;
                    if (getSilenceMode(hfpActiveDevice)) {
                        // Resume the device from silence mode.
                        setSilenceMode(hfpActiveDevice, false);
                    }
                }
                    break;

                default: {
                default:
                    Log.e(TAG, "Unknown message: " + msg.what);
                }
                    break;
            }
        }
    };
    }

    SilenceDeviceManager(AdapterService service, ServiceFactory factory, Looper looper) {
        mAdapterService = service;
@@ -221,13 +212,6 @@ public class SilenceDeviceManager {
            Log.v(TAG, "start()");
        }
        mHandler = new SilenceDeviceManagerHandler(mLooper);
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        mAdapterService.registerReceiver(mReceiver, filter);
    }

    void cleanup() {
@@ -235,7 +219,6 @@ public class SilenceDeviceManager {
            Log.v(TAG, "cleanup()");
        }
        mSilenceDevices.clear();
        mAdapterService.unregisterReceiver(mReceiver);
    }

    @VisibleForTesting
@@ -347,9 +330,4 @@ public class SilenceDeviceManager {
            writer.println("  " + device+ "  | " + getSilenceMode(device));
        }
    }

    @VisibleForTesting
    BroadcastReceiver getBroadcastReceiver() {
        return mReceiver;
    }
}
+11 −2
Original line number Diff line number Diff line
@@ -2008,6 +2008,9 @@ public class HeadsetService extends ProfileService {
            }
        }
        mActiveDeviceManager.hfpConnectionStateChanged(device, fromState, toState);
        mAdapterService
                .getSilenceDeviceManager()
                .hfpConnectionStateChanged(device, fromState, toState);
    }

    /**
@@ -2133,10 +2136,16 @@ public class HeadsetService extends ProfileService {

    private void broadcastActiveDevice(BluetoothDevice device) {
        logD("broadcastActiveDevice: " + device);

        mAdapterService.getActiveDeviceManager().hfpActiveStateChanged(device);
        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED,
                BluetoothProfile.HEADSET, mAdapterService.obfuscateAddress(device),
        mAdapterService.getSilenceDeviceManager().hfpActiveDeviceChanged(device);

        BluetoothStatsLog.write(
                BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED,
                BluetoothProfile.HEADSET,
                mAdapterService.obfuscateAddress(device),
                mAdapterService.getMetricId(device));

        Intent intent = new Intent(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
+3 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.ActiveDeviceManager;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.SilenceDeviceManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
@@ -79,6 +80,7 @@ public class A2dpServiceTest {
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private A2dpNativeInterface mMockNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private SilenceDeviceManager mSilenceDeviceManager;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

@@ -99,6 +101,7 @@ public class A2dpServiceTest {
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();
        doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager();

        mAdapter = BluetoothAdapter.getDefaultAdapter();

Loading