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

Commit fba6a29e authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "blackflash" into udc-qpr-dev

* changes:
  Reuse ClockViewFactory
  Cache affordances and selection picker side
  Use BaseFlags for quickAffordances
parents 25eb3809 28ad798b
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.customization.module

import android.app.Activity
import android.app.UiModeManager
import android.app.WallpaperManager
import android.content.Context
@@ -111,7 +112,7 @@ internal constructor(
    private var clockPickerInteractor: ClockPickerInteractor? = null
    private var clockSectionViewModel: ClockSectionViewModel? = null
    private var clockCarouselViewModelFactory: ClockCarouselViewModel.Factory? = null
    private var clockViewFactories: MutableMap<Int, ClockViewFactory> = HashMap()
    private var clockViewFactory: ClockViewFactory? = null
    private var clockPickerSnapshotRestorer: ClockPickerSnapshotRestorer? = null
    private var notificationsInteractor: NotificationsInteractor? = null
    private var notificationSectionViewModelFactory: NotificationSectionViewModel.Factory? = null
@@ -267,7 +268,12 @@ internal constructor(
        val client = getKeyguardQuickAffordancePickerProviderClient(context)
        val appContext = context.applicationContext
        return KeyguardQuickAffordancePickerInteractor(
            KeyguardQuickAffordancePickerRepository(client, bgDispatcher),
            KeyguardQuickAffordancePickerRepository(
                client,
                getApplicationCoroutineScope(),
                getFlags(),
                appContext
            ),
            client
        ) {
            getKeyguardQuickAffordanceSnapshotRestorer(appContext)
@@ -383,8 +389,7 @@ internal constructor(
    }

    override fun getClockViewFactory(activity: ComponentActivity): ClockViewFactory {
        val activityHashCode = activity.hashCode()
        return clockViewFactories[activityHashCode]
        return clockViewFactory
            ?: ClockViewFactory(
                    activity.applicationContext,
                    ScreenSizeCalculator.getInstance()
@@ -393,13 +398,13 @@ internal constructor(
                    getClockRegistry(activity.applicationContext),
                )
                .also {
                    clockViewFactories[activityHashCode] = it
                    clockViewFactory = it
                    activity.lifecycle.addObserver(
                        object : DefaultLifecycleObserver {
                            override fun onDestroy(owner: LifecycleOwner) {
                                super.onDestroy(owner)
                                clockViewFactories[activityHashCode]?.onDestroy()
                                clockViewFactories.remove(activityHashCode)
                                if ((owner as Activity).isChangingConfigurations()) return
                                clockViewFactory?.onDestroy()
                            }
                        }
                    )
+19 −24
Original line number Diff line number Diff line
@@ -17,15 +17,17 @@

package com.android.customization.picker.quickaffordance.data.repository

import android.content.Context
import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerAffordanceModel as AffordanceModel
import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel
import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel
import com.android.systemui.shared.customization.data.content.CustomizationProviderClient as Client
import com.android.systemui.shared.customization.data.content.CustomizationProviderContract as Contract
import kotlinx.coroutines.CoroutineDispatcher
import com.android.wallpaper.config.BaseFlags
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import kotlinx.coroutines.flow.shareIn

/**
 * Abstracts access to application state related to functionality for selecting, picking, or setting
@@ -33,11 +35,14 @@ import kotlinx.coroutines.withContext
 */
class KeyguardQuickAffordancePickerRepository(
    private val client: Client,
    private val backgroundDispatcher: CoroutineDispatcher,
    private val scope: CoroutineScope,
    private val flags: BaseFlags,
    private val context: Context
) {
    /** Whether the feature is enabled. */
    val isFeatureEnabled: Flow<Boolean> =
        client.observeFlags().map { flags -> flags.isFeatureEnabled() }
    fun isFeatureEnabled(): Boolean {
        return flags.isQuickAffordancesEnabled(context)
    }

    /** List of slots available on the device. */
    val slots: Flow<List<SlotModel>> =
@@ -45,27 +50,17 @@ class KeyguardQuickAffordancePickerRepository(

    /** List of all available quick affordances. */
    val affordances: Flow<List<AffordanceModel>> =
        client.observeAffordances().map { affordances ->
            affordances.map { affordance -> affordance.toModel() }
        }
        client
            .observeAffordances()
            .map { affordances -> affordances.map { affordance -> affordance.toModel() } }
            .shareIn(scope, replay = 1, started = SharingStarted.Lazily)

    /** List of slot-affordance pairs, modeling what the user has currently chosen for each slot. */
    val selections: Flow<List<SelectionModel>> =
        client.observeSelections().map { selections ->
            selections.map { selection -> selection.toModel() }
        }

    suspend fun isFeatureEnabled(): Boolean {
        return withContext(backgroundDispatcher) { client.queryFlags().isFeatureEnabled() }
    }

    private fun List<Client.Flag>.isFeatureEnabled(): Boolean {
        return find { flag ->
                flag.name ==
                    Contract.FlagsTable.FLAG_NAME_CUSTOM_LOCK_SCREEN_QUICK_AFFORDANCES_ENABLED
            }
            ?.value == true
    }
        client
            .observeSelections()
            .map { selections -> selections.map { selection -> selection.toModel() } }
            .shareIn(scope, replay = 1, started = SharingStarted.Lazily)

    private fun Client.Slot.toModel(): SlotModel {
        return SlotModel(
+19 −35
Original line number Diff line number Diff line
@@ -17,19 +17,20 @@

package com.android.customization.model.picker.quickaffordance.data.repository

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.SmallTest
import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
import com.android.systemui.shared.customization.data.content.CustomizationProviderContract
import com.android.systemui.shared.customization.data.content.CustomizationProviderClient
import com.android.systemui.shared.customization.data.content.FakeCustomizationProviderClient
import com.android.wallpaper.config.BaseFlags
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
@@ -57,7 +58,16 @@ class KeyguardQuickAffordancePickerRepositoryTest {
        underTest =
            KeyguardQuickAffordancePickerRepository(
                client = client,
                backgroundDispatcher = coroutineDispatcher,
                scope = testScope.backgroundScope,
                flags =
                    object : BaseFlags() {
                        override fun getCachedFlags(
                            context: Context
                        ): List<CustomizationProviderClient.Flag> {
                            return runBlocking { client.queryFlags() }
                        }
                    },
                context = ApplicationProvider.getApplicationContext()
            )
    }

@@ -66,35 +76,9 @@ class KeyguardQuickAffordancePickerRepositoryTest {
        Dispatchers.resetMain()
    }

    // We need at least one test to prevent Studio errors
    @Test
    fun `isFeatureEnabled - enabled`() =
        testScope.runTest {
            client.setFlag(
                CustomizationProviderContract.FlagsTable
                    .FLAG_NAME_CUSTOM_LOCK_SCREEN_QUICK_AFFORDANCES_ENABLED,
                true,
            )
            val values = mutableListOf<Boolean>()
            val job = launch { underTest.isFeatureEnabled.toList(values) }

            assertThat(values.last()).isTrue()

            job.cancel()
        }

    @Test
    fun `isFeatureEnabled - not enabled`() =
        testScope.runTest {
            client.setFlag(
                CustomizationProviderContract.FlagsTable
                    .FLAG_NAME_CUSTOM_LOCK_SCREEN_QUICK_AFFORDANCES_ENABLED,
                false,
            )
            val values = mutableListOf<Boolean>()
            val job = launch { underTest.isFeatureEnabled.toList(values) }

            assertThat(values.last()).isFalse()

            job.cancel()
    fun creationSucceeds() {
        assertThat(underTest).isNotNull()
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package com.android.customization.model.picker.quickaffordance.domain.interactor

import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.SmallTest
import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
import com.android.customization.picker.quickaffordance.domain.interactor.KeyguardQuickAffordancePickerInteractor
@@ -24,6 +25,7 @@ import com.android.customization.picker.quickaffordance.domain.interactor.Keygua
import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel
import com.android.systemui.shared.customization.data.content.FakeCustomizationProviderClient
import com.android.systemui.shared.keyguard.shared.model.KeyguardQuickAffordanceSlots
import com.android.wallpaper.config.BaseFlags
import com.android.wallpaper.testing.FakeSnapshotStore
import com.android.wallpaper.testing.collectLastValue
import com.google.common.truth.Truth.assertThat
@@ -57,12 +59,15 @@ class KeyguardQuickAffordancePickerInteractorTest {
        testScope = TestScope(testDispatcher)
        Dispatchers.setMain(testDispatcher)
        client = FakeCustomizationProviderClient()
        val testFlags = object : BaseFlags() {}
        underTest =
            KeyguardQuickAffordancePickerInteractor(
                repository =
                    KeyguardQuickAffordancePickerRepository(
                        client = client,
                        backgroundDispatcher = testDispatcher,
                        scope = testScope.backgroundScope,
                        flags = testFlags,
                        context = ApplicationProvider.getApplicationContext(),
                    ),
                client = client,
                snapshotRestorer = {
+5 −2
Original line number Diff line number Diff line
@@ -74,7 +74,8 @@ class KeyguardQuickAffordancePickerViewModelTest {

    @Before
    fun setUp() {
        InjectorProvider.setInjector(TestInjector())
        val injector = TestInjector()
        InjectorProvider.setInjector(injector)
        context = ApplicationProvider.getApplicationContext()
        val testDispatcher = StandardTestDispatcher()
        testScope = TestScope(testDispatcher)
@@ -86,7 +87,9 @@ class KeyguardQuickAffordancePickerViewModelTest {
                repository =
                    KeyguardQuickAffordancePickerRepository(
                        client = client,
                        backgroundDispatcher = testDispatcher,
                        scope = testScope.backgroundScope,
                        flags = injector.getFlags(),
                        context = context,
                    ),
                client = client,
                snapshotRestorer = {