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.dreams;
18 
19 import android.util.Log;
20 
21 import com.android.systemui.CoreStartable;
22 import com.android.systemui.dagger.qualifiers.SystemUser;
23 import com.android.systemui.dreams.callbacks.DreamStatusBarStateCallback;
24 import com.android.systemui.dreams.conditions.DreamCondition;
25 import com.android.systemui.flags.RestartDozeListener;
26 import com.android.systemui.shared.condition.Monitor;
27 import com.android.systemui.util.condition.ConditionalCoreStartable;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * A {@link CoreStartable} to retain a monitor for tracking dreaming.
33  */
34 public class DreamMonitor extends ConditionalCoreStartable {
35     private static final String TAG = "DreamMonitor";
36 
37     // We retain a reference to the monitor so it is not garbage-collected.
38     private final Monitor mConditionMonitor;
39     private final DreamCondition mDreamCondition;
40     private final DreamStatusBarStateCallback mCallback;
41     private RestartDozeListener mRestartDozeListener;
42 
43     @Inject
DreamMonitor(@ystemUser Monitor monitor, DreamCondition dreamCondition, DreamStatusBarStateCallback callback, RestartDozeListener restartDozeListener)44     public DreamMonitor(@SystemUser Monitor monitor, DreamCondition dreamCondition,
45             DreamStatusBarStateCallback callback,
46             RestartDozeListener restartDozeListener) {
47         super(monitor);
48         mConditionMonitor = monitor;
49         mDreamCondition = dreamCondition;
50         mCallback = callback;
51         mRestartDozeListener = restartDozeListener;
52     }
53 
54     @Override
onStart()55     protected void onStart() {
56         if (Log.isLoggable(TAG, Log.DEBUG)) {
57             Log.d(TAG, "started");
58         }
59 
60         mConditionMonitor.addSubscription(new Monitor.Subscription.Builder(mCallback)
61                 .addCondition(mDreamCondition)
62                 .build());
63 
64         mRestartDozeListener.init();
65         mRestartDozeListener.maybeRestartSleep();
66     }
67 }
68