blob: 7440b8c47822f2d62ab8298c826b2005248d3495 [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.
349 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700350 if (mGlobalDisplayState == state
351 && mGlobalDisplayBrightness == brightness) {
352 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700353 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700354
355 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
356 + Display.stateToString(state)
357 + ", brightness=" + brightness + ")");
358 mGlobalDisplayState = state;
359 mGlobalDisplayBrightness = brightness;
360 updateGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
361 scheduleTraversalLocked(false);
Jeff Browne75926d2014-09-18 15:24:49 -0700362 }
363
364 // Setting the display power state can take hundreds of milliseconds
365 // to complete so we defer the most expensive part of the work until
366 // after we have exited the critical section to avoid blocking other
367 // threads for a long time.
368 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
369 mTempDisplayStateWorkQueue.get(i).run();
370 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700371 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700372 } finally {
373 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700374 }
375 }
376 }
377
Jeff Brown4ccb8232014-01-16 22:16:42 -0800378 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700379 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800380 LogicalDisplay display = mLogicalDisplays.get(displayId);
381 if (display != null) {
382 DisplayInfo info = display.getDisplayInfoLocked();
383 if (info.hasAccess(callingUid)) {
384 return info;
385 }
386 }
387 return null;
388 }
389 }
390
391 private int[] getDisplayIdsInternal(int callingUid) {
392 synchronized (mSyncRoot) {
393 final int count = mLogicalDisplays.size();
394 int[] displayIds = new int[count];
395 int n = 0;
396 for (int i = 0; i < count; i++) {
397 LogicalDisplay display = mLogicalDisplays.valueAt(i);
398 DisplayInfo info = display.getDisplayInfoLocked();
399 if (info.hasAccess(callingUid)) {
400 displayIds[n++] = mLogicalDisplays.keyAt(i);
401 }
402 }
403 if (n != count) {
404 displayIds = Arrays.copyOfRange(displayIds, 0, n);
405 }
406 return displayIds;
407 }
408 }
409
410 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
411 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700412 if (mCallbacks.get(callingPid) != null) {
413 throw new SecurityException("The calling process has already "
414 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700415 }
416
Jeff Brownbd6e1502012-08-28 03:27:37 -0700417 CallbackRecord record = new CallbackRecord(callingPid, callback);
418 try {
419 IBinder binder = callback.asBinder();
420 binder.linkToDeath(record, 0);
421 } catch (RemoteException ex) {
422 // give up
423 throw new RuntimeException(ex);
424 }
425
426 mCallbacks.put(callingPid, record);
427 }
428 }
429
Jeff Brownce468a32013-11-21 16:42:03 -0800430 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700431 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800432 mCallbacks.remove(record.mPid);
433 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700434 }
435 }
436
Jeff Brown4ccb8232014-01-16 22:16:42 -0800437 private void startWifiDisplayScanInternal(int callingPid) {
438 synchronized (mSyncRoot) {
439 CallbackRecord record = mCallbacks.get(callingPid);
440 if (record == null) {
441 throw new IllegalStateException("The calling process has not "
442 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700443 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800444 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700445 }
446 }
447
Jeff Brownce468a32013-11-21 16:42:03 -0800448 private void startWifiDisplayScanLocked(CallbackRecord record) {
449 if (!record.mWifiDisplayScanRequested) {
450 record.mWifiDisplayScanRequested = true;
451 if (mWifiDisplayScanRequestCount++ == 0) {
452 if (mWifiDisplayAdapter != null) {
453 mWifiDisplayAdapter.requestStartScanLocked();
454 }
455 }
456 }
457 }
458
Jeff Brown4ccb8232014-01-16 22:16:42 -0800459 private void stopWifiDisplayScanInternal(int callingPid) {
460 synchronized (mSyncRoot) {
461 CallbackRecord record = mCallbacks.get(callingPid);
462 if (record == null) {
463 throw new IllegalStateException("The calling process has not "
464 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800465 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800466 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800467 }
468 }
469
470 private void stopWifiDisplayScanLocked(CallbackRecord record) {
471 if (record.mWifiDisplayScanRequested) {
472 record.mWifiDisplayScanRequested = false;
473 if (--mWifiDisplayScanRequestCount == 0) {
474 if (mWifiDisplayAdapter != null) {
475 mWifiDisplayAdapter.requestStopScanLocked();
476 }
477 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700478 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800479 + mWifiDisplayScanRequestCount);
480 mWifiDisplayScanRequestCount = 0;
481 }
482 }
483 }
484
Jeff Brown4ccb8232014-01-16 22:16:42 -0800485 private void connectWifiDisplayInternal(String address) {
486 synchronized (mSyncRoot) {
487 if (mWifiDisplayAdapter != null) {
488 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700489 }
Jeff Browne08ae382012-09-07 20:36:36 -0700490 }
491 }
492
Jeff Brown4ccb8232014-01-16 22:16:42 -0800493 private void pauseWifiDisplayInternal() {
494 synchronized (mSyncRoot) {
495 if (mWifiDisplayAdapter != null) {
496 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700497 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700498 }
499 }
500
Jeff Brown4ccb8232014-01-16 22:16:42 -0800501 private void resumeWifiDisplayInternal() {
502 synchronized (mSyncRoot) {
503 if (mWifiDisplayAdapter != null) {
504 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700505 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700506 }
507 }
508
Jeff Brown4ccb8232014-01-16 22:16:42 -0800509 private void disconnectWifiDisplayInternal() {
510 synchronized (mSyncRoot) {
511 if (mWifiDisplayAdapter != null) {
512 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700513 }
Jeff Browne08ae382012-09-07 20:36:36 -0700514 }
515 }
516
Jeff Brown4ccb8232014-01-16 22:16:42 -0800517 private void renameWifiDisplayInternal(String address, String alias) {
518 synchronized (mSyncRoot) {
519 if (mWifiDisplayAdapter != null) {
520 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700521 }
Jeff Brown89d55462012-09-19 11:33:42 -0700522 }
523 }
524
Jeff Brown4ccb8232014-01-16 22:16:42 -0800525 private void forgetWifiDisplayInternal(String address) {
526 synchronized (mSyncRoot) {
527 if (mWifiDisplayAdapter != null) {
528 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700529 }
Jeff Brown89d55462012-09-19 11:33:42 -0700530 }
531 }
532
Jeff Brown4ccb8232014-01-16 22:16:42 -0800533 private WifiDisplayStatus getWifiDisplayStatusInternal() {
534 synchronized (mSyncRoot) {
535 if (mWifiDisplayAdapter != null) {
536 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700537 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800538 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700539 }
540 }
541
Michael Wright75ee9fc2014-09-01 19:55:22 -0700542 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700543 IMediaProjection projection, int callingUid, String packageName,
Jeff Brown7d00aff2013-08-02 19:03:49 -0700544 String name, int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800545 synchronized (mSyncRoot) {
546 if (mVirtualDisplayAdapter == null) {
547 Slog.w(TAG, "Rejecting request to create private virtual display "
548 + "because the virtual display adapter is not available.");
549 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700550 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800551
552 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Michael Wright75ee9fc2014-09-01 19:55:22 -0700553 callback, projection, callingUid, packageName,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700554 name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800555 if (device == null) {
556 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700557 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700558
Jeff Brown4ccb8232014-01-16 22:16:42 -0800559 handleDisplayDeviceAddedLocked(device);
560 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
561 if (display != null) {
562 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700563 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800564
565 // Something weird happened and the logical display was not created.
566 Slog.w(TAG, "Rejecting request to create virtual display "
567 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700568 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800569 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700570 }
571 return -1;
572 }
573
Michael Wright01e840f2014-06-26 16:03:25 -0700574 private void resizeVirtualDisplayInternal(IBinder appToken,
575 int width, int height, int densityDpi) {
576 synchronized (mSyncRoot) {
577 if (mVirtualDisplayAdapter == null) {
578 return;
579 }
580
581 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
582 }
583 }
584
Jeff Brown92207df2014-04-16 13:16:07 -0700585 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
586 synchronized (mSyncRoot) {
587 if (mVirtualDisplayAdapter == null) {
588 return;
589 }
590
591 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
592 }
593 }
594
Jeff Brown4ccb8232014-01-16 22:16:42 -0800595 private void releaseVirtualDisplayInternal(IBinder appToken) {
596 synchronized (mSyncRoot) {
597 if (mVirtualDisplayAdapter == null) {
598 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700599 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700600
Jeff Brown4ccb8232014-01-16 22:16:42 -0800601 DisplayDevice device =
602 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
603 if (device != null) {
604 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700605 }
606 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700607 }
608
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700609 private void registerDefaultDisplayAdapter() {
610 // Register default display adapter.
611 synchronized (mSyncRoot) {
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800612 registerDisplayAdapterLocked(new LocalDisplayAdapter(
613 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700614 }
615 }
616
617 private void registerAdditionalDisplayAdapters() {
618 synchronized (mSyncRoot) {
619 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700620 registerOverlayDisplayAdapterLocked();
621 registerWifiDisplayAdapterLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700622 registerVirtualDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700623 }
624 }
625 }
626
Jeff Brown89d55462012-09-19 11:33:42 -0700627 private void registerOverlayDisplayAdapterLocked() {
628 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
629 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
630 }
631
632 private void registerWifiDisplayAdapterLocked() {
633 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700634 com.android.internal.R.bool.config_enableWifiDisplay)
635 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700636 mWifiDisplayAdapter = new WifiDisplayAdapter(
637 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
638 mPersistentDataStore);
639 registerDisplayAdapterLocked(mWifiDisplayAdapter);
640 }
641 }
642
Jeff Browna506a6e2013-06-04 00:02:38 -0700643 private void registerVirtualDisplayAdapterLocked() {
644 mVirtualDisplayAdapter = new VirtualDisplayAdapter(
645 mSyncRoot, mContext, mHandler, mDisplayAdapterListener);
646 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
647 }
648
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700649 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
650 // In safe mode, we disable non-essential display adapters to give the user
651 // an opportunity to fix broken settings or other problems that might affect
652 // system stability.
653 // In only-core mode, we disable non-essential display adapters to minimize
654 // the number of dependencies that are started while in this mode and to
655 // prevent problems that might occur due to the device being encrypted.
656 return !mSafeMode && !mOnlyCore;
657 }
658
659 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
660 mDisplayAdapters.add(adapter);
661 adapter.registerLocked();
662 }
663
Jeff Brownbd6e1502012-08-28 03:27:37 -0700664 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700665 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700666 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700667 }
668 }
669
Jeff Browna506a6e2013-06-04 00:02:38 -0700670 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700671 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700672 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700673 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700674 return;
675 }
676
Jeff Brown10acf6d2015-04-14 14:20:47 -0700677 Slog.i(TAG, "Display device added: " + info);
678 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700679
680 mDisplayDevices.add(device);
681 addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700682 Runnable work = updateDisplayStateLocked(device);
683 if (work != null) {
684 work.run();
685 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700686 scheduleTraversalLocked(false);
687 }
688
Jeff Brownbd6e1502012-08-28 03:27:37 -0700689 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700690 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700691 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700692 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700693 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700694 return;
695 }
696
Jeff Brown10acf6d2015-04-14 14:20:47 -0700697 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
698 if (diff == DisplayDeviceInfo.DIFF_STATE) {
699 Slog.i(TAG, "Display device changed state: \"" + info.name
700 + "\", " + Display.stateToString(info.state));
701 } else if (diff != 0) {
702 Slog.i(TAG, "Display device changed: " + info);
703 }
704 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700705
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700706 device.applyPendingDisplayDeviceInfoChangesLocked();
707 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700708 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700709 }
710 }
711 }
712
Jeff Brownbd6e1502012-08-28 03:27:37 -0700713 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700714 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700715 handleDisplayDeviceRemovedLocked(device);
716 }
717 }
718 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700719 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700720 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700721 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700722 return;
723 }
724
Jeff Brown10acf6d2015-04-14 14:20:47 -0700725 Slog.i(TAG, "Display device removed: " + info);
726 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700727
Jeff Browna506a6e2013-06-04 00:02:38 -0700728 updateLogicalDisplaysLocked();
729 scheduleTraversalLocked(false);
730 }
731
Jeff Browne75926d2014-09-18 15:24:49 -0700732 private void updateGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700733 final int count = mDisplayDevices.size();
734 for (int i = 0; i < count; i++) {
735 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700736 Runnable runnable = updateDisplayStateLocked(device);
737 if (runnable != null) {
738 workQueue.add(runnable);
739 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700740 }
741 }
742
Jeff Browne75926d2014-09-18 15:24:49 -0700743 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700744 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700745 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700746 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
747 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700748 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700749 }
Jeff Browne75926d2014-09-18 15:24:49 -0700750 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700751 }
752
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700753 // Adds a new logical display based on the given display device.
754 // Sends notifications if needed.
755 private void addLogicalDisplayLocked(DisplayDevice device) {
756 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
757 boolean isDefault = (deviceInfo.flags
758 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
759 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
760 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
761 isDefault = false;
762 }
763
Jeff Brown27f1d672012-10-17 18:32:34 -0700764 if (!isDefault && mSingleDisplayDemoMode) {
765 Slog.i(TAG, "Not creating a logical display for a secondary display "
766 + " because single display demo mode is enabled: " + deviceInfo);
767 return;
768 }
769
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700770 final int displayId = assignDisplayIdLocked(isDefault);
771 final int layerStack = assignLayerStackLocked(displayId);
772
Jeff Brownd728bf52012-09-08 18:05:28 -0700773 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700774 display.updateLocked(mDisplayDevices);
775 if (!display.isValidLocked()) {
776 // This should never happen currently.
777 Slog.w(TAG, "Ignoring display device because the logical display "
778 + "created from it was not considered valid: " + deviceInfo);
779 return;
780 }
781
782 mLogicalDisplays.put(displayId, display);
783
784 // Wake up waitForDefaultDisplay.
785 if (isDefault) {
786 mSyncRoot.notifyAll();
787 }
788
789 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
790 }
791
792 private int assignDisplayIdLocked(boolean isDefault) {
793 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
794 }
795
796 private int assignLayerStackLocked(int displayId) {
797 // Currently layer stacks and display ids are the same.
798 // This need not be the case.
799 return displayId;
800 }
801
802 // Updates all existing logical displays given the current set of display devices.
803 // Removes invalid logical displays.
804 // Sends notifications if needed.
805 private boolean updateLogicalDisplaysLocked() {
806 boolean changed = false;
807 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
808 final int displayId = mLogicalDisplays.keyAt(i);
809 LogicalDisplay display = mLogicalDisplays.valueAt(i);
810
811 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
812 display.updateLocked(mDisplayDevices);
813 if (!display.isValidLocked()) {
814 mLogicalDisplays.removeAt(i);
815 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
816 changed = true;
817 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
818 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
819 changed = true;
820 }
821 }
822 return changed;
823 }
824
825 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700826 // Clear all viewports before configuring displays so that we can keep
827 // track of which ones we have configured.
828 clearViewportsLocked();
829
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700830 // Configure each display device.
831 final int count = mDisplayDevices.size();
832 for (int i = 0; i < count; i++) {
833 DisplayDevice device = mDisplayDevices.get(i);
834 configureDisplayInTransactionLocked(device);
835 device.performTraversalInTransactionLocked();
836 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700837
838 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800839 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700840 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
841 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700842 }
843
Michael Wright3f145a22014-07-22 19:46:03 -0700844 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700845 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700846 synchronized (mSyncRoot) {
847 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700848 if (display == null) {
849 return;
850 }
851 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700852 if (DEBUG) {
853 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
854 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
855 }
856
Craig Mautner722285e2012-09-07 13:55:58 -0700857 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700858 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700859 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700860 if (requestedModeId == 0 && requestedRefreshRate != 0) {
861 // Scan supported modes returned by display.getInfo() to find a mode with the same
862 // size as the default display mode but with the specified refresh rate instead.
863 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
864 requestedRefreshRate);
865 }
866 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700867 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700868 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700869 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700870 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700871 scheduleTraversalLocked(inTraversal);
872 }
Craig Mautner722285e2012-09-07 13:55:58 -0700873 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700874 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700875
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800876 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
877 synchronized (mSyncRoot) {
878 LogicalDisplay display = mLogicalDisplays.get(displayId);
879 if (display == null) {
880 return;
881 }
882 if (display.getDisplayOffsetXLocked() != x
883 || display.getDisplayOffsetYLocked() != y) {
884 if (DEBUG) {
885 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
886 + x + ", " + y + ")");
887 }
888 display.setDisplayOffsetsLocked(x, y);
889 scheduleTraversalLocked(false);
890 }
891 }
892 }
893
Jeff Brownd728bf52012-09-08 18:05:28 -0700894 private void clearViewportsLocked() {
895 mDefaultViewport.valid = false;
896 mExternalTouchViewport.valid = false;
Craig Mautner722285e2012-09-07 13:55:58 -0700897 }
898
899 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -0800900 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
901 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -0700902
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700903 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -0800904 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -0700905 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -0800906 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700907 if (display != null && !display.hasContentLocked()) {
908 // If the display does not have any content of its own, then
909 // automatically mirror the default logical display contents.
910 display = null;
911 }
912 if (display == null) {
913 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
914 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700915 }
916
917 // Apply the logical display configuration to the display device.
918 if (display == null) {
919 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -0700920 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -0700922 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700923 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700924 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -0700925
926 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -0700927 if (!mDefaultViewport.valid
928 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
929 setViewportLocked(mDefaultViewport, display, device);
930 }
931 if (!mExternalTouchViewport.valid
932 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
933 setViewportLocked(mExternalTouchViewport, display, device);
934 }
935 }
936
937 private static void setViewportLocked(DisplayViewport viewport,
938 LogicalDisplay display, DisplayDevice device) {
939 viewport.valid = true;
940 viewport.displayId = display.getDisplayIdLocked();
941 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700942 }
943
944 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
945 final int count = mLogicalDisplays.size();
946 for (int i = 0; i < count; i++) {
947 LogicalDisplay display = mLogicalDisplays.valueAt(i);
948 if (display.getPrimaryDisplayDeviceLocked() == device) {
949 return display;
950 }
951 }
952 return null;
953 }
954
Jeff Brownbd6e1502012-08-28 03:27:37 -0700955 private void sendDisplayEventLocked(int displayId, int event) {
956 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
957 mHandler.sendMessage(msg);
958 }
959
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700960 // Requests that performTraversalsInTransactionFromWindowManager be called at a
961 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -0700962 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800963 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700964 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -0700965 if (!inTraversal) {
966 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
967 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700968 }
969 }
970
971 // Runs on Handler thread.
972 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700973 private void deliverDisplayEvent(int displayId, int event) {
974 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700975 Slog.d(TAG, "Delivering display event: displayId="
976 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700977 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700978
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700979 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700980 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700981 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700982 count = mCallbacks.size();
983 mTempCallbacks.clear();
984 for (int i = 0; i < count; i++) {
985 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -0700986 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700987 }
Craig Mautner4f67ba62012-08-02 11:23:00 -0700988
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700989 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700990 for (int i = 0; i < count; i++) {
991 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
992 }
993 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700994 }
995
Michael Wrightc39d47a2014-07-08 18:07:36 -0700996 private IMediaProjectionManager getProjectionService() {
997 if (mProjectionService == null) {
998 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
999 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1000 }
1001 return mProjectionService;
1002 }
1003
Jeff Brown4ccb8232014-01-16 22:16:42 -08001004 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001005 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001006
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001007 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001008 pw.println(" mOnlyCode=" + mOnlyCore);
1009 pw.println(" mSafeMode=" + mSafeMode);
1010 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001011 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001012 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1013 pw.println(" mDefaultViewport=" + mDefaultViewport);
1014 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Jeff Brown27f1d672012-10-17 18:32:34 -07001015 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001016 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001017
Jeff Brownbd6e1502012-08-28 03:27:37 -07001018 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001019 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001020
1021 pw.println();
1022 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001023 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001024 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001025 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001026 }
Craig Mautner9de49362012-08-02 14:30:30 -07001027
Jeff Brownbd6e1502012-08-28 03:27:37 -07001028 pw.println();
1029 pw.println("Display Devices: size=" + mDisplayDevices.size());
1030 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001031 pw.println(" " + device.getDisplayDeviceInfoLocked());
1032 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001033 }
1034
1035 final int logicalDisplayCount = mLogicalDisplays.size();
1036 pw.println();
1037 pw.println("Logical Displays: size=" + logicalDisplayCount);
1038 for (int i = 0; i < logicalDisplayCount; i++) {
1039 int displayId = mLogicalDisplays.keyAt(i);
1040 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1041 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001042 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001043 }
Jeff Brownce468a32013-11-21 16:42:03 -08001044
1045 final int callbackCount = mCallbacks.size();
1046 pw.println();
1047 pw.println("Callbacks: size=" + callbackCount);
1048 for (int i = 0; i < callbackCount; i++) {
1049 CallbackRecord callback = mCallbacks.valueAt(i);
1050 pw.println(" " + i + ": mPid=" + callback.mPid
1051 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1052 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001053
1054 if (mDisplayPowerController != null) {
1055 mDisplayPowerController.dump(pw);
1056 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001057 }
1058 }
1059
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001060 /**
1061 * This is the object that everything in the display manager locks on.
1062 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1063 * clear that the object belongs to the display manager service and that it is
1064 * a unique object with a special purpose.
1065 */
1066 public static final class SyncRoot {
1067 }
1068
Jeff Brownbd6e1502012-08-28 03:27:37 -07001069 private final class DisplayManagerHandler extends Handler {
1070 public DisplayManagerHandler(Looper looper) {
1071 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001072 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001073
Jeff Brownbd6e1502012-08-28 03:27:37 -07001074 @Override
1075 public void handleMessage(Message msg) {
1076 switch (msg.what) {
1077 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1078 registerDefaultDisplayAdapter();
1079 break;
1080
1081 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1082 registerAdditionalDisplayAdapters();
1083 break;
1084
1085 case MSG_DELIVER_DISPLAY_EVENT:
1086 deliverDisplayEvent(msg.arg1, msg.arg2);
1087 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001088
1089 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001090 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001091 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001092
1093 case MSG_UPDATE_VIEWPORT: {
1094 synchronized (mSyncRoot) {
1095 mTempDefaultViewport.copyFrom(mDefaultViewport);
1096 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
1097 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001098 mInputManagerInternal.setDisplayViewports(
Jeff Brownd728bf52012-09-08 18:05:28 -07001099 mTempDefaultViewport, mTempExternalTouchViewport);
1100 break;
1101 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001102 }
1103 }
1104 }
1105
1106 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1107 @Override
1108 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1109 switch (event) {
1110 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1111 handleDisplayDeviceAdded(device);
1112 break;
1113
1114 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1115 handleDisplayDeviceChanged(device);
1116 break;
1117
1118 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1119 handleDisplayDeviceRemoved(device);
1120 break;
1121 }
1122 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001123
1124 @Override
1125 public void onTraversalRequested() {
1126 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001127 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001128 }
1129 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001130 }
1131
1132 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001133 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001134 private final IDisplayManagerCallback mCallback;
1135
Jeff Brownce468a32013-11-21 16:42:03 -08001136 public boolean mWifiDisplayScanRequested;
1137
Jeff Brownbd6e1502012-08-28 03:27:37 -07001138 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1139 mPid = pid;
1140 mCallback = callback;
1141 }
1142
1143 @Override
1144 public void binderDied() {
1145 if (DEBUG) {
1146 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1147 }
Jeff Brownce468a32013-11-21 16:42:03 -08001148 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001149 }
1150
1151 public void notifyDisplayEventAsync(int displayId, int event) {
1152 try {
1153 mCallback.onDisplayEvent(displayId, event);
1154 } catch (RemoteException ex) {
1155 Slog.w(TAG, "Failed to notify process "
1156 + mPid + " that displays changed, assuming it died.", ex);
1157 binderDied();
1158 }
1159 }
1160 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001161
1162 private final class BinderService extends IDisplayManager.Stub {
1163 /**
1164 * Returns information about the specified logical display.
1165 *
1166 * @param displayId The logical display id.
1167 * @return The logical display info, or null if the display does not exist. The
1168 * returned object must be treated as immutable.
1169 */
1170 @Override // Binder call
1171 public DisplayInfo getDisplayInfo(int displayId) {
1172 final int callingUid = Binder.getCallingUid();
1173 final long token = Binder.clearCallingIdentity();
1174 try {
1175 return getDisplayInfoInternal(displayId, callingUid);
1176 } finally {
1177 Binder.restoreCallingIdentity(token);
1178 }
1179 }
1180
1181 /**
1182 * Returns the list of all display ids.
1183 */
1184 @Override // Binder call
1185 public int[] getDisplayIds() {
1186 final int callingUid = Binder.getCallingUid();
1187 final long token = Binder.clearCallingIdentity();
1188 try {
1189 return getDisplayIdsInternal(callingUid);
1190 } finally {
1191 Binder.restoreCallingIdentity(token);
1192 }
1193 }
1194
1195 @Override // Binder call
1196 public void registerCallback(IDisplayManagerCallback callback) {
1197 if (callback == null) {
1198 throw new IllegalArgumentException("listener must not be null");
1199 }
1200
1201 final int callingPid = Binder.getCallingPid();
1202 final long token = Binder.clearCallingIdentity();
1203 try {
1204 registerCallbackInternal(callback, callingPid);
1205 } finally {
1206 Binder.restoreCallingIdentity(token);
1207 }
1208 }
1209
1210 @Override // Binder call
1211 public void startWifiDisplayScan() {
1212 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1213 "Permission required to start wifi display scans");
1214
1215 final int callingPid = Binder.getCallingPid();
1216 final long token = Binder.clearCallingIdentity();
1217 try {
1218 startWifiDisplayScanInternal(callingPid);
1219 } finally {
1220 Binder.restoreCallingIdentity(token);
1221 }
1222 }
1223
1224 @Override // Binder call
1225 public void stopWifiDisplayScan() {
1226 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1227 "Permission required to stop wifi display scans");
1228
1229 final int callingPid = Binder.getCallingPid();
1230 final long token = Binder.clearCallingIdentity();
1231 try {
1232 stopWifiDisplayScanInternal(callingPid);
1233 } finally {
1234 Binder.restoreCallingIdentity(token);
1235 }
1236 }
1237
1238 @Override // Binder call
1239 public void connectWifiDisplay(String address) {
1240 if (address == null) {
1241 throw new IllegalArgumentException("address must not be null");
1242 }
1243 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1244 "Permission required to connect to a wifi display");
1245
1246 final long token = Binder.clearCallingIdentity();
1247 try {
1248 connectWifiDisplayInternal(address);
1249 } finally {
1250 Binder.restoreCallingIdentity(token);
1251 }
1252 }
1253
1254 @Override // Binder call
1255 public void disconnectWifiDisplay() {
1256 // This request does not require special permissions.
1257 // Any app can request disconnection from the currently active wifi display.
1258 // This exception should no longer be needed once wifi display control moves
1259 // to the media router service.
1260
1261 final long token = Binder.clearCallingIdentity();
1262 try {
1263 disconnectWifiDisplayInternal();
1264 } finally {
1265 Binder.restoreCallingIdentity(token);
1266 }
1267 }
1268
1269 @Override // Binder call
1270 public void renameWifiDisplay(String address, String alias) {
1271 if (address == null) {
1272 throw new IllegalArgumentException("address must not be null");
1273 }
1274 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1275 "Permission required to rename to a wifi display");
1276
1277 final long token = Binder.clearCallingIdentity();
1278 try {
1279 renameWifiDisplayInternal(address, alias);
1280 } finally {
1281 Binder.restoreCallingIdentity(token);
1282 }
1283 }
1284
1285 @Override // Binder call
1286 public void forgetWifiDisplay(String address) {
1287 if (address == null) {
1288 throw new IllegalArgumentException("address must not be null");
1289 }
1290 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1291 "Permission required to forget to a wifi display");
1292
1293 final long token = Binder.clearCallingIdentity();
1294 try {
1295 forgetWifiDisplayInternal(address);
1296 } finally {
1297 Binder.restoreCallingIdentity(token);
1298 }
1299 }
1300
1301 @Override // Binder call
1302 public void pauseWifiDisplay() {
1303 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1304 "Permission required to pause a wifi display session");
1305
1306 final long token = Binder.clearCallingIdentity();
1307 try {
1308 pauseWifiDisplayInternal();
1309 } finally {
1310 Binder.restoreCallingIdentity(token);
1311 }
1312 }
1313
1314 @Override // Binder call
1315 public void resumeWifiDisplay() {
1316 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1317 "Permission required to resume a wifi display session");
1318
1319 final long token = Binder.clearCallingIdentity();
1320 try {
1321 resumeWifiDisplayInternal();
1322 } finally {
1323 Binder.restoreCallingIdentity(token);
1324 }
1325 }
1326
1327 @Override // Binder call
1328 public WifiDisplayStatus getWifiDisplayStatus() {
1329 // This request does not require special permissions.
1330 // Any app can get information about available wifi displays.
1331
1332 final long token = Binder.clearCallingIdentity();
1333 try {
1334 return getWifiDisplayStatusInternal();
1335 } finally {
1336 Binder.restoreCallingIdentity(token);
1337 }
1338 }
1339
1340 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001341 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001342 IMediaProjection projection, String packageName, String name,
1343 int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001344 final int callingUid = Binder.getCallingUid();
1345 if (!validatePackageName(callingUid, packageName)) {
1346 throw new SecurityException("packageName must match the calling uid");
1347 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001348 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001349 throw new IllegalArgumentException("appToken must not be null");
1350 }
1351 if (TextUtils.isEmpty(name)) {
1352 throw new IllegalArgumentException("name must be non-null and non-empty");
1353 }
1354 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1355 throw new IllegalArgumentException("width, height, and densityDpi must be "
1356 + "greater than 0");
1357 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001358
Michael Wright6720be42014-07-29 19:14:16 -07001359 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1360 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1361 }
1362 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1363 flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1364 }
1365
Michael Wrightc39d47a2014-07-08 18:07:36 -07001366 if (projection != null) {
1367 try {
1368 if (!getProjectionService().isValidMediaProjection(projection)) {
1369 throw new SecurityException("Invalid media projection");
1370 }
Michael Wright6720be42014-07-29 19:14:16 -07001371 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001372 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001373 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001374 }
1375 }
1376
Michael Wright6720be42014-07-29 19:14:16 -07001377 if (callingUid != Process.SYSTEM_UID &&
1378 (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001379 if (!canProjectVideo(projection)) {
1380 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1381 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1382 + "MediaProjection token in order to create a screen sharing virtual "
1383 + "display.");
1384 }
1385 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001386 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001387 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001388 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001389 + "or an appropriate MediaProjection token to create a "
1390 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001391 }
1392 }
1393
1394 final long token = Binder.clearCallingIdentity();
1395 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001396 return createVirtualDisplayInternal(callback, projection, callingUid,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001397 packageName, name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001398 } finally {
1399 Binder.restoreCallingIdentity(token);
1400 }
1401 }
1402
1403 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001404 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001405 int width, int height, int densityDpi) {
1406 final long token = Binder.clearCallingIdentity();
1407 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001408 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001409 } finally {
1410 Binder.restoreCallingIdentity(token);
1411 }
1412 }
1413
1414 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001415 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Jeff Brown92207df2014-04-16 13:16:07 -07001416 final long token = Binder.clearCallingIdentity();
1417 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001418 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001419 } finally {
1420 Binder.restoreCallingIdentity(token);
1421 }
1422 }
1423
1424 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001425 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001426 final long token = Binder.clearCallingIdentity();
1427 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001428 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001429 } finally {
1430 Binder.restoreCallingIdentity(token);
1431 }
1432 }
1433
1434 @Override // Binder call
1435 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1436 if (mContext == null
1437 || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
1438 != PackageManager.PERMISSION_GRANTED) {
1439 pw.println("Permission Denial: can't dump DisplayManager from from pid="
1440 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1441 return;
1442 }
1443
1444 final long token = Binder.clearCallingIdentity();
1445 try {
1446 dumpInternal(pw);
1447 } finally {
1448 Binder.restoreCallingIdentity(token);
1449 }
1450 }
1451
1452 private boolean validatePackageName(int uid, String packageName) {
1453 if (packageName != null) {
1454 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1455 if (packageNames != null) {
1456 for (String n : packageNames) {
1457 if (n.equals(packageName)) {
1458 return true;
1459 }
1460 }
1461 }
1462 }
1463 return false;
1464 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001465
1466 private boolean canProjectVideo(IMediaProjection projection) {
1467 if (projection != null) {
1468 try {
1469 if (projection.canProjectVideo()) {
1470 return true;
1471 }
1472 } catch (RemoteException e) {
1473 Slog.e(TAG, "Unable to query projection service for permissions", e);
1474 }
1475 }
1476 if (mContext.checkCallingPermission(
1477 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001478 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001479 return true;
1480 }
1481 return canProjectSecureVideo(projection);
1482 }
1483
1484 private boolean canProjectSecureVideo(IMediaProjection projection) {
1485 if (projection != null) {
1486 try {
1487 if (projection.canProjectSecureVideo()){
1488 return true;
1489 }
1490 } catch (RemoteException e) {
1491 Slog.e(TAG, "Unable to query projection service for permissions", e);
1492 }
1493 }
1494 return mContext.checkCallingPermission(
1495 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001496 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001497 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001498 }
1499
1500 private final class LocalService extends DisplayManagerInternal {
1501 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001502 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001503 SensorManager sensorManager) {
1504 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001505 DisplayBlanker blanker = new DisplayBlanker() {
1506 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001507 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001508 // The order of operations is important for legacy reasons.
1509 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001510 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001511 }
1512
1513 callbacks.onDisplayStateChange(state);
1514
1515 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001516 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001517 }
1518 }
1519 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001520 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001521 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001522 }
1523 }
1524
1525 @Override
1526 public boolean requestPowerState(DisplayPowerRequest request,
1527 boolean waitForNegativeProximity) {
1528 return mDisplayPowerController.requestPowerState(request,
1529 waitForNegativeProximity);
1530 }
1531
1532 @Override
1533 public boolean isProximitySensorAvailable() {
1534 return mDisplayPowerController.isProximitySensorAvailable();
1535 }
1536
1537 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001538 public DisplayInfo getDisplayInfo(int displayId) {
1539 return getDisplayInfoInternal(displayId, Process.myUid());
1540 }
1541
1542 @Override
1543 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1544 if (listener == null) {
1545 throw new IllegalArgumentException("listener must not be null");
1546 }
1547
1548 registerDisplayTransactionListenerInternal(listener);
1549 }
1550
1551 @Override
1552 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1553 if (listener == null) {
1554 throw new IllegalArgumentException("listener must not be null");
1555 }
1556
1557 unregisterDisplayTransactionListenerInternal(listener);
1558 }
1559
1560 @Override
1561 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1562 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1563 }
1564
1565 @Override
1566 public void performTraversalInTransactionFromWindowManager() {
1567 performTraversalInTransactionFromWindowManagerInternal();
1568 }
1569
1570 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001571 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001572 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1573 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1574 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001575 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001576
1577 @Override
1578 public void setDisplayOffsets(int displayId, int x, int y) {
1579 setDisplayOffsetsInternal(displayId, x, y);
1580 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001581 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001582}