blob: 02e4fe00f893e26ced96412ac2a4a2f44ade668f [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;
Michael Wrighteef0e132017-11-21 17:57:52 +000032import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000033import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070034import android.content.Context;
35import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010036import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010037import android.content.res.Resources;
38import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070039import android.hardware.SensorManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010040import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000041import android.hardware.display.BrightnessConfiguration;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080043import android.hardware.display.DisplayManagerInternal;
44import android.hardware.display.DisplayViewport;
45import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070046import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070047import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070048import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070049import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080050import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070051import android.media.projection.IMediaProjection;
52import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070053import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070054import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070055import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080056import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070057import android.os.Looper;
58import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070059import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080060import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070061import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070062import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070063import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070064import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070065import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010066import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000067import android.os.UserManager;
Jeff Browna506a6e2013-06-04 00:02:38 -070068import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080069import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070070import android.util.Slog;
71import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070072import android.view.Display;
73import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070074import android.view.Surface;
75
Michael Wrighteef0e132017-11-21 17:57:52 +000076import com.android.internal.util.Preconditions;
Jorim Jaggied7993b2017-03-28 18:50:01 +010077import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080078import com.android.server.DisplayThread;
79import com.android.server.LocalServices;
80import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070081import com.android.server.UiThread;
Adrian Roose99bc052017-11-20 17:55:31 +010082import com.android.server.wm.WindowManagerInternal;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020083import com.android.server.wm.SurfaceAnimationThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070084
85import java.io.FileDescriptor;
86import java.io.PrintWriter;
87import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070088import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070089import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080090import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070091
92/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070093 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070094 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070095 * The {@link DisplayManagerService} manages the global lifecycle of displays,
96 * decides how to configure logical displays based on the physical display devices currently
97 * attached, sends notifications to the system and to applications when the state
98 * changes, and so on.
99 * </p><p>
100 * The display manager service relies on a collection of {@link DisplayAdapter} components,
101 * for discovering and configuring physical display devices attached to the system.
102 * There are separate display adapters for each manner that devices are attached:
103 * one display adapter for built-in local displays, one for simulated non-functional
104 * displays when the system is headless, one for simulated overlay displays used for
105 * development, one for wifi displays, etc.
106 * </p><p>
107 * Display adapters are only weakly coupled to the display manager service.
108 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700109 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700110 * by the display manager service. This separation of concerns is important for
111 * two main reasons. First, it neatly encapsulates the responsibilities of these
112 * two classes: display adapters handle individual display devices whereas
113 * the display manager service handles the global state. Second, it eliminates
114 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700115 * </p>
116 *
117 * <h3>Synchronization</h3>
118 * <p>
119 * Because the display manager may be accessed by multiple threads, the synchronization
120 * story gets a little complicated. In particular, the window manager may call into
121 * the display manager while holding a surface transaction with the expectation that
122 * it can apply changes immediately. Unfortunately, that means we can't just do
123 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700124 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700125 * To make this work, all of the objects that belong to the display manager must
126 * use the same lock. We call this lock the synchronization root and it has a unique
127 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
128 * named with the "Locked" suffix.
129 * </p><p>
130 * Where things get tricky is that the display manager is not allowed to make
131 * any potentially reentrant calls, especially into the window manager. We generally
132 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700133 * </p>
134 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800135public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700136 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700137 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700138
Jeff Brownbbd28a22012-09-20 16:47:15 -0700139 // When this system property is set to 0, WFD is forcibly disabled on boot.
140 // When this system property is set to 1, WFD is forcibly enabled on boot.
141 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
142 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
143
Jeff Brownbd6e1502012-08-28 03:27:37 -0700144 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
145
Santos Cordonc22c5632017-06-21 16:03:49 -0700146 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700147 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
148 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700149 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700150 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100151 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000152 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700153
Jeff Brownb880d882014-02-10 19:47:07 -0800154 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700155 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700156 private final Handler mUiHandler;
157 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800158 private WindowManagerInternal mWindowManagerInternal;
159 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700160 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700161
162 // The synchronization root for the display manager.
163 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800164 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
165 // into WindowManagerService methods that require mWindowMap while holding this unless you are
166 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700167 private final SyncRoot mSyncRoot = new SyncRoot();
168
169 // True if in safe mode.
170 // This option may disable certain display adapters.
171 public boolean mSafeMode;
172
173 // True if we are in a special boot mode where only core applications and
174 // services should be started. This option may disable certain display adapters.
175 public boolean mOnlyCore;
176
Jeff Brown27f1d672012-10-17 18:32:34 -0700177 // True if the display manager service should pretend there is only one display
178 // and only tell applications about the existence of the default logical display.
179 // The display manager can still mirror content to secondary displays but applications
180 // cannot present unique content on those displays.
181 // Used for demonstration purposes only.
182 private final boolean mSingleDisplayDemoMode;
183
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700184 // All callback records indexed by calling process id.
185 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700186 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700187
Jeff Brownbd6e1502012-08-28 03:27:37 -0700188 // List of all currently registered display adapters.
189 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
190
191 // List of all currently connected display devices.
192 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
193
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700194 // List of all logical displays indexed by logical display id.
195 private final SparseArray<LogicalDisplay> mLogicalDisplays =
196 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700197 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
198
Jeff Brown7f3994e2012-12-04 14:04:28 -0800199 // List of all display transaction listeners.
200 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
201 new CopyOnWriteArrayList<DisplayTransactionListener>();
202
Jeff Brownad9ef192014-04-08 17:26:30 -0700203 // Display power controller.
204 private DisplayPowerController mDisplayPowerController;
205
Jeff Brown037c33e2014-04-09 00:31:55 -0700206 // The overall display state, independent of changes that might influence one
207 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700208 private int mGlobalDisplayState = Display.STATE_ON;
209
210 // The overall display brightness.
211 // For now, this only applies to the built-in display but we may split it up eventually.
212 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700213
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700214 // Set to true when there are pending display changes that have yet to be applied
215 // to the surface flinger state.
216 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700217
Jeff Browne08ae382012-09-07 20:36:36 -0700218 // The Wifi display adapter, or null if not registered.
219 private WifiDisplayAdapter mWifiDisplayAdapter;
220
Jeff Brownce468a32013-11-21 16:42:03 -0800221 // The number of active wifi display scan requests.
222 private int mWifiDisplayScanRequestCount;
223
Jeff Browna506a6e2013-06-04 00:02:38 -0700224 // The virtual display adapter, or null if not registered.
225 private VirtualDisplayAdapter mVirtualDisplayAdapter;
226
Michael Wrighteef0e132017-11-21 17:57:52 +0000227 // The User ID of the current user
228 private @UserIdInt int mCurrentUserId;
229
Michael Wrighteedcbf12017-08-16 23:14:54 +0100230 // The stable device screen height and width. These are not tied to a specific display, even
231 // the default display, because they need to be stable over the course of the device's entire
232 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
233 // device).
234 private Point mStableDisplaySize = new Point();
235
Jeff Brownd728bf52012-09-08 18:05:28 -0700236 // Viewports of the default display and the display that should receive touch
237 // input from an external source. Used by the input system.
238 private final DisplayViewport mDefaultViewport = new DisplayViewport();
239 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700240 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700241
Jeff Brown89d55462012-09-19 11:33:42 -0700242 // Persistent data store for all internal settings maintained by the display manager service.
243 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
244
Jeff Brownbd6e1502012-08-28 03:27:37 -0700245 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700246 // May be used outside of the lock but only on the handler thread.
247 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700248
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700249 // Temporary display info, used for comparing display configurations.
250 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
251
Jeff Brownd728bf52012-09-08 18:05:28 -0700252 // Temporary viewports, used when sending new viewport information to the
253 // input system. May be used outside of the lock but only on the handler thread.
254 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
255 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700256 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700257
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700258 // The default color mode for default displays. Overrides the usual
259 // Display.Display.COLOR_MODE_DEFAULT for displays with the
260 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
261 private final int mDefaultDisplayDefaultColorMode;
262
Jeff Browne75926d2014-09-18 15:24:49 -0700263 // Temporary list of deferred work to perform when setting the display state.
264 // Only used by requestDisplayState. The field is self-synchronized and only
265 // intended for use inside of the requestGlobalDisplayStateInternal function.
266 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
267
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800268 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
269 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
270
Santos Cordonee8931e2017-04-05 10:31:15 -0700271 private final Injector mInjector;
272
Jeff Brownb880d882014-02-10 19:47:07 -0800273 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700274 this(context, new Injector());
275 }
276
277 @VisibleForTesting
278 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800279 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700280 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800281 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800282 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700283 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700284 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700285 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700286 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000287 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700288
289 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
290 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000291 mCurrentUserId = UserHandle.USER_SYSTEM;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700292 }
293
294 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100295 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000296 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
297 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100298 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
299 Process.THREAD_GROUP_TOP_APP);
300 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
301 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200302 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
303 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700304 }
305
Jeff Brown4ccb8232014-01-16 22:16:42 -0800306 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800307 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700308 // We need to pre-load the persistent data store so it's ready before the default display
309 // adapter is up so that we have it's configuration. We could load it lazily, but since
310 // 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 -0700311 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100312 synchronized(mSyncRoot) {
313 mPersistentDataStore.loadIfNeeded();
314 loadStableDisplayValuesLocked();
315 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700316 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800317
318 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
319 true /*allowIsolated*/);
320 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700321 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800322 }
323
324 @Override
325 public void onBootPhase(int phase) {
326 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
327 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700328 long timeout = SystemClock.uptimeMillis()
329 + mInjector.getDefaultDisplayDelayTimeout();
330 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
331 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800332 long delay = timeout - SystemClock.uptimeMillis();
333 if (delay <= 0) {
334 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700335 + "to be initialized. DefaultDisplay="
336 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
337 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800338 }
339 if (DEBUG) {
340 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
341 }
342 try {
343 mSyncRoot.wait(delay);
344 } catch (InterruptedException ex) {
345 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700346 }
347 }
348 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700349 }
350
Michael Wrighteef0e132017-11-21 17:57:52 +0000351 @Override
352 public void onSwitchUser(@UserIdInt int newUserId) {
353 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
354 synchronized (mSyncRoot) {
355 if (mCurrentUserId != newUserId) {
356 mCurrentUserId = newUserId;
357 BrightnessConfiguration config =
358 mPersistentDataStore.getBrightnessConfiguration(userSerial);
359 mDisplayPowerController.setBrightnessConfiguration(config);
360 }
361 }
362 }
363
Jeff Brown4ccb8232014-01-16 22:16:42 -0800364 // TODO: Use dependencies or a boot phase
365 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700366 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800367 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
368 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700369 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700370 }
371 }
372
373 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700374 * Called when the system is ready to go.
375 */
376 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700377 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700378 mSafeMode = safeMode;
379 mOnlyCore = onlyCore;
380 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700381
Jeff Brownbd6e1502012-08-28 03:27:37 -0700382 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100383 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700384 }
385
Santos Cordonee8931e2017-04-05 10:31:15 -0700386 @VisibleForTesting
387 Handler getDisplayHandler() {
388 return mHandler;
389 }
390
Michael Wrighteedcbf12017-08-16 23:14:54 +0100391 private void loadStableDisplayValuesLocked() {
392 final Point size = mPersistentDataStore.getStableDisplaySize();
393 if (size.x > 0 && size.y > 0) {
394 // Just set these values directly so we don't write the display persistent data again
395 // unnecessarily
396 mStableDisplaySize.set(size.x, size.y);
397 } else {
398 final Resources res = mContext.getResources();
399 final int width = res.getInteger(
400 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
401 final int height = res.getInteger(
402 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
403 if (width > 0 && height > 0) {
404 setStableDisplaySizeLocked(width, height);
405 }
406 }
407 }
408
409 private Point getStableDisplaySizeInternal() {
410 Point r = new Point();
411 synchronized (mSyncRoot) {
412 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
413 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
414 }
415 }
416 return r;
417 }
418
Jeff Brown4ccb8232014-01-16 22:16:42 -0800419 private void registerDisplayTransactionListenerInternal(
420 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800421 // List is self-synchronized copy-on-write.
422 mDisplayTransactionListeners.add(listener);
423 }
424
Jeff Brown4ccb8232014-01-16 22:16:42 -0800425 private void unregisterDisplayTransactionListenerInternal(
426 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800427 // List is self-synchronized copy-on-write.
428 mDisplayTransactionListeners.remove(listener);
429 }
430
Jeff Brown4ccb8232014-01-16 22:16:42 -0800431 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700432 int displayId, DisplayInfo info) {
433 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700434 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700435 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700436 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700437 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700438 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700439 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700440 }
441 }
442 }
443
Andrii Kuliancd097992017-03-23 18:31:59 -0700444 /**
445 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
446 */
447 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
448 synchronized (mSyncRoot) {
449 final LogicalDisplay display = mLogicalDisplays.get(displayId);
450 if (display != null) {
451 display.getNonOverrideDisplayInfoLocked(outInfo);
452 }
453 }
454 }
455
Santos Cordonee8931e2017-04-05 10:31:15 -0700456 @VisibleForTesting
457 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700458 synchronized (mSyncRoot) {
459 if (!mPendingTraversal) {
460 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700461 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700462 mPendingTraversal = false;
463
464 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700465 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800466
467 // List is self-synchronized copy-on-write.
468 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
469 listener.onDisplayTransaction();
470 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700471 }
472
Jeff Brown5d6443b2015-04-10 20:15:01 -0700473 private void requestGlobalDisplayStateInternal(int state, int brightness) {
474 if (state == Display.STATE_UNKNOWN) {
475 state = Display.STATE_ON;
476 }
477 if (state == Display.STATE_OFF) {
478 brightness = PowerManager.BRIGHTNESS_OFF;
479 } else if (brightness < 0) {
480 brightness = PowerManager.BRIGHTNESS_DEFAULT;
481 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
482 brightness = PowerManager.BRIGHTNESS_ON;
483 }
484
Jeff Browne75926d2014-09-18 15:24:49 -0700485 synchronized (mTempDisplayStateWorkQueue) {
486 try {
487 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700488 // Note that we do not need to schedule traversals here although it
489 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700490 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700491 if (mGlobalDisplayState == state
492 && mGlobalDisplayBrightness == brightness) {
493 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700494 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700495
496 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
497 + Display.stateToString(state)
498 + ", brightness=" + brightness + ")");
499 mGlobalDisplayState = state;
500 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700501 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700502 }
503
504 // Setting the display power state can take hundreds of milliseconds
505 // to complete so we defer the most expensive part of the work until
506 // after we have exited the critical section to avoid blocking other
507 // threads for a long time.
508 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
509 mTempDisplayStateWorkQueue.get(i).run();
510 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700511 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700512 } finally {
513 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700514 }
515 }
516 }
517
Jeff Brown4ccb8232014-01-16 22:16:42 -0800518 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700519 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800520 LogicalDisplay display = mLogicalDisplays.get(displayId);
521 if (display != null) {
522 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800523 if (info.hasAccess(callingUid)
524 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800525 return info;
526 }
527 }
528 return null;
529 }
530 }
531
532 private int[] getDisplayIdsInternal(int callingUid) {
533 synchronized (mSyncRoot) {
534 final int count = mLogicalDisplays.size();
535 int[] displayIds = new int[count];
536 int n = 0;
537 for (int i = 0; i < count; i++) {
538 LogicalDisplay display = mLogicalDisplays.valueAt(i);
539 DisplayInfo info = display.getDisplayInfoLocked();
540 if (info.hasAccess(callingUid)) {
541 displayIds[n++] = mLogicalDisplays.keyAt(i);
542 }
543 }
544 if (n != count) {
545 displayIds = Arrays.copyOfRange(displayIds, 0, n);
546 }
547 return displayIds;
548 }
549 }
550
551 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
552 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700553 if (mCallbacks.get(callingPid) != null) {
554 throw new SecurityException("The calling process has already "
555 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700556 }
557
Jeff Brownbd6e1502012-08-28 03:27:37 -0700558 CallbackRecord record = new CallbackRecord(callingPid, callback);
559 try {
560 IBinder binder = callback.asBinder();
561 binder.linkToDeath(record, 0);
562 } catch (RemoteException ex) {
563 // give up
564 throw new RuntimeException(ex);
565 }
566
567 mCallbacks.put(callingPid, record);
568 }
569 }
570
Jeff Brownce468a32013-11-21 16:42:03 -0800571 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700572 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800573 mCallbacks.remove(record.mPid);
574 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700575 }
576 }
577
Jeff Brown4ccb8232014-01-16 22:16:42 -0800578 private void startWifiDisplayScanInternal(int callingPid) {
579 synchronized (mSyncRoot) {
580 CallbackRecord record = mCallbacks.get(callingPid);
581 if (record == null) {
582 throw new IllegalStateException("The calling process has not "
583 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700584 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800585 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700586 }
587 }
588
Jeff Brownce468a32013-11-21 16:42:03 -0800589 private void startWifiDisplayScanLocked(CallbackRecord record) {
590 if (!record.mWifiDisplayScanRequested) {
591 record.mWifiDisplayScanRequested = true;
592 if (mWifiDisplayScanRequestCount++ == 0) {
593 if (mWifiDisplayAdapter != null) {
594 mWifiDisplayAdapter.requestStartScanLocked();
595 }
596 }
597 }
598 }
599
Jeff Brown4ccb8232014-01-16 22:16:42 -0800600 private void stopWifiDisplayScanInternal(int callingPid) {
601 synchronized (mSyncRoot) {
602 CallbackRecord record = mCallbacks.get(callingPid);
603 if (record == null) {
604 throw new IllegalStateException("The calling process has not "
605 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800606 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800607 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800608 }
609 }
610
611 private void stopWifiDisplayScanLocked(CallbackRecord record) {
612 if (record.mWifiDisplayScanRequested) {
613 record.mWifiDisplayScanRequested = false;
614 if (--mWifiDisplayScanRequestCount == 0) {
615 if (mWifiDisplayAdapter != null) {
616 mWifiDisplayAdapter.requestStopScanLocked();
617 }
618 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700619 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800620 + mWifiDisplayScanRequestCount);
621 mWifiDisplayScanRequestCount = 0;
622 }
623 }
624 }
625
Jeff Brown4ccb8232014-01-16 22:16:42 -0800626 private void connectWifiDisplayInternal(String address) {
627 synchronized (mSyncRoot) {
628 if (mWifiDisplayAdapter != null) {
629 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700630 }
Jeff Browne08ae382012-09-07 20:36:36 -0700631 }
632 }
633
Jeff Brown4ccb8232014-01-16 22:16:42 -0800634 private void pauseWifiDisplayInternal() {
635 synchronized (mSyncRoot) {
636 if (mWifiDisplayAdapter != null) {
637 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700638 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700639 }
640 }
641
Jeff Brown4ccb8232014-01-16 22:16:42 -0800642 private void resumeWifiDisplayInternal() {
643 synchronized (mSyncRoot) {
644 if (mWifiDisplayAdapter != null) {
645 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700646 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700647 }
648 }
649
Jeff Brown4ccb8232014-01-16 22:16:42 -0800650 private void disconnectWifiDisplayInternal() {
651 synchronized (mSyncRoot) {
652 if (mWifiDisplayAdapter != null) {
653 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700654 }
Jeff Browne08ae382012-09-07 20:36:36 -0700655 }
656 }
657
Jeff Brown4ccb8232014-01-16 22:16:42 -0800658 private void renameWifiDisplayInternal(String address, String alias) {
659 synchronized (mSyncRoot) {
660 if (mWifiDisplayAdapter != null) {
661 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700662 }
Jeff Brown89d55462012-09-19 11:33:42 -0700663 }
664 }
665
Jeff Brown4ccb8232014-01-16 22:16:42 -0800666 private void forgetWifiDisplayInternal(String address) {
667 synchronized (mSyncRoot) {
668 if (mWifiDisplayAdapter != null) {
669 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700670 }
Jeff Brown89d55462012-09-19 11:33:42 -0700671 }
672 }
673
Jeff Brown4ccb8232014-01-16 22:16:42 -0800674 private WifiDisplayStatus getWifiDisplayStatusInternal() {
675 synchronized (mSyncRoot) {
676 if (mWifiDisplayAdapter != null) {
677 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700678 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800679 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700680 }
681 }
682
Michael Wright1c9977b2016-07-12 13:30:10 -0700683 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100684 synchronized (mSyncRoot) {
685 LogicalDisplay display = mLogicalDisplays.get(displayId);
686 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700687 display.getRequestedColorModeLocked() != colorMode) {
688 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100689 scheduleTraversalLocked(false);
690 }
691 }
692 }
693
Michael Wright75ee9fc2014-09-01 19:55:22 -0700694 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700695 IMediaProjection projection, int callingUid, String packageName, String name, int width,
696 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800697 synchronized (mSyncRoot) {
698 if (mVirtualDisplayAdapter == null) {
699 Slog.w(TAG, "Rejecting request to create private virtual display "
700 + "because the virtual display adapter is not available.");
701 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700702 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800703
704 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700705 callback, projection, callingUid, packageName, name, width, height, densityDpi,
706 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707 if (device == null) {
708 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700709 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700710
Jeff Brown4ccb8232014-01-16 22:16:42 -0800711 handleDisplayDeviceAddedLocked(device);
712 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
713 if (display != null) {
714 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700715 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800716
717 // Something weird happened and the logical display was not created.
718 Slog.w(TAG, "Rejecting request to create virtual display "
719 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700720 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800721 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700722 }
723 return -1;
724 }
725
Michael Wright01e840f2014-06-26 16:03:25 -0700726 private void resizeVirtualDisplayInternal(IBinder appToken,
727 int width, int height, int densityDpi) {
728 synchronized (mSyncRoot) {
729 if (mVirtualDisplayAdapter == null) {
730 return;
731 }
732
733 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
734 }
735 }
736
Jeff Brown92207df2014-04-16 13:16:07 -0700737 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
738 synchronized (mSyncRoot) {
739 if (mVirtualDisplayAdapter == null) {
740 return;
741 }
742
743 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
744 }
745 }
746
Jeff Brown4ccb8232014-01-16 22:16:42 -0800747 private void releaseVirtualDisplayInternal(IBinder appToken) {
748 synchronized (mSyncRoot) {
749 if (mVirtualDisplayAdapter == null) {
750 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700751 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700752
Jeff Brown4ccb8232014-01-16 22:16:42 -0800753 DisplayDevice device =
754 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
755 if (device != null) {
756 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700757 }
758 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700759 }
760
Santos Cordonc22c5632017-06-21 16:03:49 -0700761 private void registerDefaultDisplayAdapters() {
762 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700763 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700764 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800765 registerDisplayAdapterLocked(new LocalDisplayAdapter(
766 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700767
768 // Standalone VR devices rely on a virtual display as their primary display for
769 // 2D UI. We register virtual display adapter along side the main display adapter
770 // here so that it is ready by the time the system sends the home Intent for
771 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
772 // the virtual display inside VR before any VR-specific apps even run.
773 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
774 mHandler, mDisplayAdapterListener);
775 if (mVirtualDisplayAdapter != null) {
776 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
777 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700778 }
779 }
780
781 private void registerAdditionalDisplayAdapters() {
782 synchronized (mSyncRoot) {
783 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700784 registerOverlayDisplayAdapterLocked();
785 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700786 }
787 }
788 }
789
Jeff Brown89d55462012-09-19 11:33:42 -0700790 private void registerOverlayDisplayAdapterLocked() {
791 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
792 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
793 }
794
795 private void registerWifiDisplayAdapterLocked() {
796 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700797 com.android.internal.R.bool.config_enableWifiDisplay)
798 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700799 mWifiDisplayAdapter = new WifiDisplayAdapter(
800 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
801 mPersistentDataStore);
802 registerDisplayAdapterLocked(mWifiDisplayAdapter);
803 }
804 }
805
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700806 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
807 // In safe mode, we disable non-essential display adapters to give the user
808 // an opportunity to fix broken settings or other problems that might affect
809 // system stability.
810 // In only-core mode, we disable non-essential display adapters to minimize
811 // the number of dependencies that are started while in this mode and to
812 // prevent problems that might occur due to the device being encrypted.
813 return !mSafeMode && !mOnlyCore;
814 }
815
816 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
817 mDisplayAdapters.add(adapter);
818 adapter.registerLocked();
819 }
820
Jeff Brownbd6e1502012-08-28 03:27:37 -0700821 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700822 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700823 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700824 }
825 }
826
Jeff Browna506a6e2013-06-04 00:02:38 -0700827 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700828 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700829 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700830 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700831 return;
832 }
833
Jeff Brown10acf6d2015-04-14 14:20:47 -0700834 Slog.i(TAG, "Display device added: " + info);
835 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700836
837 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700838 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700839 Runnable work = updateDisplayStateLocked(device);
840 if (work != null) {
841 work.run();
842 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700843 scheduleTraversalLocked(false);
844 }
845
Jeff Brownbd6e1502012-08-28 03:27:37 -0700846 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700847 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700848 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700849 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700850 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700851 return;
852 }
853
Jeff Brown10acf6d2015-04-14 14:20:47 -0700854 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
855 if (diff == DisplayDeviceInfo.DIFF_STATE) {
856 Slog.i(TAG, "Display device changed state: \"" + info.name
857 + "\", " + Display.stateToString(info.state));
858 } else if (diff != 0) {
859 Slog.i(TAG, "Display device changed: " + info);
860 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700861 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
862 try {
863 mPersistentDataStore.setColorMode(device, info.colorMode);
864 } finally {
865 mPersistentDataStore.saveIfNeeded();
866 }
867 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700868 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700869
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700870 device.applyPendingDisplayDeviceInfoChangesLocked();
871 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700872 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700873 }
874 }
875 }
876
Jeff Brownbd6e1502012-08-28 03:27:37 -0700877 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700878 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700879 handleDisplayDeviceRemovedLocked(device);
880 }
881 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700882
Jeff Browna506a6e2013-06-04 00:02:38 -0700883 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700884 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700885 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700886 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700887 return;
888 }
889
Jeff Brown10acf6d2015-04-14 14:20:47 -0700890 Slog.i(TAG, "Display device removed: " + info);
891 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700892
Jeff Browna506a6e2013-06-04 00:02:38 -0700893 updateLogicalDisplaysLocked();
894 scheduleTraversalLocked(false);
895 }
896
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700897 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700898 final int count = mDisplayDevices.size();
899 for (int i = 0; i < count; i++) {
900 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700901 Runnable runnable = updateDisplayStateLocked(device);
902 if (runnable != null) {
903 workQueue.add(runnable);
904 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700905 }
906 }
907
Jeff Browne75926d2014-09-18 15:24:49 -0700908 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700909 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700910 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700911 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
912 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700913 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700914 }
Jeff Browne75926d2014-09-18 15:24:49 -0700915 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700916 }
917
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700918 // Adds a new logical display based on the given display device.
919 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700920 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
922 boolean isDefault = (deviceInfo.flags
923 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
924 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
925 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
926 isDefault = false;
927 }
928
Jeff Brown27f1d672012-10-17 18:32:34 -0700929 if (!isDefault && mSingleDisplayDemoMode) {
930 Slog.i(TAG, "Not creating a logical display for a secondary display "
931 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700932 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700933 }
934
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700935 final int displayId = assignDisplayIdLocked(isDefault);
936 final int layerStack = assignLayerStackLocked(displayId);
937
Jeff Brownd728bf52012-09-08 18:05:28 -0700938 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700939 display.updateLocked(mDisplayDevices);
940 if (!display.isValidLocked()) {
941 // This should never happen currently.
942 Slog.w(TAG, "Ignoring display device because the logical display "
943 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700944 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700945 }
946
Michael Wrighteedcbf12017-08-16 23:14:54 +0100947 configureColorModeLocked(display, device);
948 if (isDefault) {
949 recordStableDisplayStatsIfNeededLocked(display);
950 }
951
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700952 mLogicalDisplays.put(displayId, display);
953
954 // Wake up waitForDefaultDisplay.
955 if (isDefault) {
956 mSyncRoot.notifyAll();
957 }
958
959 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700960 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700961 }
962
963 private int assignDisplayIdLocked(boolean isDefault) {
964 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
965 }
966
967 private int assignLayerStackLocked(int displayId) {
968 // Currently layer stacks and display ids are the same.
969 // This need not be the case.
970 return displayId;
971 }
972
Michael Wrighteedcbf12017-08-16 23:14:54 +0100973 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
974 if (display.getPrimaryDisplayDeviceLocked() == device) {
975 int colorMode = mPersistentDataStore.getColorMode(device);
976 if (colorMode == Display.COLOR_MODE_INVALID) {
977 if ((device.getDisplayDeviceInfoLocked().flags
978 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
979 colorMode = mDefaultDisplayDefaultColorMode;
980 } else {
981 colorMode = Display.COLOR_MODE_DEFAULT;
982 }
983 }
984 display.setRequestedColorModeLocked(colorMode);
985 }
986 }
987
988 // If we've never recorded stable device stats for this device before and they aren't
989 // explicitly configured, go ahead and record the stable device stats now based on the status
990 // of the default display at first boot.
991 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
992 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
993 DisplayInfo info = d.getDisplayInfoLocked();
994 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
995 }
996 }
997
998 private void setStableDisplaySizeLocked(int width, int height) {
999 mStableDisplaySize = new Point(width, height);
1000 try {
1001 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1002 } finally {
1003 mPersistentDataStore.saveIfNeeded();
1004 }
1005 }
1006
Michael Wrighteef0e132017-11-21 17:57:52 +00001007 private void setBrightnessConfigurationForUserInternal(
1008 @NonNull BrightnessConfiguration c, @UserIdInt int userId) {
1009 final int userSerial = getUserManager().getUserSerialNumber(userId);
1010 synchronized (mSyncRoot) {
1011 try {
1012 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial);
1013 } finally {
1014 mPersistentDataStore.saveIfNeeded();
1015 }
1016 if (userId == mCurrentUserId) {
1017 mDisplayPowerController.setBrightnessConfiguration(c);
1018 }
1019 }
1020 }
1021
1022 private void loadBrightnessConfiguration() {
1023 synchronized (mSyncRoot) {
1024 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1025 BrightnessConfiguration config =
1026 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1027 mDisplayPowerController.setBrightnessConfiguration(config);
1028 }
1029 }
1030
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001031 // Updates all existing logical displays given the current set of display devices.
1032 // Removes invalid logical displays.
1033 // Sends notifications if needed.
1034 private boolean updateLogicalDisplaysLocked() {
1035 boolean changed = false;
1036 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1037 final int displayId = mLogicalDisplays.keyAt(i);
1038 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1039
1040 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1041 display.updateLocked(mDisplayDevices);
1042 if (!display.isValidLocked()) {
1043 mLogicalDisplays.removeAt(i);
1044 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1045 changed = true;
1046 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1047 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1048 changed = true;
1049 }
1050 }
1051 return changed;
1052 }
1053
1054 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -07001055 // Clear all viewports before configuring displays so that we can keep
1056 // track of which ones we have configured.
1057 clearViewportsLocked();
1058
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001059 // Configure each display device.
1060 final int count = mDisplayDevices.size();
1061 for (int i = 0; i < count; i++) {
1062 DisplayDevice device = mDisplayDevices.get(i);
1063 configureDisplayInTransactionLocked(device);
1064 device.performTraversalInTransactionLocked();
1065 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001066
1067 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001068 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001069 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1070 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001071 }
1072
Michael Wright3f145a22014-07-22 19:46:03 -07001073 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001074 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001075 synchronized (mSyncRoot) {
1076 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001077 if (display == null) {
1078 return;
1079 }
1080 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001081 if (DEBUG) {
1082 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1083 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1084 }
1085
Craig Mautner722285e2012-09-07 13:55:58 -07001086 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001087 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001088 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001089 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1090 // Scan supported modes returned by display.getInfo() to find a mode with the same
1091 // size as the default display mode but with the specified refresh rate instead.
1092 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1093 requestedRefreshRate);
1094 }
1095 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001096 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001097 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001098 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001099 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001100 scheduleTraversalLocked(inTraversal);
1101 }
Craig Mautner722285e2012-09-07 13:55:58 -07001102 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001103 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001104
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001105 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1106 synchronized (mSyncRoot) {
1107 LogicalDisplay display = mLogicalDisplays.get(displayId);
1108 if (display == null) {
1109 return;
1110 }
1111 if (display.getDisplayOffsetXLocked() != x
1112 || display.getDisplayOffsetYLocked() != y) {
1113 if (DEBUG) {
1114 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1115 + x + ", " + y + ")");
1116 }
1117 display.setDisplayOffsetsLocked(x, y);
1118 scheduleTraversalLocked(false);
1119 }
1120 }
1121 }
1122
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001123 // Updates the lists of UIDs that are present on displays.
1124 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1125 synchronized (mSyncRoot) {
1126 mDisplayAccessUIDs.clear();
1127 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1128 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1129 newDisplayAccessUIDs.valueAt(i));
1130 }
1131 }
1132 }
1133
1134 // Checks if provided UID's content is present on the display and UID has access to it.
1135 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1136 synchronized (mSyncRoot) {
1137 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1138 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1139 }
1140 }
1141
Jeff Brownd728bf52012-09-08 18:05:28 -07001142 private void clearViewportsLocked() {
1143 mDefaultViewport.valid = false;
1144 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001145 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001146 }
1147
1148 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001149 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1150 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001151
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001152 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001153 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001154 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001155 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001156 if (display != null && !display.hasContentLocked()) {
1157 // If the display does not have any content of its own, then
1158 // automatically mirror the default logical display contents.
1159 display = null;
1160 }
1161 if (display == null) {
1162 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1163 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001164 }
1165
1166 // Apply the logical display configuration to the display device.
1167 if (display == null) {
1168 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001169 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001170 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001171 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001172 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001173 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001174
1175 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001176 if (!mDefaultViewport.valid
1177 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1178 setViewportLocked(mDefaultViewport, display, device);
1179 }
1180 if (!mExternalTouchViewport.valid
1181 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1182 setViewportLocked(mExternalTouchViewport, display, device);
1183 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001184
1185 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1186 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1187 setViewportLocked(viewport, display, device);
1188 }
1189 }
1190
1191 /** Gets the virtual device viewport or creates it if not yet created. */
1192 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1193 DisplayViewport viewport;
1194 final int count = mVirtualTouchViewports.size();
1195 for (int i = 0; i < count; i++) {
1196 viewport = mVirtualTouchViewports.get(i);
1197 if (uniqueId.equals(viewport.uniqueId)) {
1198 return viewport;
1199 }
1200 }
1201
1202 viewport = new DisplayViewport();
1203 viewport.uniqueId = uniqueId;
1204 mVirtualTouchViewports.add(viewport);
1205 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001206 }
1207
1208 private static void setViewportLocked(DisplayViewport viewport,
1209 LogicalDisplay display, DisplayDevice device) {
1210 viewport.valid = true;
1211 viewport.displayId = display.getDisplayIdLocked();
1212 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001213 }
1214
1215 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1216 final int count = mLogicalDisplays.size();
1217 for (int i = 0; i < count; i++) {
1218 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1219 if (display.getPrimaryDisplayDeviceLocked() == device) {
1220 return display;
1221 }
1222 }
1223 return null;
1224 }
1225
Jeff Brownbd6e1502012-08-28 03:27:37 -07001226 private void sendDisplayEventLocked(int displayId, int event) {
1227 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1228 mHandler.sendMessage(msg);
1229 }
1230
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001231 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1232 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001233 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001234 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001235 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001236 if (!inTraversal) {
1237 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1238 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001239 }
1240 }
1241
1242 // Runs on Handler thread.
1243 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001244 private void deliverDisplayEvent(int displayId, int event) {
1245 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001246 Slog.d(TAG, "Delivering display event: displayId="
1247 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001248 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001249
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001250 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001251 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001252 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001253 count = mCallbacks.size();
1254 mTempCallbacks.clear();
1255 for (int i = 0; i < count; i++) {
1256 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001257 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001258 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001259
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001260 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001261 for (int i = 0; i < count; i++) {
1262 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1263 }
1264 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001265 }
1266
Michael Wrightc39d47a2014-07-08 18:07:36 -07001267 private IMediaProjectionManager getProjectionService() {
1268 if (mProjectionService == null) {
1269 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1270 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1271 }
1272 return mProjectionService;
1273 }
1274
Michael Wrighteef0e132017-11-21 17:57:52 +00001275 private UserManager getUserManager() {
1276 return mContext.getSystemService(UserManager.class);
1277 }
1278
Jeff Brown4ccb8232014-01-16 22:16:42 -08001279 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001280 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001281
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001282 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001283 pw.println(" mOnlyCode=" + mOnlyCore);
1284 pw.println(" mSafeMode=" + mSafeMode);
1285 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001286 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001287 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1288 pw.println(" mDefaultViewport=" + mDefaultViewport);
1289 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001290 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001291 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001292 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001293 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001294 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1295
Jeff Brown9e316a12012-10-08 19:17:06 -07001296
Jeff Brownbd6e1502012-08-28 03:27:37 -07001297 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001298 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001299
1300 pw.println();
1301 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001302 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001303 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001304 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001305 }
Craig Mautner9de49362012-08-02 14:30:30 -07001306
Jeff Brownbd6e1502012-08-28 03:27:37 -07001307 pw.println();
1308 pw.println("Display Devices: size=" + mDisplayDevices.size());
1309 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001310 pw.println(" " + device.getDisplayDeviceInfoLocked());
1311 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001312 }
1313
1314 final int logicalDisplayCount = mLogicalDisplays.size();
1315 pw.println();
1316 pw.println("Logical Displays: size=" + logicalDisplayCount);
1317 for (int i = 0; i < logicalDisplayCount; i++) {
1318 int displayId = mLogicalDisplays.keyAt(i);
1319 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1320 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001321 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001322 }
Jeff Brownce468a32013-11-21 16:42:03 -08001323
1324 final int callbackCount = mCallbacks.size();
1325 pw.println();
1326 pw.println("Callbacks: size=" + callbackCount);
1327 for (int i = 0; i < callbackCount; i++) {
1328 CallbackRecord callback = mCallbacks.valueAt(i);
1329 pw.println(" " + i + ": mPid=" + callback.mPid
1330 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1331 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001332
1333 if (mDisplayPowerController != null) {
1334 mDisplayPowerController.dump(pw);
1335 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001336
1337 pw.println();
1338 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001339 }
1340 }
1341
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001342 /**
1343 * This is the object that everything in the display manager locks on.
1344 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1345 * clear that the object belongs to the display manager service and that it is
1346 * a unique object with a special purpose.
1347 */
1348 public static final class SyncRoot {
1349 }
1350
Santos Cordonee8931e2017-04-05 10:31:15 -07001351 @VisibleForTesting
1352 static class Injector {
1353 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1354 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1355 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1356 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001357
1358 long getDefaultDisplayDelayTimeout() {
1359 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1360 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001361 }
1362
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001363 @VisibleForTesting
1364 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1365 synchronized (mSyncRoot) {
1366 LogicalDisplay display = mLogicalDisplays.get(displayId);
1367 if (display != null) {
1368 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1369 return displayDevice.getDisplayDeviceInfoLocked();
1370 }
1371 return null;
1372 }
1373 }
1374
Jeff Brownbd6e1502012-08-28 03:27:37 -07001375 private final class DisplayManagerHandler extends Handler {
1376 public DisplayManagerHandler(Looper looper) {
1377 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001378 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001379
Jeff Brownbd6e1502012-08-28 03:27:37 -07001380 @Override
1381 public void handleMessage(Message msg) {
1382 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001383 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1384 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001385 break;
1386
1387 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1388 registerAdditionalDisplayAdapters();
1389 break;
1390
1391 case MSG_DELIVER_DISPLAY_EVENT:
1392 deliverDisplayEvent(msg.arg1, msg.arg2);
1393 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001394
1395 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001396 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001397 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001398
1399 case MSG_UPDATE_VIEWPORT: {
1400 synchronized (mSyncRoot) {
1401 mTempDefaultViewport.copyFrom(mDefaultViewport);
1402 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001403 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1404 mTempVirtualTouchViewports.clear();
1405 for (DisplayViewport d : mVirtualTouchViewports) {
1406 mTempVirtualTouchViewports.add(d.makeCopy());
1407 }
1408 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001409 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001410 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1411 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001412 break;
1413 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001414
Michael Wrighteef0e132017-11-21 17:57:52 +00001415 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1416 loadBrightnessConfiguration();
1417 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001418 }
1419 }
1420 }
1421
1422 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1423 @Override
1424 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1425 switch (event) {
1426 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1427 handleDisplayDeviceAdded(device);
1428 break;
1429
1430 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1431 handleDisplayDeviceChanged(device);
1432 break;
1433
1434 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1435 handleDisplayDeviceRemoved(device);
1436 break;
1437 }
1438 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001439
1440 @Override
1441 public void onTraversalRequested() {
1442 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001443 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001444 }
1445 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001446 }
1447
1448 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001449 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001450 private final IDisplayManagerCallback mCallback;
1451
Jeff Brownce468a32013-11-21 16:42:03 -08001452 public boolean mWifiDisplayScanRequested;
1453
Jeff Brownbd6e1502012-08-28 03:27:37 -07001454 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1455 mPid = pid;
1456 mCallback = callback;
1457 }
1458
1459 @Override
1460 public void binderDied() {
1461 if (DEBUG) {
1462 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1463 }
Jeff Brownce468a32013-11-21 16:42:03 -08001464 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001465 }
1466
1467 public void notifyDisplayEventAsync(int displayId, int event) {
1468 try {
1469 mCallback.onDisplayEvent(displayId, event);
1470 } catch (RemoteException ex) {
1471 Slog.w(TAG, "Failed to notify process "
1472 + mPid + " that displays changed, assuming it died.", ex);
1473 binderDied();
1474 }
1475 }
1476 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001477
Santos Cordonee8931e2017-04-05 10:31:15 -07001478 @VisibleForTesting
1479 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001480 /**
1481 * Returns information about the specified logical display.
1482 *
1483 * @param displayId The logical display id.
1484 * @return The logical display info, or null if the display does not exist. The
1485 * returned object must be treated as immutable.
1486 */
1487 @Override // Binder call
1488 public DisplayInfo getDisplayInfo(int displayId) {
1489 final int callingUid = Binder.getCallingUid();
1490 final long token = Binder.clearCallingIdentity();
1491 try {
1492 return getDisplayInfoInternal(displayId, callingUid);
1493 } finally {
1494 Binder.restoreCallingIdentity(token);
1495 }
1496 }
1497
1498 /**
1499 * Returns the list of all display ids.
1500 */
1501 @Override // Binder call
1502 public int[] getDisplayIds() {
1503 final int callingUid = Binder.getCallingUid();
1504 final long token = Binder.clearCallingIdentity();
1505 try {
1506 return getDisplayIdsInternal(callingUid);
1507 } finally {
1508 Binder.restoreCallingIdentity(token);
1509 }
1510 }
1511
Michael Wrighteedcbf12017-08-16 23:14:54 +01001512 /**
1513 * Returns the stable device display size, in pixels.
1514 */
1515 @Override // Binder call
1516 public Point getStableDisplaySize() {
1517 final long token = Binder.clearCallingIdentity();
1518 try {
1519 return getStableDisplaySizeInternal();
1520 } finally {
1521 Binder.restoreCallingIdentity(token);
1522 }
1523 }
1524
Jeff Brown4ccb8232014-01-16 22:16:42 -08001525 @Override // Binder call
1526 public void registerCallback(IDisplayManagerCallback callback) {
1527 if (callback == null) {
1528 throw new IllegalArgumentException("listener must not be null");
1529 }
1530
1531 final int callingPid = Binder.getCallingPid();
1532 final long token = Binder.clearCallingIdentity();
1533 try {
1534 registerCallbackInternal(callback, callingPid);
1535 } finally {
1536 Binder.restoreCallingIdentity(token);
1537 }
1538 }
1539
1540 @Override // Binder call
1541 public void startWifiDisplayScan() {
1542 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1543 "Permission required to start wifi display scans");
1544
1545 final int callingPid = Binder.getCallingPid();
1546 final long token = Binder.clearCallingIdentity();
1547 try {
1548 startWifiDisplayScanInternal(callingPid);
1549 } finally {
1550 Binder.restoreCallingIdentity(token);
1551 }
1552 }
1553
1554 @Override // Binder call
1555 public void stopWifiDisplayScan() {
1556 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1557 "Permission required to stop wifi display scans");
1558
1559 final int callingPid = Binder.getCallingPid();
1560 final long token = Binder.clearCallingIdentity();
1561 try {
1562 stopWifiDisplayScanInternal(callingPid);
1563 } finally {
1564 Binder.restoreCallingIdentity(token);
1565 }
1566 }
1567
1568 @Override // Binder call
1569 public void connectWifiDisplay(String address) {
1570 if (address == null) {
1571 throw new IllegalArgumentException("address must not be null");
1572 }
1573 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1574 "Permission required to connect to a wifi display");
1575
1576 final long token = Binder.clearCallingIdentity();
1577 try {
1578 connectWifiDisplayInternal(address);
1579 } finally {
1580 Binder.restoreCallingIdentity(token);
1581 }
1582 }
1583
1584 @Override // Binder call
1585 public void disconnectWifiDisplay() {
1586 // This request does not require special permissions.
1587 // Any app can request disconnection from the currently active wifi display.
1588 // This exception should no longer be needed once wifi display control moves
1589 // to the media router service.
1590
1591 final long token = Binder.clearCallingIdentity();
1592 try {
1593 disconnectWifiDisplayInternal();
1594 } finally {
1595 Binder.restoreCallingIdentity(token);
1596 }
1597 }
1598
1599 @Override // Binder call
1600 public void renameWifiDisplay(String address, String alias) {
1601 if (address == null) {
1602 throw new IllegalArgumentException("address must not be null");
1603 }
1604 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1605 "Permission required to rename to a wifi display");
1606
1607 final long token = Binder.clearCallingIdentity();
1608 try {
1609 renameWifiDisplayInternal(address, alias);
1610 } finally {
1611 Binder.restoreCallingIdentity(token);
1612 }
1613 }
1614
1615 @Override // Binder call
1616 public void forgetWifiDisplay(String address) {
1617 if (address == null) {
1618 throw new IllegalArgumentException("address must not be null");
1619 }
1620 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1621 "Permission required to forget to a wifi display");
1622
1623 final long token = Binder.clearCallingIdentity();
1624 try {
1625 forgetWifiDisplayInternal(address);
1626 } finally {
1627 Binder.restoreCallingIdentity(token);
1628 }
1629 }
1630
1631 @Override // Binder call
1632 public void pauseWifiDisplay() {
1633 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1634 "Permission required to pause a wifi display session");
1635
1636 final long token = Binder.clearCallingIdentity();
1637 try {
1638 pauseWifiDisplayInternal();
1639 } finally {
1640 Binder.restoreCallingIdentity(token);
1641 }
1642 }
1643
1644 @Override // Binder call
1645 public void resumeWifiDisplay() {
1646 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1647 "Permission required to resume a wifi display session");
1648
1649 final long token = Binder.clearCallingIdentity();
1650 try {
1651 resumeWifiDisplayInternal();
1652 } finally {
1653 Binder.restoreCallingIdentity(token);
1654 }
1655 }
1656
1657 @Override // Binder call
1658 public WifiDisplayStatus getWifiDisplayStatus() {
1659 // This request does not require special permissions.
1660 // Any app can get information about available wifi displays.
1661
1662 final long token = Binder.clearCallingIdentity();
1663 try {
1664 return getWifiDisplayStatusInternal();
1665 } finally {
1666 Binder.restoreCallingIdentity(token);
1667 }
1668 }
1669
1670 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001671 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001672 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001673 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1674 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001675 final long token = Binder.clearCallingIdentity();
1676 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001677 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001678 } finally {
1679 Binder.restoreCallingIdentity(token);
1680 }
1681 }
1682
1683 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001684 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001685 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001686 int width, int height, int densityDpi, Surface surface, int flags,
1687 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001688 final int callingUid = Binder.getCallingUid();
1689 if (!validatePackageName(callingUid, packageName)) {
1690 throw new SecurityException("packageName must match the calling uid");
1691 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001692 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001693 throw new IllegalArgumentException("appToken must not be null");
1694 }
1695 if (TextUtils.isEmpty(name)) {
1696 throw new IllegalArgumentException("name must be non-null and non-empty");
1697 }
1698 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1699 throw new IllegalArgumentException("width, height, and densityDpi must be "
1700 + "greater than 0");
1701 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001702 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001703 throw new IllegalArgumentException("Surface can't be single-buffered");
1704 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001705
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001706 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1707 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1708
1709 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001710 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001711 throw new IllegalArgumentException(
1712 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1713 }
Michael Wright6720be42014-07-29 19:14:16 -07001714 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001715 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1716 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001717 }
1718
Michael Wrightc39d47a2014-07-08 18:07:36 -07001719 if (projection != null) {
1720 try {
1721 if (!getProjectionService().isValidMediaProjection(projection)) {
1722 throw new SecurityException("Invalid media projection");
1723 }
Michael Wright6720be42014-07-29 19:14:16 -07001724 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001725 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001726 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001727 }
1728 }
1729
Michael Wright6720be42014-07-29 19:14:16 -07001730 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001731 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001732 if (!canProjectVideo(projection)) {
1733 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1734 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1735 + "MediaProjection token in order to create a screen sharing virtual "
1736 + "display.");
1737 }
1738 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001739 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001740 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001741 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001742 + "or an appropriate MediaProjection token to create a "
1743 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001744 }
1745 }
1746
1747 final long token = Binder.clearCallingIdentity();
1748 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001749 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1750 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001751 } finally {
1752 Binder.restoreCallingIdentity(token);
1753 }
1754 }
1755
1756 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001757 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001758 int width, int height, int densityDpi) {
1759 final long token = Binder.clearCallingIdentity();
1760 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001761 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001762 } finally {
1763 Binder.restoreCallingIdentity(token);
1764 }
1765 }
1766
1767 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001768 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001769 if (surface != null && surface.isSingleBuffered()) {
1770 throw new IllegalArgumentException("Surface can't be single-buffered");
1771 }
Jeff Brown92207df2014-04-16 13:16:07 -07001772 final long token = Binder.clearCallingIdentity();
1773 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001774 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001775 } finally {
1776 Binder.restoreCallingIdentity(token);
1777 }
1778 }
1779
1780 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001781 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001782 final long token = Binder.clearCallingIdentity();
1783 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001784 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001785 } finally {
1786 Binder.restoreCallingIdentity(token);
1787 }
1788 }
1789
1790 @Override // Binder call
1791 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001792 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001793
1794 final long token = Binder.clearCallingIdentity();
1795 try {
1796 dumpInternal(pw);
1797 } finally {
1798 Binder.restoreCallingIdentity(token);
1799 }
1800 }
1801
Kenny Guy22bd0442017-10-26 00:15:54 +01001802 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001803 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001804 mContext.enforceCallingOrSelfPermission(
1805 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1806 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001807
1808 final int callingUid = Binder.getCallingUid();
1809 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
1810 final int mode = appOpsManager.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
1811 callingUid, callingPackage);
1812 final boolean hasUsageStats;
1813 if (mode == AppOpsManager.MODE_DEFAULT) {
1814 // The default behavior here is to check if PackageManager has given the app
1815 // permission.
1816 hasUsageStats = mContext.checkCallingPermission(
1817 Manifest.permission.PACKAGE_USAGE_STATS)
1818 == PackageManager.PERMISSION_GRANTED;
1819 } else {
1820 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1821 }
1822
1823 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001824 final long token = Binder.clearCallingIdentity();
1825 try {
Michael Wright144aac92017-12-21 18:37:41 +00001826 synchronized (mSyncRoot) {
1827 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
1828 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001829 } finally {
1830 Binder.restoreCallingIdentity(token);
1831 }
1832 }
1833
Michael Wrighteef0e132017-11-21 17:57:52 +00001834 @Override // Binder call
1835 public void setBrightnessConfigurationForUser(
1836 BrightnessConfiguration c, @UserIdInt int userId) {
1837 mContext.enforceCallingOrSelfPermission(
1838 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1839 "Permission required to change the display's brightness configuration");
1840 if (userId != UserHandle.getCallingUserId()) {
1841 mContext.enforceCallingOrSelfPermission(
1842 Manifest.permission.INTERACT_ACROSS_USERS,
1843 "Permission required to change the display brightness"
1844 + " configuration of another user");
1845 }
1846 Preconditions.checkNotNull(c);
1847 final long token = Binder.clearCallingIdentity();
1848 try {
1849 setBrightnessConfigurationForUserInternal(c, userId);
1850 } finally {
1851 Binder.restoreCallingIdentity(token);
1852 }
1853 }
1854
Jeff Brown4ccb8232014-01-16 22:16:42 -08001855 private boolean validatePackageName(int uid, String packageName) {
1856 if (packageName != null) {
1857 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1858 if (packageNames != null) {
1859 for (String n : packageNames) {
1860 if (n.equals(packageName)) {
1861 return true;
1862 }
1863 }
1864 }
1865 }
1866 return false;
1867 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001868
1869 private boolean canProjectVideo(IMediaProjection projection) {
1870 if (projection != null) {
1871 try {
1872 if (projection.canProjectVideo()) {
1873 return true;
1874 }
1875 } catch (RemoteException e) {
1876 Slog.e(TAG, "Unable to query projection service for permissions", e);
1877 }
1878 }
1879 if (mContext.checkCallingPermission(
1880 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001881 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001882 return true;
1883 }
1884 return canProjectSecureVideo(projection);
1885 }
1886
1887 private boolean canProjectSecureVideo(IMediaProjection projection) {
1888 if (projection != null) {
1889 try {
1890 if (projection.canProjectSecureVideo()){
1891 return true;
1892 }
1893 } catch (RemoteException e) {
1894 Slog.e(TAG, "Unable to query projection service for permissions", e);
1895 }
1896 }
1897 return mContext.checkCallingPermission(
1898 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001899 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001900 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001901 }
1902
1903 private final class LocalService extends DisplayManagerInternal {
1904 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001905 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001906 SensorManager sensorManager) {
1907 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001908 DisplayBlanker blanker = new DisplayBlanker() {
1909 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001910 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001911 // The order of operations is important for legacy reasons.
1912 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001913 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001914 }
1915
1916 callbacks.onDisplayStateChange(state);
1917
1918 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001919 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001920 }
1921 }
1922 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001923 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001924 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001925 }
Michael Wrighteef0e132017-11-21 17:57:52 +00001926
1927 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07001928 }
1929
1930 @Override
1931 public boolean requestPowerState(DisplayPowerRequest request,
1932 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001933 synchronized (mSyncRoot) {
1934 return mDisplayPowerController.requestPowerState(request,
1935 waitForNegativeProximity);
1936 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001937 }
1938
1939 @Override
1940 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00001941 synchronized (mSyncRoot) {
1942 return mDisplayPowerController.isProximitySensorAvailable();
1943 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001944 }
1945
1946 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001947 public DisplayInfo getDisplayInfo(int displayId) {
1948 return getDisplayInfoInternal(displayId, Process.myUid());
1949 }
1950
1951 @Override
1952 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1953 if (listener == null) {
1954 throw new IllegalArgumentException("listener must not be null");
1955 }
1956
1957 registerDisplayTransactionListenerInternal(listener);
1958 }
1959
1960 @Override
1961 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1962 if (listener == null) {
1963 throw new IllegalArgumentException("listener must not be null");
1964 }
1965
1966 unregisterDisplayTransactionListenerInternal(listener);
1967 }
1968
1969 @Override
1970 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1971 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1972 }
1973
1974 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001975 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1976 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1977 }
1978
1979 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001980 public void performTraversalInTransactionFromWindowManager() {
1981 performTraversalInTransactionFromWindowManagerInternal();
1982 }
1983
1984 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001985 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001986 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1987 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1988 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001989 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001990
1991 @Override
1992 public void setDisplayOffsets(int displayId, int x, int y) {
1993 setDisplayOffsetsInternal(displayId, x, y);
1994 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001995
1996 @Override
1997 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1998 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1999 }
2000
2001 @Override
2002 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2003 return isUidPresentOnDisplayInternal(uid, displayId);
2004 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002005
2006 @Override
2007 public void persistBrightnessSliderEvents() {
Michael Wright144aac92017-12-21 18:37:41 +00002008 synchronized (mSyncRoot) {
2009 mDisplayPowerController.persistBrightnessSliderEvents();
2010 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002011 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002012 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002013}