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.Trace;
20 
21 import com.android.systemui.Dumpable;
22 
23 import java.io.FileDescriptor;
24 import java.io.PrintWriter;
25 
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 
29 /**
30  * Tracks the screen lifecycle.
31  */
32 @Singleton
33 public class ScreenLifecycle extends Lifecycle<ScreenLifecycle.Observer> implements Dumpable {
34 
35     public static final int SCREEN_OFF = 0;
36     public static final int SCREEN_TURNING_ON = 1;
37     public static final int SCREEN_ON = 2;
38     public static final int SCREEN_TURNING_OFF = 3;
39 
40     private int mScreenState = SCREEN_OFF;
41 
42     @Inject
ScreenLifecycle()43     public ScreenLifecycle() {
44     }
45 
getScreenState()46     public int getScreenState() {
47         return mScreenState;
48     }
49 
dispatchScreenTurningOn()50     public void dispatchScreenTurningOn() {
51         setScreenState(SCREEN_TURNING_ON);
52         dispatch(Observer::onScreenTurningOn);
53     }
54 
dispatchScreenTurnedOn()55     public void dispatchScreenTurnedOn() {
56         setScreenState(SCREEN_ON);
57         dispatch(Observer::onScreenTurnedOn);
58     }
59 
dispatchScreenTurningOff()60     public void dispatchScreenTurningOff() {
61         setScreenState(SCREEN_TURNING_OFF);
62         dispatch(Observer::onScreenTurningOff);
63     }
64 
dispatchScreenTurnedOff()65     public void dispatchScreenTurnedOff() {
66         setScreenState(SCREEN_OFF);
67         dispatch(Observer::onScreenTurnedOff);
68     }
69 
70     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)71     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
72         pw.println("ScreenLifecycle:");
73         pw.println("  mScreenState=" + mScreenState);
74     }
75 
setScreenState(int screenState)76     private void setScreenState(int screenState) {
77         mScreenState = screenState;
78         Trace.traceCounter(Trace.TRACE_TAG_APP, "screenState", screenState);
79     }
80 
81     public interface Observer {
onScreenTurningOn()82         default void onScreenTurningOn() {}
onScreenTurnedOn()83         default void onScreenTurnedOn() {}
onScreenTurningOff()84         default void onScreenTurningOff() {}
onScreenTurnedOff()85         default void onScreenTurnedOff() {}
86     }
87 
88 }
89