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

Commit 515408c0 authored by Kazuki Takise's avatar Kazuki Takise Committed by Android (Google) Code Review
Browse files

Merge "Implement repository for display focus in SystemUI" into main

parents c8a5acea d549458c
Loading
Loading
Loading
Loading
+69 −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.annotation.MainThread
import android.view.Display.DEFAULT_DISPLAY
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.log.dagger.FocusedDisplayRepoLog
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import com.android.wm.shell.shared.FocusTransitionListener
import com.android.wm.shell.shared.ShellTransitions
import java.util.concurrent.Executor
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.StateFlow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn

/** Repository tracking display focus. */
@SysUISingleton
@MainThread
class FocusedDisplayRepository
@Inject
constructor(
    @Application val scope: CoroutineScope,
    @Main private val mainExecutor: Executor,
    transitions: ShellTransitions,
    @FocusedDisplayRepoLog logBuffer: LogBuffer,
) {
    val focusedTask: Flow<Int> =
        conflatedCallbackFlow {
                val listener = FocusTransitionListener { displayId -> trySend(displayId) }
                transitions.setFocusTransitionListener(listener, mainExecutor)
                awaitClose { transitions.unsetFocusTransitionListener(listener) }
            }
            .onEach {
                logBuffer.log(
                    "FocusedDisplayRepository",
                    LogLevel.INFO,
                    { str1 = it.toString() },
                    { "Newly focused display: $str1" },
                )
            }

    /** Provides the currently focused display. */
    val focusedDisplayId: StateFlow<Int>
        get() = focusedTask.stateIn(scope, SharingStarted.Eagerly, DEFAULT_DISPLAY)
}
+25 −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.log.dagger

import javax.inject.Qualifier

/** A [com.android.systemui.log.LogBuffer] for display metrics related logging. */
@Qualifier
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class FocusedDisplayRepoLog
+8 −0
Original line number Diff line number Diff line
@@ -655,6 +655,14 @@ public class LogModule {
        return factory.create("DisplayMetricsRepo", 50);
    }

    /** Provides a {@link LogBuffer} for focus related logs. */
    @Provides
    @SysUISingleton
    @FocusedDisplayRepoLog
    public static LogBuffer provideFocusedDisplayRepoLogBuffer(LogBufferFactory factory) {
        return factory.create("FocusedDisplayRepo", 50);
    }

    /** Provides a {@link LogBuffer} for the scene framework. */
    @Provides
    @SysUISingleton