blob: cc5a8271eb5fa9c52b72e3fa79137778edaee75f [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;
20import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
21import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
22import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
23import static android.hardware.display.DisplayManager
Andrii Kulian7211d2e2017-01-27 15:58:05 -080024 .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080025
Santos Cordonee8931e2017-04-05 10:31:15 -070026import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060027import com.android.internal.util.DumpUtils;
Jeff Brownbd6e1502012-08-28 03:27:37 -070028import com.android.internal.util.IndentingPrintWriter;
29
Jeff Brownfa25bf52012-07-23 19:26:30 -070030import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070031import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000032import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000033import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000034import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070035import android.content.Context;
36import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010037import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010038import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010039import android.content.res.TypedArray;
Michael Wrighteedcbf12017-08-16 23:14:54 +010040import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070041import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000042import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010043import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000044import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010045import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070046import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080047import android.hardware.display.DisplayManagerInternal;
48import android.hardware.display.DisplayViewport;
49import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070050import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070052import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070053import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080054import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070055import android.media.projection.IMediaProjection;
56import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070057import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070058import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070059import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080060import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070061import android.os.Looper;
62import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070063import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080064import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070065import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010066import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070067import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010068import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070069import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070070import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070071import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010072import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000073import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010074import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070075import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080076import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010077import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070078import android.util.Slog;
79import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010080import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070081import android.view.Display;
82import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070083import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080084import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070085
Michael Wrighteef0e132017-11-21 17:57:52 +000086import com.android.internal.util.Preconditions;
Jorim Jaggied7993b2017-03-28 18:50:01 +010087import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080088import com.android.server.DisplayThread;
89import com.android.server.LocalServices;
90import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070091import com.android.server.UiThread;
Adrian Roose99bc052017-11-20 17:55:31 +010092import com.android.server.wm.WindowManagerInternal;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020093import com.android.server.wm.SurfaceAnimationThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070094
95import java.io.FileDescriptor;
96import java.io.PrintWriter;
97import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070098import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070099import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800100import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700101
102/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700103 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700104 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700105 * The {@link DisplayManagerService} manages the global lifecycle of displays,
106 * decides how to configure logical displays based on the physical display devices currently
107 * attached, sends notifications to the system and to applications when the state
108 * changes, and so on.
109 * </p><p>
110 * The display manager service relies on a collection of {@link DisplayAdapter} components,
111 * for discovering and configuring physical display devices attached to the system.
112 * There are separate display adapters for each manner that devices are attached:
113 * one display adapter for built-in local displays, one for simulated non-functional
114 * displays when the system is headless, one for simulated overlay displays used for
115 * development, one for wifi displays, etc.
116 * </p><p>
117 * Display adapters are only weakly coupled to the display manager service.
118 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700119 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700120 * by the display manager service. This separation of concerns is important for
121 * two main reasons. First, it neatly encapsulates the responsibilities of these
122 * two classes: display adapters handle individual display devices whereas
123 * the display manager service handles the global state. Second, it eliminates
124 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700125 * </p>
126 *
127 * <h3>Synchronization</h3>
128 * <p>
129 * Because the display manager may be accessed by multiple threads, the synchronization
130 * story gets a little complicated. In particular, the window manager may call into
131 * the display manager while holding a surface transaction with the expectation that
132 * it can apply changes immediately. Unfortunately, that means we can't just do
133 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700134 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700135 * To make this work, all of the objects that belong to the display manager must
136 * use the same lock. We call this lock the synchronization root and it has a unique
137 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
138 * named with the "Locked" suffix.
139 * </p><p>
140 * Where things get tricky is that the display manager is not allowed to make
141 * any potentially reentrant calls, especially into the window manager. We generally
142 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700143 * </p>
144 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800145public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700146 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700147 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700148
Jeff Brownbbd28a22012-09-20 16:47:15 -0700149 // When this system property is set to 0, WFD is forcibly disabled on boot.
150 // When this system property is set to 1, WFD is forcibly enabled on boot.
151 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
152 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
153
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200154 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
155
Jeff Brownbd6e1502012-08-28 03:27:37 -0700156 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
157
Santos Cordonc22c5632017-06-21 16:03:49 -0700158 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700159 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
160 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700161 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700162 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100163 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000164 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700165
Jeff Brownb880d882014-02-10 19:47:07 -0800166 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700167 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700168 private final Handler mUiHandler;
169 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800170 private WindowManagerInternal mWindowManagerInternal;
171 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700172 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700173
174 // The synchronization root for the display manager.
175 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800176 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
177 // into WindowManagerService methods that require mWindowMap while holding this unless you are
178 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700179 private final SyncRoot mSyncRoot = new SyncRoot();
180
181 // True if in safe mode.
182 // This option may disable certain display adapters.
183 public boolean mSafeMode;
184
185 // True if we are in a special boot mode where only core applications and
186 // services should be started. This option may disable certain display adapters.
187 public boolean mOnlyCore;
188
Jeff Brown27f1d672012-10-17 18:32:34 -0700189 // True if the display manager service should pretend there is only one display
190 // and only tell applications about the existence of the default logical display.
191 // The display manager can still mirror content to secondary displays but applications
192 // cannot present unique content on those displays.
193 // Used for demonstration purposes only.
194 private final boolean mSingleDisplayDemoMode;
195
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700196 // All callback records indexed by calling process id.
197 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700198 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700199
Jeff Brownbd6e1502012-08-28 03:27:37 -0700200 // List of all currently registered display adapters.
201 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
202
203 // List of all currently connected display devices.
204 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
205
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700206 // List of all logical displays indexed by logical display id.
207 private final SparseArray<LogicalDisplay> mLogicalDisplays =
208 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700209 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
210
Jeff Brown7f3994e2012-12-04 14:04:28 -0800211 // List of all display transaction listeners.
212 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
213 new CopyOnWriteArrayList<DisplayTransactionListener>();
214
Jeff Brownad9ef192014-04-08 17:26:30 -0700215 // Display power controller.
216 private DisplayPowerController mDisplayPowerController;
217
Jeff Brown037c33e2014-04-09 00:31:55 -0700218 // The overall display state, independent of changes that might influence one
219 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700220 private int mGlobalDisplayState = Display.STATE_ON;
221
222 // The overall display brightness.
223 // For now, this only applies to the built-in display but we may split it up eventually.
224 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700225
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700226 // Set to true when there are pending display changes that have yet to be applied
227 // to the surface flinger state.
228 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700229
Jeff Browne08ae382012-09-07 20:36:36 -0700230 // The Wifi display adapter, or null if not registered.
231 private WifiDisplayAdapter mWifiDisplayAdapter;
232
Jeff Brownce468a32013-11-21 16:42:03 -0800233 // The number of active wifi display scan requests.
234 private int mWifiDisplayScanRequestCount;
235
Jeff Browna506a6e2013-06-04 00:02:38 -0700236 // The virtual display adapter, or null if not registered.
237 private VirtualDisplayAdapter mVirtualDisplayAdapter;
238
Michael Wrighteef0e132017-11-21 17:57:52 +0000239 // The User ID of the current user
240 private @UserIdInt int mCurrentUserId;
241
Michael Wrighteedcbf12017-08-16 23:14:54 +0100242 // The stable device screen height and width. These are not tied to a specific display, even
243 // the default display, because they need to be stable over the course of the device's entire
244 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
245 // device).
246 private Point mStableDisplaySize = new Point();
247
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200248 // Whether the system has finished booting or not.
249 private boolean mSystemReady;
250
251 // The top inset of the default display.
252 // This gets persisted so that the boot animation knows how to transition from the display's
253 // full size to the size configured by the user. Right now we only persist and animate the top
254 // inset, but theoretically we could do it for all of them.
255 private int mDefaultDisplayTopInset;
256
Jeff Brownd728bf52012-09-08 18:05:28 -0700257 // Viewports of the default display and the display that should receive touch
258 // input from an external source. Used by the input system.
259 private final DisplayViewport mDefaultViewport = new DisplayViewport();
260 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700261 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700262
Jeff Brown89d55462012-09-19 11:33:42 -0700263 // Persistent data store for all internal settings maintained by the display manager service.
264 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
265
Jeff Brownbd6e1502012-08-28 03:27:37 -0700266 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700267 // May be used outside of the lock but only on the handler thread.
268 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700269
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700270 // Temporary display info, used for comparing display configurations.
271 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
272
Jeff Brownd728bf52012-09-08 18:05:28 -0700273 // Temporary viewports, used when sending new viewport information to the
274 // input system. May be used outside of the lock but only on the handler thread.
275 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
276 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700277 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700278
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700279 // The default color mode for default displays. Overrides the usual
280 // Display.Display.COLOR_MODE_DEFAULT for displays with the
281 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
282 private final int mDefaultDisplayDefaultColorMode;
283
Jeff Browne75926d2014-09-18 15:24:49 -0700284 // Temporary list of deferred work to perform when setting the display state.
285 // Only used by requestDisplayState. The field is self-synchronized and only
286 // intended for use inside of the requestGlobalDisplayStateInternal function.
287 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
288
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800289 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
290 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
291
Santos Cordonee8931e2017-04-05 10:31:15 -0700292 private final Injector mInjector;
293
Dan Gittik122df862018-03-28 16:59:22 +0100294 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
295 // is rejected by the system.
296 private final Curve mMinimumBrightnessCurve;
297 private final Spline mMinimumBrightnessSpline;
298
Jeff Brownb880d882014-02-10 19:47:07 -0800299 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700300 this(context, new Injector());
301 }
302
303 @VisibleForTesting
304 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800305 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700306 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800307 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800308 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700309 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700310 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700311 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100312 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700313 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000314 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200315 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100316 float[] lux = getFloatArray(resources.obtainTypedArray(
317 com.android.internal.R.array.config_minimumBrightnessCurveLux));
318 float[] nits = getFloatArray(resources.obtainTypedArray(
319 com.android.internal.R.array.config_minimumBrightnessCurveNits));
320 mMinimumBrightnessCurve = new Curve(lux, nits);
321 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700322
323 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
324 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000325 mCurrentUserId = UserHandle.USER_SYSTEM;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200326
327 mSystemReady = false;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700328 }
329
330 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100331 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000332 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
333 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100334 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
335 Process.THREAD_GROUP_TOP_APP);
336 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
337 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200338 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
339 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700340 }
341
Jeff Brown4ccb8232014-01-16 22:16:42 -0800342 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800343 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700344 // We need to pre-load the persistent data store so it's ready before the default display
345 // adapter is up so that we have it's configuration. We could load it lazily, but since
346 // 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 -0700347 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100348 synchronized(mSyncRoot) {
349 mPersistentDataStore.loadIfNeeded();
350 loadStableDisplayValuesLocked();
351 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700352 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800353
354 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
355 true /*allowIsolated*/);
356 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700357 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800358 }
359
360 @Override
361 public void onBootPhase(int phase) {
362 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
363 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700364 long timeout = SystemClock.uptimeMillis()
365 + mInjector.getDefaultDisplayDelayTimeout();
366 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
367 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800368 long delay = timeout - SystemClock.uptimeMillis();
369 if (delay <= 0) {
370 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700371 + "to be initialized. DefaultDisplay="
372 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
373 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800374 }
375 if (DEBUG) {
376 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
377 }
378 try {
379 mSyncRoot.wait(delay);
380 } catch (InterruptedException ex) {
381 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700382 }
383 }
384 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700385 }
386
Michael Wrighteef0e132017-11-21 17:57:52 +0000387 @Override
388 public void onSwitchUser(@UserIdInt int newUserId) {
389 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
390 synchronized (mSyncRoot) {
391 if (mCurrentUserId != newUserId) {
392 mCurrentUserId = newUserId;
393 BrightnessConfiguration config =
394 mPersistentDataStore.getBrightnessConfiguration(userSerial);
395 mDisplayPowerController.setBrightnessConfiguration(config);
396 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000397 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000398 }
399 }
400
Jeff Brown4ccb8232014-01-16 22:16:42 -0800401 // TODO: Use dependencies or a boot phase
402 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700403 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800404 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
405 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700406 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700407 }
408 }
409
410 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700411 * Called when the system is ready to go.
412 */
413 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700414 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 mSafeMode = safeMode;
416 mOnlyCore = onlyCore;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200417 mSystemReady = true;
418 // Just in case the top inset changed before the system was ready. At this point, any
419 // relevant configuration should be in place.
420 recordTopInsetLocked(mLogicalDisplays.get(Display.DEFAULT_DISPLAY));
Jeff Brownbd6e1502012-08-28 03:27:37 -0700421 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700422
Jeff Brownbd6e1502012-08-28 03:27:37 -0700423 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100424 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700425 }
426
Santos Cordonee8931e2017-04-05 10:31:15 -0700427 @VisibleForTesting
428 Handler getDisplayHandler() {
429 return mHandler;
430 }
431
Michael Wrighteedcbf12017-08-16 23:14:54 +0100432 private void loadStableDisplayValuesLocked() {
433 final Point size = mPersistentDataStore.getStableDisplaySize();
434 if (size.x > 0 && size.y > 0) {
435 // Just set these values directly so we don't write the display persistent data again
436 // unnecessarily
437 mStableDisplaySize.set(size.x, size.y);
438 } else {
439 final Resources res = mContext.getResources();
440 final int width = res.getInteger(
441 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
442 final int height = res.getInteger(
443 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
444 if (width > 0 && height > 0) {
445 setStableDisplaySizeLocked(width, height);
446 }
447 }
448 }
449
450 private Point getStableDisplaySizeInternal() {
451 Point r = new Point();
452 synchronized (mSyncRoot) {
453 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
454 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
455 }
456 }
457 return r;
458 }
459
Jeff Brown4ccb8232014-01-16 22:16:42 -0800460 private void registerDisplayTransactionListenerInternal(
461 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800462 // List is self-synchronized copy-on-write.
463 mDisplayTransactionListeners.add(listener);
464 }
465
Jeff Brown4ccb8232014-01-16 22:16:42 -0800466 private void unregisterDisplayTransactionListenerInternal(
467 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800468 // List is self-synchronized copy-on-write.
469 mDisplayTransactionListeners.remove(listener);
470 }
471
Jeff Brown4ccb8232014-01-16 22:16:42 -0800472 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700473 int displayId, DisplayInfo info) {
474 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700475 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700476 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700477 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200478 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700479 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700480 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700481 }
482 }
483 }
484
Andrii Kuliancd097992017-03-23 18:31:59 -0700485 /**
486 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
487 */
488 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
489 synchronized (mSyncRoot) {
490 final LogicalDisplay display = mLogicalDisplays.get(displayId);
491 if (display != null) {
492 display.getNonOverrideDisplayInfoLocked(outInfo);
493 }
494 }
495 }
496
Santos Cordonee8931e2017-04-05 10:31:15 -0700497 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800498 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700499 synchronized (mSyncRoot) {
500 if (!mPendingTraversal) {
501 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700502 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700503 mPendingTraversal = false;
504
Robert Carrae606b42018-02-15 15:36:23 -0800505 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700506 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800507
508 // List is self-synchronized copy-on-write.
509 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
510 listener.onDisplayTransaction();
511 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700512 }
513
Jeff Brown5d6443b2015-04-10 20:15:01 -0700514 private void requestGlobalDisplayStateInternal(int state, int brightness) {
515 if (state == Display.STATE_UNKNOWN) {
516 state = Display.STATE_ON;
517 }
518 if (state == Display.STATE_OFF) {
519 brightness = PowerManager.BRIGHTNESS_OFF;
520 } else if (brightness < 0) {
521 brightness = PowerManager.BRIGHTNESS_DEFAULT;
522 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
523 brightness = PowerManager.BRIGHTNESS_ON;
524 }
525
Jeff Browne75926d2014-09-18 15:24:49 -0700526 synchronized (mTempDisplayStateWorkQueue) {
527 try {
528 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700529 // Note that we do not need to schedule traversals here although it
530 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700531 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700532 if (mGlobalDisplayState == state
533 && mGlobalDisplayBrightness == brightness) {
534 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700535 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700536
537 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
538 + Display.stateToString(state)
539 + ", brightness=" + brightness + ")");
540 mGlobalDisplayState = state;
541 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700542 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700543 }
544
545 // Setting the display power state can take hundreds of milliseconds
546 // to complete so we defer the most expensive part of the work until
547 // after we have exited the critical section to avoid blocking other
548 // threads for a long time.
549 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
550 mTempDisplayStateWorkQueue.get(i).run();
551 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700552 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700553 } finally {
554 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700555 }
556 }
557 }
558
Jeff Brown4ccb8232014-01-16 22:16:42 -0800559 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700560 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800561 LogicalDisplay display = mLogicalDisplays.get(displayId);
562 if (display != null) {
563 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800564 if (info.hasAccess(callingUid)
565 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800566 return info;
567 }
568 }
569 return null;
570 }
571 }
572
573 private int[] getDisplayIdsInternal(int callingUid) {
574 synchronized (mSyncRoot) {
575 final int count = mLogicalDisplays.size();
576 int[] displayIds = new int[count];
577 int n = 0;
578 for (int i = 0; i < count; i++) {
579 LogicalDisplay display = mLogicalDisplays.valueAt(i);
580 DisplayInfo info = display.getDisplayInfoLocked();
581 if (info.hasAccess(callingUid)) {
582 displayIds[n++] = mLogicalDisplays.keyAt(i);
583 }
584 }
585 if (n != count) {
586 displayIds = Arrays.copyOfRange(displayIds, 0, n);
587 }
588 return displayIds;
589 }
590 }
591
592 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
593 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700594 if (mCallbacks.get(callingPid) != null) {
595 throw new SecurityException("The calling process has already "
596 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700597 }
598
Jeff Brownbd6e1502012-08-28 03:27:37 -0700599 CallbackRecord record = new CallbackRecord(callingPid, callback);
600 try {
601 IBinder binder = callback.asBinder();
602 binder.linkToDeath(record, 0);
603 } catch (RemoteException ex) {
604 // give up
605 throw new RuntimeException(ex);
606 }
607
608 mCallbacks.put(callingPid, record);
609 }
610 }
611
Jeff Brownce468a32013-11-21 16:42:03 -0800612 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700613 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800614 mCallbacks.remove(record.mPid);
615 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700616 }
617 }
618
Jeff Brown4ccb8232014-01-16 22:16:42 -0800619 private void startWifiDisplayScanInternal(int callingPid) {
620 synchronized (mSyncRoot) {
621 CallbackRecord record = mCallbacks.get(callingPid);
622 if (record == null) {
623 throw new IllegalStateException("The calling process has not "
624 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700625 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800626 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700627 }
628 }
629
Jeff Brownce468a32013-11-21 16:42:03 -0800630 private void startWifiDisplayScanLocked(CallbackRecord record) {
631 if (!record.mWifiDisplayScanRequested) {
632 record.mWifiDisplayScanRequested = true;
633 if (mWifiDisplayScanRequestCount++ == 0) {
634 if (mWifiDisplayAdapter != null) {
635 mWifiDisplayAdapter.requestStartScanLocked();
636 }
637 }
638 }
639 }
640
Jeff Brown4ccb8232014-01-16 22:16:42 -0800641 private void stopWifiDisplayScanInternal(int callingPid) {
642 synchronized (mSyncRoot) {
643 CallbackRecord record = mCallbacks.get(callingPid);
644 if (record == null) {
645 throw new IllegalStateException("The calling process has not "
646 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800647 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800648 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800649 }
650 }
651
652 private void stopWifiDisplayScanLocked(CallbackRecord record) {
653 if (record.mWifiDisplayScanRequested) {
654 record.mWifiDisplayScanRequested = false;
655 if (--mWifiDisplayScanRequestCount == 0) {
656 if (mWifiDisplayAdapter != null) {
657 mWifiDisplayAdapter.requestStopScanLocked();
658 }
659 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700660 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800661 + mWifiDisplayScanRequestCount);
662 mWifiDisplayScanRequestCount = 0;
663 }
664 }
665 }
666
Jeff Brown4ccb8232014-01-16 22:16:42 -0800667 private void connectWifiDisplayInternal(String address) {
668 synchronized (mSyncRoot) {
669 if (mWifiDisplayAdapter != null) {
670 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700671 }
Jeff Browne08ae382012-09-07 20:36:36 -0700672 }
673 }
674
Jeff Brown4ccb8232014-01-16 22:16:42 -0800675 private void pauseWifiDisplayInternal() {
676 synchronized (mSyncRoot) {
677 if (mWifiDisplayAdapter != null) {
678 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700679 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700680 }
681 }
682
Jeff Brown4ccb8232014-01-16 22:16:42 -0800683 private void resumeWifiDisplayInternal() {
684 synchronized (mSyncRoot) {
685 if (mWifiDisplayAdapter != null) {
686 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700687 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700688 }
689 }
690
Jeff Brown4ccb8232014-01-16 22:16:42 -0800691 private void disconnectWifiDisplayInternal() {
692 synchronized (mSyncRoot) {
693 if (mWifiDisplayAdapter != null) {
694 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700695 }
Jeff Browne08ae382012-09-07 20:36:36 -0700696 }
697 }
698
Jeff Brown4ccb8232014-01-16 22:16:42 -0800699 private void renameWifiDisplayInternal(String address, String alias) {
700 synchronized (mSyncRoot) {
701 if (mWifiDisplayAdapter != null) {
702 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700703 }
Jeff Brown89d55462012-09-19 11:33:42 -0700704 }
705 }
706
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707 private void forgetWifiDisplayInternal(String address) {
708 synchronized (mSyncRoot) {
709 if (mWifiDisplayAdapter != null) {
710 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700711 }
Jeff Brown89d55462012-09-19 11:33:42 -0700712 }
713 }
714
Jeff Brown4ccb8232014-01-16 22:16:42 -0800715 private WifiDisplayStatus getWifiDisplayStatusInternal() {
716 synchronized (mSyncRoot) {
717 if (mWifiDisplayAdapter != null) {
718 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700719 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800720 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700721 }
722 }
723
Michael Wright1c9977b2016-07-12 13:30:10 -0700724 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100725 synchronized (mSyncRoot) {
726 LogicalDisplay display = mLogicalDisplays.get(displayId);
727 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700728 display.getRequestedColorModeLocked() != colorMode) {
729 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100730 scheduleTraversalLocked(false);
731 }
732 }
733 }
734
Bryan Mawhinney462e29d2018-03-22 15:52:41 +0000735 private void setSaturationLevelInternal(float level) {
736 if (level < 0 || level > 1) {
737 throw new IllegalArgumentException("Saturation level must be between 0 and 1");
738 }
739 float[] matrix = (level == 1.0f ? null : computeSaturationMatrix(level));
740 DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
741 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION, matrix);
742 }
743
744 private static float[] computeSaturationMatrix(float saturation) {
745 float desaturation = 1.0f - saturation;
746 float[] luminance = {0.231f * desaturation, 0.715f * desaturation, 0.072f * desaturation};
747 float[] matrix = {
748 luminance[0] + saturation, luminance[0], luminance[0], 0,
749 luminance[1], luminance[1] + saturation, luminance[1], 0,
750 luminance[2], luminance[2], luminance[2] + saturation, 0,
751 0, 0, 0, 1
752 };
753 return matrix;
754 }
755
Michael Wright75ee9fc2014-09-01 19:55:22 -0700756 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700757 IMediaProjection projection, int callingUid, String packageName, String name, int width,
758 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800759 synchronized (mSyncRoot) {
760 if (mVirtualDisplayAdapter == null) {
761 Slog.w(TAG, "Rejecting request to create private virtual display "
762 + "because the virtual display adapter is not available.");
763 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700764 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800765
766 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700767 callback, projection, callingUid, packageName, name, width, height, densityDpi,
768 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800769 if (device == null) {
770 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700771 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700772
Jeff Brown4ccb8232014-01-16 22:16:42 -0800773 handleDisplayDeviceAddedLocked(device);
774 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
775 if (display != null) {
776 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700777 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800778
779 // Something weird happened and the logical display was not created.
780 Slog.w(TAG, "Rejecting request to create virtual display "
781 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700782 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800783 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700784 }
785 return -1;
786 }
787
Michael Wright01e840f2014-06-26 16:03:25 -0700788 private void resizeVirtualDisplayInternal(IBinder appToken,
789 int width, int height, int densityDpi) {
790 synchronized (mSyncRoot) {
791 if (mVirtualDisplayAdapter == null) {
792 return;
793 }
794
795 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
796 }
797 }
798
Jeff Brown92207df2014-04-16 13:16:07 -0700799 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
800 synchronized (mSyncRoot) {
801 if (mVirtualDisplayAdapter == null) {
802 return;
803 }
804
805 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
806 }
807 }
808
Jeff Brown4ccb8232014-01-16 22:16:42 -0800809 private void releaseVirtualDisplayInternal(IBinder appToken) {
810 synchronized (mSyncRoot) {
811 if (mVirtualDisplayAdapter == null) {
812 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700813 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700814
Jeff Brown4ccb8232014-01-16 22:16:42 -0800815 DisplayDevice device =
816 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
817 if (device != null) {
818 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700819 }
820 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700821 }
822
Santos Cordonc22c5632017-06-21 16:03:49 -0700823 private void registerDefaultDisplayAdapters() {
824 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700825 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700826 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800827 registerDisplayAdapterLocked(new LocalDisplayAdapter(
828 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700829
830 // Standalone VR devices rely on a virtual display as their primary display for
831 // 2D UI. We register virtual display adapter along side the main display adapter
832 // here so that it is ready by the time the system sends the home Intent for
833 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
834 // the virtual display inside VR before any VR-specific apps even run.
835 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
836 mHandler, mDisplayAdapterListener);
837 if (mVirtualDisplayAdapter != null) {
838 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
839 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700840 }
841 }
842
843 private void registerAdditionalDisplayAdapters() {
844 synchronized (mSyncRoot) {
845 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700846 registerOverlayDisplayAdapterLocked();
847 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700848 }
849 }
850 }
851
Jeff Brown89d55462012-09-19 11:33:42 -0700852 private void registerOverlayDisplayAdapterLocked() {
853 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
854 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
855 }
856
857 private void registerWifiDisplayAdapterLocked() {
858 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700859 com.android.internal.R.bool.config_enableWifiDisplay)
860 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700861 mWifiDisplayAdapter = new WifiDisplayAdapter(
862 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
863 mPersistentDataStore);
864 registerDisplayAdapterLocked(mWifiDisplayAdapter);
865 }
866 }
867
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700868 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
869 // In safe mode, we disable non-essential display adapters to give the user
870 // an opportunity to fix broken settings or other problems that might affect
871 // system stability.
872 // In only-core mode, we disable non-essential display adapters to minimize
873 // the number of dependencies that are started while in this mode and to
874 // prevent problems that might occur due to the device being encrypted.
875 return !mSafeMode && !mOnlyCore;
876 }
877
878 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
879 mDisplayAdapters.add(adapter);
880 adapter.registerLocked();
881 }
882
Jeff Brownbd6e1502012-08-28 03:27:37 -0700883 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700884 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700885 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700886 }
887 }
888
Jeff Browna506a6e2013-06-04 00:02:38 -0700889 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700890 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700891 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700892 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700893 return;
894 }
895
Jeff Brown10acf6d2015-04-14 14:20:47 -0700896 Slog.i(TAG, "Display device added: " + info);
897 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700898
899 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700900 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700901 Runnable work = updateDisplayStateLocked(device);
902 if (work != null) {
903 work.run();
904 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700905 scheduleTraversalLocked(false);
906 }
907
Jeff Brownbd6e1502012-08-28 03:27:37 -0700908 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700909 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700910 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700911 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700912 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700913 return;
914 }
915
Jeff Brown10acf6d2015-04-14 14:20:47 -0700916 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
917 if (diff == DisplayDeviceInfo.DIFF_STATE) {
918 Slog.i(TAG, "Display device changed state: \"" + info.name
919 + "\", " + Display.stateToString(info.state));
920 } else if (diff != 0) {
921 Slog.i(TAG, "Display device changed: " + info);
922 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700923 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
924 try {
925 mPersistentDataStore.setColorMode(device, info.colorMode);
926 } finally {
927 mPersistentDataStore.saveIfNeeded();
928 }
929 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700930 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700931
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700932 device.applyPendingDisplayDeviceInfoChangesLocked();
933 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700934 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700935 }
936 }
937 }
938
Jeff Brownbd6e1502012-08-28 03:27:37 -0700939 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700940 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700941 handleDisplayDeviceRemovedLocked(device);
942 }
943 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700944
Jeff Browna506a6e2013-06-04 00:02:38 -0700945 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700946 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700947 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700948 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700949 return;
950 }
951
Jeff Brown10acf6d2015-04-14 14:20:47 -0700952 Slog.i(TAG, "Display device removed: " + info);
953 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700954
Jeff Browna506a6e2013-06-04 00:02:38 -0700955 updateLogicalDisplaysLocked();
956 scheduleTraversalLocked(false);
957 }
958
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200959 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
960 if (displayId == Display.DEFAULT_DISPLAY) {
961 recordTopInsetLocked(display);
962 }
963 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
964 }
965
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700966 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700967 final int count = mDisplayDevices.size();
968 for (int i = 0; i < count; i++) {
969 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700970 Runnable runnable = updateDisplayStateLocked(device);
971 if (runnable != null) {
972 workQueue.add(runnable);
973 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700974 }
975 }
976
Jeff Browne75926d2014-09-18 15:24:49 -0700977 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700978 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700979 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700980 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
981 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700982 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700983 }
Jeff Browne75926d2014-09-18 15:24:49 -0700984 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700985 }
986
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700987 // Adds a new logical display based on the given display device.
988 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700989 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700990 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
991 boolean isDefault = (deviceInfo.flags
992 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
993 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
994 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
995 isDefault = false;
996 }
997
Jeff Brown27f1d672012-10-17 18:32:34 -0700998 if (!isDefault && mSingleDisplayDemoMode) {
999 Slog.i(TAG, "Not creating a logical display for a secondary display "
1000 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001001 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001002 }
1003
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001004 final int displayId = assignDisplayIdLocked(isDefault);
1005 final int layerStack = assignLayerStackLocked(displayId);
1006
Jeff Brownd728bf52012-09-08 18:05:28 -07001007 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001008 display.updateLocked(mDisplayDevices);
1009 if (!display.isValidLocked()) {
1010 // This should never happen currently.
1011 Slog.w(TAG, "Ignoring display device because the logical display "
1012 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001013 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001014 }
1015
Michael Wrighteedcbf12017-08-16 23:14:54 +01001016 configureColorModeLocked(display, device);
1017 if (isDefault) {
1018 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001019 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001020 }
1021
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001022 mLogicalDisplays.put(displayId, display);
1023
1024 // Wake up waitForDefaultDisplay.
1025 if (isDefault) {
1026 mSyncRoot.notifyAll();
1027 }
1028
1029 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001030 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001031 }
1032
1033 private int assignDisplayIdLocked(boolean isDefault) {
1034 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1035 }
1036
1037 private int assignLayerStackLocked(int displayId) {
1038 // Currently layer stacks and display ids are the same.
1039 // This need not be the case.
1040 return displayId;
1041 }
1042
Michael Wrighteedcbf12017-08-16 23:14:54 +01001043 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1044 if (display.getPrimaryDisplayDeviceLocked() == device) {
1045 int colorMode = mPersistentDataStore.getColorMode(device);
1046 if (colorMode == Display.COLOR_MODE_INVALID) {
1047 if ((device.getDisplayDeviceInfoLocked().flags
1048 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1049 colorMode = mDefaultDisplayDefaultColorMode;
1050 } else {
1051 colorMode = Display.COLOR_MODE_DEFAULT;
1052 }
1053 }
1054 display.setRequestedColorModeLocked(colorMode);
1055 }
1056 }
1057
1058 // If we've never recorded stable device stats for this device before and they aren't
1059 // explicitly configured, go ahead and record the stable device stats now based on the status
1060 // of the default display at first boot.
1061 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1062 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1063 DisplayInfo info = d.getDisplayInfoLocked();
1064 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1065 }
1066 }
1067
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001068 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
1069 // We must only persist the inset after boot has completed, otherwise we will end up
1070 // overwriting the persisted value before the masking flag has been loaded from the
1071 // resource overlay.
1072 if (!mSystemReady || d == null) {
1073 return;
1074 }
1075 int topInset = d.getInsets().top;
1076 if (topInset == mDefaultDisplayTopInset) {
1077 return;
1078 }
1079 mDefaultDisplayTopInset = topInset;
1080 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1081 }
1082
Michael Wrighteedcbf12017-08-16 23:14:54 +01001083 private void setStableDisplaySizeLocked(int width, int height) {
1084 mStableDisplaySize = new Point(width, height);
1085 try {
1086 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1087 } finally {
1088 mPersistentDataStore.saveIfNeeded();
1089 }
1090 }
1091
Dan Gittik122df862018-03-28 16:59:22 +01001092 @VisibleForTesting
1093 Curve getMinimumBrightnessCurveInternal() {
1094 return mMinimumBrightnessCurve;
1095 }
1096
Michael Wrighteef0e132017-11-21 17:57:52 +00001097 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001098 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001099 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001100 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001101 final int userSerial = getUserManager().getUserSerialNumber(userId);
1102 synchronized (mSyncRoot) {
1103 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001104 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1105 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001106 } finally {
1107 mPersistentDataStore.saveIfNeeded();
1108 }
1109 if (userId == mCurrentUserId) {
1110 mDisplayPowerController.setBrightnessConfiguration(c);
1111 }
1112 }
1113 }
1114
Dan Gittik122df862018-03-28 16:59:22 +01001115 @VisibleForTesting
1116 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1117 if (config == null) {
1118 return;
1119 }
1120 if (isBrightnessConfigurationTooDark(config)) {
1121 throw new IllegalArgumentException("brightness curve is too dark");
1122 }
1123 }
1124
1125 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1126 Pair<float[], float[]> curve = config.getCurve();
1127 float[] lux = curve.first;
1128 float[] nits = curve.second;
1129 for (int i = 0; i < lux.length; i++) {
1130 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1131 return true;
1132 }
1133 }
1134 return false;
1135 }
1136
Michael Wrighteef0e132017-11-21 17:57:52 +00001137 private void loadBrightnessConfiguration() {
1138 synchronized (mSyncRoot) {
1139 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1140 BrightnessConfiguration config =
1141 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1142 mDisplayPowerController.setBrightnessConfiguration(config);
1143 }
1144 }
1145
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001146 // Updates all existing logical displays given the current set of display devices.
1147 // Removes invalid logical displays.
1148 // Sends notifications if needed.
1149 private boolean updateLogicalDisplaysLocked() {
1150 boolean changed = false;
1151 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1152 final int displayId = mLogicalDisplays.keyAt(i);
1153 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1154
1155 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1156 display.updateLocked(mDisplayDevices);
1157 if (!display.isValidLocked()) {
1158 mLogicalDisplays.removeAt(i);
1159 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1160 changed = true;
1161 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001162 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001163 changed = true;
1164 }
1165 }
1166 return changed;
1167 }
1168
Robert Carrae606b42018-02-15 15:36:23 -08001169 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001170 // Clear all viewports before configuring displays so that we can keep
1171 // track of which ones we have configured.
1172 clearViewportsLocked();
1173
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001174 // Configure each display device.
1175 final int count = mDisplayDevices.size();
1176 for (int i = 0; i < count; i++) {
1177 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001178 configureDisplayLocked(t, device);
1179 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001180 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001181
1182 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001183 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001184 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1185 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001186 }
1187
Michael Wright3f145a22014-07-22 19:46:03 -07001188 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001189 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001190 synchronized (mSyncRoot) {
1191 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001192 if (display == null) {
1193 return;
1194 }
1195 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001196 if (DEBUG) {
1197 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1198 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1199 }
1200
Craig Mautner722285e2012-09-07 13:55:58 -07001201 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001202 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001203 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001204 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1205 // Scan supported modes returned by display.getInfo() to find a mode with the same
1206 // size as the default display mode but with the specified refresh rate instead.
1207 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1208 requestedRefreshRate);
1209 }
1210 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001211 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001212 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001213 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001214 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001215 scheduleTraversalLocked(inTraversal);
1216 }
Craig Mautner722285e2012-09-07 13:55:58 -07001217 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001218 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001219
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001220 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1221 synchronized (mSyncRoot) {
1222 LogicalDisplay display = mLogicalDisplays.get(displayId);
1223 if (display == null) {
1224 return;
1225 }
1226 if (display.getDisplayOffsetXLocked() != x
1227 || display.getDisplayOffsetYLocked() != y) {
1228 if (DEBUG) {
1229 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1230 + x + ", " + y + ")");
1231 }
1232 display.setDisplayOffsetsLocked(x, y);
1233 scheduleTraversalLocked(false);
1234 }
1235 }
1236 }
1237
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001238 // Updates the lists of UIDs that are present on displays.
1239 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1240 synchronized (mSyncRoot) {
1241 mDisplayAccessUIDs.clear();
1242 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1243 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1244 newDisplayAccessUIDs.valueAt(i));
1245 }
1246 }
1247 }
1248
1249 // Checks if provided UID's content is present on the display and UID has access to it.
1250 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1251 synchronized (mSyncRoot) {
1252 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1253 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1254 }
1255 }
1256
Jeff Brownd728bf52012-09-08 18:05:28 -07001257 private void clearViewportsLocked() {
1258 mDefaultViewport.valid = false;
1259 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001260 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001261 }
1262
Robert Carrae606b42018-02-15 15:36:23 -08001263 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001264 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1265 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001266
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001267 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001268 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001269 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001270 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001271 if (display != null && !display.hasContentLocked()) {
1272 // If the display does not have any content of its own, then
1273 // automatically mirror the default logical display contents.
1274 display = null;
1275 }
1276 if (display == null) {
1277 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1278 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001279 }
1280
1281 // Apply the logical display configuration to the display device.
1282 if (display == null) {
1283 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001284 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001285 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001286 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001287 }
Robert Carrae606b42018-02-15 15:36:23 -08001288 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001289
1290 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001291 if (!mDefaultViewport.valid
1292 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1293 setViewportLocked(mDefaultViewport, display, device);
1294 }
1295 if (!mExternalTouchViewport.valid
1296 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1297 setViewportLocked(mExternalTouchViewport, display, device);
1298 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001299
1300 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1301 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1302 setViewportLocked(viewport, display, device);
1303 }
1304 }
1305
1306 /** Gets the virtual device viewport or creates it if not yet created. */
1307 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1308 DisplayViewport viewport;
1309 final int count = mVirtualTouchViewports.size();
1310 for (int i = 0; i < count; i++) {
1311 viewport = mVirtualTouchViewports.get(i);
1312 if (uniqueId.equals(viewport.uniqueId)) {
1313 return viewport;
1314 }
1315 }
1316
1317 viewport = new DisplayViewport();
1318 viewport.uniqueId = uniqueId;
1319 mVirtualTouchViewports.add(viewport);
1320 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001321 }
1322
1323 private static void setViewportLocked(DisplayViewport viewport,
1324 LogicalDisplay display, DisplayDevice device) {
1325 viewport.valid = true;
1326 viewport.displayId = display.getDisplayIdLocked();
1327 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001328 }
1329
1330 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1331 final int count = mLogicalDisplays.size();
1332 for (int i = 0; i < count; i++) {
1333 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1334 if (display.getPrimaryDisplayDeviceLocked() == device) {
1335 return display;
1336 }
1337 }
1338 return null;
1339 }
1340
Jeff Brownbd6e1502012-08-28 03:27:37 -07001341 private void sendDisplayEventLocked(int displayId, int event) {
1342 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1343 mHandler.sendMessage(msg);
1344 }
1345
Robert Carrae606b42018-02-15 15:36:23 -08001346 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001347 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001348 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001349 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001350 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001351 if (!inTraversal) {
1352 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1353 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001354 }
1355 }
1356
1357 // Runs on Handler thread.
1358 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001359 private void deliverDisplayEvent(int displayId, int event) {
1360 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001361 Slog.d(TAG, "Delivering display event: displayId="
1362 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001363 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001364
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001365 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001366 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001367 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001368 count = mCallbacks.size();
1369 mTempCallbacks.clear();
1370 for (int i = 0; i < count; i++) {
1371 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001372 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001373 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001374
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001375 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001376 for (int i = 0; i < count; i++) {
1377 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1378 }
1379 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001380 }
1381
Michael Wrightc39d47a2014-07-08 18:07:36 -07001382 private IMediaProjectionManager getProjectionService() {
1383 if (mProjectionService == null) {
1384 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1385 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1386 }
1387 return mProjectionService;
1388 }
1389
Michael Wrighteef0e132017-11-21 17:57:52 +00001390 private UserManager getUserManager() {
1391 return mContext.getSystemService(UserManager.class);
1392 }
1393
Jeff Brown4ccb8232014-01-16 22:16:42 -08001394 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001395 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001396
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001397 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001398 pw.println(" mOnlyCode=" + mOnlyCore);
1399 pw.println(" mSafeMode=" + mSafeMode);
1400 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001401 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001402 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1403 pw.println(" mDefaultViewport=" + mDefaultViewport);
1404 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001405 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001406 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001407 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001408 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001409 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1410
Jeff Brown9e316a12012-10-08 19:17:06 -07001411
Jeff Brownbd6e1502012-08-28 03:27:37 -07001412 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001413 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001414
1415 pw.println();
1416 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001417 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001418 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001419 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001420 }
Craig Mautner9de49362012-08-02 14:30:30 -07001421
Jeff Brownbd6e1502012-08-28 03:27:37 -07001422 pw.println();
1423 pw.println("Display Devices: size=" + mDisplayDevices.size());
1424 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001425 pw.println(" " + device.getDisplayDeviceInfoLocked());
1426 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001427 }
1428
1429 final int logicalDisplayCount = mLogicalDisplays.size();
1430 pw.println();
1431 pw.println("Logical Displays: size=" + logicalDisplayCount);
1432 for (int i = 0; i < logicalDisplayCount; i++) {
1433 int displayId = mLogicalDisplays.keyAt(i);
1434 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1435 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001436 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001437 }
Jeff Brownce468a32013-11-21 16:42:03 -08001438
1439 final int callbackCount = mCallbacks.size();
1440 pw.println();
1441 pw.println("Callbacks: size=" + callbackCount);
1442 for (int i = 0; i < callbackCount; i++) {
1443 CallbackRecord callback = mCallbacks.valueAt(i);
1444 pw.println(" " + i + ": mPid=" + callback.mPid
1445 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1446 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001447
1448 if (mDisplayPowerController != null) {
1449 mDisplayPowerController.dump(pw);
1450 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001451
1452 pw.println();
1453 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001454 }
1455 }
1456
Dan Gittik122df862018-03-28 16:59:22 +01001457 private static float[] getFloatArray(TypedArray array) {
1458 int length = array.length();
1459 float[] floatArray = new float[length];
1460 for (int i = 0; i < length; i++) {
1461 floatArray[i] = array.getFloat(i, Float.NaN);
1462 }
1463 array.recycle();
1464 return floatArray;
1465 }
1466
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001467 /**
1468 * This is the object that everything in the display manager locks on.
1469 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1470 * clear that the object belongs to the display manager service and that it is
1471 * a unique object with a special purpose.
1472 */
1473 public static final class SyncRoot {
1474 }
1475
Santos Cordonee8931e2017-04-05 10:31:15 -07001476 @VisibleForTesting
1477 static class Injector {
1478 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1479 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1480 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1481 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001482
1483 long getDefaultDisplayDelayTimeout() {
1484 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1485 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001486 }
1487
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001488 @VisibleForTesting
1489 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1490 synchronized (mSyncRoot) {
1491 LogicalDisplay display = mLogicalDisplays.get(displayId);
1492 if (display != null) {
1493 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1494 return displayDevice.getDisplayDeviceInfoLocked();
1495 }
1496 return null;
1497 }
1498 }
1499
Jeff Brownbd6e1502012-08-28 03:27:37 -07001500 private final class DisplayManagerHandler extends Handler {
1501 public DisplayManagerHandler(Looper looper) {
1502 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001503 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001504
Jeff Brownbd6e1502012-08-28 03:27:37 -07001505 @Override
1506 public void handleMessage(Message msg) {
1507 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001508 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1509 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001510 break;
1511
1512 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1513 registerAdditionalDisplayAdapters();
1514 break;
1515
1516 case MSG_DELIVER_DISPLAY_EVENT:
1517 deliverDisplayEvent(msg.arg1, msg.arg2);
1518 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001519
1520 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001521 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001522 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001523
1524 case MSG_UPDATE_VIEWPORT: {
1525 synchronized (mSyncRoot) {
1526 mTempDefaultViewport.copyFrom(mDefaultViewport);
1527 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001528 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1529 mTempVirtualTouchViewports.clear();
1530 for (DisplayViewport d : mVirtualTouchViewports) {
1531 mTempVirtualTouchViewports.add(d.makeCopy());
1532 }
1533 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001534 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001535 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1536 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001537 break;
1538 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001539
Michael Wrighteef0e132017-11-21 17:57:52 +00001540 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1541 loadBrightnessConfiguration();
1542 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001543 }
1544 }
1545 }
1546
1547 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1548 @Override
1549 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1550 switch (event) {
1551 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1552 handleDisplayDeviceAdded(device);
1553 break;
1554
1555 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1556 handleDisplayDeviceChanged(device);
1557 break;
1558
1559 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1560 handleDisplayDeviceRemoved(device);
1561 break;
1562 }
1563 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001564
1565 @Override
1566 public void onTraversalRequested() {
1567 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001568 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001569 }
1570 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001571 }
1572
1573 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001574 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001575 private final IDisplayManagerCallback mCallback;
1576
Jeff Brownce468a32013-11-21 16:42:03 -08001577 public boolean mWifiDisplayScanRequested;
1578
Jeff Brownbd6e1502012-08-28 03:27:37 -07001579 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1580 mPid = pid;
1581 mCallback = callback;
1582 }
1583
1584 @Override
1585 public void binderDied() {
1586 if (DEBUG) {
1587 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1588 }
Jeff Brownce468a32013-11-21 16:42:03 -08001589 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001590 }
1591
1592 public void notifyDisplayEventAsync(int displayId, int event) {
1593 try {
1594 mCallback.onDisplayEvent(displayId, event);
1595 } catch (RemoteException ex) {
1596 Slog.w(TAG, "Failed to notify process "
1597 + mPid + " that displays changed, assuming it died.", ex);
1598 binderDied();
1599 }
1600 }
1601 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001602
Santos Cordonee8931e2017-04-05 10:31:15 -07001603 @VisibleForTesting
1604 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001605 /**
1606 * Returns information about the specified logical display.
1607 *
1608 * @param displayId The logical display id.
1609 * @return The logical display info, or null if the display does not exist. The
1610 * returned object must be treated as immutable.
1611 */
1612 @Override // Binder call
1613 public DisplayInfo getDisplayInfo(int displayId) {
1614 final int callingUid = Binder.getCallingUid();
1615 final long token = Binder.clearCallingIdentity();
1616 try {
1617 return getDisplayInfoInternal(displayId, callingUid);
1618 } finally {
1619 Binder.restoreCallingIdentity(token);
1620 }
1621 }
1622
1623 /**
1624 * Returns the list of all display ids.
1625 */
1626 @Override // Binder call
1627 public int[] getDisplayIds() {
1628 final int callingUid = Binder.getCallingUid();
1629 final long token = Binder.clearCallingIdentity();
1630 try {
1631 return getDisplayIdsInternal(callingUid);
1632 } finally {
1633 Binder.restoreCallingIdentity(token);
1634 }
1635 }
1636
Michael Wrighteedcbf12017-08-16 23:14:54 +01001637 /**
1638 * Returns the stable device display size, in pixels.
1639 */
1640 @Override // Binder call
1641 public Point getStableDisplaySize() {
1642 final long token = Binder.clearCallingIdentity();
1643 try {
1644 return getStableDisplaySizeInternal();
1645 } finally {
1646 Binder.restoreCallingIdentity(token);
1647 }
1648 }
1649
Jeff Brown4ccb8232014-01-16 22:16:42 -08001650 @Override // Binder call
1651 public void registerCallback(IDisplayManagerCallback callback) {
1652 if (callback == null) {
1653 throw new IllegalArgumentException("listener must not be null");
1654 }
1655
1656 final int callingPid = Binder.getCallingPid();
1657 final long token = Binder.clearCallingIdentity();
1658 try {
1659 registerCallbackInternal(callback, callingPid);
1660 } finally {
1661 Binder.restoreCallingIdentity(token);
1662 }
1663 }
1664
1665 @Override // Binder call
1666 public void startWifiDisplayScan() {
1667 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1668 "Permission required to start wifi display scans");
1669
1670 final int callingPid = Binder.getCallingPid();
1671 final long token = Binder.clearCallingIdentity();
1672 try {
1673 startWifiDisplayScanInternal(callingPid);
1674 } finally {
1675 Binder.restoreCallingIdentity(token);
1676 }
1677 }
1678
1679 @Override // Binder call
1680 public void stopWifiDisplayScan() {
1681 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1682 "Permission required to stop wifi display scans");
1683
1684 final int callingPid = Binder.getCallingPid();
1685 final long token = Binder.clearCallingIdentity();
1686 try {
1687 stopWifiDisplayScanInternal(callingPid);
1688 } finally {
1689 Binder.restoreCallingIdentity(token);
1690 }
1691 }
1692
1693 @Override // Binder call
1694 public void connectWifiDisplay(String address) {
1695 if (address == null) {
1696 throw new IllegalArgumentException("address must not be null");
1697 }
1698 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1699 "Permission required to connect to a wifi display");
1700
1701 final long token = Binder.clearCallingIdentity();
1702 try {
1703 connectWifiDisplayInternal(address);
1704 } finally {
1705 Binder.restoreCallingIdentity(token);
1706 }
1707 }
1708
1709 @Override // Binder call
1710 public void disconnectWifiDisplay() {
1711 // This request does not require special permissions.
1712 // Any app can request disconnection from the currently active wifi display.
1713 // This exception should no longer be needed once wifi display control moves
1714 // to the media router service.
1715
1716 final long token = Binder.clearCallingIdentity();
1717 try {
1718 disconnectWifiDisplayInternal();
1719 } finally {
1720 Binder.restoreCallingIdentity(token);
1721 }
1722 }
1723
1724 @Override // Binder call
1725 public void renameWifiDisplay(String address, String alias) {
1726 if (address == null) {
1727 throw new IllegalArgumentException("address must not be null");
1728 }
1729 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1730 "Permission required to rename to a wifi display");
1731
1732 final long token = Binder.clearCallingIdentity();
1733 try {
1734 renameWifiDisplayInternal(address, alias);
1735 } finally {
1736 Binder.restoreCallingIdentity(token);
1737 }
1738 }
1739
1740 @Override // Binder call
1741 public void forgetWifiDisplay(String address) {
1742 if (address == null) {
1743 throw new IllegalArgumentException("address must not be null");
1744 }
1745 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1746 "Permission required to forget to a wifi display");
1747
1748 final long token = Binder.clearCallingIdentity();
1749 try {
1750 forgetWifiDisplayInternal(address);
1751 } finally {
1752 Binder.restoreCallingIdentity(token);
1753 }
1754 }
1755
1756 @Override // Binder call
1757 public void pauseWifiDisplay() {
1758 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1759 "Permission required to pause a wifi display session");
1760
1761 final long token = Binder.clearCallingIdentity();
1762 try {
1763 pauseWifiDisplayInternal();
1764 } finally {
1765 Binder.restoreCallingIdentity(token);
1766 }
1767 }
1768
1769 @Override // Binder call
1770 public void resumeWifiDisplay() {
1771 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1772 "Permission required to resume a wifi display session");
1773
1774 final long token = Binder.clearCallingIdentity();
1775 try {
1776 resumeWifiDisplayInternal();
1777 } finally {
1778 Binder.restoreCallingIdentity(token);
1779 }
1780 }
1781
1782 @Override // Binder call
1783 public WifiDisplayStatus getWifiDisplayStatus() {
1784 // This request does not require special permissions.
1785 // Any app can get information about available wifi displays.
1786
1787 final long token = Binder.clearCallingIdentity();
1788 try {
1789 return getWifiDisplayStatusInternal();
1790 } finally {
1791 Binder.restoreCallingIdentity(token);
1792 }
1793 }
1794
1795 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001796 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001797 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001798 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1799 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001800 final long token = Binder.clearCallingIdentity();
1801 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001802 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001803 } finally {
1804 Binder.restoreCallingIdentity(token);
1805 }
1806 }
1807
1808 @Override // Binder call
Bryan Mawhinney462e29d2018-03-22 15:52:41 +00001809 public void setSaturationLevel(float level) {
1810 mContext.enforceCallingOrSelfPermission(
1811 Manifest.permission.CONTROL_DISPLAY_SATURATION,
1812 "Permission required to set display saturation level");
1813 final long token = Binder.clearCallingIdentity();
1814 try {
1815 setSaturationLevelInternal(level);
1816 } finally {
1817 Binder.restoreCallingIdentity(token);
1818 }
1819 }
1820
1821 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001822 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001823 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001824 int width, int height, int densityDpi, Surface surface, int flags,
1825 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001826 final int callingUid = Binder.getCallingUid();
1827 if (!validatePackageName(callingUid, packageName)) {
1828 throw new SecurityException("packageName must match the calling uid");
1829 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001830 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001831 throw new IllegalArgumentException("appToken must not be null");
1832 }
1833 if (TextUtils.isEmpty(name)) {
1834 throw new IllegalArgumentException("name must be non-null and non-empty");
1835 }
1836 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1837 throw new IllegalArgumentException("width, height, and densityDpi must be "
1838 + "greater than 0");
1839 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001840 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001841 throw new IllegalArgumentException("Surface can't be single-buffered");
1842 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001843
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001844 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1845 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1846
1847 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001848 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001849 throw new IllegalArgumentException(
1850 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1851 }
Michael Wright6720be42014-07-29 19:14:16 -07001852 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001853 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1854 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001855 }
1856
Michael Wrightc39d47a2014-07-08 18:07:36 -07001857 if (projection != null) {
1858 try {
1859 if (!getProjectionService().isValidMediaProjection(projection)) {
1860 throw new SecurityException("Invalid media projection");
1861 }
Michael Wright6720be42014-07-29 19:14:16 -07001862 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001863 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001864 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001865 }
1866 }
1867
Michael Wright6720be42014-07-29 19:14:16 -07001868 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001869 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001870 if (!canProjectVideo(projection)) {
1871 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1872 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1873 + "MediaProjection token in order to create a screen sharing virtual "
1874 + "display.");
1875 }
1876 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001877 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001878 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001879 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001880 + "or an appropriate MediaProjection token to create a "
1881 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001882 }
1883 }
1884
1885 final long token = Binder.clearCallingIdentity();
1886 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001887 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1888 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001889 } finally {
1890 Binder.restoreCallingIdentity(token);
1891 }
1892 }
1893
1894 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001895 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001896 int width, int height, int densityDpi) {
1897 final long token = Binder.clearCallingIdentity();
1898 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001899 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001900 } finally {
1901 Binder.restoreCallingIdentity(token);
1902 }
1903 }
1904
1905 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001906 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001907 if (surface != null && surface.isSingleBuffered()) {
1908 throw new IllegalArgumentException("Surface can't be single-buffered");
1909 }
Jeff Brown92207df2014-04-16 13:16:07 -07001910 final long token = Binder.clearCallingIdentity();
1911 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001912 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001913 } finally {
1914 Binder.restoreCallingIdentity(token);
1915 }
1916 }
1917
1918 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001919 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001920 final long token = Binder.clearCallingIdentity();
1921 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001922 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001923 } finally {
1924 Binder.restoreCallingIdentity(token);
1925 }
1926 }
1927
1928 @Override // Binder call
1929 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001930 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001931
1932 final long token = Binder.clearCallingIdentity();
1933 try {
1934 dumpInternal(pw);
1935 } finally {
1936 Binder.restoreCallingIdentity(token);
1937 }
1938 }
1939
Kenny Guy22bd0442017-10-26 00:15:54 +01001940 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001941 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001942 mContext.enforceCallingOrSelfPermission(
1943 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1944 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001945
1946 final int callingUid = Binder.getCallingUid();
1947 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00001948 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00001949 callingUid, callingPackage);
1950 final boolean hasUsageStats;
1951 if (mode == AppOpsManager.MODE_DEFAULT) {
1952 // The default behavior here is to check if PackageManager has given the app
1953 // permission.
1954 hasUsageStats = mContext.checkCallingPermission(
1955 Manifest.permission.PACKAGE_USAGE_STATS)
1956 == PackageManager.PERMISSION_GRANTED;
1957 } else {
1958 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1959 }
1960
1961 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001962 final long token = Binder.clearCallingIdentity();
1963 try {
Michael Wright144aac92017-12-21 18:37:41 +00001964 synchronized (mSyncRoot) {
1965 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
1966 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001967 } finally {
1968 Binder.restoreCallingIdentity(token);
1969 }
1970 }
1971
Michael Wrighteef0e132017-11-21 17:57:52 +00001972 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00001973 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
1974 mContext.enforceCallingOrSelfPermission(
1975 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
1976 "Permission required to to access ambient light stats.");
1977 final int callingUid = Binder.getCallingUid();
1978 final int userId = UserHandle.getUserId(callingUid);
1979 final long token = Binder.clearCallingIdentity();
1980 try {
1981 synchronized (mSyncRoot) {
1982 return mDisplayPowerController.getAmbientBrightnessStats(userId);
1983 }
1984 } finally {
1985 Binder.restoreCallingIdentity(token);
1986 }
1987 }
1988
1989 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00001990 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00001991 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001992 mContext.enforceCallingOrSelfPermission(
1993 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1994 "Permission required to change the display's brightness configuration");
1995 if (userId != UserHandle.getCallingUserId()) {
1996 mContext.enforceCallingOrSelfPermission(
1997 Manifest.permission.INTERACT_ACROSS_USERS,
1998 "Permission required to change the display brightness"
1999 + " configuration of another user");
2000 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002001 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2002 packageName = null;
2003 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002004 final long token = Binder.clearCallingIdentity();
2005 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002006 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002007 } finally {
2008 Binder.restoreCallingIdentity(token);
2009 }
2010 }
2011
Michael Wrightd8460232018-01-16 18:04:59 +00002012 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002013 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2014 mContext.enforceCallingOrSelfPermission(
2015 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2016 "Permission required to read the display's brightness configuration");
2017 if (userId != UserHandle.getCallingUserId()) {
2018 mContext.enforceCallingOrSelfPermission(
2019 Manifest.permission.INTERACT_ACROSS_USERS,
2020 "Permission required to read the display brightness"
2021 + " configuration of another user");
2022 }
2023 final long token = Binder.clearCallingIdentity();
2024 try {
2025 final int userSerial = getUserManager().getUserSerialNumber(userId);
2026 synchronized (mSyncRoot) {
2027 BrightnessConfiguration config =
2028 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2029 if (config == null) {
2030 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2031 }
2032 return config;
2033 }
2034 } finally {
2035 Binder.restoreCallingIdentity(token);
2036 }
2037 }
2038
2039 @Override // Binder call
2040 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2041 mContext.enforceCallingOrSelfPermission(
2042 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2043 "Permission required to read the display's default brightness configuration");
2044 final long token = Binder.clearCallingIdentity();
2045 try {
2046 synchronized (mSyncRoot) {
2047 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2048 }
2049 } finally {
2050 Binder.restoreCallingIdentity(token);
2051 }
2052 }
2053
2054 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002055 public void setTemporaryBrightness(int brightness) {
2056 mContext.enforceCallingOrSelfPermission(
2057 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2058 "Permission required to set the display's brightness");
2059 final long token = Binder.clearCallingIdentity();
2060 try {
2061 synchronized (mSyncRoot) {
2062 mDisplayPowerController.setTemporaryBrightness(brightness);
2063 }
2064 } finally {
2065 Binder.restoreCallingIdentity(token);
2066 }
2067 }
2068
2069 @Override // Binder call
2070 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2071 mContext.enforceCallingOrSelfPermission(
2072 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2073 "Permission required to set the display's auto brightness adjustment");
2074 final long token = Binder.clearCallingIdentity();
2075 try {
2076 synchronized (mSyncRoot) {
2077 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2078 }
2079 } finally {
2080 Binder.restoreCallingIdentity(token);
2081 }
2082 }
2083
Dan Gittik7a32fba2018-03-28 12:19:38 +01002084 @Override // Binder call
2085 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2086 FileDescriptor err, String[] args, ShellCallback callback,
2087 ResultReceiver resultReceiver) {
2088 final long token = Binder.clearCallingIdentity();
2089 try {
2090 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2091 command.exec(this, in, out, err, args, callback, resultReceiver);
2092 } finally {
2093 Binder.restoreCallingIdentity(token);
2094 }
2095 }
2096
Dan Gittik122df862018-03-28 16:59:22 +01002097 @Override // Binder call
2098 public Curve getMinimumBrightnessCurve() {
2099 final long token = Binder.clearCallingIdentity();
2100 try {
2101 return getMinimumBrightnessCurveInternal();
2102 } finally {
2103 Binder.restoreCallingIdentity(token);
2104 }
2105 }
2106
Dan Gittik7a32fba2018-03-28 12:19:38 +01002107 void setBrightness(int brightness) {
2108 Settings.System.putIntForUser(mContext.getContentResolver(),
2109 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2110 }
2111
2112 void resetBrightnessConfiguration() {
2113 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2114 mContext.getPackageName());
2115 }
2116
Jeff Brown4ccb8232014-01-16 22:16:42 -08002117 private boolean validatePackageName(int uid, String packageName) {
2118 if (packageName != null) {
2119 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2120 if (packageNames != null) {
2121 for (String n : packageNames) {
2122 if (n.equals(packageName)) {
2123 return true;
2124 }
2125 }
2126 }
2127 }
2128 return false;
2129 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002130
2131 private boolean canProjectVideo(IMediaProjection projection) {
2132 if (projection != null) {
2133 try {
2134 if (projection.canProjectVideo()) {
2135 return true;
2136 }
2137 } catch (RemoteException e) {
2138 Slog.e(TAG, "Unable to query projection service for permissions", e);
2139 }
2140 }
2141 if (mContext.checkCallingPermission(
2142 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002143 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002144 return true;
2145 }
2146 return canProjectSecureVideo(projection);
2147 }
2148
2149 private boolean canProjectSecureVideo(IMediaProjection projection) {
2150 if (projection != null) {
2151 try {
2152 if (projection.canProjectSecureVideo()){
2153 return true;
2154 }
2155 } catch (RemoteException e) {
2156 Slog.e(TAG, "Unable to query projection service for permissions", e);
2157 }
2158 }
2159 return mContext.checkCallingPermission(
2160 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002161 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002162 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002163 }
2164
2165 private final class LocalService extends DisplayManagerInternal {
2166 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002167 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002168 SensorManager sensorManager) {
2169 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002170 DisplayBlanker blanker = new DisplayBlanker() {
2171 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002172 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002173 // The order of operations is important for legacy reasons.
2174 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002175 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002176 }
2177
2178 callbacks.onDisplayStateChange(state);
2179
2180 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002181 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002182 }
2183 }
2184 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002185 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002186 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002187 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002188
2189 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002190 }
2191
2192 @Override
2193 public boolean requestPowerState(DisplayPowerRequest request,
2194 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002195 synchronized (mSyncRoot) {
2196 return mDisplayPowerController.requestPowerState(request,
2197 waitForNegativeProximity);
2198 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002199 }
2200
2201 @Override
2202 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002203 synchronized (mSyncRoot) {
2204 return mDisplayPowerController.isProximitySensorAvailable();
2205 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002206 }
2207
2208 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002209 public DisplayInfo getDisplayInfo(int displayId) {
2210 return getDisplayInfoInternal(displayId, Process.myUid());
2211 }
2212
2213 @Override
2214 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2215 if (listener == null) {
2216 throw new IllegalArgumentException("listener must not be null");
2217 }
2218
2219 registerDisplayTransactionListenerInternal(listener);
2220 }
2221
2222 @Override
2223 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2224 if (listener == null) {
2225 throw new IllegalArgumentException("listener must not be null");
2226 }
2227
2228 unregisterDisplayTransactionListenerInternal(listener);
2229 }
2230
2231 @Override
2232 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2233 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2234 }
2235
2236 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002237 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2238 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2239 }
2240
2241 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002242 public void performTraversal(SurfaceControl.Transaction t) {
2243 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002244 }
2245
2246 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002247 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002248 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2249 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2250 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002251 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002252
2253 @Override
2254 public void setDisplayOffsets(int displayId, int x, int y) {
2255 setDisplayOffsetsInternal(displayId, x, y);
2256 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002257
2258 @Override
2259 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2260 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2261 }
2262
2263 @Override
2264 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2265 return isUidPresentOnDisplayInternal(uid, displayId);
2266 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002267
2268 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002269 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002270 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002271 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002272 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002273 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002274
2275 @Override
2276 public void onOverlayChanged() {
2277 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002278 for (int i = 0; i < mDisplayDevices.size(); i++) {
2279 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002280 }
2281 }
2282 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002283 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002284}