1 /*
2  * Copyright (C) 2023 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 
17 package com.android.systemui.flags
18 
19 import android.os.PowerManager
20 import android.util.Log
21 import com.android.internal.annotations.VisibleForTesting
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.dagger.qualifiers.Background
24 import com.android.systemui.plugins.statusbar.StatusBarStateController
25 import com.android.systemui.util.concurrency.DelayableExecutor
26 import com.android.systemui.util.settings.SecureSettings
27 import com.android.systemui.util.time.SystemClock
28 import javax.inject.Inject
29 
30 @SysUISingleton
31 class RestartDozeListener
32 @Inject
33 constructor(
34     private val settings: SecureSettings,
35     private val statusBarStateController: StatusBarStateController,
36     private val powerManager: PowerManager,
37     private val systemClock: SystemClock,
38     @Background val bgExecutor: DelayableExecutor,
39 ) {
40 
41     companion object {
42         @VisibleForTesting val RESTART_SLEEP_KEY = "restart_nap_after_start"
43     }
44 
45     private var inited = false
46 
47     val listener =
48         object : StatusBarStateController.StateListener {
49             override fun onDozingChanged(isDozing: Boolean) {
50                 storeSleepState(isDozing)
51             }
52         }
53 
54     fun init() {
55         if (inited) {
56             return
57         }
58         inited = true
59 
60         statusBarStateController.addCallback(listener)
61     }
62 
63     fun destroy() {
64         statusBarStateController.removeCallback(listener)
65     }
66 
67     fun maybeRestartSleep() {
68         bgExecutor.executeDelayed(
69             {
70                 if (settings.getBool(RESTART_SLEEP_KEY, false)) {
71                     Log.d("RestartDozeListener", "Restarting sleep state")
72                     powerManager.wakeUp(
73                         systemClock.uptimeMillis(),
74                         PowerManager.WAKE_REASON_APPLICATION,
75                         "RestartDozeListener"
76                     )
77                     powerManager.goToSleep(systemClock.uptimeMillis())
78                 }
79             },
80             1000
81         )
82     }
83 
84     private fun storeSleepState(sleeping: Boolean) {
85         bgExecutor.execute { settings.putBool(RESTART_SLEEP_KEY, sleeping) }
86     }
87 }
88