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

Commit f6f54256 authored by William Escande's avatar William Escande
Browse files

HidHost: Use AdapterService provided

The data provided in parameter of the constructor is already an
adapterService. Making use of it remove the need to rely on
AdapterService.getAdapterService (known for flakiness in test)

Test: atest HidHostServiceTest
Bug: 311772251
Flag: Exempt, no-op change
Change-Id: Id1d683d811f42b97120035eb9f79d68db426cc49
parent 42062718
Loading
Loading
Loading
Loading
+14 −31
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.IBluetoothHidHost;
import android.content.AttributionSource;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
@@ -112,8 +111,8 @@ public class HidHostService extends ProfileService {
    private static HidHostService sHidHostService;
    private BluetoothDevice mTargetDevice = null;

    private DatabaseManager mDatabaseManager;
    private AdapterService mAdapterService;
    private final AdapterService mAdapterService;
    private final DatabaseManager mDatabaseManager;
    private final HidHostNativeInterface mNativeInterface;

    private static final int MESSAGE_CONNECT = 1;
@@ -140,8 +139,11 @@ public class HidHostService extends ProfileService {
    public static final int STATE_DISCONNECTING = BluetoothProfile.STATE_DISCONNECTING;
    public static final int STATE_ACCEPTING = BluetoothProfile.STATE_DISCONNECTING + 1;

    public HidHostService(Context ctx) {
        super(ctx);
    public HidHostService(AdapterService adapterService) {
        super(adapterService);

        mAdapterService = requireNonNull(adapterService);
        mDatabaseManager = requireNonNull(mAdapterService.getDatabase());
        mNativeInterface = requireNonNull(HidHostNativeInterface.getInstance());
    }

@@ -156,15 +158,6 @@ public class HidHostService extends ProfileService {

    @Override
    public void start() {
        mDatabaseManager =
                requireNonNull(
                        AdapterService.getAdapterService().getDatabase(),
                        "DatabaseManager cannot be null when HidHostService starts");
        mAdapterService =
                requireNonNull(
                        AdapterService.getAdapterService(),
                        "AdapterService cannot be null when HidHostService starts");

        mNativeInterface.init(this);
        mNativeAvailable = true;
        setHidHostService(this);
@@ -201,7 +194,7 @@ public class HidHostService extends ProfileService {

    private byte[] getIdentityAddress(BluetoothDevice device) {
        if (Flags.identityAddressNullIfUnknown()) {
            return Utils.getByteBrEdrAddress(device);
            return Utils.getByteBrEdrAddress(mAdapterService, device);
        } else {
            return mAdapterService.getByteIdentityAddress(device);
        }
@@ -819,8 +812,7 @@ public class HidHostService extends ProfileService {
            // Locally initiated connection, move out of quiet mode
            Log.i(TAG, "updateQuiteMode: " + " Move out of quite mode for device: " + device);
            mTargetDevice = null;
            AdapterService adapterService = AdapterService.getAdapterService();
            adapterService.enable(false);
            mAdapterService.enable(false);
        }
    }

@@ -1452,11 +1444,8 @@ public class HidHostService extends ProfileService {
                        + "->"
                        + newState);

        AdapterService adapterService = AdapterService.getAdapterService();
        if (adapterService != null) {
            adapterService.updateProfileConnectionAdapterProperties(
        mAdapterService.updateProfileConnectionAdapterProperties(
                device, BluetoothProfile.HID_HOST, newState, prevState);
        }

        Intent intent = new Intent(BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
@@ -1525,20 +1514,14 @@ public class HidHostService extends ProfileService {
     */
    @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
    public boolean okToConnect(BluetoothDevice device) {
        AdapterService adapterService = AdapterService.getAdapterService();
        // Check if adapter service is null.
        if (adapterService == null) {
            Log.w(TAG, "okToConnect: adapter service is null");
            return false;
        }
        // Check if this is an incoming connection in Quiet mode.
        if (adapterService.isQuietModeEnabled() && mTargetDevice == null) {
            Log.w(TAG, "okToConnect: return false as quiet mode enabled");
        if (mAdapterService.isQuietModeEnabled() && mTargetDevice == null) {
            Log.w(TAG, "okToConnect: return false because of quiet mode enabled. device=" + device);
            return false;
        }
        // Check connection policy and accept or reject the connection.
        int connectionPolicy = getConnectionPolicy(device);
        int bondState = adapterService.getBondState(device);
        int bondState = mAdapterService.getBondState(device);
        // Allow this connection only if the device is bonded. Any attempt to connect while
        // bonding would potentially lead to an unauthorized connection.
        if (bondState != BluetoothDevice.BOND_BONDED) {
+13 −15
Original line number Diff line number Diff line
@@ -20,9 +20,8 @@ import static org.mockito.Mockito.*;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Looper;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;

@@ -43,29 +42,29 @@ import org.mockito.junit.MockitoRule;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class HidHostServiceTest {
    private HidHostService mService = null;
    private BluetoothAdapter mAdapter = null;
    private BluetoothDevice mTestDevice;
    private Context mTargetContext;

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

    private HidHostService mService;
    private BluetoothDevice mTestDevice;

    @Mock private AdapterService mAdapterService;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private HidHostNativeInterface mNativeInterface;

    @Before
    public void setUp() throws Exception {
        mTargetContext = InstrumentationRegistry.getTargetContext();
        TestUtils.setAdapterService(mAdapterService);
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        HidHostNativeInterface.setInstance(mNativeInterface);
        mService = new HidHostService(mTargetContext);

        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        mService = new HidHostService(mAdapterService);
        mService.start();
        mService.setAvailable(true);
        // Try getting the Bluetooth adapter
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        Assert.assertNotNull(mAdapter);

        // Get a device for testing
        mTestDevice = TestUtils.getTestDevice(mAdapter, 0);
@@ -78,7 +77,6 @@ public class HidHostServiceTest {
        HidHostNativeInterface.setInstance(null);
        mService = HidHostService.getHidHostService();
        Assert.assertNull(mService);
        TestUtils.clearAdapterService(mAdapterService);
    }

    @Test