• 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.annotation.IntDef;
20 import android.app.IWallpaperManager;
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.graphics.Point;
24 import android.os.Bundle;
25 import android.os.PowerManager;
26 import android.os.RemoteException;
27 import android.os.Trace;
28 import android.util.DisplayMetrics;
29 
30 import androidx.annotation.Nullable;
31 
32 import com.android.systemui.Dumpable;
33 import com.android.systemui.dagger.SysUISingleton;
34 import com.android.systemui.dump.DumpManager;
35 import com.android.systemui.power.domain.interactor.PowerInteractor;
36 import com.android.systemui.res.R;
37 import com.android.systemui.util.time.SystemClock;
38 
39 import java.io.PrintWriter;
40 import java.lang.annotation.Retention;
41 import java.lang.annotation.RetentionPolicy;
42 
43 import javax.inject.Inject;
44 
45 /**
46  * Tracks the wakefulness lifecycle, including why we're waking or sleeping.
47  *
48  * @deprecated Collect flows from {@link PowerInteractor} instead.
49  */
50 @SysUISingleton
51 @Deprecated
52 public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observer> implements
53         Dumpable {
54 
55     @IntDef(prefix = { "WAKEFULNESS_" }, value = {
56             WAKEFULNESS_ASLEEP,
57             WAKEFULNESS_WAKING,
58             WAKEFULNESS_AWAKE,
59             WAKEFULNESS_GOING_TO_SLEEP,
60     })
61     @Retention(RetentionPolicy.SOURCE)
62     public @interface Wakefulness {}
63 
64     public static final int WAKEFULNESS_ASLEEP = 0;
65     public static final int WAKEFULNESS_WAKING = 1;
66     public static final int WAKEFULNESS_AWAKE = 2;
67     public static final int WAKEFULNESS_GOING_TO_SLEEP = 3;
68 
69     private final Context mContext;
70     private final DisplayMetrics mDisplayMetrics;
71     private final SystemClock mSystemClock;
72 
73     @Nullable
74     private final IWallpaperManager mWallpaperManagerService;
75 
76     private int mWakefulness = WAKEFULNESS_AWAKE;
77 
78     private @PowerManager.WakeReason int mLastWakeReason = PowerManager.WAKE_REASON_UNKNOWN;
79 
80     public static final long UNKNOWN_LAST_WAKE_TIME = -1;
81     private long mLastWakeTime = UNKNOWN_LAST_WAKE_TIME;
82 
83     @Nullable
84     private Point mLastWakeOriginLocation = null;
85 
86     private @PowerManager.GoToSleepReason int mLastSleepReason =
87             PowerManager.GO_TO_SLEEP_REASON_MIN;
88 
89     @Nullable
90     private Point mLastSleepOriginLocation = null;
91 
92     @Inject
WakefulnessLifecycle( Context context, @Nullable IWallpaperManager wallpaperManagerService, SystemClock systemClock, DumpManager dumpManager)93     public WakefulnessLifecycle(
94             Context context,
95             @Nullable IWallpaperManager wallpaperManagerService,
96             SystemClock systemClock,
97             DumpManager dumpManager) {
98         mContext = context;
99         mDisplayMetrics = context.getResources().getDisplayMetrics();
100         mWallpaperManagerService = wallpaperManagerService;
101         mSystemClock = systemClock;
102 
103         dumpManager.registerDumpable(getClass().getSimpleName(), this);
104     }
105 
getWakefulness()106     public @Wakefulness int getWakefulness() {
107         return mWakefulness;
108     }
109 
110     /**
111      * Returns the most recent reason the device woke up. This is one of PowerManager.WAKE_REASON_*.
112      */
getLastWakeReason()113     public @PowerManager.WakeReason int getLastWakeReason() {
114         return mLastWakeReason;
115     }
116 
117     /**
118      * Returns the most recent time (in device uptimeMillis) the display woke up.
119      * Returns {@link UNKNOWN_LAST_WAKE_TIME} if there hasn't been a wakeup yet.
120      */
getLastWakeTime()121     public long getLastWakeTime() {
122         return mLastWakeTime;
123     }
124 
125     /**
126      * Returns the most recent reason the device went to sleep up. This is one of
127      * PowerManager.GO_TO_SLEEP_REASON_*.
128      */
getLastSleepReason()129     public @PowerManager.GoToSleepReason int getLastSleepReason() {
130         return mLastSleepReason;
131     }
132 
dispatchStartedWakingUp(@owerManager.WakeReason int pmWakeReason)133     public void dispatchStartedWakingUp(@PowerManager.WakeReason int pmWakeReason) {
134         if (getWakefulness() == WAKEFULNESS_WAKING) {
135             return;
136         }
137         setWakefulness(WAKEFULNESS_WAKING);
138         mLastWakeReason = pmWakeReason;
139         mLastWakeTime = mSystemClock.uptimeMillis();
140         updateLastWakeOriginLocation();
141 
142         if (mWallpaperManagerService != null) {
143             try {
144                 mWallpaperManagerService.notifyWakingUp(
145                         mLastWakeOriginLocation.x, mLastWakeOriginLocation.y, new Bundle());
146             } catch (RemoteException e) {
147                 e.printStackTrace();
148             }
149         }
150 
151         dispatch(Observer::onStartedWakingUp);
152     }
153 
dispatchFinishedWakingUp()154     public void dispatchFinishedWakingUp() {
155         if (getWakefulness() == WAKEFULNESS_AWAKE) {
156             return;
157         }
158         setWakefulness(WAKEFULNESS_AWAKE);
159         dispatch(Observer::onFinishedWakingUp);
160         dispatch(Observer::onPostFinishedWakingUp);
161     }
162 
dispatchStartedGoingToSleep(@owerManager.GoToSleepReason int pmSleepReason)163     public void dispatchStartedGoingToSleep(@PowerManager.GoToSleepReason int pmSleepReason) {
164         if (getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP) {
165             return;
166         }
167         setWakefulness(WAKEFULNESS_GOING_TO_SLEEP);
168         mLastSleepReason = pmSleepReason;
169         updateLastSleepOriginLocation();
170 
171         if (mWallpaperManagerService != null) {
172             try {
173                 mWallpaperManagerService.notifyGoingToSleep(
174                         mLastSleepOriginLocation.x, mLastSleepOriginLocation.y, new Bundle());
175             } catch (RemoteException e) {
176                 e.printStackTrace();
177             }
178         }
179 
180         dispatch(Observer::onStartedGoingToSleep);
181     }
182 
dispatchFinishedGoingToSleep()183     public void dispatchFinishedGoingToSleep() {
184         if (getWakefulness() == WAKEFULNESS_ASLEEP) {
185             return;
186         }
187         setWakefulness(WAKEFULNESS_ASLEEP);
188         dispatch(Observer::onFinishedGoingToSleep);
189     }
190 
191     @Override
dump(PrintWriter pw, String[] args)192     public void dump(PrintWriter pw, String[] args) {
193         pw.println("WakefulnessLifecycle:");
194         pw.println("  mWakefulness=" + mWakefulness);
195     }
196 
setWakefulness(@akefulness int wakefulness)197     private void setWakefulness(@Wakefulness int wakefulness) {
198         mWakefulness = wakefulness;
199         Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness);
200     }
201 
updateLastWakeOriginLocation()202     private void updateLastWakeOriginLocation() {
203         mLastWakeOriginLocation = null;
204 
205         switch (mLastWakeReason) {
206             case PowerManager.WAKE_REASON_POWER_BUTTON:
207                 mLastWakeOriginLocation = getPowerButtonOrigin();
208                 break;
209             default:
210                 mLastWakeOriginLocation = getDefaultWakeSleepOrigin();
211                 break;
212         }
213     }
214 
updateLastSleepOriginLocation()215     private void updateLastSleepOriginLocation() {
216         mLastSleepOriginLocation = null;
217 
218         switch (mLastSleepReason) {
219             case PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON:
220                 mLastSleepOriginLocation = getPowerButtonOrigin();
221                 break;
222             default:
223                 mLastSleepOriginLocation = getDefaultWakeSleepOrigin();
224                 break;
225         }
226     }
227 
228     /**
229      * Returns the point on the screen closest to the physical power button.
230      */
getPowerButtonOrigin()231     private Point getPowerButtonOrigin() {
232         final boolean isPortrait = mContext.getResources().getConfiguration().orientation
233                 == Configuration.ORIENTATION_PORTRAIT;
234 
235         if (isPortrait) {
236             return new Point(
237                     mDisplayMetrics.widthPixels,
238                     mContext.getResources().getDimensionPixelSize(
239                             R.dimen.physical_power_button_center_screen_location_y));
240         } else {
241             return new Point(
242                     mContext.getResources().getDimensionPixelSize(
243                             R.dimen.physical_power_button_center_screen_location_y),
244                     mDisplayMetrics.heightPixels);
245         }
246     }
247 
248     /**
249      * Returns the point on the screen used as the default origin for wake/sleep events. This is the
250      * middle-bottom of the screen.
251      */
getDefaultWakeSleepOrigin()252     private Point getDefaultWakeSleepOrigin() {
253         return new Point(mDisplayMetrics.widthPixels / 2, mDisplayMetrics.heightPixels);
254     }
255 
256     public interface Observer {
onStartedWakingUp()257         default void onStartedWakingUp() {}
onFinishedWakingUp()258         default void onFinishedWakingUp() {}
259 
260         /**
261          * Called after the finished waking up call, ensuring it's after all the other listeners,
262          * reacting to {@link #onFinishedWakingUp()}
263          */
onPostFinishedWakingUp()264         default void onPostFinishedWakingUp() {}
onStartedGoingToSleep()265         default void onStartedGoingToSleep() {}
onFinishedGoingToSleep()266         default void onFinishedGoingToSleep() {}
267     }
268 }
269