blob: f1e20116d4ed240510e6dba1669ca302b47570ef [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;
Jeff Brownfa25bf52012-07-23 19:26:30 -070032import android.content.Context;
33import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010034import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010035import android.content.res.Resources;
36import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070037import android.hardware.SensorManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010038import android.hardware.display.BrightnessChangeEvent;
Jeff Brownbd6e1502012-08-28 03:27:37 -070039import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080040import android.hardware.display.DisplayManagerInternal;
41import android.hardware.display.DisplayViewport;
42import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070043import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070044import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070045import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070046import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080047import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070048import android.media.projection.IMediaProjection;
49import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070050import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070052import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080053import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070054import android.os.Looper;
55import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070056import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080057import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070058import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070059import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070060import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070061import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070062import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010063import android.os.UserHandle;
Jeff Browna506a6e2013-06-04 00:02:38 -070064import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080065import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070066import android.util.Slog;
67import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070068import android.view.Display;
69import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070070import android.view.Surface;
Jeff Brown4ccb8232014-01-16 22:16:42 -080071import android.view.WindowManagerInternal;
Jeff Browna506a6e2013-06-04 00:02:38 -070072
Jorim Jaggied7993b2017-03-28 18:50:01 +010073import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080074import com.android.server.DisplayThread;
75import com.android.server.LocalServices;
76import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070077import com.android.server.UiThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070078
79import java.io.FileDescriptor;
80import java.io.PrintWriter;
81import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070082import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070083import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080084import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070085
86/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070087 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070088 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070089 * The {@link DisplayManagerService} manages the global lifecycle of displays,
90 * decides how to configure logical displays based on the physical display devices currently
91 * attached, sends notifications to the system and to applications when the state
92 * changes, and so on.
93 * </p><p>
94 * The display manager service relies on a collection of {@link DisplayAdapter} components,
95 * for discovering and configuring physical display devices attached to the system.
96 * There are separate display adapters for each manner that devices are attached:
97 * one display adapter for built-in local displays, one for simulated non-functional
98 * displays when the system is headless, one for simulated overlay displays used for
99 * development, one for wifi displays, etc.
100 * </p><p>
101 * Display adapters are only weakly coupled to the display manager service.
102 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700103 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700104 * by the display manager service. This separation of concerns is important for
105 * two main reasons. First, it neatly encapsulates the responsibilities of these
106 * two classes: display adapters handle individual display devices whereas
107 * the display manager service handles the global state. Second, it eliminates
108 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700109 * </p>
110 *
111 * <h3>Synchronization</h3>
112 * <p>
113 * Because the display manager may be accessed by multiple threads, the synchronization
114 * story gets a little complicated. In particular, the window manager may call into
115 * the display manager while holding a surface transaction with the expectation that
116 * it can apply changes immediately. Unfortunately, that means we can't just do
117 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700118 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700119 * To make this work, all of the objects that belong to the display manager must
120 * use the same lock. We call this lock the synchronization root and it has a unique
121 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
122 * named with the "Locked" suffix.
123 * </p><p>
124 * Where things get tricky is that the display manager is not allowed to make
125 * any potentially reentrant calls, especially into the window manager. We generally
126 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700127 * </p>
128 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800129public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700130 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700131 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700132
Jeff Brownbbd28a22012-09-20 16:47:15 -0700133 // When this system property is set to 0, WFD is forcibly disabled on boot.
134 // When this system property is set to 1, WFD is forcibly enabled on boot.
135 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
136 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
137
Jeff Brownbd6e1502012-08-28 03:27:37 -0700138 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
139
Santos Cordonc22c5632017-06-21 16:03:49 -0700140 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700141 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
142 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700144 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100145 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700146
Jeff Brownb880d882014-02-10 19:47:07 -0800147 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700148 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700149 private final Handler mUiHandler;
150 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800151 private WindowManagerInternal mWindowManagerInternal;
152 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700153 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700154
155 // The synchronization root for the display manager.
156 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800157 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
158 // into WindowManagerService methods that require mWindowMap while holding this unless you are
159 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700160 private final SyncRoot mSyncRoot = new SyncRoot();
161
162 // True if in safe mode.
163 // This option may disable certain display adapters.
164 public boolean mSafeMode;
165
166 // True if we are in a special boot mode where only core applications and
167 // services should be started. This option may disable certain display adapters.
168 public boolean mOnlyCore;
169
Jeff Brown27f1d672012-10-17 18:32:34 -0700170 // True if the display manager service should pretend there is only one display
171 // and only tell applications about the existence of the default logical display.
172 // The display manager can still mirror content to secondary displays but applications
173 // cannot present unique content on those displays.
174 // Used for demonstration purposes only.
175 private final boolean mSingleDisplayDemoMode;
176
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700177 // All callback records indexed by calling process id.
178 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700179 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700180
Jeff Brownbd6e1502012-08-28 03:27:37 -0700181 // List of all currently registered display adapters.
182 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
183
184 // List of all currently connected display devices.
185 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
186
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700187 // List of all logical displays indexed by logical display id.
188 private final SparseArray<LogicalDisplay> mLogicalDisplays =
189 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700190 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
191
Jeff Brown7f3994e2012-12-04 14:04:28 -0800192 // List of all display transaction listeners.
193 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
194 new CopyOnWriteArrayList<DisplayTransactionListener>();
195
Jeff Brownad9ef192014-04-08 17:26:30 -0700196 // Display power controller.
197 private DisplayPowerController mDisplayPowerController;
198
Jeff Brown037c33e2014-04-09 00:31:55 -0700199 // The overall display state, independent of changes that might influence one
200 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700201 private int mGlobalDisplayState = Display.STATE_ON;
202
203 // The overall display brightness.
204 // For now, this only applies to the built-in display but we may split it up eventually.
205 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700206
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700207 // Set to true when there are pending display changes that have yet to be applied
208 // to the surface flinger state.
209 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700210
Jeff Browne08ae382012-09-07 20:36:36 -0700211 // The Wifi display adapter, or null if not registered.
212 private WifiDisplayAdapter mWifiDisplayAdapter;
213
Jeff Brownce468a32013-11-21 16:42:03 -0800214 // The number of active wifi display scan requests.
215 private int mWifiDisplayScanRequestCount;
216
Jeff Browna506a6e2013-06-04 00:02:38 -0700217 // The virtual display adapter, or null if not registered.
218 private VirtualDisplayAdapter mVirtualDisplayAdapter;
219
Michael Wrighteedcbf12017-08-16 23:14:54 +0100220 // The stable device screen height and width. These are not tied to a specific display, even
221 // the default display, because they need to be stable over the course of the device's entire
222 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
223 // device).
224 private Point mStableDisplaySize = new Point();
225
Jeff Brownd728bf52012-09-08 18:05:28 -0700226 // Viewports of the default display and the display that should receive touch
227 // input from an external source. Used by the input system.
228 private final DisplayViewport mDefaultViewport = new DisplayViewport();
229 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700230 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700231
Jeff Brown89d55462012-09-19 11:33:42 -0700232 // Persistent data store for all internal settings maintained by the display manager service.
233 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
234
Jeff Brownbd6e1502012-08-28 03:27:37 -0700235 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700236 // May be used outside of the lock but only on the handler thread.
237 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700238
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700239 // Temporary display info, used for comparing display configurations.
240 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
241
Jeff Brownd728bf52012-09-08 18:05:28 -0700242 // Temporary viewports, used when sending new viewport information to the
243 // input system. May be used outside of the lock but only on the handler thread.
244 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
245 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700246 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700247
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700248 // The default color mode for default displays. Overrides the usual
249 // Display.Display.COLOR_MODE_DEFAULT for displays with the
250 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
251 private final int mDefaultDisplayDefaultColorMode;
252
Jeff Browne75926d2014-09-18 15:24:49 -0700253 // Temporary list of deferred work to perform when setting the display state.
254 // Only used by requestDisplayState. The field is self-synchronized and only
255 // intended for use inside of the requestGlobalDisplayStateInternal function.
256 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
257
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800258 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
259 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
260
Santos Cordonee8931e2017-04-05 10:31:15 -0700261 private final Injector mInjector;
262
Kenny Guy22bd0442017-10-26 00:15:54 +0100263 private final BrightnessTracker mBrightnessTracker;
264
Jeff Brownb880d882014-02-10 19:47:07 -0800265 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700266 this(context, new Injector());
267 }
268
269 @VisibleForTesting
270 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800271 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700272 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800273 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800274 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700275 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700276 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700277 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700278 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
279 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700280
281 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
282 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Kenny Guy22bd0442017-10-26 00:15:54 +0100283 mBrightnessTracker = new BrightnessTracker(context, null);
Joel Fernandes2d314e12017-04-04 16:32:15 -0700284 }
285
286 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100287 // android.display and android.anim is critical to user experience and we should make sure
288 // it is not in the default foregroup groups, add it to top-app to make sure it uses all the
289 // cores and scheduling settings for top-app when it runs.
290 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
291 Process.THREAD_GROUP_TOP_APP);
292 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
293 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700294 }
295
Jeff Brown4ccb8232014-01-16 22:16:42 -0800296 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800297 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700298 // We need to pre-load the persistent data store so it's ready before the default display
299 // adapter is up so that we have it's configuration. We could load it lazily, but since
300 // 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 -0700301 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100302 synchronized(mSyncRoot) {
303 mPersistentDataStore.loadIfNeeded();
304 loadStableDisplayValuesLocked();
305 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700306 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800307
308 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
309 true /*allowIsolated*/);
310 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700311 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800312 }
313
314 @Override
315 public void onBootPhase(int phase) {
316 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
317 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700318 long timeout = SystemClock.uptimeMillis()
319 + mInjector.getDefaultDisplayDelayTimeout();
320 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
321 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800322 long delay = timeout - SystemClock.uptimeMillis();
323 if (delay <= 0) {
324 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700325 + "to be initialized. DefaultDisplay="
326 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
327 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800328 }
329 if (DEBUG) {
330 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
331 }
332 try {
333 mSyncRoot.wait(delay);
334 } catch (InterruptedException ex) {
335 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700336 }
337 }
338 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700339 }
340
Jeff Brown4ccb8232014-01-16 22:16:42 -0800341 // TODO: Use dependencies or a boot phase
342 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700343 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800344 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
345 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700346 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700347 }
348 }
349
350 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700351 * Called when the system is ready to go.
352 */
353 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700354 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700355 mSafeMode = safeMode;
356 mOnlyCore = onlyCore;
357 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700358
Jeff Brownbd6e1502012-08-28 03:27:37 -0700359 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100360 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700361 }
362
Santos Cordonee8931e2017-04-05 10:31:15 -0700363 @VisibleForTesting
364 Handler getDisplayHandler() {
365 return mHandler;
366 }
367
Michael Wrighteedcbf12017-08-16 23:14:54 +0100368 private void loadStableDisplayValuesLocked() {
369 final Point size = mPersistentDataStore.getStableDisplaySize();
370 if (size.x > 0 && size.y > 0) {
371 // Just set these values directly so we don't write the display persistent data again
372 // unnecessarily
373 mStableDisplaySize.set(size.x, size.y);
374 } else {
375 final Resources res = mContext.getResources();
376 final int width = res.getInteger(
377 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
378 final int height = res.getInteger(
379 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
380 if (width > 0 && height > 0) {
381 setStableDisplaySizeLocked(width, height);
382 }
383 }
384 }
385
386 private Point getStableDisplaySizeInternal() {
387 Point r = new Point();
388 synchronized (mSyncRoot) {
389 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
390 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
391 }
392 }
393 return r;
394 }
395
Jeff Brown4ccb8232014-01-16 22:16:42 -0800396 private void registerDisplayTransactionListenerInternal(
397 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800398 // List is self-synchronized copy-on-write.
399 mDisplayTransactionListeners.add(listener);
400 }
401
Jeff Brown4ccb8232014-01-16 22:16:42 -0800402 private void unregisterDisplayTransactionListenerInternal(
403 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800404 // List is self-synchronized copy-on-write.
405 mDisplayTransactionListeners.remove(listener);
406 }
407
Jeff Brown4ccb8232014-01-16 22:16:42 -0800408 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700409 int displayId, DisplayInfo info) {
410 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700411 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700412 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700413 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700414 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700415 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700416 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700417 }
418 }
419 }
420
Andrii Kuliancd097992017-03-23 18:31:59 -0700421 /**
422 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
423 */
424 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
425 synchronized (mSyncRoot) {
426 final LogicalDisplay display = mLogicalDisplays.get(displayId);
427 if (display != null) {
428 display.getNonOverrideDisplayInfoLocked(outInfo);
429 }
430 }
431 }
432
Santos Cordonee8931e2017-04-05 10:31:15 -0700433 @VisibleForTesting
434 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700435 synchronized (mSyncRoot) {
436 if (!mPendingTraversal) {
437 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700438 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700439 mPendingTraversal = false;
440
441 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700442 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800443
444 // List is self-synchronized copy-on-write.
445 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
446 listener.onDisplayTransaction();
447 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700448 }
449
Jeff Brown5d6443b2015-04-10 20:15:01 -0700450 private void requestGlobalDisplayStateInternal(int state, int brightness) {
451 if (state == Display.STATE_UNKNOWN) {
452 state = Display.STATE_ON;
453 }
454 if (state == Display.STATE_OFF) {
455 brightness = PowerManager.BRIGHTNESS_OFF;
456 } else if (brightness < 0) {
457 brightness = PowerManager.BRIGHTNESS_DEFAULT;
458 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
459 brightness = PowerManager.BRIGHTNESS_ON;
460 }
461
Jeff Browne75926d2014-09-18 15:24:49 -0700462 synchronized (mTempDisplayStateWorkQueue) {
463 try {
464 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700465 // Note that we do not need to schedule traversals here although it
466 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700467 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700468 if (mGlobalDisplayState == state
469 && mGlobalDisplayBrightness == brightness) {
470 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700471 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700472
473 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
474 + Display.stateToString(state)
475 + ", brightness=" + brightness + ")");
476 mGlobalDisplayState = state;
477 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700478 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700479 }
480
481 // Setting the display power state can take hundreds of milliseconds
482 // to complete so we defer the most expensive part of the work until
483 // after we have exited the critical section to avoid blocking other
484 // threads for a long time.
485 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
486 mTempDisplayStateWorkQueue.get(i).run();
487 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700488 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700489 } finally {
490 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700491 }
492 }
493 }
494
Jeff Brown4ccb8232014-01-16 22:16:42 -0800495 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700496 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800497 LogicalDisplay display = mLogicalDisplays.get(displayId);
498 if (display != null) {
499 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800500 if (info.hasAccess(callingUid)
501 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800502 return info;
503 }
504 }
505 return null;
506 }
507 }
508
509 private int[] getDisplayIdsInternal(int callingUid) {
510 synchronized (mSyncRoot) {
511 final int count = mLogicalDisplays.size();
512 int[] displayIds = new int[count];
513 int n = 0;
514 for (int i = 0; i < count; i++) {
515 LogicalDisplay display = mLogicalDisplays.valueAt(i);
516 DisplayInfo info = display.getDisplayInfoLocked();
517 if (info.hasAccess(callingUid)) {
518 displayIds[n++] = mLogicalDisplays.keyAt(i);
519 }
520 }
521 if (n != count) {
522 displayIds = Arrays.copyOfRange(displayIds, 0, n);
523 }
524 return displayIds;
525 }
526 }
527
528 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
529 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700530 if (mCallbacks.get(callingPid) != null) {
531 throw new SecurityException("The calling process has already "
532 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700533 }
534
Jeff Brownbd6e1502012-08-28 03:27:37 -0700535 CallbackRecord record = new CallbackRecord(callingPid, callback);
536 try {
537 IBinder binder = callback.asBinder();
538 binder.linkToDeath(record, 0);
539 } catch (RemoteException ex) {
540 // give up
541 throw new RuntimeException(ex);
542 }
543
544 mCallbacks.put(callingPid, record);
545 }
546 }
547
Jeff Brownce468a32013-11-21 16:42:03 -0800548 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700549 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800550 mCallbacks.remove(record.mPid);
551 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700552 }
553 }
554
Jeff Brown4ccb8232014-01-16 22:16:42 -0800555 private void startWifiDisplayScanInternal(int callingPid) {
556 synchronized (mSyncRoot) {
557 CallbackRecord record = mCallbacks.get(callingPid);
558 if (record == null) {
559 throw new IllegalStateException("The calling process has not "
560 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700561 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800562 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700563 }
564 }
565
Jeff Brownce468a32013-11-21 16:42:03 -0800566 private void startWifiDisplayScanLocked(CallbackRecord record) {
567 if (!record.mWifiDisplayScanRequested) {
568 record.mWifiDisplayScanRequested = true;
569 if (mWifiDisplayScanRequestCount++ == 0) {
570 if (mWifiDisplayAdapter != null) {
571 mWifiDisplayAdapter.requestStartScanLocked();
572 }
573 }
574 }
575 }
576
Jeff Brown4ccb8232014-01-16 22:16:42 -0800577 private void stopWifiDisplayScanInternal(int callingPid) {
578 synchronized (mSyncRoot) {
579 CallbackRecord record = mCallbacks.get(callingPid);
580 if (record == null) {
581 throw new IllegalStateException("The calling process has not "
582 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800583 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800584 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800585 }
586 }
587
588 private void stopWifiDisplayScanLocked(CallbackRecord record) {
589 if (record.mWifiDisplayScanRequested) {
590 record.mWifiDisplayScanRequested = false;
591 if (--mWifiDisplayScanRequestCount == 0) {
592 if (mWifiDisplayAdapter != null) {
593 mWifiDisplayAdapter.requestStopScanLocked();
594 }
595 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700596 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800597 + mWifiDisplayScanRequestCount);
598 mWifiDisplayScanRequestCount = 0;
599 }
600 }
601 }
602
Jeff Brown4ccb8232014-01-16 22:16:42 -0800603 private void connectWifiDisplayInternal(String address) {
604 synchronized (mSyncRoot) {
605 if (mWifiDisplayAdapter != null) {
606 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700607 }
Jeff Browne08ae382012-09-07 20:36:36 -0700608 }
609 }
610
Jeff Brown4ccb8232014-01-16 22:16:42 -0800611 private void pauseWifiDisplayInternal() {
612 synchronized (mSyncRoot) {
613 if (mWifiDisplayAdapter != null) {
614 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700615 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700616 }
617 }
618
Jeff Brown4ccb8232014-01-16 22:16:42 -0800619 private void resumeWifiDisplayInternal() {
620 synchronized (mSyncRoot) {
621 if (mWifiDisplayAdapter != null) {
622 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700623 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700624 }
625 }
626
Jeff Brown4ccb8232014-01-16 22:16:42 -0800627 private void disconnectWifiDisplayInternal() {
628 synchronized (mSyncRoot) {
629 if (mWifiDisplayAdapter != null) {
630 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700631 }
Jeff Browne08ae382012-09-07 20:36:36 -0700632 }
633 }
634
Jeff Brown4ccb8232014-01-16 22:16:42 -0800635 private void renameWifiDisplayInternal(String address, String alias) {
636 synchronized (mSyncRoot) {
637 if (mWifiDisplayAdapter != null) {
638 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700639 }
Jeff Brown89d55462012-09-19 11:33:42 -0700640 }
641 }
642
Jeff Brown4ccb8232014-01-16 22:16:42 -0800643 private void forgetWifiDisplayInternal(String address) {
644 synchronized (mSyncRoot) {
645 if (mWifiDisplayAdapter != null) {
646 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700647 }
Jeff Brown89d55462012-09-19 11:33:42 -0700648 }
649 }
650
Jeff Brown4ccb8232014-01-16 22:16:42 -0800651 private WifiDisplayStatus getWifiDisplayStatusInternal() {
652 synchronized (mSyncRoot) {
653 if (mWifiDisplayAdapter != null) {
654 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700655 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800656 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700657 }
658 }
659
Michael Wright1c9977b2016-07-12 13:30:10 -0700660 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100661 synchronized (mSyncRoot) {
662 LogicalDisplay display = mLogicalDisplays.get(displayId);
663 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700664 display.getRequestedColorModeLocked() != colorMode) {
665 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100666 scheduleTraversalLocked(false);
667 }
668 }
669 }
670
Michael Wright75ee9fc2014-09-01 19:55:22 -0700671 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700672 IMediaProjection projection, int callingUid, String packageName, String name, int width,
673 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800674 synchronized (mSyncRoot) {
675 if (mVirtualDisplayAdapter == null) {
676 Slog.w(TAG, "Rejecting request to create private virtual display "
677 + "because the virtual display adapter is not available.");
678 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700679 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800680
681 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700682 callback, projection, callingUid, packageName, name, width, height, densityDpi,
683 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800684 if (device == null) {
685 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700686 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700687
Jeff Brown4ccb8232014-01-16 22:16:42 -0800688 handleDisplayDeviceAddedLocked(device);
689 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
690 if (display != null) {
691 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700692 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800693
694 // Something weird happened and the logical display was not created.
695 Slog.w(TAG, "Rejecting request to create virtual display "
696 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700697 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800698 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700699 }
700 return -1;
701 }
702
Michael Wright01e840f2014-06-26 16:03:25 -0700703 private void resizeVirtualDisplayInternal(IBinder appToken,
704 int width, int height, int densityDpi) {
705 synchronized (mSyncRoot) {
706 if (mVirtualDisplayAdapter == null) {
707 return;
708 }
709
710 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
711 }
712 }
713
Jeff Brown92207df2014-04-16 13:16:07 -0700714 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
715 synchronized (mSyncRoot) {
716 if (mVirtualDisplayAdapter == null) {
717 return;
718 }
719
720 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
721 }
722 }
723
Jeff Brown4ccb8232014-01-16 22:16:42 -0800724 private void releaseVirtualDisplayInternal(IBinder appToken) {
725 synchronized (mSyncRoot) {
726 if (mVirtualDisplayAdapter == null) {
727 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700728 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700729
Jeff Brown4ccb8232014-01-16 22:16:42 -0800730 DisplayDevice device =
731 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
732 if (device != null) {
733 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700734 }
735 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700736 }
737
Santos Cordonc22c5632017-06-21 16:03:49 -0700738 private void registerDefaultDisplayAdapters() {
739 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700740 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700741 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800742 registerDisplayAdapterLocked(new LocalDisplayAdapter(
743 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700744
745 // Standalone VR devices rely on a virtual display as their primary display for
746 // 2D UI. We register virtual display adapter along side the main display adapter
747 // here so that it is ready by the time the system sends the home Intent for
748 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
749 // the virtual display inside VR before any VR-specific apps even run.
750 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
751 mHandler, mDisplayAdapterListener);
752 if (mVirtualDisplayAdapter != null) {
753 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
754 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700755 }
756 }
757
758 private void registerAdditionalDisplayAdapters() {
759 synchronized (mSyncRoot) {
760 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700761 registerOverlayDisplayAdapterLocked();
762 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700763 }
764 }
765 }
766
Jeff Brown89d55462012-09-19 11:33:42 -0700767 private void registerOverlayDisplayAdapterLocked() {
768 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
769 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
770 }
771
772 private void registerWifiDisplayAdapterLocked() {
773 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700774 com.android.internal.R.bool.config_enableWifiDisplay)
775 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700776 mWifiDisplayAdapter = new WifiDisplayAdapter(
777 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
778 mPersistentDataStore);
779 registerDisplayAdapterLocked(mWifiDisplayAdapter);
780 }
781 }
782
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700783 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
784 // In safe mode, we disable non-essential display adapters to give the user
785 // an opportunity to fix broken settings or other problems that might affect
786 // system stability.
787 // In only-core mode, we disable non-essential display adapters to minimize
788 // the number of dependencies that are started while in this mode and to
789 // prevent problems that might occur due to the device being encrypted.
790 return !mSafeMode && !mOnlyCore;
791 }
792
793 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
794 mDisplayAdapters.add(adapter);
795 adapter.registerLocked();
796 }
797
Jeff Brownbd6e1502012-08-28 03:27:37 -0700798 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700799 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700800 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700801 }
802 }
803
Jeff Browna506a6e2013-06-04 00:02:38 -0700804 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700805 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700806 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700807 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700808 return;
809 }
810
Jeff Brown10acf6d2015-04-14 14:20:47 -0700811 Slog.i(TAG, "Display device added: " + info);
812 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700813
814 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700815 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700816 Runnable work = updateDisplayStateLocked(device);
817 if (work != null) {
818 work.run();
819 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700820 scheduleTraversalLocked(false);
821 }
822
Jeff Brownbd6e1502012-08-28 03:27:37 -0700823 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700824 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700825 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700826 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700827 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700828 return;
829 }
830
Jeff Brown10acf6d2015-04-14 14:20:47 -0700831 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
832 if (diff == DisplayDeviceInfo.DIFF_STATE) {
833 Slog.i(TAG, "Display device changed state: \"" + info.name
834 + "\", " + Display.stateToString(info.state));
835 } else if (diff != 0) {
836 Slog.i(TAG, "Display device changed: " + info);
837 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700838 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
839 try {
840 mPersistentDataStore.setColorMode(device, info.colorMode);
841 } finally {
842 mPersistentDataStore.saveIfNeeded();
843 }
844 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700845 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700846
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700847 device.applyPendingDisplayDeviceInfoChangesLocked();
848 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700849 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700850 }
851 }
852 }
853
Jeff Brownbd6e1502012-08-28 03:27:37 -0700854 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700855 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700856 handleDisplayDeviceRemovedLocked(device);
857 }
858 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700859
Jeff Browna506a6e2013-06-04 00:02:38 -0700860 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700861 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700862 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700863 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700864 return;
865 }
866
Jeff Brown10acf6d2015-04-14 14:20:47 -0700867 Slog.i(TAG, "Display device removed: " + info);
868 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700869
Jeff Browna506a6e2013-06-04 00:02:38 -0700870 updateLogicalDisplaysLocked();
871 scheduleTraversalLocked(false);
872 }
873
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700874 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700875 final int count = mDisplayDevices.size();
876 for (int i = 0; i < count; i++) {
877 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700878 Runnable runnable = updateDisplayStateLocked(device);
879 if (runnable != null) {
880 workQueue.add(runnable);
881 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700882 }
883 }
884
Jeff Browne75926d2014-09-18 15:24:49 -0700885 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700886 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700887 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700888 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
889 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700890 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700891 }
Jeff Browne75926d2014-09-18 15:24:49 -0700892 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700893 }
894
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700895 // Adds a new logical display based on the given display device.
896 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700897 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700898 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
899 boolean isDefault = (deviceInfo.flags
900 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
901 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
902 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
903 isDefault = false;
904 }
905
Jeff Brown27f1d672012-10-17 18:32:34 -0700906 if (!isDefault && mSingleDisplayDemoMode) {
907 Slog.i(TAG, "Not creating a logical display for a secondary display "
908 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700909 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700910 }
911
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700912 final int displayId = assignDisplayIdLocked(isDefault);
913 final int layerStack = assignLayerStackLocked(displayId);
914
Jeff Brownd728bf52012-09-08 18:05:28 -0700915 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700916 display.updateLocked(mDisplayDevices);
917 if (!display.isValidLocked()) {
918 // This should never happen currently.
919 Slog.w(TAG, "Ignoring display device because the logical display "
920 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700921 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700922 }
923
Michael Wrighteedcbf12017-08-16 23:14:54 +0100924 configureColorModeLocked(display, device);
925 if (isDefault) {
926 recordStableDisplayStatsIfNeededLocked(display);
927 }
928
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700929 mLogicalDisplays.put(displayId, display);
930
931 // Wake up waitForDefaultDisplay.
932 if (isDefault) {
933 mSyncRoot.notifyAll();
934 }
935
936 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700937 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700938 }
939
940 private int assignDisplayIdLocked(boolean isDefault) {
941 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
942 }
943
944 private int assignLayerStackLocked(int displayId) {
945 // Currently layer stacks and display ids are the same.
946 // This need not be the case.
947 return displayId;
948 }
949
Michael Wrighteedcbf12017-08-16 23:14:54 +0100950 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
951 if (display.getPrimaryDisplayDeviceLocked() == device) {
952 int colorMode = mPersistentDataStore.getColorMode(device);
953 if (colorMode == Display.COLOR_MODE_INVALID) {
954 if ((device.getDisplayDeviceInfoLocked().flags
955 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
956 colorMode = mDefaultDisplayDefaultColorMode;
957 } else {
958 colorMode = Display.COLOR_MODE_DEFAULT;
959 }
960 }
961 display.setRequestedColorModeLocked(colorMode);
962 }
963 }
964
965 // If we've never recorded stable device stats for this device before and they aren't
966 // explicitly configured, go ahead and record the stable device stats now based on the status
967 // of the default display at first boot.
968 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
969 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
970 DisplayInfo info = d.getDisplayInfoLocked();
971 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
972 }
973 }
974
975 private void setStableDisplaySizeLocked(int width, int height) {
976 mStableDisplaySize = new Point(width, height);
977 try {
978 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
979 } finally {
980 mPersistentDataStore.saveIfNeeded();
981 }
982 }
983
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700984 // Updates all existing logical displays given the current set of display devices.
985 // Removes invalid logical displays.
986 // Sends notifications if needed.
987 private boolean updateLogicalDisplaysLocked() {
988 boolean changed = false;
989 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
990 final int displayId = mLogicalDisplays.keyAt(i);
991 LogicalDisplay display = mLogicalDisplays.valueAt(i);
992
993 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
994 display.updateLocked(mDisplayDevices);
995 if (!display.isValidLocked()) {
996 mLogicalDisplays.removeAt(i);
997 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
998 changed = true;
999 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1000 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1001 changed = true;
1002 }
1003 }
1004 return changed;
1005 }
1006
1007 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -07001008 // Clear all viewports before configuring displays so that we can keep
1009 // track of which ones we have configured.
1010 clearViewportsLocked();
1011
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001012 // Configure each display device.
1013 final int count = mDisplayDevices.size();
1014 for (int i = 0; i < count; i++) {
1015 DisplayDevice device = mDisplayDevices.get(i);
1016 configureDisplayInTransactionLocked(device);
1017 device.performTraversalInTransactionLocked();
1018 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001019
1020 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001021 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001022 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1023 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001024 }
1025
Michael Wright3f145a22014-07-22 19:46:03 -07001026 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001027 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001028 synchronized (mSyncRoot) {
1029 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001030 if (display == null) {
1031 return;
1032 }
1033 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001034 if (DEBUG) {
1035 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1036 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1037 }
1038
Craig Mautner722285e2012-09-07 13:55:58 -07001039 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001040 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001041 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001042 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1043 // Scan supported modes returned by display.getInfo() to find a mode with the same
1044 // size as the default display mode but with the specified refresh rate instead.
1045 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1046 requestedRefreshRate);
1047 }
1048 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001049 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001050 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001051 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001052 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001053 scheduleTraversalLocked(inTraversal);
1054 }
Craig Mautner722285e2012-09-07 13:55:58 -07001055 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001056 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001057
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001058 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1059 synchronized (mSyncRoot) {
1060 LogicalDisplay display = mLogicalDisplays.get(displayId);
1061 if (display == null) {
1062 return;
1063 }
1064 if (display.getDisplayOffsetXLocked() != x
1065 || display.getDisplayOffsetYLocked() != y) {
1066 if (DEBUG) {
1067 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1068 + x + ", " + y + ")");
1069 }
1070 display.setDisplayOffsetsLocked(x, y);
1071 scheduleTraversalLocked(false);
1072 }
1073 }
1074 }
1075
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001076 // Updates the lists of UIDs that are present on displays.
1077 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1078 synchronized (mSyncRoot) {
1079 mDisplayAccessUIDs.clear();
1080 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1081 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1082 newDisplayAccessUIDs.valueAt(i));
1083 }
1084 }
1085 }
1086
1087 // Checks if provided UID's content is present on the display and UID has access to it.
1088 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1089 synchronized (mSyncRoot) {
1090 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1091 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1092 }
1093 }
1094
Jeff Brownd728bf52012-09-08 18:05:28 -07001095 private void clearViewportsLocked() {
1096 mDefaultViewport.valid = false;
1097 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001098 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001099 }
1100
1101 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001102 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1103 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001104
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001105 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001106 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001107 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001108 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001109 if (display != null && !display.hasContentLocked()) {
1110 // If the display does not have any content of its own, then
1111 // automatically mirror the default logical display contents.
1112 display = null;
1113 }
1114 if (display == null) {
1115 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1116 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001117 }
1118
1119 // Apply the logical display configuration to the display device.
1120 if (display == null) {
1121 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001122 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001123 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001124 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001125 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001126 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001127
1128 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001129 if (!mDefaultViewport.valid
1130 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1131 setViewportLocked(mDefaultViewport, display, device);
1132 }
1133 if (!mExternalTouchViewport.valid
1134 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1135 setViewportLocked(mExternalTouchViewport, display, device);
1136 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001137
1138 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1139 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1140 setViewportLocked(viewport, display, device);
1141 }
1142 }
1143
1144 /** Gets the virtual device viewport or creates it if not yet created. */
1145 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1146 DisplayViewport viewport;
1147 final int count = mVirtualTouchViewports.size();
1148 for (int i = 0; i < count; i++) {
1149 viewport = mVirtualTouchViewports.get(i);
1150 if (uniqueId.equals(viewport.uniqueId)) {
1151 return viewport;
1152 }
1153 }
1154
1155 viewport = new DisplayViewport();
1156 viewport.uniqueId = uniqueId;
1157 mVirtualTouchViewports.add(viewport);
1158 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001159 }
1160
1161 private static void setViewportLocked(DisplayViewport viewport,
1162 LogicalDisplay display, DisplayDevice device) {
1163 viewport.valid = true;
1164 viewport.displayId = display.getDisplayIdLocked();
1165 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001166 }
1167
1168 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1169 final int count = mLogicalDisplays.size();
1170 for (int i = 0; i < count; i++) {
1171 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1172 if (display.getPrimaryDisplayDeviceLocked() == device) {
1173 return display;
1174 }
1175 }
1176 return null;
1177 }
1178
Jeff Brownbd6e1502012-08-28 03:27:37 -07001179 private void sendDisplayEventLocked(int displayId, int event) {
1180 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1181 mHandler.sendMessage(msg);
1182 }
1183
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001184 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1185 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001186 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001187 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001188 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001189 if (!inTraversal) {
1190 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1191 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001192 }
1193 }
1194
1195 // Runs on Handler thread.
1196 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001197 private void deliverDisplayEvent(int displayId, int event) {
1198 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001199 Slog.d(TAG, "Delivering display event: displayId="
1200 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001201 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001202
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001203 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001204 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001205 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001206 count = mCallbacks.size();
1207 mTempCallbacks.clear();
1208 for (int i = 0; i < count; i++) {
1209 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001210 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001211 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001212
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001213 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001214 for (int i = 0; i < count; i++) {
1215 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1216 }
1217 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001218 }
1219
Michael Wrightc39d47a2014-07-08 18:07:36 -07001220 private IMediaProjectionManager getProjectionService() {
1221 if (mProjectionService == null) {
1222 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1223 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1224 }
1225 return mProjectionService;
1226 }
1227
Jeff Brown4ccb8232014-01-16 22:16:42 -08001228 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001229 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001230
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001231 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001232 pw.println(" mOnlyCode=" + mOnlyCore);
1233 pw.println(" mSafeMode=" + mSafeMode);
1234 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001235 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001236 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1237 pw.println(" mDefaultViewport=" + mDefaultViewport);
1238 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001239 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001240 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001241 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001242 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001243 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1244
Jeff Brown9e316a12012-10-08 19:17:06 -07001245
Jeff Brownbd6e1502012-08-28 03:27:37 -07001246 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001247 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001248
1249 pw.println();
1250 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001251 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001252 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001253 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001254 }
Craig Mautner9de49362012-08-02 14:30:30 -07001255
Jeff Brownbd6e1502012-08-28 03:27:37 -07001256 pw.println();
1257 pw.println("Display Devices: size=" + mDisplayDevices.size());
1258 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001259 pw.println(" " + device.getDisplayDeviceInfoLocked());
1260 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001261 }
1262
1263 final int logicalDisplayCount = mLogicalDisplays.size();
1264 pw.println();
1265 pw.println("Logical Displays: size=" + logicalDisplayCount);
1266 for (int i = 0; i < logicalDisplayCount; i++) {
1267 int displayId = mLogicalDisplays.keyAt(i);
1268 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1269 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001270 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001271 }
Jeff Brownce468a32013-11-21 16:42:03 -08001272
1273 final int callbackCount = mCallbacks.size();
1274 pw.println();
1275 pw.println("Callbacks: size=" + callbackCount);
1276 for (int i = 0; i < callbackCount; i++) {
1277 CallbackRecord callback = mCallbacks.valueAt(i);
1278 pw.println(" " + i + ": mPid=" + callback.mPid
1279 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1280 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001281
1282 if (mDisplayPowerController != null) {
1283 mDisplayPowerController.dump(pw);
1284 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001285
1286 pw.println();
1287 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001288 }
1289 }
1290
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001291 /**
1292 * This is the object that everything in the display manager locks on.
1293 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1294 * clear that the object belongs to the display manager service and that it is
1295 * a unique object with a special purpose.
1296 */
1297 public static final class SyncRoot {
1298 }
1299
Santos Cordonee8931e2017-04-05 10:31:15 -07001300 @VisibleForTesting
1301 static class Injector {
1302 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1303 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1304 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1305 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001306
1307 long getDefaultDisplayDelayTimeout() {
1308 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1309 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001310 }
1311
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001312 @VisibleForTesting
1313 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1314 synchronized (mSyncRoot) {
1315 LogicalDisplay display = mLogicalDisplays.get(displayId);
1316 if (display != null) {
1317 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1318 return displayDevice.getDisplayDeviceInfoLocked();
1319 }
1320 return null;
1321 }
1322 }
1323
Jeff Brownbd6e1502012-08-28 03:27:37 -07001324 private final class DisplayManagerHandler extends Handler {
1325 public DisplayManagerHandler(Looper looper) {
1326 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001327 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001328
Jeff Brownbd6e1502012-08-28 03:27:37 -07001329 @Override
1330 public void handleMessage(Message msg) {
1331 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001332 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1333 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001334 break;
1335
1336 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1337 registerAdditionalDisplayAdapters();
1338 break;
1339
1340 case MSG_DELIVER_DISPLAY_EVENT:
1341 deliverDisplayEvent(msg.arg1, msg.arg2);
1342 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001343
1344 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001345 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001346 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001347
1348 case MSG_UPDATE_VIEWPORT: {
1349 synchronized (mSyncRoot) {
1350 mTempDefaultViewport.copyFrom(mDefaultViewport);
1351 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001352 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1353 mTempVirtualTouchViewports.clear();
1354 for (DisplayViewport d : mVirtualTouchViewports) {
1355 mTempVirtualTouchViewports.add(d.makeCopy());
1356 }
1357 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001358 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001359 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1360 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001361 break;
1362 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001363
1364 case MSG_REGISTER_BRIGHTNESS_TRACKER:
1365 mBrightnessTracker.start();
1366 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001367 }
1368 }
1369 }
1370
1371 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1372 @Override
1373 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1374 switch (event) {
1375 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1376 handleDisplayDeviceAdded(device);
1377 break;
1378
1379 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1380 handleDisplayDeviceChanged(device);
1381 break;
1382
1383 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1384 handleDisplayDeviceRemoved(device);
1385 break;
1386 }
1387 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001388
1389 @Override
1390 public void onTraversalRequested() {
1391 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001392 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001393 }
1394 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001395 }
1396
1397 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001398 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001399 private final IDisplayManagerCallback mCallback;
1400
Jeff Brownce468a32013-11-21 16:42:03 -08001401 public boolean mWifiDisplayScanRequested;
1402
Jeff Brownbd6e1502012-08-28 03:27:37 -07001403 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1404 mPid = pid;
1405 mCallback = callback;
1406 }
1407
1408 @Override
1409 public void binderDied() {
1410 if (DEBUG) {
1411 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1412 }
Jeff Brownce468a32013-11-21 16:42:03 -08001413 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001414 }
1415
1416 public void notifyDisplayEventAsync(int displayId, int event) {
1417 try {
1418 mCallback.onDisplayEvent(displayId, event);
1419 } catch (RemoteException ex) {
1420 Slog.w(TAG, "Failed to notify process "
1421 + mPid + " that displays changed, assuming it died.", ex);
1422 binderDied();
1423 }
1424 }
1425 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001426
Santos Cordonee8931e2017-04-05 10:31:15 -07001427 @VisibleForTesting
1428 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001429 /**
1430 * Returns information about the specified logical display.
1431 *
1432 * @param displayId The logical display id.
1433 * @return The logical display info, or null if the display does not exist. The
1434 * returned object must be treated as immutable.
1435 */
1436 @Override // Binder call
1437 public DisplayInfo getDisplayInfo(int displayId) {
1438 final int callingUid = Binder.getCallingUid();
1439 final long token = Binder.clearCallingIdentity();
1440 try {
1441 return getDisplayInfoInternal(displayId, callingUid);
1442 } finally {
1443 Binder.restoreCallingIdentity(token);
1444 }
1445 }
1446
1447 /**
1448 * Returns the list of all display ids.
1449 */
1450 @Override // Binder call
1451 public int[] getDisplayIds() {
1452 final int callingUid = Binder.getCallingUid();
1453 final long token = Binder.clearCallingIdentity();
1454 try {
1455 return getDisplayIdsInternal(callingUid);
1456 } finally {
1457 Binder.restoreCallingIdentity(token);
1458 }
1459 }
1460
Michael Wrighteedcbf12017-08-16 23:14:54 +01001461 /**
1462 * Returns the stable device display size, in pixels.
1463 */
1464 @Override // Binder call
1465 public Point getStableDisplaySize() {
1466 final long token = Binder.clearCallingIdentity();
1467 try {
1468 return getStableDisplaySizeInternal();
1469 } finally {
1470 Binder.restoreCallingIdentity(token);
1471 }
1472 }
1473
Jeff Brown4ccb8232014-01-16 22:16:42 -08001474 @Override // Binder call
1475 public void registerCallback(IDisplayManagerCallback callback) {
1476 if (callback == null) {
1477 throw new IllegalArgumentException("listener must not be null");
1478 }
1479
1480 final int callingPid = Binder.getCallingPid();
1481 final long token = Binder.clearCallingIdentity();
1482 try {
1483 registerCallbackInternal(callback, callingPid);
1484 } finally {
1485 Binder.restoreCallingIdentity(token);
1486 }
1487 }
1488
1489 @Override // Binder call
1490 public void startWifiDisplayScan() {
1491 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1492 "Permission required to start wifi display scans");
1493
1494 final int callingPid = Binder.getCallingPid();
1495 final long token = Binder.clearCallingIdentity();
1496 try {
1497 startWifiDisplayScanInternal(callingPid);
1498 } finally {
1499 Binder.restoreCallingIdentity(token);
1500 }
1501 }
1502
1503 @Override // Binder call
1504 public void stopWifiDisplayScan() {
1505 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1506 "Permission required to stop wifi display scans");
1507
1508 final int callingPid = Binder.getCallingPid();
1509 final long token = Binder.clearCallingIdentity();
1510 try {
1511 stopWifiDisplayScanInternal(callingPid);
1512 } finally {
1513 Binder.restoreCallingIdentity(token);
1514 }
1515 }
1516
1517 @Override // Binder call
1518 public void connectWifiDisplay(String address) {
1519 if (address == null) {
1520 throw new IllegalArgumentException("address must not be null");
1521 }
1522 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1523 "Permission required to connect to a wifi display");
1524
1525 final long token = Binder.clearCallingIdentity();
1526 try {
1527 connectWifiDisplayInternal(address);
1528 } finally {
1529 Binder.restoreCallingIdentity(token);
1530 }
1531 }
1532
1533 @Override // Binder call
1534 public void disconnectWifiDisplay() {
1535 // This request does not require special permissions.
1536 // Any app can request disconnection from the currently active wifi display.
1537 // This exception should no longer be needed once wifi display control moves
1538 // to the media router service.
1539
1540 final long token = Binder.clearCallingIdentity();
1541 try {
1542 disconnectWifiDisplayInternal();
1543 } finally {
1544 Binder.restoreCallingIdentity(token);
1545 }
1546 }
1547
1548 @Override // Binder call
1549 public void renameWifiDisplay(String address, String alias) {
1550 if (address == null) {
1551 throw new IllegalArgumentException("address must not be null");
1552 }
1553 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1554 "Permission required to rename to a wifi display");
1555
1556 final long token = Binder.clearCallingIdentity();
1557 try {
1558 renameWifiDisplayInternal(address, alias);
1559 } finally {
1560 Binder.restoreCallingIdentity(token);
1561 }
1562 }
1563
1564 @Override // Binder call
1565 public void forgetWifiDisplay(String address) {
1566 if (address == null) {
1567 throw new IllegalArgumentException("address must not be null");
1568 }
1569 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1570 "Permission required to forget to a wifi display");
1571
1572 final long token = Binder.clearCallingIdentity();
1573 try {
1574 forgetWifiDisplayInternal(address);
1575 } finally {
1576 Binder.restoreCallingIdentity(token);
1577 }
1578 }
1579
1580 @Override // Binder call
1581 public void pauseWifiDisplay() {
1582 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1583 "Permission required to pause a wifi display session");
1584
1585 final long token = Binder.clearCallingIdentity();
1586 try {
1587 pauseWifiDisplayInternal();
1588 } finally {
1589 Binder.restoreCallingIdentity(token);
1590 }
1591 }
1592
1593 @Override // Binder call
1594 public void resumeWifiDisplay() {
1595 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1596 "Permission required to resume a wifi display session");
1597
1598 final long token = Binder.clearCallingIdentity();
1599 try {
1600 resumeWifiDisplayInternal();
1601 } finally {
1602 Binder.restoreCallingIdentity(token);
1603 }
1604 }
1605
1606 @Override // Binder call
1607 public WifiDisplayStatus getWifiDisplayStatus() {
1608 // This request does not require special permissions.
1609 // Any app can get information about available wifi displays.
1610
1611 final long token = Binder.clearCallingIdentity();
1612 try {
1613 return getWifiDisplayStatusInternal();
1614 } finally {
1615 Binder.restoreCallingIdentity(token);
1616 }
1617 }
1618
1619 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001620 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001621 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001622 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1623 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001624 final long token = Binder.clearCallingIdentity();
1625 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001626 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001627 } finally {
1628 Binder.restoreCallingIdentity(token);
1629 }
1630 }
1631
1632 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001633 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001634 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001635 int width, int height, int densityDpi, Surface surface, int flags,
1636 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001637 final int callingUid = Binder.getCallingUid();
1638 if (!validatePackageName(callingUid, packageName)) {
1639 throw new SecurityException("packageName must match the calling uid");
1640 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001641 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001642 throw new IllegalArgumentException("appToken must not be null");
1643 }
1644 if (TextUtils.isEmpty(name)) {
1645 throw new IllegalArgumentException("name must be non-null and non-empty");
1646 }
1647 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1648 throw new IllegalArgumentException("width, height, and densityDpi must be "
1649 + "greater than 0");
1650 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001651 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001652 throw new IllegalArgumentException("Surface can't be single-buffered");
1653 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001654
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001655 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1656 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1657
1658 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001659 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001660 throw new IllegalArgumentException(
1661 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1662 }
Michael Wright6720be42014-07-29 19:14:16 -07001663 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001664 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1665 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001666 }
1667
Michael Wrightc39d47a2014-07-08 18:07:36 -07001668 if (projection != null) {
1669 try {
1670 if (!getProjectionService().isValidMediaProjection(projection)) {
1671 throw new SecurityException("Invalid media projection");
1672 }
Michael Wright6720be42014-07-29 19:14:16 -07001673 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001674 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001675 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001676 }
1677 }
1678
Michael Wright6720be42014-07-29 19:14:16 -07001679 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001680 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001681 if (!canProjectVideo(projection)) {
1682 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1683 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1684 + "MediaProjection token in order to create a screen sharing virtual "
1685 + "display.");
1686 }
1687 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001688 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001689 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001690 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001691 + "or an appropriate MediaProjection token to create a "
1692 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001693 }
1694 }
1695
1696 final long token = Binder.clearCallingIdentity();
1697 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001698 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1699 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001700 } finally {
1701 Binder.restoreCallingIdentity(token);
1702 }
1703 }
1704
1705 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001706 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001707 int width, int height, int densityDpi) {
1708 final long token = Binder.clearCallingIdentity();
1709 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001710 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001711 } finally {
1712 Binder.restoreCallingIdentity(token);
1713 }
1714 }
1715
1716 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001717 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001718 if (surface != null && surface.isSingleBuffered()) {
1719 throw new IllegalArgumentException("Surface can't be single-buffered");
1720 }
Jeff Brown92207df2014-04-16 13:16:07 -07001721 final long token = Binder.clearCallingIdentity();
1722 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001723 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001724 } finally {
1725 Binder.restoreCallingIdentity(token);
1726 }
1727 }
1728
1729 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001730 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001731 final long token = Binder.clearCallingIdentity();
1732 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001733 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001734 } finally {
1735 Binder.restoreCallingIdentity(token);
1736 }
1737 }
1738
1739 @Override // Binder call
1740 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001741 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001742
1743 final long token = Binder.clearCallingIdentity();
1744 try {
1745 dumpInternal(pw);
1746 } finally {
1747 Binder.restoreCallingIdentity(token);
1748 }
1749 }
1750
Kenny Guy22bd0442017-10-26 00:15:54 +01001751 @Override // Binder call
1752 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents() {
1753 mContext.enforceCallingOrSelfPermission(
1754 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1755 "Permission to read brightness events.");
1756 int userId = UserHandle.getUserId(Binder.getCallingUid());
1757 final long token = Binder.clearCallingIdentity();
1758 try {
1759 return mBrightnessTracker.getEvents(userId);
1760 } finally {
1761 Binder.restoreCallingIdentity(token);
1762 }
1763 }
1764
1765 @Override // Binder call
1766 public void setBrightness(int brightness) {
1767 // STOPSHIP - remove when adaptive brightness controller accepts curves.
1768 mContext.enforceCallingOrSelfPermission(
1769 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1770 "Permission to set brightness.");
1771 int userId = UserHandle.getUserId(Binder.getCallingUid());
1772 final long token = Binder.clearCallingIdentity();
1773 try {
1774 mBrightnessTracker.setBrightness(brightness, userId);
1775 } finally {
1776 Binder.restoreCallingIdentity(token);
1777 }
1778 }
1779
Jeff Brown4ccb8232014-01-16 22:16:42 -08001780 private boolean validatePackageName(int uid, String packageName) {
1781 if (packageName != null) {
1782 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1783 if (packageNames != null) {
1784 for (String n : packageNames) {
1785 if (n.equals(packageName)) {
1786 return true;
1787 }
1788 }
1789 }
1790 }
1791 return false;
1792 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001793
1794 private boolean canProjectVideo(IMediaProjection projection) {
1795 if (projection != null) {
1796 try {
1797 if (projection.canProjectVideo()) {
1798 return true;
1799 }
1800 } catch (RemoteException e) {
1801 Slog.e(TAG, "Unable to query projection service for permissions", e);
1802 }
1803 }
1804 if (mContext.checkCallingPermission(
1805 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001806 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001807 return true;
1808 }
1809 return canProjectSecureVideo(projection);
1810 }
1811
1812 private boolean canProjectSecureVideo(IMediaProjection projection) {
1813 if (projection != null) {
1814 try {
1815 if (projection.canProjectSecureVideo()){
1816 return true;
1817 }
1818 } catch (RemoteException e) {
1819 Slog.e(TAG, "Unable to query projection service for permissions", e);
1820 }
1821 }
1822 return mContext.checkCallingPermission(
1823 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001824 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001825 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001826 }
1827
1828 private final class LocalService extends DisplayManagerInternal {
1829 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001830 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001831 SensorManager sensorManager) {
1832 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001833 DisplayBlanker blanker = new DisplayBlanker() {
1834 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001835 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001836 // The order of operations is important for legacy reasons.
1837 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001838 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001839 }
1840
1841 callbacks.onDisplayStateChange(state);
1842
1843 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001844 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001845 }
1846 }
1847 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001848 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001849 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001850 }
1851 }
1852
1853 @Override
1854 public boolean requestPowerState(DisplayPowerRequest request,
1855 boolean waitForNegativeProximity) {
1856 return mDisplayPowerController.requestPowerState(request,
1857 waitForNegativeProximity);
1858 }
1859
1860 @Override
1861 public boolean isProximitySensorAvailable() {
1862 return mDisplayPowerController.isProximitySensorAvailable();
1863 }
1864
1865 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001866 public DisplayInfo getDisplayInfo(int displayId) {
1867 return getDisplayInfoInternal(displayId, Process.myUid());
1868 }
1869
1870 @Override
1871 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1872 if (listener == null) {
1873 throw new IllegalArgumentException("listener must not be null");
1874 }
1875
1876 registerDisplayTransactionListenerInternal(listener);
1877 }
1878
1879 @Override
1880 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1881 if (listener == null) {
1882 throw new IllegalArgumentException("listener must not be null");
1883 }
1884
1885 unregisterDisplayTransactionListenerInternal(listener);
1886 }
1887
1888 @Override
1889 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1890 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1891 }
1892
1893 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001894 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1895 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1896 }
1897
1898 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001899 public void performTraversalInTransactionFromWindowManager() {
1900 performTraversalInTransactionFromWindowManagerInternal();
1901 }
1902
1903 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001904 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001905 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1906 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1907 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001908 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001909
1910 @Override
1911 public void setDisplayOffsets(int displayId, int x, int y) {
1912 setDisplayOffsetsInternal(displayId, x, y);
1913 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001914
1915 @Override
1916 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1917 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1918 }
1919
1920 @Override
1921 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1922 return isUidPresentOnDisplayInternal(uid, displayId);
1923 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001924 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001925}