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 import com.android.systemui.dump.DumpManager; 23 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(DumpManager dumpManager)43 public ScreenLifecycle(DumpManager dumpManager) { 44 dumpManager.registerDumpable(getClass().getSimpleName(), this); 45 } 46 getScreenState()47 public int getScreenState() { 48 return mScreenState; 49 } 50 dispatchScreenTurningOn()51 public void dispatchScreenTurningOn() { 52 setScreenState(SCREEN_TURNING_ON); 53 dispatch(Observer::onScreenTurningOn); 54 } 55 dispatchScreenTurnedOn()56 public void dispatchScreenTurnedOn() { 57 setScreenState(SCREEN_ON); 58 dispatch(Observer::onScreenTurnedOn); 59 } 60 dispatchScreenTurningOff()61 public void dispatchScreenTurningOff() { 62 setScreenState(SCREEN_TURNING_OFF); 63 dispatch(Observer::onScreenTurningOff); 64 } 65 dispatchScreenTurnedOff()66 public void dispatchScreenTurnedOff() { 67 setScreenState(SCREEN_OFF); 68 dispatch(Observer::onScreenTurnedOff); 69 } 70 71 @Override dump(PrintWriter pw, String[] args)72 public void dump(PrintWriter pw, String[] args) { 73 pw.println("ScreenLifecycle:"); 74 pw.println(" mScreenState=" + mScreenState); 75 } 76 setScreenState(int screenState)77 private void setScreenState(int screenState) { 78 mScreenState = screenState; 79 Trace.traceCounter(Trace.TRACE_TAG_APP, "screenState", screenState); 80 } 81 82 public interface Observer { onScreenTurningOn()83 default void onScreenTurningOn() {} onScreenTurnedOn()84 default void onScreenTurnedOn() {} onScreenTurningOff()85 default void onScreenTurningOff() {} onScreenTurnedOff()86 default void onScreenTurnedOff() {} 87 } 88 89 } 90