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