Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 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 | |
Lucas Dupin | 7a0e5bc | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Adrian Roos | 51465ea | 2017-07-27 13:57:35 +0200 | [diff] [blame] | 20 | import android.os.Trace; |
| 21 | |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 22 | import com.android.systemui.Dumpable; |
| 23 | |
| 24 | import java.io.FileDescriptor; |
| 25 | import java.io.PrintWriter; |
Lucas Dupin | 7a0e5bc | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 26 | import java.lang.annotation.Retention; |
| 27 | import java.lang.annotation.RetentionPolicy; |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 28 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 32 | /** |
| 33 | * Tracks the wakefulness lifecycle. |
| 34 | */ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 35 | @Singleton |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 36 | public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observer> implements |
| 37 | Dumpable { |
| 38 | |
Lucas Dupin | 7a0e5bc | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 39 | @IntDef(prefix = { "WAKEFULNESS_" }, value = { |
| 40 | WAKEFULNESS_ASLEEP, |
| 41 | WAKEFULNESS_WAKING, |
| 42 | WAKEFULNESS_AWAKE, |
| 43 | WAKEFULNESS_GOING_TO_SLEEP, |
| 44 | }) |
| 45 | @Retention(RetentionPolicy.SOURCE) |
| 46 | public @interface Wakefulness {} |
| 47 | |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 48 | public static final int WAKEFULNESS_ASLEEP = 0; |
| 49 | public static final int WAKEFULNESS_WAKING = 1; |
| 50 | public static final int WAKEFULNESS_AWAKE = 2; |
| 51 | public static final int WAKEFULNESS_GOING_TO_SLEEP = 3; |
| 52 | |
| 53 | private int mWakefulness = WAKEFULNESS_ASLEEP; |
| 54 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 55 | @Inject |
| 56 | public WakefulnessLifecycle() { |
| 57 | } |
| 58 | |
Lucas Dupin | 7a0e5bc | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 59 | public @Wakefulness int getWakefulness() { |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 60 | return mWakefulness; |
| 61 | } |
| 62 | |
| 63 | public void dispatchStartedWakingUp() { |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 64 | if (getWakefulness() == WAKEFULNESS_WAKING) { |
| 65 | return; |
| 66 | } |
Adrian Roos | 51465ea | 2017-07-27 13:57:35 +0200 | [diff] [blame] | 67 | setWakefulness(WAKEFULNESS_WAKING); |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 68 | dispatch(Observer::onStartedWakingUp); |
| 69 | } |
| 70 | |
| 71 | public void dispatchFinishedWakingUp() { |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 72 | if (getWakefulness() == WAKEFULNESS_AWAKE) { |
| 73 | return; |
| 74 | } |
Adrian Roos | 51465ea | 2017-07-27 13:57:35 +0200 | [diff] [blame] | 75 | setWakefulness(WAKEFULNESS_AWAKE); |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 76 | dispatch(Observer::onFinishedWakingUp); |
| 77 | } |
| 78 | |
| 79 | public void dispatchStartedGoingToSleep() { |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 80 | if (getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP) { |
| 81 | return; |
| 82 | } |
Adrian Roos | 51465ea | 2017-07-27 13:57:35 +0200 | [diff] [blame] | 83 | setWakefulness(WAKEFULNESS_GOING_TO_SLEEP); |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 84 | dispatch(Observer::onStartedGoingToSleep); |
| 85 | } |
| 86 | |
| 87 | public void dispatchFinishedGoingToSleep() { |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 88 | if (getWakefulness() == WAKEFULNESS_ASLEEP) { |
| 89 | return; |
| 90 | } |
Adrian Roos | 51465ea | 2017-07-27 13:57:35 +0200 | [diff] [blame] | 91 | setWakefulness(WAKEFULNESS_ASLEEP); |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 92 | dispatch(Observer::onFinishedGoingToSleep); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 97 | pw.println("WakefulnessLifecycle:"); |
| 98 | pw.println(" mWakefulness=" + mWakefulness); |
| 99 | } |
| 100 | |
Lucas Dupin | 7a0e5bc | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 101 | private void setWakefulness(@Wakefulness int wakefulness) { |
Adrian Roos | 51465ea | 2017-07-27 13:57:35 +0200 | [diff] [blame] | 102 | mWakefulness = wakefulness; |
| 103 | Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness); |
| 104 | } |
| 105 | |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 106 | public interface Observer { |
| 107 | default void onStartedWakingUp() {} |
| 108 | default void onFinishedWakingUp() {} |
| 109 | default void onStartedGoingToSleep() {} |
| 110 | default void onFinishedGoingToSleep() {} |
| 111 | } |
| 112 | } |