blob: 7530f3ef942b9ca12ce4b79e7732122bfcd21b1a [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);
Kenny Guycfe7b702017-11-14 21:04:58 +00001288
1289 pw.println();
1290 mBrightnessTracker.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001291 }
1292 }
1293
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001294 /**
1295 * This is the object that everything in the display manager locks on.
1296 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1297 * clear that the object belongs to the display manager service and that it is
1298 * a unique object with a special purpose.
1299 */
1300 public static final class SyncRoot {
1301 }
1302
Santos Cordonee8931e2017-04-05 10:31:15 -07001303 @VisibleForTesting
1304 static class Injector {
1305 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1306 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1307 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1308 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001309
1310 long getDefaultDisplayDelayTimeout() {
1311 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1312 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001313 }
1314
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001315 @VisibleForTesting
1316 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1317 synchronized (mSyncRoot) {
1318 LogicalDisplay display = mLogicalDisplays.get(displayId);
1319 if (display != null) {
1320 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1321 return displayDevice.getDisplayDeviceInfoLocked();
1322 }
1323 return null;
1324 }
1325 }
1326
Jeff Brownbd6e1502012-08-28 03:27:37 -07001327 private final class DisplayManagerHandler extends Handler {
1328 public DisplayManagerHandler(Looper looper) {
1329 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001330 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001331
Jeff Brownbd6e1502012-08-28 03:27:37 -07001332 @Override
1333 public void handleMessage(Message msg) {
1334 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001335 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1336 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001337 break;
1338
1339 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1340 registerAdditionalDisplayAdapters();
1341 break;
1342
1343 case MSG_DELIVER_DISPLAY_EVENT:
1344 deliverDisplayEvent(msg.arg1, msg.arg2);
1345 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001346
1347 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001348 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001349 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001350
1351 case MSG_UPDATE_VIEWPORT: {
1352 synchronized (mSyncRoot) {
1353 mTempDefaultViewport.copyFrom(mDefaultViewport);
1354 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001355 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1356 mTempVirtualTouchViewports.clear();
1357 for (DisplayViewport d : mVirtualTouchViewports) {
1358 mTempVirtualTouchViewports.add(d.makeCopy());
1359 }
1360 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001361 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001362 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1363 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001364 break;
1365 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001366
1367 case MSG_REGISTER_BRIGHTNESS_TRACKER:
1368 mBrightnessTracker.start();
1369 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001370 }
1371 }
1372 }
1373
1374 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1375 @Override
1376 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1377 switch (event) {
1378 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1379 handleDisplayDeviceAdded(device);
1380 break;
1381
1382 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1383 handleDisplayDeviceChanged(device);
1384 break;
1385
1386 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1387 handleDisplayDeviceRemoved(device);
1388 break;
1389 }
1390 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001391
1392 @Override
1393 public void onTraversalRequested() {
1394 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001395 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001396 }
1397 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001398 }
1399
1400 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001401 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001402 private final IDisplayManagerCallback mCallback;
1403
Jeff Brownce468a32013-11-21 16:42:03 -08001404 public boolean mWifiDisplayScanRequested;
1405
Jeff Brownbd6e1502012-08-28 03:27:37 -07001406 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1407 mPid = pid;
1408 mCallback = callback;
1409 }
1410
1411 @Override
1412 public void binderDied() {
1413 if (DEBUG) {
1414 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1415 }
Jeff Brownce468a32013-11-21 16:42:03 -08001416 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001417 }
1418
1419 public void notifyDisplayEventAsync(int displayId, int event) {
1420 try {
1421 mCallback.onDisplayEvent(displayId, event);
1422 } catch (RemoteException ex) {
1423 Slog.w(TAG, "Failed to notify process "
1424 + mPid + " that displays changed, assuming it died.", ex);
1425 binderDied();
1426 }
1427 }
1428 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001429
Santos Cordonee8931e2017-04-05 10:31:15 -07001430 @VisibleForTesting
1431 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001432 /**
1433 * Returns information about the specified logical display.
1434 *
1435 * @param displayId The logical display id.
1436 * @return The logical display info, or null if the display does not exist. The
1437 * returned object must be treated as immutable.
1438 */
1439 @Override // Binder call
1440 public DisplayInfo getDisplayInfo(int displayId) {
1441 final int callingUid = Binder.getCallingUid();
1442 final long token = Binder.clearCallingIdentity();
1443 try {
1444 return getDisplayInfoInternal(displayId, callingUid);
1445 } finally {
1446 Binder.restoreCallingIdentity(token);
1447 }
1448 }
1449
1450 /**
1451 * Returns the list of all display ids.
1452 */
1453 @Override // Binder call
1454 public int[] getDisplayIds() {
1455 final int callingUid = Binder.getCallingUid();
1456 final long token = Binder.clearCallingIdentity();
1457 try {
1458 return getDisplayIdsInternal(callingUid);
1459 } finally {
1460 Binder.restoreCallingIdentity(token);
1461 }
1462 }
1463
Michael Wrighteedcbf12017-08-16 23:14:54 +01001464 /**
1465 * Returns the stable device display size, in pixels.
1466 */
1467 @Override // Binder call
1468 public Point getStableDisplaySize() {
1469 final long token = Binder.clearCallingIdentity();
1470 try {
1471 return getStableDisplaySizeInternal();
1472 } finally {
1473 Binder.restoreCallingIdentity(token);
1474 }
1475 }
1476
Jeff Brown4ccb8232014-01-16 22:16:42 -08001477 @Override // Binder call
1478 public void registerCallback(IDisplayManagerCallback callback) {
1479 if (callback == null) {
1480 throw new IllegalArgumentException("listener must not be null");
1481 }
1482
1483 final int callingPid = Binder.getCallingPid();
1484 final long token = Binder.clearCallingIdentity();
1485 try {
1486 registerCallbackInternal(callback, callingPid);
1487 } finally {
1488 Binder.restoreCallingIdentity(token);
1489 }
1490 }
1491
1492 @Override // Binder call
1493 public void startWifiDisplayScan() {
1494 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1495 "Permission required to start wifi display scans");
1496
1497 final int callingPid = Binder.getCallingPid();
1498 final long token = Binder.clearCallingIdentity();
1499 try {
1500 startWifiDisplayScanInternal(callingPid);
1501 } finally {
1502 Binder.restoreCallingIdentity(token);
1503 }
1504 }
1505
1506 @Override // Binder call
1507 public void stopWifiDisplayScan() {
1508 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1509 "Permission required to stop wifi display scans");
1510
1511 final int callingPid = Binder.getCallingPid();
1512 final long token = Binder.clearCallingIdentity();
1513 try {
1514 stopWifiDisplayScanInternal(callingPid);
1515 } finally {
1516 Binder.restoreCallingIdentity(token);
1517 }
1518 }
1519
1520 @Override // Binder call
1521 public void connectWifiDisplay(String address) {
1522 if (address == null) {
1523 throw new IllegalArgumentException("address must not be null");
1524 }
1525 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1526 "Permission required to connect to a wifi display");
1527
1528 final long token = Binder.clearCallingIdentity();
1529 try {
1530 connectWifiDisplayInternal(address);
1531 } finally {
1532 Binder.restoreCallingIdentity(token);
1533 }
1534 }
1535
1536 @Override // Binder call
1537 public void disconnectWifiDisplay() {
1538 // This request does not require special permissions.
1539 // Any app can request disconnection from the currently active wifi display.
1540 // This exception should no longer be needed once wifi display control moves
1541 // to the media router service.
1542
1543 final long token = Binder.clearCallingIdentity();
1544 try {
1545 disconnectWifiDisplayInternal();
1546 } finally {
1547 Binder.restoreCallingIdentity(token);
1548 }
1549 }
1550
1551 @Override // Binder call
1552 public void renameWifiDisplay(String address, String alias) {
1553 if (address == null) {
1554 throw new IllegalArgumentException("address must not be null");
1555 }
1556 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1557 "Permission required to rename to a wifi display");
1558
1559 final long token = Binder.clearCallingIdentity();
1560 try {
1561 renameWifiDisplayInternal(address, alias);
1562 } finally {
1563 Binder.restoreCallingIdentity(token);
1564 }
1565 }
1566
1567 @Override // Binder call
1568 public void forgetWifiDisplay(String address) {
1569 if (address == null) {
1570 throw new IllegalArgumentException("address must not be null");
1571 }
1572 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1573 "Permission required to forget to a wifi display");
1574
1575 final long token = Binder.clearCallingIdentity();
1576 try {
1577 forgetWifiDisplayInternal(address);
1578 } finally {
1579 Binder.restoreCallingIdentity(token);
1580 }
1581 }
1582
1583 @Override // Binder call
1584 public void pauseWifiDisplay() {
1585 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1586 "Permission required to pause a wifi display session");
1587
1588 final long token = Binder.clearCallingIdentity();
1589 try {
1590 pauseWifiDisplayInternal();
1591 } finally {
1592 Binder.restoreCallingIdentity(token);
1593 }
1594 }
1595
1596 @Override // Binder call
1597 public void resumeWifiDisplay() {
1598 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1599 "Permission required to resume a wifi display session");
1600
1601 final long token = Binder.clearCallingIdentity();
1602 try {
1603 resumeWifiDisplayInternal();
1604 } finally {
1605 Binder.restoreCallingIdentity(token);
1606 }
1607 }
1608
1609 @Override // Binder call
1610 public WifiDisplayStatus getWifiDisplayStatus() {
1611 // This request does not require special permissions.
1612 // Any app can get information about available wifi displays.
1613
1614 final long token = Binder.clearCallingIdentity();
1615 try {
1616 return getWifiDisplayStatusInternal();
1617 } finally {
1618 Binder.restoreCallingIdentity(token);
1619 }
1620 }
1621
1622 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001623 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001624 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001625 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1626 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001627 final long token = Binder.clearCallingIdentity();
1628 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001629 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001630 } finally {
1631 Binder.restoreCallingIdentity(token);
1632 }
1633 }
1634
1635 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001636 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001637 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001638 int width, int height, int densityDpi, Surface surface, int flags,
1639 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001640 final int callingUid = Binder.getCallingUid();
1641 if (!validatePackageName(callingUid, packageName)) {
1642 throw new SecurityException("packageName must match the calling uid");
1643 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001644 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001645 throw new IllegalArgumentException("appToken must not be null");
1646 }
1647 if (TextUtils.isEmpty(name)) {
1648 throw new IllegalArgumentException("name must be non-null and non-empty");
1649 }
1650 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1651 throw new IllegalArgumentException("width, height, and densityDpi must be "
1652 + "greater than 0");
1653 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001654 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001655 throw new IllegalArgumentException("Surface can't be single-buffered");
1656 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001657
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001658 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1659 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1660
1661 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001662 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001663 throw new IllegalArgumentException(
1664 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1665 }
Michael Wright6720be42014-07-29 19:14:16 -07001666 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001667 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1668 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001669 }
1670
Michael Wrightc39d47a2014-07-08 18:07:36 -07001671 if (projection != null) {
1672 try {
1673 if (!getProjectionService().isValidMediaProjection(projection)) {
1674 throw new SecurityException("Invalid media projection");
1675 }
Michael Wright6720be42014-07-29 19:14:16 -07001676 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001677 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001678 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001679 }
1680 }
1681
Michael Wright6720be42014-07-29 19:14:16 -07001682 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001683 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001684 if (!canProjectVideo(projection)) {
1685 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1686 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1687 + "MediaProjection token in order to create a screen sharing virtual "
1688 + "display.");
1689 }
1690 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001691 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001692 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001693 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001694 + "or an appropriate MediaProjection token to create a "
1695 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001696 }
1697 }
1698
1699 final long token = Binder.clearCallingIdentity();
1700 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001701 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1702 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001703 } finally {
1704 Binder.restoreCallingIdentity(token);
1705 }
1706 }
1707
1708 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001709 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001710 int width, int height, int densityDpi) {
1711 final long token = Binder.clearCallingIdentity();
1712 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001713 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001714 } finally {
1715 Binder.restoreCallingIdentity(token);
1716 }
1717 }
1718
1719 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001720 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001721 if (surface != null && surface.isSingleBuffered()) {
1722 throw new IllegalArgumentException("Surface can't be single-buffered");
1723 }
Jeff Brown92207df2014-04-16 13:16:07 -07001724 final long token = Binder.clearCallingIdentity();
1725 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001726 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001727 } finally {
1728 Binder.restoreCallingIdentity(token);
1729 }
1730 }
1731
1732 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001733 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001734 final long token = Binder.clearCallingIdentity();
1735 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001736 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001737 } finally {
1738 Binder.restoreCallingIdentity(token);
1739 }
1740 }
1741
1742 @Override // Binder call
1743 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001744 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001745
1746 final long token = Binder.clearCallingIdentity();
1747 try {
1748 dumpInternal(pw);
1749 } finally {
1750 Binder.restoreCallingIdentity(token);
1751 }
1752 }
1753
Kenny Guy22bd0442017-10-26 00:15:54 +01001754 @Override // Binder call
1755 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents() {
1756 mContext.enforceCallingOrSelfPermission(
1757 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1758 "Permission to read brightness events.");
1759 int userId = UserHandle.getUserId(Binder.getCallingUid());
1760 final long token = Binder.clearCallingIdentity();
1761 try {
1762 return mBrightnessTracker.getEvents(userId);
1763 } finally {
1764 Binder.restoreCallingIdentity(token);
1765 }
1766 }
1767
1768 @Override // Binder call
1769 public void setBrightness(int brightness) {
1770 // STOPSHIP - remove when adaptive brightness controller accepts curves.
1771 mContext.enforceCallingOrSelfPermission(
1772 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1773 "Permission to set brightness.");
1774 int userId = UserHandle.getUserId(Binder.getCallingUid());
1775 final long token = Binder.clearCallingIdentity();
1776 try {
1777 mBrightnessTracker.setBrightness(brightness, userId);
1778 } finally {
1779 Binder.restoreCallingIdentity(token);
1780 }
1781 }
1782
Jeff Brown4ccb8232014-01-16 22:16:42 -08001783 private boolean validatePackageName(int uid, String packageName) {
1784 if (packageName != null) {
1785 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1786 if (packageNames != null) {
1787 for (String n : packageNames) {
1788 if (n.equals(packageName)) {
1789 return true;
1790 }
1791 }
1792 }
1793 }
1794 return false;
1795 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001796
1797 private boolean canProjectVideo(IMediaProjection projection) {
1798 if (projection != null) {
1799 try {
1800 if (projection.canProjectVideo()) {
1801 return true;
1802 }
1803 } catch (RemoteException e) {
1804 Slog.e(TAG, "Unable to query projection service for permissions", e);
1805 }
1806 }
1807 if (mContext.checkCallingPermission(
1808 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001809 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001810 return true;
1811 }
1812 return canProjectSecureVideo(projection);
1813 }
1814
1815 private boolean canProjectSecureVideo(IMediaProjection projection) {
1816 if (projection != null) {
1817 try {
1818 if (projection.canProjectSecureVideo()){
1819 return true;
1820 }
1821 } catch (RemoteException e) {
1822 Slog.e(TAG, "Unable to query projection service for permissions", e);
1823 }
1824 }
1825 return mContext.checkCallingPermission(
1826 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001827 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001828 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001829 }
1830
1831 private final class LocalService extends DisplayManagerInternal {
1832 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001833 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001834 SensorManager sensorManager) {
1835 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001836 DisplayBlanker blanker = new DisplayBlanker() {
1837 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001838 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001839 // The order of operations is important for legacy reasons.
1840 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001841 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001842 }
1843
1844 callbacks.onDisplayStateChange(state);
1845
1846 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001847 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001848 }
1849 }
1850 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001851 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001852 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001853 }
1854 }
1855
1856 @Override
1857 public boolean requestPowerState(DisplayPowerRequest request,
1858 boolean waitForNegativeProximity) {
1859 return mDisplayPowerController.requestPowerState(request,
1860 waitForNegativeProximity);
1861 }
1862
1863 @Override
1864 public boolean isProximitySensorAvailable() {
1865 return mDisplayPowerController.isProximitySensorAvailable();
1866 }
1867
1868 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001869 public DisplayInfo getDisplayInfo(int displayId) {
1870 return getDisplayInfoInternal(displayId, Process.myUid());
1871 }
1872
1873 @Override
1874 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1875 if (listener == null) {
1876 throw new IllegalArgumentException("listener must not be null");
1877 }
1878
1879 registerDisplayTransactionListenerInternal(listener);
1880 }
1881
1882 @Override
1883 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1884 if (listener == null) {
1885 throw new IllegalArgumentException("listener must not be null");
1886 }
1887
1888 unregisterDisplayTransactionListenerInternal(listener);
1889 }
1890
1891 @Override
1892 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1893 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1894 }
1895
1896 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001897 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1898 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1899 }
1900
1901 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001902 public void performTraversalInTransactionFromWindowManager() {
1903 performTraversalInTransactionFromWindowManagerInternal();
1904 }
1905
1906 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001907 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001908 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1909 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1910 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001911 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001912
1913 @Override
1914 public void setDisplayOffsets(int displayId, int x, int y) {
1915 setDisplayOffsetsInternal(displayId, x, y);
1916 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001917
1918 @Override
1919 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1920 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1921 }
1922
1923 @Override
1924 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1925 return isUidPresentOnDisplayInternal(uid, displayId);
1926 }
Kenny Guycfe7b702017-11-14 21:04:58 +00001927
1928 @Override
1929 public void persistBrightnessSliderEvents() {
1930 mBrightnessTracker.persistEvents();
1931 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001932 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001933}