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

Commit ac508b99 authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato
Browse files

Remove dependency to DumpManager from PerDisplayRepository

This allows the class to be moved into displaylib without having systemui dependencies.

Note how the test for PerInstanceRepository didn't change at all, but is testing also the new dumpable wrapper regardless as the same instance of PerDisplayRepoDumpHelper is provided in tests.

Bug: 401305290
Test: PerDisplayInstanceRepositoryImplTest
Flag: NONE - Just moving code around
Change-Id: I94e199ca565628a1ed85d7f9fc89688e79253795
parent 751316f9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyString
import org.mockito.kotlin.eq
import org.mockito.kotlin.any
import org.mockito.kotlin.verify

@RunWith(AndroidJUnit4::class)
@@ -105,7 +105,7 @@ class PerDisplayInstanceRepositoryImplTest : SysuiTestCase() {

    @Test
    fun start_registersDumpable() {
        verify(kosmos.dumpManager).registerNormalDumpable(anyString(), eq(underTest))
        verify(kosmos.dumpManager).registerNormalDumpable(anyString(), any())
    }

    private fun createDisplay(displayId: Int): Display =
+5 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import com.android.systemui.display.data.repository.DisplayWindowPropertiesRepos
import com.android.systemui.display.data.repository.DisplayWindowPropertiesRepositoryImpl
import com.android.systemui.display.data.repository.FocusedDisplayRepository
import com.android.systemui.display.data.repository.FocusedDisplayRepositoryImpl
import com.android.systemui.display.data.repository.PerDisplayRepoDumpHelper
import com.android.systemui.display.data.repository.PerDisplayRepository
import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor
import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractorImpl
import com.android.systemui.display.domain.interactor.DisplayWindowPropertiesInteractorModule
@@ -80,6 +82,9 @@ interface DisplayModule {
        impl: DisplayWindowPropertiesRepositoryImpl
    ): DisplayWindowPropertiesRepository

    @Binds
    fun dumpRegistrationLambda(helper: PerDisplayRepoDumpHelper): PerDisplayRepository.InitCallback

    companion object {
        @Provides
        @SysUISingleton
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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 com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dump.DumpManager
import com.android.systemui.dump.DumpableFromToString
import javax.inject.Inject

/** Helper class to register PerDisplayRepository in the dump manager in SystemUI. */
@SysUISingleton
class PerDisplayRepoDumpHelper @Inject constructor(private val dumpManager: DumpManager) :
    PerDisplayRepository.InitCallback {
    /**
     * Registers PerDisplayRepository in the dump manager.
     *
     * The repository will be identified by the given debug name.
     */
    override fun onInit(debugName: String, instance: Any) {
        dumpManager.registerNormalDumpable(
            "PerDisplayRepository-$debugName",
            DumpableFromToString(instance),
        )
    }
}
+21 −8
Original line number Diff line number Diff line
@@ -20,13 +20,10 @@ import android.util.Log
import android.view.Display
import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.app.tracing.traceSection
import com.android.systemui.Dumpable
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.dump.DumpManager
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import java.io.PrintWriter
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
@@ -88,6 +85,20 @@ interface PerDisplayRepository<T> {

    /** Debug name for this repository, mainly for tracing and logging. */
    val debugName: String

    /**
     * Callback to run when a given repository is initialized.
     *
     * This allows the caller to perform custom logic when the repository is ready to be used, e.g.
     * register to dumpManager.
     *
     * Note that the instance is *leaked* outside of this class, so it should only be done when
     * repository is meant to live as long as the caller. In systemUI this is ok because the
     * repository lives as long as the process itself.
     */
    interface InitCallback {
        fun onInit(debugName: String, instance: Any)
    }
}

/**
@@ -110,8 +121,8 @@ constructor(
    @Assisted private val instanceProvider: PerDisplayInstanceProvider<T>,
    @Background private val backgroundApplicationScope: CoroutineScope,
    private val displayRepository: DisplayRepository,
    private val dumpManager: DumpManager,
) : PerDisplayRepository<T>, Dumpable {
    private val initCallback: PerDisplayRepository.InitCallback,
) : PerDisplayRepository<T> {

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

@@ -120,7 +131,7 @@ constructor(
    }

    private suspend fun start() {
        dumpManager.registerNormalDumpable("PerDisplayRepository-${debugName}", this)
        initCallback.onInit(debugName, this)
        displayRepository.displayIds.collectLatest { displayIds ->
            val toRemove = perDisplayInstances.keys - displayIds
            toRemove.forEach { displayId ->
@@ -169,8 +180,9 @@ constructor(
        private const val TAG = "PerDisplayInstanceRepo"
    }

    override fun dump(pw: PrintWriter, args: Array<out String>) {
        pw.println(perDisplayInstances)
    override fun toString(): String {
        return "PerDisplayInstanceRepositoryImpl(" +
            "debugName='$debugName', instances=$perDisplayInstances)"
    }
}

@@ -193,6 +205,7 @@ class DefaultDisplayOnlyInstanceRepositoryImpl<T>(
    private val lazyDefaultDisplayInstance by lazy {
        instanceProvider.createInstance(Display.DEFAULT_DISPLAY)
    }

    override fun get(displayId: Int): T? = lazyDefaultDisplayInstance
}

+27 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.dump

import com.android.systemui.Dumpable
import java.io.PrintWriter

/** Dumpable implementation that just calls toString() on the instance. */
class DumpableFromToString<T>(private val instance: T) : Dumpable {
    override fun dump(pw: PrintWriter, args: Array<out String>) {
        pw.println("$instance")
    }
}
Loading