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 package com.android.systemui.dreams.conditions;
17 
18 import android.app.DreamManager;
19 
20 import com.android.keyguard.KeyguardUpdateMonitor;
21 import com.android.keyguard.KeyguardUpdateMonitorCallback;
22 import com.android.systemui.dagger.qualifiers.Application;
23 import com.android.systemui.shared.condition.Condition;
24 
25 import javax.inject.Inject;
26 
27 import kotlinx.coroutines.CoroutineScope;
28 
29 /**
30  * {@link DreamCondition} provides a signal when a dream begins and ends.
31  */
32 public class DreamCondition extends Condition {
33     private final DreamManager mDreamManager;
34     private final KeyguardUpdateMonitor mUpdateMonitor;
35 
36     private final KeyguardUpdateMonitorCallback mUpdateCallback =
37             new KeyguardUpdateMonitorCallback() {
38                 @Override
39                 public void onDreamingStateChanged(boolean dreaming) {
40                     updateCondition(dreaming);
41                 }
42             };
43 
44     @Inject
DreamCondition( @pplication CoroutineScope scope, DreamManager dreamManager, KeyguardUpdateMonitor monitor)45     public DreamCondition(
46             @Application CoroutineScope scope,
47             DreamManager dreamManager,
48             KeyguardUpdateMonitor monitor) {
49         super(scope);
50         mDreamManager = dreamManager;
51         mUpdateMonitor = monitor;
52     }
53 
54     @Override
start()55     protected void start() {
56         mUpdateMonitor.registerCallback(mUpdateCallback);
57         updateCondition(mDreamManager.isDreaming());
58     }
59 
60     @Override
stop()61     protected void stop() {
62         mUpdateMonitor.removeCallback(mUpdateCallback);
63     }
64 
65     @Override
getStartStrategy()66     protected int getStartStrategy() {
67         return START_EAGERLY;
68     }
69 }
70