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

Commit f860443c authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[SB][Scene] Start/stop tracking battery events separately from NPVC.

Bug: 300484307
Test: enable scene flags -> on lockscreen, verify battery percentage
shows while charging and doesn't show while not charging (unless setting
is enabled)
Test: atest KeyguardStatusBarViewModelTest
KeyguardStatusBarViewControllerTest

Change-Id: I459be774926034887dde4d2121030b2860d4572b
parent 278310e3
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -43,9 +43,9 @@ import android.widget.TextView;
import androidx.annotation.VisibleForTesting;

import com.android.settingslib.Utils;
import com.android.systemui.res.R;
import com.android.systemui.battery.BatteryMeterView;
import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.phone.SysuiDarkIconDispatcher.DarkChange;
import com.android.systemui.statusbar.phone.userswitcher.StatusBarUserSwitcherContainer;
import com.android.systemui.user.ui.binder.StatusBarUserChipViewBinder;
@@ -367,8 +367,11 @@ public class KeyguardStatusBarView extends RelativeLayout {
        mMultiUserAvatar.setImageDrawable(picture);
    }

    /** Should only be called from {@link KeyguardStatusBarViewController}. */
    void onBatteryLevelChanged(boolean charging) {
    /**
     * Should only be called from {@link KeyguardStatusBarViewController} or
     * {@link com.android.systemui.statusbar.ui.binder.KeyguardStatusBarViewBinder}.
     */
    public void onBatteryChargingChanged(boolean charging) {
        if (mBatteryCharging != charging) {
            mBatteryCharging = charging;
            updateVisibilities();
+5 −1
Original line number Diff line number Diff line
@@ -172,7 +172,7 @@ public class KeyguardStatusBarViewController extends ViewController<KeyguardStat
            new BatteryController.BatteryStateChangeCallback() {
                @Override
                public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
                    mView.onBatteryLevelChanged(charging);
                    mView.onBatteryChargingChanged(charging);
                }
            };

@@ -435,6 +435,10 @@ public class KeyguardStatusBarViewController extends ViewController<KeyguardStat

    /** Sets whether this controller should listen to battery updates. */
    public void setBatteryListening(boolean listening) {
        if (isMigrationEnabled()) {
            return;
        }

        if (listening == mBatteryListening) {
            return;
        }
+7 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import androidx.lifecycle.repeatOnLifecycle
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.statusbar.phone.KeyguardStatusBarView
import com.android.systemui.statusbar.ui.viewmodel.KeyguardStatusBarViewModel
import kotlinx.coroutines.launch

/** Binds [KeyguardStatusBarViewModel] to [KeyguardStatusBarView]. */
object KeyguardStatusBarViewBinder {
@@ -32,10 +33,14 @@ object KeyguardStatusBarViewBinder {
    ) {
        view.repeatWhenAttached {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.isVisible.collect { isVisible ->
                        view.visibility = if (isVisible) View.VISIBLE else View.INVISIBLE
                    }
                }

                launch { viewModel.isBatteryCharging.collect { view.onBatteryChargingChanged(it) } }
            }
        }
    }
}
+24 −0
Original line number Diff line number Diff line
@@ -16,12 +16,17 @@

package com.android.systemui.statusbar.ui.viewmodel

import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor
import com.android.systemui.keyguard.shared.model.StatusBarState
import com.android.systemui.statusbar.policy.BatteryController
import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
@@ -41,6 +46,7 @@ class KeyguardStatusBarViewModel
constructor(
    @Application scope: CoroutineScope,
    keyguardInteractor: KeyguardInteractor,
    batteryController: BatteryController,
) {
    /** True if this view should be visible and false otherwise. */
    val isVisible: StateFlow<Boolean> =
@@ -51,4 +57,22 @@ constructor(
                !isDozing && statusBarState == StatusBarState.KEYGUARD
            }
            .stateIn(scope, SharingStarted.WhileSubscribed(), false)

    /** True if the device's battery is currently charging and false otherwise. */
    // Note: Never make this an eagerly-started state flow so that the callback is removed when the
    // keyguard status bar view isn't attached.
    val isBatteryCharging: Flow<Boolean> = conflatedCallbackFlow {
        val callback =
            object : BatteryStateChangeCallback {
                override fun onBatteryLevelChanged(
                    level: Int,
                    pluggedIn: Boolean,
                    charging: Boolean,
                ) {
                    trySend(charging)
                }
            }
        batteryController.addCallback(callback)
        awaitClose { batteryController.removeCallback(callback) }
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -173,7 +173,8 @@ public class KeyguardStatusBarViewControllerTest extends SysuiTestCase {
        mViewModel =
                new KeyguardStatusBarViewModel(
                        mTestScope.getBackgroundScope(),
                        mKeyguardInteractor);
                        mKeyguardInteractor,
                        mBatteryController);

        allowTestableLooperAsMainThread();
        TestableLooper.get(this).runWithLooper(() -> {
@@ -316,6 +317,15 @@ public class KeyguardStatusBarViewControllerTest extends SysuiTestCase {
        verify(mBatteryController).addCallback(any());
    }

    @Test
    public void setBatteryListening_true_flagOn_callbackNotAdded() {
        mFeatureFlags.set(Flags.MIGRATE_KEYGUARD_STATUS_BAR_VIEW, true);

        mController.setBatteryListening(true);

        verify(mBatteryController, never()).addCallback(any());
    }

    @Test
    public void updateTopClipping_viewClippingUpdated() {
        int viewTop = 20;
Loading