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

Commit 92ccaa32 authored by William Escande's avatar William Escande
Browse files

AdapterService: Move variable to final

These field can be initialized at constructor:
* Looper
* handler
* DatabaseManager
* SilenceDeviceManager

We could probably extend this list in the futur.
But at the moment this is the bar minimal require for passing test when
we enabled fastBindToApp flag

Bug: 323672160
Test: atest BluetoothInstrumentationTests with & without fastBindToApp
Change-Id: I50c2b15c89586cdc15f4d783d249f3cc830b37b4
parent 24f35cb3
Loading
Loading
Loading
Loading
+17 −31
Original line number Diff line number Diff line
@@ -250,6 +250,9 @@ public class AdapterService extends Service {

    private final DeviceConfigListener mDeviceConfigListener = new DeviceConfigListener();

    private final Looper mLooper;
    private final AdapterServiceHandler mHandler;

    private int mStackReportedState;
    private long mTxTimeTotalMs;
    private long mRxTimeTotalMs;
@@ -282,8 +285,8 @@ public class AdapterService extends Service {
    @Nullable private PhonePolicy mPhonePolicy;

    private ActiveDeviceManager mActiveDeviceManager;
    private DatabaseManager mDatabaseManager;
    private SilenceDeviceManager mSilenceDeviceManager;
    private final DatabaseManager mDatabaseManager;
    private final SilenceDeviceManager mSilenceDeviceManager;
    private CompanionManager mBtCompanionManager;
    private AppOpsManager mAppOps;

@@ -316,8 +319,6 @@ public class AdapterService extends Service {
    private volatile boolean mTestModeEnabled = false;

    private MetricsLogger mMetricsLogger;
    private Looper mLooper;
    private AdapterServiceHandler mHandler;

    /** Handlers for incoming service calls */
    private AdapterServiceBinder mBinder;
@@ -355,15 +356,21 @@ public class AdapterService extends Service {
    }

    // Keep a constructor for ActivityThread.handleCreateService
    AdapterService() {}
    AdapterService() {
        this(Looper.getMainLooper());
    }

    @VisibleForTesting
    AdapterService(Looper looper) {
        mLooper = looper;
        mLooper = requireNonNull(looper);
        mHandler = new AdapterServiceHandler(mLooper);
        mSilenceDeviceManager = new SilenceDeviceManager(this, new ServiceFactory(), mLooper);
        mDatabaseManager = new DatabaseManager(this);
    }

    @VisibleForTesting
    AdapterService(Context ctx) {
        this(Looper.getMainLooper());
        attachBaseContext(ctx);
    }

@@ -615,10 +622,6 @@ public class AdapterService extends Service {
            return;
        }
        // OnCreate must perform the minimum of infaillible and mandatory initialization
        if (mLooper == null) {
            mLooper = Looper.getMainLooper();
        }
        mHandler = new AdapterServiceHandler(mLooper);
        mAdapterProperties = new AdapterProperties(this);
        mAdapterStateMachine = new AdapterState(this, mLooper);
        mBinder = new AdapterServiceBinder(this);
@@ -632,13 +635,6 @@ public class AdapterService extends Service {
    private void init() {
        debugLog("init()");
        Config.init(this);
        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            if (mLooper == null) {
                mLooper = Looper.getMainLooper();
            }
            mHandler = new AdapterServiceHandler(mLooper);
        }
        initMetricsLogger();
        mDeviceConfigListener.start();

@@ -705,7 +701,6 @@ public class AdapterService extends Service {
            mSdpManager = new SdpManager(this);
        }

        mDatabaseManager = new DatabaseManager(this);
        mDatabaseManager.start(MetadataDatabase.createDatabase(this));

        boolean isAutomotiveDevice =
@@ -734,7 +729,6 @@ public class AdapterService extends Service {
        }
        mActiveDeviceManager.start();

        mSilenceDeviceManager = new SilenceDeviceManager(this, new ServiceFactory(), mLooper);
        mSilenceDeviceManager.start();

        mBtCompanionManager = new CompanionManager(this, new ServiceFactory());
@@ -1358,9 +1352,7 @@ public class AdapterService extends Service {
            }
        }

        if (mDatabaseManager != null) {
        mDatabaseManager.cleanup();
        }

        if (mAdapterStateMachine != null) {
            mAdapterStateMachine.doQuit();
@@ -1402,9 +1394,7 @@ public class AdapterService extends Service {
            mPhonePolicy.cleanup();
        }

        if (mSilenceDeviceManager != null) {
        mSilenceDeviceManager.cleanup();
        }

        if (mActiveDeviceManager != null) {
            mActiveDeviceManager.cleanup();
@@ -5687,9 +5677,7 @@ public class AdapterService extends Service {

    @VisibleForTesting
    boolean factoryReset() {
        if (mDatabaseManager != null) {
        mDatabaseManager.factoryReset();
        }

        if (mBluetoothKeystoreService != null) {
            mBluetoothKeystoreService.factoryReset();
@@ -5935,10 +5923,8 @@ public class AdapterService extends Service {
        if (mCsipSetCoordinatorService != null && mCsipSetCoordinatorService.isAvailable()) {
            mCsipSetCoordinatorService.handleBondStateChanged(device, fromState, toState);
        }
        if (mDatabaseManager != null) {
        mDatabaseManager.handleBondStateChanged(device, fromState, toState);
    }
    }

    static int convertScanModeToHal(int mode) {
        switch (mode) {
+27 −18
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.Process;
@@ -115,7 +116,21 @@ public class AdapterServiceTest {
    private static final int MESSAGE_PROFILE_SERVICE_REGISTERED = 2;
    private static final int MESSAGE_PROFILE_SERVICE_UNREGISTERED = 3;

    private AdapterService mAdapterService;
    private MockAdapterService mAdapterService;

    static class MockAdapterService extends AdapterService {

        int mSetProfileServiceStateCounter = 0;

        MockAdapterService(Looper looper) {
            super(looper);
        }

        @Override
        void setProfileServiceState(int profileId, int state) {
            mSetProfileServiceStateCounter++;
        }
    }

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

@@ -208,12 +223,10 @@ public class AdapterServiceTest {
        ScanNativeInterface.setInstance(mScanNativeInterface);

        // Post the creation of AdapterService since it rely on Looper.myLooper()
        handler.post(() -> mAdapterService = spy(new AdapterService(mLooper.getLooper())));
        handler.post(() -> mAdapterService = new MockAdapterService(mLooper.getLooper()));
        assertThat(mLooper.dispatchAll()).isEqualTo(1);
        assertThat(mAdapterService).isNotNull();

        doNothing().when(mAdapterService).setProfileServiceState(anyInt(), anyInt());

        mMockPackageManager = mock(PackageManager.class);
        when(mMockPackageManager.getPermissionInfo(any(), anyInt()))
                .thenReturn(new PermissionInfo());
@@ -406,7 +419,7 @@ public class AdapterServiceTest {

    static void onToBleOn(
            TestLooper looper,
            AdapterService adapter,
            MockAdapterService adapter,
            Context ctx,
            IBluetoothCallback callback,
            boolean onlyGatt,
@@ -417,7 +430,7 @@ public class AdapterServiceTest {

        if (!onlyGatt) {
            // Stop PBAP and PAN services
            verify(adapter, times(4)).setProfileServiceState(anyInt(), anyInt());
            assertThat(adapter.mSetProfileServiceStateCounter).isEqualTo(4);

            for (ProfileService service : services) {
                adapter.onProfileServiceStateChanged(service, STATE_OFF);
@@ -441,11 +454,12 @@ public class AdapterServiceTest {
                List.of(mMockService, mMockService2),
                mNativeInterface);
    }

    // Method is re-used in other AdapterService*Test
    static void doEnable(
            TestLooper looper,
            ProfileService gattService,
            AdapterService adapter,
            MockAdapterService adapter,
            Context ctx,
            boolean onlyGatt,
            List<ProfileService> services,
@@ -467,7 +481,7 @@ public class AdapterServiceTest {

        if (!onlyGatt) {
            // Start Mock PBAP and PAN services
            verify(adapter, times(2)).setProfileServiceState(anyInt(), anyInt());
            assertThat(adapter.mSetProfileServiceStateCounter).isEqualTo(2);

            for (ProfileService service : services) {
                adapter.addProfile(service);
@@ -502,7 +516,7 @@ public class AdapterServiceTest {
    private static void doDisable(
            TestLooper looper,
            ProfileService gattService,
            AdapterService adapter,
            MockAdapterService adapter,
            Context ctx,
            boolean onlyGatt,
            List<ProfileService> services,
@@ -674,8 +688,7 @@ public class AdapterServiceTest {
        mAdapterService.startBrEdr();
        syncHandler(AdapterState.USER_TURN_ON);
        verifyStateChange(STATE_BLE_ON, STATE_TURNING_ON);
        verify(mAdapterService, times(2))
                .setProfileServiceState(anyInt(), anyInt()); // Register Mock PBAP and PAN services
        assertThat(mAdapterService.mSetProfileServiceStateCounter).isEqualTo(2);

        mAdapterService.addProfile(mMockService);
        syncHandler(MESSAGE_PROFILE_SERVICE_REGISTERED);
@@ -690,8 +703,7 @@ public class AdapterServiceTest {
        syncHandler(AdapterState.BREDR_START_TIMEOUT);

        verifyStateChange(STATE_TURNING_ON, STATE_TURNING_OFF);
        verify(mAdapterService, times(4))
                .setProfileServiceState(anyInt(), anyInt()); // Stop PBAP and PAN services
        assertThat(mAdapterService.mSetProfileServiceStateCounter).isEqualTo(4);

        mAdapterService.onProfileServiceStateChanged(mMockService, STATE_OFF);
        syncHandler(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);
@@ -702,10 +714,7 @@ public class AdapterServiceTest {
        assertThat(mAdapterService.getBluetoothGatt()).isNotNull();
    }

    /**
     * Test: Don't stop a classic profile
     * Check whether the AdapterService quits gracefully
     */
    /** Test: Don't stop a classic profile Check whether the AdapterService quits gracefully */
    @Test
    public void testProfileStopTimeout() {
        doEnable(false);
@@ -713,7 +722,7 @@ public class AdapterServiceTest {
        mAdapterService.disable();
        syncHandler(AdapterState.USER_TURN_OFF);
        verifyStateChange(STATE_ON, STATE_TURNING_OFF);
        verify(mAdapterService, times(4)).setProfileServiceState(anyInt(), anyInt());
        assertThat(mAdapterService.mSetProfileServiceStateCounter).isEqualTo(4);

        mAdapterService.onProfileServiceStateChanged(mMockService, STATE_OFF);
        syncHandler(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);