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

Commit 2041db09 authored by Angela Wang's avatar Angela Wang
Browse files

Refactor the timing to start LocalDataManager

In b/403752469, we submited a workaround to prevent crashes. The workaround starts LocalDataManager in the foreground when the AmbientVolumeUiController is created but it's better to do so in the background when the AmbientVolumeUiController.start() is called.

Prevent calling refresh() when LocalDataManager doesn't start yet in
loadDevices() and do the first refresh() once it starts.

Flag: EXEMPT bugfix
Bug: 417378166
Test: atest AmbientVolumeUiControllerTest
Test: atest HearingDevicesDialogDelegateTest
Change-Id: If6ce4ac11bb0a618876c940caa0e407641e80b8e
parent b388fc17
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ public class AmbientVolumeUiController implements
    private final Set<Integer> mRangeInitializedSliderSides = new ArraySet<>();
    private CachedBluetoothDevice mCachedDevice;
    private boolean mShowUiWhenLocalDataExist = true;
    private boolean mStarted = false;

    public AmbientVolumeUiController(@NonNull Context context,
            @NonNull LocalBluetoothManager bluetoothManager,
@@ -82,7 +83,6 @@ public class AmbientVolumeUiController implements
        mLocalDataManager = new HearingDeviceLocalDataManager(context);
        mLocalDataManager.setOnDeviceLocalDataChangeListener(this,
                ThreadUtils.getBackgroundExecutor());
        mLocalDataManager.start();
    }

    @VisibleForTesting
@@ -278,6 +278,10 @@ public class AmbientVolumeUiController implements
     * events.
     */
    public void start() {
        if (mStarted) {
            return;
        }
        mStarted = true;
        mEventManager.registerCallback(this);
        mLocalDataManager.start();
        mCachedDevices.forEach(device -> {
@@ -285,6 +289,7 @@ public class AmbientVolumeUiController implements
            mVolumeController.registerCallback(ThreadUtils.getBackgroundExecutor(),
                    device.getDevice());
        });
        postOnMainThread(this::refresh);
    }

    /**
@@ -292,6 +297,10 @@ public class AmbientVolumeUiController implements
     * events.
     */
    public void stop() {
        if (!mStarted) {
            return;
        }
        mStarted = false;
        mEventManager.unregisterCallback(this);
        mLocalDataManager.stop();
        mCachedDevices.forEach(device -> {
@@ -337,8 +346,10 @@ public class AmbientVolumeUiController implements

        mAmbientLayout.setControlExpandable(mSideToDeviceMap.size() >  1);
        mAmbientLayout.setupSliders(mSideToDeviceMap);
        if (mStarted) {
            refresh();
        }
    }

    /** Refreshes the ambient volume UI. */
    public void refresh() {
+6 −2
Original line number Diff line number Diff line
@@ -157,21 +157,23 @@ public class AmbientVolumeUiControllerTest {
    }

    @Test
    public void loadDevice_ambientControlNotAvailable_ambientLayoutGone() {
    public void refresh_ambientControlNotAvailable_ambientLayoutGone() {
        when(mVolumeController.isAmbientControlAvailable(mDevice)).thenReturn(false);
        when(mVolumeController.isAmbientControlAvailable(mMemberDevice)).thenReturn(false);

        mController.loadDevice(mCachedDevice);
        mController.refresh();

        verify(mAmbientLayout).setVisible(false);
    }

    @Test
    public void loadDevice_supportVcpAndAmbientControlAvailable_ambientLayoutVisible() {
    public void refresh_supportVcpAndAmbientControlAvailable_ambientLayoutVisible() {
        when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile));
        when(mVolumeController.isAmbientControlAvailable(mDevice)).thenReturn(true);

        mController.loadDevice(mCachedDevice);
        mController.refresh();

        verify(mAmbientLayout).setVisible(true);
    }
@@ -188,10 +190,12 @@ public class AmbientVolumeUiControllerTest {
                any(CachedBluetoothDevice.Callback.class));
        verify(mCachedMemberDevice).registerCallback(any(Executor.class),
                any(CachedBluetoothDevice.Callback.class));
        verify(mController).refresh();
    }

    @Test
    public void stop_callbackUnregistered() {
        mController.start();
        mController.stop();

        verify(mEventManager).unregisterCallback(mController);
+18 −20
Original line number Diff line number Diff line
@@ -158,9 +158,11 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    private SystemUIDialog mDialog;
    private SystemUIDialog.Factory mDialogFactory;
    private HearingDevicesDialogDelegate mDialogDelegate;
    private TestableLooper mTestableLooper;

    @Before
    public void setUp() {
        mTestableLooper = TestableLooper.get(this);
        when(mLocalBluetoothManager.getBluetoothAdapter()).thenReturn(mLocalBluetoothAdapter);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
        when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile);
@@ -195,8 +197,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    @Test
    public void clickPairNewDeviceButton_intentActionMatch() {
        setUpDeviceDialogWithPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        getPairNewDeviceButton(mDialog).performClick();

@@ -244,8 +245,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
                R.array.config_quickSettingsHearingDevicesRelatedToolName, new String[]{});

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        assertToolsUi(0);
    }
@@ -259,8 +259,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
                R.array.config_quickSettingsHearingDevicesRelatedToolName, new String[]{});

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        assertToolsUi(1);
    }
@@ -281,8 +280,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mDrawable.mutate()).thenReturn(mDrawable);

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        assertToolsUi(2);
    }
@@ -293,8 +291,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(PRESET_INDEX_UNAVAILABLE);

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.GONE);
@@ -307,8 +304,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_PRESET_INDEX);

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.VISIBLE);
@@ -322,8 +318,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mCachedDevice.getProfiles()).thenReturn(List.of());

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        ViewGroup ambientLayout = getAmbientLayout(mDialog);
        assertThat(ambientLayout.getVisibility()).isEqualTo(View.GONE);
@@ -335,8 +330,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mVolumeControlProfile.getAudioInputControlServices(mDevice)).thenReturn(List.of());

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        ViewGroup ambientLayout = getAmbientLayout(mDialog);
        assertThat(ambientLayout.getVisibility()).isEqualTo(View.GONE);
@@ -352,8 +346,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mVolumeControlProfile.getConnectionStatus(mDevice)).thenReturn(STATE_CONNECTED);

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();

        ViewGroup ambientLayout = getAmbientLayout(mDialog);
        assertThat(ambientLayout.getVisibility()).isEqualTo(View.VISIBLE);
@@ -362,8 +355,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    @Test
    public void onActiveDeviceChanged_presetExist_presetSelected() {
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        showDialogAndProcessAllTasks();
        BluetoothHapPresetInfo info = getTestPresetInfo();
        when(mHapClientProfile.getAllPresetInfo(mDevice)).thenReturn(List.of(info));
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_PRESET_INDEX);
@@ -473,6 +465,12 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        return audioInputControl;
    }

    private void showDialogAndProcessAllTasks() {
        mDialog.show();
        mExecutor.runAllReady();
        mTestableLooper.processAllMessages();
    }

    @After
    public void reset() {
        if (mDialogDelegate != null) {