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

Commit 53aa2a3a authored by Anton Potapov's avatar Anton Potapov
Browse files

Precalculate lazy fields on component initialization.

This doesn't actually update the logic, because the component is a
singleton and gets the feature in the constructor. So the value would
never change at the run time. But the creation of the controllers in the
constructor would limit the concurrency to the component creation.

Test: manual: open Device Controls with enabled feature
Test: manual: run the device with disabled feature
Test: atest ControlsComponentTest
Bug: 295495878
Change-Id: I209e799294d3fce4a97650e0074b1c726b8630d5
parent 0edf204a
Loading
Loading
Loading
Loading
+21 −18
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.systemui.controls.dagger

import android.content.Context
import com.android.internal.widget.LockPatternUtils
import com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT
import com.android.systemui.controls.controller.ControlsController
@@ -44,10 +43,9 @@ class ControlsComponent
@Inject
constructor(
    @ControlsFeatureEnabled private val featureEnabled: Boolean,
    private val context: Context,
    private val lazyControlsController: Lazy<ControlsController>,
    private val lazyControlsUiController: Lazy<ControlsUiController>,
    private val lazyControlsListingController: Lazy<ControlsListingController>,
    lazyControlsController: Lazy<ControlsController>,
    lazyControlsUiController: Lazy<ControlsUiController>,
    lazyControlsListingController: Lazy<ControlsListingController>,
    private val lockPatternUtils: LockPatternUtils,
    private val keyguardStateController: KeyguardStateController,
    private val userTracker: UserTracker,
@@ -55,27 +53,25 @@ constructor(
    optionalControlsTileResourceConfiguration: Optional<ControlsTileResourceConfiguration>
) {

    private val controlsController: Optional<ControlsController> =
        optionalIf(isEnabled(), lazyControlsController)
    private val controlsUiController: Optional<ControlsUiController> =
        optionalIf(isEnabled(), lazyControlsUiController)
    private val controlsListingController: Optional<ControlsListingController> =
        optionalIf(isEnabled(), lazyControlsListingController)

    val canShowWhileLockedSetting: StateFlow<Boolean> =
        controlsSettingsRepository.canShowControlsInLockscreen

    private val controlsTileResourceConfiguration: ControlsTileResourceConfiguration =
        optionalControlsTileResourceConfiguration.orElse(ControlsTileResourceConfigurationImpl())

    fun getControlsController(): Optional<ControlsController> {
        return if (featureEnabled) Optional.of(lazyControlsController.get()) else Optional.empty()
    }
    fun getControlsController(): Optional<ControlsController> = controlsController

    fun getControlsUiController(): Optional<ControlsUiController> {
        return if (featureEnabled) Optional.of(lazyControlsUiController.get()) else Optional.empty()
    }
    fun getControlsUiController(): Optional<ControlsUiController> = controlsUiController

    fun getControlsListingController(): Optional<ControlsListingController> {
        return if (featureEnabled) {
            Optional.of(lazyControlsListingController.get())
        } else {
            Optional.empty()
        }
    }
    fun getControlsListingController(): Optional<ControlsListingController> =
        controlsListingController

    /** @return true if controls are feature-enabled and the user has the setting enabled */
    fun isEnabled() = featureEnabled
@@ -118,4 +114,11 @@ constructor(
    fun getTileImageId(): Int {
        return controlsTileResourceConfiguration.getTileImageId()
    }

    private fun <T : Any> optionalIf(condition: Boolean, provider: Lazy<T>): Optional<T> =
        if (condition) {
            Optional.of(provider.get())
        } else {
            Optional.empty()
        }
}
+3 −5
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import com.android.systemui.controls.settings.FakeControlsSettingsRepository
import com.android.systemui.controls.ui.ControlsUiController
import com.android.systemui.settings.UserTracker
import com.android.systemui.statusbar.policy.KeyguardStateController
import dagger.Lazy
import java.util.Optional
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
@@ -173,10 +172,9 @@ class ControlsComponentTest : SysuiTestCase() {
    private fun setupComponent(enabled: Boolean): ControlsComponent {
        return ControlsComponent(
            enabled,
            mContext,
            Lazy { controller },
            Lazy { uiController },
            Lazy { listingController },
            { controller },
            { uiController },
            { listingController },
            lockPatternUtils,
            keyguardStateController,
            userTracker,