blob: c1bfa478ee89a503073fe392efd3cfe3e958b016 [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;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020079import com.android.server.wm.SurfaceAnimationThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070080
81import java.io.FileDescriptor;
82import java.io.PrintWriter;
83import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070084import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070085import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080086import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070087
88/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070089 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070090 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070091 * The {@link DisplayManagerService} manages the global lifecycle of displays,
92 * decides how to configure logical displays based on the physical display devices currently
93 * attached, sends notifications to the system and to applications when the state
94 * changes, and so on.
95 * </p><p>
96 * The display manager service relies on a collection of {@link DisplayAdapter} components,
97 * for discovering and configuring physical display devices attached to the system.
98 * There are separate display adapters for each manner that devices are attached:
99 * one display adapter for built-in local displays, one for simulated non-functional
100 * displays when the system is headless, one for simulated overlay displays used for
101 * development, one for wifi displays, etc.
102 * </p><p>
103 * Display adapters are only weakly coupled to the display manager service.
104 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700105 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700106 * by the display manager service. This separation of concerns is important for
107 * two main reasons. First, it neatly encapsulates the responsibilities of these
108 * two classes: display adapters handle individual display devices whereas
109 * the display manager service handles the global state. Second, it eliminates
110 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700111 * </p>
112 *
113 * <h3>Synchronization</h3>
114 * <p>
115 * Because the display manager may be accessed by multiple threads, the synchronization
116 * story gets a little complicated. In particular, the window manager may call into
117 * the display manager while holding a surface transaction with the expectation that
118 * it can apply changes immediately. Unfortunately, that means we can't just do
119 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700120 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700121 * To make this work, all of the objects that belong to the display manager must
122 * use the same lock. We call this lock the synchronization root and it has a unique
123 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
124 * named with the "Locked" suffix.
125 * </p><p>
126 * Where things get tricky is that the display manager is not allowed to make
127 * any potentially reentrant calls, especially into the window manager. We generally
128 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700129 * </p>
130 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800131public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700132 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700133 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700134
Jeff Brownbbd28a22012-09-20 16:47:15 -0700135 // When this system property is set to 0, WFD is forcibly disabled on boot.
136 // When this system property is set to 1, WFD is forcibly enabled on boot.
137 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
138 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
139
Jeff Brownbd6e1502012-08-28 03:27:37 -0700140 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
141
Santos Cordonc22c5632017-06-21 16:03:49 -0700142 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700143 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
144 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700145 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700146 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100147 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700148
Jeff Brownb880d882014-02-10 19:47:07 -0800149 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700150 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700151 private final Handler mUiHandler;
152 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800153 private WindowManagerInternal mWindowManagerInternal;
154 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700155 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700156
157 // The synchronization root for the display manager.
158 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800159 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
160 // into WindowManagerService methods that require mWindowMap while holding this unless you are
161 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700162 private final SyncRoot mSyncRoot = new SyncRoot();
163
164 // True if in safe mode.
165 // This option may disable certain display adapters.
166 public boolean mSafeMode;
167
168 // True if we are in a special boot mode where only core applications and
169 // services should be started. This option may disable certain display adapters.
170 public boolean mOnlyCore;
171
Jeff Brown27f1d672012-10-17 18:32:34 -0700172 // True if the display manager service should pretend there is only one display
173 // and only tell applications about the existence of the default logical display.
174 // The display manager can still mirror content to secondary displays but applications
175 // cannot present unique content on those displays.
176 // Used for demonstration purposes only.
177 private final boolean mSingleDisplayDemoMode;
178
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700179 // All callback records indexed by calling process id.
180 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700181 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700182
Jeff Brownbd6e1502012-08-28 03:27:37 -0700183 // List of all currently registered display adapters.
184 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
185
186 // List of all currently connected display devices.
187 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
188
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700189 // List of all logical displays indexed by logical display id.
190 private final SparseArray<LogicalDisplay> mLogicalDisplays =
191 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700192 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
193
Jeff Brown7f3994e2012-12-04 14:04:28 -0800194 // List of all display transaction listeners.
195 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
196 new CopyOnWriteArrayList<DisplayTransactionListener>();
197
Jeff Brownad9ef192014-04-08 17:26:30 -0700198 // Display power controller.
199 private DisplayPowerController mDisplayPowerController;
200
Jeff Brown037c33e2014-04-09 00:31:55 -0700201 // The overall display state, independent of changes that might influence one
202 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700203 private int mGlobalDisplayState = Display.STATE_ON;
204
205 // The overall display brightness.
206 // For now, this only applies to the built-in display but we may split it up eventually.
207 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700208
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700209 // Set to true when there are pending display changes that have yet to be applied
210 // to the surface flinger state.
211 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700212
Jeff Browne08ae382012-09-07 20:36:36 -0700213 // The Wifi display adapter, or null if not registered.
214 private WifiDisplayAdapter mWifiDisplayAdapter;
215
Jeff Brownce468a32013-11-21 16:42:03 -0800216 // The number of active wifi display scan requests.
217 private int mWifiDisplayScanRequestCount;
218
Jeff Browna506a6e2013-06-04 00:02:38 -0700219 // The virtual display adapter, or null if not registered.
220 private VirtualDisplayAdapter mVirtualDisplayAdapter;
221
Michael Wrighteedcbf12017-08-16 23:14:54 +0100222 // The stable device screen height and width. These are not tied to a specific display, even
223 // the default display, because they need to be stable over the course of the device's entire
224 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
225 // device).
226 private Point mStableDisplaySize = new Point();
227
Jeff Brownd728bf52012-09-08 18:05:28 -0700228 // Viewports of the default display and the display that should receive touch
229 // input from an external source. Used by the input system.
230 private final DisplayViewport mDefaultViewport = new DisplayViewport();
231 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700232 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700233
Jeff Brown89d55462012-09-19 11:33:42 -0700234 // Persistent data store for all internal settings maintained by the display manager service.
235 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
236
Jeff Brownbd6e1502012-08-28 03:27:37 -0700237 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700238 // May be used outside of the lock but only on the handler thread.
239 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700240
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700241 // Temporary display info, used for comparing display configurations.
242 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
243
Jeff Brownd728bf52012-09-08 18:05:28 -0700244 // Temporary viewports, used when sending new viewport information to the
245 // input system. May be used outside of the lock but only on the handler thread.
246 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
247 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700248 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700249
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700250 // The default color mode for default displays. Overrides the usual
251 // Display.Display.COLOR_MODE_DEFAULT for displays with the
252 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
253 private final int mDefaultDisplayDefaultColorMode;
254
Jeff Browne75926d2014-09-18 15:24:49 -0700255 // Temporary list of deferred work to perform when setting the display state.
256 // Only used by requestDisplayState. The field is self-synchronized and only
257 // intended for use inside of the requestGlobalDisplayStateInternal function.
258 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
259
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800260 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
261 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
262
Santos Cordonee8931e2017-04-05 10:31:15 -0700263 private final Injector mInjector;
264
Kenny Guy22bd0442017-10-26 00:15:54 +0100265 private final BrightnessTracker mBrightnessTracker;
266
Jeff Brownb880d882014-02-10 19:47:07 -0800267 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700268 this(context, new Injector());
269 }
270
271 @VisibleForTesting
272 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800273 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700274 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800275 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800276 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700277 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700278 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700279 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700280 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
281 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700282
283 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
284 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Kenny Guy22bd0442017-10-26 00:15:54 +0100285 mBrightnessTracker = new BrightnessTracker(context, null);
Joel Fernandes2d314e12017-04-04 16:32:15 -0700286 }
287
288 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100289 // android.display and android.anim is critical to user experience and we should make sure
290 // it is not in the default foregroup groups, add it to top-app to make sure it uses all the
291 // cores and scheduling settings for top-app when it runs.
292 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
293 Process.THREAD_GROUP_TOP_APP);
294 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
295 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200296 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
297 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700298 }
299
Jeff Brown4ccb8232014-01-16 22:16:42 -0800300 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800301 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700302 // We need to pre-load the persistent data store so it's ready before the default display
303 // adapter is up so that we have it's configuration. We could load it lazily, but since
304 // 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 -0700305 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100306 synchronized(mSyncRoot) {
307 mPersistentDataStore.loadIfNeeded();
308 loadStableDisplayValuesLocked();
309 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700310 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800311
312 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
313 true /*allowIsolated*/);
314 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700315 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800316 }
317
318 @Override
319 public void onBootPhase(int phase) {
320 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
321 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700322 long timeout = SystemClock.uptimeMillis()
323 + mInjector.getDefaultDisplayDelayTimeout();
324 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
325 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800326 long delay = timeout - SystemClock.uptimeMillis();
327 if (delay <= 0) {
328 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700329 + "to be initialized. DefaultDisplay="
330 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
331 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800332 }
333 if (DEBUG) {
334 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
335 }
336 try {
337 mSyncRoot.wait(delay);
338 } catch (InterruptedException ex) {
339 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700340 }
341 }
342 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700343 }
344
Jeff Brown4ccb8232014-01-16 22:16:42 -0800345 // TODO: Use dependencies or a boot phase
346 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700347 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800348 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
349 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700350 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700351 }
352 }
353
354 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700355 * Called when the system is ready to go.
356 */
357 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700358 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700359 mSafeMode = safeMode;
360 mOnlyCore = onlyCore;
361 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700362
Jeff Brownbd6e1502012-08-28 03:27:37 -0700363 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100364 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700365 }
366
Santos Cordonee8931e2017-04-05 10:31:15 -0700367 @VisibleForTesting
368 Handler getDisplayHandler() {
369 return mHandler;
370 }
371
Michael Wrighteedcbf12017-08-16 23:14:54 +0100372 private void loadStableDisplayValuesLocked() {
373 final Point size = mPersistentDataStore.getStableDisplaySize();
374 if (size.x > 0 && size.y > 0) {
375 // Just set these values directly so we don't write the display persistent data again
376 // unnecessarily
377 mStableDisplaySize.set(size.x, size.y);
378 } else {
379 final Resources res = mContext.getResources();
380 final int width = res.getInteger(
381 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
382 final int height = res.getInteger(
383 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
384 if (width > 0 && height > 0) {
385 setStableDisplaySizeLocked(width, height);
386 }
387 }
388 }
389
390 private Point getStableDisplaySizeInternal() {
391 Point r = new Point();
392 synchronized (mSyncRoot) {
393 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
394 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
395 }
396 }
397 return r;
398 }
399
Jeff Brown4ccb8232014-01-16 22:16:42 -0800400 private void registerDisplayTransactionListenerInternal(
401 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800402 // List is self-synchronized copy-on-write.
403 mDisplayTransactionListeners.add(listener);
404 }
405
Jeff Brown4ccb8232014-01-16 22:16:42 -0800406 private void unregisterDisplayTransactionListenerInternal(
407 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800408 // List is self-synchronized copy-on-write.
409 mDisplayTransactionListeners.remove(listener);
410 }
411
Jeff Brown4ccb8232014-01-16 22:16:42 -0800412 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700413 int displayId, DisplayInfo info) {
414 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700416 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700417 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700418 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700419 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700420 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700421 }
422 }
423 }
424
Andrii Kuliancd097992017-03-23 18:31:59 -0700425 /**
426 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
427 */
428 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
429 synchronized (mSyncRoot) {
430 final LogicalDisplay display = mLogicalDisplays.get(displayId);
431 if (display != null) {
432 display.getNonOverrideDisplayInfoLocked(outInfo);
433 }
434 }
435 }
436
Santos Cordonee8931e2017-04-05 10:31:15 -0700437 @VisibleForTesting
438 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700439 synchronized (mSyncRoot) {
440 if (!mPendingTraversal) {
441 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700442 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700443 mPendingTraversal = false;
444
445 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700446 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800447
448 // List is self-synchronized copy-on-write.
449 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
450 listener.onDisplayTransaction();
451 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700452 }
453
Jeff Brown5d6443b2015-04-10 20:15:01 -0700454 private void requestGlobalDisplayStateInternal(int state, int brightness) {
455 if (state == Display.STATE_UNKNOWN) {
456 state = Display.STATE_ON;
457 }
458 if (state == Display.STATE_OFF) {
459 brightness = PowerManager.BRIGHTNESS_OFF;
460 } else if (brightness < 0) {
461 brightness = PowerManager.BRIGHTNESS_DEFAULT;
462 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
463 brightness = PowerManager.BRIGHTNESS_ON;
464 }
465
Jeff Browne75926d2014-09-18 15:24:49 -0700466 synchronized (mTempDisplayStateWorkQueue) {
467 try {
468 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700469 // Note that we do not need to schedule traversals here although it
470 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700471 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700472 if (mGlobalDisplayState == state
473 && mGlobalDisplayBrightness == brightness) {
474 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700475 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700476
477 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
478 + Display.stateToString(state)
479 + ", brightness=" + brightness + ")");
480 mGlobalDisplayState = state;
481 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700482 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700483 }
484
485 // Setting the display power state can take hundreds of milliseconds
486 // to complete so we defer the most expensive part of the work until
487 // after we have exited the critical section to avoid blocking other
488 // threads for a long time.
489 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
490 mTempDisplayStateWorkQueue.get(i).run();
491 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700492 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700493 } finally {
494 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700495 }
496 }
497 }
498
Jeff Brown4ccb8232014-01-16 22:16:42 -0800499 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700500 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800501 LogicalDisplay display = mLogicalDisplays.get(displayId);
502 if (display != null) {
503 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800504 if (info.hasAccess(callingUid)
505 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800506 return info;
507 }
508 }
509 return null;
510 }
511 }
512
513 private int[] getDisplayIdsInternal(int callingUid) {
514 synchronized (mSyncRoot) {
515 final int count = mLogicalDisplays.size();
516 int[] displayIds = new int[count];
517 int n = 0;
518 for (int i = 0; i < count; i++) {
519 LogicalDisplay display = mLogicalDisplays.valueAt(i);
520 DisplayInfo info = display.getDisplayInfoLocked();
521 if (info.hasAccess(callingUid)) {
522 displayIds[n++] = mLogicalDisplays.keyAt(i);
523 }
524 }
525 if (n != count) {
526 displayIds = Arrays.copyOfRange(displayIds, 0, n);
527 }
528 return displayIds;
529 }
530 }
531
532 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
533 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700534 if (mCallbacks.get(callingPid) != null) {
535 throw new SecurityException("The calling process has already "
536 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700537 }
538
Jeff Brownbd6e1502012-08-28 03:27:37 -0700539 CallbackRecord record = new CallbackRecord(callingPid, callback);
540 try {
541 IBinder binder = callback.asBinder();
542 binder.linkToDeath(record, 0);
543 } catch (RemoteException ex) {
544 // give up
545 throw new RuntimeException(ex);
546 }
547
548 mCallbacks.put(callingPid, record);
549 }
550 }
551
Jeff Brownce468a32013-11-21 16:42:03 -0800552 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700553 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800554 mCallbacks.remove(record.mPid);
555 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700556 }
557 }
558
Jeff Brown4ccb8232014-01-16 22:16:42 -0800559 private void startWifiDisplayScanInternal(int callingPid) {
560 synchronized (mSyncRoot) {
561 CallbackRecord record = mCallbacks.get(callingPid);
562 if (record == null) {
563 throw new IllegalStateException("The calling process has not "
564 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700565 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800566 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700567 }
568 }
569
Jeff Brownce468a32013-11-21 16:42:03 -0800570 private void startWifiDisplayScanLocked(CallbackRecord record) {
571 if (!record.mWifiDisplayScanRequested) {
572 record.mWifiDisplayScanRequested = true;
573 if (mWifiDisplayScanRequestCount++ == 0) {
574 if (mWifiDisplayAdapter != null) {
575 mWifiDisplayAdapter.requestStartScanLocked();
576 }
577 }
578 }
579 }
580
Jeff Brown4ccb8232014-01-16 22:16:42 -0800581 private void stopWifiDisplayScanInternal(int callingPid) {
582 synchronized (mSyncRoot) {
583 CallbackRecord record = mCallbacks.get(callingPid);
584 if (record == null) {
585 throw new IllegalStateException("The calling process has not "
586 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800587 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800588 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800589 }
590 }
591
592 private void stopWifiDisplayScanLocked(CallbackRecord record) {
593 if (record.mWifiDisplayScanRequested) {
594 record.mWifiDisplayScanRequested = false;
595 if (--mWifiDisplayScanRequestCount == 0) {
596 if (mWifiDisplayAdapter != null) {
597 mWifiDisplayAdapter.requestStopScanLocked();
598 }
599 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700600 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800601 + mWifiDisplayScanRequestCount);
602 mWifiDisplayScanRequestCount = 0;
603 }
604 }
605 }
606
Jeff Brown4ccb8232014-01-16 22:16:42 -0800607 private void connectWifiDisplayInternal(String address) {
608 synchronized (mSyncRoot) {
609 if (mWifiDisplayAdapter != null) {
610 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700611 }
Jeff Browne08ae382012-09-07 20:36:36 -0700612 }
613 }
614
Jeff Brown4ccb8232014-01-16 22:16:42 -0800615 private void pauseWifiDisplayInternal() {
616 synchronized (mSyncRoot) {
617 if (mWifiDisplayAdapter != null) {
618 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700619 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700620 }
621 }
622
Jeff Brown4ccb8232014-01-16 22:16:42 -0800623 private void resumeWifiDisplayInternal() {
624 synchronized (mSyncRoot) {
625 if (mWifiDisplayAdapter != null) {
626 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700627 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700628 }
629 }
630
Jeff Brown4ccb8232014-01-16 22:16:42 -0800631 private void disconnectWifiDisplayInternal() {
632 synchronized (mSyncRoot) {
633 if (mWifiDisplayAdapter != null) {
634 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700635 }
Jeff Browne08ae382012-09-07 20:36:36 -0700636 }
637 }
638
Jeff Brown4ccb8232014-01-16 22:16:42 -0800639 private void renameWifiDisplayInternal(String address, String alias) {
640 synchronized (mSyncRoot) {
641 if (mWifiDisplayAdapter != null) {
642 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700643 }
Jeff Brown89d55462012-09-19 11:33:42 -0700644 }
645 }
646
Jeff Brown4ccb8232014-01-16 22:16:42 -0800647 private void forgetWifiDisplayInternal(String address) {
648 synchronized (mSyncRoot) {
649 if (mWifiDisplayAdapter != null) {
650 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700651 }
Jeff Brown89d55462012-09-19 11:33:42 -0700652 }
653 }
654
Jeff Brown4ccb8232014-01-16 22:16:42 -0800655 private WifiDisplayStatus getWifiDisplayStatusInternal() {
656 synchronized (mSyncRoot) {
657 if (mWifiDisplayAdapter != null) {
658 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700659 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800660 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700661 }
662 }
663
Michael Wright1c9977b2016-07-12 13:30:10 -0700664 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100665 synchronized (mSyncRoot) {
666 LogicalDisplay display = mLogicalDisplays.get(displayId);
667 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700668 display.getRequestedColorModeLocked() != colorMode) {
669 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100670 scheduleTraversalLocked(false);
671 }
672 }
673 }
674
Michael Wright75ee9fc2014-09-01 19:55:22 -0700675 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700676 IMediaProjection projection, int callingUid, String packageName, String name, int width,
677 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800678 synchronized (mSyncRoot) {
679 if (mVirtualDisplayAdapter == null) {
680 Slog.w(TAG, "Rejecting request to create private virtual display "
681 + "because the virtual display adapter is not available.");
682 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700683 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800684
685 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700686 callback, projection, callingUid, packageName, name, width, height, densityDpi,
687 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800688 if (device == null) {
689 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700690 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700691
Jeff Brown4ccb8232014-01-16 22:16:42 -0800692 handleDisplayDeviceAddedLocked(device);
693 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
694 if (display != null) {
695 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700696 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800697
698 // Something weird happened and the logical display was not created.
699 Slog.w(TAG, "Rejecting request to create virtual display "
700 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700701 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800702 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700703 }
704 return -1;
705 }
706
Michael Wright01e840f2014-06-26 16:03:25 -0700707 private void resizeVirtualDisplayInternal(IBinder appToken,
708 int width, int height, int densityDpi) {
709 synchronized (mSyncRoot) {
710 if (mVirtualDisplayAdapter == null) {
711 return;
712 }
713
714 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
715 }
716 }
717
Jeff Brown92207df2014-04-16 13:16:07 -0700718 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
719 synchronized (mSyncRoot) {
720 if (mVirtualDisplayAdapter == null) {
721 return;
722 }
723
724 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
725 }
726 }
727
Jeff Brown4ccb8232014-01-16 22:16:42 -0800728 private void releaseVirtualDisplayInternal(IBinder appToken) {
729 synchronized (mSyncRoot) {
730 if (mVirtualDisplayAdapter == null) {
731 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700732 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700733
Jeff Brown4ccb8232014-01-16 22:16:42 -0800734 DisplayDevice device =
735 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
736 if (device != null) {
737 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700738 }
739 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700740 }
741
Santos Cordonc22c5632017-06-21 16:03:49 -0700742 private void registerDefaultDisplayAdapters() {
743 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700744 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700745 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800746 registerDisplayAdapterLocked(new LocalDisplayAdapter(
747 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700748
749 // Standalone VR devices rely on a virtual display as their primary display for
750 // 2D UI. We register virtual display adapter along side the main display adapter
751 // here so that it is ready by the time the system sends the home Intent for
752 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
753 // the virtual display inside VR before any VR-specific apps even run.
754 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
755 mHandler, mDisplayAdapterListener);
756 if (mVirtualDisplayAdapter != null) {
757 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
758 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700759 }
760 }
761
762 private void registerAdditionalDisplayAdapters() {
763 synchronized (mSyncRoot) {
764 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700765 registerOverlayDisplayAdapterLocked();
766 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700767 }
768 }
769 }
770
Jeff Brown89d55462012-09-19 11:33:42 -0700771 private void registerOverlayDisplayAdapterLocked() {
772 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
773 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
774 }
775
776 private void registerWifiDisplayAdapterLocked() {
777 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700778 com.android.internal.R.bool.config_enableWifiDisplay)
779 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700780 mWifiDisplayAdapter = new WifiDisplayAdapter(
781 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
782 mPersistentDataStore);
783 registerDisplayAdapterLocked(mWifiDisplayAdapter);
784 }
785 }
786
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700787 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
788 // In safe mode, we disable non-essential display adapters to give the user
789 // an opportunity to fix broken settings or other problems that might affect
790 // system stability.
791 // In only-core mode, we disable non-essential display adapters to minimize
792 // the number of dependencies that are started while in this mode and to
793 // prevent problems that might occur due to the device being encrypted.
794 return !mSafeMode && !mOnlyCore;
795 }
796
797 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
798 mDisplayAdapters.add(adapter);
799 adapter.registerLocked();
800 }
801
Jeff Brownbd6e1502012-08-28 03:27:37 -0700802 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700803 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700804 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700805 }
806 }
807
Jeff Browna506a6e2013-06-04 00:02:38 -0700808 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700809 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700810 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700811 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700812 return;
813 }
814
Jeff Brown10acf6d2015-04-14 14:20:47 -0700815 Slog.i(TAG, "Display device added: " + info);
816 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700817
818 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700819 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700820 Runnable work = updateDisplayStateLocked(device);
821 if (work != null) {
822 work.run();
823 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700824 scheduleTraversalLocked(false);
825 }
826
Jeff Brownbd6e1502012-08-28 03:27:37 -0700827 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700828 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700829 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700830 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700831 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700832 return;
833 }
834
Jeff Brown10acf6d2015-04-14 14:20:47 -0700835 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
836 if (diff == DisplayDeviceInfo.DIFF_STATE) {
837 Slog.i(TAG, "Display device changed state: \"" + info.name
838 + "\", " + Display.stateToString(info.state));
839 } else if (diff != 0) {
840 Slog.i(TAG, "Display device changed: " + info);
841 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700842 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
843 try {
844 mPersistentDataStore.setColorMode(device, info.colorMode);
845 } finally {
846 mPersistentDataStore.saveIfNeeded();
847 }
848 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700849 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700850
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700851 device.applyPendingDisplayDeviceInfoChangesLocked();
852 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700853 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700854 }
855 }
856 }
857
Jeff Brownbd6e1502012-08-28 03:27:37 -0700858 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700859 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700860 handleDisplayDeviceRemovedLocked(device);
861 }
862 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700863
Jeff Browna506a6e2013-06-04 00:02:38 -0700864 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700865 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700866 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700867 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700868 return;
869 }
870
Jeff Brown10acf6d2015-04-14 14:20:47 -0700871 Slog.i(TAG, "Display device removed: " + info);
872 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700873
Jeff Browna506a6e2013-06-04 00:02:38 -0700874 updateLogicalDisplaysLocked();
875 scheduleTraversalLocked(false);
876 }
877
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700878 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700879 final int count = mDisplayDevices.size();
880 for (int i = 0; i < count; i++) {
881 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700882 Runnable runnable = updateDisplayStateLocked(device);
883 if (runnable != null) {
884 workQueue.add(runnable);
885 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700886 }
887 }
888
Jeff Browne75926d2014-09-18 15:24:49 -0700889 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700890 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700891 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700892 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
893 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700894 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700895 }
Jeff Browne75926d2014-09-18 15:24:49 -0700896 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700897 }
898
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700899 // Adds a new logical display based on the given display device.
900 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700901 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700902 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
903 boolean isDefault = (deviceInfo.flags
904 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
905 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
906 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
907 isDefault = false;
908 }
909
Jeff Brown27f1d672012-10-17 18:32:34 -0700910 if (!isDefault && mSingleDisplayDemoMode) {
911 Slog.i(TAG, "Not creating a logical display for a secondary display "
912 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700913 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700914 }
915
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700916 final int displayId = assignDisplayIdLocked(isDefault);
917 final int layerStack = assignLayerStackLocked(displayId);
918
Jeff Brownd728bf52012-09-08 18:05:28 -0700919 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700920 display.updateLocked(mDisplayDevices);
921 if (!display.isValidLocked()) {
922 // This should never happen currently.
923 Slog.w(TAG, "Ignoring display device because the logical display "
924 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700925 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700926 }
927
Michael Wrighteedcbf12017-08-16 23:14:54 +0100928 configureColorModeLocked(display, device);
929 if (isDefault) {
930 recordStableDisplayStatsIfNeededLocked(display);
931 }
932
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700933 mLogicalDisplays.put(displayId, display);
934
935 // Wake up waitForDefaultDisplay.
936 if (isDefault) {
937 mSyncRoot.notifyAll();
938 }
939
940 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700941 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700942 }
943
944 private int assignDisplayIdLocked(boolean isDefault) {
945 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
946 }
947
948 private int assignLayerStackLocked(int displayId) {
949 // Currently layer stacks and display ids are the same.
950 // This need not be the case.
951 return displayId;
952 }
953
Michael Wrighteedcbf12017-08-16 23:14:54 +0100954 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
955 if (display.getPrimaryDisplayDeviceLocked() == device) {
956 int colorMode = mPersistentDataStore.getColorMode(device);
957 if (colorMode == Display.COLOR_MODE_INVALID) {
958 if ((device.getDisplayDeviceInfoLocked().flags
959 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
960 colorMode = mDefaultDisplayDefaultColorMode;
961 } else {
962 colorMode = Display.COLOR_MODE_DEFAULT;
963 }
964 }
965 display.setRequestedColorModeLocked(colorMode);
966 }
967 }
968
969 // If we've never recorded stable device stats for this device before and they aren't
970 // explicitly configured, go ahead and record the stable device stats now based on the status
971 // of the default display at first boot.
972 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
973 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
974 DisplayInfo info = d.getDisplayInfoLocked();
975 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
976 }
977 }
978
979 private void setStableDisplaySizeLocked(int width, int height) {
980 mStableDisplaySize = new Point(width, height);
981 try {
982 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
983 } finally {
984 mPersistentDataStore.saveIfNeeded();
985 }
986 }
987
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700988 // Updates all existing logical displays given the current set of display devices.
989 // Removes invalid logical displays.
990 // Sends notifications if needed.
991 private boolean updateLogicalDisplaysLocked() {
992 boolean changed = false;
993 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
994 final int displayId = mLogicalDisplays.keyAt(i);
995 LogicalDisplay display = mLogicalDisplays.valueAt(i);
996
997 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
998 display.updateLocked(mDisplayDevices);
999 if (!display.isValidLocked()) {
1000 mLogicalDisplays.removeAt(i);
1001 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1002 changed = true;
1003 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1004 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1005 changed = true;
1006 }
1007 }
1008 return changed;
1009 }
1010
1011 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -07001012 // Clear all viewports before configuring displays so that we can keep
1013 // track of which ones we have configured.
1014 clearViewportsLocked();
1015
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001016 // Configure each display device.
1017 final int count = mDisplayDevices.size();
1018 for (int i = 0; i < count; i++) {
1019 DisplayDevice device = mDisplayDevices.get(i);
1020 configureDisplayInTransactionLocked(device);
1021 device.performTraversalInTransactionLocked();
1022 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001023
1024 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001025 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001026 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1027 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001028 }
1029
Michael Wright3f145a22014-07-22 19:46:03 -07001030 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001031 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001032 synchronized (mSyncRoot) {
1033 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001034 if (display == null) {
1035 return;
1036 }
1037 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001038 if (DEBUG) {
1039 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1040 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1041 }
1042
Craig Mautner722285e2012-09-07 13:55:58 -07001043 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001044 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001045 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001046 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1047 // Scan supported modes returned by display.getInfo() to find a mode with the same
1048 // size as the default display mode but with the specified refresh rate instead.
1049 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1050 requestedRefreshRate);
1051 }
1052 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001053 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001054 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001055 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001056 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001057 scheduleTraversalLocked(inTraversal);
1058 }
Craig Mautner722285e2012-09-07 13:55:58 -07001059 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001060 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001061
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001062 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1063 synchronized (mSyncRoot) {
1064 LogicalDisplay display = mLogicalDisplays.get(displayId);
1065 if (display == null) {
1066 return;
1067 }
1068 if (display.getDisplayOffsetXLocked() != x
1069 || display.getDisplayOffsetYLocked() != y) {
1070 if (DEBUG) {
1071 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1072 + x + ", " + y + ")");
1073 }
1074 display.setDisplayOffsetsLocked(x, y);
1075 scheduleTraversalLocked(false);
1076 }
1077 }
1078 }
1079
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001080 // Updates the lists of UIDs that are present on displays.
1081 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1082 synchronized (mSyncRoot) {
1083 mDisplayAccessUIDs.clear();
1084 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1085 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1086 newDisplayAccessUIDs.valueAt(i));
1087 }
1088 }
1089 }
1090
1091 // Checks if provided UID's content is present on the display and UID has access to it.
1092 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1093 synchronized (mSyncRoot) {
1094 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1095 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1096 }
1097 }
1098
Jeff Brownd728bf52012-09-08 18:05:28 -07001099 private void clearViewportsLocked() {
1100 mDefaultViewport.valid = false;
1101 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001102 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001103 }
1104
1105 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001106 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1107 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001108
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001109 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001110 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001111 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001112 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001113 if (display != null && !display.hasContentLocked()) {
1114 // If the display does not have any content of its own, then
1115 // automatically mirror the default logical display contents.
1116 display = null;
1117 }
1118 if (display == null) {
1119 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1120 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001121 }
1122
1123 // Apply the logical display configuration to the display device.
1124 if (display == null) {
1125 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001126 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001127 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001128 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001129 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001130 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001131
1132 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001133 if (!mDefaultViewport.valid
1134 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1135 setViewportLocked(mDefaultViewport, display, device);
1136 }
1137 if (!mExternalTouchViewport.valid
1138 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1139 setViewportLocked(mExternalTouchViewport, display, device);
1140 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001141
1142 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1143 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1144 setViewportLocked(viewport, display, device);
1145 }
1146 }
1147
1148 /** Gets the virtual device viewport or creates it if not yet created. */
1149 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1150 DisplayViewport viewport;
1151 final int count = mVirtualTouchViewports.size();
1152 for (int i = 0; i < count; i++) {
1153 viewport = mVirtualTouchViewports.get(i);
1154 if (uniqueId.equals(viewport.uniqueId)) {
1155 return viewport;
1156 }
1157 }
1158
1159 viewport = new DisplayViewport();
1160 viewport.uniqueId = uniqueId;
1161 mVirtualTouchViewports.add(viewport);
1162 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001163 }
1164
1165 private static void setViewportLocked(DisplayViewport viewport,
1166 LogicalDisplay display, DisplayDevice device) {
1167 viewport.valid = true;
1168 viewport.displayId = display.getDisplayIdLocked();
1169 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001170 }
1171
1172 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1173 final int count = mLogicalDisplays.size();
1174 for (int i = 0; i < count; i++) {
1175 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1176 if (display.getPrimaryDisplayDeviceLocked() == device) {
1177 return display;
1178 }
1179 }
1180 return null;
1181 }
1182
Jeff Brownbd6e1502012-08-28 03:27:37 -07001183 private void sendDisplayEventLocked(int displayId, int event) {
1184 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1185 mHandler.sendMessage(msg);
1186 }
1187
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001188 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1189 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001190 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001191 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001192 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001193 if (!inTraversal) {
1194 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1195 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001196 }
1197 }
1198
1199 // Runs on Handler thread.
1200 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001201 private void deliverDisplayEvent(int displayId, int event) {
1202 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001203 Slog.d(TAG, "Delivering display event: displayId="
1204 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001205 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001206
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001207 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001208 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001209 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001210 count = mCallbacks.size();
1211 mTempCallbacks.clear();
1212 for (int i = 0; i < count; i++) {
1213 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001214 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001215 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001216
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001217 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001218 for (int i = 0; i < count; i++) {
1219 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1220 }
1221 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001222 }
1223
Michael Wrightc39d47a2014-07-08 18:07:36 -07001224 private IMediaProjectionManager getProjectionService() {
1225 if (mProjectionService == null) {
1226 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1227 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1228 }
1229 return mProjectionService;
1230 }
1231
Jeff Brown4ccb8232014-01-16 22:16:42 -08001232 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001233 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001234
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001235 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001236 pw.println(" mOnlyCode=" + mOnlyCore);
1237 pw.println(" mSafeMode=" + mSafeMode);
1238 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001239 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001240 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1241 pw.println(" mDefaultViewport=" + mDefaultViewport);
1242 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001243 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001244 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001245 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001246 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001247 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1248
Jeff Brown9e316a12012-10-08 19:17:06 -07001249
Jeff Brownbd6e1502012-08-28 03:27:37 -07001250 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001251 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001252
1253 pw.println();
1254 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001255 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001256 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001257 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001258 }
Craig Mautner9de49362012-08-02 14:30:30 -07001259
Jeff Brownbd6e1502012-08-28 03:27:37 -07001260 pw.println();
1261 pw.println("Display Devices: size=" + mDisplayDevices.size());
1262 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001263 pw.println(" " + device.getDisplayDeviceInfoLocked());
1264 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001265 }
1266
1267 final int logicalDisplayCount = mLogicalDisplays.size();
1268 pw.println();
1269 pw.println("Logical Displays: size=" + logicalDisplayCount);
1270 for (int i = 0; i < logicalDisplayCount; i++) {
1271 int displayId = mLogicalDisplays.keyAt(i);
1272 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1273 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001274 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001275 }
Jeff Brownce468a32013-11-21 16:42:03 -08001276
1277 final int callbackCount = mCallbacks.size();
1278 pw.println();
1279 pw.println("Callbacks: size=" + callbackCount);
1280 for (int i = 0; i < callbackCount; i++) {
1281 CallbackRecord callback = mCallbacks.valueAt(i);
1282 pw.println(" " + i + ": mPid=" + callback.mPid
1283 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1284 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001285
1286 if (mDisplayPowerController != null) {
1287 mDisplayPowerController.dump(pw);
1288 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001289
1290 pw.println();
1291 mPersistentDataStore.dump(pw);
Kenny Guycfe7b702017-11-14 21:04:58 +00001292
1293 pw.println();
1294 mBrightnessTracker.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001295 }
1296 }
1297
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001298 /**
1299 * This is the object that everything in the display manager locks on.
1300 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1301 * clear that the object belongs to the display manager service and that it is
1302 * a unique object with a special purpose.
1303 */
1304 public static final class SyncRoot {
1305 }
1306
Santos Cordonee8931e2017-04-05 10:31:15 -07001307 @VisibleForTesting
1308 static class Injector {
1309 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1310 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1311 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1312 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001313
1314 long getDefaultDisplayDelayTimeout() {
1315 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1316 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001317 }
1318
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001319 @VisibleForTesting
1320 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1321 synchronized (mSyncRoot) {
1322 LogicalDisplay display = mLogicalDisplays.get(displayId);
1323 if (display != null) {
1324 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1325 return displayDevice.getDisplayDeviceInfoLocked();
1326 }
1327 return null;
1328 }
1329 }
1330
Jeff Brownbd6e1502012-08-28 03:27:37 -07001331 private final class DisplayManagerHandler extends Handler {
1332 public DisplayManagerHandler(Looper looper) {
1333 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001334 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001335
Jeff Brownbd6e1502012-08-28 03:27:37 -07001336 @Override
1337 public void handleMessage(Message msg) {
1338 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001339 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1340 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001341 break;
1342
1343 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1344 registerAdditionalDisplayAdapters();
1345 break;
1346
1347 case MSG_DELIVER_DISPLAY_EVENT:
1348 deliverDisplayEvent(msg.arg1, msg.arg2);
1349 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001350
1351 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001352 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001353 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001354
1355 case MSG_UPDATE_VIEWPORT: {
1356 synchronized (mSyncRoot) {
1357 mTempDefaultViewport.copyFrom(mDefaultViewport);
1358 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001359 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1360 mTempVirtualTouchViewports.clear();
1361 for (DisplayViewport d : mVirtualTouchViewports) {
1362 mTempVirtualTouchViewports.add(d.makeCopy());
1363 }
1364 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001365 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001366 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1367 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001368 break;
1369 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001370
1371 case MSG_REGISTER_BRIGHTNESS_TRACKER:
1372 mBrightnessTracker.start();
1373 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001374 }
1375 }
1376 }
1377
1378 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1379 @Override
1380 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1381 switch (event) {
1382 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1383 handleDisplayDeviceAdded(device);
1384 break;
1385
1386 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1387 handleDisplayDeviceChanged(device);
1388 break;
1389
1390 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1391 handleDisplayDeviceRemoved(device);
1392 break;
1393 }
1394 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001395
1396 @Override
1397 public void onTraversalRequested() {
1398 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001399 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001400 }
1401 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001402 }
1403
1404 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001405 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001406 private final IDisplayManagerCallback mCallback;
1407
Jeff Brownce468a32013-11-21 16:42:03 -08001408 public boolean mWifiDisplayScanRequested;
1409
Jeff Brownbd6e1502012-08-28 03:27:37 -07001410 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1411 mPid = pid;
1412 mCallback = callback;
1413 }
1414
1415 @Override
1416 public void binderDied() {
1417 if (DEBUG) {
1418 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1419 }
Jeff Brownce468a32013-11-21 16:42:03 -08001420 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001421 }
1422
1423 public void notifyDisplayEventAsync(int displayId, int event) {
1424 try {
1425 mCallback.onDisplayEvent(displayId, event);
1426 } catch (RemoteException ex) {
1427 Slog.w(TAG, "Failed to notify process "
1428 + mPid + " that displays changed, assuming it died.", ex);
1429 binderDied();
1430 }
1431 }
1432 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001433
Santos Cordonee8931e2017-04-05 10:31:15 -07001434 @VisibleForTesting
1435 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001436 /**
1437 * Returns information about the specified logical display.
1438 *
1439 * @param displayId The logical display id.
1440 * @return The logical display info, or null if the display does not exist. The
1441 * returned object must be treated as immutable.
1442 */
1443 @Override // Binder call
1444 public DisplayInfo getDisplayInfo(int displayId) {
1445 final int callingUid = Binder.getCallingUid();
1446 final long token = Binder.clearCallingIdentity();
1447 try {
1448 return getDisplayInfoInternal(displayId, callingUid);
1449 } finally {
1450 Binder.restoreCallingIdentity(token);
1451 }
1452 }
1453
1454 /**
1455 * Returns the list of all display ids.
1456 */
1457 @Override // Binder call
1458 public int[] getDisplayIds() {
1459 final int callingUid = Binder.getCallingUid();
1460 final long token = Binder.clearCallingIdentity();
1461 try {
1462 return getDisplayIdsInternal(callingUid);
1463 } finally {
1464 Binder.restoreCallingIdentity(token);
1465 }
1466 }
1467
Michael Wrighteedcbf12017-08-16 23:14:54 +01001468 /**
1469 * Returns the stable device display size, in pixels.
1470 */
1471 @Override // Binder call
1472 public Point getStableDisplaySize() {
1473 final long token = Binder.clearCallingIdentity();
1474 try {
1475 return getStableDisplaySizeInternal();
1476 } finally {
1477 Binder.restoreCallingIdentity(token);
1478 }
1479 }
1480
Jeff Brown4ccb8232014-01-16 22:16:42 -08001481 @Override // Binder call
1482 public void registerCallback(IDisplayManagerCallback callback) {
1483 if (callback == null) {
1484 throw new IllegalArgumentException("listener must not be null");
1485 }
1486
1487 final int callingPid = Binder.getCallingPid();
1488 final long token = Binder.clearCallingIdentity();
1489 try {
1490 registerCallbackInternal(callback, callingPid);
1491 } finally {
1492 Binder.restoreCallingIdentity(token);
1493 }
1494 }
1495
1496 @Override // Binder call
1497 public void startWifiDisplayScan() {
1498 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1499 "Permission required to start wifi display scans");
1500
1501 final int callingPid = Binder.getCallingPid();
1502 final long token = Binder.clearCallingIdentity();
1503 try {
1504 startWifiDisplayScanInternal(callingPid);
1505 } finally {
1506 Binder.restoreCallingIdentity(token);
1507 }
1508 }
1509
1510 @Override // Binder call
1511 public void stopWifiDisplayScan() {
1512 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1513 "Permission required to stop wifi display scans");
1514
1515 final int callingPid = Binder.getCallingPid();
1516 final long token = Binder.clearCallingIdentity();
1517 try {
1518 stopWifiDisplayScanInternal(callingPid);
1519 } finally {
1520 Binder.restoreCallingIdentity(token);
1521 }
1522 }
1523
1524 @Override // Binder call
1525 public void connectWifiDisplay(String address) {
1526 if (address == null) {
1527 throw new IllegalArgumentException("address must not be null");
1528 }
1529 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1530 "Permission required to connect to a wifi display");
1531
1532 final long token = Binder.clearCallingIdentity();
1533 try {
1534 connectWifiDisplayInternal(address);
1535 } finally {
1536 Binder.restoreCallingIdentity(token);
1537 }
1538 }
1539
1540 @Override // Binder call
1541 public void disconnectWifiDisplay() {
1542 // This request does not require special permissions.
1543 // Any app can request disconnection from the currently active wifi display.
1544 // This exception should no longer be needed once wifi display control moves
1545 // to the media router service.
1546
1547 final long token = Binder.clearCallingIdentity();
1548 try {
1549 disconnectWifiDisplayInternal();
1550 } finally {
1551 Binder.restoreCallingIdentity(token);
1552 }
1553 }
1554
1555 @Override // Binder call
1556 public void renameWifiDisplay(String address, String alias) {
1557 if (address == null) {
1558 throw new IllegalArgumentException("address must not be null");
1559 }
1560 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1561 "Permission required to rename to a wifi display");
1562
1563 final long token = Binder.clearCallingIdentity();
1564 try {
1565 renameWifiDisplayInternal(address, alias);
1566 } finally {
1567 Binder.restoreCallingIdentity(token);
1568 }
1569 }
1570
1571 @Override // Binder call
1572 public void forgetWifiDisplay(String address) {
1573 if (address == null) {
1574 throw new IllegalArgumentException("address must not be null");
1575 }
1576 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1577 "Permission required to forget to a wifi display");
1578
1579 final long token = Binder.clearCallingIdentity();
1580 try {
1581 forgetWifiDisplayInternal(address);
1582 } finally {
1583 Binder.restoreCallingIdentity(token);
1584 }
1585 }
1586
1587 @Override // Binder call
1588 public void pauseWifiDisplay() {
1589 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1590 "Permission required to pause a wifi display session");
1591
1592 final long token = Binder.clearCallingIdentity();
1593 try {
1594 pauseWifiDisplayInternal();
1595 } finally {
1596 Binder.restoreCallingIdentity(token);
1597 }
1598 }
1599
1600 @Override // Binder call
1601 public void resumeWifiDisplay() {
1602 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1603 "Permission required to resume a wifi display session");
1604
1605 final long token = Binder.clearCallingIdentity();
1606 try {
1607 resumeWifiDisplayInternal();
1608 } finally {
1609 Binder.restoreCallingIdentity(token);
1610 }
1611 }
1612
1613 @Override // Binder call
1614 public WifiDisplayStatus getWifiDisplayStatus() {
1615 // This request does not require special permissions.
1616 // Any app can get information about available wifi displays.
1617
1618 final long token = Binder.clearCallingIdentity();
1619 try {
1620 return getWifiDisplayStatusInternal();
1621 } finally {
1622 Binder.restoreCallingIdentity(token);
1623 }
1624 }
1625
1626 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001627 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001628 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001629 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1630 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001631 final long token = Binder.clearCallingIdentity();
1632 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001633 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001634 } finally {
1635 Binder.restoreCallingIdentity(token);
1636 }
1637 }
1638
1639 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001640 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001641 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001642 int width, int height, int densityDpi, Surface surface, int flags,
1643 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001644 final int callingUid = Binder.getCallingUid();
1645 if (!validatePackageName(callingUid, packageName)) {
1646 throw new SecurityException("packageName must match the calling uid");
1647 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001648 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001649 throw new IllegalArgumentException("appToken must not be null");
1650 }
1651 if (TextUtils.isEmpty(name)) {
1652 throw new IllegalArgumentException("name must be non-null and non-empty");
1653 }
1654 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1655 throw new IllegalArgumentException("width, height, and densityDpi must be "
1656 + "greater than 0");
1657 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001658 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001659 throw new IllegalArgumentException("Surface can't be single-buffered");
1660 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001661
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001662 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1663 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1664
1665 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001666 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001667 throw new IllegalArgumentException(
1668 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1669 }
Michael Wright6720be42014-07-29 19:14:16 -07001670 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001671 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1672 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001673 }
1674
Michael Wrightc39d47a2014-07-08 18:07:36 -07001675 if (projection != null) {
1676 try {
1677 if (!getProjectionService().isValidMediaProjection(projection)) {
1678 throw new SecurityException("Invalid media projection");
1679 }
Michael Wright6720be42014-07-29 19:14:16 -07001680 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001681 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001682 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001683 }
1684 }
1685
Michael Wright6720be42014-07-29 19:14:16 -07001686 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001687 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001688 if (!canProjectVideo(projection)) {
1689 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1690 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1691 + "MediaProjection token in order to create a screen sharing virtual "
1692 + "display.");
1693 }
1694 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001695 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001696 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001697 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001698 + "or an appropriate MediaProjection token to create a "
1699 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001700 }
1701 }
1702
1703 final long token = Binder.clearCallingIdentity();
1704 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001705 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1706 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001707 } finally {
1708 Binder.restoreCallingIdentity(token);
1709 }
1710 }
1711
1712 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001713 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001714 int width, int height, int densityDpi) {
1715 final long token = Binder.clearCallingIdentity();
1716 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001717 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001718 } finally {
1719 Binder.restoreCallingIdentity(token);
1720 }
1721 }
1722
1723 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001724 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001725 if (surface != null && surface.isSingleBuffered()) {
1726 throw new IllegalArgumentException("Surface can't be single-buffered");
1727 }
Jeff Brown92207df2014-04-16 13:16:07 -07001728 final long token = Binder.clearCallingIdentity();
1729 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001730 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001731 } finally {
1732 Binder.restoreCallingIdentity(token);
1733 }
1734 }
1735
1736 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001737 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001738 final long token = Binder.clearCallingIdentity();
1739 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001740 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001741 } finally {
1742 Binder.restoreCallingIdentity(token);
1743 }
1744 }
1745
1746 @Override // Binder call
1747 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001748 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001749
1750 final long token = Binder.clearCallingIdentity();
1751 try {
1752 dumpInternal(pw);
1753 } finally {
1754 Binder.restoreCallingIdentity(token);
1755 }
1756 }
1757
Kenny Guy22bd0442017-10-26 00:15:54 +01001758 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001759 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001760 mContext.enforceCallingOrSelfPermission(
1761 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1762 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001763
1764 final int callingUid = Binder.getCallingUid();
1765 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
1766 final int mode = appOpsManager.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
1767 callingUid, callingPackage);
1768 final boolean hasUsageStats;
1769 if (mode == AppOpsManager.MODE_DEFAULT) {
1770 // The default behavior here is to check if PackageManager has given the app
1771 // permission.
1772 hasUsageStats = mContext.checkCallingPermission(
1773 Manifest.permission.PACKAGE_USAGE_STATS)
1774 == PackageManager.PERMISSION_GRANTED;
1775 } else {
1776 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1777 }
1778
1779 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001780 final long token = Binder.clearCallingIdentity();
1781 try {
Kenny Guy29aa30e2017-11-30 13:43:46 +00001782 return mBrightnessTracker.getEvents(userId, hasUsageStats);
Kenny Guy22bd0442017-10-26 00:15:54 +01001783 } finally {
1784 Binder.restoreCallingIdentity(token);
1785 }
1786 }
1787
1788 @Override // Binder call
1789 public void setBrightness(int brightness) {
1790 // STOPSHIP - remove when adaptive brightness controller accepts curves.
1791 mContext.enforceCallingOrSelfPermission(
1792 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1793 "Permission to set brightness.");
1794 int userId = UserHandle.getUserId(Binder.getCallingUid());
1795 final long token = Binder.clearCallingIdentity();
1796 try {
1797 mBrightnessTracker.setBrightness(brightness, userId);
1798 } finally {
1799 Binder.restoreCallingIdentity(token);
1800 }
1801 }
1802
Jeff Brown4ccb8232014-01-16 22:16:42 -08001803 private boolean validatePackageName(int uid, String packageName) {
1804 if (packageName != null) {
1805 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1806 if (packageNames != null) {
1807 for (String n : packageNames) {
1808 if (n.equals(packageName)) {
1809 return true;
1810 }
1811 }
1812 }
1813 }
1814 return false;
1815 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001816
1817 private boolean canProjectVideo(IMediaProjection projection) {
1818 if (projection != null) {
1819 try {
1820 if (projection.canProjectVideo()) {
1821 return true;
1822 }
1823 } catch (RemoteException e) {
1824 Slog.e(TAG, "Unable to query projection service for permissions", e);
1825 }
1826 }
1827 if (mContext.checkCallingPermission(
1828 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001829 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001830 return true;
1831 }
1832 return canProjectSecureVideo(projection);
1833 }
1834
1835 private boolean canProjectSecureVideo(IMediaProjection projection) {
1836 if (projection != null) {
1837 try {
1838 if (projection.canProjectSecureVideo()){
1839 return true;
1840 }
1841 } catch (RemoteException e) {
1842 Slog.e(TAG, "Unable to query projection service for permissions", e);
1843 }
1844 }
1845 return mContext.checkCallingPermission(
1846 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001847 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001848 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001849 }
1850
1851 private final class LocalService extends DisplayManagerInternal {
1852 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001853 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001854 SensorManager sensorManager) {
1855 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001856 DisplayBlanker blanker = new DisplayBlanker() {
1857 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001858 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001859 // The order of operations is important for legacy reasons.
1860 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001861 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001862 }
1863
1864 callbacks.onDisplayStateChange(state);
1865
1866 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001867 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001868 }
1869 }
1870 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001871 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001872 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001873 }
1874 }
1875
1876 @Override
1877 public boolean requestPowerState(DisplayPowerRequest request,
1878 boolean waitForNegativeProximity) {
1879 return mDisplayPowerController.requestPowerState(request,
1880 waitForNegativeProximity);
1881 }
1882
1883 @Override
1884 public boolean isProximitySensorAvailable() {
1885 return mDisplayPowerController.isProximitySensorAvailable();
1886 }
1887
1888 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001889 public DisplayInfo getDisplayInfo(int displayId) {
1890 return getDisplayInfoInternal(displayId, Process.myUid());
1891 }
1892
1893 @Override
1894 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1895 if (listener == null) {
1896 throw new IllegalArgumentException("listener must not be null");
1897 }
1898
1899 registerDisplayTransactionListenerInternal(listener);
1900 }
1901
1902 @Override
1903 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1904 if (listener == null) {
1905 throw new IllegalArgumentException("listener must not be null");
1906 }
1907
1908 unregisterDisplayTransactionListenerInternal(listener);
1909 }
1910
1911 @Override
1912 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1913 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1914 }
1915
1916 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001917 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1918 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1919 }
1920
1921 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001922 public void performTraversalInTransactionFromWindowManager() {
1923 performTraversalInTransactionFromWindowManagerInternal();
1924 }
1925
1926 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001927 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001928 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1929 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1930 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001931 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001932
1933 @Override
1934 public void setDisplayOffsets(int displayId, int x, int y) {
1935 setDisplayOffsetsInternal(displayId, x, y);
1936 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001937
1938 @Override
1939 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1940 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1941 }
1942
1943 @Override
1944 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1945 return isUidPresentOnDisplayInternal(uid, displayId);
1946 }
Kenny Guycfe7b702017-11-14 21:04:58 +00001947
1948 @Override
1949 public void persistBrightnessSliderEvents() {
1950 mBrightnessTracker.persistEvents();
1951 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001952 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001953}