• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 user activity timeout based on the
102      * current foreground activity.  It can only be used to make the timeout shorter
103      * than usual, not longer.
104      *
105      * This method must only be called by the window manager.
106      *
107      * @param timeoutMillis The overridden timeout, or -1 to disable the override.
108      */
setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis)109     public abstract void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis);
110 
111     /**
112      * Used by the window manager to tell the power manager that the user is no longer actively
113      * using the device.
114      */
setUserInactiveOverrideFromWindowManager()115     public abstract void setUserInactiveOverrideFromWindowManager();
116 
117     /**
118      * Used by device administration to set the maximum screen off timeout.
119      *
120      * This method must only be called by the device administration policy manager.
121      */
setMaximumScreenOffTimeoutFromDeviceAdmin(int userId, long timeMs)122     public abstract void setMaximumScreenOffTimeoutFromDeviceAdmin(int userId, long timeMs);
123 
124     /**
125      * Used by the dream manager to override certain properties while dozing.
126      *
127      * @param screenState The overridden screen state, or {@link Display#STATE_UNKNOWN}
128      * to disable the override.
129      * @param reason The reason for overriding the screen state.
130      * @param screenBrightnessFloat The overridden screen brightness between
131      * {@link PowerManager#BRIGHTNESS_MIN} and {@link PowerManager#BRIGHTNESS_MAX}, or
132      * {@link PowerManager#BRIGHTNESS_INVALID_FLOAT} if screenBrightnessInt should be used instead.
133      * @param screenBrightnessInt The overridden screen brightness between 1 and 255, or
134      * {@link PowerManager#BRIGHTNESS_DEFAULT} to disable the override. Not used if
135      *                            screenBrightnessFloat is provided (is not NaN).
136      * @param useNormalBrightnessForDoze Whether use normal brightness while device is dozing.
137      */
setDozeOverrideFromDreamManager( int screenState, @Display.StateReason int reason, float screenBrightnessFloat, int screenBrightnessInt, boolean useNormalBrightnessForDoze)138     public abstract void setDozeOverrideFromDreamManager(
139             int screenState, @Display.StateReason int reason, float screenBrightnessFloat,
140             int screenBrightnessInt, boolean useNormalBrightnessForDoze);
141 
142     /**
143      * Used by sidekick manager to tell the power manager if it shouldn't change the display state
144      * when a draw wake lock is acquired. Some processes may grab such a wake lock to do some work
145      * in a powered-up state, but we shouldn't give up sidekick control over the display until this
146      * override is lifted.
147      */
setDrawWakeLockOverrideFromSidekick(boolean keepState)148     public abstract void setDrawWakeLockOverrideFromSidekick(boolean keepState);
149 
getLowPowerState(int serviceType)150     public abstract PowerSaveState getLowPowerState(int serviceType);
151 
registerLowPowerModeObserver(LowPowerModeListener listener)152     public abstract void registerLowPowerModeObserver(LowPowerModeListener listener);
153 
154     /**
155      * Same as {@link #registerLowPowerModeObserver} but can take a lambda.
156      */
registerLowPowerModeObserver(int serviceType, Consumer<PowerSaveState> listener)157     public void registerLowPowerModeObserver(int serviceType, Consumer<PowerSaveState> listener) {
158         registerLowPowerModeObserver(new LowPowerModeListener() {
159             @Override
160             public int getServiceType() {
161                 return serviceType;
162             }
163 
164             @Override
165             public void onLowPowerModeChanged(PowerSaveState state) {
166                 listener.accept(state);
167             }
168         });
169     }
170 
171     public interface LowPowerModeListener {
getServiceType()172         int getServiceType();
onLowPowerModeChanged(PowerSaveState state)173         void onLowPowerModeChanged(PowerSaveState state);
174     }
175 
setDeviceIdleMode(boolean enabled)176     public abstract boolean setDeviceIdleMode(boolean enabled);
177 
setLightDeviceIdleMode(boolean enabled)178     public abstract boolean setLightDeviceIdleMode(boolean enabled);
179 
setDeviceIdleWhitelist(int[] appids)180     public abstract void setDeviceIdleWhitelist(int[] appids);
181 
setDeviceIdleTempWhitelist(int[] appids)182     public abstract void setDeviceIdleTempWhitelist(int[] appids);
183 
184     /**
185      * Updates the Low Power Standby allowlist.
186      *
187      * @param uids UIDs that are exempt from Low Power Standby restrictions
188      */
setLowPowerStandbyAllowlist(int[] uids)189     public abstract void setLowPowerStandbyAllowlist(int[] uids);
190 
191     /**
192      * Used by LowPowerStandbyController to notify the power manager that Low Power Standby's
193      * active state has changed.
194      *
195      * @param active {@code true} to activate Low Power Standby, {@code false} to turn it off.
196      */
setLowPowerStandbyActive(boolean active)197     public abstract void setLowPowerStandbyActive(boolean active);
198 
startUidChanges()199     public abstract void startUidChanges();
200 
finishUidChanges()201     public abstract void finishUidChanges();
202 
updateUidProcState(int uid, int procState)203     public abstract void updateUidProcState(int uid, int procState);
204 
uidGone(int uid)205     public abstract void uidGone(int uid);
206 
uidActive(int uid)207     public abstract void uidActive(int uid);
208 
uidIdle(int uid)209     public abstract void uidIdle(int uid);
210 
211     /**
212      * Boost: It is sent when user interacting with the device, for example,
213      * touchscreen events are incoming.
214      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl
215      */
216     public static final int BOOST_INTERACTION = 0;
217 
218     /**
219      * Boost: It indicates that the framework is likely to provide a new display
220      * frame soon. This implies that the device should ensure that the display
221      * processing path is powered up and ready to receive that update.
222      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl
223      */
224     public static final int BOOST_DISPLAY_UPDATE_IMMINENT = 1;
225 
226     /**
227      * SetPowerBoost() indicates the device may need to boost some resources, as
228      * the load is likely to increase before the kernel governors can react.
229      * Depending on the boost, it may be appropriate to raise the frequencies of
230      * CPU, GPU, memory subsystem, or stop CPU from going into deep sleep state.
231      *
232      * @param boost Boost which is to be set with a timeout.
233      * @param durationMs The expected duration of the user's interaction, if
234      *        known, or 0 if the expected duration is unknown.
235      *        a negative value indicates canceling previous boost.
236      *        A given platform can choose to boost some time based on durationMs,
237      *        and may also pick an appropriate timeout for 0 case.
238      */
setPowerBoost(int boost, int durationMs)239     public abstract void setPowerBoost(int boost, int durationMs);
240 
241     /**
242      * Mode: It indicates that the device is to allow wake up when the screen
243      * is tapped twice.
244      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
245      */
246     public static final int MODE_DOUBLE_TAP_TO_WAKE = 0;
247 
248     /**
249      * Mode: It indicates Low power mode is activated or not. Low power mode
250      * is intended to save battery at the cost of performance.
251      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
252      */
253     public static final int MODE_LOW_POWER = 1;
254 
255     /**
256      * Mode: It indicates Sustained Performance mode is activated or not.
257      * Sustained performance mode is intended to provide a consistent level of
258      * performance for a prolonged amount of time.
259      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
260      */
261     public static final int MODE_SUSTAINED_PERFORMANCE = 2;
262 
263     /**
264      * Mode: It sets the device to a fixed performance level which can be sustained
265      * under normal indoor conditions for at least 10 minutes.
266      * Fixed performance mode puts both upper and lower bounds on performance such
267      * that any workload run while in a fixed performance mode should complete in
268      * a repeatable amount of time.
269      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
270      */
271     public static final int MODE_FIXED_PERFORMANCE = 3;
272 
273     /**
274      * Mode: It indicates VR Mode is activated or not. VR mode is intended to
275      * provide minimum guarantee for performance for the amount of time the device
276      * can sustain it.
277      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
278      */
279     public static final int MODE_VR = 4;
280 
281     /**
282      * Mode: It indicates that an application has been launched.
283      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
284      */
285     public static final int MODE_LAUNCH = 5;
286 
287     /**
288      * Mode: It indicates that the device is about to enter a period of expensive
289      * rendering.
290      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
291      */
292     public static final int MODE_EXPENSIVE_RENDERING = 6;
293 
294     /**
295      * Mode: It indicates that the device is about entering/leaving interactive
296      * state or on-interactive state.
297      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
298      */
299     public static final int MODE_INTERACTIVE = 7;
300 
301     /**
302      * Mode: It indicates the device is in device idle, externally known as doze.
303      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
304      */
305     public static final int MODE_DEVICE_IDLE = 8;
306 
307     /**
308      * Mode: It indicates that display is either off or still on but is optimized
309      * for low power.
310      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
311      */
312     public static final int MODE_DISPLAY_INACTIVE = 9;
313 
314     /**
315      * Mode: It indicates that display is changing layout due to rotation or fold
316      * unfold behavior.
317      * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl
318      */
319     public static final int MODE_DISPLAY_CHANGE = 17;
320 
321     /**
322      * SetPowerMode() is called to enable/disable specific hint mode, which
323      * may result in adjustment of power/performance parameters of the
324      * cpufreq governor and other controls on device side.
325      *
326      * @param mode Mode which is to be enable/disable.
327      * @param enabled true to enable, false to disable the mode.
328      */
setPowerMode(int mode, boolean enabled)329     public abstract void setPowerMode(int mode, boolean enabled);
330 
331     /** Returns whether there hasn't been a user activity event for the given number of ms. */
wasDeviceIdleFor(long ms)332     public abstract boolean wasDeviceIdleFor(long ms);
333 
334     /** Returns information about the last wakeup event. */
getLastWakeup()335     public abstract PowerManager.WakeData getLastWakeup();
336 
337     /** Returns information about the last event to go to sleep. */
getLastGoToSleep()338     public abstract PowerManager.SleepData getLastGoToSleep();
339 
340     /** Allows power button to intercept a power key button press. */
interceptPowerKeyDown(KeyEvent event)341     public abstract boolean interceptPowerKeyDown(KeyEvent event);
342 
343     /**
344      * Internal version of {@link android.os.PowerManager#nap} which allows for napping while the
345      * device is not awake.
346      */
nap(long eventTime, boolean allowWake)347     public abstract void nap(long eventTime, boolean allowWake);
348 
349     /**
350      * Returns true if ambient display is suppressed by any app with any token. This method will
351      * return false if ambient display is not available.
352      */
isAmbientDisplaySuppressed()353     public abstract boolean isAmbientDisplaySuppressed();
354 
355     /**
356      * Notifies PowerManager that the device has entered a postured state (stationary + upright).
357      * This may affect dream eligibility.
358      */
setDevicePostured(boolean isPostured)359     public abstract void setDevicePostured(boolean isPostured);
360 
361     /**
362      * Notifies PowerManager that settings have changed and that it should refresh its state.
363      */
updateSettings()364     public abstract void updateSettings();
365 }
366