1 /*
2  * Copyright (C) 2017 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.keyguard;
18 
19 import android.os.Handler;
20 import android.os.Message;
21 import android.os.PowerManager;
22 
23 import com.android.systemui.dagger.SysUISingleton;
24 
25 import javax.inject.Inject;
26 
27 /**
28  * Dispatches the lifecycles keyguard gets from WindowManager on the main thread.
29  */
30 @SysUISingleton
31 public class KeyguardLifecyclesDispatcher {
32 
33     static final int SCREEN_TURNING_ON = 0;
34     static final int SCREEN_TURNED_ON = 1;
35     static final int SCREEN_TURNING_OFF = 2;
36     static final int SCREEN_TURNED_OFF = 3;
37 
38     static final int STARTED_WAKING_UP = 4;
39     static final int FINISHED_WAKING_UP = 5;
40     static final int STARTED_GOING_TO_SLEEP = 6;
41     static final int FINISHED_GOING_TO_SLEEP = 7;
42 
43     private final ScreenLifecycle mScreenLifecycle;
44     private final WakefulnessLifecycle mWakefulnessLifecycle;
45 
46     @Inject
KeyguardLifecyclesDispatcher(ScreenLifecycle screenLifecycle, WakefulnessLifecycle wakefulnessLifecycle)47     public KeyguardLifecyclesDispatcher(ScreenLifecycle screenLifecycle,
48             WakefulnessLifecycle wakefulnessLifecycle) {
49         mScreenLifecycle = screenLifecycle;
50         mWakefulnessLifecycle = wakefulnessLifecycle;
51     }
52 
dispatch(int what)53     void dispatch(int what) {
54         mHandler.obtainMessage(what).sendToTarget();
55     }
56 
57     /**
58      * @param what Message to send.
59      * @param pmReason Reason this message was triggered - this should be a value from either
60      * {@link PowerManager.WakeReason} or {@link PowerManager.GoToSleepReason}.
61      */
dispatch(int what, int pmReason)62     void dispatch(int what, int pmReason) {
63         final Message message = mHandler.obtainMessage(what);
64         message.arg1 = pmReason;
65         message.sendToTarget();
66     }
67 
68     private Handler mHandler = new Handler() {
69         @Override
70         public void handleMessage(Message msg) {
71             switch (msg.what) {
72                 case SCREEN_TURNING_ON:
73                     mScreenLifecycle.dispatchScreenTurningOn();
74                     break;
75                 case SCREEN_TURNED_ON:
76                     mScreenLifecycle.dispatchScreenTurnedOn();
77                     break;
78                 case SCREEN_TURNING_OFF:
79                     mScreenLifecycle.dispatchScreenTurningOff();
80                     break;
81                 case SCREEN_TURNED_OFF:
82                     mScreenLifecycle.dispatchScreenTurnedOff();
83                     break;
84                 case STARTED_WAKING_UP:
85                     mWakefulnessLifecycle.dispatchStartedWakingUp(msg.arg1 /* pmReason */);
86                     break;
87                 case FINISHED_WAKING_UP:
88                     mWakefulnessLifecycle.dispatchFinishedWakingUp();
89                     break;
90                 case STARTED_GOING_TO_SLEEP:
91                     mWakefulnessLifecycle.dispatchStartedGoingToSleep(msg.arg1 /* pmReason */);
92                     break;
93                 case FINISHED_GOING_TO_SLEEP:
94                     mWakefulnessLifecycle.dispatchFinishedGoingToSleep();
95                     break;
96                 default:
97                     throw new IllegalArgumentException("Unknown message: " + msg);
98             }
99         }
100     };
101 
102 }
103