blob: 360a7d105cce3c467809554ea42a5589c197d50d [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 // The top inset of the default display.
251 // This gets persisted so that the boot animation knows how to transition from the display's
252 // full size to the size configured by the user. Right now we only persist and animate the top
253 // inset, but theoretically we could do it for all of them.
254 private int mDefaultDisplayTopInset;
255
Jeff Brownd728bf52012-09-08 18:05:28 -0700256 // Viewports of the default display and the display that should receive touch
257 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100258 @GuardedBy("mSyncRoot")
259 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700260
Jeff Brown89d55462012-09-19 11:33:42 -0700261 // Persistent data store for all internal settings maintained by the display manager service.
262 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
263
Jeff Brownbd6e1502012-08-28 03:27:37 -0700264 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700265 // May be used outside of the lock but only on the handler thread.
266 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700267
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700268 // Temporary display info, used for comparing display configurations.
269 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
270
Jeff Brownd728bf52012-09-08 18:05:28 -0700271 // Temporary viewports, used when sending new viewport information to the
272 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100273 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700274
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700275 // The default color mode for default displays. Overrides the usual
276 // Display.Display.COLOR_MODE_DEFAULT for displays with the
277 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
278 private final int mDefaultDisplayDefaultColorMode;
279
Jeff Browne75926d2014-09-18 15:24:49 -0700280 // Temporary list of deferred work to perform when setting the display state.
281 // Only used by requestDisplayState. The field is self-synchronized and only
282 // intended for use inside of the requestGlobalDisplayStateInternal function.
283 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
284
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800285 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
286 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
287
Santos Cordonee8931e2017-04-05 10:31:15 -0700288 private final Injector mInjector;
289
Dan Gittik122df862018-03-28 16:59:22 +0100290 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
291 // is rejected by the system.
292 private final Curve mMinimumBrightnessCurve;
293 private final Spline mMinimumBrightnessSpline;
294
Jeff Brownb880d882014-02-10 19:47:07 -0800295 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700296 this(context, new Injector());
297 }
298
299 @VisibleForTesting
300 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800301 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700302 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800303 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800304 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700305 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700306 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700307 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100308 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700309 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000310 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200311 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100312 float[] lux = getFloatArray(resources.obtainTypedArray(
313 com.android.internal.R.array.config_minimumBrightnessCurveLux));
314 float[] nits = getFloatArray(resources.obtainTypedArray(
315 com.android.internal.R.array.config_minimumBrightnessCurveNits));
316 mMinimumBrightnessCurve = new Curve(lux, nits);
317 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700318
319 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
320 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000321 mCurrentUserId = UserHandle.USER_SYSTEM;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700322 }
323
324 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100325 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000326 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
327 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100328 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
329 Process.THREAD_GROUP_TOP_APP);
330 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
331 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200332 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
333 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700334 }
335
Jeff Brown4ccb8232014-01-16 22:16:42 -0800336 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800337 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700338 // We need to pre-load the persistent data store so it's ready before the default display
339 // adapter is up so that we have it's configuration. We could load it lazily, but since
340 // 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 -0700341 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100342 synchronized(mSyncRoot) {
343 mPersistentDataStore.loadIfNeeded();
344 loadStableDisplayValuesLocked();
345 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700346 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800347
348 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
349 true /*allowIsolated*/);
350 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700351 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800352 }
353
354 @Override
355 public void onBootPhase(int phase) {
356 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
357 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700358 long timeout = SystemClock.uptimeMillis()
359 + mInjector.getDefaultDisplayDelayTimeout();
360 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
361 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800362 long delay = timeout - SystemClock.uptimeMillis();
363 if (delay <= 0) {
364 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700365 + "to be initialized. DefaultDisplay="
366 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
367 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800368 }
369 if (DEBUG) {
370 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
371 }
372 try {
373 mSyncRoot.wait(delay);
374 } catch (InterruptedException ex) {
375 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700376 }
377 }
378 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700379 }
380
Michael Wrighteef0e132017-11-21 17:57:52 +0000381 @Override
382 public void onSwitchUser(@UserIdInt int newUserId) {
383 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
384 synchronized (mSyncRoot) {
385 if (mCurrentUserId != newUserId) {
386 mCurrentUserId = newUserId;
387 BrightnessConfiguration config =
388 mPersistentDataStore.getBrightnessConfiguration(userSerial);
389 mDisplayPowerController.setBrightnessConfiguration(config);
390 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000391 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000392 }
393 }
394
Jeff Brown4ccb8232014-01-16 22:16:42 -0800395 // TODO: Use dependencies or a boot phase
396 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700397 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800398 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
399 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700400 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700401 }
402 }
403
404 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700405 * Called when the system is ready to go.
406 */
407 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700408 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700409 mSafeMode = safeMode;
410 mOnlyCore = onlyCore;
411 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700412
Jeff Brownbd6e1502012-08-28 03:27:37 -0700413 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100414 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 }
416
Santos Cordonee8931e2017-04-05 10:31:15 -0700417 @VisibleForTesting
418 Handler getDisplayHandler() {
419 return mHandler;
420 }
421
Michael Wrighteedcbf12017-08-16 23:14:54 +0100422 private void loadStableDisplayValuesLocked() {
423 final Point size = mPersistentDataStore.getStableDisplaySize();
424 if (size.x > 0 && size.y > 0) {
425 // Just set these values directly so we don't write the display persistent data again
426 // unnecessarily
427 mStableDisplaySize.set(size.x, size.y);
428 } else {
429 final Resources res = mContext.getResources();
430 final int width = res.getInteger(
431 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
432 final int height = res.getInteger(
433 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
434 if (width > 0 && height > 0) {
435 setStableDisplaySizeLocked(width, height);
436 }
437 }
438 }
439
440 private Point getStableDisplaySizeInternal() {
441 Point r = new Point();
442 synchronized (mSyncRoot) {
443 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
444 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
445 }
446 }
447 return r;
448 }
449
Jeff Brown4ccb8232014-01-16 22:16:42 -0800450 private void registerDisplayTransactionListenerInternal(
451 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800452 // List is self-synchronized copy-on-write.
453 mDisplayTransactionListeners.add(listener);
454 }
455
Jeff Brown4ccb8232014-01-16 22:16:42 -0800456 private void unregisterDisplayTransactionListenerInternal(
457 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800458 // List is self-synchronized copy-on-write.
459 mDisplayTransactionListeners.remove(listener);
460 }
461
Jeff Brown4ccb8232014-01-16 22:16:42 -0800462 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700463 int displayId, DisplayInfo info) {
464 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700465 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700466 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700467 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200468 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700469 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700470 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700471 }
472 }
473 }
474
Andrii Kuliancd097992017-03-23 18:31:59 -0700475 /**
476 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
477 */
478 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
479 synchronized (mSyncRoot) {
480 final LogicalDisplay display = mLogicalDisplays.get(displayId);
481 if (display != null) {
482 display.getNonOverrideDisplayInfoLocked(outInfo);
483 }
484 }
485 }
486
Santos Cordonee8931e2017-04-05 10:31:15 -0700487 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800488 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700489 synchronized (mSyncRoot) {
490 if (!mPendingTraversal) {
491 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700492 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700493 mPendingTraversal = false;
494
Robert Carrae606b42018-02-15 15:36:23 -0800495 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700496 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800497
498 // List is self-synchronized copy-on-write.
499 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800500 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800501 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700502 }
503
Jeff Brown5d6443b2015-04-10 20:15:01 -0700504 private void requestGlobalDisplayStateInternal(int state, int brightness) {
505 if (state == Display.STATE_UNKNOWN) {
506 state = Display.STATE_ON;
507 }
508 if (state == Display.STATE_OFF) {
509 brightness = PowerManager.BRIGHTNESS_OFF;
510 } else if (brightness < 0) {
511 brightness = PowerManager.BRIGHTNESS_DEFAULT;
512 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
513 brightness = PowerManager.BRIGHTNESS_ON;
514 }
515
Jeff Browne75926d2014-09-18 15:24:49 -0700516 synchronized (mTempDisplayStateWorkQueue) {
517 try {
518 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700519 // Note that we do not need to schedule traversals here although it
520 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700521 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700522 if (mGlobalDisplayState == state
523 && mGlobalDisplayBrightness == brightness) {
524 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700525 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700526
527 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
528 + Display.stateToString(state)
529 + ", brightness=" + brightness + ")");
530 mGlobalDisplayState = state;
531 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700532 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700533 }
534
535 // Setting the display power state can take hundreds of milliseconds
536 // to complete so we defer the most expensive part of the work until
537 // after we have exited the critical section to avoid blocking other
538 // threads for a long time.
539 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
540 mTempDisplayStateWorkQueue.get(i).run();
541 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700542 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700543 } finally {
544 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700545 }
546 }
547 }
548
Jeff Brown4ccb8232014-01-16 22:16:42 -0800549 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700550 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800551 LogicalDisplay display = mLogicalDisplays.get(displayId);
552 if (display != null) {
553 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800554 if (info.hasAccess(callingUid)
555 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800556 return info;
557 }
558 }
559 return null;
560 }
561 }
562
563 private int[] getDisplayIdsInternal(int callingUid) {
564 synchronized (mSyncRoot) {
565 final int count = mLogicalDisplays.size();
566 int[] displayIds = new int[count];
567 int n = 0;
568 for (int i = 0; i < count; i++) {
569 LogicalDisplay display = mLogicalDisplays.valueAt(i);
570 DisplayInfo info = display.getDisplayInfoLocked();
571 if (info.hasAccess(callingUid)) {
572 displayIds[n++] = mLogicalDisplays.keyAt(i);
573 }
574 }
575 if (n != count) {
576 displayIds = Arrays.copyOfRange(displayIds, 0, n);
577 }
578 return displayIds;
579 }
580 }
581
582 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
583 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700584 if (mCallbacks.get(callingPid) != null) {
585 throw new SecurityException("The calling process has already "
586 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700587 }
588
Jeff Brownbd6e1502012-08-28 03:27:37 -0700589 CallbackRecord record = new CallbackRecord(callingPid, callback);
590 try {
591 IBinder binder = callback.asBinder();
592 binder.linkToDeath(record, 0);
593 } catch (RemoteException ex) {
594 // give up
595 throw new RuntimeException(ex);
596 }
597
598 mCallbacks.put(callingPid, record);
599 }
600 }
601
Jeff Brownce468a32013-11-21 16:42:03 -0800602 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700603 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800604 mCallbacks.remove(record.mPid);
605 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700606 }
607 }
608
Jeff Brown4ccb8232014-01-16 22:16:42 -0800609 private void startWifiDisplayScanInternal(int callingPid) {
610 synchronized (mSyncRoot) {
611 CallbackRecord record = mCallbacks.get(callingPid);
612 if (record == null) {
613 throw new IllegalStateException("The calling process has not "
614 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700615 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800616 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700617 }
618 }
619
Jeff Brownce468a32013-11-21 16:42:03 -0800620 private void startWifiDisplayScanLocked(CallbackRecord record) {
621 if (!record.mWifiDisplayScanRequested) {
622 record.mWifiDisplayScanRequested = true;
623 if (mWifiDisplayScanRequestCount++ == 0) {
624 if (mWifiDisplayAdapter != null) {
625 mWifiDisplayAdapter.requestStartScanLocked();
626 }
627 }
628 }
629 }
630
Jeff Brown4ccb8232014-01-16 22:16:42 -0800631 private void stopWifiDisplayScanInternal(int callingPid) {
632 synchronized (mSyncRoot) {
633 CallbackRecord record = mCallbacks.get(callingPid);
634 if (record == null) {
635 throw new IllegalStateException("The calling process has not "
636 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800637 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800638 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800639 }
640 }
641
642 private void stopWifiDisplayScanLocked(CallbackRecord record) {
643 if (record.mWifiDisplayScanRequested) {
644 record.mWifiDisplayScanRequested = false;
645 if (--mWifiDisplayScanRequestCount == 0) {
646 if (mWifiDisplayAdapter != null) {
647 mWifiDisplayAdapter.requestStopScanLocked();
648 }
649 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700650 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800651 + mWifiDisplayScanRequestCount);
652 mWifiDisplayScanRequestCount = 0;
653 }
654 }
655 }
656
Jeff Brown4ccb8232014-01-16 22:16:42 -0800657 private void connectWifiDisplayInternal(String address) {
658 synchronized (mSyncRoot) {
659 if (mWifiDisplayAdapter != null) {
660 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700661 }
Jeff Browne08ae382012-09-07 20:36:36 -0700662 }
663 }
664
Jeff Brown4ccb8232014-01-16 22:16:42 -0800665 private void pauseWifiDisplayInternal() {
666 synchronized (mSyncRoot) {
667 if (mWifiDisplayAdapter != null) {
668 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700669 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700670 }
671 }
672
Jeff Brown4ccb8232014-01-16 22:16:42 -0800673 private void resumeWifiDisplayInternal() {
674 synchronized (mSyncRoot) {
675 if (mWifiDisplayAdapter != null) {
676 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700677 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700678 }
679 }
680
Jeff Brown4ccb8232014-01-16 22:16:42 -0800681 private void disconnectWifiDisplayInternal() {
682 synchronized (mSyncRoot) {
683 if (mWifiDisplayAdapter != null) {
684 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700685 }
Jeff Browne08ae382012-09-07 20:36:36 -0700686 }
687 }
688
Jeff Brown4ccb8232014-01-16 22:16:42 -0800689 private void renameWifiDisplayInternal(String address, String alias) {
690 synchronized (mSyncRoot) {
691 if (mWifiDisplayAdapter != null) {
692 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700693 }
Jeff Brown89d55462012-09-19 11:33:42 -0700694 }
695 }
696
Jeff Brown4ccb8232014-01-16 22:16:42 -0800697 private void forgetWifiDisplayInternal(String address) {
698 synchronized (mSyncRoot) {
699 if (mWifiDisplayAdapter != null) {
700 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700701 }
Jeff Brown89d55462012-09-19 11:33:42 -0700702 }
703 }
704
Jeff Brown4ccb8232014-01-16 22:16:42 -0800705 private WifiDisplayStatus getWifiDisplayStatusInternal() {
706 synchronized (mSyncRoot) {
707 if (mWifiDisplayAdapter != null) {
708 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700709 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800710 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700711 }
712 }
713
Michael Wright1c9977b2016-07-12 13:30:10 -0700714 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100715 synchronized (mSyncRoot) {
716 LogicalDisplay display = mLogicalDisplays.get(displayId);
717 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700718 display.getRequestedColorModeLocked() != colorMode) {
719 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100720 scheduleTraversalLocked(false);
721 }
722 }
723 }
724
Bryan Mawhinney462e29d2018-03-22 15:52:41 +0000725 private void setSaturationLevelInternal(float level) {
726 if (level < 0 || level > 1) {
727 throw new IllegalArgumentException("Saturation level must be between 0 and 1");
728 }
729 float[] matrix = (level == 1.0f ? null : computeSaturationMatrix(level));
730 DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
731 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION, matrix);
732 }
733
734 private static float[] computeSaturationMatrix(float saturation) {
735 float desaturation = 1.0f - saturation;
736 float[] luminance = {0.231f * desaturation, 0.715f * desaturation, 0.072f * desaturation};
737 float[] matrix = {
738 luminance[0] + saturation, luminance[0], luminance[0], 0,
739 luminance[1], luminance[1] + saturation, luminance[1], 0,
740 luminance[2], luminance[2], luminance[2] + saturation, 0,
741 0, 0, 0, 1
742 };
743 return matrix;
744 }
745
Michael Wright75ee9fc2014-09-01 19:55:22 -0700746 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700747 IMediaProjection projection, int callingUid, String packageName, String name, int width,
748 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800749 synchronized (mSyncRoot) {
750 if (mVirtualDisplayAdapter == null) {
751 Slog.w(TAG, "Rejecting request to create private virtual display "
752 + "because the virtual display adapter is not available.");
753 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700754 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800755
756 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700757 callback, projection, callingUid, packageName, name, width, height, densityDpi,
758 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800759 if (device == null) {
760 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700761 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700762
Jeff Brown4ccb8232014-01-16 22:16:42 -0800763 handleDisplayDeviceAddedLocked(device);
764 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
765 if (display != null) {
766 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700767 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800768
769 // Something weird happened and the logical display was not created.
770 Slog.w(TAG, "Rejecting request to create virtual display "
771 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700772 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800773 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700774 }
775 return -1;
776 }
777
Michael Wright01e840f2014-06-26 16:03:25 -0700778 private void resizeVirtualDisplayInternal(IBinder appToken,
779 int width, int height, int densityDpi) {
780 synchronized (mSyncRoot) {
781 if (mVirtualDisplayAdapter == null) {
782 return;
783 }
784
785 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
786 }
787 }
788
Jeff Brown92207df2014-04-16 13:16:07 -0700789 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
790 synchronized (mSyncRoot) {
791 if (mVirtualDisplayAdapter == null) {
792 return;
793 }
794
795 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
796 }
797 }
798
Jeff Brown4ccb8232014-01-16 22:16:42 -0800799 private void releaseVirtualDisplayInternal(IBinder appToken) {
800 synchronized (mSyncRoot) {
801 if (mVirtualDisplayAdapter == null) {
802 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700803 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700804
Jeff Brown4ccb8232014-01-16 22:16:42 -0800805 DisplayDevice device =
806 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
807 if (device != null) {
808 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700809 }
810 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700811 }
812
Santos Cordonc22c5632017-06-21 16:03:49 -0700813 private void registerDefaultDisplayAdapters() {
814 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700815 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700816 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800817 registerDisplayAdapterLocked(new LocalDisplayAdapter(
818 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700819
820 // Standalone VR devices rely on a virtual display as their primary display for
821 // 2D UI. We register virtual display adapter along side the main display adapter
822 // here so that it is ready by the time the system sends the home Intent for
823 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
824 // the virtual display inside VR before any VR-specific apps even run.
825 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
826 mHandler, mDisplayAdapterListener);
827 if (mVirtualDisplayAdapter != null) {
828 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
829 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700830 }
831 }
832
833 private void registerAdditionalDisplayAdapters() {
834 synchronized (mSyncRoot) {
835 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700836 registerOverlayDisplayAdapterLocked();
837 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700838 }
839 }
840 }
841
Jeff Brown89d55462012-09-19 11:33:42 -0700842 private void registerOverlayDisplayAdapterLocked() {
843 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
844 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
845 }
846
847 private void registerWifiDisplayAdapterLocked() {
848 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700849 com.android.internal.R.bool.config_enableWifiDisplay)
850 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700851 mWifiDisplayAdapter = new WifiDisplayAdapter(
852 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
853 mPersistentDataStore);
854 registerDisplayAdapterLocked(mWifiDisplayAdapter);
855 }
856 }
857
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700858 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
859 // In safe mode, we disable non-essential display adapters to give the user
860 // an opportunity to fix broken settings or other problems that might affect
861 // system stability.
862 // In only-core mode, we disable non-essential display adapters to minimize
863 // the number of dependencies that are started while in this mode and to
864 // prevent problems that might occur due to the device being encrypted.
865 return !mSafeMode && !mOnlyCore;
866 }
867
868 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
869 mDisplayAdapters.add(adapter);
870 adapter.registerLocked();
871 }
872
Jeff Brownbd6e1502012-08-28 03:27:37 -0700873 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700874 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700875 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700876 }
877 }
878
Jeff Browna506a6e2013-06-04 00:02:38 -0700879 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700880 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700881 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700882 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700883 return;
884 }
885
Jeff Brown10acf6d2015-04-14 14:20:47 -0700886 Slog.i(TAG, "Display device added: " + info);
887 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700888
889 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700890 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700891 Runnable work = updateDisplayStateLocked(device);
892 if (work != null) {
893 work.run();
894 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700895 scheduleTraversalLocked(false);
896 }
897
Jeff Brownbd6e1502012-08-28 03:27:37 -0700898 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700899 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700900 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700901 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700902 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700903 return;
904 }
905
Jeff Brown10acf6d2015-04-14 14:20:47 -0700906 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
907 if (diff == DisplayDeviceInfo.DIFF_STATE) {
908 Slog.i(TAG, "Display device changed state: \"" + info.name
909 + "\", " + Display.stateToString(info.state));
910 } else if (diff != 0) {
911 Slog.i(TAG, "Display device changed: " + info);
912 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700913 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
914 try {
915 mPersistentDataStore.setColorMode(device, info.colorMode);
916 } finally {
917 mPersistentDataStore.saveIfNeeded();
918 }
919 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700920 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700921
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700922 device.applyPendingDisplayDeviceInfoChangesLocked();
923 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700924 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700925 }
926 }
927 }
928
Jeff Brownbd6e1502012-08-28 03:27:37 -0700929 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700930 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700931 handleDisplayDeviceRemovedLocked(device);
932 }
933 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700934
Jeff Browna506a6e2013-06-04 00:02:38 -0700935 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700936 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700937 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700938 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700939 return;
940 }
941
Jeff Brown10acf6d2015-04-14 14:20:47 -0700942 Slog.i(TAG, "Display device removed: " + info);
943 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700944
Jeff Browna506a6e2013-06-04 00:02:38 -0700945 updateLogicalDisplaysLocked();
946 scheduleTraversalLocked(false);
947 }
948
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200949 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
950 if (displayId == Display.DEFAULT_DISPLAY) {
951 recordTopInsetLocked(display);
952 }
953 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
954 }
955
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700956 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700957 final int count = mDisplayDevices.size();
958 for (int i = 0; i < count; i++) {
959 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700960 Runnable runnable = updateDisplayStateLocked(device);
961 if (runnable != null) {
962 workQueue.add(runnable);
963 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700964 }
965 }
966
Jeff Browne75926d2014-09-18 15:24:49 -0700967 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700968 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700969 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700970 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
971 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700972 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700973 }
Jeff Browne75926d2014-09-18 15:24:49 -0700974 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700975 }
976
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700977 // Adds a new logical display based on the given display device.
978 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700979 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700980 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
981 boolean isDefault = (deviceInfo.flags
982 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
983 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
984 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
985 isDefault = false;
986 }
987
Jeff Brown27f1d672012-10-17 18:32:34 -0700988 if (!isDefault && mSingleDisplayDemoMode) {
989 Slog.i(TAG, "Not creating a logical display for a secondary display "
990 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700991 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700992 }
993
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700994 final int displayId = assignDisplayIdLocked(isDefault);
995 final int layerStack = assignLayerStackLocked(displayId);
996
Jeff Brownd728bf52012-09-08 18:05:28 -0700997 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700998 display.updateLocked(mDisplayDevices);
999 if (!display.isValidLocked()) {
1000 // This should never happen currently.
1001 Slog.w(TAG, "Ignoring display device because the logical display "
1002 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001003 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001004 }
1005
Michael Wrighteedcbf12017-08-16 23:14:54 +01001006 configureColorModeLocked(display, device);
1007 if (isDefault) {
1008 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001009 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001010 }
1011
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001012 mLogicalDisplays.put(displayId, display);
1013
1014 // Wake up waitForDefaultDisplay.
1015 if (isDefault) {
1016 mSyncRoot.notifyAll();
1017 }
1018
1019 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001020 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001021 }
1022
1023 private int assignDisplayIdLocked(boolean isDefault) {
1024 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1025 }
1026
1027 private int assignLayerStackLocked(int displayId) {
1028 // Currently layer stacks and display ids are the same.
1029 // This need not be the case.
1030 return displayId;
1031 }
1032
Michael Wrighteedcbf12017-08-16 23:14:54 +01001033 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1034 if (display.getPrimaryDisplayDeviceLocked() == device) {
1035 int colorMode = mPersistentDataStore.getColorMode(device);
1036 if (colorMode == Display.COLOR_MODE_INVALID) {
1037 if ((device.getDisplayDeviceInfoLocked().flags
1038 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1039 colorMode = mDefaultDisplayDefaultColorMode;
1040 } else {
1041 colorMode = Display.COLOR_MODE_DEFAULT;
1042 }
1043 }
1044 display.setRequestedColorModeLocked(colorMode);
1045 }
1046 }
1047
1048 // If we've never recorded stable device stats for this device before and they aren't
1049 // explicitly configured, go ahead and record the stable device stats now based on the status
1050 // of the default display at first boot.
1051 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1052 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1053 DisplayInfo info = d.getDisplayInfoLocked();
1054 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1055 }
1056 }
1057
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001058 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001059 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001060 return;
1061 }
1062 int topInset = d.getInsets().top;
1063 if (topInset == mDefaultDisplayTopInset) {
1064 return;
1065 }
1066 mDefaultDisplayTopInset = topInset;
1067 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1068 }
1069
Michael Wrighteedcbf12017-08-16 23:14:54 +01001070 private void setStableDisplaySizeLocked(int width, int height) {
1071 mStableDisplaySize = new Point(width, height);
1072 try {
1073 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1074 } finally {
1075 mPersistentDataStore.saveIfNeeded();
1076 }
1077 }
1078
Dan Gittik122df862018-03-28 16:59:22 +01001079 @VisibleForTesting
1080 Curve getMinimumBrightnessCurveInternal() {
1081 return mMinimumBrightnessCurve;
1082 }
1083
Michael Wrighteef0e132017-11-21 17:57:52 +00001084 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001085 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001086 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001087 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001088 final int userSerial = getUserManager().getUserSerialNumber(userId);
1089 synchronized (mSyncRoot) {
1090 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001091 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1092 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001093 } finally {
1094 mPersistentDataStore.saveIfNeeded();
1095 }
1096 if (userId == mCurrentUserId) {
1097 mDisplayPowerController.setBrightnessConfiguration(c);
1098 }
1099 }
1100 }
1101
Dan Gittik122df862018-03-28 16:59:22 +01001102 @VisibleForTesting
1103 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1104 if (config == null) {
1105 return;
1106 }
1107 if (isBrightnessConfigurationTooDark(config)) {
1108 throw new IllegalArgumentException("brightness curve is too dark");
1109 }
1110 }
1111
1112 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1113 Pair<float[], float[]> curve = config.getCurve();
1114 float[] lux = curve.first;
1115 float[] nits = curve.second;
1116 for (int i = 0; i < lux.length; i++) {
1117 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1118 return true;
1119 }
1120 }
1121 return false;
1122 }
1123
Michael Wrighteef0e132017-11-21 17:57:52 +00001124 private void loadBrightnessConfiguration() {
1125 synchronized (mSyncRoot) {
1126 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1127 BrightnessConfiguration config =
1128 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1129 mDisplayPowerController.setBrightnessConfiguration(config);
1130 }
1131 }
1132
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001133 // Updates all existing logical displays given the current set of display devices.
1134 // Removes invalid logical displays.
1135 // Sends notifications if needed.
1136 private boolean updateLogicalDisplaysLocked() {
1137 boolean changed = false;
1138 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1139 final int displayId = mLogicalDisplays.keyAt(i);
1140 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1141
1142 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1143 display.updateLocked(mDisplayDevices);
1144 if (!display.isValidLocked()) {
1145 mLogicalDisplays.removeAt(i);
1146 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1147 changed = true;
1148 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001149 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001150 changed = true;
1151 }
1152 }
1153 return changed;
1154 }
1155
Robert Carrae606b42018-02-15 15:36:23 -08001156 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001157 // Clear all viewports before configuring displays so that we can keep
1158 // track of which ones we have configured.
1159 clearViewportsLocked();
1160
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001161 // Configure each display device.
1162 final int count = mDisplayDevices.size();
1163 for (int i = 0; i < count; i++) {
1164 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001165 configureDisplayLocked(t, device);
1166 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001167 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001168
1169 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001170 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001171 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1172 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001173 }
1174
Michael Wright3f145a22014-07-22 19:46:03 -07001175 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001176 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001177 synchronized (mSyncRoot) {
1178 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001179 if (display == null) {
1180 return;
1181 }
1182 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001183 if (DEBUG) {
1184 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1185 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1186 }
1187
Craig Mautner722285e2012-09-07 13:55:58 -07001188 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001189 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001190 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001191 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1192 // Scan supported modes returned by display.getInfo() to find a mode with the same
1193 // size as the default display mode but with the specified refresh rate instead.
1194 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1195 requestedRefreshRate);
1196 }
1197 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001198 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001199 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001200 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001201 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001202 scheduleTraversalLocked(inTraversal);
1203 }
Craig Mautner722285e2012-09-07 13:55:58 -07001204 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001205 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001206
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001207 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1208 synchronized (mSyncRoot) {
1209 LogicalDisplay display = mLogicalDisplays.get(displayId);
1210 if (display == null) {
1211 return;
1212 }
1213 if (display.getDisplayOffsetXLocked() != x
1214 || display.getDisplayOffsetYLocked() != y) {
1215 if (DEBUG) {
1216 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1217 + x + ", " + y + ")");
1218 }
1219 display.setDisplayOffsetsLocked(x, y);
1220 scheduleTraversalLocked(false);
1221 }
1222 }
1223 }
1224
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001225 // Updates the lists of UIDs that are present on displays.
1226 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1227 synchronized (mSyncRoot) {
1228 mDisplayAccessUIDs.clear();
1229 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1230 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1231 newDisplayAccessUIDs.valueAt(i));
1232 }
1233 }
1234 }
1235
1236 // Checks if provided UID's content is present on the display and UID has access to it.
1237 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1238 synchronized (mSyncRoot) {
1239 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1240 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1241 }
1242 }
1243
Jeff Brownd728bf52012-09-08 18:05:28 -07001244 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001245 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001246 }
1247
Robert Carrae606b42018-02-15 15:36:23 -08001248 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001249 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1250 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001251
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001252 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001253 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001254 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001255 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001256 if (display != null && !display.hasContentLocked()) {
1257 // If the display does not have any content of its own, then
1258 // automatically mirror the default logical display contents.
1259 display = null;
1260 }
1261 if (display == null) {
1262 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1263 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001264 }
1265
1266 // Apply the logical display configuration to the display device.
1267 if (display == null) {
1268 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001269 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001270 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001271 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001272 }
Robert Carrae606b42018-02-15 15:36:23 -08001273 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001274
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001275 // Update the corresponding viewport.
1276 DisplayViewport internalViewport = getInternalViewportLocked();
1277 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1278 populateViewportLocked(internalViewport, display, device);
Jeff Brownd728bf52012-09-08 18:05:28 -07001279 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001280 DisplayViewport externalViewport = getExternalViewportLocked();
1281 if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1282 populateViewportLocked(externalViewport, display, device);
1283 } else if (!externalViewport.valid) {
1284 // TODO (b/116850516) move this logic into InputReader
1285 externalViewport.copyFrom(internalViewport);
1286 externalViewport.type = DisplayViewport.VIEWPORT_EXTERNAL;
Jeff Brownd728bf52012-09-08 18:05:28 -07001287 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001288
1289 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001290 final DisplayViewport viewport = getVirtualViewportLocked(info.uniqueId);
1291 populateViewportLocked(viewport, display, device);
Santos Cordonee8931e2017-04-05 10:31:15 -07001292 }
1293 }
1294
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001295 /** Get the virtual device viewport that has the specified uniqueId.
1296 * If such viewport does not exist, create it. */
1297 private DisplayViewport getVirtualViewportLocked(@NonNull String uniqueId) {
Santos Cordonee8931e2017-04-05 10:31:15 -07001298 DisplayViewport viewport;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001299 final int count = mViewports.size();
Santos Cordonee8931e2017-04-05 10:31:15 -07001300 for (int i = 0; i < count; i++) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001301 viewport = mViewports.get(i);
Santos Cordonee8931e2017-04-05 10:31:15 -07001302 if (uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001303 if (viewport.type != VIEWPORT_VIRTUAL) {
1304 Slog.wtf(TAG, "Found a viewport with uniqueId '" + uniqueId
1305 + "' but it has type " + DisplayViewport.typeToString(viewport.type)
1306 + " (expected VIRTUAL)");
1307 continue;
1308 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001309 return viewport;
1310 }
1311 }
1312
1313 viewport = new DisplayViewport();
1314 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001315 viewport.type = VIEWPORT_VIRTUAL;
1316 mViewports.add(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001317 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001318 }
1319
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001320 private DisplayViewport getInternalViewportLocked() {
1321 return getViewportByTypeLocked(VIEWPORT_INTERNAL);
1322 }
1323
1324 private DisplayViewport getExternalViewportLocked() {
1325 return getViewportByTypeLocked(VIEWPORT_EXTERNAL);
1326 }
1327
1328 /**
1329 * Get internal or external viewport. Create it if does not currently exist.
1330 * @param viewportType - either INTERNAL or EXTERNAL
1331 * @return the viewport with the requested type
1332 */
1333 private DisplayViewport getViewportByTypeLocked(int viewportType) {
1334 // Only allow a single INTERNAL or EXTERNAL viewport, which makes this function possible.
1335 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1336 // Creates the viewport if none exists.
1337 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL) {
1338 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1339 + DisplayViewport.typeToString(viewportType));
1340 return null;
1341 }
1342 DisplayViewport viewport;
1343 final int count = mViewports.size();
1344 for (int i = 0; i < count; i++) {
1345 viewport = mViewports.get(i);
1346 if (viewport.type == viewportType) {
1347 return viewport;
1348 }
1349 }
1350
1351 viewport = new DisplayViewport();
1352 viewport.type = viewportType;
1353 mViewports.add(viewport);
1354 return viewport;
1355 }
1356
1357 private static void populateViewportLocked(DisplayViewport viewport,
Jeff Brownd728bf52012-09-08 18:05:28 -07001358 LogicalDisplay display, DisplayDevice device) {
1359 viewport.valid = true;
1360 viewport.displayId = display.getDisplayIdLocked();
1361 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001362 }
1363
1364 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1365 final int count = mLogicalDisplays.size();
1366 for (int i = 0; i < count; i++) {
1367 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1368 if (display.getPrimaryDisplayDeviceLocked() == device) {
1369 return display;
1370 }
1371 }
1372 return null;
1373 }
1374
Jeff Brownbd6e1502012-08-28 03:27:37 -07001375 private void sendDisplayEventLocked(int displayId, int event) {
1376 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1377 mHandler.sendMessage(msg);
1378 }
1379
Robert Carrae606b42018-02-15 15:36:23 -08001380 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001381 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001382 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001383 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001384 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001385 if (!inTraversal) {
1386 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1387 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001388 }
1389 }
1390
1391 // Runs on Handler thread.
1392 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001393 private void deliverDisplayEvent(int displayId, int event) {
1394 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001395 Slog.d(TAG, "Delivering display event: displayId="
1396 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001397 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001398
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001399 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001400 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001401 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001402 count = mCallbacks.size();
1403 mTempCallbacks.clear();
1404 for (int i = 0; i < count; i++) {
1405 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001406 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001407 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001408
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001409 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001410 for (int i = 0; i < count; i++) {
1411 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1412 }
1413 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001414 }
1415
Michael Wrightc39d47a2014-07-08 18:07:36 -07001416 private IMediaProjectionManager getProjectionService() {
1417 if (mProjectionService == null) {
1418 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1419 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1420 }
1421 return mProjectionService;
1422 }
1423
Michael Wrighteef0e132017-11-21 17:57:52 +00001424 private UserManager getUserManager() {
1425 return mContext.getSystemService(UserManager.class);
1426 }
1427
Jeff Brown4ccb8232014-01-16 22:16:42 -08001428 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001429 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001430
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001431 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001432 pw.println(" mOnlyCode=" + mOnlyCore);
1433 pw.println(" mSafeMode=" + mSafeMode);
1434 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001435 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001436 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001437 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001438 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001439 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001440 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001441 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1442
Jeff Brown9e316a12012-10-08 19:17:06 -07001443
Jeff Brownbd6e1502012-08-28 03:27:37 -07001444 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001445 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001446
1447 pw.println();
1448 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001449 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001450 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001451 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001452 }
Craig Mautner9de49362012-08-02 14:30:30 -07001453
Jeff Brownbd6e1502012-08-28 03:27:37 -07001454 pw.println();
1455 pw.println("Display Devices: size=" + mDisplayDevices.size());
1456 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001457 pw.println(" " + device.getDisplayDeviceInfoLocked());
1458 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001459 }
1460
1461 final int logicalDisplayCount = mLogicalDisplays.size();
1462 pw.println();
1463 pw.println("Logical Displays: size=" + logicalDisplayCount);
1464 for (int i = 0; i < logicalDisplayCount; i++) {
1465 int displayId = mLogicalDisplays.keyAt(i);
1466 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1467 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001468 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001469 }
Jeff Brownce468a32013-11-21 16:42:03 -08001470
1471 final int callbackCount = mCallbacks.size();
1472 pw.println();
1473 pw.println("Callbacks: size=" + callbackCount);
1474 for (int i = 0; i < callbackCount; i++) {
1475 CallbackRecord callback = mCallbacks.valueAt(i);
1476 pw.println(" " + i + ": mPid=" + callback.mPid
1477 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1478 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001479
1480 if (mDisplayPowerController != null) {
1481 mDisplayPowerController.dump(pw);
1482 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001483
1484 pw.println();
1485 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001486 }
1487 }
1488
Dan Gittik122df862018-03-28 16:59:22 +01001489 private static float[] getFloatArray(TypedArray array) {
1490 int length = array.length();
1491 float[] floatArray = new float[length];
1492 for (int i = 0; i < length; i++) {
1493 floatArray[i] = array.getFloat(i, Float.NaN);
1494 }
1495 array.recycle();
1496 return floatArray;
1497 }
1498
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001499 /**
1500 * This is the object that everything in the display manager locks on.
1501 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1502 * clear that the object belongs to the display manager service and that it is
1503 * a unique object with a special purpose.
1504 */
1505 public static final class SyncRoot {
1506 }
1507
Santos Cordonee8931e2017-04-05 10:31:15 -07001508 @VisibleForTesting
1509 static class Injector {
1510 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1511 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1512 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1513 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001514
1515 long getDefaultDisplayDelayTimeout() {
1516 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1517 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001518 }
1519
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001520 @VisibleForTesting
1521 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1522 synchronized (mSyncRoot) {
1523 LogicalDisplay display = mLogicalDisplays.get(displayId);
1524 if (display != null) {
1525 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1526 return displayDevice.getDisplayDeviceInfoLocked();
1527 }
1528 return null;
1529 }
1530 }
1531
Jeff Brownbd6e1502012-08-28 03:27:37 -07001532 private final class DisplayManagerHandler extends Handler {
1533 public DisplayManagerHandler(Looper looper) {
1534 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001535 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001536
Jeff Brownbd6e1502012-08-28 03:27:37 -07001537 @Override
1538 public void handleMessage(Message msg) {
1539 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001540 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1541 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001542 break;
1543
1544 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1545 registerAdditionalDisplayAdapters();
1546 break;
1547
1548 case MSG_DELIVER_DISPLAY_EVENT:
1549 deliverDisplayEvent(msg.arg1, msg.arg2);
1550 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001551
1552 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001553 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001554 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001555
1556 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001557 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001558 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001559 changed = !mTempViewports.equals(mViewports);
1560 if (changed) {
1561 mTempViewports.clear();
1562 for (DisplayViewport d : mViewports) {
1563 mTempViewports.add(d.makeCopy());
1564 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001565 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001566 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001567 if (changed) {
1568 mInputManagerInternal.setDisplayViewports(mTempViewports);
1569 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001570 break;
1571 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001572
Michael Wrighteef0e132017-11-21 17:57:52 +00001573 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1574 loadBrightnessConfiguration();
1575 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001576 }
1577 }
1578 }
1579
1580 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1581 @Override
1582 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1583 switch (event) {
1584 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1585 handleDisplayDeviceAdded(device);
1586 break;
1587
1588 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1589 handleDisplayDeviceChanged(device);
1590 break;
1591
1592 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1593 handleDisplayDeviceRemoved(device);
1594 break;
1595 }
1596 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001597
1598 @Override
1599 public void onTraversalRequested() {
1600 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001601 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001602 }
1603 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001604 }
1605
1606 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001607 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001608 private final IDisplayManagerCallback mCallback;
1609
Jeff Brownce468a32013-11-21 16:42:03 -08001610 public boolean mWifiDisplayScanRequested;
1611
Jeff Brownbd6e1502012-08-28 03:27:37 -07001612 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1613 mPid = pid;
1614 mCallback = callback;
1615 }
1616
1617 @Override
1618 public void binderDied() {
1619 if (DEBUG) {
1620 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1621 }
Jeff Brownce468a32013-11-21 16:42:03 -08001622 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001623 }
1624
1625 public void notifyDisplayEventAsync(int displayId, int event) {
1626 try {
1627 mCallback.onDisplayEvent(displayId, event);
1628 } catch (RemoteException ex) {
1629 Slog.w(TAG, "Failed to notify process "
1630 + mPid + " that displays changed, assuming it died.", ex);
1631 binderDied();
1632 }
1633 }
1634 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001635
Santos Cordonee8931e2017-04-05 10:31:15 -07001636 @VisibleForTesting
1637 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001638 /**
1639 * Returns information about the specified logical display.
1640 *
1641 * @param displayId The logical display id.
1642 * @return The logical display info, or null if the display does not exist. The
1643 * returned object must be treated as immutable.
1644 */
1645 @Override // Binder call
1646 public DisplayInfo getDisplayInfo(int displayId) {
1647 final int callingUid = Binder.getCallingUid();
1648 final long token = Binder.clearCallingIdentity();
1649 try {
1650 return getDisplayInfoInternal(displayId, callingUid);
1651 } finally {
1652 Binder.restoreCallingIdentity(token);
1653 }
1654 }
1655
1656 /**
1657 * Returns the list of all display ids.
1658 */
1659 @Override // Binder call
1660 public int[] getDisplayIds() {
1661 final int callingUid = Binder.getCallingUid();
1662 final long token = Binder.clearCallingIdentity();
1663 try {
1664 return getDisplayIdsInternal(callingUid);
1665 } finally {
1666 Binder.restoreCallingIdentity(token);
1667 }
1668 }
1669
Michael Wrighteedcbf12017-08-16 23:14:54 +01001670 /**
1671 * Returns the stable device display size, in pixels.
1672 */
1673 @Override // Binder call
1674 public Point getStableDisplaySize() {
1675 final long token = Binder.clearCallingIdentity();
1676 try {
1677 return getStableDisplaySizeInternal();
1678 } finally {
1679 Binder.restoreCallingIdentity(token);
1680 }
1681 }
1682
Jeff Brown4ccb8232014-01-16 22:16:42 -08001683 @Override // Binder call
1684 public void registerCallback(IDisplayManagerCallback callback) {
1685 if (callback == null) {
1686 throw new IllegalArgumentException("listener must not be null");
1687 }
1688
1689 final int callingPid = Binder.getCallingPid();
1690 final long token = Binder.clearCallingIdentity();
1691 try {
1692 registerCallbackInternal(callback, callingPid);
1693 } finally {
1694 Binder.restoreCallingIdentity(token);
1695 }
1696 }
1697
1698 @Override // Binder call
1699 public void startWifiDisplayScan() {
1700 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1701 "Permission required to start wifi display scans");
1702
1703 final int callingPid = Binder.getCallingPid();
1704 final long token = Binder.clearCallingIdentity();
1705 try {
1706 startWifiDisplayScanInternal(callingPid);
1707 } finally {
1708 Binder.restoreCallingIdentity(token);
1709 }
1710 }
1711
1712 @Override // Binder call
1713 public void stopWifiDisplayScan() {
1714 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1715 "Permission required to stop wifi display scans");
1716
1717 final int callingPid = Binder.getCallingPid();
1718 final long token = Binder.clearCallingIdentity();
1719 try {
1720 stopWifiDisplayScanInternal(callingPid);
1721 } finally {
1722 Binder.restoreCallingIdentity(token);
1723 }
1724 }
1725
1726 @Override // Binder call
1727 public void connectWifiDisplay(String address) {
1728 if (address == null) {
1729 throw new IllegalArgumentException("address must not be null");
1730 }
1731 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1732 "Permission required to connect to a wifi display");
1733
1734 final long token = Binder.clearCallingIdentity();
1735 try {
1736 connectWifiDisplayInternal(address);
1737 } finally {
1738 Binder.restoreCallingIdentity(token);
1739 }
1740 }
1741
1742 @Override // Binder call
1743 public void disconnectWifiDisplay() {
1744 // This request does not require special permissions.
1745 // Any app can request disconnection from the currently active wifi display.
1746 // This exception should no longer be needed once wifi display control moves
1747 // to the media router service.
1748
1749 final long token = Binder.clearCallingIdentity();
1750 try {
1751 disconnectWifiDisplayInternal();
1752 } finally {
1753 Binder.restoreCallingIdentity(token);
1754 }
1755 }
1756
1757 @Override // Binder call
1758 public void renameWifiDisplay(String address, String alias) {
1759 if (address == null) {
1760 throw new IllegalArgumentException("address must not be null");
1761 }
1762 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1763 "Permission required to rename to a wifi display");
1764
1765 final long token = Binder.clearCallingIdentity();
1766 try {
1767 renameWifiDisplayInternal(address, alias);
1768 } finally {
1769 Binder.restoreCallingIdentity(token);
1770 }
1771 }
1772
1773 @Override // Binder call
1774 public void forgetWifiDisplay(String address) {
1775 if (address == null) {
1776 throw new IllegalArgumentException("address must not be null");
1777 }
1778 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1779 "Permission required to forget to a wifi display");
1780
1781 final long token = Binder.clearCallingIdentity();
1782 try {
1783 forgetWifiDisplayInternal(address);
1784 } finally {
1785 Binder.restoreCallingIdentity(token);
1786 }
1787 }
1788
1789 @Override // Binder call
1790 public void pauseWifiDisplay() {
1791 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1792 "Permission required to pause a wifi display session");
1793
1794 final long token = Binder.clearCallingIdentity();
1795 try {
1796 pauseWifiDisplayInternal();
1797 } finally {
1798 Binder.restoreCallingIdentity(token);
1799 }
1800 }
1801
1802 @Override // Binder call
1803 public void resumeWifiDisplay() {
1804 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1805 "Permission required to resume a wifi display session");
1806
1807 final long token = Binder.clearCallingIdentity();
1808 try {
1809 resumeWifiDisplayInternal();
1810 } finally {
1811 Binder.restoreCallingIdentity(token);
1812 }
1813 }
1814
1815 @Override // Binder call
1816 public WifiDisplayStatus getWifiDisplayStatus() {
1817 // This request does not require special permissions.
1818 // Any app can get information about available wifi displays.
1819
1820 final long token = Binder.clearCallingIdentity();
1821 try {
1822 return getWifiDisplayStatusInternal();
1823 } finally {
1824 Binder.restoreCallingIdentity(token);
1825 }
1826 }
1827
1828 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001829 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001830 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001831 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1832 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001833 final long token = Binder.clearCallingIdentity();
1834 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001835 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001836 } finally {
1837 Binder.restoreCallingIdentity(token);
1838 }
1839 }
1840
1841 @Override // Binder call
Bryan Mawhinney462e29d2018-03-22 15:52:41 +00001842 public void setSaturationLevel(float level) {
1843 mContext.enforceCallingOrSelfPermission(
1844 Manifest.permission.CONTROL_DISPLAY_SATURATION,
1845 "Permission required to set display saturation level");
1846 final long token = Binder.clearCallingIdentity();
1847 try {
1848 setSaturationLevelInternal(level);
1849 } finally {
1850 Binder.restoreCallingIdentity(token);
1851 }
1852 }
1853
1854 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001855 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001856 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001857 int width, int height, int densityDpi, Surface surface, int flags,
1858 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001859 final int callingUid = Binder.getCallingUid();
1860 if (!validatePackageName(callingUid, packageName)) {
1861 throw new SecurityException("packageName must match the calling uid");
1862 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001863 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001864 throw new IllegalArgumentException("appToken must not be null");
1865 }
1866 if (TextUtils.isEmpty(name)) {
1867 throw new IllegalArgumentException("name must be non-null and non-empty");
1868 }
1869 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1870 throw new IllegalArgumentException("width, height, and densityDpi must be "
1871 + "greater than 0");
1872 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001873 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001874 throw new IllegalArgumentException("Surface can't be single-buffered");
1875 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001876
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001877 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1878 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1879
1880 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001881 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001882 throw new IllegalArgumentException(
1883 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1884 }
Michael Wright6720be42014-07-29 19:14:16 -07001885 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001886 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1887 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001888 }
1889
Michael Wrightc39d47a2014-07-08 18:07:36 -07001890 if (projection != null) {
1891 try {
1892 if (!getProjectionService().isValidMediaProjection(projection)) {
1893 throw new SecurityException("Invalid media projection");
1894 }
Michael Wright6720be42014-07-29 19:14:16 -07001895 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001896 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001897 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001898 }
1899 }
1900
Michael Wright6720be42014-07-29 19:14:16 -07001901 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001902 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001903 if (!canProjectVideo(projection)) {
1904 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1905 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1906 + "MediaProjection token in order to create a screen sharing virtual "
1907 + "display.");
1908 }
1909 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001910 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001911 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001912 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001913 + "or an appropriate MediaProjection token to create a "
1914 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001915 }
1916 }
1917
1918 final long token = Binder.clearCallingIdentity();
1919 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001920 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1921 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001922 } finally {
1923 Binder.restoreCallingIdentity(token);
1924 }
1925 }
1926
1927 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001928 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001929 int width, int height, int densityDpi) {
1930 final long token = Binder.clearCallingIdentity();
1931 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001932 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001933 } finally {
1934 Binder.restoreCallingIdentity(token);
1935 }
1936 }
1937
1938 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001939 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001940 if (surface != null && surface.isSingleBuffered()) {
1941 throw new IllegalArgumentException("Surface can't be single-buffered");
1942 }
Jeff Brown92207df2014-04-16 13:16:07 -07001943 final long token = Binder.clearCallingIdentity();
1944 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001945 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001946 } finally {
1947 Binder.restoreCallingIdentity(token);
1948 }
1949 }
1950
1951 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001952 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001953 final long token = Binder.clearCallingIdentity();
1954 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001955 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001956 } finally {
1957 Binder.restoreCallingIdentity(token);
1958 }
1959 }
1960
1961 @Override // Binder call
1962 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001963 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001964
1965 final long token = Binder.clearCallingIdentity();
1966 try {
1967 dumpInternal(pw);
1968 } finally {
1969 Binder.restoreCallingIdentity(token);
1970 }
1971 }
1972
Kenny Guy22bd0442017-10-26 00:15:54 +01001973 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001974 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001975 mContext.enforceCallingOrSelfPermission(
1976 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1977 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001978
1979 final int callingUid = Binder.getCallingUid();
1980 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00001981 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00001982 callingUid, callingPackage);
1983 final boolean hasUsageStats;
1984 if (mode == AppOpsManager.MODE_DEFAULT) {
1985 // The default behavior here is to check if PackageManager has given the app
1986 // permission.
1987 hasUsageStats = mContext.checkCallingPermission(
1988 Manifest.permission.PACKAGE_USAGE_STATS)
1989 == PackageManager.PERMISSION_GRANTED;
1990 } else {
1991 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1992 }
1993
1994 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001995 final long token = Binder.clearCallingIdentity();
1996 try {
Michael Wright144aac92017-12-21 18:37:41 +00001997 synchronized (mSyncRoot) {
1998 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
1999 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002000 } finally {
2001 Binder.restoreCallingIdentity(token);
2002 }
2003 }
2004
Michael Wrighteef0e132017-11-21 17:57:52 +00002005 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002006 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2007 mContext.enforceCallingOrSelfPermission(
2008 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2009 "Permission required to to access ambient light stats.");
2010 final int callingUid = Binder.getCallingUid();
2011 final int userId = UserHandle.getUserId(callingUid);
2012 final long token = Binder.clearCallingIdentity();
2013 try {
2014 synchronized (mSyncRoot) {
2015 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2016 }
2017 } finally {
2018 Binder.restoreCallingIdentity(token);
2019 }
2020 }
2021
2022 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002023 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002024 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002025 mContext.enforceCallingOrSelfPermission(
2026 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2027 "Permission required to change the display's brightness configuration");
2028 if (userId != UserHandle.getCallingUserId()) {
2029 mContext.enforceCallingOrSelfPermission(
2030 Manifest.permission.INTERACT_ACROSS_USERS,
2031 "Permission required to change the display brightness"
2032 + " configuration of another user");
2033 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002034 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2035 packageName = null;
2036 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002037 final long token = Binder.clearCallingIdentity();
2038 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002039 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002040 } finally {
2041 Binder.restoreCallingIdentity(token);
2042 }
2043 }
2044
Michael Wrightd8460232018-01-16 18:04:59 +00002045 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002046 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2047 mContext.enforceCallingOrSelfPermission(
2048 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2049 "Permission required to read the display's brightness configuration");
2050 if (userId != UserHandle.getCallingUserId()) {
2051 mContext.enforceCallingOrSelfPermission(
2052 Manifest.permission.INTERACT_ACROSS_USERS,
2053 "Permission required to read the display brightness"
2054 + " configuration of another user");
2055 }
2056 final long token = Binder.clearCallingIdentity();
2057 try {
2058 final int userSerial = getUserManager().getUserSerialNumber(userId);
2059 synchronized (mSyncRoot) {
2060 BrightnessConfiguration config =
2061 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2062 if (config == null) {
2063 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2064 }
2065 return config;
2066 }
2067 } finally {
2068 Binder.restoreCallingIdentity(token);
2069 }
2070 }
2071
2072 @Override // Binder call
2073 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2074 mContext.enforceCallingOrSelfPermission(
2075 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2076 "Permission required to read the display's default brightness configuration");
2077 final long token = Binder.clearCallingIdentity();
2078 try {
2079 synchronized (mSyncRoot) {
2080 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2081 }
2082 } finally {
2083 Binder.restoreCallingIdentity(token);
2084 }
2085 }
2086
2087 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002088 public void setTemporaryBrightness(int brightness) {
2089 mContext.enforceCallingOrSelfPermission(
2090 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2091 "Permission required to set the display's brightness");
2092 final long token = Binder.clearCallingIdentity();
2093 try {
2094 synchronized (mSyncRoot) {
2095 mDisplayPowerController.setTemporaryBrightness(brightness);
2096 }
2097 } finally {
2098 Binder.restoreCallingIdentity(token);
2099 }
2100 }
2101
2102 @Override // Binder call
2103 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2104 mContext.enforceCallingOrSelfPermission(
2105 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2106 "Permission required to set the display's auto brightness adjustment");
2107 final long token = Binder.clearCallingIdentity();
2108 try {
2109 synchronized (mSyncRoot) {
2110 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2111 }
2112 } finally {
2113 Binder.restoreCallingIdentity(token);
2114 }
2115 }
2116
Dan Gittik7a32fba2018-03-28 12:19:38 +01002117 @Override // Binder call
2118 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2119 FileDescriptor err, String[] args, ShellCallback callback,
2120 ResultReceiver resultReceiver) {
2121 final long token = Binder.clearCallingIdentity();
2122 try {
2123 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2124 command.exec(this, in, out, err, args, callback, resultReceiver);
2125 } finally {
2126 Binder.restoreCallingIdentity(token);
2127 }
2128 }
2129
Dan Gittik122df862018-03-28 16:59:22 +01002130 @Override // Binder call
2131 public Curve getMinimumBrightnessCurve() {
2132 final long token = Binder.clearCallingIdentity();
2133 try {
2134 return getMinimumBrightnessCurveInternal();
2135 } finally {
2136 Binder.restoreCallingIdentity(token);
2137 }
2138 }
2139
Dan Gittik7a32fba2018-03-28 12:19:38 +01002140 void setBrightness(int brightness) {
2141 Settings.System.putIntForUser(mContext.getContentResolver(),
2142 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2143 }
2144
2145 void resetBrightnessConfiguration() {
2146 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2147 mContext.getPackageName());
2148 }
2149
Jeff Brown4ccb8232014-01-16 22:16:42 -08002150 private boolean validatePackageName(int uid, String packageName) {
2151 if (packageName != null) {
2152 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2153 if (packageNames != null) {
2154 for (String n : packageNames) {
2155 if (n.equals(packageName)) {
2156 return true;
2157 }
2158 }
2159 }
2160 }
2161 return false;
2162 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002163
2164 private boolean canProjectVideo(IMediaProjection projection) {
2165 if (projection != null) {
2166 try {
2167 if (projection.canProjectVideo()) {
2168 return true;
2169 }
2170 } catch (RemoteException e) {
2171 Slog.e(TAG, "Unable to query projection service for permissions", e);
2172 }
2173 }
2174 if (mContext.checkCallingPermission(
2175 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002176 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002177 return true;
2178 }
2179 return canProjectSecureVideo(projection);
2180 }
2181
2182 private boolean canProjectSecureVideo(IMediaProjection projection) {
2183 if (projection != null) {
2184 try {
2185 if (projection.canProjectSecureVideo()){
2186 return true;
2187 }
2188 } catch (RemoteException e) {
2189 Slog.e(TAG, "Unable to query projection service for permissions", e);
2190 }
2191 }
2192 return mContext.checkCallingPermission(
2193 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002194 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002195 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002196 }
2197
2198 private final class LocalService extends DisplayManagerInternal {
2199 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002200 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002201 SensorManager sensorManager) {
2202 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002203 DisplayBlanker blanker = new DisplayBlanker() {
2204 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002205 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002206 // The order of operations is important for legacy reasons.
2207 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002208 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002209 }
2210
2211 callbacks.onDisplayStateChange(state);
2212
2213 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002214 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002215 }
2216 }
2217 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002218 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002219 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002220 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002221
2222 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002223 }
2224
2225 @Override
2226 public boolean requestPowerState(DisplayPowerRequest request,
2227 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002228 synchronized (mSyncRoot) {
2229 return mDisplayPowerController.requestPowerState(request,
2230 waitForNegativeProximity);
2231 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002232 }
2233
2234 @Override
2235 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002236 synchronized (mSyncRoot) {
2237 return mDisplayPowerController.isProximitySensorAvailable();
2238 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002239 }
2240
2241 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002242 public boolean screenshot(int displayId, Surface outSurface) {
2243 synchronized (mSyncRoot) {
2244 final LogicalDisplay display = mLogicalDisplays.get(displayId);
2245 if (display != null) {
2246 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
2247 if (device != null) {
2248 final IBinder token = device.getDisplayTokenLocked();
2249 if (token != null) {
2250 SurfaceControl.screenshot(token, outSurface);
2251 return true;
2252 }
2253 }
2254 }
2255 }
2256 return false;
2257 }
2258
2259 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002260 public DisplayInfo getDisplayInfo(int displayId) {
2261 return getDisplayInfoInternal(displayId, Process.myUid());
2262 }
2263
2264 @Override
2265 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2266 if (listener == null) {
2267 throw new IllegalArgumentException("listener must not be null");
2268 }
2269
2270 registerDisplayTransactionListenerInternal(listener);
2271 }
2272
2273 @Override
2274 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2275 if (listener == null) {
2276 throw new IllegalArgumentException("listener must not be null");
2277 }
2278
2279 unregisterDisplayTransactionListenerInternal(listener);
2280 }
2281
2282 @Override
2283 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2284 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2285 }
2286
2287 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002288 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2289 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2290 }
2291
2292 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002293 public void performTraversal(SurfaceControl.Transaction t) {
2294 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002295 }
2296
2297 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002298 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002299 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2300 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2301 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002302 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002303
2304 @Override
2305 public void setDisplayOffsets(int displayId, int x, int y) {
2306 setDisplayOffsetsInternal(displayId, x, y);
2307 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002308
2309 @Override
2310 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2311 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2312 }
2313
2314 @Override
2315 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2316 return isUidPresentOnDisplayInternal(uid, displayId);
2317 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002318
2319 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002320 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002321 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002322 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002323 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002324 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002325
2326 @Override
2327 public void onOverlayChanged() {
2328 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002329 for (int i = 0; i < mDisplayDevices.size(); i++) {
2330 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002331 }
2332 }
2333 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002334 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002335}