• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.systemui.unfold.data.repository
17 
18 import android.os.PowerManager
19 import android.view.Display
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.dagger.qualifiers.Background
22 import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
23 import java.util.concurrent.Executor
24 import javax.inject.Inject
25 import kotlinx.coroutines.CoroutineScope
26 import kotlinx.coroutines.channels.awaitClose
27 import kotlinx.coroutines.flow.SharingStarted
28 import kotlinx.coroutines.flow.StateFlow
29 import kotlinx.coroutines.flow.stateIn
30 
31 /** Repository to get screen timeout updates */
32 @SysUISingleton
33 class ScreenTimeoutPolicyRepository
34 @Inject
35 constructor(
36     private val powerManager: PowerManager,
37     @Background private val executor: Executor,
38     @Background private val scope: CoroutineScope,
39 ) {
40 
41     /** Stores true if there is an active screen timeout */
42     val screenTimeoutActive: StateFlow<Boolean> =
43         conflatedCallbackFlow {
44                 val listener =
45                     PowerManager.ScreenTimeoutPolicyListener { screenTimeoutPolicy ->
46                         trySend(screenTimeoutPolicy == PowerManager.SCREEN_TIMEOUT_ACTIVE)
47                     }
48                 powerManager.addScreenTimeoutPolicyListener(
49                     Display.DEFAULT_DISPLAY,
50                     executor,
51                     listener,
52                 )
53                 awaitClose {
54                     powerManager.removeScreenTimeoutPolicyListener(
55                         Display.DEFAULT_DISPLAY,
56                         listener,
57                     )
58                 }
59             }
60             .stateIn(scope, started = SharingStarted.Eagerly, initialValue = true)
61 }
62