DisplayManagerInternal.java revision 970d4132ea28e748c1010be39450a98bbf7466f3
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.hardware.display;
18
19import android.hardware.SensorManager;
20import android.os.Handler;
21import android.os.PowerManager;
22import android.view.Display;
23import android.view.DisplayInfo;
24
25/**
26 * Display manager local system service interface.
27 *
28 * @hide Only for use within the system server.
29 */
30public abstract class DisplayManagerInternal {
31    /**
32     * Called by the power manager to initialize power management facilities.
33     */
34    public abstract void initPowerManagement(DisplayPowerCallbacks callbacks,
35            Handler handler, SensorManager sensorManager);
36
37    /**
38     * Called by the power manager to request a new power state.
39     * <p>
40     * The display power controller makes a copy of the provided object and then
41     * begins adjusting the power state to match what was requested.
42     * </p>
43     *
44     * @param request The requested power state.
45     * @param waitForNegativeProximity If true, issues a request to wait for
46     * negative proximity before turning the screen back on, assuming the screen
47     * was turned off by the proximity sensor.
48     * @return True if display is ready, false if there are important changes that must
49     * be made asynchronously (such as turning the screen on), in which case the caller
50     * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()}
51     * then try the request again later until the state converges.
52     */
53    public abstract boolean requestPowerState(DisplayPowerRequest request,
54            boolean waitForNegativeProximity);
55
56    /**
57     * Returns true if the proximity sensor screen-off function is available.
58     */
59    public abstract boolean isProximitySensorAvailable();
60
61    /**
62     * Returns information about the specified logical display.
63     *
64     * @param displayId The logical display id.
65     * @return The logical display info, or null if the display does not exist.  The
66     * returned object must be treated as immutable.
67     */
68    public abstract DisplayInfo getDisplayInfo(int displayId);
69
70    /**
71     * Registers a display transaction listener to provide the client a chance to
72     * update its surfaces within the same transaction as any display layout updates.
73     *
74     * @param listener The listener to register.
75     */
76    public abstract void registerDisplayTransactionListener(DisplayTransactionListener listener);
77
78    /**
79     * Unregisters a display transaction listener to provide the client a chance to
80     * update its surfaces within the same transaction as any display layout updates.
81     *
82     * @param listener The listener to unregister.
83     */
84    public abstract void unregisterDisplayTransactionListener(DisplayTransactionListener listener);
85
86    /**
87     * Overrides the display information of a particular logical display.
88     * This is used by the window manager to control the size and characteristics
89     * of the default display.  It is expected to apply the requested change
90     * to the display information synchronously so that applications will immediately
91     * observe the new state.
92     *
93     * NOTE: This method must be the only entry point by which the window manager
94     * influences the logical configuration of displays.
95     *
96     * @param displayId The logical display id.
97     * @param info The new data to be stored.
98     */
99    public abstract void setDisplayInfoOverrideFromWindowManager(
100            int displayId, DisplayInfo info);
101
102    /**
103     * Called by the window manager to perform traversals while holding a
104     * surface flinger transaction.
105     */
106    public abstract void performTraversalInTransactionFromWindowManager();
107
108    /**
109     * Tells the display manager whether there is interesting unique content on the
110     * specified logical display.  This is used to control automatic mirroring.
111     * <p>
112     * If the display has unique content, then the display manager arranges for it
113     * to be presented on a physical display if appropriate.  Otherwise, the display manager
114     * may choose to make the physical display mirror some other logical display.
115     * </p>
116     *
117     * @param displayId The logical display id to update.
118     * @param hasContent True if the logical display has content.
119     * @param inTraversal True if called from WindowManagerService during a window traversal
120     * prior to call to performTraversalInTransactionFromWindowManager.
121     */
122    public abstract void setDisplayHasContent(int displayId, boolean hasContent,
123            boolean inTraversal);
124
125    /**
126     * Describes the requested power state of the display.
127     *
128     * This object is intended to describe the general characteristics of the
129     * power state, such as whether the screen should be on or off and the current
130     * brightness controls leaving the DisplayPowerController to manage the
131     * details of how the transitions between states should occur.  The goal is for
132     * the PowerManagerService to focus on the global power state and not
133     * have to micro-manage screen off animations, auto-brightness and other effects.
134     */
135    public static final class DisplayPowerRequest {
136        // Policy: Turn screen off as if the user pressed the power button
137        // including playing a screen off animation if applicable.
138        public static final int POLICY_OFF = 0;
139        // Policy: Enable dozing and always-on display functionality.
140        public static final int POLICY_DOZE = 1;
141        // Policy: Make the screen dim when the user activity timeout is
142        // about to expire.
143        public static final int POLICY_DIM = 2;
144        // Policy: Make the screen bright as usual.
145        public static final int POLICY_BRIGHT = 3;
146
147        // The basic overall policy to apply: off, doze, dim or bright.
148        public int policy;
149
150        // If true, the proximity sensor overrides the screen state when an object is
151        // nearby, turning it off temporarily until the object is moved away.
152        public boolean useProximitySensor;
153
154        // The desired screen brightness in the range 0 (minimum / off) to 255 (brightest).
155        // The display power controller may choose to clamp the brightness.
156        // When auto-brightness is enabled, this field should specify a nominal default
157        // value to use while waiting for the light sensor to report enough data.
158        public int screenBrightness;
159
160        // The screen auto-brightness adjustment factor in the range -1 (dimmer) to 1 (brighter).
161        public float screenAutoBrightnessAdjustment;
162
163        // If true, enables automatic brightness control.
164        public boolean useAutoBrightness;
165
166        //If true, scales the brightness to half of desired.
167        public boolean lowPowerMode;
168
169        // If true, prevents the screen from completely turning on if it is currently off.
170        // The display does not enter a "ready" state if this flag is true and screen on is
171        // blocked.  The window manager policy blocks screen on while it prepares the keyguard to
172        // prevent the user from seeing intermediate updates.
173        //
174        // Technically, we may not block the screen itself from turning on (because that introduces
175        // extra unnecessary latency) but we do prevent content on screen from becoming
176        // visible to the user.
177        public boolean blockScreenOn;
178
179        // Overrides the policy for adjusting screen brightness and state while dozing.
180        public int dozeScreenBrightness;
181        public int dozeScreenState;
182
183        public DisplayPowerRequest() {
184            policy = POLICY_BRIGHT;
185            useProximitySensor = false;
186            screenBrightness = PowerManager.BRIGHTNESS_ON;
187            screenAutoBrightnessAdjustment = 0.0f;
188            useAutoBrightness = false;
189            blockScreenOn = false;
190            dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
191            dozeScreenState = Display.STATE_UNKNOWN;
192        }
193
194        public DisplayPowerRequest(DisplayPowerRequest other) {
195            copyFrom(other);
196        }
197
198        public boolean isBrightOrDim() {
199            return policy == POLICY_BRIGHT || policy == POLICY_DIM;
200        }
201
202        public void copyFrom(DisplayPowerRequest other) {
203            policy = other.policy;
204            useProximitySensor = other.useProximitySensor;
205            screenBrightness = other.screenBrightness;
206            screenAutoBrightnessAdjustment = other.screenAutoBrightnessAdjustment;
207            useAutoBrightness = other.useAutoBrightness;
208            blockScreenOn = other.blockScreenOn;
209            lowPowerMode = other.lowPowerMode;
210            dozeScreenBrightness = other.dozeScreenBrightness;
211            dozeScreenState = other.dozeScreenState;
212        }
213
214        @Override
215        public boolean equals(Object o) {
216            return o instanceof DisplayPowerRequest
217                    && equals((DisplayPowerRequest)o);
218        }
219
220        public boolean equals(DisplayPowerRequest other) {
221            return other != null
222                    && policy == other.policy
223                    && useProximitySensor == other.useProximitySensor
224                    && screenBrightness == other.screenBrightness
225                    && screenAutoBrightnessAdjustment == other.screenAutoBrightnessAdjustment
226                    && useAutoBrightness == other.useAutoBrightness
227                    && blockScreenOn == other.blockScreenOn
228                    && lowPowerMode == other.lowPowerMode
229                    && dozeScreenBrightness == other.dozeScreenBrightness
230                    && dozeScreenState == other.dozeScreenState;
231        }
232
233        @Override
234        public int hashCode() {
235            return 0; // don't care
236        }
237
238        @Override
239        public String toString() {
240            return "policy=" + policyToString(policy)
241                    + ", useProximitySensor=" + useProximitySensor
242                    + ", screenBrightness=" + screenBrightness
243                    + ", screenAutoBrightnessAdjustment=" + screenAutoBrightnessAdjustment
244                    + ", useAutoBrightness=" + useAutoBrightness
245                    + ", blockScreenOn=" + blockScreenOn
246                    + ", lowPowerMode=" + lowPowerMode
247                    + ", dozeScreenBrightness=" + dozeScreenBrightness
248                    + ", dozeScreenState=" + Display.stateToString(dozeScreenState);
249        }
250
251        public static String policyToString(int policy) {
252            switch (policy) {
253                case POLICY_OFF:
254                    return "OFF";
255                case POLICY_DOZE:
256                    return "DOZE";
257                case POLICY_DIM:
258                    return "DIM";
259                case POLICY_BRIGHT:
260                    return "BRIGHT";
261                default:
262                    return Integer.toString(policy);
263            }
264        }
265    }
266
267    /**
268     * Asynchronous callbacks from the power controller to the power manager service.
269     */
270    public interface DisplayPowerCallbacks {
271        void onStateChanged();
272        void onProximityPositive();
273        void onProximityNegative();
274        void onDisplayStateChange(int state); // one of the Display state constants
275
276        void acquireSuspendBlocker();
277        void releaseSuspendBlocker();
278    }
279
280    /**
281     * Called within a Surface transaction whenever the size or orientation of a
282     * display may have changed.  Provides an opportunity for the client to
283     * update the position of its surfaces as part of the same transaction.
284     */
285    public interface DisplayTransactionListener {
286        void onDisplayTransaction();
287    }
288}
289