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

Commit ee511dd6 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

Use mDatabaseManager when calling database functions

When Bluetooth is turning off, there were cases
that getConnectedPolicy() or setConnectedPolicy()
of ProfileService are called, which threw NPE as
mAdapterService is null.
This CL adds mDatabaseManager to profile services
so that they can handle those cases.

Bug: 231243228
Tag: #stability
Test: atest GoogleBluetoothInstrumentationTests &&
manually turning on/off Bluetooth
to confirm NPE is not thrown

Merged-In: I7e6006d69d0ce245f27e27002e9e6c5740e7093b
Change-Id: I7e6006d69d0ce245f27e27002e9e6c5740e7093b
(cherry picked from commit c7b7420e)
parent f05df7a8
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.util.Log;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;

@@ -62,6 +63,7 @@ public class BatteryService extends ProfileService {
    private static BatteryService sBatteryService;

    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
    private final Map<BluetoothDevice, BatteryStateMachine> mStateMachines = new HashMap<>();

@@ -94,6 +96,8 @@ public class BatteryService extends ProfileService {

        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when BatteryService starts");
        mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                "DatabaseManager cannot be null when BatteryService starts");

        mStateMachines.clear();
        mStateMachinesThread = new HandlerThread("BatteryService.StateMachines");
@@ -395,8 +399,7 @@ public class BatteryService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
        mAdapterService.getDatabase()
                .setProfileConnectionPolicy(device, BluetoothProfile.BATTERY,
        mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.BATTERY,
                        connectionPolicy);
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            connect(device);
@@ -413,8 +416,7 @@ public class BatteryService extends ProfileService {
    public int getConnectionPolicy(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED,
                "Need BLUETOOTH_PRIVILEGED permission");
        return mAdapterService.getDatabase()
                .getProfileConnectionPolicy(device, BluetoothProfile.BATTERY);
        return mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.BATTERY);
    }
    /**
     * Called when the battery level of the device is notified.
+7 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.bluetooth.bass_client;

import static android.Manifest.permission.BLUETOOTH_CONNECT;

import static com.android.bluetooth.Utils.enforceBluetoothPrivilegedPermission;

import android.bluetooth.BluetoothAdapter;
@@ -47,6 +48,7 @@ import android.util.Log;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.internal.annotations.VisibleForTesting;

import java.nio.ByteBuffer;
@@ -74,6 +76,7 @@ public class BassClientService extends ProfileService {
    private HandlerThread mStateMachinesThread;
    private HandlerThread mCallbackHandlerThread;
    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BassUtils mBassUtils = null;
    private Map<BluetoothDevice, BluetoothDevice> mActiveSourceMap;
@@ -218,6 +221,8 @@ public class BassClientService extends ProfileService {
        }
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when BassClientService starts");
        mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                "DatabaseManager cannot be null when BassClientService starts");
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mStateMachines.clear();
        mStateMachinesThread = new HandlerThread("BassClientService.StateMachines");
@@ -545,7 +550,7 @@ public class BassClientService extends ProfileService {
            Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
        boolean setSuccessfully =
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                mDatabaseManager.setProfileConnectionPolicy(device,
                        BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT, connectionPolicy);
        if (setSuccessfully && connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            connect(device);
@@ -568,8 +573,7 @@ public class BassClientService extends ProfileService {
     * @return connection policy of the device
     */
    public int getConnectionPolicy(BluetoothDevice device) {
        return mAdapterService
                .getDatabase()
        return mDatabaseManager
                .getProfileConnectionPolicy(device, BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT);
    }

+7 −3
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.util.Pair;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;

@@ -76,6 +77,7 @@ public class CsipSetCoordinatorService extends ProfileService {
    private static CsipSetCoordinatorService sCsipSetCoordinatorService;

    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
    private BluetoothDevice mPreviousAudioDevice;

@@ -121,10 +123,12 @@ public class CsipSetCoordinatorService extends ProfileService {
            throw new IllegalStateException("start() called twice");
        }

        // Get AdapterService, CsipSetCoordinatorNativeInterface.
        // Get AdapterService, DatabaseManager, CsipSetCoordinatorNativeInterface.
        // None of them can be null.
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when CsipSetCoordinatorService starts");
        mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                "DatabaseManager cannot be null when CsipSetCoordinatorService starts");
        mCsipSetCoordinatorNativeInterface = Objects.requireNonNull(
                CsipSetCoordinatorNativeInterface.getInstance(),
                "CsipSetCoordinatorNativeInterface cannot be null when"
@@ -461,7 +465,7 @@ public class CsipSetCoordinatorService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
        mAdapterService.getDatabase().setProfileConnectionPolicy(
        mDatabaseManager.setProfileConnectionPolicy(
                device, BluetoothProfile.CSIP_SET_COORDINATOR, connectionPolicy);
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            connect(device);
@@ -479,7 +483,7 @@ public class CsipSetCoordinatorService extends ProfileService {
     */
    public int getConnectionPolicy(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        return mAdapterService.getDatabase().getProfileConnectionPolicy(
        return mDatabaseManager.getProfileConnectionPolicy(
                device, BluetoothProfile.CSIP_SET_COORDINATOR);
    }

+7 −5
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.csip.CsipSetCoordinatorService;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;
@@ -79,6 +80,7 @@ public class HapClientService extends ProfileService {
    @VisibleForTesting
    HapClientNativeInterface mHapClientNativeInterface;
    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
    private BroadcastReceiver mBondStateChangedReceiver;
    private BroadcastReceiver mConnectionStateChangedReceiver;
@@ -151,10 +153,12 @@ public class HapClientService extends ProfileService {
            throw new IllegalStateException("start() called twice");
        }

        // Get AdapterService, HapClientNativeInterface, AudioManager.
        // Get AdapterService, HapClientNativeInterface, DatabaseManager, AudioManager.
        // None of them can be null.
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when HapClientService starts");
        mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                "DatabaseManager cannot be null when HapClientService starts");
        mHapClientNativeInterface = Objects.requireNonNull(
                HapClientNativeInterface.getInstance(),
                "HapClientNativeInterface cannot be null when HapClientService starts");
@@ -368,8 +372,7 @@ public class HapClientService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
        mAdapterService.getDatabase()
                .setProfileConnectionPolicy(device, BluetoothProfile.HAP_CLIENT,
        mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.HAP_CLIENT,
                        connectionPolicy);
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            connect(device);
@@ -392,8 +395,7 @@ public class HapClientService extends ProfileService {
     * @hide
     */
    public int getConnectionPolicy(BluetoothDevice device) {
        return mAdapterService.getDatabase()
                .getProfileConnectionPolicy(device, BluetoothProfile.HAP_CLIENT);
        return mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.HAP_CLIENT);
    }

    /**
+7 −5
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;

@@ -71,6 +72,7 @@ public class VolumeControlService extends ProfileService {
    private static VolumeControlService sVolumeControlService;

    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
    private BluetoothDevice mPreviousAudioDevice;

@@ -211,10 +213,12 @@ public class VolumeControlService extends ProfileService {
            throw new IllegalStateException("start() called twice");
        }

        // Get AdapterService, VolumeControlNativeInterface, AudioManager.
        // Get AdapterService, VolumeControlNativeInterface, DatabaseManager, AudioManager.
        // None of them can be null.
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when VolumeControlService starts");
        mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                "DatabaseManager cannot be null when VolumeControlService starts");
        mVolumeControlNativeInterface = Objects.requireNonNull(
                VolumeControlNativeInterface.getInstance(),
                "VolumeControlNativeInterface cannot be null when VolumeControlService starts");
@@ -532,8 +536,7 @@ public class VolumeControlService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
        mAdapterService.getDatabase()
                .setProfileConnectionPolicy(device, BluetoothProfile.VOLUME_CONTROL,
        mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.VOLUME_CONTROL,
                        connectionPolicy);
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            connect(device);
@@ -547,8 +550,7 @@ public class VolumeControlService extends ProfileService {
    public int getConnectionPolicy(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED,
                "Need BLUETOOTH_PRIVILEGED permission");
        return mAdapterService.getDatabase()
                .getProfileConnectionPolicy(device, BluetoothProfile.VOLUME_CONTROL);
        return mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.VOLUME_CONTROL);
    }

    boolean isVolumeOffsetAvailable(BluetoothDevice device) {
Loading