1 /*
2  * Copyright (C) 2022 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 
18 package com.android.systemui.power.domain.interactor
19 
20 import android.os.PowerManager
21 import com.android.systemui.classifier.FalsingCollector
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.keyguard.data.repository.KeyguardRepository
24 import com.android.systemui.plugins.statusbar.StatusBarStateController
25 import com.android.systemui.power.data.repository.PowerRepository
26 import com.android.systemui.statusbar.phone.ScreenOffAnimationController
27 import javax.inject.Inject
28 import kotlinx.coroutines.flow.Flow
29 
30 /** Hosts business logic for interacting with the power system. */
31 @SysUISingleton
32 class PowerInteractor
33 @Inject
34 constructor(
35     private val repository: PowerRepository,
36     private val keyguardRepository: KeyguardRepository,
37     private val falsingCollector: FalsingCollector,
38     private val screenOffAnimationController: ScreenOffAnimationController,
39     private val statusBarStateController: StatusBarStateController,
40 ) {
41     /** Whether the screen is on or off. */
42     val isInteractive: Flow<Boolean> = repository.isInteractive
43 
44     /**
45      * Wakes up the device if the device was dozing.
46      *
47      * @param why a string explaining why we're waking the device for debugging purposes. Should be
48      *   in SCREAMING_SNAKE_CASE.
49      * @param wakeReason the PowerManager-based reason why we're waking the device.
50      */
51     fun wakeUpIfDozing(why: String, @PowerManager.WakeReason wakeReason: Int) {
52         if (
53             statusBarStateController.isDozing && screenOffAnimationController.allowWakeUpIfDozing()
54         ) {
55             repository.wakeUp(why, wakeReason)
56             falsingCollector.onScreenOnFromTouch()
57         }
58     }
59 
60     /**
61      * Wakes up the device if the device was dozing or going to sleep in order to display a
62      * full-screen intent.
63      */
64     fun wakeUpForFullScreenIntent() {
65         if (
66             keyguardRepository.wakefulness.value.isStartingToSleep() ||
67                 statusBarStateController.isDozing
68         ) {
69             repository.wakeUp(why = FSI_WAKE_WHY, wakeReason = PowerManager.WAKE_REASON_APPLICATION)
70         }
71     }
72 
73     /**
74      * Wakes up the device if dreaming with a screensaver.
75      *
76      * @param why a string explaining why we're waking the device for debugging purposes. Should be
77      *   in SCREAMING_SNAKE_CASE.
78      * @param wakeReason the PowerManager-based reason why we're waking the device.
79      */
80     fun wakeUpIfDreaming(why: String, @PowerManager.WakeReason wakeReason: Int) {
81         if (statusBarStateController.isDreaming) {
82             repository.wakeUp(why, wakeReason)
83         }
84     }
85 
86     companion object {
87         private const val FSI_WAKE_WHY = "full_screen_intent"
88     }
89 }
90