• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Trace;
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     private static final String TAG = "KeyguardLifecyclesDispatcher";
43 
44     private final ScreenLifecycle mScreenLifecycle;
45     private final WakefulnessLifecycle mWakefulnessLifecycle;
46 
47     @Inject
KeyguardLifecyclesDispatcher(ScreenLifecycle screenLifecycle, WakefulnessLifecycle wakefulnessLifecycle)48     public KeyguardLifecyclesDispatcher(ScreenLifecycle screenLifecycle,
49             WakefulnessLifecycle wakefulnessLifecycle) {
50         mScreenLifecycle = screenLifecycle;
51         mWakefulnessLifecycle = wakefulnessLifecycle;
52     }
53 
dispatch(int what)54     void dispatch(int what) {
55         mHandler.obtainMessage(what).sendToTarget();
56     }
57 
58     /**
59      * @param what Message to send.
60      * @param pmReason Reason this message was triggered - this should be a value from either
61      * {@link PowerManager.WakeReason} or {@link PowerManager.GoToSleepReason}.
62      */
dispatch(int what, int pmReason)63     void dispatch(int what, int pmReason) {
64         final Message message = mHandler.obtainMessage(what);
65         message.arg1 = pmReason;
66         message.sendToTarget();
67     }
68 
69     /**
70      * @param what Message to send.
71      * @param object Object to send with the message
72      */
dispatch(int what, Object object)73     void dispatch(int what, Object object) {
74         mHandler.obtainMessage(what, object).sendToTarget();
75     }
76 
77     private Handler mHandler = new Handler() {
78         @Override
79         public void handleMessage(Message msg) {
80             switch (msg.what) {
81                 case SCREEN_TURNING_ON:
82                     Trace.beginSection("KeyguardLifecyclesDispatcher#SCREEN_TURNING_ON");
83                     mScreenLifecycle.dispatchScreenTurningOn();
84                     Trace.endSection();
85                     break;
86                 case SCREEN_TURNED_ON:
87                     mScreenLifecycle.dispatchScreenTurnedOn();
88                     break;
89                 case SCREEN_TURNING_OFF:
90                     mScreenLifecycle.dispatchScreenTurningOff();
91                     break;
92                 case SCREEN_TURNED_OFF:
93                     mScreenLifecycle.dispatchScreenTurnedOff();
94                     break;
95                 case STARTED_WAKING_UP:
96                     mWakefulnessLifecycle.dispatchStartedWakingUp(msg.arg1 /* pmReason */);
97                     break;
98                 case FINISHED_WAKING_UP:
99                     mWakefulnessLifecycle.dispatchFinishedWakingUp();
100                     break;
101                 case STARTED_GOING_TO_SLEEP:
102                     mWakefulnessLifecycle.dispatchStartedGoingToSleep(msg.arg1 /* pmReason */);
103                     break;
104                 case FINISHED_GOING_TO_SLEEP:
105                     mWakefulnessLifecycle.dispatchFinishedGoingToSleep();
106                     break;
107                 default:
108                     throw new IllegalArgumentException("Unknown message: " + msg);
109             }
110         }
111     };
112 
113 }
114