blob: 9ea0444818aa794577d5ea393e9066327db37bc3 [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) {
671 if (mDisplayDevices.contains(device)) {
672 Slog.w(TAG, "Attempted to add already added display device: "
673 + device.getDisplayDeviceInfoLocked());
674 return;
675 }
676
677 Slog.i(TAG, "Display device added: " + device.getDisplayDeviceInfoLocked());
678
679 mDisplayDevices.add(device);
680 addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700681 Runnable work = updateDisplayStateLocked(device);
682 if (work != null) {
683 work.run();
684 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700685 scheduleTraversalLocked(false);
686 }
687
Jeff Brownbd6e1502012-08-28 03:27:37 -0700688 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700689 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700690 if (!mDisplayDevices.contains(device)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700691 Slog.w(TAG, "Attempted to change non-existent display device: "
692 + device.getDisplayDeviceInfoLocked());
Jeff Brownbd6e1502012-08-28 03:27:37 -0700693 return;
694 }
695
Jeff Browne87bf032012-09-20 18:30:13 -0700696 Slog.i(TAG, "Display device changed: " + device.getDisplayDeviceInfoLocked());
697
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700698 device.applyPendingDisplayDeviceInfoChangesLocked();
699 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700700 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700701 }
702 }
703 }
704
Jeff Brownbd6e1502012-08-28 03:27:37 -0700705 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700706 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700707 handleDisplayDeviceRemovedLocked(device);
708 }
709 }
710 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
711 if (!mDisplayDevices.remove(device)) {
712 Slog.w(TAG, "Attempted to remove non-existent display device: "
713 + device.getDisplayDeviceInfoLocked());
714 return;
715 }
716
717 Slog.i(TAG, "Display device removed: " + device.getDisplayDeviceInfoLocked());
718
Jeff Browna506a6e2013-06-04 00:02:38 -0700719 updateLogicalDisplaysLocked();
720 scheduleTraversalLocked(false);
721 }
722
Jeff Browne75926d2014-09-18 15:24:49 -0700723 private void updateGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700724 final int count = mDisplayDevices.size();
725 for (int i = 0; i < count; i++) {
726 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700727 Runnable runnable = updateDisplayStateLocked(device);
728 if (runnable != null) {
729 workQueue.add(runnable);
730 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700731 }
732 }
733
Jeff Browne75926d2014-09-18 15:24:49 -0700734 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700735 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700736 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700737 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
738 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700739 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700740 }
Jeff Browne75926d2014-09-18 15:24:49 -0700741 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700742 }
743
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700744 // Adds a new logical display based on the given display device.
745 // Sends notifications if needed.
746 private void addLogicalDisplayLocked(DisplayDevice device) {
747 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
748 boolean isDefault = (deviceInfo.flags
749 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
750 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
751 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
752 isDefault = false;
753 }
754
Jeff Brown27f1d672012-10-17 18:32:34 -0700755 if (!isDefault && mSingleDisplayDemoMode) {
756 Slog.i(TAG, "Not creating a logical display for a secondary display "
757 + " because single display demo mode is enabled: " + deviceInfo);
758 return;
759 }
760
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700761 final int displayId = assignDisplayIdLocked(isDefault);
762 final int layerStack = assignLayerStackLocked(displayId);
763
Jeff Brownd728bf52012-09-08 18:05:28 -0700764 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700765 display.updateLocked(mDisplayDevices);
766 if (!display.isValidLocked()) {
767 // This should never happen currently.
768 Slog.w(TAG, "Ignoring display device because the logical display "
769 + "created from it was not considered valid: " + deviceInfo);
770 return;
771 }
772
773 mLogicalDisplays.put(displayId, display);
774
775 // Wake up waitForDefaultDisplay.
776 if (isDefault) {
777 mSyncRoot.notifyAll();
778 }
779
780 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
781 }
782
783 private int assignDisplayIdLocked(boolean isDefault) {
784 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
785 }
786
787 private int assignLayerStackLocked(int displayId) {
788 // Currently layer stacks and display ids are the same.
789 // This need not be the case.
790 return displayId;
791 }
792
793 // Updates all existing logical displays given the current set of display devices.
794 // Removes invalid logical displays.
795 // Sends notifications if needed.
796 private boolean updateLogicalDisplaysLocked() {
797 boolean changed = false;
798 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
799 final int displayId = mLogicalDisplays.keyAt(i);
800 LogicalDisplay display = mLogicalDisplays.valueAt(i);
801
802 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
803 display.updateLocked(mDisplayDevices);
804 if (!display.isValidLocked()) {
805 mLogicalDisplays.removeAt(i);
806 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
807 changed = true;
808 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
809 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
810 changed = true;
811 }
812 }
813 return changed;
814 }
815
816 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700817 // Clear all viewports before configuring displays so that we can keep
818 // track of which ones we have configured.
819 clearViewportsLocked();
820
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700821 // Configure each display device.
822 final int count = mDisplayDevices.size();
823 for (int i = 0; i < count; i++) {
824 DisplayDevice device = mDisplayDevices.get(i);
825 configureDisplayInTransactionLocked(device);
826 device.performTraversalInTransactionLocked();
827 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700828
829 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800830 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700831 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
832 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700833 }
834
Michael Wright3f145a22014-07-22 19:46:03 -0700835 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
836 float requestedRefreshRate, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700837 synchronized (mSyncRoot) {
838 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700839 if (display == null) {
840 return;
841 }
842 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700843 if (DEBUG) {
844 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
845 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
846 }
847
Craig Mautner722285e2012-09-07 13:55:58 -0700848 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700849 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700850 }
Michael Wright3f145a22014-07-22 19:46:03 -0700851 if (display.getRequestedRefreshRateLocked() != requestedRefreshRate) {
852 if (DEBUG) {
853 Slog.d(TAG, "Display " + displayId + " has requested a new refresh rate: "
854 + requestedRefreshRate + "fps");
855 }
856 display.setRequestedRefreshRateLocked(requestedRefreshRate);
857 scheduleTraversalLocked(inTraversal);
858 }
Craig Mautner722285e2012-09-07 13:55:58 -0700859 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700860 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700861
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800862 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
863 synchronized (mSyncRoot) {
864 LogicalDisplay display = mLogicalDisplays.get(displayId);
865 if (display == null) {
866 return;
867 }
868 if (display.getDisplayOffsetXLocked() != x
869 || display.getDisplayOffsetYLocked() != y) {
870 if (DEBUG) {
871 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
872 + x + ", " + y + ")");
873 }
874 display.setDisplayOffsetsLocked(x, y);
875 scheduleTraversalLocked(false);
876 }
877 }
878 }
879
Jeff Brownd728bf52012-09-08 18:05:28 -0700880 private void clearViewportsLocked() {
881 mDefaultViewport.valid = false;
882 mExternalTouchViewport.valid = false;
Craig Mautner722285e2012-09-07 13:55:58 -0700883 }
884
885 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -0800886 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
887 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -0700888
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700889 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -0800890 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -0700891 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -0800892 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700893 if (display != null && !display.hasContentLocked()) {
894 // If the display does not have any content of its own, then
895 // automatically mirror the default logical display contents.
896 display = null;
897 }
898 if (display == null) {
899 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
900 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700901 }
902
903 // Apply the logical display configuration to the display device.
904 if (display == null) {
905 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -0700906 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700907 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -0700908 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700909 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700910 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -0700911
912 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -0700913 if (!mDefaultViewport.valid
914 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
915 setViewportLocked(mDefaultViewport, display, device);
916 }
917 if (!mExternalTouchViewport.valid
918 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
919 setViewportLocked(mExternalTouchViewport, display, device);
920 }
921 }
922
923 private static void setViewportLocked(DisplayViewport viewport,
924 LogicalDisplay display, DisplayDevice device) {
925 viewport.valid = true;
926 viewport.displayId = display.getDisplayIdLocked();
927 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700928 }
929
930 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
931 final int count = mLogicalDisplays.size();
932 for (int i = 0; i < count; i++) {
933 LogicalDisplay display = mLogicalDisplays.valueAt(i);
934 if (display.getPrimaryDisplayDeviceLocked() == device) {
935 return display;
936 }
937 }
938 return null;
939 }
940
Jeff Brownbd6e1502012-08-28 03:27:37 -0700941 private void sendDisplayEventLocked(int displayId, int event) {
942 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
943 mHandler.sendMessage(msg);
944 }
945
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700946 // Requests that performTraversalsInTransactionFromWindowManager be called at a
947 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -0700948 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800949 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700950 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -0700951 if (!inTraversal) {
952 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
953 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700954 }
955 }
956
957 // Runs on Handler thread.
958 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700959 private void deliverDisplayEvent(int displayId, int event) {
960 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700961 Slog.d(TAG, "Delivering display event: displayId="
962 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700963 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700964
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700965 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700966 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700967 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700968 count = mCallbacks.size();
969 mTempCallbacks.clear();
970 for (int i = 0; i < count; i++) {
971 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -0700972 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700973 }
Craig Mautner4f67ba62012-08-02 11:23:00 -0700974
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700975 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700976 for (int i = 0; i < count; i++) {
977 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
978 }
979 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700980 }
981
Michael Wrightc39d47a2014-07-08 18:07:36 -0700982 private IMediaProjectionManager getProjectionService() {
983 if (mProjectionService == null) {
984 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
985 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
986 }
987 return mProjectionService;
988 }
989
Jeff Brown4ccb8232014-01-16 22:16:42 -0800990 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700991 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -0700992
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700993 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -0700994 pw.println(" mOnlyCode=" + mOnlyCore);
995 pw.println(" mSafeMode=" + mSafeMode);
996 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -0700997 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -0700998 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
999 pw.println(" mDefaultViewport=" + mDefaultViewport);
1000 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Jeff Brown27f1d672012-10-17 18:32:34 -07001001 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001002 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001003
Jeff Brownbd6e1502012-08-28 03:27:37 -07001004 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001005 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001006
1007 pw.println();
1008 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001009 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001010 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001011 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001012 }
Craig Mautner9de49362012-08-02 14:30:30 -07001013
Jeff Brownbd6e1502012-08-28 03:27:37 -07001014 pw.println();
1015 pw.println("Display Devices: size=" + mDisplayDevices.size());
1016 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001017 pw.println(" " + device.getDisplayDeviceInfoLocked());
1018 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001019 }
1020
1021 final int logicalDisplayCount = mLogicalDisplays.size();
1022 pw.println();
1023 pw.println("Logical Displays: size=" + logicalDisplayCount);
1024 for (int i = 0; i < logicalDisplayCount; i++) {
1025 int displayId = mLogicalDisplays.keyAt(i);
1026 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1027 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001028 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001029 }
Jeff Brownce468a32013-11-21 16:42:03 -08001030
1031 final int callbackCount = mCallbacks.size();
1032 pw.println();
1033 pw.println("Callbacks: size=" + callbackCount);
1034 for (int i = 0; i < callbackCount; i++) {
1035 CallbackRecord callback = mCallbacks.valueAt(i);
1036 pw.println(" " + i + ": mPid=" + callback.mPid
1037 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1038 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001039
1040 if (mDisplayPowerController != null) {
1041 mDisplayPowerController.dump(pw);
1042 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001043 }
1044 }
1045
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001046 /**
1047 * This is the object that everything in the display manager locks on.
1048 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1049 * clear that the object belongs to the display manager service and that it is
1050 * a unique object with a special purpose.
1051 */
1052 public static final class SyncRoot {
1053 }
1054
Jeff Brownbd6e1502012-08-28 03:27:37 -07001055 private final class DisplayManagerHandler extends Handler {
1056 public DisplayManagerHandler(Looper looper) {
1057 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001058 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001059
Jeff Brownbd6e1502012-08-28 03:27:37 -07001060 @Override
1061 public void handleMessage(Message msg) {
1062 switch (msg.what) {
1063 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1064 registerDefaultDisplayAdapter();
1065 break;
1066
1067 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1068 registerAdditionalDisplayAdapters();
1069 break;
1070
1071 case MSG_DELIVER_DISPLAY_EVENT:
1072 deliverDisplayEvent(msg.arg1, msg.arg2);
1073 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001074
1075 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001076 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001077 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001078
1079 case MSG_UPDATE_VIEWPORT: {
1080 synchronized (mSyncRoot) {
1081 mTempDefaultViewport.copyFrom(mDefaultViewport);
1082 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
1083 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001084 mInputManagerInternal.setDisplayViewports(
Jeff Brownd728bf52012-09-08 18:05:28 -07001085 mTempDefaultViewport, mTempExternalTouchViewport);
1086 break;
1087 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001088 }
1089 }
1090 }
1091
1092 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1093 @Override
1094 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1095 switch (event) {
1096 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1097 handleDisplayDeviceAdded(device);
1098 break;
1099
1100 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1101 handleDisplayDeviceChanged(device);
1102 break;
1103
1104 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1105 handleDisplayDeviceRemoved(device);
1106 break;
1107 }
1108 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001109
1110 @Override
1111 public void onTraversalRequested() {
1112 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001113 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001114 }
1115 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001116 }
1117
1118 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001119 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001120 private final IDisplayManagerCallback mCallback;
1121
Jeff Brownce468a32013-11-21 16:42:03 -08001122 public boolean mWifiDisplayScanRequested;
1123
Jeff Brownbd6e1502012-08-28 03:27:37 -07001124 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1125 mPid = pid;
1126 mCallback = callback;
1127 }
1128
1129 @Override
1130 public void binderDied() {
1131 if (DEBUG) {
1132 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1133 }
Jeff Brownce468a32013-11-21 16:42:03 -08001134 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001135 }
1136
1137 public void notifyDisplayEventAsync(int displayId, int event) {
1138 try {
1139 mCallback.onDisplayEvent(displayId, event);
1140 } catch (RemoteException ex) {
1141 Slog.w(TAG, "Failed to notify process "
1142 + mPid + " that displays changed, assuming it died.", ex);
1143 binderDied();
1144 }
1145 }
1146 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001147
1148 private final class BinderService extends IDisplayManager.Stub {
1149 /**
1150 * Returns information about the specified logical display.
1151 *
1152 * @param displayId The logical display id.
1153 * @return The logical display info, or null if the display does not exist. The
1154 * returned object must be treated as immutable.
1155 */
1156 @Override // Binder call
1157 public DisplayInfo getDisplayInfo(int displayId) {
1158 final int callingUid = Binder.getCallingUid();
1159 final long token = Binder.clearCallingIdentity();
1160 try {
1161 return getDisplayInfoInternal(displayId, callingUid);
1162 } finally {
1163 Binder.restoreCallingIdentity(token);
1164 }
1165 }
1166
1167 /**
1168 * Returns the list of all display ids.
1169 */
1170 @Override // Binder call
1171 public int[] getDisplayIds() {
1172 final int callingUid = Binder.getCallingUid();
1173 final long token = Binder.clearCallingIdentity();
1174 try {
1175 return getDisplayIdsInternal(callingUid);
1176 } finally {
1177 Binder.restoreCallingIdentity(token);
1178 }
1179 }
1180
1181 @Override // Binder call
1182 public void registerCallback(IDisplayManagerCallback callback) {
1183 if (callback == null) {
1184 throw new IllegalArgumentException("listener must not be null");
1185 }
1186
1187 final int callingPid = Binder.getCallingPid();
1188 final long token = Binder.clearCallingIdentity();
1189 try {
1190 registerCallbackInternal(callback, callingPid);
1191 } finally {
1192 Binder.restoreCallingIdentity(token);
1193 }
1194 }
1195
1196 @Override // Binder call
1197 public void startWifiDisplayScan() {
1198 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1199 "Permission required to start wifi display scans");
1200
1201 final int callingPid = Binder.getCallingPid();
1202 final long token = Binder.clearCallingIdentity();
1203 try {
1204 startWifiDisplayScanInternal(callingPid);
1205 } finally {
1206 Binder.restoreCallingIdentity(token);
1207 }
1208 }
1209
1210 @Override // Binder call
1211 public void stopWifiDisplayScan() {
1212 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1213 "Permission required to stop wifi display scans");
1214
1215 final int callingPid = Binder.getCallingPid();
1216 final long token = Binder.clearCallingIdentity();
1217 try {
1218 stopWifiDisplayScanInternal(callingPid);
1219 } finally {
1220 Binder.restoreCallingIdentity(token);
1221 }
1222 }
1223
1224 @Override // Binder call
1225 public void connectWifiDisplay(String address) {
1226 if (address == null) {
1227 throw new IllegalArgumentException("address must not be null");
1228 }
1229 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1230 "Permission required to connect to a wifi display");
1231
1232 final long token = Binder.clearCallingIdentity();
1233 try {
1234 connectWifiDisplayInternal(address);
1235 } finally {
1236 Binder.restoreCallingIdentity(token);
1237 }
1238 }
1239
1240 @Override // Binder call
1241 public void disconnectWifiDisplay() {
1242 // This request does not require special permissions.
1243 // Any app can request disconnection from the currently active wifi display.
1244 // This exception should no longer be needed once wifi display control moves
1245 // to the media router service.
1246
1247 final long token = Binder.clearCallingIdentity();
1248 try {
1249 disconnectWifiDisplayInternal();
1250 } finally {
1251 Binder.restoreCallingIdentity(token);
1252 }
1253 }
1254
1255 @Override // Binder call
1256 public void renameWifiDisplay(String address, String alias) {
1257 if (address == null) {
1258 throw new IllegalArgumentException("address must not be null");
1259 }
1260 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1261 "Permission required to rename to a wifi display");
1262
1263 final long token = Binder.clearCallingIdentity();
1264 try {
1265 renameWifiDisplayInternal(address, alias);
1266 } finally {
1267 Binder.restoreCallingIdentity(token);
1268 }
1269 }
1270
1271 @Override // Binder call
1272 public void forgetWifiDisplay(String address) {
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 forget to a wifi display");
1278
1279 final long token = Binder.clearCallingIdentity();
1280 try {
1281 forgetWifiDisplayInternal(address);
1282 } finally {
1283 Binder.restoreCallingIdentity(token);
1284 }
1285 }
1286
1287 @Override // Binder call
1288 public void pauseWifiDisplay() {
1289 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1290 "Permission required to pause a wifi display session");
1291
1292 final long token = Binder.clearCallingIdentity();
1293 try {
1294 pauseWifiDisplayInternal();
1295 } finally {
1296 Binder.restoreCallingIdentity(token);
1297 }
1298 }
1299
1300 @Override // Binder call
1301 public void resumeWifiDisplay() {
1302 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1303 "Permission required to resume a wifi display session");
1304
1305 final long token = Binder.clearCallingIdentity();
1306 try {
1307 resumeWifiDisplayInternal();
1308 } finally {
1309 Binder.restoreCallingIdentity(token);
1310 }
1311 }
1312
1313 @Override // Binder call
1314 public WifiDisplayStatus getWifiDisplayStatus() {
1315 // This request does not require special permissions.
1316 // Any app can get information about available wifi displays.
1317
1318 final long token = Binder.clearCallingIdentity();
1319 try {
1320 return getWifiDisplayStatusInternal();
1321 } finally {
1322 Binder.restoreCallingIdentity(token);
1323 }
1324 }
1325
1326 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001327 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001328 IMediaProjection projection, String packageName, String name,
1329 int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001330 final int callingUid = Binder.getCallingUid();
1331 if (!validatePackageName(callingUid, packageName)) {
1332 throw new SecurityException("packageName must match the calling uid");
1333 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001334 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001335 throw new IllegalArgumentException("appToken must not be null");
1336 }
1337 if (TextUtils.isEmpty(name)) {
1338 throw new IllegalArgumentException("name must be non-null and non-empty");
1339 }
1340 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1341 throw new IllegalArgumentException("width, height, and densityDpi must be "
1342 + "greater than 0");
1343 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001344
Michael Wright6720be42014-07-29 19:14:16 -07001345 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1346 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1347 }
1348 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1349 flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1350 }
1351
Michael Wrightc39d47a2014-07-08 18:07:36 -07001352 if (projection != null) {
1353 try {
1354 if (!getProjectionService().isValidMediaProjection(projection)) {
1355 throw new SecurityException("Invalid media projection");
1356 }
Michael Wright6720be42014-07-29 19:14:16 -07001357 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001358 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001359 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001360 }
1361 }
1362
Michael Wright6720be42014-07-29 19:14:16 -07001363 if (callingUid != Process.SYSTEM_UID &&
1364 (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001365 if (!canProjectVideo(projection)) {
1366 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1367 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1368 + "MediaProjection token in order to create a screen sharing virtual "
1369 + "display.");
1370 }
1371 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001372 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001373 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001374 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001375 + "or an appropriate MediaProjection token to create a "
1376 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001377 }
1378 }
1379
1380 final long token = Binder.clearCallingIdentity();
1381 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001382 return createVirtualDisplayInternal(callback, projection, callingUid,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001383 packageName, name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001384 } finally {
1385 Binder.restoreCallingIdentity(token);
1386 }
1387 }
1388
1389 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001390 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001391 int width, int height, int densityDpi) {
1392 final long token = Binder.clearCallingIdentity();
1393 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001394 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001395 } finally {
1396 Binder.restoreCallingIdentity(token);
1397 }
1398 }
1399
1400 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001401 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Jeff Brown92207df2014-04-16 13:16:07 -07001402 final long token = Binder.clearCallingIdentity();
1403 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001404 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001405 } finally {
1406 Binder.restoreCallingIdentity(token);
1407 }
1408 }
1409
1410 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001411 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001412 final long token = Binder.clearCallingIdentity();
1413 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001414 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001415 } finally {
1416 Binder.restoreCallingIdentity(token);
1417 }
1418 }
1419
1420 @Override // Binder call
1421 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1422 if (mContext == null
1423 || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
1424 != PackageManager.PERMISSION_GRANTED) {
1425 pw.println("Permission Denial: can't dump DisplayManager from from pid="
1426 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1427 return;
1428 }
1429
1430 final long token = Binder.clearCallingIdentity();
1431 try {
1432 dumpInternal(pw);
1433 } finally {
1434 Binder.restoreCallingIdentity(token);
1435 }
1436 }
1437
1438 private boolean validatePackageName(int uid, String packageName) {
1439 if (packageName != null) {
1440 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1441 if (packageNames != null) {
1442 for (String n : packageNames) {
1443 if (n.equals(packageName)) {
1444 return true;
1445 }
1446 }
1447 }
1448 }
1449 return false;
1450 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001451
1452 private boolean canProjectVideo(IMediaProjection projection) {
1453 if (projection != null) {
1454 try {
1455 if (projection.canProjectVideo()) {
1456 return true;
1457 }
1458 } catch (RemoteException e) {
1459 Slog.e(TAG, "Unable to query projection service for permissions", e);
1460 }
1461 }
1462 if (mContext.checkCallingPermission(
1463 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001464 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001465 return true;
1466 }
1467 return canProjectSecureVideo(projection);
1468 }
1469
1470 private boolean canProjectSecureVideo(IMediaProjection projection) {
1471 if (projection != null) {
1472 try {
1473 if (projection.canProjectSecureVideo()){
1474 return true;
1475 }
1476 } catch (RemoteException e) {
1477 Slog.e(TAG, "Unable to query projection service for permissions", e);
1478 }
1479 }
1480 return mContext.checkCallingPermission(
1481 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001482 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001483 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001484 }
1485
1486 private final class LocalService extends DisplayManagerInternal {
1487 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001488 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001489 SensorManager sensorManager) {
1490 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001491 DisplayBlanker blanker = new DisplayBlanker() {
1492 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001493 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001494 // The order of operations is important for legacy reasons.
1495 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001496 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001497 }
1498
1499 callbacks.onDisplayStateChange(state);
1500
1501 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001502 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001503 }
1504 }
1505 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001506 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001507 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001508 }
1509 }
1510
1511 @Override
1512 public boolean requestPowerState(DisplayPowerRequest request,
1513 boolean waitForNegativeProximity) {
1514 return mDisplayPowerController.requestPowerState(request,
1515 waitForNegativeProximity);
1516 }
1517
1518 @Override
1519 public boolean isProximitySensorAvailable() {
1520 return mDisplayPowerController.isProximitySensorAvailable();
1521 }
1522
1523 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001524 public DisplayInfo getDisplayInfo(int displayId) {
1525 return getDisplayInfoInternal(displayId, Process.myUid());
1526 }
1527
1528 @Override
1529 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1530 if (listener == null) {
1531 throw new IllegalArgumentException("listener must not be null");
1532 }
1533
1534 registerDisplayTransactionListenerInternal(listener);
1535 }
1536
1537 @Override
1538 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1539 if (listener == null) {
1540 throw new IllegalArgumentException("listener must not be null");
1541 }
1542
1543 unregisterDisplayTransactionListenerInternal(listener);
1544 }
1545
1546 @Override
1547 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1548 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1549 }
1550
1551 @Override
1552 public void performTraversalInTransactionFromWindowManager() {
1553 performTraversalInTransactionFromWindowManagerInternal();
1554 }
1555
1556 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001557 public void setDisplayProperties(int displayId, boolean hasContent,
1558 float requestedRefreshRate, boolean inTraversal) {
1559 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001560 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001561
1562 @Override
1563 public void setDisplayOffsets(int displayId, int x, int y) {
1564 setDisplayOffsetsInternal(displayId, x, y);
1565 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001566 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001567}