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

Commit 9e39ccc6 authored by Chris Göllner's avatar Chris Göllner
Browse files

Create generic implementation of PerDisplayStore.

Most of the "Store" classes for multi display, have the same
implementation.
To avoid logic duplication, this generic class can be used now instead.

Also migrates StatusBarWindowControllerStore and
StatusBarInitializerStore to use it.

Test: PerDisplayStoreImplTest
Bug: 373800041
Flag: com.android.systemui.status_bar_connected_displays
Change-Id: I49fc4207138f396ef3b5a3381270164c54e991e7
parent 07274db5
Loading
Loading
Loading
Loading
+27 −32
Original line number Diff line number Diff line
@@ -14,23 +14,15 @@
 * limitations under the License.
 */

package com.android.systemui.statusbar.window
package com.android.systemui.display.data.repository

import android.platform.test.annotations.EnableFlags
import android.view.Display
import android.view.WindowManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.app.viewcapture.ViewCaptureAwareWindowManager
import com.android.app.viewcapture.mockViewCaptureAwareWindowManager
import com.android.systemui.SysuiTestCase
import com.android.systemui.display.data.repository.displayRepository
import com.android.systemui.display.data.repository.fakeDisplayWindowPropertiesRepository
import com.android.systemui.kosmos.applicationCoroutineScope
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.statusbar.core.StatusBarConnectedDisplays
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.runBlocking
@@ -43,28 +35,13 @@ import org.mockito.kotlin.mock

@RunWith(AndroidJUnit4::class)
@SmallTest
@EnableFlags(StatusBarConnectedDisplays.FLAG_NAME)
class MultiDisplayStatusBarWindowControllerStoreTest : SysuiTestCase() {
class PerDisplayStoreImplTest : SysuiTestCase() {

    private val kosmos = testKosmos().also { it.testDispatcher = it.unconfinedTestDispatcher }
    private val testScope = kosmos.testScope
    private val fakeDisplayRepository = kosmos.displayRepository

    private val store =
        MultiDisplayStatusBarWindowControllerStore(
            backgroundApplicationScope = kosmos.applicationCoroutineScope,
            controllerFactory = kosmos.fakeStatusBarWindowControllerFactory,
            displayWindowPropertiesRepository = kosmos.fakeDisplayWindowPropertiesRepository,
            viewCaptureAwareWindowManagerFactory =
                object : ViewCaptureAwareWindowManager.Factory {
                    override fun create(
                        windowManager: WindowManager
                    ): ViewCaptureAwareWindowManager {
                        return kosmos.mockViewCaptureAwareWindowManager
                    }
                },
            displayRepository = fakeDisplayRepository,
        )
    private val store = kosmos.fakePerDisplayStore

    @Before
    fun start() {
@@ -80,34 +57,52 @@ class MultiDisplayStatusBarWindowControllerStoreTest : SysuiTestCase() {
    @Test
    fun forDisplay_defaultDisplay_multipleCalls_returnsSameInstance() =
        testScope.runTest {
            val controller = store.defaultDisplay
            val instance = store.defaultDisplay

            assertThat(store.defaultDisplay).isSameInstanceAs(controller)
            assertThat(store.defaultDisplay).isSameInstanceAs(instance)
        }

    @Test
    fun forDisplay_nonDefaultDisplay_multipleCalls_returnsSameInstance() =
        testScope.runTest {
            val controller = store.forDisplay(NON_DEFAULT_DISPLAY_ID)
            val instance = store.forDisplay(NON_DEFAULT_DISPLAY_ID)

            assertThat(store.forDisplay(NON_DEFAULT_DISPLAY_ID)).isSameInstanceAs(controller)
            assertThat(store.forDisplay(NON_DEFAULT_DISPLAY_ID)).isSameInstanceAs(instance)
        }

    @Test
    fun forDisplay_nonDefaultDisplay_afterDisplayRemoved_returnsNewInstance() =
        testScope.runTest {
            val controller = store.forDisplay(NON_DEFAULT_DISPLAY_ID)
            val instance = store.forDisplay(NON_DEFAULT_DISPLAY_ID)

            fakeDisplayRepository.removeDisplay(NON_DEFAULT_DISPLAY_ID)
            fakeDisplayRepository.addDisplay(createDisplay(NON_DEFAULT_DISPLAY_ID))

            assertThat(store.forDisplay(NON_DEFAULT_DISPLAY_ID)).isNotSameInstanceAs(controller)
            assertThat(store.forDisplay(NON_DEFAULT_DISPLAY_ID)).isNotSameInstanceAs(instance)
        }

    @Test(expected = IllegalArgumentException::class)
    fun forDisplay_nonExistingDisplayId_throws() =
        testScope.runTest { store.forDisplay(NON_EXISTING_DISPLAY_ID) }

    @Test
    fun forDisplay_afterDisplayRemoved_onDisplayRemovalActionInvoked() =
        testScope.runTest {
            val instance = store.forDisplay(NON_DEFAULT_DISPLAY_ID)

            fakeDisplayRepository.removeDisplay(NON_DEFAULT_DISPLAY_ID)

            assertThat(store.removalActions).containsExactly(instance)
        }

    @Test
    fun forDisplay_withoutDisplayRemoval_onDisplayRemovalActionIsNotInvoked() =
        testScope.runTest {
            store.forDisplay(NON_DEFAULT_DISPLAY_ID)

            assertThat(store.removalActions).isEmpty()
        }

    private fun createDisplay(displayId: Int): Display = mock {
        on { getDisplayId() } doReturn displayId
    }
+108 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.display.data.repository

import android.view.Display
import com.android.systemui.CoreStartable
import com.android.systemui.dagger.qualifiers.Background
import java.io.PrintWriter
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/** Provides per display instances of [T]. */
interface PerDisplayStore<T> {

    /**
     * The instance for the default/main display of the device. For example, on a phone or a tablet,
     * the default display is the internal/built-in display of the device.
     *
     * Note that the id of the default display is [Display.DEFAULT_DISPLAY].
     */
    val defaultDisplay: T

    /**
     * Returns an instance for a specific display id.
     *
     * @throws IllegalArgumentException if [displayId] doesn't match the id of any existing
     *   displays.
     */
    fun forDisplay(displayId: Int): T
}

abstract class PerDisplayStoreImpl<T>(
    @Background private val backgroundApplicationScope: CoroutineScope,
    private val displayRepository: DisplayRepository,
) : PerDisplayStore<T>, CoreStartable {

    private val perDisplayInstances = ConcurrentHashMap<Int, T>()

    /**
     * The instance for the default/main display of the device. For example, on a phone or a tablet,
     * the default display is the internal/built-in display of the device.
     *
     * Note that the id of the default display is [Display.DEFAULT_DISPLAY].
     */
    override val defaultDisplay: T
        get() = forDisplay(Display.DEFAULT_DISPLAY)

    /**
     * Returns an instance for a specific display id.
     *
     * @throws IllegalArgumentException if [displayId] doesn't match the id of any existing
     *   displays.
     */
    override fun forDisplay(displayId: Int): T {
        if (displayRepository.getDisplay(displayId) == null) {
            throw IllegalArgumentException("Display with id $displayId doesn't exist.")
        }
        return perDisplayInstances.computeIfAbsent(displayId) {
            createInstanceForDisplay(displayId)
        }
    }

    abstract fun createInstanceForDisplay(displayId: Int): T

    override fun start() {
        val instanceType = instanceClass.simpleName
        backgroundApplicationScope.launch(CoroutineName("PerDisplayStore#<$instanceType>start")) {
            displayRepository.displayRemovalEvent.collect { removedDisplayId ->
                val removedInstance = perDisplayInstances.remove(removedDisplayId)
                removedInstance?.let { onDisplayRemovalAction(it) }
            }
        }
    }

    abstract val instanceClass: Class<T>

    /**
     * Will be called when the display associated with [instance] was removed. It allows to perform
     * any clean up if needed.
     */
    open suspend fun onDisplayRemovalAction(instance: T) {}

    override fun dump(pw: PrintWriter, args: Array<out String>) {
        pw.println(perDisplayInstances)
    }
}

class SingleDisplayStore<T>(defaultInstance: T) : PerDisplayStore<T> {
    override val defaultDisplay: T = defaultInstance

    override fun forDisplay(displayId: Int): T = defaultDisplay
}
+17 −53
Original line number Diff line number Diff line
@@ -16,88 +16,52 @@

package com.android.systemui.statusbar.core

import android.view.Display
import com.android.systemui.CoreStartable
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.display.data.repository.DisplayRepository
import com.android.systemui.display.data.repository.PerDisplayStore
import com.android.systemui.display.data.repository.PerDisplayStoreImpl
import com.android.systemui.display.data.repository.SingleDisplayStore
import com.android.systemui.statusbar.window.StatusBarWindowControllerStore
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/** Provides per display instances of [StatusBarInitializer]. */
interface StatusBarInitializerStore {
    /**
     * The instance for the default/main display of the device. For example, on a phone or a tablet,
     * the default display is the internal/built-in display of the device.
     *
     * Note that the id of the default display is [Display.DEFAULT_DISPLAY].
     */
    val defaultDisplay: StatusBarInitializer

    /**
     * Returns an instance for a specific display id.
     *
     * @throws IllegalArgumentException if [displayId] doesn't match the id of any existing
     *   displays.
     */
    fun forDisplay(displayId: Int): StatusBarInitializer
}
interface StatusBarInitializerStore : PerDisplayStore<StatusBarInitializer>

@SysUISingleton
class MultiDisplayStatusBarInitializerStore
@Inject
constructor(
    @Background private val backgroundApplicationScope: CoroutineScope,
    @Background backgroundApplicationScope: CoroutineScope,
    displayRepository: DisplayRepository,
    private val factory: StatusBarInitializer.Factory,
    private val displayRepository: DisplayRepository,
    private val statusBarWindowControllerStore: StatusBarWindowControllerStore,
) : StatusBarInitializerStore, CoreStartable {
) :
    StatusBarInitializerStore,
    PerDisplayStoreImpl<StatusBarInitializer>(backgroundApplicationScope, displayRepository) {

    init {
        StatusBarConnectedDisplays.assertInNewMode()
    }

    private val perDisplayInitializers = ConcurrentHashMap<Int, StatusBarInitializer>()

    override val defaultDisplay: StatusBarInitializer
        get() = forDisplay(Display.DEFAULT_DISPLAY)

    override fun forDisplay(displayId: Int): StatusBarInitializer {
        if (displayRepository.getDisplay(displayId) == null) {
            throw IllegalArgumentException("Display with id $displayId doesn't exist.")
        }
        return perDisplayInitializers.computeIfAbsent(displayId) {
            factory.create(
    override fun createInstanceForDisplay(displayId: Int): StatusBarInitializer {
        return factory.create(
            statusBarWindowController = statusBarWindowControllerStore.forDisplay(displayId)
        )
    }
    }

    override fun start() {
        backgroundApplicationScope.launch(
            CoroutineName("MultiDisplayStatusBarInitializerStore#start")
        ) {
            displayRepository.displayRemovalEvent.collect { removedDisplayId ->
                perDisplayInitializers.remove(removedDisplayId)
            }
        }
    }
    override val instanceClass = StatusBarInitializer::class.java
}

@SysUISingleton
class SingleDisplayStatusBarInitializerStore
@Inject
constructor(private val defaultInstance: StatusBarInitializer) : StatusBarInitializerStore {
constructor(defaultInitializer: StatusBarInitializer) :
    StatusBarInitializerStore,
    PerDisplayStore<StatusBarInitializer> by SingleDisplayStore(defaultInitializer) {

    init {
        StatusBarConnectedDisplays.assertInLegacyMode()
    }

    override val defaultDisplay: StatusBarInitializer = defaultInstance

    override fun forDisplay(displayId: Int): StatusBarInitializer = defaultInstance
}
+17 −56
Original line number Diff line number Diff line
@@ -17,78 +17,40 @@
package com.android.systemui.statusbar.window

import android.content.Context
import android.view.Display
import android.view.WindowManager
import com.android.app.viewcapture.ViewCaptureAwareWindowManager
import com.android.systemui.CoreStartable
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.display.data.repository.DisplayRepository
import com.android.systemui.display.data.repository.DisplayWindowPropertiesRepository
import com.android.systemui.display.data.repository.PerDisplayStore
import com.android.systemui.display.data.repository.PerDisplayStoreImpl
import com.android.systemui.display.data.repository.SingleDisplayStore
import com.android.systemui.statusbar.core.StatusBarConnectedDisplays
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/** Store that allows to retrieve per display instances of [StatusBarWindowController]. */
interface StatusBarWindowControllerStore {
    /**
     * The instance for the default/main display of the device. For example, on a phone or a tablet,
     * the default display is the internal/built-in display of the device.
     *
     * Note that the id of the default display is [Display.DEFAULT_DISPLAY].
     */
    val defaultDisplay: StatusBarWindowController

    /**
     * Returns an instance for a specific display id.
     *
     * @throws IllegalArgumentException if [displayId] doesn't match the id of any existing
     *   displays.
     */
    fun forDisplay(displayId: Int): StatusBarWindowController
}
interface StatusBarWindowControllerStore : PerDisplayStore<StatusBarWindowController>

@SysUISingleton
class MultiDisplayStatusBarWindowControllerStore
@Inject
constructor(
    @Background private val backgroundApplicationScope: CoroutineScope,
    @Background backgroundApplicationScope: CoroutineScope,
    private val controllerFactory: StatusBarWindowController.Factory,
    private val displayWindowPropertiesRepository: DisplayWindowPropertiesRepository,
    private val viewCaptureAwareWindowManagerFactory: ViewCaptureAwareWindowManager.Factory,
    private val displayRepository: DisplayRepository,
) : StatusBarWindowControllerStore, CoreStartable {
    displayRepository: DisplayRepository,
) :
    StatusBarWindowControllerStore,
    PerDisplayStoreImpl<StatusBarWindowController>(backgroundApplicationScope, displayRepository) {

    init {
        StatusBarConnectedDisplays.assertInNewMode()
    }

    private val perDisplayControllers = ConcurrentHashMap<Int, StatusBarWindowController>()

    override fun start() {
        backgroundApplicationScope.launch(CoroutineName("StatusBarWindowController#start")) {
            displayRepository.displayRemovalEvent.collect { displayId ->
                perDisplayControllers.remove(displayId)
            }
        }
    }

    override val defaultDisplay: StatusBarWindowController
        get() = forDisplay(Display.DEFAULT_DISPLAY)

    override fun forDisplay(displayId: Int): StatusBarWindowController {
        if (displayRepository.getDisplay(displayId) == null) {
            throw IllegalArgumentException("Display with id $displayId doesn't exist.")
        }
        return perDisplayControllers.computeIfAbsent(displayId) {
            createControllerForDisplay(displayId)
        }
    }

    private fun createControllerForDisplay(displayId: Int): StatusBarWindowController {
    override fun createInstanceForDisplay(displayId: Int): StatusBarWindowController {
        val statusBarDisplayContext =
            displayWindowPropertiesRepository.get(
                displayId = displayId,
@@ -101,6 +63,8 @@ constructor(
            viewCaptureAwareWindowManager,
        )
    }

    override val instanceClass = StatusBarWindowController::class.java
}

@SysUISingleton
@@ -110,16 +74,13 @@ constructor(
    context: Context,
    viewCaptureAwareWindowManager: ViewCaptureAwareWindowManager,
    factory: StatusBarWindowControllerImpl.Factory,
) : StatusBarWindowControllerStore {
) :
    StatusBarWindowControllerStore,
    PerDisplayStore<StatusBarWindowController> by SingleDisplayStore(
        factory.create(context, viewCaptureAwareWindowManager)
    ) {

    init {
        StatusBarConnectedDisplays.assertInLegacyMode()
    }

    private val controller: StatusBarWindowController =
        factory.create(context, viewCaptureAwareWindowManager)

    override val defaultDisplay = controller

    override fun forDisplay(displayId: Int) = controller
}
+0 −97
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.statusbar.core

import android.platform.test.annotations.EnableFlags
import android.view.Display
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.display.data.repository.displayRepository
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@SmallTest
@EnableFlags(StatusBarConnectedDisplays.FLAG_NAME)
class MultiDisplayStatusBarInitializerStoreTest : SysuiTestCase() {

    private val kosmos =
        testKosmos().also {
            // Using unconfinedTestDispatcher to avoid having to call `runCurrent` in the tests.
            it.testDispatcher = it.unconfinedTestDispatcher
        }
    private val testScope = kosmos.testScope
    private val fakeDisplayRepository = kosmos.displayRepository
    private val store = kosmos.multiDisplayStatusBarInitializerStore

    @Before
    fun start() {
        store.start()
    }

    @Before
    fun addDisplays() = runBlocking {
        fakeDisplayRepository.addDisplay(DEFAULT_DISPLAY_ID)
        fakeDisplayRepository.addDisplay(NON_DEFAULT_DISPLAY_ID)
    }

    @Test
    fun forDisplay_defaultDisplay_multipleCalls_returnsSameInstance() =
        testScope.runTest {
            val controller = store.defaultDisplay

            assertThat(store.defaultDisplay).isSameInstanceAs(controller)
        }

    @Test
    fun forDisplay_nonDefaultDisplay_multipleCalls_returnsSameInstance() =
        testScope.runTest {
            val controller = store.forDisplay(NON_DEFAULT_DISPLAY_ID)

            assertThat(store.forDisplay(NON_DEFAULT_DISPLAY_ID)).isSameInstanceAs(controller)
        }

    @Test
    fun forDisplay_nonDefaultDisplay_afterDisplayRemoved_returnsNewInstance() =
        testScope.runTest {
            val controller = store.forDisplay(NON_DEFAULT_DISPLAY_ID)

            fakeDisplayRepository.removeDisplay(NON_DEFAULT_DISPLAY_ID)
            fakeDisplayRepository.addDisplay(NON_DEFAULT_DISPLAY_ID)

            assertThat(store.forDisplay(NON_DEFAULT_DISPLAY_ID)).isNotSameInstanceAs(controller)
        }

    @Test(expected = IllegalArgumentException::class)
    fun forDisplay_nonExistingDisplayId_throws() =
        testScope.runTest { store.forDisplay(NON_EXISTING_DISPLAY_ID) }

    companion object {
        private const val DEFAULT_DISPLAY_ID = Display.DEFAULT_DISPLAY
        private const val NON_DEFAULT_DISPLAY_ID = Display.DEFAULT_DISPLAY + 1
        private const val NON_EXISTING_DISPLAY_ID = Display.DEFAULT_DISPLAY + 2
    }
}
Loading