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

Commit 4b3b2daa authored by Alina Zaidi's avatar Alina Zaidi
Browse files

Add a store to generate mutiple instances of AutoHideController for multi display

Bug: 373309973
Test: atest com.android.systemui.statusbar.phone.MultiDisplayAutoHideControllerStoreTest
Flag: com.android.systemui.status_bar_connected_displays
Change-Id: I896854ab63687215360d32f06ece342767d05216
parent e09bad52
Loading
Loading
Loading
Loading
+68 −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.statusbar.phone

import android.platform.test.annotations.EnableFlags
import android.view.Display.DEFAULT_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.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.core.StatusBarConnectedDisplays
import com.android.systemui.testKosmos
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.never
import org.mockito.kotlin.verify

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

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val testScope = kosmos.testScope
    private val fakeDisplayRepository = kosmos.displayRepository

    // Lazy so that @EnableFlags has time to run before underTest is instantiated.
    private val underTest by lazy { kosmos.multiDisplayAutoHideControllerStore }

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

    @Test
    fun beforeDisplayRemoved_doesNotStopInstances() =
        testScope.runTest {
            val instance = underTest.forDisplay(DEFAULT_DISPLAY)

            verify(instance, never()).stop()
        }

    @Test
    fun displayRemoved_stopsInstance() =
        testScope.runTest {
            val instance = underTest.forDisplay(DEFAULT_DISPLAY)

            fakeDisplayRepository.removeDisplay(DEFAULT_DISPLAY)

            verify(instance).stop()
        }
}
+3 −0
Original line number Diff line number Diff line
@@ -51,6 +51,9 @@ interface AutoHideController {
    /** Hides system bars on user touch if the interaction requires them to be hidden. */
    fun checkUserAutoHide(event: MotionEvent)

    /** Called when work should stop and resources should be released. */
    fun stop()

    /** Dumps the current state of the [AutoHideController] */
    fun dump(pw: PrintWriter)

+5 −0
Original line number Diff line number Diff line
@@ -190,6 +190,11 @@ public class AutoHideControllerImpl implements AutoHideController {
        return false;
    }

    @Override
    public void stop() {
        mHandler.removeCallbacks(mAutoHide);
    }

    @Override
    public void dump(@NonNull PrintWriter pw) {
        pw.println("AutoHideController:");
+73 −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.statusbar.phone

import android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR
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 javax.inject.Inject
import kotlinx.coroutines.CoroutineScope

/** Provides per display instances of [AutoHideController] */
interface AutoHideControllerStore : PerDisplayStore<AutoHideController>

@SysUISingleton
class MultiDisplayAutoHideControllerStore
@Inject
constructor(
    @Background backgroundApplicationScope: CoroutineScope,
    displayRepository: DisplayRepository,
    private val displayWindowPropertiesRepository: DisplayWindowPropertiesRepository,
    private val autoHideControllerFactory: AutoHideControllerImpl.Factory,
) :
    AutoHideControllerStore,
    PerDisplayStoreImpl<AutoHideController>(backgroundApplicationScope, displayRepository) {

    init {
        StatusBarConnectedDisplays.assertInNewMode()
    }

    override fun createInstanceForDisplay(displayId: Int): AutoHideController {
        val displayWindowProperties =
            displayWindowPropertiesRepository.get(displayId, TYPE_STATUS_BAR)
        return autoHideControllerFactory.create(displayWindowProperties.context)
    }

    override suspend fun onDisplayRemovalAction(instance: AutoHideController) {
        instance.stop()
    }

    override val instanceClass = AutoHideController::class.java
}

@SysUISingleton
class SingleDisplayAutoHideControllerStore
@Inject
constructor(defaultController: AutoHideController) :
    AutoHideControllerStore,
    PerDisplayStore<AutoHideController> by SingleDisplayStore(defaultController) {

    init {
        StatusBarConnectedDisplays.assertInLegacyMode()
    }
}
+16 −0
Original line number Diff line number Diff line
@@ -37,7 +37,10 @@ import com.android.systemui.statusbar.data.repository.StatusBarModeRepositorySto
import com.android.systemui.statusbar.events.PrivacyDotViewControllerModule
import com.android.systemui.statusbar.phone.AutoHideController
import com.android.systemui.statusbar.phone.AutoHideControllerImpl
import com.android.systemui.statusbar.phone.AutoHideControllerStore
import com.android.systemui.statusbar.phone.CentralSurfacesCommandQueueCallbacks
import com.android.systemui.statusbar.phone.MultiDisplayAutoHideControllerStore
import com.android.systemui.statusbar.phone.SingleDisplayAutoHideControllerStore
import com.android.systemui.statusbar.window.StatusBarWindowControllerStore
import com.android.systemui.statusbar.window.data.repository.StatusBarWindowStateRepositoryStore
import com.android.systemui.statusbar.window.data.repository.StatusBarWindowStateRepositoryStoreImpl
@@ -191,5 +194,18 @@ interface StatusBarPhoneModule {
                singleDisplayStoreLazy.get()
            }
        }

        @Provides
        @SysUISingleton
        fun autoHideStore(
            singleDisplayLazy: Lazy<SingleDisplayAutoHideControllerStore>,
            multiDisplayLazy: Lazy<MultiDisplayAutoHideControllerStore>,
        ): AutoHideControllerStore {
            return if (StatusBarConnectedDisplays.isEnabled) {
                multiDisplayLazy.get()
            } else {
                singleDisplayLazy.get()
            }
        }
    }
}
Loading