blob: b2ab797a409edad4edb94832ac116a6e3e365532 [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
Jeff Brownbd6e1502012-08-28 03:27:37 -070019import com.android.internal.util.IndentingPrintWriter;
20
Jeff Brownfa25bf52012-07-23 19:26:30 -070021import android.Manifest;
22import android.content.Context;
23import android.content.pm.PackageManager;
Jeff Brownad9ef192014-04-08 17:26:30 -070024import android.hardware.SensorManager;
Jeff Brown7d00aff2013-08-02 19:03:49 -070025import android.hardware.display.DisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070026import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080027import android.hardware.display.DisplayManagerInternal;
28import android.hardware.display.DisplayViewport;
29import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070030import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070031import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070032import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070033import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080034import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070035import android.media.projection.IMediaProjection;
36import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070037import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070038import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070039import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080040import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070041import android.os.Looper;
42import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070043import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080044import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070045import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070046import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070047import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070048import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070049import android.os.Trace;
Jeff Browna506a6e2013-06-04 00:02:38 -070050import android.text.TextUtils;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.util.Slog;
52import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070053import android.view.Display;
54import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070055import android.view.Surface;
Jeff Brown4ccb8232014-01-16 22:16:42 -080056import android.view.WindowManagerInternal;
Jeff Browna506a6e2013-06-04 00:02:38 -070057
Jeff Brown4ccb8232014-01-16 22:16:42 -080058import com.android.server.DisplayThread;
59import com.android.server.LocalServices;
60import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070061import com.android.server.UiThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070062
63import java.io.FileDescriptor;
64import java.io.PrintWriter;
65import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070066import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070067import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080068import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070069
70/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070071 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070072 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070073 * The {@link DisplayManagerService} manages the global lifecycle of displays,
74 * decides how to configure logical displays based on the physical display devices currently
75 * attached, sends notifications to the system and to applications when the state
76 * changes, and so on.
77 * </p><p>
78 * The display manager service relies on a collection of {@link DisplayAdapter} components,
79 * for discovering and configuring physical display devices attached to the system.
80 * There are separate display adapters for each manner that devices are attached:
81 * one display adapter for built-in local displays, one for simulated non-functional
82 * displays when the system is headless, one for simulated overlay displays used for
83 * development, one for wifi displays, etc.
84 * </p><p>
85 * Display adapters are only weakly coupled to the display manager service.
86 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -070087 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -070088 * by the display manager service. This separation of concerns is important for
89 * two main reasons. First, it neatly encapsulates the responsibilities of these
90 * two classes: display adapters handle individual display devices whereas
91 * the display manager service handles the global state. Second, it eliminates
92 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -070093 * </p>
94 *
95 * <h3>Synchronization</h3>
96 * <p>
97 * Because the display manager may be accessed by multiple threads, the synchronization
98 * story gets a little complicated. In particular, the window manager may call into
99 * the display manager while holding a surface transaction with the expectation that
100 * it can apply changes immediately. Unfortunately, that means we can't just do
101 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700102 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700103 * To make this work, all of the objects that belong to the display manager must
104 * use the same lock. We call this lock the synchronization root and it has a unique
105 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
106 * named with the "Locked" suffix.
107 * </p><p>
108 * Where things get tricky is that the display manager is not allowed to make
109 * any potentially reentrant calls, especially into the window manager. We generally
110 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700111 * </p>
112 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800113public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700114 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700115 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700116
Jeff Brownbbd28a22012-09-20 16:47:15 -0700117 // When this system property is set to 0, WFD is forcibly disabled on boot.
118 // When this system property is set to 1, WFD is forcibly enabled on boot.
119 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
120 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
121
Jeff Brownbd6e1502012-08-28 03:27:37 -0700122 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
123
124 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER = 1;
125 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
126 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700127 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700128 private static final int MSG_UPDATE_VIEWPORT = 5;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700129
Jeff Brownb880d882014-02-10 19:47:07 -0800130 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700131 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700132 private final Handler mUiHandler;
133 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800134 private WindowManagerInternal mWindowManagerInternal;
135 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700136 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700137
138 // The synchronization root for the display manager.
139 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800140 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
141 // into WindowManagerService methods that require mWindowMap while holding this unless you are
142 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 private final SyncRoot mSyncRoot = new SyncRoot();
144
145 // True if in safe mode.
146 // This option may disable certain display adapters.
147 public boolean mSafeMode;
148
149 // True if we are in a special boot mode where only core applications and
150 // services should be started. This option may disable certain display adapters.
151 public boolean mOnlyCore;
152
Jeff Brown27f1d672012-10-17 18:32:34 -0700153 // True if the display manager service should pretend there is only one display
154 // and only tell applications about the existence of the default logical display.
155 // The display manager can still mirror content to secondary displays but applications
156 // cannot present unique content on those displays.
157 // Used for demonstration purposes only.
158 private final boolean mSingleDisplayDemoMode;
159
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700160 // All callback records indexed by calling process id.
161 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700162 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700163
Jeff Brownbd6e1502012-08-28 03:27:37 -0700164 // List of all currently registered display adapters.
165 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
166
167 // List of all currently connected display devices.
168 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
169
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700170 // List of all logical displays indexed by logical display id.
171 private final SparseArray<LogicalDisplay> mLogicalDisplays =
172 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700173 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
174
Jeff Brown7f3994e2012-12-04 14:04:28 -0800175 // List of all display transaction listeners.
176 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
177 new CopyOnWriteArrayList<DisplayTransactionListener>();
178
Jeff Brownad9ef192014-04-08 17:26:30 -0700179 // Display power controller.
180 private DisplayPowerController mDisplayPowerController;
181
Jeff Brown037c33e2014-04-09 00:31:55 -0700182 // The overall display state, independent of changes that might influence one
183 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700184 private int mGlobalDisplayState = Display.STATE_ON;
185
186 // The overall display brightness.
187 // For now, this only applies to the built-in display but we may split it up eventually.
188 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700189
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700190 // Set to true when there are pending display changes that have yet to be applied
191 // to the surface flinger state.
192 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700193
Jeff Browne08ae382012-09-07 20:36:36 -0700194 // The Wifi display adapter, or null if not registered.
195 private WifiDisplayAdapter mWifiDisplayAdapter;
196
Jeff Brownce468a32013-11-21 16:42:03 -0800197 // The number of active wifi display scan requests.
198 private int mWifiDisplayScanRequestCount;
199
Jeff Browna506a6e2013-06-04 00:02:38 -0700200 // The virtual display adapter, or null if not registered.
201 private VirtualDisplayAdapter mVirtualDisplayAdapter;
202
Jeff Brownd728bf52012-09-08 18:05:28 -0700203 // Viewports of the default display and the display that should receive touch
204 // input from an external source. Used by the input system.
205 private final DisplayViewport mDefaultViewport = new DisplayViewport();
206 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
207
Jeff Brown89d55462012-09-19 11:33:42 -0700208 // Persistent data store for all internal settings maintained by the display manager service.
209 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
210
Jeff Brownbd6e1502012-08-28 03:27:37 -0700211 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700212 // May be used outside of the lock but only on the handler thread.
213 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700214
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700215 // Temporary display info, used for comparing display configurations.
216 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
217
Jeff Brownd728bf52012-09-08 18:05:28 -0700218 // Temporary viewports, used when sending new viewport information to the
219 // input system. May be used outside of the lock but only on the handler thread.
220 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
221 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
222
Jeff Browne75926d2014-09-18 15:24:49 -0700223 // Temporary list of deferred work to perform when setting the display state.
224 // Only used by requestDisplayState. The field is self-synchronized and only
225 // intended for use inside of the requestGlobalDisplayStateInternal function.
226 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
227
Jeff Brownb880d882014-02-10 19:47:07 -0800228 public DisplayManagerService(Context context) {
229 super(context);
230 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800231 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700232 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700233 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700234 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700235
236 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
237 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700238 }
239
Jeff Brown4ccb8232014-01-16 22:16:42 -0800240 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800241 public void onStart() {
242 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
243
244 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
245 true /*allowIsolated*/);
246 publishLocalService(DisplayManagerInternal.class, new LocalService());
247 }
248
249 @Override
250 public void onBootPhase(int phase) {
251 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
252 synchronized (mSyncRoot) {
253 long timeout = SystemClock.uptimeMillis() + WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
254 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null) {
255 long delay = timeout - SystemClock.uptimeMillis();
256 if (delay <= 0) {
257 throw new RuntimeException("Timeout waiting for default display "
258 + "to be initialized.");
259 }
260 if (DEBUG) {
261 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
262 }
263 try {
264 mSyncRoot.wait(delay);
265 } catch (InterruptedException ex) {
266 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700267 }
268 }
269 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700270 }
271
Jeff Brown4ccb8232014-01-16 22:16:42 -0800272 // TODO: Use dependencies or a boot phase
273 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700274 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800275 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
276 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700277 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700278 }
279 }
280
281 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700282 * Called when the system is ready to go.
283 */
284 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700285 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700286 mSafeMode = safeMode;
287 mOnlyCore = onlyCore;
288 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700289
Jeff Brownbd6e1502012-08-28 03:27:37 -0700290 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
291 }
292
Jeff Brown4ccb8232014-01-16 22:16:42 -0800293 private void registerDisplayTransactionListenerInternal(
294 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800295 // List is self-synchronized copy-on-write.
296 mDisplayTransactionListeners.add(listener);
297 }
298
Jeff Brown4ccb8232014-01-16 22:16:42 -0800299 private void unregisterDisplayTransactionListenerInternal(
300 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800301 // List is self-synchronized copy-on-write.
302 mDisplayTransactionListeners.remove(listener);
303 }
304
Jeff Brown4ccb8232014-01-16 22:16:42 -0800305 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700306 int displayId, DisplayInfo info) {
307 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700308 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700309 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700310 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700311 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700312 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700313 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700314 }
315 }
316 }
317
Jeff Brown4ccb8232014-01-16 22:16:42 -0800318 private void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700319 synchronized (mSyncRoot) {
320 if (!mPendingTraversal) {
321 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700322 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700323 mPendingTraversal = false;
324
325 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700326 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800327
328 // List is self-synchronized copy-on-write.
329 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
330 listener.onDisplayTransaction();
331 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700332 }
333
Jeff Brown5d6443b2015-04-10 20:15:01 -0700334 private void requestGlobalDisplayStateInternal(int state, int brightness) {
335 if (state == Display.STATE_UNKNOWN) {
336 state = Display.STATE_ON;
337 }
338 if (state == Display.STATE_OFF) {
339 brightness = PowerManager.BRIGHTNESS_OFF;
340 } else if (brightness < 0) {
341 brightness = PowerManager.BRIGHTNESS_DEFAULT;
342 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
343 brightness = PowerManager.BRIGHTNESS_ON;
344 }
345
Jeff Browne75926d2014-09-18 15:24:49 -0700346 synchronized (mTempDisplayStateWorkQueue) {
347 try {
348 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700349 // Note that we do not need to schedule traversals here although it
350 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700351 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700352 if (mGlobalDisplayState == state
353 && mGlobalDisplayBrightness == brightness) {
354 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700355 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700356
357 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
358 + Display.stateToString(state)
359 + ", brightness=" + brightness + ")");
360 mGlobalDisplayState = state;
361 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700362 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700363 }
364
365 // Setting the display power state can take hundreds of milliseconds
366 // to complete so we defer the most expensive part of the work until
367 // after we have exited the critical section to avoid blocking other
368 // threads for a long time.
369 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
370 mTempDisplayStateWorkQueue.get(i).run();
371 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700372 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700373 } finally {
374 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700375 }
376 }
377 }
378
Jeff Brown4ccb8232014-01-16 22:16:42 -0800379 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700380 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800381 LogicalDisplay display = mLogicalDisplays.get(displayId);
382 if (display != null) {
383 DisplayInfo info = display.getDisplayInfoLocked();
384 if (info.hasAccess(callingUid)) {
385 return info;
386 }
387 }
388 return null;
389 }
390 }
391
392 private int[] getDisplayIdsInternal(int callingUid) {
393 synchronized (mSyncRoot) {
394 final int count = mLogicalDisplays.size();
395 int[] displayIds = new int[count];
396 int n = 0;
397 for (int i = 0; i < count; i++) {
398 LogicalDisplay display = mLogicalDisplays.valueAt(i);
399 DisplayInfo info = display.getDisplayInfoLocked();
400 if (info.hasAccess(callingUid)) {
401 displayIds[n++] = mLogicalDisplays.keyAt(i);
402 }
403 }
404 if (n != count) {
405 displayIds = Arrays.copyOfRange(displayIds, 0, n);
406 }
407 return displayIds;
408 }
409 }
410
411 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
412 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700413 if (mCallbacks.get(callingPid) != null) {
414 throw new SecurityException("The calling process has already "
415 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700416 }
417
Jeff Brownbd6e1502012-08-28 03:27:37 -0700418 CallbackRecord record = new CallbackRecord(callingPid, callback);
419 try {
420 IBinder binder = callback.asBinder();
421 binder.linkToDeath(record, 0);
422 } catch (RemoteException ex) {
423 // give up
424 throw new RuntimeException(ex);
425 }
426
427 mCallbacks.put(callingPid, record);
428 }
429 }
430
Jeff Brownce468a32013-11-21 16:42:03 -0800431 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700432 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800433 mCallbacks.remove(record.mPid);
434 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700435 }
436 }
437
Jeff Brown4ccb8232014-01-16 22:16:42 -0800438 private void startWifiDisplayScanInternal(int callingPid) {
439 synchronized (mSyncRoot) {
440 CallbackRecord record = mCallbacks.get(callingPid);
441 if (record == null) {
442 throw new IllegalStateException("The calling process has not "
443 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700444 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800445 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700446 }
447 }
448
Jeff Brownce468a32013-11-21 16:42:03 -0800449 private void startWifiDisplayScanLocked(CallbackRecord record) {
450 if (!record.mWifiDisplayScanRequested) {
451 record.mWifiDisplayScanRequested = true;
452 if (mWifiDisplayScanRequestCount++ == 0) {
453 if (mWifiDisplayAdapter != null) {
454 mWifiDisplayAdapter.requestStartScanLocked();
455 }
456 }
457 }
458 }
459
Jeff Brown4ccb8232014-01-16 22:16:42 -0800460 private void stopWifiDisplayScanInternal(int callingPid) {
461 synchronized (mSyncRoot) {
462 CallbackRecord record = mCallbacks.get(callingPid);
463 if (record == null) {
464 throw new IllegalStateException("The calling process has not "
465 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800466 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800467 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800468 }
469 }
470
471 private void stopWifiDisplayScanLocked(CallbackRecord record) {
472 if (record.mWifiDisplayScanRequested) {
473 record.mWifiDisplayScanRequested = false;
474 if (--mWifiDisplayScanRequestCount == 0) {
475 if (mWifiDisplayAdapter != null) {
476 mWifiDisplayAdapter.requestStopScanLocked();
477 }
478 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700479 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800480 + mWifiDisplayScanRequestCount);
481 mWifiDisplayScanRequestCount = 0;
482 }
483 }
484 }
485
Jeff Brown4ccb8232014-01-16 22:16:42 -0800486 private void connectWifiDisplayInternal(String address) {
487 synchronized (mSyncRoot) {
488 if (mWifiDisplayAdapter != null) {
489 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700490 }
Jeff Browne08ae382012-09-07 20:36:36 -0700491 }
492 }
493
Jeff Brown4ccb8232014-01-16 22:16:42 -0800494 private void pauseWifiDisplayInternal() {
495 synchronized (mSyncRoot) {
496 if (mWifiDisplayAdapter != null) {
497 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700498 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700499 }
500 }
501
Jeff Brown4ccb8232014-01-16 22:16:42 -0800502 private void resumeWifiDisplayInternal() {
503 synchronized (mSyncRoot) {
504 if (mWifiDisplayAdapter != null) {
505 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700506 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700507 }
508 }
509
Jeff Brown4ccb8232014-01-16 22:16:42 -0800510 private void disconnectWifiDisplayInternal() {
511 synchronized (mSyncRoot) {
512 if (mWifiDisplayAdapter != null) {
513 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700514 }
Jeff Browne08ae382012-09-07 20:36:36 -0700515 }
516 }
517
Jeff Brown4ccb8232014-01-16 22:16:42 -0800518 private void renameWifiDisplayInternal(String address, String alias) {
519 synchronized (mSyncRoot) {
520 if (mWifiDisplayAdapter != null) {
521 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700522 }
Jeff Brown89d55462012-09-19 11:33:42 -0700523 }
524 }
525
Jeff Brown4ccb8232014-01-16 22:16:42 -0800526 private void forgetWifiDisplayInternal(String address) {
527 synchronized (mSyncRoot) {
528 if (mWifiDisplayAdapter != null) {
529 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700530 }
Jeff Brown89d55462012-09-19 11:33:42 -0700531 }
532 }
533
Jeff Brown4ccb8232014-01-16 22:16:42 -0800534 private WifiDisplayStatus getWifiDisplayStatusInternal() {
535 synchronized (mSyncRoot) {
536 if (mWifiDisplayAdapter != null) {
537 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700538 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800539 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700540 }
541 }
542
Michael Wright75ee9fc2014-09-01 19:55:22 -0700543 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700544 IMediaProjection projection, int callingUid, String packageName,
Jeff Brown7d00aff2013-08-02 19:03:49 -0700545 String name, int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800546 synchronized (mSyncRoot) {
547 if (mVirtualDisplayAdapter == null) {
548 Slog.w(TAG, "Rejecting request to create private virtual display "
549 + "because the virtual display adapter is not available.");
550 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700551 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800552
553 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Michael Wright75ee9fc2014-09-01 19:55:22 -0700554 callback, projection, callingUid, packageName,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700555 name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800556 if (device == null) {
557 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700558 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700559
Jeff Brown4ccb8232014-01-16 22:16:42 -0800560 handleDisplayDeviceAddedLocked(device);
561 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
562 if (display != null) {
563 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700564 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800565
566 // Something weird happened and the logical display was not created.
567 Slog.w(TAG, "Rejecting request to create virtual display "
568 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700569 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800570 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700571 }
572 return -1;
573 }
574
Michael Wright01e840f2014-06-26 16:03:25 -0700575 private void resizeVirtualDisplayInternal(IBinder appToken,
576 int width, int height, int densityDpi) {
577 synchronized (mSyncRoot) {
578 if (mVirtualDisplayAdapter == null) {
579 return;
580 }
581
582 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
583 }
584 }
585
Jeff Brown92207df2014-04-16 13:16:07 -0700586 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
587 synchronized (mSyncRoot) {
588 if (mVirtualDisplayAdapter == null) {
589 return;
590 }
591
592 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
593 }
594 }
595
Jeff Brown4ccb8232014-01-16 22:16:42 -0800596 private void releaseVirtualDisplayInternal(IBinder appToken) {
597 synchronized (mSyncRoot) {
598 if (mVirtualDisplayAdapter == null) {
599 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700600 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700601
Jeff Brown4ccb8232014-01-16 22:16:42 -0800602 DisplayDevice device =
603 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
604 if (device != null) {
605 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700606 }
607 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700608 }
609
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700610 private void registerDefaultDisplayAdapter() {
611 // Register default display adapter.
612 synchronized (mSyncRoot) {
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800613 registerDisplayAdapterLocked(new LocalDisplayAdapter(
614 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700615 }
616 }
617
618 private void registerAdditionalDisplayAdapters() {
619 synchronized (mSyncRoot) {
620 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700621 registerOverlayDisplayAdapterLocked();
622 registerWifiDisplayAdapterLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700623 registerVirtualDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700624 }
625 }
626 }
627
Jeff Brown89d55462012-09-19 11:33:42 -0700628 private void registerOverlayDisplayAdapterLocked() {
629 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
630 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
631 }
632
633 private void registerWifiDisplayAdapterLocked() {
634 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700635 com.android.internal.R.bool.config_enableWifiDisplay)
636 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700637 mWifiDisplayAdapter = new WifiDisplayAdapter(
638 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
639 mPersistentDataStore);
640 registerDisplayAdapterLocked(mWifiDisplayAdapter);
641 }
642 }
643
Jeff Browna506a6e2013-06-04 00:02:38 -0700644 private void registerVirtualDisplayAdapterLocked() {
645 mVirtualDisplayAdapter = new VirtualDisplayAdapter(
646 mSyncRoot, mContext, mHandler, mDisplayAdapterListener);
647 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
648 }
649
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700650 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
651 // In safe mode, we disable non-essential display adapters to give the user
652 // an opportunity to fix broken settings or other problems that might affect
653 // system stability.
654 // In only-core mode, we disable non-essential display adapters to minimize
655 // the number of dependencies that are started while in this mode and to
656 // prevent problems that might occur due to the device being encrypted.
657 return !mSafeMode && !mOnlyCore;
658 }
659
660 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
661 mDisplayAdapters.add(adapter);
662 adapter.registerLocked();
663 }
664
Jeff Brownbd6e1502012-08-28 03:27:37 -0700665 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700666 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700667 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700668 }
669 }
670
Jeff Browna506a6e2013-06-04 00:02:38 -0700671 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700672 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700673 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700674 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700675 return;
676 }
677
Jeff Brown10acf6d2015-04-14 14:20:47 -0700678 Slog.i(TAG, "Display device added: " + info);
679 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700680
681 mDisplayDevices.add(device);
682 addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700683 Runnable work = updateDisplayStateLocked(device);
684 if (work != null) {
685 work.run();
686 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700687 scheduleTraversalLocked(false);
688 }
689
Jeff Brownbd6e1502012-08-28 03:27:37 -0700690 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700691 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700692 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700693 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700694 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700695 return;
696 }
697
Jeff Brown10acf6d2015-04-14 14:20:47 -0700698 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
699 if (diff == DisplayDeviceInfo.DIFF_STATE) {
700 Slog.i(TAG, "Display device changed state: \"" + info.name
701 + "\", " + Display.stateToString(info.state));
702 } else if (diff != 0) {
703 Slog.i(TAG, "Display device changed: " + info);
704 }
705 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700706
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700707 device.applyPendingDisplayDeviceInfoChangesLocked();
708 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700709 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700710 }
711 }
712 }
713
Jeff Brownbd6e1502012-08-28 03:27:37 -0700714 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700715 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700716 handleDisplayDeviceRemovedLocked(device);
717 }
718 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700719
Jeff Browna506a6e2013-06-04 00:02:38 -0700720 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700721 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700722 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700723 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700724 return;
725 }
726
Jeff Brown10acf6d2015-04-14 14:20:47 -0700727 Slog.i(TAG, "Display device removed: " + info);
728 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700729
Jeff Browna506a6e2013-06-04 00:02:38 -0700730 updateLogicalDisplaysLocked();
731 scheduleTraversalLocked(false);
732 }
733
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700734 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700735 final int count = mDisplayDevices.size();
736 for (int i = 0; i < count; i++) {
737 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700738 Runnable runnable = updateDisplayStateLocked(device);
739 if (runnable != null) {
740 workQueue.add(runnable);
741 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700742 }
743 }
744
Jeff Browne75926d2014-09-18 15:24:49 -0700745 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700746 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700747 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700748 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
749 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700750 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700751 }
Jeff Browne75926d2014-09-18 15:24:49 -0700752 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700753 }
754
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700755 // Adds a new logical display based on the given display device.
756 // Sends notifications if needed.
757 private void addLogicalDisplayLocked(DisplayDevice device) {
758 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
759 boolean isDefault = (deviceInfo.flags
760 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
761 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
762 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
763 isDefault = false;
764 }
765
Jeff Brown27f1d672012-10-17 18:32:34 -0700766 if (!isDefault && mSingleDisplayDemoMode) {
767 Slog.i(TAG, "Not creating a logical display for a secondary display "
768 + " because single display demo mode is enabled: " + deviceInfo);
769 return;
770 }
771
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700772 final int displayId = assignDisplayIdLocked(isDefault);
773 final int layerStack = assignLayerStackLocked(displayId);
774
Jeff Brownd728bf52012-09-08 18:05:28 -0700775 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700776 display.updateLocked(mDisplayDevices);
777 if (!display.isValidLocked()) {
778 // This should never happen currently.
779 Slog.w(TAG, "Ignoring display device because the logical display "
780 + "created from it was not considered valid: " + deviceInfo);
781 return;
782 }
783
784 mLogicalDisplays.put(displayId, display);
785
786 // Wake up waitForDefaultDisplay.
787 if (isDefault) {
788 mSyncRoot.notifyAll();
789 }
790
791 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
792 }
793
794 private int assignDisplayIdLocked(boolean isDefault) {
795 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
796 }
797
798 private int assignLayerStackLocked(int displayId) {
799 // Currently layer stacks and display ids are the same.
800 // This need not be the case.
801 return displayId;
802 }
803
804 // Updates all existing logical displays given the current set of display devices.
805 // Removes invalid logical displays.
806 // Sends notifications if needed.
807 private boolean updateLogicalDisplaysLocked() {
808 boolean changed = false;
809 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
810 final int displayId = mLogicalDisplays.keyAt(i);
811 LogicalDisplay display = mLogicalDisplays.valueAt(i);
812
813 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
814 display.updateLocked(mDisplayDevices);
815 if (!display.isValidLocked()) {
816 mLogicalDisplays.removeAt(i);
817 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
818 changed = true;
819 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
820 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
821 changed = true;
822 }
823 }
824 return changed;
825 }
826
827 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700828 // Clear all viewports before configuring displays so that we can keep
829 // track of which ones we have configured.
830 clearViewportsLocked();
831
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700832 // Configure each display device.
833 final int count = mDisplayDevices.size();
834 for (int i = 0; i < count; i++) {
835 DisplayDevice device = mDisplayDevices.get(i);
836 configureDisplayInTransactionLocked(device);
837 device.performTraversalInTransactionLocked();
838 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700839
840 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800841 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700842 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
843 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700844 }
845
Michael Wright3f145a22014-07-22 19:46:03 -0700846 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700847 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700848 synchronized (mSyncRoot) {
849 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700850 if (display == null) {
851 return;
852 }
853 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700854 if (DEBUG) {
855 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
856 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
857 }
858
Craig Mautner722285e2012-09-07 13:55:58 -0700859 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700860 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700861 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700862 if (requestedModeId == 0 && requestedRefreshRate != 0) {
863 // Scan supported modes returned by display.getInfo() to find a mode with the same
864 // size as the default display mode but with the specified refresh rate instead.
865 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
866 requestedRefreshRate);
867 }
868 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700869 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700870 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700871 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700872 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700873 scheduleTraversalLocked(inTraversal);
874 }
Craig Mautner722285e2012-09-07 13:55:58 -0700875 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700876 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700877
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800878 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
879 synchronized (mSyncRoot) {
880 LogicalDisplay display = mLogicalDisplays.get(displayId);
881 if (display == null) {
882 return;
883 }
884 if (display.getDisplayOffsetXLocked() != x
885 || display.getDisplayOffsetYLocked() != y) {
886 if (DEBUG) {
887 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
888 + x + ", " + y + ")");
889 }
890 display.setDisplayOffsetsLocked(x, y);
891 scheduleTraversalLocked(false);
892 }
893 }
894 }
895
Jeff Brownd728bf52012-09-08 18:05:28 -0700896 private void clearViewportsLocked() {
897 mDefaultViewport.valid = false;
898 mExternalTouchViewport.valid = false;
Craig Mautner722285e2012-09-07 13:55:58 -0700899 }
900
901 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -0800902 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
903 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -0700904
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700905 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -0800906 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -0700907 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -0800908 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700909 if (display != null && !display.hasContentLocked()) {
910 // If the display does not have any content of its own, then
911 // automatically mirror the default logical display contents.
912 display = null;
913 }
914 if (display == null) {
915 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
916 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700917 }
918
919 // Apply the logical display configuration to the display device.
920 if (display == null) {
921 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -0700922 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700923 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -0700924 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700925 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700926 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -0700927
928 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -0700929 if (!mDefaultViewport.valid
930 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
931 setViewportLocked(mDefaultViewport, display, device);
932 }
933 if (!mExternalTouchViewport.valid
934 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
935 setViewportLocked(mExternalTouchViewport, display, device);
936 }
937 }
938
939 private static void setViewportLocked(DisplayViewport viewport,
940 LogicalDisplay display, DisplayDevice device) {
941 viewport.valid = true;
942 viewport.displayId = display.getDisplayIdLocked();
943 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700944 }
945
946 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
947 final int count = mLogicalDisplays.size();
948 for (int i = 0; i < count; i++) {
949 LogicalDisplay display = mLogicalDisplays.valueAt(i);
950 if (display.getPrimaryDisplayDeviceLocked() == device) {
951 return display;
952 }
953 }
954 return null;
955 }
956
Jeff Brownbd6e1502012-08-28 03:27:37 -0700957 private void sendDisplayEventLocked(int displayId, int event) {
958 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
959 mHandler.sendMessage(msg);
960 }
961
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700962 // Requests that performTraversalsInTransactionFromWindowManager be called at a
963 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -0700964 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800965 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700966 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -0700967 if (!inTraversal) {
968 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
969 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700970 }
971 }
972
973 // Runs on Handler thread.
974 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700975 private void deliverDisplayEvent(int displayId, int event) {
976 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700977 Slog.d(TAG, "Delivering display event: displayId="
978 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700979 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700980
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700981 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700982 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700983 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700984 count = mCallbacks.size();
985 mTempCallbacks.clear();
986 for (int i = 0; i < count; i++) {
987 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -0700988 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700989 }
Craig Mautner4f67ba62012-08-02 11:23:00 -0700990
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700991 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700992 for (int i = 0; i < count; i++) {
993 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
994 }
995 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700996 }
997
Michael Wrightc39d47a2014-07-08 18:07:36 -0700998 private IMediaProjectionManager getProjectionService() {
999 if (mProjectionService == null) {
1000 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1001 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1002 }
1003 return mProjectionService;
1004 }
1005
Jeff Brown4ccb8232014-01-16 22:16:42 -08001006 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001007 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001008
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001009 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001010 pw.println(" mOnlyCode=" + mOnlyCore);
1011 pw.println(" mSafeMode=" + mSafeMode);
1012 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001013 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001014 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1015 pw.println(" mDefaultViewport=" + mDefaultViewport);
1016 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Jeff Brown27f1d672012-10-17 18:32:34 -07001017 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001018 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001019
Jeff Brownbd6e1502012-08-28 03:27:37 -07001020 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001021 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001022
1023 pw.println();
1024 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001025 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001026 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001027 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001028 }
Craig Mautner9de49362012-08-02 14:30:30 -07001029
Jeff Brownbd6e1502012-08-28 03:27:37 -07001030 pw.println();
1031 pw.println("Display Devices: size=" + mDisplayDevices.size());
1032 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001033 pw.println(" " + device.getDisplayDeviceInfoLocked());
1034 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001035 }
1036
1037 final int logicalDisplayCount = mLogicalDisplays.size();
1038 pw.println();
1039 pw.println("Logical Displays: size=" + logicalDisplayCount);
1040 for (int i = 0; i < logicalDisplayCount; i++) {
1041 int displayId = mLogicalDisplays.keyAt(i);
1042 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1043 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001044 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001045 }
Jeff Brownce468a32013-11-21 16:42:03 -08001046
1047 final int callbackCount = mCallbacks.size();
1048 pw.println();
1049 pw.println("Callbacks: size=" + callbackCount);
1050 for (int i = 0; i < callbackCount; i++) {
1051 CallbackRecord callback = mCallbacks.valueAt(i);
1052 pw.println(" " + i + ": mPid=" + callback.mPid
1053 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1054 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001055
1056 if (mDisplayPowerController != null) {
1057 mDisplayPowerController.dump(pw);
1058 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001059 }
1060 }
1061
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001062 /**
1063 * This is the object that everything in the display manager locks on.
1064 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1065 * clear that the object belongs to the display manager service and that it is
1066 * a unique object with a special purpose.
1067 */
1068 public static final class SyncRoot {
1069 }
1070
Jeff Brownbd6e1502012-08-28 03:27:37 -07001071 private final class DisplayManagerHandler extends Handler {
1072 public DisplayManagerHandler(Looper looper) {
1073 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001074 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001075
Jeff Brownbd6e1502012-08-28 03:27:37 -07001076 @Override
1077 public void handleMessage(Message msg) {
1078 switch (msg.what) {
1079 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1080 registerDefaultDisplayAdapter();
1081 break;
1082
1083 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1084 registerAdditionalDisplayAdapters();
1085 break;
1086
1087 case MSG_DELIVER_DISPLAY_EVENT:
1088 deliverDisplayEvent(msg.arg1, msg.arg2);
1089 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001090
1091 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001092 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001093 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001094
1095 case MSG_UPDATE_VIEWPORT: {
1096 synchronized (mSyncRoot) {
1097 mTempDefaultViewport.copyFrom(mDefaultViewport);
1098 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
1099 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001100 mInputManagerInternal.setDisplayViewports(
Jeff Brownd728bf52012-09-08 18:05:28 -07001101 mTempDefaultViewport, mTempExternalTouchViewport);
1102 break;
1103 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001104 }
1105 }
1106 }
1107
1108 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1109 @Override
1110 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1111 switch (event) {
1112 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1113 handleDisplayDeviceAdded(device);
1114 break;
1115
1116 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1117 handleDisplayDeviceChanged(device);
1118 break;
1119
1120 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1121 handleDisplayDeviceRemoved(device);
1122 break;
1123 }
1124 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001125
1126 @Override
1127 public void onTraversalRequested() {
1128 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001129 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001130 }
1131 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001132 }
1133
1134 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001135 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001136 private final IDisplayManagerCallback mCallback;
1137
Jeff Brownce468a32013-11-21 16:42:03 -08001138 public boolean mWifiDisplayScanRequested;
1139
Jeff Brownbd6e1502012-08-28 03:27:37 -07001140 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1141 mPid = pid;
1142 mCallback = callback;
1143 }
1144
1145 @Override
1146 public void binderDied() {
1147 if (DEBUG) {
1148 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1149 }
Jeff Brownce468a32013-11-21 16:42:03 -08001150 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001151 }
1152
1153 public void notifyDisplayEventAsync(int displayId, int event) {
1154 try {
1155 mCallback.onDisplayEvent(displayId, event);
1156 } catch (RemoteException ex) {
1157 Slog.w(TAG, "Failed to notify process "
1158 + mPid + " that displays changed, assuming it died.", ex);
1159 binderDied();
1160 }
1161 }
1162 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001163
1164 private final class BinderService extends IDisplayManager.Stub {
1165 /**
1166 * Returns information about the specified logical display.
1167 *
1168 * @param displayId The logical display id.
1169 * @return The logical display info, or null if the display does not exist. The
1170 * returned object must be treated as immutable.
1171 */
1172 @Override // Binder call
1173 public DisplayInfo getDisplayInfo(int displayId) {
1174 final int callingUid = Binder.getCallingUid();
1175 final long token = Binder.clearCallingIdentity();
1176 try {
1177 return getDisplayInfoInternal(displayId, callingUid);
1178 } finally {
1179 Binder.restoreCallingIdentity(token);
1180 }
1181 }
1182
1183 /**
1184 * Returns the list of all display ids.
1185 */
1186 @Override // Binder call
1187 public int[] getDisplayIds() {
1188 final int callingUid = Binder.getCallingUid();
1189 final long token = Binder.clearCallingIdentity();
1190 try {
1191 return getDisplayIdsInternal(callingUid);
1192 } finally {
1193 Binder.restoreCallingIdentity(token);
1194 }
1195 }
1196
1197 @Override // Binder call
1198 public void registerCallback(IDisplayManagerCallback callback) {
1199 if (callback == null) {
1200 throw new IllegalArgumentException("listener must not be null");
1201 }
1202
1203 final int callingPid = Binder.getCallingPid();
1204 final long token = Binder.clearCallingIdentity();
1205 try {
1206 registerCallbackInternal(callback, callingPid);
1207 } finally {
1208 Binder.restoreCallingIdentity(token);
1209 }
1210 }
1211
1212 @Override // Binder call
1213 public void startWifiDisplayScan() {
1214 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1215 "Permission required to start wifi display scans");
1216
1217 final int callingPid = Binder.getCallingPid();
1218 final long token = Binder.clearCallingIdentity();
1219 try {
1220 startWifiDisplayScanInternal(callingPid);
1221 } finally {
1222 Binder.restoreCallingIdentity(token);
1223 }
1224 }
1225
1226 @Override // Binder call
1227 public void stopWifiDisplayScan() {
1228 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1229 "Permission required to stop wifi display scans");
1230
1231 final int callingPid = Binder.getCallingPid();
1232 final long token = Binder.clearCallingIdentity();
1233 try {
1234 stopWifiDisplayScanInternal(callingPid);
1235 } finally {
1236 Binder.restoreCallingIdentity(token);
1237 }
1238 }
1239
1240 @Override // Binder call
1241 public void connectWifiDisplay(String address) {
1242 if (address == null) {
1243 throw new IllegalArgumentException("address must not be null");
1244 }
1245 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1246 "Permission required to connect to a wifi display");
1247
1248 final long token = Binder.clearCallingIdentity();
1249 try {
1250 connectWifiDisplayInternal(address);
1251 } finally {
1252 Binder.restoreCallingIdentity(token);
1253 }
1254 }
1255
1256 @Override // Binder call
1257 public void disconnectWifiDisplay() {
1258 // This request does not require special permissions.
1259 // Any app can request disconnection from the currently active wifi display.
1260 // This exception should no longer be needed once wifi display control moves
1261 // to the media router service.
1262
1263 final long token = Binder.clearCallingIdentity();
1264 try {
1265 disconnectWifiDisplayInternal();
1266 } finally {
1267 Binder.restoreCallingIdentity(token);
1268 }
1269 }
1270
1271 @Override // Binder call
1272 public void renameWifiDisplay(String address, String alias) {
1273 if (address == null) {
1274 throw new IllegalArgumentException("address must not be null");
1275 }
1276 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1277 "Permission required to rename to a wifi display");
1278
1279 final long token = Binder.clearCallingIdentity();
1280 try {
1281 renameWifiDisplayInternal(address, alias);
1282 } finally {
1283 Binder.restoreCallingIdentity(token);
1284 }
1285 }
1286
1287 @Override // Binder call
1288 public void forgetWifiDisplay(String address) {
1289 if (address == null) {
1290 throw new IllegalArgumentException("address must not be null");
1291 }
1292 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1293 "Permission required to forget to a wifi display");
1294
1295 final long token = Binder.clearCallingIdentity();
1296 try {
1297 forgetWifiDisplayInternal(address);
1298 } finally {
1299 Binder.restoreCallingIdentity(token);
1300 }
1301 }
1302
1303 @Override // Binder call
1304 public void pauseWifiDisplay() {
1305 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1306 "Permission required to pause a wifi display session");
1307
1308 final long token = Binder.clearCallingIdentity();
1309 try {
1310 pauseWifiDisplayInternal();
1311 } finally {
1312 Binder.restoreCallingIdentity(token);
1313 }
1314 }
1315
1316 @Override // Binder call
1317 public void resumeWifiDisplay() {
1318 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1319 "Permission required to resume a wifi display session");
1320
1321 final long token = Binder.clearCallingIdentity();
1322 try {
1323 resumeWifiDisplayInternal();
1324 } finally {
1325 Binder.restoreCallingIdentity(token);
1326 }
1327 }
1328
1329 @Override // Binder call
1330 public WifiDisplayStatus getWifiDisplayStatus() {
1331 // This request does not require special permissions.
1332 // Any app can get information about available wifi displays.
1333
1334 final long token = Binder.clearCallingIdentity();
1335 try {
1336 return getWifiDisplayStatusInternal();
1337 } finally {
1338 Binder.restoreCallingIdentity(token);
1339 }
1340 }
1341
1342 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001343 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001344 IMediaProjection projection, String packageName, String name,
1345 int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001346 final int callingUid = Binder.getCallingUid();
1347 if (!validatePackageName(callingUid, packageName)) {
1348 throw new SecurityException("packageName must match the calling uid");
1349 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001350 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001351 throw new IllegalArgumentException("appToken must not be null");
1352 }
1353 if (TextUtils.isEmpty(name)) {
1354 throw new IllegalArgumentException("name must be non-null and non-empty");
1355 }
1356 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1357 throw new IllegalArgumentException("width, height, and densityDpi must be "
1358 + "greater than 0");
1359 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001360
Michael Wright6720be42014-07-29 19:14:16 -07001361 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1362 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1363 }
1364 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1365 flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1366 }
1367
Michael Wrightc39d47a2014-07-08 18:07:36 -07001368 if (projection != null) {
1369 try {
1370 if (!getProjectionService().isValidMediaProjection(projection)) {
1371 throw new SecurityException("Invalid media projection");
1372 }
Michael Wright6720be42014-07-29 19:14:16 -07001373 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001374 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001375 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001376 }
1377 }
1378
Michael Wright6720be42014-07-29 19:14:16 -07001379 if (callingUid != Process.SYSTEM_UID &&
1380 (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001381 if (!canProjectVideo(projection)) {
1382 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1383 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1384 + "MediaProjection token in order to create a screen sharing virtual "
1385 + "display.");
1386 }
1387 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001388 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001389 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001390 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001391 + "or an appropriate MediaProjection token to create a "
1392 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001393 }
1394 }
1395
1396 final long token = Binder.clearCallingIdentity();
1397 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001398 return createVirtualDisplayInternal(callback, projection, callingUid,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001399 packageName, name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001400 } finally {
1401 Binder.restoreCallingIdentity(token);
1402 }
1403 }
1404
1405 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001406 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001407 int width, int height, int densityDpi) {
1408 final long token = Binder.clearCallingIdentity();
1409 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001410 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001411 } finally {
1412 Binder.restoreCallingIdentity(token);
1413 }
1414 }
1415
1416 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001417 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Jeff Brown92207df2014-04-16 13:16:07 -07001418 final long token = Binder.clearCallingIdentity();
1419 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001420 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001421 } finally {
1422 Binder.restoreCallingIdentity(token);
1423 }
1424 }
1425
1426 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001427 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001428 final long token = Binder.clearCallingIdentity();
1429 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001430 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001431 } finally {
1432 Binder.restoreCallingIdentity(token);
1433 }
1434 }
1435
1436 @Override // Binder call
1437 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1438 if (mContext == null
1439 || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
1440 != PackageManager.PERMISSION_GRANTED) {
1441 pw.println("Permission Denial: can't dump DisplayManager from from pid="
1442 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1443 return;
1444 }
1445
1446 final long token = Binder.clearCallingIdentity();
1447 try {
1448 dumpInternal(pw);
1449 } finally {
1450 Binder.restoreCallingIdentity(token);
1451 }
1452 }
1453
1454 private boolean validatePackageName(int uid, String packageName) {
1455 if (packageName != null) {
1456 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1457 if (packageNames != null) {
1458 for (String n : packageNames) {
1459 if (n.equals(packageName)) {
1460 return true;
1461 }
1462 }
1463 }
1464 }
1465 return false;
1466 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001467
1468 private boolean canProjectVideo(IMediaProjection projection) {
1469 if (projection != null) {
1470 try {
1471 if (projection.canProjectVideo()) {
1472 return true;
1473 }
1474 } catch (RemoteException e) {
1475 Slog.e(TAG, "Unable to query projection service for permissions", e);
1476 }
1477 }
1478 if (mContext.checkCallingPermission(
1479 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001480 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001481 return true;
1482 }
1483 return canProjectSecureVideo(projection);
1484 }
1485
1486 private boolean canProjectSecureVideo(IMediaProjection projection) {
1487 if (projection != null) {
1488 try {
1489 if (projection.canProjectSecureVideo()){
1490 return true;
1491 }
1492 } catch (RemoteException e) {
1493 Slog.e(TAG, "Unable to query projection service for permissions", e);
1494 }
1495 }
1496 return mContext.checkCallingPermission(
1497 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001498 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001499 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001500 }
1501
1502 private final class LocalService extends DisplayManagerInternal {
1503 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001504 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001505 SensorManager sensorManager) {
1506 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001507 DisplayBlanker blanker = new DisplayBlanker() {
1508 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001509 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001510 // The order of operations is important for legacy reasons.
1511 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001512 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001513 }
1514
1515 callbacks.onDisplayStateChange(state);
1516
1517 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001518 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001519 }
1520 }
1521 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001522 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001523 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001524 }
1525 }
1526
1527 @Override
1528 public boolean requestPowerState(DisplayPowerRequest request,
1529 boolean waitForNegativeProximity) {
1530 return mDisplayPowerController.requestPowerState(request,
1531 waitForNegativeProximity);
1532 }
1533
1534 @Override
1535 public boolean isProximitySensorAvailable() {
1536 return mDisplayPowerController.isProximitySensorAvailable();
1537 }
1538
1539 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001540 public DisplayInfo getDisplayInfo(int displayId) {
1541 return getDisplayInfoInternal(displayId, Process.myUid());
1542 }
1543
1544 @Override
1545 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1546 if (listener == null) {
1547 throw new IllegalArgumentException("listener must not be null");
1548 }
1549
1550 registerDisplayTransactionListenerInternal(listener);
1551 }
1552
1553 @Override
1554 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1555 if (listener == null) {
1556 throw new IllegalArgumentException("listener must not be null");
1557 }
1558
1559 unregisterDisplayTransactionListenerInternal(listener);
1560 }
1561
1562 @Override
1563 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1564 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1565 }
1566
1567 @Override
1568 public void performTraversalInTransactionFromWindowManager() {
1569 performTraversalInTransactionFromWindowManagerInternal();
1570 }
1571
1572 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001573 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001574 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1575 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1576 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001577 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001578
1579 @Override
1580 public void setDisplayOffsets(int displayId, int x, int y) {
1581 setDisplayOffsetsInternal(displayId, x, y);
1582 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001583 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001584}