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