1 /*
2  * Copyright (C) 2014 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 android.os;
18 
19 import android.view.Display;
20 import android.view.KeyEvent;
21 
22 import java.util.function.Consumer;
23 
24 /**
25  * Power manager local system service interface.
26  *
27  * @hide Only for use within the system server.
28  */
29 public abstract class PowerManagerInternal {
30     /**
31      * Wakefulness: The device is asleep.  It can only be awoken by a call to wakeUp().
32      * The screen should be off or in the process of being turned off by the display controller.
33      * The device typically passes through the dozing state first.
34      */
35     public static final int WAKEFULNESS_ASLEEP = 0;
36 
37     /**
38      * Wakefulness: The device is fully awake.  It can be put to sleep by a call to goToSleep().
39      * When the user activity timeout expires, the device may start dreaming or go to sleep.
40      */
41     public static final int WAKEFULNESS_AWAKE = 1;
42 
43     /**
44      * Wakefulness: The device is dreaming.  It can be awoken by a call to wakeUp(),
45      * which ends the dream.  The device goes to sleep when goToSleep() is called, when
46      * the dream ends or when unplugged.
47      * User activity may brighten the screen but does not end the dream.
48      */
49     public static final int WAKEFULNESS_DREAMING = 2;
50 
51     /**
52      * Wakefulness: The device is dozing.  It is almost asleep but is allowing a special
53      * low-power "doze" dream to run which keeps the display on but lets the application
54      * processor be suspended.  It can be awoken by a call to wakeUp() which ends the dream.
55      * The device fully goes to sleep if the dream cannot be started or ends on its own.
56      */
57     public static final int WAKEFULNESS_DOZING = 3;
58 
wakefulnessToString(int wakefulness)59     public static String wakefulnessToString(int wakefulness) {
60         switch (wakefulness) {
61             case WAKEFULNESS_ASLEEP:
62                 return "Asleep";
63             case WAKEFULNESS_AWAKE:
64                 return "Awake";
65             case WAKEFULNESS_DREAMING:
66                 return "Dreaming";
67             case WAKEFULNESS_DOZING:
68                 return "Dozing";
69             default:
70                 return Integer.toString(wakefulness);
71         }
72     }
73 
74     /**
75      * Converts platform constants to proto enums.
76      */
wakefulnessToProtoEnum(int wakefulness)77     public static int wakefulnessToProtoEnum(int wakefulness) {
78         switch (wakefulness) {
79             case WAKEFULNESS_ASLEEP:
80                 return PowerManagerInternalProto.WAKEFULNESS_ASLEEP;
81             case WAKEFULNESS_AWAKE:
82                 return PowerManagerInternalProto.WAKEFULNESS_AWAKE;
83             case WAKEFULNESS_DREAMING:
84                 return PowerManagerInternalProto.WAKEFULNESS_DREAMING;
85             case WAKEFULNESS_DOZING:
86                 return PowerManagerInternalProto.WAKEFULNESS_DOZING;
87             default:
88                 return wakefulness;
89         }
90     }
91 
92     /**
93      * Returns true if the wakefulness state represents an interactive state
94      * as defined by {@link android.os.PowerManager#isInteractive}.
95      */
isInteractive(int wakefulness)96     public static boolean isInteractive(int wakefulness) {
97         return wakefulness == WAKEFULNESS_AWAKE || wakefulness == WAKEFULNESS_DREAMING;
98     }
99 
100     /**
101      * Used by the window manager to override the screen brightness based on the
102      * current foreground activity.
103      *
104      * This method must only be called by the window manager.
105      *
106      * @param brightness The overridden brightness, or Float.NaN to disable the override.
107      */
setScreenBrightnessOverrideFromWindowManager(float brightness)108     public abstract void setScreenBrightnessOverrideFromWindowManager(float brightness);
109 
110     /**
111      * Used by the window manager to override the user activity timeout based on the
112      * current foreground activity.  It can only be used to make the timeout shorter
113      * than usual, not longer.
114      *
115      * This method must only be called by the window manager.
116      *
117      * @param timeoutMillis The overridden timeout, or -1 to disable the override.
118      */
setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis)119     public abstract void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis);
120 
121     /**
122      * Used by the window manager to tell the power manager that the user is no longer actively
123      * using the device.
124      */
setUserInactiveOverrideFromWindowManager()125     public abstract void setUserInactiveOverrideFromWindowManager();
126 
127     /**
128      * Used by device administration to set the maximum screen off timeout.
129      *
130      * This method must only be called by the device administration policy manager.
131      */
setMaximumScreenOffTimeoutFromDeviceAdmin(int userId, long timeMs)132     public abstract void setMaximumScreenOffTimeoutFromDeviceAdmin(int userId, long timeMs);
133 
134     /**
135      * Used by the dream manager to override certain properties while dozing.
136      *
137      * @param screenState The overridden screen state, or {@link Display#STATE_UNKNOWN}
138      * to disable the override.
139      * @param reason The reason for overriding the screen state.
140      * @param screenBrightness The overridden screen brightness, or
141      * {@link PowerManager#BRIGHTNESS_DEFAULT} to disable the override.
142      */
setDozeOverrideFromDreamManager( int screenState, @Display.StateReason int reason, int screenBrightness)143     public abstract void setDozeOverrideFromDreamManager(
144             int screenState, @Display.StateReason int reason, int screenBrightness);
145 
146     /**
147      * Used by sidekick manager to tell the power manager if it shouldn't change the display state
148      * when a draw wake lock is acquired. Some processes may grab such a wake lock to do some work
149      * in a powered-up state, but we shouldn't give up sidekick control over the display until this
150      * override is lifted.
151      */
setDrawWakeLockOverrideFromSidekick(boolean keepState)152     public abstract void setDrawWakeLockOverrideFromSidekick(boolean keepState);
153 
getLowPowerState(int serviceType)154     public abstract PowerSaveState getLowPowerState(int serviceType);
155 
registerLowPowerModeObserver(LowPowerModeListener listener)156     public abstract void registerLowPowerModeObserver(LowPowerModeListener listener);
157 
158     /**
159      * Same as {@link #registerLowPowerModeObserver} but can take a lambda.
160      */
registerLowPowerModeObserver(int serviceType, Consumer<PowerSaveState> listener)161     public void registerLowPowerModeObserver(int serviceType, Consumer<PowerSaveState> listener) {
162         registerLowPowerModeObserver(new LowPowerModeListener() {
163             @Override
164             public int getServiceType() {
165                 return serviceType;
166             }
167 
168             @Override
169             public void onLowPowerModeChanged(PowerSaveState state) {
170                 listener.accept(state);
171             }
172         });
173     }
174 
175     public interface LowPowerModeListener {
getServiceType()176         int getServiceType();
onLowPowerModeChanged(PowerSaveState state)177         void onLowPowerModeChanged(PowerSaveState state);
178     }
179 
setDeviceIdleMode(boolean enabled)180     public abstract boolean setDeviceIdleMode(boolean enabled);
181 
setLightDeviceIdleMode(boolean enabled)182     public abstract boolean setLightDeviceIdleMode(boolean enabled);
183 
setDeviceIdleWhitelist(int[] appids)184     public abstract void setDeviceIdleWhitelist(int[] appids);
185 
setDeviceIdleTempWhitelist(int[] appids)186     public abstract void setDeviceIdleTempWhitelist(int[] appids);
187 
188     /**
189      * Updates the Low Power Standby allowlist.
190      *
191      * @param uids UIDs that are exempt from Low Power Standby restrictions
192      */
setLowPowerStandbyAllowlist(int[] uids)193     public abstract void setLowPowerStandbyAllowlist(int[] uids);
194 
195     /**
196      * Used by LowPowerStandbyController to notify the power manager that Low Power Standby's
197      * active state has changed.
198      *
199      * @param active {@code true} to activate Low Power Standby, {@code false} to turn it off.
200      */
setLowPowerStandbyActive(boolean active)201     public abstract void setLowPowerStandbyActive(boolean active);
202 
startUidChanges()203     public abstract void startUidChanges();
204 
finishUidChanges()205     public abstract void finishUidChanges();
206 
updateUidProcState(int uid, int procState)207     public abstract void updateUidProcState(int uid, int procState);
208 
uidGone(int uid)209     public abstract void uidGone(int uid);
210 
uidActive(int uid)211     public abstract void uidActive(int uid);
212 
uidIdle(int uid)213     public abstract void uidIdle(int uid);
214 
215     /**
216      * Boost: It is sent when user interacting with the device, for example,
217      * touchscreen events are incoming.
218      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl
219      */
220     public static final int BOOST_INTERACTION = 0;
221 
222     /**
223      * Boost: It indicates that the framework is likely to provide a new display
224      * frame soon. This implies that the device should ensure that the display
225      * processing path is powered up and ready to receive that update.
226      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl
227      */
228     public static final int BOOST_DISPLAY_UPDATE_IMMINENT = 1;
229 
230     /**
231      * SetPowerBoost() indicates the device may need to boost some resources, as
232      * the load is likely to increase before the kernel governors can react.
233      * Depending on the boost, it may be appropriate to raise the frequencies of
234      * CPU, GPU, memory subsystem, or stop CPU from going into deep sleep state.
235      *
236      * @param boost Boost which is to be set with a timeout.
237      * @param durationMs The expected duration of the user's interaction, if
238      *        known, or 0 if the expected duration is unknown.
239      *        a negative value indicates canceling previous boost.
240      *        A given platform can choose to boost some time based on durationMs,
241      *        and may also pick an appropriate timeout for 0 case.
242      */
setPowerBoost(int boost, int durationMs)243     public abstract void setPowerBoost(int boost, int durationMs);
244 
245     /**
246      * Mode: It indicates that the device is to allow wake up when the screen
247      * is tapped twice.
248      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
249      */
250     public static final int MODE_DOUBLE_TAP_TO_WAKE = 0;
251 
252     /**
253      * Mode: It indicates Low power mode is activated or not. Low power mode
254      * is intended to save battery at the cost of performance.
255      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
256      */
257     public static final int MODE_LOW_POWER = 1;
258 
259     /**
260      * Mode: It indicates Sustained Performance mode is activated or not.
261      * Sustained performance mode is intended to provide a consistent level of
262      * performance for a prolonged amount of time.
263      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
264      */
265     public static final int MODE_SUSTAINED_PERFORMANCE = 2;
266 
267     /**
268      * Mode: It sets the device to a fixed performance level which can be sustained
269      * under normal indoor conditions for at least 10 minutes.
270      * Fixed performance mode puts both upper and lower bounds on performance such
271      * that any workload run while in a fixed performance mode should complete in
272      * a repeatable amount of time.
273      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
274      */
275     public static final int MODE_FIXED_PERFORMANCE = 3;
276 
277     /**
278      * Mode: It indicates VR Mode is activated or not. VR mode is intended to
279      * provide minimum guarantee for performance for the amount of time the device
280      * can sustain it.
281      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
282      */
283     public static final int MODE_VR = 4;
284 
285     /**
286      * Mode: It indicates that an application has been launched.
287      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
288      */
289     public static final int MODE_LAUNCH = 5;
290 
291     /**
292      * Mode: It indicates that the device is about to enter a period of expensive
293      * rendering.
294      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
295      */
296     public static final int MODE_EXPENSIVE_RENDERING = 6;
297 
298     /**
299      * Mode: It indicates that the device is about entering/leaving interactive
300      * state or on-interactive state.
301      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
302      */
303     public static final int MODE_INTERACTIVE = 7;
304 
305     /**
306      * Mode: It indicates the device is in device idle, externally known as doze.
307      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
308      */
309     public static final int MODE_DEVICE_IDLE = 8;
310 
311     /**
312      * Mode: It indicates that display is either off or still on but is optimized
313      * for low power.
314      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
315      */
316     public static final int MODE_DISPLAY_INACTIVE = 9;
317 
318     /**
319      * Mode: It indicates that display is changing layout due to rotation or fold
320      * unfold behavior.
321      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
322      */
323     public static final int MODE_DISPLAY_CHANGE = 17;
324 
325     /**
326      * SetPowerMode() is called to enable/disable specific hint mode, which
327      * may result in adjustment of power/performance parameters of the
328      * cpufreq governor and other controls on device side.
329      *
330      * @param mode Mode which is to be enable/disable.
331      * @param enabled true to enable, false to disable the mode.
332      */
setPowerMode(int mode, boolean enabled)333     public abstract void setPowerMode(int mode, boolean enabled);
334 
335     /** Returns whether there hasn't been a user activity event for the given number of ms. */
wasDeviceIdleFor(long ms)336     public abstract boolean wasDeviceIdleFor(long ms);
337 
338     /** Returns information about the last wakeup event. */
getLastWakeup()339     public abstract PowerManager.WakeData getLastWakeup();
340 
341     /** Returns information about the last event to go to sleep. */
getLastGoToSleep()342     public abstract PowerManager.SleepData getLastGoToSleep();
343 
344     /** Allows power button to intercept a power key button press. */
interceptPowerKeyDown(KeyEvent event)345     public abstract boolean interceptPowerKeyDown(KeyEvent event);
346 
347     /**
348      * Internal version of {@link android.os.PowerManager#nap} which allows for napping while the
349      * device is not awake.
350      */
nap(long eventTime, boolean allowWake)351     public abstract void nap(long eventTime, boolean allowWake);
352 
353     /**
354      * Returns true if ambient display is suppressed by any app with any token. This method will
355      * return false if ambient display is not available.
356      */
isAmbientDisplaySuppressed()357     public abstract boolean isAmbientDisplaySuppressed();
358 }
359