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

Commit 82a15aaf authored by Alexey Patosin's avatar Alexey Patosin
Browse files

Disable system HfpClientConnectionService for wearables.

Bug: 300026493
Test: atest BluetoothInstrumentationTests
Change-Id: Iee82a38db62783f88a74252dca49da22e9b0a307
parent 2070c387
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.bluetooth.hfpclient;

import static android.content.pm.PackageManager.FEATURE_WATCH;

import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
@@ -85,6 +87,14 @@ public class HeadsetClientService extends ProfileService {

    public static final String HFP_CLIENT_STOP_TAG = "hfp_client_stop_tag";

    HeadsetClientService() {}

    @VisibleForTesting
    HeadsetClientService(Context ctx) {
        attachBaseContext(ctx);
        onCreate();
    }

    public static boolean isEnabled() {
        return BluetoothProperties.isProfileHfpHfEnabled().orElse(false);
    }
@@ -134,9 +144,12 @@ public class HeadsetClientService extends ProfileService {
            registerReceiver(mBroadcastReceiver, filter);

            // Start the HfpClientConnectionService to create connection with telecom when HFP
            // connection is available.
            // connection is available on non-wearable device.
            if (getPackageManager() != null
                    && !getPackageManager().hasSystemFeature(FEATURE_WATCH)) {
                Intent startIntent = new Intent(this, HfpClientConnectionService.class);
                startService(startIntent);
            }

            // Create the thread on which all State Machines will run
            mSmThread = new HandlerThread("HeadsetClient.SM");
@@ -156,10 +169,13 @@ public class HeadsetClientService extends ProfileService {
                    return false;
                }

                // Stop the HfpClientConnectionService.
                // Stop the HfpClientConnectionService for non-wearables devices.
                if (getPackageManager() != null
                        && !getPackageManager().hasSystemFeature(FEATURE_WATCH)) {
                    Intent stopIntent = new Intent(this, HfpClientConnectionService.class);
                    sHeadsetClientService.stopService(stopIntent);
                }
            }

            setHeadsetClientService(null);

+74 −13
Original line number Diff line number Diff line
@@ -16,11 +16,14 @@

package com.android.bluetooth.hfpclient;

import static android.content.pm.PackageManager.FEATURE_WATCH;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

@@ -29,6 +32,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSinkAudioPolicy;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.BatteryManager;

import androidx.test.InstrumentationRegistry;
@@ -49,6 +53,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

@MediumTest
@@ -78,18 +83,10 @@ public class HeadsetClientServiceTest {
        doReturn(mRemoteDevices).when(mAdapterService).getRemoteDevices();
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        NativeInterface.setInstance(mNativeInterface);
        TestUtils.startService(mServiceRule, HeadsetClientService.class);
        // At this point the service should have started so check NOT null
        mService = HeadsetClientService.getHeadsetClientService();
        Assert.assertNotNull(mService);
        // Try getting the Bluetooth adapter
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        Assert.assertNotNull(mAdapter);
    }

    @After
    public void tearDown() throws Exception {
        TestUtils.stopService(mServiceRule, HeadsetClientService.class);
        NativeInterface.setInstance(null);
        mService = HeadsetClientService.getHeadsetClientService();
        Assert.assertNull(mService);
@@ -97,13 +94,17 @@ public class HeadsetClientServiceTest {
    }

    @Test
    public void testInitialize() {
    public void testInitialize() throws Exception {
        startService();
        Assert.assertNotNull(HeadsetClientService.getHeadsetClientService());
        stopService();
    }

    @Ignore("b/260202548")
    @Test
    public void testSendBIEVtoStateMachineWhenBatteryChanged() {
    public void testSendBIEVtoStateMachineWhenBatteryChanged() throws Exception {
        startService();

        // Put mock state machine
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:01:02:03:04:05");
@@ -120,10 +121,14 @@ public class HeadsetClientServiceTest {
                    eq(HeadsetClientStateMachine.SEND_BIEV),
                    eq(2),
                    anyInt());

        stopService();
    }

    @Test
    public void testUpdateBatteryLevel() {
    public void testUpdateBatteryLevel() throws Exception {
        startService();

        // Put mock state machine
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:01:02:03:04:05");
@@ -137,10 +142,14 @@ public class HeadsetClientServiceTest {
                    eq(HeadsetClientStateMachine.SEND_BIEV),
                    eq(2),
                    anyInt());

        stopService();
    }

    @Test
    public void testSetCallAudioPolicy() {
    public void testSetCallAudioPolicy() throws Exception {
        startService();

        // Put mock state machine
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:01:02:03:04:05");
@@ -150,15 +159,67 @@ public class HeadsetClientServiceTest {

        verify(mStateMachine, timeout(STANDARD_WAIT_MILLIS).times(1))
                .setAudioPolicy(any(BluetoothSinkAudioPolicy.class));

        stopService();
    }

    @Test
    public void testDumpDoesNotCrash() {
    public void testDumpDoesNotCrash() throws Exception {
        startService();

        // Put mock state machine
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:01:02:03:04:05");
        mService.getStateMachineMap().put(device, mStateMachine);

        mService.dump(new StringBuilder());

        stopService();
    }

    @Test
    public void testHfpClientConnectionServiceStarted() throws Exception {
        Context context = Mockito.mock(Context.class);
        PackageManager packageManager = Mockito.mock(PackageManager.class);

        doReturn(false).when(packageManager).hasSystemFeature(FEATURE_WATCH);
        doReturn(packageManager).when(context).getPackageManager();

        HeadsetClientService service = new HeadsetClientService(context);
        service.doStart();

        verify(context).startService(any(Intent.class));

        service.doStop();
    }

    @Test
    public void testHfpClientConnectionServiceNotStarted_wearable() throws Exception {
        Context context = Mockito.mock(Context.class);
        PackageManager packageManager = Mockito.mock(PackageManager.class);

        doReturn(true).when(packageManager).hasSystemFeature(FEATURE_WATCH);
        doReturn(packageManager).when(context).getPackageManager();

        HeadsetClientService service = new HeadsetClientService(context);
        service.doStart();

        verify(context, never()).startService(any(Intent.class));

        service.doStop();
    }

    private void startService() throws Exception {
        TestUtils.startService(mServiceRule, HeadsetClientService.class);
        // At this point the service should have started so check NOT null
        mService = HeadsetClientService.getHeadsetClientService();
        Assert.assertNotNull(mService);
        // Try getting the Bluetooth adapter
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        Assert.assertNotNull(mAdapter);
    }

    private void stopService() throws Exception {
        TestUtils.stopService(mServiceRule, HeadsetClientService.class);
    }
}