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.touch.scrim;
18 
19 import android.os.PowerManager;
20 import android.os.SystemClock;
21 
22 import com.android.systemui.dagger.SysUISingleton;
23 import com.android.systemui.dagger.qualifiers.Main;
24 import com.android.systemui.shade.ShadeExpansionChangeEvent;
25 import com.android.systemui.unfold.util.CallbackController;
26 
27 import java.util.HashSet;
28 import java.util.concurrent.Executor;
29 
30 import javax.inject.Inject;
31 
32 /**
33  * {@link BouncerlessScrimController} handles scrim progression when no keyguard is set. When
34  * fully expanded, the controller dismisses the dream.
35  */
36 @SysUISingleton
37 public class BouncerlessScrimController implements ScrimController,
38         CallbackController<BouncerlessScrimController.Callback> {
39     private static final String TAG = "BLScrimController";
40 
41     /**
42      * {@link Callback} allows {@link BouncerlessScrimController} clients to be informed of
43      * expansion progression and wakeup
44      */
45     public interface Callback {
46         /**
47          * Invoked when there is a change to the scrim expansion.
48          */
onExpansion(ShadeExpansionChangeEvent event)49         void onExpansion(ShadeExpansionChangeEvent event);
50 
51         /**
52          * Invoked after {@link BouncerlessScrimController} has started waking up the device.
53          */
onWakeup()54         void onWakeup();
55     }
56 
57     private final Executor mExecutor;
58     private final PowerManager mPowerManager;
59 
60     @Override
addCallback(Callback listener)61     public void addCallback(Callback listener) {
62         mExecutor.execute(() -> mCallbacks.add(listener));
63     }
64 
65     @Override
removeCallback(Callback listener)66     public void removeCallback(Callback listener) {
67         mExecutor.execute(() -> mCallbacks.remove(listener));
68     }
69 
70     private final HashSet<Callback> mCallbacks;
71 
72 
73     @Inject
BouncerlessScrimController(@ain Executor executor, PowerManager powerManager)74     public BouncerlessScrimController(@Main Executor executor,
75             PowerManager powerManager) {
76         mExecutor = executor;
77         mPowerManager = powerManager;
78         mCallbacks = new HashSet<>();
79     }
80 
81     @Override
expand(ShadeExpansionChangeEvent event)82     public void expand(ShadeExpansionChangeEvent event) {
83         if (event.getExpanded())  {
84             mPowerManager.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE,
85                     "com.android.systemui:SwipeUp");
86             mExecutor.execute(() -> mCallbacks.forEach(callback -> callback.onWakeup()));
87         } else {
88             mExecutor.execute(() -> mCallbacks.forEach(callback -> callback.onExpansion(event)));
89         }
90     }
91 }
92