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

Commit f3dfa3f2 authored by Anton Potapov's avatar Anton Potapov Committed by Android (Google) Code Review
Browse files

Merge "Add package updates repository" into main

parents 06ab26fe a75bd285
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.tiles.impl.custom.data.repository

import android.os.UserHandle
import com.android.systemui.common.coroutine.ConflatedCallbackFlow
import com.android.systemui.qs.external.TileServiceManager
import com.android.systemui.qs.pipeline.shared.TileSpec
import com.android.systemui.qs.tiles.impl.custom.di.bound.CustomTileBoundScope
import com.android.systemui.qs.tiles.impl.custom.di.bound.CustomTileUser
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.onEach
import kotlinx.coroutines.flow.shareIn

interface CustomTilePackageUpdatesRepository {

    val packageChanges: Flow<Unit>
}

@CustomTileBoundScope
class CustomTilePackageUpdatesRepositoryImpl
@Inject
constructor(
    tileSpec: TileSpec.CustomTileSpec,
    @CustomTileUser user: UserHandle,
    serviceManager: TileServiceManager,
    defaultsRepository: CustomTileDefaultsRepository,
    @CustomTileBoundScope boundScope: CoroutineScope,
) : CustomTilePackageUpdatesRepository {

    override val packageChanges: Flow<Unit> =
        ConflatedCallbackFlow.conflatedCallbackFlow {
                serviceManager.setTileChangeListener { changedComponentName ->
                    if (changedComponentName == tileSpec.componentName) {
                        trySend(Unit)
                    }
                }

                awaitClose { serviceManager.setTileChangeListener(null) }
            }
            .onEach { defaultsRepository.requestNewDefaults(user, tileSpec.componentName, true) }
            .shareIn(boundScope, SharingStarted.WhileSubscribed())
}
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import kotlinx.coroutines.CoroutineScope

/** @see CustomTileBoundScope */
@CustomTileBoundScope
@Subcomponent
@Subcomponent(modules = [CustomTileBoundModule::class])
interface CustomTileBoundComponent {

    @Subcomponent.Builder
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.tiles.impl.custom.di.bound

import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepository
import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepositoryImpl
import dagger.Binds
import dagger.Module

@Module
interface CustomTileBoundModule {

    @Binds
    fun bindCustomTilePackageUpdatesRepository(
        impl: CustomTilePackageUpdatesRepositoryImpl
    ): CustomTilePackageUpdatesRepository
}
+119 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.tiles.impl.custom

import android.content.ComponentName
import android.os.UserHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.qs.external.TileLifecycleManager
import com.android.systemui.qs.external.TileServiceManager
import com.android.systemui.qs.pipeline.shared.TileSpec
import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepository
import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepositoryImpl
import com.android.systemui.qs.tiles.impl.custom.data.repository.FakeCustomTileDefaultsRepository
import com.android.systemui.qs.tiles.impl.custom.data.repository.FakeCustomTileDefaultsRepository.DefaultsRequest
import com.android.systemui.util.mockito.capture
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class CustomTilePackageUpdatesRepositoryTest : SysuiTestCase() {

    @Mock private lateinit var tileServiceManager: TileServiceManager

    @Captor
    private lateinit var listenerCaptor: ArgumentCaptor<TileLifecycleManager.TileChangeListener>

    private val defaultsRepository = FakeCustomTileDefaultsRepository()
    private val testDispatcher = StandardTestDispatcher()
    private val testScope = TestScope(testDispatcher)

    private lateinit var underTest: CustomTilePackageUpdatesRepository

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)

        underTest =
            CustomTilePackageUpdatesRepositoryImpl(
                TileSpec.create(COMPONENT_1),
                USER,
                tileServiceManager,
                defaultsRepository,
                testScope.backgroundScope,
            )
    }

    @Test
    fun packageChangesUpdatesDefaults() =
        testScope.runTest {
            val events = mutableListOf<Unit>()
            underTest.packageChanges.onEach { events.add(it) }.launchIn(backgroundScope)
            runCurrent()
            verify(tileServiceManager).setTileChangeListener(capture(listenerCaptor))

            emitPackageChange()
            runCurrent()

            assertThat(events).hasSize(1)
            assertThat(defaultsRepository.defaultsRequests).isNotEmpty()
            assertThat(defaultsRepository.defaultsRequests.last())
                .isEqualTo(DefaultsRequest(USER, COMPONENT_1, true))
        }

    @Test
    fun packageChangesEmittedOnlyForTheTile() =
        testScope.runTest {
            val events = mutableListOf<Unit>()
            underTest.packageChanges.onEach { events.add(it) }.launchIn(backgroundScope)
            runCurrent()
            verify(tileServiceManager).setTileChangeListener(capture(listenerCaptor))

            emitPackageChange(COMPONENT_2)
            runCurrent()

            assertThat(events).isEmpty()
        }

    private fun emitPackageChange(componentName: ComponentName = COMPONENT_1) {
        listenerCaptor.value.onTileChanged(componentName)
    }

    private companion object {
        val USER = UserHandle(0)
        val COMPONENT_1 = ComponentName("pkg.test.1", "cls.test")
        val COMPONENT_2 = ComponentName("pkg.test.2", "cls.test")
    }
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.tiles.impl.custom.data.repository

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow

class FakeCustomTilePackageUpdatesRepository : CustomTilePackageUpdatesRepository {

    private val mutablePackageChanges = MutableSharedFlow<Unit>()

    override val packageChanges: Flow<Unit>
        get() = mutablePackageChanges

    suspend fun emitPackageChange() {
        mutablePackageChanges.emit(Unit)
    }
}