blob: 19a74d7cdeac1221c5de0628fd198aae028bde9e [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080019import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
20import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
21import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
22import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
23import static android.hardware.display.DisplayManager
Andrii Kulian7211d2e2017-01-27 15:58:05 -080024 .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080025
Santos Cordonee8931e2017-04-05 10:31:15 -070026import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060027import com.android.internal.util.DumpUtils;
Jeff Brownbd6e1502012-08-28 03:27:37 -070028import com.android.internal.util.IndentingPrintWriter;
29
Jeff Brownfa25bf52012-07-23 19:26:30 -070030import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070031import android.annotation.NonNull;
Kenny Guy29aa30e2017-11-30 13:43:46 +000032import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070033import android.content.Context;
34import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010035import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010036import android.content.res.Resources;
37import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070038import android.hardware.SensorManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010039import android.hardware.display.BrightnessChangeEvent;
Jeff Brownbd6e1502012-08-28 03:27:37 -070040import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080041import android.hardware.display.DisplayManagerInternal;
42import android.hardware.display.DisplayViewport;
43import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070044import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070045import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070046import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070047import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080048import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070049import android.media.projection.IMediaProjection;
50import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070051import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070052import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070053import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080054import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070055import android.os.Looper;
56import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070057import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080058import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070059import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070060import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070061import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070062import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070063import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010064import android.os.UserHandle;
Jeff Browna506a6e2013-06-04 00:02:38 -070065import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080066import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070067import android.util.Slog;
68import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070069import android.view.Display;
70import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070071import android.view.Surface;
72
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;
Adrian Roose99bc052017-11-20 17:55:31 +010078import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070079
80import java.io.FileDescriptor;
81import java.io.PrintWriter;
82import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070083import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070084import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080085import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070086
87/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070088 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070089 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070090 * The {@link DisplayManagerService} manages the global lifecycle of displays,
91 * decides how to configure logical displays based on the physical display devices currently
92 * attached, sends notifications to the system and to applications when the state
93 * changes, and so on.
94 * </p><p>
95 * The display manager service relies on a collection of {@link DisplayAdapter} components,
96 * for discovering and configuring physical display devices attached to the system.
97 * There are separate display adapters for each manner that devices are attached:
98 * one display adapter for built-in local displays, one for simulated non-functional
99 * displays when the system is headless, one for simulated overlay displays used for
100 * development, one for wifi displays, etc.
101 * </p><p>
102 * Display adapters are only weakly coupled to the display manager service.
103 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700104 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700105 * by the display manager service. This separation of concerns is important for
106 * two main reasons. First, it neatly encapsulates the responsibilities of these
107 * two classes: display adapters handle individual display devices whereas
108 * the display manager service handles the global state. Second, it eliminates
109 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700110 * </p>
111 *
112 * <h3>Synchronization</h3>
113 * <p>
114 * Because the display manager may be accessed by multiple threads, the synchronization
115 * story gets a little complicated. In particular, the window manager may call into
116 * the display manager while holding a surface transaction with the expectation that
117 * it can apply changes immediately. Unfortunately, that means we can't just do
118 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700119 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700120 * To make this work, all of the objects that belong to the display manager must
121 * use the same lock. We call this lock the synchronization root and it has a unique
122 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
123 * named with the "Locked" suffix.
124 * </p><p>
125 * Where things get tricky is that the display manager is not allowed to make
126 * any potentially reentrant calls, especially into the window manager. We generally
127 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700128 * </p>
129 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800130public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700131 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700132 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700133
Jeff Brownbbd28a22012-09-20 16:47:15 -0700134 // When this system property is set to 0, WFD is forcibly disabled on boot.
135 // When this system property is set to 1, WFD is forcibly enabled on boot.
136 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
137 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
138
Jeff Brownbd6e1502012-08-28 03:27:37 -0700139 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
140
Santos Cordonc22c5632017-06-21 16:03:49 -0700141 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700142 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
143 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700144 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700145 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100146 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700147
Jeff Brownb880d882014-02-10 19:47:07 -0800148 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700149 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700150 private final Handler mUiHandler;
151 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800152 private WindowManagerInternal mWindowManagerInternal;
153 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700154 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700155
156 // The synchronization root for the display manager.
157 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800158 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
159 // into WindowManagerService methods that require mWindowMap while holding this unless you are
160 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700161 private final SyncRoot mSyncRoot = new SyncRoot();
162
163 // True if in safe mode.
164 // This option may disable certain display adapters.
165 public boolean mSafeMode;
166
167 // True if we are in a special boot mode where only core applications and
168 // services should be started. This option may disable certain display adapters.
169 public boolean mOnlyCore;
170
Jeff Brown27f1d672012-10-17 18:32:34 -0700171 // True if the display manager service should pretend there is only one display
172 // and only tell applications about the existence of the default logical display.
173 // The display manager can still mirror content to secondary displays but applications
174 // cannot present unique content on those displays.
175 // Used for demonstration purposes only.
176 private final boolean mSingleDisplayDemoMode;
177
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700178 // All callback records indexed by calling process id.
179 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700180 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700181
Jeff Brownbd6e1502012-08-28 03:27:37 -0700182 // List of all currently registered display adapters.
183 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
184
185 // List of all currently connected display devices.
186 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
187
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700188 // List of all logical displays indexed by logical display id.
189 private final SparseArray<LogicalDisplay> mLogicalDisplays =
190 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700191 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
192
Jeff Brown7f3994e2012-12-04 14:04:28 -0800193 // List of all display transaction listeners.
194 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
195 new CopyOnWriteArrayList<DisplayTransactionListener>();
196
Jeff Brownad9ef192014-04-08 17:26:30 -0700197 // Display power controller.
198 private DisplayPowerController mDisplayPowerController;
199
Jeff Brown037c33e2014-04-09 00:31:55 -0700200 // The overall display state, independent of changes that might influence one
201 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700202 private int mGlobalDisplayState = Display.STATE_ON;
203
204 // The overall display brightness.
205 // For now, this only applies to the built-in display but we may split it up eventually.
206 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700207
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700208 // Set to true when there are pending display changes that have yet to be applied
209 // to the surface flinger state.
210 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700211
Jeff Browne08ae382012-09-07 20:36:36 -0700212 // The Wifi display adapter, or null if not registered.
213 private WifiDisplayAdapter mWifiDisplayAdapter;
214
Jeff Brownce468a32013-11-21 16:42:03 -0800215 // The number of active wifi display scan requests.
216 private int mWifiDisplayScanRequestCount;
217
Jeff Browna506a6e2013-06-04 00:02:38 -0700218 // The virtual display adapter, or null if not registered.
219 private VirtualDisplayAdapter mVirtualDisplayAdapter;
220
Michael Wrighteedcbf12017-08-16 23:14:54 +0100221 // The stable device screen height and width. These are not tied to a specific display, even
222 // the default display, because they need to be stable over the course of the device's entire
223 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
224 // device).
225 private Point mStableDisplaySize = new Point();
226
Jeff Brownd728bf52012-09-08 18:05:28 -0700227 // Viewports of the default display and the display that should receive touch
228 // input from an external source. Used by the input system.
229 private final DisplayViewport mDefaultViewport = new DisplayViewport();
230 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700231 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700232
Jeff Brown89d55462012-09-19 11:33:42 -0700233 // Persistent data store for all internal settings maintained by the display manager service.
234 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
235
Jeff Brownbd6e1502012-08-28 03:27:37 -0700236 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700237 // May be used outside of the lock but only on the handler thread.
238 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700239
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700240 // Temporary display info, used for comparing display configurations.
241 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
242
Jeff Brownd728bf52012-09-08 18:05:28 -0700243 // Temporary viewports, used when sending new viewport information to the
244 // input system. May be used outside of the lock but only on the handler thread.
245 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
246 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700247 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700248
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700249 // The default color mode for default displays. Overrides the usual
250 // Display.Display.COLOR_MODE_DEFAULT for displays with the
251 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
252 private final int mDefaultDisplayDefaultColorMode;
253
Jeff Browne75926d2014-09-18 15:24:49 -0700254 // Temporary list of deferred work to perform when setting the display state.
255 // Only used by requestDisplayState. The field is self-synchronized and only
256 // intended for use inside of the requestGlobalDisplayStateInternal function.
257 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
258
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800259 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
260 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
261
Santos Cordonee8931e2017-04-05 10:31:15 -0700262 private final Injector mInjector;
263
Kenny Guy22bd0442017-10-26 00:15:54 +0100264 private final BrightnessTracker mBrightnessTracker;
265
Jeff Brownb880d882014-02-10 19:47:07 -0800266 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700267 this(context, new Injector());
268 }
269
270 @VisibleForTesting
271 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800272 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700273 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800274 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800275 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700276 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700277 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700278 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700279 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
280 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700281
282 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
283 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Kenny Guy22bd0442017-10-26 00:15:54 +0100284 mBrightnessTracker = new BrightnessTracker(context, null);
Joel Fernandes2d314e12017-04-04 16:32:15 -0700285 }
286
287 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100288 // android.display and android.anim is critical to user experience and we should make sure
289 // it is not in the default foregroup groups, add it to top-app to make sure it uses all the
290 // cores and scheduling settings for top-app when it runs.
291 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
292 Process.THREAD_GROUP_TOP_APP);
293 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
294 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700295 }
296
Jeff Brown4ccb8232014-01-16 22:16:42 -0800297 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800298 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700299 // We need to pre-load the persistent data store so it's ready before the default display
300 // adapter is up so that we have it's configuration. We could load it lazily, but since
301 // 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 -0700302 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100303 synchronized(mSyncRoot) {
304 mPersistentDataStore.loadIfNeeded();
305 loadStableDisplayValuesLocked();
306 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700307 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800308
309 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
310 true /*allowIsolated*/);
311 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700312 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800313 }
314
315 @Override
316 public void onBootPhase(int phase) {
317 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
318 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700319 long timeout = SystemClock.uptimeMillis()
320 + mInjector.getDefaultDisplayDelayTimeout();
321 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
322 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800323 long delay = timeout - SystemClock.uptimeMillis();
324 if (delay <= 0) {
325 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700326 + "to be initialized. DefaultDisplay="
327 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
328 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800329 }
330 if (DEBUG) {
331 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
332 }
333 try {
334 mSyncRoot.wait(delay);
335 } catch (InterruptedException ex) {
336 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700337 }
338 }
339 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700340 }
341
Jeff Brown4ccb8232014-01-16 22:16:42 -0800342 // TODO: Use dependencies or a boot phase
343 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700344 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800345 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
346 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700347 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700348 }
349 }
350
351 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700352 * Called when the system is ready to go.
353 */
354 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700355 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700356 mSafeMode = safeMode;
357 mOnlyCore = onlyCore;
358 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700359
Jeff Brownbd6e1502012-08-28 03:27:37 -0700360 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100361 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700362 }
363
Santos Cordonee8931e2017-04-05 10:31:15 -0700364 @VisibleForTesting
365 Handler getDisplayHandler() {
366 return mHandler;
367 }
368
Michael Wrighteedcbf12017-08-16 23:14:54 +0100369 private void loadStableDisplayValuesLocked() {
370 final Point size = mPersistentDataStore.getStableDisplaySize();
371 if (size.x > 0 && size.y > 0) {
372 // Just set these values directly so we don't write the display persistent data again
373 // unnecessarily
374 mStableDisplaySize.set(size.x, size.y);
375 } else {
376 final Resources res = mContext.getResources();
377 final int width = res.getInteger(
378 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
379 final int height = res.getInteger(
380 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
381 if (width > 0 && height > 0) {
382 setStableDisplaySizeLocked(width, height);
383 }
384 }
385 }
386
387 private Point getStableDisplaySizeInternal() {
388 Point r = new Point();
389 synchronized (mSyncRoot) {
390 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
391 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
392 }
393 }
394 return r;
395 }
396
Jeff Brown4ccb8232014-01-16 22:16:42 -0800397 private void registerDisplayTransactionListenerInternal(
398 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800399 // List is self-synchronized copy-on-write.
400 mDisplayTransactionListeners.add(listener);
401 }
402
Jeff Brown4ccb8232014-01-16 22:16:42 -0800403 private void unregisterDisplayTransactionListenerInternal(
404 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800405 // List is self-synchronized copy-on-write.
406 mDisplayTransactionListeners.remove(listener);
407 }
408
Jeff Brown4ccb8232014-01-16 22:16:42 -0800409 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700410 int displayId, DisplayInfo info) {
411 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700412 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700413 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700414 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700415 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700416 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700417 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700418 }
419 }
420 }
421
Andrii Kuliancd097992017-03-23 18:31:59 -0700422 /**
423 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
424 */
425 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
426 synchronized (mSyncRoot) {
427 final LogicalDisplay display = mLogicalDisplays.get(displayId);
428 if (display != null) {
429 display.getNonOverrideDisplayInfoLocked(outInfo);
430 }
431 }
432 }
433
Santos Cordonee8931e2017-04-05 10:31:15 -0700434 @VisibleForTesting
435 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700436 synchronized (mSyncRoot) {
437 if (!mPendingTraversal) {
438 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700439 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700440 mPendingTraversal = false;
441
442 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700443 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800444
445 // List is self-synchronized copy-on-write.
446 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
447 listener.onDisplayTransaction();
448 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700449 }
450
Jeff Brown5d6443b2015-04-10 20:15:01 -0700451 private void requestGlobalDisplayStateInternal(int state, int brightness) {
452 if (state == Display.STATE_UNKNOWN) {
453 state = Display.STATE_ON;
454 }
455 if (state == Display.STATE_OFF) {
456 brightness = PowerManager.BRIGHTNESS_OFF;
457 } else if (brightness < 0) {
458 brightness = PowerManager.BRIGHTNESS_DEFAULT;
459 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
460 brightness = PowerManager.BRIGHTNESS_ON;
461 }
462
Jeff Browne75926d2014-09-18 15:24:49 -0700463 synchronized (mTempDisplayStateWorkQueue) {
464 try {
465 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700466 // Note that we do not need to schedule traversals here although it
467 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700468 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700469 if (mGlobalDisplayState == state
470 && mGlobalDisplayBrightness == brightness) {
471 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700472 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700473
474 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
475 + Display.stateToString(state)
476 + ", brightness=" + brightness + ")");
477 mGlobalDisplayState = state;
478 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700479 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700480 }
481
482 // Setting the display power state can take hundreds of milliseconds
483 // to complete so we defer the most expensive part of the work until
484 // after we have exited the critical section to avoid blocking other
485 // threads for a long time.
486 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
487 mTempDisplayStateWorkQueue.get(i).run();
488 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700489 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700490 } finally {
491 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700492 }
493 }
494 }
495
Jeff Brown4ccb8232014-01-16 22:16:42 -0800496 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700497 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800498 LogicalDisplay display = mLogicalDisplays.get(displayId);
499 if (display != null) {
500 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800501 if (info.hasAccess(callingUid)
502 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800503 return info;
504 }
505 }
506 return null;
507 }
508 }
509
510 private int[] getDisplayIdsInternal(int callingUid) {
511 synchronized (mSyncRoot) {
512 final int count = mLogicalDisplays.size();
513 int[] displayIds = new int[count];
514 int n = 0;
515 for (int i = 0; i < count; i++) {
516 LogicalDisplay display = mLogicalDisplays.valueAt(i);
517 DisplayInfo info = display.getDisplayInfoLocked();
518 if (info.hasAccess(callingUid)) {
519 displayIds[n++] = mLogicalDisplays.keyAt(i);
520 }
521 }
522 if (n != count) {
523 displayIds = Arrays.copyOfRange(displayIds, 0, n);
524 }
525 return displayIds;
526 }
527 }
528
529 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
530 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700531 if (mCallbacks.get(callingPid) != null) {
532 throw new SecurityException("The calling process has already "
533 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700534 }
535
Jeff Brownbd6e1502012-08-28 03:27:37 -0700536 CallbackRecord record = new CallbackRecord(callingPid, callback);
537 try {
538 IBinder binder = callback.asBinder();
539 binder.linkToDeath(record, 0);
540 } catch (RemoteException ex) {
541 // give up
542 throw new RuntimeException(ex);
543 }
544
545 mCallbacks.put(callingPid, record);
546 }
547 }
548
Jeff Brownce468a32013-11-21 16:42:03 -0800549 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700550 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800551 mCallbacks.remove(record.mPid);
552 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700553 }
554 }
555
Jeff Brown4ccb8232014-01-16 22:16:42 -0800556 private void startWifiDisplayScanInternal(int callingPid) {
557 synchronized (mSyncRoot) {
558 CallbackRecord record = mCallbacks.get(callingPid);
559 if (record == null) {
560 throw new IllegalStateException("The calling process has not "
561 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700562 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800563 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700564 }
565 }
566
Jeff Brownce468a32013-11-21 16:42:03 -0800567 private void startWifiDisplayScanLocked(CallbackRecord record) {
568 if (!record.mWifiDisplayScanRequested) {
569 record.mWifiDisplayScanRequested = true;
570 if (mWifiDisplayScanRequestCount++ == 0) {
571 if (mWifiDisplayAdapter != null) {
572 mWifiDisplayAdapter.requestStartScanLocked();
573 }
574 }
575 }
576 }
577
Jeff Brown4ccb8232014-01-16 22:16:42 -0800578 private void stopWifiDisplayScanInternal(int callingPid) {
579 synchronized (mSyncRoot) {
580 CallbackRecord record = mCallbacks.get(callingPid);
581 if (record == null) {
582 throw new IllegalStateException("The calling process has not "
583 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800584 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800585 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800586 }
587 }
588
589 private void stopWifiDisplayScanLocked(CallbackRecord record) {
590 if (record.mWifiDisplayScanRequested) {
591 record.mWifiDisplayScanRequested = false;
592 if (--mWifiDisplayScanRequestCount == 0) {
593 if (mWifiDisplayAdapter != null) {
594 mWifiDisplayAdapter.requestStopScanLocked();
595 }
596 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700597 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800598 + mWifiDisplayScanRequestCount);
599 mWifiDisplayScanRequestCount = 0;
600 }
601 }
602 }
603
Jeff Brown4ccb8232014-01-16 22:16:42 -0800604 private void connectWifiDisplayInternal(String address) {
605 synchronized (mSyncRoot) {
606 if (mWifiDisplayAdapter != null) {
607 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700608 }
Jeff Browne08ae382012-09-07 20:36:36 -0700609 }
610 }
611
Jeff Brown4ccb8232014-01-16 22:16:42 -0800612 private void pauseWifiDisplayInternal() {
613 synchronized (mSyncRoot) {
614 if (mWifiDisplayAdapter != null) {
615 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700616 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700617 }
618 }
619
Jeff Brown4ccb8232014-01-16 22:16:42 -0800620 private void resumeWifiDisplayInternal() {
621 synchronized (mSyncRoot) {
622 if (mWifiDisplayAdapter != null) {
623 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700624 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700625 }
626 }
627
Jeff Brown4ccb8232014-01-16 22:16:42 -0800628 private void disconnectWifiDisplayInternal() {
629 synchronized (mSyncRoot) {
630 if (mWifiDisplayAdapter != null) {
631 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700632 }
Jeff Browne08ae382012-09-07 20:36:36 -0700633 }
634 }
635
Jeff Brown4ccb8232014-01-16 22:16:42 -0800636 private void renameWifiDisplayInternal(String address, String alias) {
637 synchronized (mSyncRoot) {
638 if (mWifiDisplayAdapter != null) {
639 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700640 }
Jeff Brown89d55462012-09-19 11:33:42 -0700641 }
642 }
643
Jeff Brown4ccb8232014-01-16 22:16:42 -0800644 private void forgetWifiDisplayInternal(String address) {
645 synchronized (mSyncRoot) {
646 if (mWifiDisplayAdapter != null) {
647 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700648 }
Jeff Brown89d55462012-09-19 11:33:42 -0700649 }
650 }
651
Jeff Brown4ccb8232014-01-16 22:16:42 -0800652 private WifiDisplayStatus getWifiDisplayStatusInternal() {
653 synchronized (mSyncRoot) {
654 if (mWifiDisplayAdapter != null) {
655 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700656 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800657 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700658 }
659 }
660
Michael Wright1c9977b2016-07-12 13:30:10 -0700661 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100662 synchronized (mSyncRoot) {
663 LogicalDisplay display = mLogicalDisplays.get(displayId);
664 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700665 display.getRequestedColorModeLocked() != colorMode) {
666 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100667 scheduleTraversalLocked(false);
668 }
669 }
670 }
671
Michael Wright75ee9fc2014-09-01 19:55:22 -0700672 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700673 IMediaProjection projection, int callingUid, String packageName, String name, int width,
674 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800675 synchronized (mSyncRoot) {
676 if (mVirtualDisplayAdapter == null) {
677 Slog.w(TAG, "Rejecting request to create private virtual display "
678 + "because the virtual display adapter is not available.");
679 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700680 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800681
682 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700683 callback, projection, callingUid, packageName, name, width, height, densityDpi,
684 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800685 if (device == null) {
686 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700687 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700688
Jeff Brown4ccb8232014-01-16 22:16:42 -0800689 handleDisplayDeviceAddedLocked(device);
690 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
691 if (display != null) {
692 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700693 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800694
695 // Something weird happened and the logical display was not created.
696 Slog.w(TAG, "Rejecting request to create virtual display "
697 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700698 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800699 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700700 }
701 return -1;
702 }
703
Michael Wright01e840f2014-06-26 16:03:25 -0700704 private void resizeVirtualDisplayInternal(IBinder appToken,
705 int width, int height, int densityDpi) {
706 synchronized (mSyncRoot) {
707 if (mVirtualDisplayAdapter == null) {
708 return;
709 }
710
711 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
712 }
713 }
714
Jeff Brown92207df2014-04-16 13:16:07 -0700715 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
716 synchronized (mSyncRoot) {
717 if (mVirtualDisplayAdapter == null) {
718 return;
719 }
720
721 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
722 }
723 }
724
Jeff Brown4ccb8232014-01-16 22:16:42 -0800725 private void releaseVirtualDisplayInternal(IBinder appToken) {
726 synchronized (mSyncRoot) {
727 if (mVirtualDisplayAdapter == null) {
728 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700729 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700730
Jeff Brown4ccb8232014-01-16 22:16:42 -0800731 DisplayDevice device =
732 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
733 if (device != null) {
734 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700735 }
736 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700737 }
738
Santos Cordonc22c5632017-06-21 16:03:49 -0700739 private void registerDefaultDisplayAdapters() {
740 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700741 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700742 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800743 registerDisplayAdapterLocked(new LocalDisplayAdapter(
744 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700745
746 // Standalone VR devices rely on a virtual display as their primary display for
747 // 2D UI. We register virtual display adapter along side the main display adapter
748 // here so that it is ready by the time the system sends the home Intent for
749 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
750 // the virtual display inside VR before any VR-specific apps even run.
751 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
752 mHandler, mDisplayAdapterListener);
753 if (mVirtualDisplayAdapter != null) {
754 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
755 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700756 }
757 }
758
759 private void registerAdditionalDisplayAdapters() {
760 synchronized (mSyncRoot) {
761 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700762 registerOverlayDisplayAdapterLocked();
763 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700764 }
765 }
766 }
767
Jeff Brown89d55462012-09-19 11:33:42 -0700768 private void registerOverlayDisplayAdapterLocked() {
769 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
770 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
771 }
772
773 private void registerWifiDisplayAdapterLocked() {
774 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700775 com.android.internal.R.bool.config_enableWifiDisplay)
776 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700777 mWifiDisplayAdapter = new WifiDisplayAdapter(
778 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
779 mPersistentDataStore);
780 registerDisplayAdapterLocked(mWifiDisplayAdapter);
781 }
782 }
783
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700784 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
785 // In safe mode, we disable non-essential display adapters to give the user
786 // an opportunity to fix broken settings or other problems that might affect
787 // system stability.
788 // In only-core mode, we disable non-essential display adapters to minimize
789 // the number of dependencies that are started while in this mode and to
790 // prevent problems that might occur due to the device being encrypted.
791 return !mSafeMode && !mOnlyCore;
792 }
793
794 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
795 mDisplayAdapters.add(adapter);
796 adapter.registerLocked();
797 }
798
Jeff Brownbd6e1502012-08-28 03:27:37 -0700799 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700800 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700801 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700802 }
803 }
804
Jeff Browna506a6e2013-06-04 00:02:38 -0700805 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700806 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700807 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700808 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700809 return;
810 }
811
Jeff Brown10acf6d2015-04-14 14:20:47 -0700812 Slog.i(TAG, "Display device added: " + info);
813 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700814
815 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700816 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700817 Runnable work = updateDisplayStateLocked(device);
818 if (work != null) {
819 work.run();
820 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700821 scheduleTraversalLocked(false);
822 }
823
Jeff Brownbd6e1502012-08-28 03:27:37 -0700824 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700825 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700826 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700827 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700828 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700829 return;
830 }
831
Jeff Brown10acf6d2015-04-14 14:20:47 -0700832 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
833 if (diff == DisplayDeviceInfo.DIFF_STATE) {
834 Slog.i(TAG, "Display device changed state: \"" + info.name
835 + "\", " + Display.stateToString(info.state));
836 } else if (diff != 0) {
837 Slog.i(TAG, "Display device changed: " + info);
838 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700839 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
840 try {
841 mPersistentDataStore.setColorMode(device, info.colorMode);
842 } finally {
843 mPersistentDataStore.saveIfNeeded();
844 }
845 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700846 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700847
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700848 device.applyPendingDisplayDeviceInfoChangesLocked();
849 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700850 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700851 }
852 }
853 }
854
Jeff Brownbd6e1502012-08-28 03:27:37 -0700855 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700856 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700857 handleDisplayDeviceRemovedLocked(device);
858 }
859 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700860
Jeff Browna506a6e2013-06-04 00:02:38 -0700861 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700862 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700863 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700864 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700865 return;
866 }
867
Jeff Brown10acf6d2015-04-14 14:20:47 -0700868 Slog.i(TAG, "Display device removed: " + info);
869 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700870
Jeff Browna506a6e2013-06-04 00:02:38 -0700871 updateLogicalDisplaysLocked();
872 scheduleTraversalLocked(false);
873 }
874
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700875 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700876 final int count = mDisplayDevices.size();
877 for (int i = 0; i < count; i++) {
878 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700879 Runnable runnable = updateDisplayStateLocked(device);
880 if (runnable != null) {
881 workQueue.add(runnable);
882 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700883 }
884 }
885
Jeff Browne75926d2014-09-18 15:24:49 -0700886 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700887 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700888 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700889 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
890 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700891 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700892 }
Jeff Browne75926d2014-09-18 15:24:49 -0700893 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700894 }
895
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700896 // Adds a new logical display based on the given display device.
897 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700898 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700899 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
900 boolean isDefault = (deviceInfo.flags
901 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
902 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
903 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
904 isDefault = false;
905 }
906
Jeff Brown27f1d672012-10-17 18:32:34 -0700907 if (!isDefault && mSingleDisplayDemoMode) {
908 Slog.i(TAG, "Not creating a logical display for a secondary display "
909 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700910 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700911 }
912
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700913 final int displayId = assignDisplayIdLocked(isDefault);
914 final int layerStack = assignLayerStackLocked(displayId);
915
Jeff Brownd728bf52012-09-08 18:05:28 -0700916 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700917 display.updateLocked(mDisplayDevices);
918 if (!display.isValidLocked()) {
919 // This should never happen currently.
920 Slog.w(TAG, "Ignoring display device because the logical display "
921 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700922 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700923 }
924
Michael Wrighteedcbf12017-08-16 23:14:54 +0100925 configureColorModeLocked(display, device);
926 if (isDefault) {
927 recordStableDisplayStatsIfNeededLocked(display);
928 }
929
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700930 mLogicalDisplays.put(displayId, display);
931
932 // Wake up waitForDefaultDisplay.
933 if (isDefault) {
934 mSyncRoot.notifyAll();
935 }
936
937 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700938 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700939 }
940
941 private int assignDisplayIdLocked(boolean isDefault) {
942 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
943 }
944
945 private int assignLayerStackLocked(int displayId) {
946 // Currently layer stacks and display ids are the same.
947 // This need not be the case.
948 return displayId;
949 }
950
Michael Wrighteedcbf12017-08-16 23:14:54 +0100951 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
952 if (display.getPrimaryDisplayDeviceLocked() == device) {
953 int colorMode = mPersistentDataStore.getColorMode(device);
954 if (colorMode == Display.COLOR_MODE_INVALID) {
955 if ((device.getDisplayDeviceInfoLocked().flags
956 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
957 colorMode = mDefaultDisplayDefaultColorMode;
958 } else {
959 colorMode = Display.COLOR_MODE_DEFAULT;
960 }
961 }
962 display.setRequestedColorModeLocked(colorMode);
963 }
964 }
965
966 // If we've never recorded stable device stats for this device before and they aren't
967 // explicitly configured, go ahead and record the stable device stats now based on the status
968 // of the default display at first boot.
969 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
970 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
971 DisplayInfo info = d.getDisplayInfoLocked();
972 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
973 }
974 }
975
976 private void setStableDisplaySizeLocked(int width, int height) {
977 mStableDisplaySize = new Point(width, height);
978 try {
979 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
980 } finally {
981 mPersistentDataStore.saveIfNeeded();
982 }
983 }
984
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700985 // Updates all existing logical displays given the current set of display devices.
986 // Removes invalid logical displays.
987 // Sends notifications if needed.
988 private boolean updateLogicalDisplaysLocked() {
989 boolean changed = false;
990 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
991 final int displayId = mLogicalDisplays.keyAt(i);
992 LogicalDisplay display = mLogicalDisplays.valueAt(i);
993
994 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
995 display.updateLocked(mDisplayDevices);
996 if (!display.isValidLocked()) {
997 mLogicalDisplays.removeAt(i);
998 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
999 changed = true;
1000 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1001 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1002 changed = true;
1003 }
1004 }
1005 return changed;
1006 }
1007
1008 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -07001009 // Clear all viewports before configuring displays so that we can keep
1010 // track of which ones we have configured.
1011 clearViewportsLocked();
1012
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001013 // Configure each display device.
1014 final int count = mDisplayDevices.size();
1015 for (int i = 0; i < count; i++) {
1016 DisplayDevice device = mDisplayDevices.get(i);
1017 configureDisplayInTransactionLocked(device);
1018 device.performTraversalInTransactionLocked();
1019 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001020
1021 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001022 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001023 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1024 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001025 }
1026
Michael Wright3f145a22014-07-22 19:46:03 -07001027 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001028 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001029 synchronized (mSyncRoot) {
1030 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001031 if (display == null) {
1032 return;
1033 }
1034 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001035 if (DEBUG) {
1036 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1037 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1038 }
1039
Craig Mautner722285e2012-09-07 13:55:58 -07001040 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001041 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001042 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001043 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1044 // Scan supported modes returned by display.getInfo() to find a mode with the same
1045 // size as the default display mode but with the specified refresh rate instead.
1046 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1047 requestedRefreshRate);
1048 }
1049 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001050 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001051 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001052 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001053 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001054 scheduleTraversalLocked(inTraversal);
1055 }
Craig Mautner722285e2012-09-07 13:55:58 -07001056 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001057 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001058
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001059 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1060 synchronized (mSyncRoot) {
1061 LogicalDisplay display = mLogicalDisplays.get(displayId);
1062 if (display == null) {
1063 return;
1064 }
1065 if (display.getDisplayOffsetXLocked() != x
1066 || display.getDisplayOffsetYLocked() != y) {
1067 if (DEBUG) {
1068 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1069 + x + ", " + y + ")");
1070 }
1071 display.setDisplayOffsetsLocked(x, y);
1072 scheduleTraversalLocked(false);
1073 }
1074 }
1075 }
1076
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001077 // Updates the lists of UIDs that are present on displays.
1078 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1079 synchronized (mSyncRoot) {
1080 mDisplayAccessUIDs.clear();
1081 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1082 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1083 newDisplayAccessUIDs.valueAt(i));
1084 }
1085 }
1086 }
1087
1088 // Checks if provided UID's content is present on the display and UID has access to it.
1089 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1090 synchronized (mSyncRoot) {
1091 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1092 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1093 }
1094 }
1095
Jeff Brownd728bf52012-09-08 18:05:28 -07001096 private void clearViewportsLocked() {
1097 mDefaultViewport.valid = false;
1098 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001099 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001100 }
1101
1102 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001103 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1104 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001105
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001106 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001107 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001108 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001109 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001110 if (display != null && !display.hasContentLocked()) {
1111 // If the display does not have any content of its own, then
1112 // automatically mirror the default logical display contents.
1113 display = null;
1114 }
1115 if (display == null) {
1116 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1117 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001118 }
1119
1120 // Apply the logical display configuration to the display device.
1121 if (display == null) {
1122 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001123 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001124 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001125 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001126 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001127 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001128
1129 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001130 if (!mDefaultViewport.valid
1131 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1132 setViewportLocked(mDefaultViewport, display, device);
1133 }
1134 if (!mExternalTouchViewport.valid
1135 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1136 setViewportLocked(mExternalTouchViewport, display, device);
1137 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001138
1139 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1140 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1141 setViewportLocked(viewport, display, device);
1142 }
1143 }
1144
1145 /** Gets the virtual device viewport or creates it if not yet created. */
1146 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1147 DisplayViewport viewport;
1148 final int count = mVirtualTouchViewports.size();
1149 for (int i = 0; i < count; i++) {
1150 viewport = mVirtualTouchViewports.get(i);
1151 if (uniqueId.equals(viewport.uniqueId)) {
1152 return viewport;
1153 }
1154 }
1155
1156 viewport = new DisplayViewport();
1157 viewport.uniqueId = uniqueId;
1158 mVirtualTouchViewports.add(viewport);
1159 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001160 }
1161
1162 private static void setViewportLocked(DisplayViewport viewport,
1163 LogicalDisplay display, DisplayDevice device) {
1164 viewport.valid = true;
1165 viewport.displayId = display.getDisplayIdLocked();
1166 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001167 }
1168
1169 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1170 final int count = mLogicalDisplays.size();
1171 for (int i = 0; i < count; i++) {
1172 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1173 if (display.getPrimaryDisplayDeviceLocked() == device) {
1174 return display;
1175 }
1176 }
1177 return null;
1178 }
1179
Jeff Brownbd6e1502012-08-28 03:27:37 -07001180 private void sendDisplayEventLocked(int displayId, int event) {
1181 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1182 mHandler.sendMessage(msg);
1183 }
1184
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001185 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1186 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001187 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001188 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001189 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001190 if (!inTraversal) {
1191 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1192 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001193 }
1194 }
1195
1196 // Runs on Handler thread.
1197 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001198 private void deliverDisplayEvent(int displayId, int event) {
1199 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001200 Slog.d(TAG, "Delivering display event: displayId="
1201 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001202 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001203
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001204 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001205 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001206 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001207 count = mCallbacks.size();
1208 mTempCallbacks.clear();
1209 for (int i = 0; i < count; i++) {
1210 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001211 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001212 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001213
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001214 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001215 for (int i = 0; i < count; i++) {
1216 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1217 }
1218 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001219 }
1220
Michael Wrightc39d47a2014-07-08 18:07:36 -07001221 private IMediaProjectionManager getProjectionService() {
1222 if (mProjectionService == null) {
1223 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1224 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1225 }
1226 return mProjectionService;
1227 }
1228
Jeff Brown4ccb8232014-01-16 22:16:42 -08001229 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001230 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001231
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001232 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001233 pw.println(" mOnlyCode=" + mOnlyCore);
1234 pw.println(" mSafeMode=" + mSafeMode);
1235 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001236 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001237 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1238 pw.println(" mDefaultViewport=" + mDefaultViewport);
1239 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001240 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001241 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001242 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001243 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001244 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1245
Jeff Brown9e316a12012-10-08 19:17:06 -07001246
Jeff Brownbd6e1502012-08-28 03:27:37 -07001247 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001248 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001249
1250 pw.println();
1251 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001252 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001253 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001254 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001255 }
Craig Mautner9de49362012-08-02 14:30:30 -07001256
Jeff Brownbd6e1502012-08-28 03:27:37 -07001257 pw.println();
1258 pw.println("Display Devices: size=" + mDisplayDevices.size());
1259 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001260 pw.println(" " + device.getDisplayDeviceInfoLocked());
1261 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001262 }
1263
1264 final int logicalDisplayCount = mLogicalDisplays.size();
1265 pw.println();
1266 pw.println("Logical Displays: size=" + logicalDisplayCount);
1267 for (int i = 0; i < logicalDisplayCount; i++) {
1268 int displayId = mLogicalDisplays.keyAt(i);
1269 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1270 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001271 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001272 }
Jeff Brownce468a32013-11-21 16:42:03 -08001273
1274 final int callbackCount = mCallbacks.size();
1275 pw.println();
1276 pw.println("Callbacks: size=" + callbackCount);
1277 for (int i = 0; i < callbackCount; i++) {
1278 CallbackRecord callback = mCallbacks.valueAt(i);
1279 pw.println(" " + i + ": mPid=" + callback.mPid
1280 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1281 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001282
1283 if (mDisplayPowerController != null) {
1284 mDisplayPowerController.dump(pw);
1285 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001286
1287 pw.println();
1288 mPersistentDataStore.dump(pw);
Kenny Guycfe7b702017-11-14 21:04:58 +00001289
1290 pw.println();
1291 mBrightnessTracker.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001292 }
1293 }
1294
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001295 /**
1296 * This is the object that everything in the display manager locks on.
1297 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1298 * clear that the object belongs to the display manager service and that it is
1299 * a unique object with a special purpose.
1300 */
1301 public static final class SyncRoot {
1302 }
1303
Santos Cordonee8931e2017-04-05 10:31:15 -07001304 @VisibleForTesting
1305 static class Injector {
1306 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1307 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1308 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1309 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001310
1311 long getDefaultDisplayDelayTimeout() {
1312 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1313 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001314 }
1315
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001316 @VisibleForTesting
1317 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1318 synchronized (mSyncRoot) {
1319 LogicalDisplay display = mLogicalDisplays.get(displayId);
1320 if (display != null) {
1321 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1322 return displayDevice.getDisplayDeviceInfoLocked();
1323 }
1324 return null;
1325 }
1326 }
1327
Jeff Brownbd6e1502012-08-28 03:27:37 -07001328 private final class DisplayManagerHandler extends Handler {
1329 public DisplayManagerHandler(Looper looper) {
1330 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001331 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001332
Jeff Brownbd6e1502012-08-28 03:27:37 -07001333 @Override
1334 public void handleMessage(Message msg) {
1335 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001336 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1337 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001338 break;
1339
1340 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1341 registerAdditionalDisplayAdapters();
1342 break;
1343
1344 case MSG_DELIVER_DISPLAY_EVENT:
1345 deliverDisplayEvent(msg.arg1, msg.arg2);
1346 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001347
1348 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001349 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001350 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001351
1352 case MSG_UPDATE_VIEWPORT: {
1353 synchronized (mSyncRoot) {
1354 mTempDefaultViewport.copyFrom(mDefaultViewport);
1355 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001356 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1357 mTempVirtualTouchViewports.clear();
1358 for (DisplayViewport d : mVirtualTouchViewports) {
1359 mTempVirtualTouchViewports.add(d.makeCopy());
1360 }
1361 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001362 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001363 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1364 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001365 break;
1366 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001367
1368 case MSG_REGISTER_BRIGHTNESS_TRACKER:
1369 mBrightnessTracker.start();
1370 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001371 }
1372 }
1373 }
1374
1375 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1376 @Override
1377 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1378 switch (event) {
1379 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1380 handleDisplayDeviceAdded(device);
1381 break;
1382
1383 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1384 handleDisplayDeviceChanged(device);
1385 break;
1386
1387 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1388 handleDisplayDeviceRemoved(device);
1389 break;
1390 }
1391 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001392
1393 @Override
1394 public void onTraversalRequested() {
1395 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001396 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001397 }
1398 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001399 }
1400
1401 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001402 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001403 private final IDisplayManagerCallback mCallback;
1404
Jeff Brownce468a32013-11-21 16:42:03 -08001405 public boolean mWifiDisplayScanRequested;
1406
Jeff Brownbd6e1502012-08-28 03:27:37 -07001407 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1408 mPid = pid;
1409 mCallback = callback;
1410 }
1411
1412 @Override
1413 public void binderDied() {
1414 if (DEBUG) {
1415 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1416 }
Jeff Brownce468a32013-11-21 16:42:03 -08001417 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001418 }
1419
1420 public void notifyDisplayEventAsync(int displayId, int event) {
1421 try {
1422 mCallback.onDisplayEvent(displayId, event);
1423 } catch (RemoteException ex) {
1424 Slog.w(TAG, "Failed to notify process "
1425 + mPid + " that displays changed, assuming it died.", ex);
1426 binderDied();
1427 }
1428 }
1429 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001430
Santos Cordonee8931e2017-04-05 10:31:15 -07001431 @VisibleForTesting
1432 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001433 /**
1434 * Returns information about the specified logical display.
1435 *
1436 * @param displayId The logical display id.
1437 * @return The logical display info, or null if the display does not exist. The
1438 * returned object must be treated as immutable.
1439 */
1440 @Override // Binder call
1441 public DisplayInfo getDisplayInfo(int displayId) {
1442 final int callingUid = Binder.getCallingUid();
1443 final long token = Binder.clearCallingIdentity();
1444 try {
1445 return getDisplayInfoInternal(displayId, callingUid);
1446 } finally {
1447 Binder.restoreCallingIdentity(token);
1448 }
1449 }
1450
1451 /**
1452 * Returns the list of all display ids.
1453 */
1454 @Override // Binder call
1455 public int[] getDisplayIds() {
1456 final int callingUid = Binder.getCallingUid();
1457 final long token = Binder.clearCallingIdentity();
1458 try {
1459 return getDisplayIdsInternal(callingUid);
1460 } finally {
1461 Binder.restoreCallingIdentity(token);
1462 }
1463 }
1464
Michael Wrighteedcbf12017-08-16 23:14:54 +01001465 /**
1466 * Returns the stable device display size, in pixels.
1467 */
1468 @Override // Binder call
1469 public Point getStableDisplaySize() {
1470 final long token = Binder.clearCallingIdentity();
1471 try {
1472 return getStableDisplaySizeInternal();
1473 } finally {
1474 Binder.restoreCallingIdentity(token);
1475 }
1476 }
1477
Jeff Brown4ccb8232014-01-16 22:16:42 -08001478 @Override // Binder call
1479 public void registerCallback(IDisplayManagerCallback callback) {
1480 if (callback == null) {
1481 throw new IllegalArgumentException("listener must not be null");
1482 }
1483
1484 final int callingPid = Binder.getCallingPid();
1485 final long token = Binder.clearCallingIdentity();
1486 try {
1487 registerCallbackInternal(callback, callingPid);
1488 } finally {
1489 Binder.restoreCallingIdentity(token);
1490 }
1491 }
1492
1493 @Override // Binder call
1494 public void startWifiDisplayScan() {
1495 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1496 "Permission required to start wifi display scans");
1497
1498 final int callingPid = Binder.getCallingPid();
1499 final long token = Binder.clearCallingIdentity();
1500 try {
1501 startWifiDisplayScanInternal(callingPid);
1502 } finally {
1503 Binder.restoreCallingIdentity(token);
1504 }
1505 }
1506
1507 @Override // Binder call
1508 public void stopWifiDisplayScan() {
1509 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1510 "Permission required to stop wifi display scans");
1511
1512 final int callingPid = Binder.getCallingPid();
1513 final long token = Binder.clearCallingIdentity();
1514 try {
1515 stopWifiDisplayScanInternal(callingPid);
1516 } finally {
1517 Binder.restoreCallingIdentity(token);
1518 }
1519 }
1520
1521 @Override // Binder call
1522 public void connectWifiDisplay(String address) {
1523 if (address == null) {
1524 throw new IllegalArgumentException("address must not be null");
1525 }
1526 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1527 "Permission required to connect to a wifi display");
1528
1529 final long token = Binder.clearCallingIdentity();
1530 try {
1531 connectWifiDisplayInternal(address);
1532 } finally {
1533 Binder.restoreCallingIdentity(token);
1534 }
1535 }
1536
1537 @Override // Binder call
1538 public void disconnectWifiDisplay() {
1539 // This request does not require special permissions.
1540 // Any app can request disconnection from the currently active wifi display.
1541 // This exception should no longer be needed once wifi display control moves
1542 // to the media router service.
1543
1544 final long token = Binder.clearCallingIdentity();
1545 try {
1546 disconnectWifiDisplayInternal();
1547 } finally {
1548 Binder.restoreCallingIdentity(token);
1549 }
1550 }
1551
1552 @Override // Binder call
1553 public void renameWifiDisplay(String address, String alias) {
1554 if (address == null) {
1555 throw new IllegalArgumentException("address must not be null");
1556 }
1557 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1558 "Permission required to rename to a wifi display");
1559
1560 final long token = Binder.clearCallingIdentity();
1561 try {
1562 renameWifiDisplayInternal(address, alias);
1563 } finally {
1564 Binder.restoreCallingIdentity(token);
1565 }
1566 }
1567
1568 @Override // Binder call
1569 public void forgetWifiDisplay(String address) {
1570 if (address == null) {
1571 throw new IllegalArgumentException("address must not be null");
1572 }
1573 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1574 "Permission required to forget to a wifi display");
1575
1576 final long token = Binder.clearCallingIdentity();
1577 try {
1578 forgetWifiDisplayInternal(address);
1579 } finally {
1580 Binder.restoreCallingIdentity(token);
1581 }
1582 }
1583
1584 @Override // Binder call
1585 public void pauseWifiDisplay() {
1586 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1587 "Permission required to pause a wifi display session");
1588
1589 final long token = Binder.clearCallingIdentity();
1590 try {
1591 pauseWifiDisplayInternal();
1592 } finally {
1593 Binder.restoreCallingIdentity(token);
1594 }
1595 }
1596
1597 @Override // Binder call
1598 public void resumeWifiDisplay() {
1599 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1600 "Permission required to resume a wifi display session");
1601
1602 final long token = Binder.clearCallingIdentity();
1603 try {
1604 resumeWifiDisplayInternal();
1605 } finally {
1606 Binder.restoreCallingIdentity(token);
1607 }
1608 }
1609
1610 @Override // Binder call
1611 public WifiDisplayStatus getWifiDisplayStatus() {
1612 // This request does not require special permissions.
1613 // Any app can get information about available wifi displays.
1614
1615 final long token = Binder.clearCallingIdentity();
1616 try {
1617 return getWifiDisplayStatusInternal();
1618 } finally {
1619 Binder.restoreCallingIdentity(token);
1620 }
1621 }
1622
1623 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001624 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001625 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001626 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1627 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001628 final long token = Binder.clearCallingIdentity();
1629 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001630 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001631 } finally {
1632 Binder.restoreCallingIdentity(token);
1633 }
1634 }
1635
1636 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001637 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001638 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001639 int width, int height, int densityDpi, Surface surface, int flags,
1640 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001641 final int callingUid = Binder.getCallingUid();
1642 if (!validatePackageName(callingUid, packageName)) {
1643 throw new SecurityException("packageName must match the calling uid");
1644 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001645 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001646 throw new IllegalArgumentException("appToken must not be null");
1647 }
1648 if (TextUtils.isEmpty(name)) {
1649 throw new IllegalArgumentException("name must be non-null and non-empty");
1650 }
1651 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1652 throw new IllegalArgumentException("width, height, and densityDpi must be "
1653 + "greater than 0");
1654 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001655 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001656 throw new IllegalArgumentException("Surface can't be single-buffered");
1657 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001658
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001659 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1660 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1661
1662 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001663 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001664 throw new IllegalArgumentException(
1665 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1666 }
Michael Wright6720be42014-07-29 19:14:16 -07001667 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001668 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1669 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001670 }
1671
Michael Wrightc39d47a2014-07-08 18:07:36 -07001672 if (projection != null) {
1673 try {
1674 if (!getProjectionService().isValidMediaProjection(projection)) {
1675 throw new SecurityException("Invalid media projection");
1676 }
Michael Wright6720be42014-07-29 19:14:16 -07001677 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001678 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001679 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001680 }
1681 }
1682
Michael Wright6720be42014-07-29 19:14:16 -07001683 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001684 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001685 if (!canProjectVideo(projection)) {
1686 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1687 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1688 + "MediaProjection token in order to create a screen sharing virtual "
1689 + "display.");
1690 }
1691 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001692 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001693 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001694 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001695 + "or an appropriate MediaProjection token to create a "
1696 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001697 }
1698 }
1699
1700 final long token = Binder.clearCallingIdentity();
1701 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001702 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1703 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001704 } finally {
1705 Binder.restoreCallingIdentity(token);
1706 }
1707 }
1708
1709 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001710 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001711 int width, int height, int densityDpi) {
1712 final long token = Binder.clearCallingIdentity();
1713 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001714 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001715 } finally {
1716 Binder.restoreCallingIdentity(token);
1717 }
1718 }
1719
1720 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001721 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001722 if (surface != null && surface.isSingleBuffered()) {
1723 throw new IllegalArgumentException("Surface can't be single-buffered");
1724 }
Jeff Brown92207df2014-04-16 13:16:07 -07001725 final long token = Binder.clearCallingIdentity();
1726 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001727 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001728 } finally {
1729 Binder.restoreCallingIdentity(token);
1730 }
1731 }
1732
1733 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001734 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001735 final long token = Binder.clearCallingIdentity();
1736 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001737 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001738 } finally {
1739 Binder.restoreCallingIdentity(token);
1740 }
1741 }
1742
1743 @Override // Binder call
1744 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001745 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001746
1747 final long token = Binder.clearCallingIdentity();
1748 try {
1749 dumpInternal(pw);
1750 } finally {
1751 Binder.restoreCallingIdentity(token);
1752 }
1753 }
1754
Kenny Guy22bd0442017-10-26 00:15:54 +01001755 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001756 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001757 mContext.enforceCallingOrSelfPermission(
1758 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1759 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001760
1761 final int callingUid = Binder.getCallingUid();
1762 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
1763 final int mode = appOpsManager.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
1764 callingUid, callingPackage);
1765 final boolean hasUsageStats;
1766 if (mode == AppOpsManager.MODE_DEFAULT) {
1767 // The default behavior here is to check if PackageManager has given the app
1768 // permission.
1769 hasUsageStats = mContext.checkCallingPermission(
1770 Manifest.permission.PACKAGE_USAGE_STATS)
1771 == PackageManager.PERMISSION_GRANTED;
1772 } else {
1773 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1774 }
1775
1776 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001777 final long token = Binder.clearCallingIdentity();
1778 try {
Kenny Guy29aa30e2017-11-30 13:43:46 +00001779 return mBrightnessTracker.getEvents(userId, hasUsageStats);
Kenny Guy22bd0442017-10-26 00:15:54 +01001780 } finally {
1781 Binder.restoreCallingIdentity(token);
1782 }
1783 }
1784
1785 @Override // Binder call
1786 public void setBrightness(int brightness) {
1787 // STOPSHIP - remove when adaptive brightness controller accepts curves.
1788 mContext.enforceCallingOrSelfPermission(
1789 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1790 "Permission to set brightness.");
1791 int userId = UserHandle.getUserId(Binder.getCallingUid());
1792 final long token = Binder.clearCallingIdentity();
1793 try {
1794 mBrightnessTracker.setBrightness(brightness, userId);
1795 } finally {
1796 Binder.restoreCallingIdentity(token);
1797 }
1798 }
1799
Jeff Brown4ccb8232014-01-16 22:16:42 -08001800 private boolean validatePackageName(int uid, String packageName) {
1801 if (packageName != null) {
1802 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1803 if (packageNames != null) {
1804 for (String n : packageNames) {
1805 if (n.equals(packageName)) {
1806 return true;
1807 }
1808 }
1809 }
1810 }
1811 return false;
1812 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001813
1814 private boolean canProjectVideo(IMediaProjection projection) {
1815 if (projection != null) {
1816 try {
1817 if (projection.canProjectVideo()) {
1818 return true;
1819 }
1820 } catch (RemoteException e) {
1821 Slog.e(TAG, "Unable to query projection service for permissions", e);
1822 }
1823 }
1824 if (mContext.checkCallingPermission(
1825 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001826 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001827 return true;
1828 }
1829 return canProjectSecureVideo(projection);
1830 }
1831
1832 private boolean canProjectSecureVideo(IMediaProjection projection) {
1833 if (projection != null) {
1834 try {
1835 if (projection.canProjectSecureVideo()){
1836 return true;
1837 }
1838 } catch (RemoteException e) {
1839 Slog.e(TAG, "Unable to query projection service for permissions", e);
1840 }
1841 }
1842 return mContext.checkCallingPermission(
1843 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001844 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001845 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001846 }
1847
1848 private final class LocalService extends DisplayManagerInternal {
1849 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001850 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001851 SensorManager sensorManager) {
1852 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001853 DisplayBlanker blanker = new DisplayBlanker() {
1854 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001855 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001856 // The order of operations is important for legacy reasons.
1857 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001858 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001859 }
1860
1861 callbacks.onDisplayStateChange(state);
1862
1863 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001864 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001865 }
1866 }
1867 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001868 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001869 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001870 }
1871 }
1872
1873 @Override
1874 public boolean requestPowerState(DisplayPowerRequest request,
1875 boolean waitForNegativeProximity) {
1876 return mDisplayPowerController.requestPowerState(request,
1877 waitForNegativeProximity);
1878 }
1879
1880 @Override
1881 public boolean isProximitySensorAvailable() {
1882 return mDisplayPowerController.isProximitySensorAvailable();
1883 }
1884
1885 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001886 public DisplayInfo getDisplayInfo(int displayId) {
1887 return getDisplayInfoInternal(displayId, Process.myUid());
1888 }
1889
1890 @Override
1891 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1892 if (listener == null) {
1893 throw new IllegalArgumentException("listener must not be null");
1894 }
1895
1896 registerDisplayTransactionListenerInternal(listener);
1897 }
1898
1899 @Override
1900 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1901 if (listener == null) {
1902 throw new IllegalArgumentException("listener must not be null");
1903 }
1904
1905 unregisterDisplayTransactionListenerInternal(listener);
1906 }
1907
1908 @Override
1909 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1910 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1911 }
1912
1913 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001914 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1915 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1916 }
1917
1918 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001919 public void performTraversalInTransactionFromWindowManager() {
1920 performTraversalInTransactionFromWindowManagerInternal();
1921 }
1922
1923 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001924 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001925 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1926 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1927 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001928 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001929
1930 @Override
1931 public void setDisplayOffsets(int displayId, int x, int y) {
1932 setDisplayOffsetsInternal(displayId, x, y);
1933 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001934
1935 @Override
1936 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1937 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1938 }
1939
1940 @Override
1941 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1942 return isUidPresentOnDisplayInternal(uid, displayId);
1943 }
Kenny Guycfe7b702017-11-14 21:04:58 +00001944
1945 @Override
1946 public void persistBrightnessSliderEvents() {
1947 mBrightnessTracker.persistEvents();
1948 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001949 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001950}