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 -1 to disable the override.
106      */
setScreenBrightnessOverrideFromWindowManager(int brightness)107     public abstract void setScreenBrightnessOverrideFromWindowManager(int 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     /** Returns whether there hasn't been a user activity event for the given number of ms. */
wasDeviceIdleFor(long ms)205     public abstract boolean wasDeviceIdleFor(long ms);
206 
207     /** Returns information about the last wakeup event. */
getLastWakeup()208     public abstract PowerManager.WakeData getLastWakeup();
209 }
210