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

Commit 755e52ba authored by Angela Wang's avatar Angela Wang
Browse files

Integrates Bluetooth HAP service client into SettingsLib (2/3)

Refreshes the device when the profile connected and invokes the callback
when the profile connected or disconnected.

Bug: 249235823
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=HapClientProfileTest
Change-Id: I9625543ad4af53a301af3db799348d7d8ca72add
parent 8f3744e2
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -26,9 +26,12 @@ import android.bluetooth.BluetoothHapClient;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;

import com.android.settingslib.R;

import java.util.List;

/**
 * HapClientProfile handles the Bluetooth HAP service client role.
 */
@@ -39,6 +42,8 @@ public class HapClientProfile implements LocalBluetoothProfile {
    private static final int ORDINAL = 1;

    private final BluetoothAdapter mBluetoothAdapter;
    private final CachedBluetoothDeviceManager mDeviceManager;
    private final LocalBluetoothProfileManager mProfileManager;
    private BluetoothHapClient mService;
    private boolean mIsProfileReady;

@@ -48,17 +53,36 @@ public class HapClientProfile implements LocalBluetoothProfile {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            mService = (BluetoothHapClient) proxy;
            // We just bound to the service, so refresh the UI for any connected HapClient devices.
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
            while (!deviceList.isEmpty()) {
                BluetoothDevice nextDevice = deviceList.remove(0);
                CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
                // Adds a new device into mDeviceManager if it does not exist
                if (device == null) {
                    Log.w(TAG, "HapClient profile found new device: " + nextDevice);
                    device = mDeviceManager.addDevice(nextDevice);
                }
                device.onProfileStateChanged(
                        HapClientProfile.this, BluetoothProfile.STATE_CONNECTED);
                device.refresh();
            }

            mIsProfileReady = true;
            mProfileManager.callServiceConnectedListeners();
        }

        @Override
        public void onServiceDisconnected(int profile) {
            mIsProfileReady = false;
            mProfileManager.callServiceDisconnectedListeners();
        }
    }

    HapClientProfile(Context context, CachedBluetoothDeviceManager deviceManager,
            LocalBluetoothProfileManager profileManager) {
        mDeviceManager = deviceManager;
        mProfileManager = profileManager;
        BluetoothManager bluetoothManager = context.getSystemService(BluetoothManager.class);
        if (bluetoothManager != null) {
            mBluetoothAdapter = bluetoothManager.getAdapter();
+3 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;
@@ -78,6 +79,7 @@ public class HapClientProfileTest {
        mServiceListener.onServiceConnected(BluetoothProfile.HAP_CLIENT, mService);

        assertThat(mProfile.isProfileReady()).isTrue();
        verify(mProfileManager).callServiceConnectedListeners();
    }

    @Test
@@ -85,6 +87,7 @@ public class HapClientProfileTest {
        mServiceListener.onServiceDisconnected(BluetoothProfile.HAP_CLIENT);

        assertThat(mProfile.isProfileReady()).isFalse();
        verify(mProfileManager).callServiceDisconnectedListeners();
    }

    @Test