blob: e70460aef3e0343fe49167967deaf1d872fbdd3f [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 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 com.android.server.display;
18
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080019import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010020import static android.hardware.display.DisplayManager
21 .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080022import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
23import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
24import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010025import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
26import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
27import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jeff Brownbd6e1502012-08-28 03:27:37 -070028
Jeff Brownfa25bf52012-07-23 19:26:30 -070029import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070030import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000031import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000032import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000033import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070034import android.content.Context;
35import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010036import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010037import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010038import android.content.res.TypedArray;
Michael Wrighteedcbf12017-08-16 23:14:54 +010039import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070040import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000041import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010042import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000043import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010044import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070045import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080046import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080047import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010048import android.hardware.display.DisplayViewport;
Jeff Brownfa25bf52012-07-23 19:26:30 -070049import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070050import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070051import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070052import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080053import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070054import android.media.projection.IMediaProjection;
55import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070056import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070057import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070058import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080059import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070060import android.os.Looper;
61import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070062import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080063import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070064import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010065import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070066import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010067import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070068import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070069import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070070import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010071import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000072import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010073import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070074import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080075import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010076import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070077import android.util.Slog;
78import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010079import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070080import android.view.Display;
81import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070082import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080083import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070084
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010085import com.android.internal.annotations.GuardedBy;
86import com.android.internal.annotations.VisibleForTesting;
87import com.android.internal.util.DumpUtils;
88import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +010089import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080090import com.android.server.DisplayThread;
91import com.android.server.LocalServices;
92import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070093import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020094import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010095import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070096
97import java.io.FileDescriptor;
98import java.io.PrintWriter;
99import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700100import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700101import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800102import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700103
104/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700105 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700106 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700107 * The {@link DisplayManagerService} manages the global lifecycle of displays,
108 * decides how to configure logical displays based on the physical display devices currently
109 * attached, sends notifications to the system and to applications when the state
110 * changes, and so on.
111 * </p><p>
112 * The display manager service relies on a collection of {@link DisplayAdapter} components,
113 * for discovering and configuring physical display devices attached to the system.
114 * There are separate display adapters for each manner that devices are attached:
115 * one display adapter for built-in local displays, one for simulated non-functional
116 * displays when the system is headless, one for simulated overlay displays used for
117 * development, one for wifi displays, etc.
118 * </p><p>
119 * Display adapters are only weakly coupled to the display manager service.
120 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700121 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700122 * by the display manager service. This separation of concerns is important for
123 * two main reasons. First, it neatly encapsulates the responsibilities of these
124 * two classes: display adapters handle individual display devices whereas
125 * the display manager service handles the global state. Second, it eliminates
126 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700127 * </p>
128 *
129 * <h3>Synchronization</h3>
130 * <p>
131 * Because the display manager may be accessed by multiple threads, the synchronization
132 * story gets a little complicated. In particular, the window manager may call into
133 * the display manager while holding a surface transaction with the expectation that
134 * it can apply changes immediately. Unfortunately, that means we can't just do
135 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700136 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700137 * To make this work, all of the objects that belong to the display manager must
138 * use the same lock. We call this lock the synchronization root and it has a unique
139 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
140 * named with the "Locked" suffix.
141 * </p><p>
142 * Where things get tricky is that the display manager is not allowed to make
143 * any potentially reentrant calls, especially into the window manager. We generally
144 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700145 * </p>
146 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800147public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700148 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700149 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700150
Jeff Brownbbd28a22012-09-20 16:47:15 -0700151 // When this system property is set to 0, WFD is forcibly disabled on boot.
152 // When this system property is set to 1, WFD is forcibly enabled on boot.
153 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
154 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
155
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200156 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
157
Jeff Brownbd6e1502012-08-28 03:27:37 -0700158 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
159
Santos Cordonc22c5632017-06-21 16:03:49 -0700160 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700161 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
162 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700163 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700164 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100165 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000166 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700167
Jeff Brownb880d882014-02-10 19:47:07 -0800168 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700169 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700170 private final Handler mUiHandler;
171 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800172 private WindowManagerInternal mWindowManagerInternal;
173 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700174 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700175
176 // The synchronization root for the display manager.
177 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800178 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
179 // into WindowManagerService methods that require mWindowMap while holding this unless you are
180 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700181 private final SyncRoot mSyncRoot = new SyncRoot();
182
183 // True if in safe mode.
184 // This option may disable certain display adapters.
185 public boolean mSafeMode;
186
187 // True if we are in a special boot mode where only core applications and
188 // services should be started. This option may disable certain display adapters.
189 public boolean mOnlyCore;
190
Jeff Brown27f1d672012-10-17 18:32:34 -0700191 // True if the display manager service should pretend there is only one display
192 // and only tell applications about the existence of the default logical display.
193 // The display manager can still mirror content to secondary displays but applications
194 // cannot present unique content on those displays.
195 // Used for demonstration purposes only.
196 private final boolean mSingleDisplayDemoMode;
197
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700198 // All callback records indexed by calling process id.
199 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700200 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700201
Jeff Brownbd6e1502012-08-28 03:27:37 -0700202 // List of all currently registered display adapters.
203 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
204
205 // List of all currently connected display devices.
206 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
207
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700208 // List of all logical displays indexed by logical display id.
209 private final SparseArray<LogicalDisplay> mLogicalDisplays =
210 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700211 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
212
Jeff Brown7f3994e2012-12-04 14:04:28 -0800213 // List of all display transaction listeners.
214 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
215 new CopyOnWriteArrayList<DisplayTransactionListener>();
216
Jeff Brownad9ef192014-04-08 17:26:30 -0700217 // Display power controller.
218 private DisplayPowerController mDisplayPowerController;
219
Jeff Brown037c33e2014-04-09 00:31:55 -0700220 // The overall display state, independent of changes that might influence one
221 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700222 private int mGlobalDisplayState = Display.STATE_ON;
223
224 // The overall display brightness.
225 // For now, this only applies to the built-in display but we may split it up eventually.
226 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700227
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700228 // Set to true when there are pending display changes that have yet to be applied
229 // to the surface flinger state.
230 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700231
Jeff Browne08ae382012-09-07 20:36:36 -0700232 // The Wifi display adapter, or null if not registered.
233 private WifiDisplayAdapter mWifiDisplayAdapter;
234
Jeff Brownce468a32013-11-21 16:42:03 -0800235 // The number of active wifi display scan requests.
236 private int mWifiDisplayScanRequestCount;
237
Jeff Browna506a6e2013-06-04 00:02:38 -0700238 // The virtual display adapter, or null if not registered.
239 private VirtualDisplayAdapter mVirtualDisplayAdapter;
240
Michael Wrighteef0e132017-11-21 17:57:52 +0000241 // The User ID of the current user
242 private @UserIdInt int mCurrentUserId;
243
Michael Wrighteedcbf12017-08-16 23:14:54 +0100244 // The stable device screen height and width. These are not tied to a specific display, even
245 // the default display, because they need to be stable over the course of the device's entire
246 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
247 // device).
248 private Point mStableDisplaySize = new Point();
249
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200250 // Whether the system has finished booting or not.
251 private boolean mSystemReady;
252
253 // The top inset of the default display.
254 // This gets persisted so that the boot animation knows how to transition from the display's
255 // full size to the size configured by the user. Right now we only persist and animate the top
256 // inset, but theoretically we could do it for all of them.
257 private int mDefaultDisplayTopInset;
258
Jeff Brownd728bf52012-09-08 18:05:28 -0700259 // Viewports of the default display and the display that should receive touch
260 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100261 @GuardedBy("mSyncRoot")
262 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700263
Jeff Brown89d55462012-09-19 11:33:42 -0700264 // Persistent data store for all internal settings maintained by the display manager service.
265 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
266
Jeff Brownbd6e1502012-08-28 03:27:37 -0700267 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700268 // May be used outside of the lock but only on the handler thread.
269 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700270
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700271 // Temporary display info, used for comparing display configurations.
272 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
273
Jeff Brownd728bf52012-09-08 18:05:28 -0700274 // Temporary viewports, used when sending new viewport information to the
275 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100276 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700277
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700278 // The default color mode for default displays. Overrides the usual
279 // Display.Display.COLOR_MODE_DEFAULT for displays with the
280 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
281 private final int mDefaultDisplayDefaultColorMode;
282
Jeff Browne75926d2014-09-18 15:24:49 -0700283 // Temporary list of deferred work to perform when setting the display state.
284 // Only used by requestDisplayState. The field is self-synchronized and only
285 // intended for use inside of the requestGlobalDisplayStateInternal function.
286 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
287
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800288 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
289 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
290
Santos Cordonee8931e2017-04-05 10:31:15 -0700291 private final Injector mInjector;
292
Dan Gittik122df862018-03-28 16:59:22 +0100293 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
294 // is rejected by the system.
295 private final Curve mMinimumBrightnessCurve;
296 private final Spline mMinimumBrightnessSpline;
297
Jeff Brownb880d882014-02-10 19:47:07 -0800298 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700299 this(context, new Injector());
300 }
301
302 @VisibleForTesting
303 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800304 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700305 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800306 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800307 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700308 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700309 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700310 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100311 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700312 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000313 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200314 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100315 float[] lux = getFloatArray(resources.obtainTypedArray(
316 com.android.internal.R.array.config_minimumBrightnessCurveLux));
317 float[] nits = getFloatArray(resources.obtainTypedArray(
318 com.android.internal.R.array.config_minimumBrightnessCurveNits));
319 mMinimumBrightnessCurve = new Curve(lux, nits);
320 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700321
322 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
323 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000324 mCurrentUserId = UserHandle.USER_SYSTEM;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200325
326 mSystemReady = false;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700327 }
328
329 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100330 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000331 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
332 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100333 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
334 Process.THREAD_GROUP_TOP_APP);
335 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
336 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200337 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
338 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700339 }
340
Jeff Brown4ccb8232014-01-16 22:16:42 -0800341 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800342 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700343 // We need to pre-load the persistent data store so it's ready before the default display
344 // adapter is up so that we have it's configuration. We could load it lazily, but since
345 // we're going to have to read it in eventually we may as well do it here rather than after
Santos Cordonc22c5632017-06-21 16:03:49 -0700346 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100347 synchronized(mSyncRoot) {
348 mPersistentDataStore.loadIfNeeded();
349 loadStableDisplayValuesLocked();
350 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700351 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800352
353 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
354 true /*allowIsolated*/);
355 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700356 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800357 }
358
359 @Override
360 public void onBootPhase(int phase) {
361 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
362 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700363 long timeout = SystemClock.uptimeMillis()
364 + mInjector.getDefaultDisplayDelayTimeout();
365 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
366 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800367 long delay = timeout - SystemClock.uptimeMillis();
368 if (delay <= 0) {
369 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700370 + "to be initialized. DefaultDisplay="
371 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
372 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800373 }
374 if (DEBUG) {
375 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
376 }
377 try {
378 mSyncRoot.wait(delay);
379 } catch (InterruptedException ex) {
380 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700381 }
382 }
383 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700384 }
385
Michael Wrighteef0e132017-11-21 17:57:52 +0000386 @Override
387 public void onSwitchUser(@UserIdInt int newUserId) {
388 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
389 synchronized (mSyncRoot) {
390 if (mCurrentUserId != newUserId) {
391 mCurrentUserId = newUserId;
392 BrightnessConfiguration config =
393 mPersistentDataStore.getBrightnessConfiguration(userSerial);
394 mDisplayPowerController.setBrightnessConfiguration(config);
395 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000396 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000397 }
398 }
399
Jeff Brown4ccb8232014-01-16 22:16:42 -0800400 // TODO: Use dependencies or a boot phase
401 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700402 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800403 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
404 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700405 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700406 }
407 }
408
409 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700410 * Called when the system is ready to go.
411 */
412 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700413 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700414 mSafeMode = safeMode;
415 mOnlyCore = onlyCore;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200416 mSystemReady = true;
417 // Just in case the top inset changed before the system was ready. At this point, any
418 // relevant configuration should be in place.
419 recordTopInsetLocked(mLogicalDisplays.get(Display.DEFAULT_DISPLAY));
Jeff Brownbd6e1502012-08-28 03:27:37 -0700420 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700421
Jeff Brownbd6e1502012-08-28 03:27:37 -0700422 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100423 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700424 }
425
Santos Cordonee8931e2017-04-05 10:31:15 -0700426 @VisibleForTesting
427 Handler getDisplayHandler() {
428 return mHandler;
429 }
430
Michael Wrighteedcbf12017-08-16 23:14:54 +0100431 private void loadStableDisplayValuesLocked() {
432 final Point size = mPersistentDataStore.getStableDisplaySize();
433 if (size.x > 0 && size.y > 0) {
434 // Just set these values directly so we don't write the display persistent data again
435 // unnecessarily
436 mStableDisplaySize.set(size.x, size.y);
437 } else {
438 final Resources res = mContext.getResources();
439 final int width = res.getInteger(
440 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
441 final int height = res.getInteger(
442 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
443 if (width > 0 && height > 0) {
444 setStableDisplaySizeLocked(width, height);
445 }
446 }
447 }
448
449 private Point getStableDisplaySizeInternal() {
450 Point r = new Point();
451 synchronized (mSyncRoot) {
452 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
453 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
454 }
455 }
456 return r;
457 }
458
Jeff Brown4ccb8232014-01-16 22:16:42 -0800459 private void registerDisplayTransactionListenerInternal(
460 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800461 // List is self-synchronized copy-on-write.
462 mDisplayTransactionListeners.add(listener);
463 }
464
Jeff Brown4ccb8232014-01-16 22:16:42 -0800465 private void unregisterDisplayTransactionListenerInternal(
466 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800467 // List is self-synchronized copy-on-write.
468 mDisplayTransactionListeners.remove(listener);
469 }
470
Jeff Brown4ccb8232014-01-16 22:16:42 -0800471 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700472 int displayId, DisplayInfo info) {
473 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700474 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700475 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700476 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200477 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700478 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700479 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700480 }
481 }
482 }
483
Andrii Kuliancd097992017-03-23 18:31:59 -0700484 /**
485 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
486 */
487 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
488 synchronized (mSyncRoot) {
489 final LogicalDisplay display = mLogicalDisplays.get(displayId);
490 if (display != null) {
491 display.getNonOverrideDisplayInfoLocked(outInfo);
492 }
493 }
494 }
495
Santos Cordonee8931e2017-04-05 10:31:15 -0700496 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800497 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700498 synchronized (mSyncRoot) {
499 if (!mPendingTraversal) {
500 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700501 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700502 mPendingTraversal = false;
503
Robert Carrae606b42018-02-15 15:36:23 -0800504 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700505 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800506
507 // List is self-synchronized copy-on-write.
508 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
509 listener.onDisplayTransaction();
510 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700511 }
512
Jeff Brown5d6443b2015-04-10 20:15:01 -0700513 private void requestGlobalDisplayStateInternal(int state, int brightness) {
514 if (state == Display.STATE_UNKNOWN) {
515 state = Display.STATE_ON;
516 }
517 if (state == Display.STATE_OFF) {
518 brightness = PowerManager.BRIGHTNESS_OFF;
519 } else if (brightness < 0) {
520 brightness = PowerManager.BRIGHTNESS_DEFAULT;
521 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
522 brightness = PowerManager.BRIGHTNESS_ON;
523 }
524
Jeff Browne75926d2014-09-18 15:24:49 -0700525 synchronized (mTempDisplayStateWorkQueue) {
526 try {
527 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700528 // Note that we do not need to schedule traversals here although it
529 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700530 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700531 if (mGlobalDisplayState == state
532 && mGlobalDisplayBrightness == brightness) {
533 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700534 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700535
536 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
537 + Display.stateToString(state)
538 + ", brightness=" + brightness + ")");
539 mGlobalDisplayState = state;
540 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700541 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700542 }
543
544 // Setting the display power state can take hundreds of milliseconds
545 // to complete so we defer the most expensive part of the work until
546 // after we have exited the critical section to avoid blocking other
547 // threads for a long time.
548 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
549 mTempDisplayStateWorkQueue.get(i).run();
550 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700551 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700552 } finally {
553 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700554 }
555 }
556 }
557
Jeff Brown4ccb8232014-01-16 22:16:42 -0800558 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700559 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800560 LogicalDisplay display = mLogicalDisplays.get(displayId);
561 if (display != null) {
562 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800563 if (info.hasAccess(callingUid)
564 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800565 return info;
566 }
567 }
568 return null;
569 }
570 }
571
572 private int[] getDisplayIdsInternal(int callingUid) {
573 synchronized (mSyncRoot) {
574 final int count = mLogicalDisplays.size();
575 int[] displayIds = new int[count];
576 int n = 0;
577 for (int i = 0; i < count; i++) {
578 LogicalDisplay display = mLogicalDisplays.valueAt(i);
579 DisplayInfo info = display.getDisplayInfoLocked();
580 if (info.hasAccess(callingUid)) {
581 displayIds[n++] = mLogicalDisplays.keyAt(i);
582 }
583 }
584 if (n != count) {
585 displayIds = Arrays.copyOfRange(displayIds, 0, n);
586 }
587 return displayIds;
588 }
589 }
590
591 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
592 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700593 if (mCallbacks.get(callingPid) != null) {
594 throw new SecurityException("The calling process has already "
595 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700596 }
597
Jeff Brownbd6e1502012-08-28 03:27:37 -0700598 CallbackRecord record = new CallbackRecord(callingPid, callback);
599 try {
600 IBinder binder = callback.asBinder();
601 binder.linkToDeath(record, 0);
602 } catch (RemoteException ex) {
603 // give up
604 throw new RuntimeException(ex);
605 }
606
607 mCallbacks.put(callingPid, record);
608 }
609 }
610
Jeff Brownce468a32013-11-21 16:42:03 -0800611 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700612 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800613 mCallbacks.remove(record.mPid);
614 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700615 }
616 }
617
Jeff Brown4ccb8232014-01-16 22:16:42 -0800618 private void startWifiDisplayScanInternal(int callingPid) {
619 synchronized (mSyncRoot) {
620 CallbackRecord record = mCallbacks.get(callingPid);
621 if (record == null) {
622 throw new IllegalStateException("The calling process has not "
623 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700624 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800625 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700626 }
627 }
628
Jeff Brownce468a32013-11-21 16:42:03 -0800629 private void startWifiDisplayScanLocked(CallbackRecord record) {
630 if (!record.mWifiDisplayScanRequested) {
631 record.mWifiDisplayScanRequested = true;
632 if (mWifiDisplayScanRequestCount++ == 0) {
633 if (mWifiDisplayAdapter != null) {
634 mWifiDisplayAdapter.requestStartScanLocked();
635 }
636 }
637 }
638 }
639
Jeff Brown4ccb8232014-01-16 22:16:42 -0800640 private void stopWifiDisplayScanInternal(int callingPid) {
641 synchronized (mSyncRoot) {
642 CallbackRecord record = mCallbacks.get(callingPid);
643 if (record == null) {
644 throw new IllegalStateException("The calling process has not "
645 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800646 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800647 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800648 }
649 }
650
651 private void stopWifiDisplayScanLocked(CallbackRecord record) {
652 if (record.mWifiDisplayScanRequested) {
653 record.mWifiDisplayScanRequested = false;
654 if (--mWifiDisplayScanRequestCount == 0) {
655 if (mWifiDisplayAdapter != null) {
656 mWifiDisplayAdapter.requestStopScanLocked();
657 }
658 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700659 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800660 + mWifiDisplayScanRequestCount);
661 mWifiDisplayScanRequestCount = 0;
662 }
663 }
664 }
665
Jeff Brown4ccb8232014-01-16 22:16:42 -0800666 private void connectWifiDisplayInternal(String address) {
667 synchronized (mSyncRoot) {
668 if (mWifiDisplayAdapter != null) {
669 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700670 }
Jeff Browne08ae382012-09-07 20:36:36 -0700671 }
672 }
673
Jeff Brown4ccb8232014-01-16 22:16:42 -0800674 private void pauseWifiDisplayInternal() {
675 synchronized (mSyncRoot) {
676 if (mWifiDisplayAdapter != null) {
677 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700678 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700679 }
680 }
681
Jeff Brown4ccb8232014-01-16 22:16:42 -0800682 private void resumeWifiDisplayInternal() {
683 synchronized (mSyncRoot) {
684 if (mWifiDisplayAdapter != null) {
685 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700686 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700687 }
688 }
689
Jeff Brown4ccb8232014-01-16 22:16:42 -0800690 private void disconnectWifiDisplayInternal() {
691 synchronized (mSyncRoot) {
692 if (mWifiDisplayAdapter != null) {
693 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700694 }
Jeff Browne08ae382012-09-07 20:36:36 -0700695 }
696 }
697
Jeff Brown4ccb8232014-01-16 22:16:42 -0800698 private void renameWifiDisplayInternal(String address, String alias) {
699 synchronized (mSyncRoot) {
700 if (mWifiDisplayAdapter != null) {
701 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700702 }
Jeff Brown89d55462012-09-19 11:33:42 -0700703 }
704 }
705
Jeff Brown4ccb8232014-01-16 22:16:42 -0800706 private void forgetWifiDisplayInternal(String address) {
707 synchronized (mSyncRoot) {
708 if (mWifiDisplayAdapter != null) {
709 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700710 }
Jeff Brown89d55462012-09-19 11:33:42 -0700711 }
712 }
713
Jeff Brown4ccb8232014-01-16 22:16:42 -0800714 private WifiDisplayStatus getWifiDisplayStatusInternal() {
715 synchronized (mSyncRoot) {
716 if (mWifiDisplayAdapter != null) {
717 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700718 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800719 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700720 }
721 }
722
Michael Wright1c9977b2016-07-12 13:30:10 -0700723 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100724 synchronized (mSyncRoot) {
725 LogicalDisplay display = mLogicalDisplays.get(displayId);
726 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700727 display.getRequestedColorModeLocked() != colorMode) {
728 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100729 scheduleTraversalLocked(false);
730 }
731 }
732 }
733
Bryan Mawhinney462e29d2018-03-22 15:52:41 +0000734 private void setSaturationLevelInternal(float level) {
735 if (level < 0 || level > 1) {
736 throw new IllegalArgumentException("Saturation level must be between 0 and 1");
737 }
738 float[] matrix = (level == 1.0f ? null : computeSaturationMatrix(level));
739 DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
740 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION, matrix);
741 }
742
743 private static float[] computeSaturationMatrix(float saturation) {
744 float desaturation = 1.0f - saturation;
745 float[] luminance = {0.231f * desaturation, 0.715f * desaturation, 0.072f * desaturation};
746 float[] matrix = {
747 luminance[0] + saturation, luminance[0], luminance[0], 0,
748 luminance[1], luminance[1] + saturation, luminance[1], 0,
749 luminance[2], luminance[2], luminance[2] + saturation, 0,
750 0, 0, 0, 1
751 };
752 return matrix;
753 }
754
Michael Wright75ee9fc2014-09-01 19:55:22 -0700755 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700756 IMediaProjection projection, int callingUid, String packageName, String name, int width,
757 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800758 synchronized (mSyncRoot) {
759 if (mVirtualDisplayAdapter == null) {
760 Slog.w(TAG, "Rejecting request to create private virtual display "
761 + "because the virtual display adapter is not available.");
762 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700763 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800764
765 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700766 callback, projection, callingUid, packageName, name, width, height, densityDpi,
767 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800768 if (device == null) {
769 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700770 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700771
Jeff Brown4ccb8232014-01-16 22:16:42 -0800772 handleDisplayDeviceAddedLocked(device);
773 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
774 if (display != null) {
775 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700776 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800777
778 // Something weird happened and the logical display was not created.
779 Slog.w(TAG, "Rejecting request to create virtual display "
780 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700781 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800782 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700783 }
784 return -1;
785 }
786
Michael Wright01e840f2014-06-26 16:03:25 -0700787 private void resizeVirtualDisplayInternal(IBinder appToken,
788 int width, int height, int densityDpi) {
789 synchronized (mSyncRoot) {
790 if (mVirtualDisplayAdapter == null) {
791 return;
792 }
793
794 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
795 }
796 }
797
Jeff Brown92207df2014-04-16 13:16:07 -0700798 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
799 synchronized (mSyncRoot) {
800 if (mVirtualDisplayAdapter == null) {
801 return;
802 }
803
804 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
805 }
806 }
807
Jeff Brown4ccb8232014-01-16 22:16:42 -0800808 private void releaseVirtualDisplayInternal(IBinder appToken) {
809 synchronized (mSyncRoot) {
810 if (mVirtualDisplayAdapter == null) {
811 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700812 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700813
Jeff Brown4ccb8232014-01-16 22:16:42 -0800814 DisplayDevice device =
815 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
816 if (device != null) {
817 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700818 }
819 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700820 }
821
Santos Cordonc22c5632017-06-21 16:03:49 -0700822 private void registerDefaultDisplayAdapters() {
823 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700824 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700825 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800826 registerDisplayAdapterLocked(new LocalDisplayAdapter(
827 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700828
829 // Standalone VR devices rely on a virtual display as their primary display for
830 // 2D UI. We register virtual display adapter along side the main display adapter
831 // here so that it is ready by the time the system sends the home Intent for
832 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
833 // the virtual display inside VR before any VR-specific apps even run.
834 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
835 mHandler, mDisplayAdapterListener);
836 if (mVirtualDisplayAdapter != null) {
837 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
838 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700839 }
840 }
841
842 private void registerAdditionalDisplayAdapters() {
843 synchronized (mSyncRoot) {
844 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700845 registerOverlayDisplayAdapterLocked();
846 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700847 }
848 }
849 }
850
Jeff Brown89d55462012-09-19 11:33:42 -0700851 private void registerOverlayDisplayAdapterLocked() {
852 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
853 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
854 }
855
856 private void registerWifiDisplayAdapterLocked() {
857 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700858 com.android.internal.R.bool.config_enableWifiDisplay)
859 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700860 mWifiDisplayAdapter = new WifiDisplayAdapter(
861 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
862 mPersistentDataStore);
863 registerDisplayAdapterLocked(mWifiDisplayAdapter);
864 }
865 }
866
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700867 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
868 // In safe mode, we disable non-essential display adapters to give the user
869 // an opportunity to fix broken settings or other problems that might affect
870 // system stability.
871 // In only-core mode, we disable non-essential display adapters to minimize
872 // the number of dependencies that are started while in this mode and to
873 // prevent problems that might occur due to the device being encrypted.
874 return !mSafeMode && !mOnlyCore;
875 }
876
877 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
878 mDisplayAdapters.add(adapter);
879 adapter.registerLocked();
880 }
881
Jeff Brownbd6e1502012-08-28 03:27:37 -0700882 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700883 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700884 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700885 }
886 }
887
Jeff Browna506a6e2013-06-04 00:02:38 -0700888 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700889 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700890 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700891 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700892 return;
893 }
894
Jeff Brown10acf6d2015-04-14 14:20:47 -0700895 Slog.i(TAG, "Display device added: " + info);
896 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700897
898 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700899 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700900 Runnable work = updateDisplayStateLocked(device);
901 if (work != null) {
902 work.run();
903 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700904 scheduleTraversalLocked(false);
905 }
906
Jeff Brownbd6e1502012-08-28 03:27:37 -0700907 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700908 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700909 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700910 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700911 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700912 return;
913 }
914
Jeff Brown10acf6d2015-04-14 14:20:47 -0700915 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
916 if (diff == DisplayDeviceInfo.DIFF_STATE) {
917 Slog.i(TAG, "Display device changed state: \"" + info.name
918 + "\", " + Display.stateToString(info.state));
919 } else if (diff != 0) {
920 Slog.i(TAG, "Display device changed: " + info);
921 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700922 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
923 try {
924 mPersistentDataStore.setColorMode(device, info.colorMode);
925 } finally {
926 mPersistentDataStore.saveIfNeeded();
927 }
928 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700929 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700930
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700931 device.applyPendingDisplayDeviceInfoChangesLocked();
932 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700933 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700934 }
935 }
936 }
937
Jeff Brownbd6e1502012-08-28 03:27:37 -0700938 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700939 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700940 handleDisplayDeviceRemovedLocked(device);
941 }
942 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700943
Jeff Browna506a6e2013-06-04 00:02:38 -0700944 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700945 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700946 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700947 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700948 return;
949 }
950
Jeff Brown10acf6d2015-04-14 14:20:47 -0700951 Slog.i(TAG, "Display device removed: " + info);
952 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700953
Jeff Browna506a6e2013-06-04 00:02:38 -0700954 updateLogicalDisplaysLocked();
955 scheduleTraversalLocked(false);
956 }
957
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200958 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
959 if (displayId == Display.DEFAULT_DISPLAY) {
960 recordTopInsetLocked(display);
961 }
962 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
963 }
964
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700965 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700966 final int count = mDisplayDevices.size();
967 for (int i = 0; i < count; i++) {
968 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700969 Runnable runnable = updateDisplayStateLocked(device);
970 if (runnable != null) {
971 workQueue.add(runnable);
972 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700973 }
974 }
975
Jeff Browne75926d2014-09-18 15:24:49 -0700976 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700977 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700978 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700979 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
980 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700981 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700982 }
Jeff Browne75926d2014-09-18 15:24:49 -0700983 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700984 }
985
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700986 // Adds a new logical display based on the given display device.
987 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700988 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700989 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
990 boolean isDefault = (deviceInfo.flags
991 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
992 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
993 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
994 isDefault = false;
995 }
996
Jeff Brown27f1d672012-10-17 18:32:34 -0700997 if (!isDefault && mSingleDisplayDemoMode) {
998 Slog.i(TAG, "Not creating a logical display for a secondary display "
999 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001000 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001001 }
1002
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001003 final int displayId = assignDisplayIdLocked(isDefault);
1004 final int layerStack = assignLayerStackLocked(displayId);
1005
Jeff Brownd728bf52012-09-08 18:05:28 -07001006 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001007 display.updateLocked(mDisplayDevices);
1008 if (!display.isValidLocked()) {
1009 // This should never happen currently.
1010 Slog.w(TAG, "Ignoring display device because the logical display "
1011 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001012 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001013 }
1014
Michael Wrighteedcbf12017-08-16 23:14:54 +01001015 configureColorModeLocked(display, device);
1016 if (isDefault) {
1017 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001018 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001019 }
1020
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001021 mLogicalDisplays.put(displayId, display);
1022
1023 // Wake up waitForDefaultDisplay.
1024 if (isDefault) {
1025 mSyncRoot.notifyAll();
1026 }
1027
1028 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001029 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001030 }
1031
1032 private int assignDisplayIdLocked(boolean isDefault) {
1033 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1034 }
1035
1036 private int assignLayerStackLocked(int displayId) {
1037 // Currently layer stacks and display ids are the same.
1038 // This need not be the case.
1039 return displayId;
1040 }
1041
Michael Wrighteedcbf12017-08-16 23:14:54 +01001042 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1043 if (display.getPrimaryDisplayDeviceLocked() == device) {
1044 int colorMode = mPersistentDataStore.getColorMode(device);
1045 if (colorMode == Display.COLOR_MODE_INVALID) {
1046 if ((device.getDisplayDeviceInfoLocked().flags
1047 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1048 colorMode = mDefaultDisplayDefaultColorMode;
1049 } else {
1050 colorMode = Display.COLOR_MODE_DEFAULT;
1051 }
1052 }
1053 display.setRequestedColorModeLocked(colorMode);
1054 }
1055 }
1056
1057 // If we've never recorded stable device stats for this device before and they aren't
1058 // explicitly configured, go ahead and record the stable device stats now based on the status
1059 // of the default display at first boot.
1060 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1061 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1062 DisplayInfo info = d.getDisplayInfoLocked();
1063 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1064 }
1065 }
1066
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001067 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
1068 // We must only persist the inset after boot has completed, otherwise we will end up
1069 // overwriting the persisted value before the masking flag has been loaded from the
1070 // resource overlay.
1071 if (!mSystemReady || d == null) {
1072 return;
1073 }
1074 int topInset = d.getInsets().top;
1075 if (topInset == mDefaultDisplayTopInset) {
1076 return;
1077 }
1078 mDefaultDisplayTopInset = topInset;
1079 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1080 }
1081
Michael Wrighteedcbf12017-08-16 23:14:54 +01001082 private void setStableDisplaySizeLocked(int width, int height) {
1083 mStableDisplaySize = new Point(width, height);
1084 try {
1085 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1086 } finally {
1087 mPersistentDataStore.saveIfNeeded();
1088 }
1089 }
1090
Dan Gittik122df862018-03-28 16:59:22 +01001091 @VisibleForTesting
1092 Curve getMinimumBrightnessCurveInternal() {
1093 return mMinimumBrightnessCurve;
1094 }
1095
Michael Wrighteef0e132017-11-21 17:57:52 +00001096 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001097 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001098 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001099 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001100 final int userSerial = getUserManager().getUserSerialNumber(userId);
1101 synchronized (mSyncRoot) {
1102 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001103 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1104 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001105 } finally {
1106 mPersistentDataStore.saveIfNeeded();
1107 }
1108 if (userId == mCurrentUserId) {
1109 mDisplayPowerController.setBrightnessConfiguration(c);
1110 }
1111 }
1112 }
1113
Dan Gittik122df862018-03-28 16:59:22 +01001114 @VisibleForTesting
1115 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1116 if (config == null) {
1117 return;
1118 }
1119 if (isBrightnessConfigurationTooDark(config)) {
1120 throw new IllegalArgumentException("brightness curve is too dark");
1121 }
1122 }
1123
1124 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1125 Pair<float[], float[]> curve = config.getCurve();
1126 float[] lux = curve.first;
1127 float[] nits = curve.second;
1128 for (int i = 0; i < lux.length; i++) {
1129 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1130 return true;
1131 }
1132 }
1133 return false;
1134 }
1135
Michael Wrighteef0e132017-11-21 17:57:52 +00001136 private void loadBrightnessConfiguration() {
1137 synchronized (mSyncRoot) {
1138 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1139 BrightnessConfiguration config =
1140 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1141 mDisplayPowerController.setBrightnessConfiguration(config);
1142 }
1143 }
1144
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001145 // Updates all existing logical displays given the current set of display devices.
1146 // Removes invalid logical displays.
1147 // Sends notifications if needed.
1148 private boolean updateLogicalDisplaysLocked() {
1149 boolean changed = false;
1150 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1151 final int displayId = mLogicalDisplays.keyAt(i);
1152 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1153
1154 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1155 display.updateLocked(mDisplayDevices);
1156 if (!display.isValidLocked()) {
1157 mLogicalDisplays.removeAt(i);
1158 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1159 changed = true;
1160 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001161 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001162 changed = true;
1163 }
1164 }
1165 return changed;
1166 }
1167
Robert Carrae606b42018-02-15 15:36:23 -08001168 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001169 // Clear all viewports before configuring displays so that we can keep
1170 // track of which ones we have configured.
1171 clearViewportsLocked();
1172
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001173 // Configure each display device.
1174 final int count = mDisplayDevices.size();
1175 for (int i = 0; i < count; i++) {
1176 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001177 configureDisplayLocked(t, device);
1178 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001179 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001180
1181 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001182 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001183 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1184 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001185 }
1186
Michael Wright3f145a22014-07-22 19:46:03 -07001187 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001188 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001189 synchronized (mSyncRoot) {
1190 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001191 if (display == null) {
1192 return;
1193 }
1194 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001195 if (DEBUG) {
1196 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1197 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1198 }
1199
Craig Mautner722285e2012-09-07 13:55:58 -07001200 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001201 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001202 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001203 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1204 // Scan supported modes returned by display.getInfo() to find a mode with the same
1205 // size as the default display mode but with the specified refresh rate instead.
1206 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1207 requestedRefreshRate);
1208 }
1209 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001210 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001211 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001212 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001213 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001214 scheduleTraversalLocked(inTraversal);
1215 }
Craig Mautner722285e2012-09-07 13:55:58 -07001216 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001217 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001218
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001219 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1220 synchronized (mSyncRoot) {
1221 LogicalDisplay display = mLogicalDisplays.get(displayId);
1222 if (display == null) {
1223 return;
1224 }
1225 if (display.getDisplayOffsetXLocked() != x
1226 || display.getDisplayOffsetYLocked() != y) {
1227 if (DEBUG) {
1228 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1229 + x + ", " + y + ")");
1230 }
1231 display.setDisplayOffsetsLocked(x, y);
1232 scheduleTraversalLocked(false);
1233 }
1234 }
1235 }
1236
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001237 // Updates the lists of UIDs that are present on displays.
1238 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1239 synchronized (mSyncRoot) {
1240 mDisplayAccessUIDs.clear();
1241 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1242 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1243 newDisplayAccessUIDs.valueAt(i));
1244 }
1245 }
1246 }
1247
1248 // Checks if provided UID's content is present on the display and UID has access to it.
1249 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1250 synchronized (mSyncRoot) {
1251 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1252 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1253 }
1254 }
1255
Jeff Brownd728bf52012-09-08 18:05:28 -07001256 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001257 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001258 }
1259
Robert Carrae606b42018-02-15 15:36:23 -08001260 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001261 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1262 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001263
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001264 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001265 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001266 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001267 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001268 if (display != null && !display.hasContentLocked()) {
1269 // If the display does not have any content of its own, then
1270 // automatically mirror the default logical display contents.
1271 display = null;
1272 }
1273 if (display == null) {
1274 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1275 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001276 }
1277
1278 // Apply the logical display configuration to the display device.
1279 if (display == null) {
1280 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001281 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001282 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001283 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001284 }
Robert Carrae606b42018-02-15 15:36:23 -08001285 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001286
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001287 // Update the corresponding viewport.
1288 DisplayViewport internalViewport = getInternalViewportLocked();
1289 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1290 populateViewportLocked(internalViewport, display, device);
Jeff Brownd728bf52012-09-08 18:05:28 -07001291 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001292 DisplayViewport externalViewport = getExternalViewportLocked();
1293 if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1294 populateViewportLocked(externalViewport, display, device);
1295 } else if (!externalViewport.valid) {
1296 // TODO (b/116850516) move this logic into InputReader
1297 externalViewport.copyFrom(internalViewport);
1298 externalViewport.type = DisplayViewport.VIEWPORT_EXTERNAL;
Jeff Brownd728bf52012-09-08 18:05:28 -07001299 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001300
1301 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001302 final DisplayViewport viewport = getVirtualViewportLocked(info.uniqueId);
1303 populateViewportLocked(viewport, display, device);
Santos Cordonee8931e2017-04-05 10:31:15 -07001304 }
1305 }
1306
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001307 /** Get the virtual device viewport that has the specified uniqueId.
1308 * If such viewport does not exist, create it. */
1309 private DisplayViewport getVirtualViewportLocked(@NonNull String uniqueId) {
Santos Cordonee8931e2017-04-05 10:31:15 -07001310 DisplayViewport viewport;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001311 final int count = mViewports.size();
Santos Cordonee8931e2017-04-05 10:31:15 -07001312 for (int i = 0; i < count; i++) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001313 viewport = mViewports.get(i);
Santos Cordonee8931e2017-04-05 10:31:15 -07001314 if (uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001315 if (viewport.type != VIEWPORT_VIRTUAL) {
1316 Slog.wtf(TAG, "Found a viewport with uniqueId '" + uniqueId
1317 + "' but it has type " + DisplayViewport.typeToString(viewport.type)
1318 + " (expected VIRTUAL)");
1319 continue;
1320 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001321 return viewport;
1322 }
1323 }
1324
1325 viewport = new DisplayViewport();
1326 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001327 viewport.type = VIEWPORT_VIRTUAL;
1328 mViewports.add(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001329 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001330 }
1331
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001332 private DisplayViewport getInternalViewportLocked() {
1333 return getViewportByTypeLocked(VIEWPORT_INTERNAL);
1334 }
1335
1336 private DisplayViewport getExternalViewportLocked() {
1337 return getViewportByTypeLocked(VIEWPORT_EXTERNAL);
1338 }
1339
1340 /**
1341 * Get internal or external viewport. Create it if does not currently exist.
1342 * @param viewportType - either INTERNAL or EXTERNAL
1343 * @return the viewport with the requested type
1344 */
1345 private DisplayViewport getViewportByTypeLocked(int viewportType) {
1346 // Only allow a single INTERNAL or EXTERNAL viewport, which makes this function possible.
1347 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1348 // Creates the viewport if none exists.
1349 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL) {
1350 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1351 + DisplayViewport.typeToString(viewportType));
1352 return null;
1353 }
1354 DisplayViewport viewport;
1355 final int count = mViewports.size();
1356 for (int i = 0; i < count; i++) {
1357 viewport = mViewports.get(i);
1358 if (viewport.type == viewportType) {
1359 return viewport;
1360 }
1361 }
1362
1363 viewport = new DisplayViewport();
1364 viewport.type = viewportType;
1365 mViewports.add(viewport);
1366 return viewport;
1367 }
1368
1369 private static void populateViewportLocked(DisplayViewport viewport,
Jeff Brownd728bf52012-09-08 18:05:28 -07001370 LogicalDisplay display, DisplayDevice device) {
1371 viewport.valid = true;
1372 viewport.displayId = display.getDisplayIdLocked();
1373 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001374 }
1375
1376 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1377 final int count = mLogicalDisplays.size();
1378 for (int i = 0; i < count; i++) {
1379 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1380 if (display.getPrimaryDisplayDeviceLocked() == device) {
1381 return display;
1382 }
1383 }
1384 return null;
1385 }
1386
Jeff Brownbd6e1502012-08-28 03:27:37 -07001387 private void sendDisplayEventLocked(int displayId, int event) {
1388 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1389 mHandler.sendMessage(msg);
1390 }
1391
Robert Carrae606b42018-02-15 15:36:23 -08001392 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001393 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001394 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001395 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001396 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001397 if (!inTraversal) {
1398 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1399 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001400 }
1401 }
1402
1403 // Runs on Handler thread.
1404 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001405 private void deliverDisplayEvent(int displayId, int event) {
1406 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001407 Slog.d(TAG, "Delivering display event: displayId="
1408 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001409 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001410
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001411 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001412 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001413 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001414 count = mCallbacks.size();
1415 mTempCallbacks.clear();
1416 for (int i = 0; i < count; i++) {
1417 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001418 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001419 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001420
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001421 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001422 for (int i = 0; i < count; i++) {
1423 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1424 }
1425 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001426 }
1427
Michael Wrightc39d47a2014-07-08 18:07:36 -07001428 private IMediaProjectionManager getProjectionService() {
1429 if (mProjectionService == null) {
1430 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1431 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1432 }
1433 return mProjectionService;
1434 }
1435
Michael Wrighteef0e132017-11-21 17:57:52 +00001436 private UserManager getUserManager() {
1437 return mContext.getSystemService(UserManager.class);
1438 }
1439
Jeff Brown4ccb8232014-01-16 22:16:42 -08001440 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001441 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001442
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001443 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001444 pw.println(" mOnlyCode=" + mOnlyCore);
1445 pw.println(" mSafeMode=" + mSafeMode);
1446 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001447 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001448 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001449 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001450 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001451 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001452 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001453 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1454
Jeff Brown9e316a12012-10-08 19:17:06 -07001455
Jeff Brownbd6e1502012-08-28 03:27:37 -07001456 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001457 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001458
1459 pw.println();
1460 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001461 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001462 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001463 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001464 }
Craig Mautner9de49362012-08-02 14:30:30 -07001465
Jeff Brownbd6e1502012-08-28 03:27:37 -07001466 pw.println();
1467 pw.println("Display Devices: size=" + mDisplayDevices.size());
1468 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001469 pw.println(" " + device.getDisplayDeviceInfoLocked());
1470 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001471 }
1472
1473 final int logicalDisplayCount = mLogicalDisplays.size();
1474 pw.println();
1475 pw.println("Logical Displays: size=" + logicalDisplayCount);
1476 for (int i = 0; i < logicalDisplayCount; i++) {
1477 int displayId = mLogicalDisplays.keyAt(i);
1478 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1479 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001480 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001481 }
Jeff Brownce468a32013-11-21 16:42:03 -08001482
1483 final int callbackCount = mCallbacks.size();
1484 pw.println();
1485 pw.println("Callbacks: size=" + callbackCount);
1486 for (int i = 0; i < callbackCount; i++) {
1487 CallbackRecord callback = mCallbacks.valueAt(i);
1488 pw.println(" " + i + ": mPid=" + callback.mPid
1489 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1490 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001491
1492 if (mDisplayPowerController != null) {
1493 mDisplayPowerController.dump(pw);
1494 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001495
1496 pw.println();
1497 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001498 }
1499 }
1500
Dan Gittik122df862018-03-28 16:59:22 +01001501 private static float[] getFloatArray(TypedArray array) {
1502 int length = array.length();
1503 float[] floatArray = new float[length];
1504 for (int i = 0; i < length; i++) {
1505 floatArray[i] = array.getFloat(i, Float.NaN);
1506 }
1507 array.recycle();
1508 return floatArray;
1509 }
1510
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001511 /**
1512 * This is the object that everything in the display manager locks on.
1513 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1514 * clear that the object belongs to the display manager service and that it is
1515 * a unique object with a special purpose.
1516 */
1517 public static final class SyncRoot {
1518 }
1519
Santos Cordonee8931e2017-04-05 10:31:15 -07001520 @VisibleForTesting
1521 static class Injector {
1522 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1523 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1524 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1525 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001526
1527 long getDefaultDisplayDelayTimeout() {
1528 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1529 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001530 }
1531
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001532 @VisibleForTesting
1533 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1534 synchronized (mSyncRoot) {
1535 LogicalDisplay display = mLogicalDisplays.get(displayId);
1536 if (display != null) {
1537 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1538 return displayDevice.getDisplayDeviceInfoLocked();
1539 }
1540 return null;
1541 }
1542 }
1543
Jeff Brownbd6e1502012-08-28 03:27:37 -07001544 private final class DisplayManagerHandler extends Handler {
1545 public DisplayManagerHandler(Looper looper) {
1546 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001547 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001548
Jeff Brownbd6e1502012-08-28 03:27:37 -07001549 @Override
1550 public void handleMessage(Message msg) {
1551 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001552 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1553 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001554 break;
1555
1556 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1557 registerAdditionalDisplayAdapters();
1558 break;
1559
1560 case MSG_DELIVER_DISPLAY_EVENT:
1561 deliverDisplayEvent(msg.arg1, msg.arg2);
1562 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001563
1564 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001565 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001566 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001567
1568 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001569 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001570 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001571 changed = !mTempViewports.equals(mViewports);
1572 if (changed) {
1573 mTempViewports.clear();
1574 for (DisplayViewport d : mViewports) {
1575 mTempViewports.add(d.makeCopy());
1576 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001577 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001578 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001579 if (changed) {
1580 mInputManagerInternal.setDisplayViewports(mTempViewports);
1581 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001582 break;
1583 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001584
Michael Wrighteef0e132017-11-21 17:57:52 +00001585 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1586 loadBrightnessConfiguration();
1587 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001588 }
1589 }
1590 }
1591
1592 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1593 @Override
1594 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1595 switch (event) {
1596 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1597 handleDisplayDeviceAdded(device);
1598 break;
1599
1600 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1601 handleDisplayDeviceChanged(device);
1602 break;
1603
1604 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1605 handleDisplayDeviceRemoved(device);
1606 break;
1607 }
1608 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001609
1610 @Override
1611 public void onTraversalRequested() {
1612 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001613 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001614 }
1615 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001616 }
1617
1618 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001619 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001620 private final IDisplayManagerCallback mCallback;
1621
Jeff Brownce468a32013-11-21 16:42:03 -08001622 public boolean mWifiDisplayScanRequested;
1623
Jeff Brownbd6e1502012-08-28 03:27:37 -07001624 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1625 mPid = pid;
1626 mCallback = callback;
1627 }
1628
1629 @Override
1630 public void binderDied() {
1631 if (DEBUG) {
1632 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1633 }
Jeff Brownce468a32013-11-21 16:42:03 -08001634 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001635 }
1636
1637 public void notifyDisplayEventAsync(int displayId, int event) {
1638 try {
1639 mCallback.onDisplayEvent(displayId, event);
1640 } catch (RemoteException ex) {
1641 Slog.w(TAG, "Failed to notify process "
1642 + mPid + " that displays changed, assuming it died.", ex);
1643 binderDied();
1644 }
1645 }
1646 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001647
Santos Cordonee8931e2017-04-05 10:31:15 -07001648 @VisibleForTesting
1649 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001650 /**
1651 * Returns information about the specified logical display.
1652 *
1653 * @param displayId The logical display id.
1654 * @return The logical display info, or null if the display does not exist. The
1655 * returned object must be treated as immutable.
1656 */
1657 @Override // Binder call
1658 public DisplayInfo getDisplayInfo(int displayId) {
1659 final int callingUid = Binder.getCallingUid();
1660 final long token = Binder.clearCallingIdentity();
1661 try {
1662 return getDisplayInfoInternal(displayId, callingUid);
1663 } finally {
1664 Binder.restoreCallingIdentity(token);
1665 }
1666 }
1667
1668 /**
1669 * Returns the list of all display ids.
1670 */
1671 @Override // Binder call
1672 public int[] getDisplayIds() {
1673 final int callingUid = Binder.getCallingUid();
1674 final long token = Binder.clearCallingIdentity();
1675 try {
1676 return getDisplayIdsInternal(callingUid);
1677 } finally {
1678 Binder.restoreCallingIdentity(token);
1679 }
1680 }
1681
Michael Wrighteedcbf12017-08-16 23:14:54 +01001682 /**
1683 * Returns the stable device display size, in pixels.
1684 */
1685 @Override // Binder call
1686 public Point getStableDisplaySize() {
1687 final long token = Binder.clearCallingIdentity();
1688 try {
1689 return getStableDisplaySizeInternal();
1690 } finally {
1691 Binder.restoreCallingIdentity(token);
1692 }
1693 }
1694
Jeff Brown4ccb8232014-01-16 22:16:42 -08001695 @Override // Binder call
1696 public void registerCallback(IDisplayManagerCallback callback) {
1697 if (callback == null) {
1698 throw new IllegalArgumentException("listener must not be null");
1699 }
1700
1701 final int callingPid = Binder.getCallingPid();
1702 final long token = Binder.clearCallingIdentity();
1703 try {
1704 registerCallbackInternal(callback, callingPid);
1705 } finally {
1706 Binder.restoreCallingIdentity(token);
1707 }
1708 }
1709
1710 @Override // Binder call
1711 public void startWifiDisplayScan() {
1712 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1713 "Permission required to start wifi display scans");
1714
1715 final int callingPid = Binder.getCallingPid();
1716 final long token = Binder.clearCallingIdentity();
1717 try {
1718 startWifiDisplayScanInternal(callingPid);
1719 } finally {
1720 Binder.restoreCallingIdentity(token);
1721 }
1722 }
1723
1724 @Override // Binder call
1725 public void stopWifiDisplayScan() {
1726 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1727 "Permission required to stop wifi display scans");
1728
1729 final int callingPid = Binder.getCallingPid();
1730 final long token = Binder.clearCallingIdentity();
1731 try {
1732 stopWifiDisplayScanInternal(callingPid);
1733 } finally {
1734 Binder.restoreCallingIdentity(token);
1735 }
1736 }
1737
1738 @Override // Binder call
1739 public void connectWifiDisplay(String address) {
1740 if (address == null) {
1741 throw new IllegalArgumentException("address must not be null");
1742 }
1743 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1744 "Permission required to connect to a wifi display");
1745
1746 final long token = Binder.clearCallingIdentity();
1747 try {
1748 connectWifiDisplayInternal(address);
1749 } finally {
1750 Binder.restoreCallingIdentity(token);
1751 }
1752 }
1753
1754 @Override // Binder call
1755 public void disconnectWifiDisplay() {
1756 // This request does not require special permissions.
1757 // Any app can request disconnection from the currently active wifi display.
1758 // This exception should no longer be needed once wifi display control moves
1759 // to the media router service.
1760
1761 final long token = Binder.clearCallingIdentity();
1762 try {
1763 disconnectWifiDisplayInternal();
1764 } finally {
1765 Binder.restoreCallingIdentity(token);
1766 }
1767 }
1768
1769 @Override // Binder call
1770 public void renameWifiDisplay(String address, String alias) {
1771 if (address == null) {
1772 throw new IllegalArgumentException("address must not be null");
1773 }
1774 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1775 "Permission required to rename to a wifi display");
1776
1777 final long token = Binder.clearCallingIdentity();
1778 try {
1779 renameWifiDisplayInternal(address, alias);
1780 } finally {
1781 Binder.restoreCallingIdentity(token);
1782 }
1783 }
1784
1785 @Override // Binder call
1786 public void forgetWifiDisplay(String address) {
1787 if (address == null) {
1788 throw new IllegalArgumentException("address must not be null");
1789 }
1790 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1791 "Permission required to forget to a wifi display");
1792
1793 final long token = Binder.clearCallingIdentity();
1794 try {
1795 forgetWifiDisplayInternal(address);
1796 } finally {
1797 Binder.restoreCallingIdentity(token);
1798 }
1799 }
1800
1801 @Override // Binder call
1802 public void pauseWifiDisplay() {
1803 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1804 "Permission required to pause a wifi display session");
1805
1806 final long token = Binder.clearCallingIdentity();
1807 try {
1808 pauseWifiDisplayInternal();
1809 } finally {
1810 Binder.restoreCallingIdentity(token);
1811 }
1812 }
1813
1814 @Override // Binder call
1815 public void resumeWifiDisplay() {
1816 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1817 "Permission required to resume a wifi display session");
1818
1819 final long token = Binder.clearCallingIdentity();
1820 try {
1821 resumeWifiDisplayInternal();
1822 } finally {
1823 Binder.restoreCallingIdentity(token);
1824 }
1825 }
1826
1827 @Override // Binder call
1828 public WifiDisplayStatus getWifiDisplayStatus() {
1829 // This request does not require special permissions.
1830 // Any app can get information about available wifi displays.
1831
1832 final long token = Binder.clearCallingIdentity();
1833 try {
1834 return getWifiDisplayStatusInternal();
1835 } finally {
1836 Binder.restoreCallingIdentity(token);
1837 }
1838 }
1839
1840 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001841 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001842 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001843 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1844 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001845 final long token = Binder.clearCallingIdentity();
1846 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001847 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001848 } finally {
1849 Binder.restoreCallingIdentity(token);
1850 }
1851 }
1852
1853 @Override // Binder call
Bryan Mawhinney462e29d2018-03-22 15:52:41 +00001854 public void setSaturationLevel(float level) {
1855 mContext.enforceCallingOrSelfPermission(
1856 Manifest.permission.CONTROL_DISPLAY_SATURATION,
1857 "Permission required to set display saturation level");
1858 final long token = Binder.clearCallingIdentity();
1859 try {
1860 setSaturationLevelInternal(level);
1861 } finally {
1862 Binder.restoreCallingIdentity(token);
1863 }
1864 }
1865
1866 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001867 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001868 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001869 int width, int height, int densityDpi, Surface surface, int flags,
1870 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001871 final int callingUid = Binder.getCallingUid();
1872 if (!validatePackageName(callingUid, packageName)) {
1873 throw new SecurityException("packageName must match the calling uid");
1874 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001875 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001876 throw new IllegalArgumentException("appToken must not be null");
1877 }
1878 if (TextUtils.isEmpty(name)) {
1879 throw new IllegalArgumentException("name must be non-null and non-empty");
1880 }
1881 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1882 throw new IllegalArgumentException("width, height, and densityDpi must be "
1883 + "greater than 0");
1884 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001885 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001886 throw new IllegalArgumentException("Surface can't be single-buffered");
1887 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001888
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001889 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1890 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1891
1892 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001893 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001894 throw new IllegalArgumentException(
1895 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1896 }
Michael Wright6720be42014-07-29 19:14:16 -07001897 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001898 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1899 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001900 }
1901
Michael Wrightc39d47a2014-07-08 18:07:36 -07001902 if (projection != null) {
1903 try {
1904 if (!getProjectionService().isValidMediaProjection(projection)) {
1905 throw new SecurityException("Invalid media projection");
1906 }
Michael Wright6720be42014-07-29 19:14:16 -07001907 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001908 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001909 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001910 }
1911 }
1912
Michael Wright6720be42014-07-29 19:14:16 -07001913 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001914 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001915 if (!canProjectVideo(projection)) {
1916 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1917 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1918 + "MediaProjection token in order to create a screen sharing virtual "
1919 + "display.");
1920 }
1921 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001922 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001923 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001924 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001925 + "or an appropriate MediaProjection token to create a "
1926 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001927 }
1928 }
1929
1930 final long token = Binder.clearCallingIdentity();
1931 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001932 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1933 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001934 } finally {
1935 Binder.restoreCallingIdentity(token);
1936 }
1937 }
1938
1939 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001940 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001941 int width, int height, int densityDpi) {
1942 final long token = Binder.clearCallingIdentity();
1943 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001944 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001945 } finally {
1946 Binder.restoreCallingIdentity(token);
1947 }
1948 }
1949
1950 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001951 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001952 if (surface != null && surface.isSingleBuffered()) {
1953 throw new IllegalArgumentException("Surface can't be single-buffered");
1954 }
Jeff Brown92207df2014-04-16 13:16:07 -07001955 final long token = Binder.clearCallingIdentity();
1956 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001957 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001958 } finally {
1959 Binder.restoreCallingIdentity(token);
1960 }
1961 }
1962
1963 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001964 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001965 final long token = Binder.clearCallingIdentity();
1966 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001967 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001968 } finally {
1969 Binder.restoreCallingIdentity(token);
1970 }
1971 }
1972
1973 @Override // Binder call
1974 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001975 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001976
1977 final long token = Binder.clearCallingIdentity();
1978 try {
1979 dumpInternal(pw);
1980 } finally {
1981 Binder.restoreCallingIdentity(token);
1982 }
1983 }
1984
Kenny Guy22bd0442017-10-26 00:15:54 +01001985 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001986 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001987 mContext.enforceCallingOrSelfPermission(
1988 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1989 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001990
1991 final int callingUid = Binder.getCallingUid();
1992 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00001993 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00001994 callingUid, callingPackage);
1995 final boolean hasUsageStats;
1996 if (mode == AppOpsManager.MODE_DEFAULT) {
1997 // The default behavior here is to check if PackageManager has given the app
1998 // permission.
1999 hasUsageStats = mContext.checkCallingPermission(
2000 Manifest.permission.PACKAGE_USAGE_STATS)
2001 == PackageManager.PERMISSION_GRANTED;
2002 } else {
2003 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2004 }
2005
2006 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002007 final long token = Binder.clearCallingIdentity();
2008 try {
Michael Wright144aac92017-12-21 18:37:41 +00002009 synchronized (mSyncRoot) {
2010 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2011 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002012 } finally {
2013 Binder.restoreCallingIdentity(token);
2014 }
2015 }
2016
Michael Wrighteef0e132017-11-21 17:57:52 +00002017 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002018 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2019 mContext.enforceCallingOrSelfPermission(
2020 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2021 "Permission required to to access ambient light stats.");
2022 final int callingUid = Binder.getCallingUid();
2023 final int userId = UserHandle.getUserId(callingUid);
2024 final long token = Binder.clearCallingIdentity();
2025 try {
2026 synchronized (mSyncRoot) {
2027 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2028 }
2029 } finally {
2030 Binder.restoreCallingIdentity(token);
2031 }
2032 }
2033
2034 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002035 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002036 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002037 mContext.enforceCallingOrSelfPermission(
2038 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2039 "Permission required to change the display's brightness configuration");
2040 if (userId != UserHandle.getCallingUserId()) {
2041 mContext.enforceCallingOrSelfPermission(
2042 Manifest.permission.INTERACT_ACROSS_USERS,
2043 "Permission required to change the display brightness"
2044 + " configuration of another user");
2045 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002046 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2047 packageName = null;
2048 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002049 final long token = Binder.clearCallingIdentity();
2050 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002051 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002052 } finally {
2053 Binder.restoreCallingIdentity(token);
2054 }
2055 }
2056
Michael Wrightd8460232018-01-16 18:04:59 +00002057 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002058 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2059 mContext.enforceCallingOrSelfPermission(
2060 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2061 "Permission required to read the display's brightness configuration");
2062 if (userId != UserHandle.getCallingUserId()) {
2063 mContext.enforceCallingOrSelfPermission(
2064 Manifest.permission.INTERACT_ACROSS_USERS,
2065 "Permission required to read the display brightness"
2066 + " configuration of another user");
2067 }
2068 final long token = Binder.clearCallingIdentity();
2069 try {
2070 final int userSerial = getUserManager().getUserSerialNumber(userId);
2071 synchronized (mSyncRoot) {
2072 BrightnessConfiguration config =
2073 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2074 if (config == null) {
2075 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2076 }
2077 return config;
2078 }
2079 } finally {
2080 Binder.restoreCallingIdentity(token);
2081 }
2082 }
2083
2084 @Override // Binder call
2085 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2086 mContext.enforceCallingOrSelfPermission(
2087 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2088 "Permission required to read the display's default brightness configuration");
2089 final long token = Binder.clearCallingIdentity();
2090 try {
2091 synchronized (mSyncRoot) {
2092 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2093 }
2094 } finally {
2095 Binder.restoreCallingIdentity(token);
2096 }
2097 }
2098
2099 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002100 public void setTemporaryBrightness(int brightness) {
2101 mContext.enforceCallingOrSelfPermission(
2102 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2103 "Permission required to set the display's brightness");
2104 final long token = Binder.clearCallingIdentity();
2105 try {
2106 synchronized (mSyncRoot) {
2107 mDisplayPowerController.setTemporaryBrightness(brightness);
2108 }
2109 } finally {
2110 Binder.restoreCallingIdentity(token);
2111 }
2112 }
2113
2114 @Override // Binder call
2115 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2116 mContext.enforceCallingOrSelfPermission(
2117 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2118 "Permission required to set the display's auto brightness adjustment");
2119 final long token = Binder.clearCallingIdentity();
2120 try {
2121 synchronized (mSyncRoot) {
2122 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2123 }
2124 } finally {
2125 Binder.restoreCallingIdentity(token);
2126 }
2127 }
2128
Dan Gittik7a32fba2018-03-28 12:19:38 +01002129 @Override // Binder call
2130 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2131 FileDescriptor err, String[] args, ShellCallback callback,
2132 ResultReceiver resultReceiver) {
2133 final long token = Binder.clearCallingIdentity();
2134 try {
2135 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2136 command.exec(this, in, out, err, args, callback, resultReceiver);
2137 } finally {
2138 Binder.restoreCallingIdentity(token);
2139 }
2140 }
2141
Dan Gittik122df862018-03-28 16:59:22 +01002142 @Override // Binder call
2143 public Curve getMinimumBrightnessCurve() {
2144 final long token = Binder.clearCallingIdentity();
2145 try {
2146 return getMinimumBrightnessCurveInternal();
2147 } finally {
2148 Binder.restoreCallingIdentity(token);
2149 }
2150 }
2151
Dan Gittik7a32fba2018-03-28 12:19:38 +01002152 void setBrightness(int brightness) {
2153 Settings.System.putIntForUser(mContext.getContentResolver(),
2154 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2155 }
2156
2157 void resetBrightnessConfiguration() {
2158 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2159 mContext.getPackageName());
2160 }
2161
Jeff Brown4ccb8232014-01-16 22:16:42 -08002162 private boolean validatePackageName(int uid, String packageName) {
2163 if (packageName != null) {
2164 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2165 if (packageNames != null) {
2166 for (String n : packageNames) {
2167 if (n.equals(packageName)) {
2168 return true;
2169 }
2170 }
2171 }
2172 }
2173 return false;
2174 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002175
2176 private boolean canProjectVideo(IMediaProjection projection) {
2177 if (projection != null) {
2178 try {
2179 if (projection.canProjectVideo()) {
2180 return true;
2181 }
2182 } catch (RemoteException e) {
2183 Slog.e(TAG, "Unable to query projection service for permissions", e);
2184 }
2185 }
2186 if (mContext.checkCallingPermission(
2187 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002188 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002189 return true;
2190 }
2191 return canProjectSecureVideo(projection);
2192 }
2193
2194 private boolean canProjectSecureVideo(IMediaProjection projection) {
2195 if (projection != null) {
2196 try {
2197 if (projection.canProjectSecureVideo()){
2198 return true;
2199 }
2200 } catch (RemoteException e) {
2201 Slog.e(TAG, "Unable to query projection service for permissions", e);
2202 }
2203 }
2204 return mContext.checkCallingPermission(
2205 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002206 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002207 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002208 }
2209
2210 private final class LocalService extends DisplayManagerInternal {
2211 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002212 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002213 SensorManager sensorManager) {
2214 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002215 DisplayBlanker blanker = new DisplayBlanker() {
2216 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002217 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002218 // The order of operations is important for legacy reasons.
2219 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002220 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002221 }
2222
2223 callbacks.onDisplayStateChange(state);
2224
2225 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002226 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002227 }
2228 }
2229 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002230 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002231 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002232 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002233
2234 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002235 }
2236
2237 @Override
2238 public boolean requestPowerState(DisplayPowerRequest request,
2239 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002240 synchronized (mSyncRoot) {
2241 return mDisplayPowerController.requestPowerState(request,
2242 waitForNegativeProximity);
2243 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002244 }
2245
2246 @Override
2247 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002248 synchronized (mSyncRoot) {
2249 return mDisplayPowerController.isProximitySensorAvailable();
2250 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002251 }
2252
2253 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002254 public boolean screenshot(int displayId, Surface outSurface) {
2255 synchronized (mSyncRoot) {
2256 final LogicalDisplay display = mLogicalDisplays.get(displayId);
2257 if (display != null) {
2258 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
2259 if (device != null) {
2260 final IBinder token = device.getDisplayTokenLocked();
2261 if (token != null) {
2262 SurfaceControl.screenshot(token, outSurface);
2263 return true;
2264 }
2265 }
2266 }
2267 }
2268 return false;
2269 }
2270
2271 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002272 public DisplayInfo getDisplayInfo(int displayId) {
2273 return getDisplayInfoInternal(displayId, Process.myUid());
2274 }
2275
2276 @Override
2277 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2278 if (listener == null) {
2279 throw new IllegalArgumentException("listener must not be null");
2280 }
2281
2282 registerDisplayTransactionListenerInternal(listener);
2283 }
2284
2285 @Override
2286 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2287 if (listener == null) {
2288 throw new IllegalArgumentException("listener must not be null");
2289 }
2290
2291 unregisterDisplayTransactionListenerInternal(listener);
2292 }
2293
2294 @Override
2295 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2296 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2297 }
2298
2299 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002300 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2301 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2302 }
2303
2304 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002305 public void performTraversal(SurfaceControl.Transaction t) {
2306 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002307 }
2308
2309 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002310 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002311 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2312 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2313 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002314 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002315
2316 @Override
2317 public void setDisplayOffsets(int displayId, int x, int y) {
2318 setDisplayOffsetsInternal(displayId, x, y);
2319 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002320
2321 @Override
2322 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2323 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2324 }
2325
2326 @Override
2327 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2328 return isUidPresentOnDisplayInternal(uid, displayId);
2329 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002330
2331 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002332 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002333 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002334 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002335 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002336 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002337
2338 @Override
2339 public void onOverlayChanged() {
2340 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002341 for (int i = 0; i < mDisplayDevices.size(); i++) {
2342 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002343 }
2344 }
2345 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002346 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002347}