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

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

Hap: merge Start into constructor

Bug: 311772251
Test: atest HapClientTests
Flag: Exempt refactor
Change-Id: Id1fddef6250693f0c2c3bb71a39dbaaba7cc7c83
parent 7c214d76
Loading
Loading
Loading
Loading
+15 −41
Original line number Diff line number Diff line
@@ -77,6 +77,9 @@ public class HapClientService extends ProfileService {
    private final Map<BluetoothDevice, Integer> mDeviceFeaturesMap = new HashMap<>();
    private final Map<BluetoothDevice, List<BluetoothHapPresetInfo>> mPresetsMap = new HashMap<>();
    private final AdapterService mAdapterService;
    private final DatabaseManager mDatabaseManager;
    private final Handler mHandler;
    private final HandlerThread mStateMachinesThread;
    private final HapClientNativeInterface mHapClientNativeInterface;

    @VisibleForTesting
@@ -85,10 +88,6 @@ public class HapClientService extends ProfileService {

    @VisibleForTesting ServiceFactory mFactory = new ServiceFactory();

    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
    private Handler mHandler;

    public static boolean isEnabled() {
        return BluetoothProperties.isProfileHapClientEnabled().orElse(false);
    }
@@ -126,31 +125,7 @@ public class HapClientService extends ProfileService {
        super(adapterService);
        mAdapterService = requireNonNull(adapterService);
        mHapClientNativeInterface = requireNonNull(nativeInterface);
    }

    @Override
    public void cleanup() {
        Log.d(TAG, "cleanup()");
    }

    @Override
    protected IProfileServiceBinder initBinder() {
        return new BluetoothHapClientBinder(this);
    }

    @Override
    public void start() {
        Log.d(TAG, "start()");

        if (sHapClient != null) {
            throw new IllegalStateException("start() called twice");
        }

        // Get DatabaseManager
        mDatabaseManager =
                requireNonNull(
                        mAdapterService.getDatabase(),
                        "DatabaseManager cannot be null when HapClientService starts");
        mDatabaseManager = requireNonNull(mAdapterService.getDatabase());

        // Start handler thread for state machines
        mHandler = new Handler(Looper.getMainLooper());
@@ -165,6 +140,11 @@ public class HapClientService extends ProfileService {
        setHapClient(this);
    }

    @Override
    protected IProfileServiceBinder initBinder() {
        return new BluetoothHapClientBinder(this);
    }

    @Override
    public void stop() {
        Log.d(TAG, "stop()");
@@ -185,21 +165,15 @@ public class HapClientService extends ProfileService {
            mStateMachines.clear();
        }

        if (mStateMachinesThread != null) {
        try {
            mStateMachinesThread.quitSafely();
            mStateMachinesThread.join(SM_THREAD_JOIN_TIMEOUT_MS);
                mStateMachinesThread = null;
        } catch (InterruptedException e) {
            // Do not rethrow as we are shutting down anyway
        }
        }

        // Unregister Handler and stop all queued messages.
        if (mHandler != null) {
        mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }

        // Cleanup GATT interface
        mHapClientNativeInterface.cleanup();
+0 −9
Original line number Diff line number Diff line
@@ -226,15 +226,6 @@ public class HapClientTest {
        Assert.assertEquals(mService, HapClientService.getHapClientService());
    }

    /** Test stop HA Service Client */
    @Test
    public void testStopHapService() {
        Assert.assertEquals(mService, HapClientService.getHapClientService());

        InstrumentationRegistry.getInstrumentation().runOnMainSync(mService::stop);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(mService::start);
    }

    /** Test get/set policy for BluetoothDevice */
    @Test
    public void testGetSetPolicy() throws Exception {