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
17package android.os;
18
19import android.view.Display;
20
21import java.util.function.Consumer;
22
23/**
24 * Power manager local system service interface.
25 *
26 * @hide Only for use within the system server.
27 */
28public 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
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
150    public abstract void setDrawWakeLockOverrideFromSidekick(boolean keepState);
151
152    public abstract PowerSaveState getLowPowerState(int serviceType);
153
154    public abstract void registerLowPowerModeObserver(LowPowerModeListener listener);
155
156    /**
157     * Same as {@link #registerLowPowerModeObserver} but can take a lambda.
158     */
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 {
174        int getServiceType();
175        void onLowPowerModeChanged(PowerSaveState state);
176    }
177
178    public abstract boolean setDeviceIdleMode(boolean enabled);
179
180    public abstract boolean setLightDeviceIdleMode(boolean enabled);
181
182    public abstract void setDeviceIdleWhitelist(int[] appids);
183
184    public abstract void setDeviceIdleTempWhitelist(int[] appids);
185
186    public abstract void startUidChanges();
187
188    public abstract void finishUidChanges();
189
190    public abstract void updateUidProcState(int uid, int procState);
191
192    public abstract void uidGone(int uid);
193
194    public abstract void uidActive(int uid);
195
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     */
202    public abstract void powerHint(int hintId, int data);
203}
204