blob: ab3aff99937d7e5903e5beef1a737f1069f29557 [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080019import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
20import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
21import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
22import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
23import static android.hardware.display.DisplayManager
Andrii Kulian7211d2e2017-01-27 15:58:05 -080024 .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080025
Santos Cordonee8931e2017-04-05 10:31:15 -070026import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060027import com.android.internal.util.DumpUtils;
Jeff Brownbd6e1502012-08-28 03:27:37 -070028import com.android.internal.util.IndentingPrintWriter;
29
Jeff Brownfa25bf52012-07-23 19:26:30 -070030import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070031import android.annotation.NonNull;
Jeff Brownfa25bf52012-07-23 19:26:30 -070032import android.content.Context;
33import android.content.pm.PackageManager;
Jeff Brownad9ef192014-04-08 17:26:30 -070034import android.hardware.SensorManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070035import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080036import android.hardware.display.DisplayManagerInternal;
37import android.hardware.display.DisplayViewport;
38import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070039import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070040import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070041import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070042import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080043import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070044import android.media.projection.IMediaProjection;
45import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070046import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070047import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070048import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080049import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070050import android.os.Looper;
51import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070052import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080053import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070054import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070055import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070056import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070057import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070058import android.os.Trace;
Jeff Browna506a6e2013-06-04 00:02:38 -070059import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080060import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070061import android.util.Slog;
62import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070063import android.view.Display;
64import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070065import android.view.Surface;
Jeff Brown4ccb8232014-01-16 22:16:42 -080066import android.view.WindowManagerInternal;
Jeff Browna506a6e2013-06-04 00:02:38 -070067
Jorim Jaggied7993b2017-03-28 18:50:01 +010068import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080069import com.android.server.DisplayThread;
70import com.android.server.LocalServices;
71import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070072import com.android.server.UiThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070073
74import java.io.FileDescriptor;
75import java.io.PrintWriter;
76import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070077import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070078import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080079import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070080
81/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070082 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070083 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070084 * The {@link DisplayManagerService} manages the global lifecycle of displays,
85 * decides how to configure logical displays based on the physical display devices currently
86 * attached, sends notifications to the system and to applications when the state
87 * changes, and so on.
88 * </p><p>
89 * The display manager service relies on a collection of {@link DisplayAdapter} components,
90 * for discovering and configuring physical display devices attached to the system.
91 * There are separate display adapters for each manner that devices are attached:
92 * one display adapter for built-in local displays, one for simulated non-functional
93 * displays when the system is headless, one for simulated overlay displays used for
94 * development, one for wifi displays, etc.
95 * </p><p>
96 * Display adapters are only weakly coupled to the display manager service.
97 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -070098 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -070099 * by the display manager service. This separation of concerns is important for
100 * two main reasons. First, it neatly encapsulates the responsibilities of these
101 * two classes: display adapters handle individual display devices whereas
102 * the display manager service handles the global state. Second, it eliminates
103 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700104 * </p>
105 *
106 * <h3>Synchronization</h3>
107 * <p>
108 * Because the display manager may be accessed by multiple threads, the synchronization
109 * story gets a little complicated. In particular, the window manager may call into
110 * the display manager while holding a surface transaction with the expectation that
111 * it can apply changes immediately. Unfortunately, that means we can't just do
112 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700113 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700114 * To make this work, all of the objects that belong to the display manager must
115 * use the same lock. We call this lock the synchronization root and it has a unique
116 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
117 * named with the "Locked" suffix.
118 * </p><p>
119 * Where things get tricky is that the display manager is not allowed to make
120 * any potentially reentrant calls, especially into the window manager. We generally
121 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700122 * </p>
123 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800124public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700125 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700126 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700127
Jeff Brownbbd28a22012-09-20 16:47:15 -0700128 // When this system property is set to 0, WFD is forcibly disabled on boot.
129 // When this system property is set to 1, WFD is forcibly enabled on boot.
130 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
131 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
132
Jeff Brownbd6e1502012-08-28 03:27:37 -0700133 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
134
135 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER = 1;
136 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
137 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700138 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700139 private static final int MSG_UPDATE_VIEWPORT = 5;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700140
Jeff Brownb880d882014-02-10 19:47:07 -0800141 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700142 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 private final Handler mUiHandler;
144 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800145 private WindowManagerInternal mWindowManagerInternal;
146 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700147 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700148
149 // The synchronization root for the display manager.
150 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800151 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
152 // into WindowManagerService methods that require mWindowMap while holding this unless you are
153 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700154 private final SyncRoot mSyncRoot = new SyncRoot();
155
156 // True if in safe mode.
157 // This option may disable certain display adapters.
158 public boolean mSafeMode;
159
160 // True if we are in a special boot mode where only core applications and
161 // services should be started. This option may disable certain display adapters.
162 public boolean mOnlyCore;
163
Jeff Brown27f1d672012-10-17 18:32:34 -0700164 // True if the display manager service should pretend there is only one display
165 // and only tell applications about the existence of the default logical display.
166 // The display manager can still mirror content to secondary displays but applications
167 // cannot present unique content on those displays.
168 // Used for demonstration purposes only.
169 private final boolean mSingleDisplayDemoMode;
170
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700171 // All callback records indexed by calling process id.
172 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700173 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700174
Jeff Brownbd6e1502012-08-28 03:27:37 -0700175 // List of all currently registered display adapters.
176 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
177
178 // List of all currently connected display devices.
179 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
180
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700181 // List of all logical displays indexed by logical display id.
182 private final SparseArray<LogicalDisplay> mLogicalDisplays =
183 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700184 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
185
Jeff Brown7f3994e2012-12-04 14:04:28 -0800186 // List of all display transaction listeners.
187 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
188 new CopyOnWriteArrayList<DisplayTransactionListener>();
189
Jeff Brownad9ef192014-04-08 17:26:30 -0700190 // Display power controller.
191 private DisplayPowerController mDisplayPowerController;
192
Jeff Brown037c33e2014-04-09 00:31:55 -0700193 // The overall display state, independent of changes that might influence one
194 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700195 private int mGlobalDisplayState = Display.STATE_ON;
196
197 // The overall display brightness.
198 // For now, this only applies to the built-in display but we may split it up eventually.
199 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700200
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700201 // Set to true when there are pending display changes that have yet to be applied
202 // to the surface flinger state.
203 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700204
Jeff Browne08ae382012-09-07 20:36:36 -0700205 // The Wifi display adapter, or null if not registered.
206 private WifiDisplayAdapter mWifiDisplayAdapter;
207
Jeff Brownce468a32013-11-21 16:42:03 -0800208 // The number of active wifi display scan requests.
209 private int mWifiDisplayScanRequestCount;
210
Jeff Browna506a6e2013-06-04 00:02:38 -0700211 // The virtual display adapter, or null if not registered.
212 private VirtualDisplayAdapter mVirtualDisplayAdapter;
213
Jeff Brownd728bf52012-09-08 18:05:28 -0700214 // Viewports of the default display and the display that should receive touch
215 // input from an external source. Used by the input system.
216 private final DisplayViewport mDefaultViewport = new DisplayViewport();
217 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700218 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700219
Jeff Brown89d55462012-09-19 11:33:42 -0700220 // Persistent data store for all internal settings maintained by the display manager service.
221 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
222
Jeff Brownbd6e1502012-08-28 03:27:37 -0700223 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700224 // May be used outside of the lock but only on the handler thread.
225 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700226
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700227 // Temporary display info, used for comparing display configurations.
228 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
229
Jeff Brownd728bf52012-09-08 18:05:28 -0700230 // Temporary viewports, used when sending new viewport information to the
231 // input system. May be used outside of the lock but only on the handler thread.
232 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
233 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700234 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700235
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700236 // The default color mode for default displays. Overrides the usual
237 // Display.Display.COLOR_MODE_DEFAULT for displays with the
238 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
239 private final int mDefaultDisplayDefaultColorMode;
240
Jeff Browne75926d2014-09-18 15:24:49 -0700241 // Temporary list of deferred work to perform when setting the display state.
242 // Only used by requestDisplayState. The field is self-synchronized and only
243 // intended for use inside of the requestGlobalDisplayStateInternal function.
244 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
245
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800246 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
247 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
248
Santos Cordonee8931e2017-04-05 10:31:15 -0700249 private final Injector mInjector;
250
Jeff Brownb880d882014-02-10 19:47:07 -0800251 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700252 this(context, new Injector());
253 }
254
255 @VisibleForTesting
256 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800257 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700258 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800259 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800260 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700261 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700262 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700263 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700264 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
265 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700266
267 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
268 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Joel Fernandes2d314e12017-04-04 16:32:15 -0700269
270 }
271
272 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100273 // android.display and android.anim is critical to user experience and we should make sure
274 // it is not in the default foregroup groups, add it to top-app to make sure it uses all the
275 // cores and scheduling settings for top-app when it runs.
276 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
277 Process.THREAD_GROUP_TOP_APP);
278 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
279 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700280 }
281
Jeff Brown4ccb8232014-01-16 22:16:42 -0800282 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800283 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700284 // We need to pre-load the persistent data store so it's ready before the default display
285 // adapter is up so that we have it's configuration. We could load it lazily, but since
286 // we're going to have to read it in eventually we may as well do it here rather than after
287 // we've waited for the diplay to register itself with us.
288 mPersistentDataStore.loadIfNeeded();
Jeff Brown4ccb8232014-01-16 22:16:42 -0800289 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
290
291 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
292 true /*allowIsolated*/);
293 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700294 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800295 }
296
297 @Override
298 public void onBootPhase(int phase) {
299 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
300 synchronized (mSyncRoot) {
301 long timeout = SystemClock.uptimeMillis() + WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
302 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null) {
303 long delay = timeout - SystemClock.uptimeMillis();
304 if (delay <= 0) {
305 throw new RuntimeException("Timeout waiting for default display "
306 + "to be initialized.");
307 }
308 if (DEBUG) {
309 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
310 }
311 try {
312 mSyncRoot.wait(delay);
313 } catch (InterruptedException ex) {
314 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700315 }
316 }
317 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700318 }
319
Jeff Brown4ccb8232014-01-16 22:16:42 -0800320 // TODO: Use dependencies or a boot phase
321 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700322 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800323 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
324 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700325 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700326 }
327 }
328
329 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700330 * Called when the system is ready to go.
331 */
332 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700333 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700334 mSafeMode = safeMode;
335 mOnlyCore = onlyCore;
336 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700337
Jeff Brownbd6e1502012-08-28 03:27:37 -0700338 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
339 }
340
Santos Cordonee8931e2017-04-05 10:31:15 -0700341 @VisibleForTesting
342 Handler getDisplayHandler() {
343 return mHandler;
344 }
345
Jeff Brown4ccb8232014-01-16 22:16:42 -0800346 private void registerDisplayTransactionListenerInternal(
347 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800348 // List is self-synchronized copy-on-write.
349 mDisplayTransactionListeners.add(listener);
350 }
351
Jeff Brown4ccb8232014-01-16 22:16:42 -0800352 private void unregisterDisplayTransactionListenerInternal(
353 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800354 // List is self-synchronized copy-on-write.
355 mDisplayTransactionListeners.remove(listener);
356 }
357
Jeff Brown4ccb8232014-01-16 22:16:42 -0800358 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700359 int displayId, DisplayInfo info) {
360 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700361 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700362 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700363 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700364 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700365 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700366 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700367 }
368 }
369 }
370
Andrii Kuliancd097992017-03-23 18:31:59 -0700371 /**
372 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
373 */
374 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
375 synchronized (mSyncRoot) {
376 final LogicalDisplay display = mLogicalDisplays.get(displayId);
377 if (display != null) {
378 display.getNonOverrideDisplayInfoLocked(outInfo);
379 }
380 }
381 }
382
Santos Cordonee8931e2017-04-05 10:31:15 -0700383 @VisibleForTesting
384 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700385 synchronized (mSyncRoot) {
386 if (!mPendingTraversal) {
387 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700388 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700389 mPendingTraversal = false;
390
391 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700392 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800393
394 // List is self-synchronized copy-on-write.
395 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
396 listener.onDisplayTransaction();
397 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700398 }
399
Jeff Brown5d6443b2015-04-10 20:15:01 -0700400 private void requestGlobalDisplayStateInternal(int state, int brightness) {
401 if (state == Display.STATE_UNKNOWN) {
402 state = Display.STATE_ON;
403 }
404 if (state == Display.STATE_OFF) {
405 brightness = PowerManager.BRIGHTNESS_OFF;
406 } else if (brightness < 0) {
407 brightness = PowerManager.BRIGHTNESS_DEFAULT;
408 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
409 brightness = PowerManager.BRIGHTNESS_ON;
410 }
411
Jeff Browne75926d2014-09-18 15:24:49 -0700412 synchronized (mTempDisplayStateWorkQueue) {
413 try {
414 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700415 // Note that we do not need to schedule traversals here although it
416 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700417 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700418 if (mGlobalDisplayState == state
419 && mGlobalDisplayBrightness == brightness) {
420 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700421 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700422
423 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
424 + Display.stateToString(state)
425 + ", brightness=" + brightness + ")");
426 mGlobalDisplayState = state;
427 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700428 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700429 }
430
431 // Setting the display power state can take hundreds of milliseconds
432 // to complete so we defer the most expensive part of the work until
433 // after we have exited the critical section to avoid blocking other
434 // threads for a long time.
435 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
436 mTempDisplayStateWorkQueue.get(i).run();
437 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700438 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700439 } finally {
440 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700441 }
442 }
443 }
444
Jeff Brown4ccb8232014-01-16 22:16:42 -0800445 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700446 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800447 LogicalDisplay display = mLogicalDisplays.get(displayId);
448 if (display != null) {
449 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800450 if (info.hasAccess(callingUid)
451 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800452 return info;
453 }
454 }
455 return null;
456 }
457 }
458
459 private int[] getDisplayIdsInternal(int callingUid) {
460 synchronized (mSyncRoot) {
461 final int count = mLogicalDisplays.size();
462 int[] displayIds = new int[count];
463 int n = 0;
464 for (int i = 0; i < count; i++) {
465 LogicalDisplay display = mLogicalDisplays.valueAt(i);
466 DisplayInfo info = display.getDisplayInfoLocked();
467 if (info.hasAccess(callingUid)) {
468 displayIds[n++] = mLogicalDisplays.keyAt(i);
469 }
470 }
471 if (n != count) {
472 displayIds = Arrays.copyOfRange(displayIds, 0, n);
473 }
474 return displayIds;
475 }
476 }
477
478 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
479 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700480 if (mCallbacks.get(callingPid) != null) {
481 throw new SecurityException("The calling process has already "
482 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700483 }
484
Jeff Brownbd6e1502012-08-28 03:27:37 -0700485 CallbackRecord record = new CallbackRecord(callingPid, callback);
486 try {
487 IBinder binder = callback.asBinder();
488 binder.linkToDeath(record, 0);
489 } catch (RemoteException ex) {
490 // give up
491 throw new RuntimeException(ex);
492 }
493
494 mCallbacks.put(callingPid, record);
495 }
496 }
497
Jeff Brownce468a32013-11-21 16:42:03 -0800498 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700499 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800500 mCallbacks.remove(record.mPid);
501 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700502 }
503 }
504
Jeff Brown4ccb8232014-01-16 22:16:42 -0800505 private void startWifiDisplayScanInternal(int callingPid) {
506 synchronized (mSyncRoot) {
507 CallbackRecord record = mCallbacks.get(callingPid);
508 if (record == null) {
509 throw new IllegalStateException("The calling process has not "
510 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700511 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800512 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700513 }
514 }
515
Jeff Brownce468a32013-11-21 16:42:03 -0800516 private void startWifiDisplayScanLocked(CallbackRecord record) {
517 if (!record.mWifiDisplayScanRequested) {
518 record.mWifiDisplayScanRequested = true;
519 if (mWifiDisplayScanRequestCount++ == 0) {
520 if (mWifiDisplayAdapter != null) {
521 mWifiDisplayAdapter.requestStartScanLocked();
522 }
523 }
524 }
525 }
526
Jeff Brown4ccb8232014-01-16 22:16:42 -0800527 private void stopWifiDisplayScanInternal(int callingPid) {
528 synchronized (mSyncRoot) {
529 CallbackRecord record = mCallbacks.get(callingPid);
530 if (record == null) {
531 throw new IllegalStateException("The calling process has not "
532 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800533 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800534 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800535 }
536 }
537
538 private void stopWifiDisplayScanLocked(CallbackRecord record) {
539 if (record.mWifiDisplayScanRequested) {
540 record.mWifiDisplayScanRequested = false;
541 if (--mWifiDisplayScanRequestCount == 0) {
542 if (mWifiDisplayAdapter != null) {
543 mWifiDisplayAdapter.requestStopScanLocked();
544 }
545 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700546 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800547 + mWifiDisplayScanRequestCount);
548 mWifiDisplayScanRequestCount = 0;
549 }
550 }
551 }
552
Jeff Brown4ccb8232014-01-16 22:16:42 -0800553 private void connectWifiDisplayInternal(String address) {
554 synchronized (mSyncRoot) {
555 if (mWifiDisplayAdapter != null) {
556 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700557 }
Jeff Browne08ae382012-09-07 20:36:36 -0700558 }
559 }
560
Jeff Brown4ccb8232014-01-16 22:16:42 -0800561 private void pauseWifiDisplayInternal() {
562 synchronized (mSyncRoot) {
563 if (mWifiDisplayAdapter != null) {
564 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700565 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700566 }
567 }
568
Jeff Brown4ccb8232014-01-16 22:16:42 -0800569 private void resumeWifiDisplayInternal() {
570 synchronized (mSyncRoot) {
571 if (mWifiDisplayAdapter != null) {
572 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700573 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700574 }
575 }
576
Jeff Brown4ccb8232014-01-16 22:16:42 -0800577 private void disconnectWifiDisplayInternal() {
578 synchronized (mSyncRoot) {
579 if (mWifiDisplayAdapter != null) {
580 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700581 }
Jeff Browne08ae382012-09-07 20:36:36 -0700582 }
583 }
584
Jeff Brown4ccb8232014-01-16 22:16:42 -0800585 private void renameWifiDisplayInternal(String address, String alias) {
586 synchronized (mSyncRoot) {
587 if (mWifiDisplayAdapter != null) {
588 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700589 }
Jeff Brown89d55462012-09-19 11:33:42 -0700590 }
591 }
592
Jeff Brown4ccb8232014-01-16 22:16:42 -0800593 private void forgetWifiDisplayInternal(String address) {
594 synchronized (mSyncRoot) {
595 if (mWifiDisplayAdapter != null) {
596 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700597 }
Jeff Brown89d55462012-09-19 11:33:42 -0700598 }
599 }
600
Jeff Brown4ccb8232014-01-16 22:16:42 -0800601 private WifiDisplayStatus getWifiDisplayStatusInternal() {
602 synchronized (mSyncRoot) {
603 if (mWifiDisplayAdapter != null) {
604 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700605 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800606 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700607 }
608 }
609
Michael Wright1c9977b2016-07-12 13:30:10 -0700610 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100611 synchronized (mSyncRoot) {
612 LogicalDisplay display = mLogicalDisplays.get(displayId);
613 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700614 display.getRequestedColorModeLocked() != colorMode) {
615 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100616 scheduleTraversalLocked(false);
617 }
618 }
619 }
620
Michael Wright75ee9fc2014-09-01 19:55:22 -0700621 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700622 IMediaProjection projection, int callingUid, String packageName, String name, int width,
623 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800624 synchronized (mSyncRoot) {
625 if (mVirtualDisplayAdapter == null) {
626 Slog.w(TAG, "Rejecting request to create private virtual display "
627 + "because the virtual display adapter is not available.");
628 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700629 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800630
631 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700632 callback, projection, callingUid, packageName, name, width, height, densityDpi,
633 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800634 if (device == null) {
635 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700636 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700637
Jeff Brown4ccb8232014-01-16 22:16:42 -0800638 handleDisplayDeviceAddedLocked(device);
639 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
640 if (display != null) {
641 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700642 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800643
644 // Something weird happened and the logical display was not created.
645 Slog.w(TAG, "Rejecting request to create virtual display "
646 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700647 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800648 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700649 }
650 return -1;
651 }
652
Michael Wright01e840f2014-06-26 16:03:25 -0700653 private void resizeVirtualDisplayInternal(IBinder appToken,
654 int width, int height, int densityDpi) {
655 synchronized (mSyncRoot) {
656 if (mVirtualDisplayAdapter == null) {
657 return;
658 }
659
660 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
661 }
662 }
663
Jeff Brown92207df2014-04-16 13:16:07 -0700664 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
665 synchronized (mSyncRoot) {
666 if (mVirtualDisplayAdapter == null) {
667 return;
668 }
669
670 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
671 }
672 }
673
Jeff Brown4ccb8232014-01-16 22:16:42 -0800674 private void releaseVirtualDisplayInternal(IBinder appToken) {
675 synchronized (mSyncRoot) {
676 if (mVirtualDisplayAdapter == null) {
677 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700678 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700679
Jeff Brown4ccb8232014-01-16 22:16:42 -0800680 DisplayDevice device =
681 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
682 if (device != null) {
683 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700684 }
685 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700686 }
687
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700688 private void registerDefaultDisplayAdapter() {
689 // Register default display adapter.
690 synchronized (mSyncRoot) {
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800691 registerDisplayAdapterLocked(new LocalDisplayAdapter(
692 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700693 }
694 }
695
696 private void registerAdditionalDisplayAdapters() {
697 synchronized (mSyncRoot) {
698 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700699 registerOverlayDisplayAdapterLocked();
700 registerWifiDisplayAdapterLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700701 registerVirtualDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700702 }
703 }
704 }
705
Jeff Brown89d55462012-09-19 11:33:42 -0700706 private void registerOverlayDisplayAdapterLocked() {
707 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
708 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
709 }
710
711 private void registerWifiDisplayAdapterLocked() {
712 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700713 com.android.internal.R.bool.config_enableWifiDisplay)
714 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700715 mWifiDisplayAdapter = new WifiDisplayAdapter(
716 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
717 mPersistentDataStore);
718 registerDisplayAdapterLocked(mWifiDisplayAdapter);
719 }
720 }
721
Jeff Browna506a6e2013-06-04 00:02:38 -0700722 private void registerVirtualDisplayAdapterLocked() {
Santos Cordonee8931e2017-04-05 10:31:15 -0700723 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext, mHandler,
724 mDisplayAdapterListener);
Jeff Browna506a6e2013-06-04 00:02:38 -0700725 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
726 }
727
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700728 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
729 // In safe mode, we disable non-essential display adapters to give the user
730 // an opportunity to fix broken settings or other problems that might affect
731 // system stability.
732 // In only-core mode, we disable non-essential display adapters to minimize
733 // the number of dependencies that are started while in this mode and to
734 // prevent problems that might occur due to the device being encrypted.
735 return !mSafeMode && !mOnlyCore;
736 }
737
738 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
739 mDisplayAdapters.add(adapter);
740 adapter.registerLocked();
741 }
742
Jeff Brownbd6e1502012-08-28 03:27:37 -0700743 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700744 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700745 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700746 }
747 }
748
Jeff Browna506a6e2013-06-04 00:02:38 -0700749 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700750 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700751 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700752 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700753 return;
754 }
755
Jeff Brown10acf6d2015-04-14 14:20:47 -0700756 Slog.i(TAG, "Display device added: " + info);
757 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700758
759 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700760 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700761 Runnable work = updateDisplayStateLocked(device);
762 if (work != null) {
763 work.run();
764 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700765 if (display != null && display.getPrimaryDisplayDeviceLocked() == device) {
766 int colorMode = mPersistentDataStore.getColorMode(device);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700767 if (colorMode == Display.COLOR_MODE_INVALID) {
768 if ((device.getDisplayDeviceInfoLocked().flags
769 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
770 colorMode = mDefaultDisplayDefaultColorMode;
771 } else {
772 colorMode = Display.COLOR_MODE_DEFAULT;
773 }
774 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700775 display.setRequestedColorModeLocked(colorMode);
776 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700777 scheduleTraversalLocked(false);
778 }
779
Jeff Brownbd6e1502012-08-28 03:27:37 -0700780 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700781 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700782 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700783 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700784 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700785 return;
786 }
787
Jeff Brown10acf6d2015-04-14 14:20:47 -0700788 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
789 if (diff == DisplayDeviceInfo.DIFF_STATE) {
790 Slog.i(TAG, "Display device changed state: \"" + info.name
791 + "\", " + Display.stateToString(info.state));
792 } else if (diff != 0) {
793 Slog.i(TAG, "Display device changed: " + info);
794 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700795 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
796 try {
797 mPersistentDataStore.setColorMode(device, info.colorMode);
798 } finally {
799 mPersistentDataStore.saveIfNeeded();
800 }
801 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700802 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700803
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700804 device.applyPendingDisplayDeviceInfoChangesLocked();
805 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700806 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700807 }
808 }
809 }
810
Jeff Brownbd6e1502012-08-28 03:27:37 -0700811 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700812 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700813 handleDisplayDeviceRemovedLocked(device);
814 }
815 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700816
Jeff Browna506a6e2013-06-04 00:02:38 -0700817 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700818 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700819 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700820 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700821 return;
822 }
823
Jeff Brown10acf6d2015-04-14 14:20:47 -0700824 Slog.i(TAG, "Display device removed: " + info);
825 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700826
Jeff Browna506a6e2013-06-04 00:02:38 -0700827 updateLogicalDisplaysLocked();
828 scheduleTraversalLocked(false);
829 }
830
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700831 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700832 final int count = mDisplayDevices.size();
833 for (int i = 0; i < count; i++) {
834 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700835 Runnable runnable = updateDisplayStateLocked(device);
836 if (runnable != null) {
837 workQueue.add(runnable);
838 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700839 }
840 }
841
Jeff Browne75926d2014-09-18 15:24:49 -0700842 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700843 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700844 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700845 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
846 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700847 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700848 }
Jeff Browne75926d2014-09-18 15:24:49 -0700849 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700850 }
851
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700852 // Adds a new logical display based on the given display device.
853 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700854 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700855 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
856 boolean isDefault = (deviceInfo.flags
857 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
858 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
859 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
860 isDefault = false;
861 }
862
Jeff Brown27f1d672012-10-17 18:32:34 -0700863 if (!isDefault && mSingleDisplayDemoMode) {
864 Slog.i(TAG, "Not creating a logical display for a secondary display "
865 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700866 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700867 }
868
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700869 final int displayId = assignDisplayIdLocked(isDefault);
870 final int layerStack = assignLayerStackLocked(displayId);
871
Jeff Brownd728bf52012-09-08 18:05:28 -0700872 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700873 display.updateLocked(mDisplayDevices);
874 if (!display.isValidLocked()) {
875 // This should never happen currently.
876 Slog.w(TAG, "Ignoring display device because the logical display "
877 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700878 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700879 }
880
881 mLogicalDisplays.put(displayId, display);
882
883 // Wake up waitForDefaultDisplay.
884 if (isDefault) {
885 mSyncRoot.notifyAll();
886 }
887
888 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700889 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700890 }
891
892 private int assignDisplayIdLocked(boolean isDefault) {
893 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
894 }
895
896 private int assignLayerStackLocked(int displayId) {
897 // Currently layer stacks and display ids are the same.
898 // This need not be the case.
899 return displayId;
900 }
901
902 // Updates all existing logical displays given the current set of display devices.
903 // Removes invalid logical displays.
904 // Sends notifications if needed.
905 private boolean updateLogicalDisplaysLocked() {
906 boolean changed = false;
907 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
908 final int displayId = mLogicalDisplays.keyAt(i);
909 LogicalDisplay display = mLogicalDisplays.valueAt(i);
910
911 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
912 display.updateLocked(mDisplayDevices);
913 if (!display.isValidLocked()) {
914 mLogicalDisplays.removeAt(i);
915 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
916 changed = true;
917 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
918 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
919 changed = true;
920 }
921 }
922 return changed;
923 }
924
925 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700926 // Clear all viewports before configuring displays so that we can keep
927 // track of which ones we have configured.
928 clearViewportsLocked();
929
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700930 // Configure each display device.
931 final int count = mDisplayDevices.size();
932 for (int i = 0; i < count; i++) {
933 DisplayDevice device = mDisplayDevices.get(i);
934 configureDisplayInTransactionLocked(device);
935 device.performTraversalInTransactionLocked();
936 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700937
938 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800939 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700940 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
941 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700942 }
943
Michael Wright3f145a22014-07-22 19:46:03 -0700944 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700945 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700946 synchronized (mSyncRoot) {
947 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700948 if (display == null) {
949 return;
950 }
951 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700952 if (DEBUG) {
953 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
954 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
955 }
956
Craig Mautner722285e2012-09-07 13:55:58 -0700957 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700958 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700959 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700960 if (requestedModeId == 0 && requestedRefreshRate != 0) {
961 // Scan supported modes returned by display.getInfo() to find a mode with the same
962 // size as the default display mode but with the specified refresh rate instead.
963 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
964 requestedRefreshRate);
965 }
966 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700967 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700968 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700969 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700970 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700971 scheduleTraversalLocked(inTraversal);
972 }
Craig Mautner722285e2012-09-07 13:55:58 -0700973 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700974 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700975
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800976 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
977 synchronized (mSyncRoot) {
978 LogicalDisplay display = mLogicalDisplays.get(displayId);
979 if (display == null) {
980 return;
981 }
982 if (display.getDisplayOffsetXLocked() != x
983 || display.getDisplayOffsetYLocked() != y) {
984 if (DEBUG) {
985 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
986 + x + ", " + y + ")");
987 }
988 display.setDisplayOffsetsLocked(x, y);
989 scheduleTraversalLocked(false);
990 }
991 }
992 }
993
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800994 // Updates the lists of UIDs that are present on displays.
995 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
996 synchronized (mSyncRoot) {
997 mDisplayAccessUIDs.clear();
998 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
999 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1000 newDisplayAccessUIDs.valueAt(i));
1001 }
1002 }
1003 }
1004
1005 // Checks if provided UID's content is present on the display and UID has access to it.
1006 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1007 synchronized (mSyncRoot) {
1008 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1009 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1010 }
1011 }
1012
Jeff Brownd728bf52012-09-08 18:05:28 -07001013 private void clearViewportsLocked() {
1014 mDefaultViewport.valid = false;
1015 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001016 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001017 }
1018
1019 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001020 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1021 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001022
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001023 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001024 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001025 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001026 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001027 if (display != null && !display.hasContentLocked()) {
1028 // If the display does not have any content of its own, then
1029 // automatically mirror the default logical display contents.
1030 display = null;
1031 }
1032 if (display == null) {
1033 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1034 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001035 }
1036
1037 // Apply the logical display configuration to the display device.
1038 if (display == null) {
1039 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001040 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001041 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001042 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001043 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001044 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001045
1046 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001047 if (!mDefaultViewport.valid
1048 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1049 setViewportLocked(mDefaultViewport, display, device);
1050 }
1051 if (!mExternalTouchViewport.valid
1052 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1053 setViewportLocked(mExternalTouchViewport, display, device);
1054 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001055
1056 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1057 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1058 setViewportLocked(viewport, display, device);
1059 }
1060 }
1061
1062 /** Gets the virtual device viewport or creates it if not yet created. */
1063 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1064 DisplayViewport viewport;
1065 final int count = mVirtualTouchViewports.size();
1066 for (int i = 0; i < count; i++) {
1067 viewport = mVirtualTouchViewports.get(i);
1068 if (uniqueId.equals(viewport.uniqueId)) {
1069 return viewport;
1070 }
1071 }
1072
1073 viewport = new DisplayViewport();
1074 viewport.uniqueId = uniqueId;
1075 mVirtualTouchViewports.add(viewport);
1076 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001077 }
1078
1079 private static void setViewportLocked(DisplayViewport viewport,
1080 LogicalDisplay display, DisplayDevice device) {
1081 viewport.valid = true;
1082 viewport.displayId = display.getDisplayIdLocked();
1083 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001084 }
1085
1086 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1087 final int count = mLogicalDisplays.size();
1088 for (int i = 0; i < count; i++) {
1089 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1090 if (display.getPrimaryDisplayDeviceLocked() == device) {
1091 return display;
1092 }
1093 }
1094 return null;
1095 }
1096
Jeff Brownbd6e1502012-08-28 03:27:37 -07001097 private void sendDisplayEventLocked(int displayId, int event) {
1098 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1099 mHandler.sendMessage(msg);
1100 }
1101
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001102 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1103 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001104 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001105 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001106 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001107 if (!inTraversal) {
1108 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1109 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001110 }
1111 }
1112
1113 // Runs on Handler thread.
1114 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001115 private void deliverDisplayEvent(int displayId, int event) {
1116 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001117 Slog.d(TAG, "Delivering display event: displayId="
1118 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001119 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001120
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001121 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001122 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001123 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001124 count = mCallbacks.size();
1125 mTempCallbacks.clear();
1126 for (int i = 0; i < count; i++) {
1127 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001128 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001129 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001130
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001131 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001132 for (int i = 0; i < count; i++) {
1133 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1134 }
1135 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001136 }
1137
Michael Wrightc39d47a2014-07-08 18:07:36 -07001138 private IMediaProjectionManager getProjectionService() {
1139 if (mProjectionService == null) {
1140 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1141 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1142 }
1143 return mProjectionService;
1144 }
1145
Jeff Brown4ccb8232014-01-16 22:16:42 -08001146 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001147 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001148
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001149 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001150 pw.println(" mOnlyCode=" + mOnlyCore);
1151 pw.println(" mSafeMode=" + mSafeMode);
1152 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001153 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001154 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1155 pw.println(" mDefaultViewport=" + mDefaultViewport);
1156 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001157 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001158 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001159 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001160 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001161
Jeff Brownbd6e1502012-08-28 03:27:37 -07001162 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001163 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001164
1165 pw.println();
1166 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001167 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001168 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001169 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001170 }
Craig Mautner9de49362012-08-02 14:30:30 -07001171
Jeff Brownbd6e1502012-08-28 03:27:37 -07001172 pw.println();
1173 pw.println("Display Devices: size=" + mDisplayDevices.size());
1174 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001175 pw.println(" " + device.getDisplayDeviceInfoLocked());
1176 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001177 }
1178
1179 final int logicalDisplayCount = mLogicalDisplays.size();
1180 pw.println();
1181 pw.println("Logical Displays: size=" + logicalDisplayCount);
1182 for (int i = 0; i < logicalDisplayCount; i++) {
1183 int displayId = mLogicalDisplays.keyAt(i);
1184 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1185 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001186 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001187 }
Jeff Brownce468a32013-11-21 16:42:03 -08001188
1189 final int callbackCount = mCallbacks.size();
1190 pw.println();
1191 pw.println("Callbacks: size=" + callbackCount);
1192 for (int i = 0; i < callbackCount; i++) {
1193 CallbackRecord callback = mCallbacks.valueAt(i);
1194 pw.println(" " + i + ": mPid=" + callback.mPid
1195 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1196 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001197
1198 if (mDisplayPowerController != null) {
1199 mDisplayPowerController.dump(pw);
1200 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001201
1202 pw.println();
1203 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001204 }
1205 }
1206
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001207 /**
1208 * This is the object that everything in the display manager locks on.
1209 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1210 * clear that the object belongs to the display manager service and that it is
1211 * a unique object with a special purpose.
1212 */
1213 public static final class SyncRoot {
1214 }
1215
Santos Cordonee8931e2017-04-05 10:31:15 -07001216 @VisibleForTesting
1217 static class Injector {
1218 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1219 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1220 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1221 }
1222 }
1223
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001224 @VisibleForTesting
1225 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1226 synchronized (mSyncRoot) {
1227 LogicalDisplay display = mLogicalDisplays.get(displayId);
1228 if (display != null) {
1229 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1230 return displayDevice.getDisplayDeviceInfoLocked();
1231 }
1232 return null;
1233 }
1234 }
1235
Jeff Brownbd6e1502012-08-28 03:27:37 -07001236 private final class DisplayManagerHandler extends Handler {
1237 public DisplayManagerHandler(Looper looper) {
1238 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001239 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001240
Jeff Brownbd6e1502012-08-28 03:27:37 -07001241 @Override
1242 public void handleMessage(Message msg) {
1243 switch (msg.what) {
1244 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1245 registerDefaultDisplayAdapter();
1246 break;
1247
1248 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1249 registerAdditionalDisplayAdapters();
1250 break;
1251
1252 case MSG_DELIVER_DISPLAY_EVENT:
1253 deliverDisplayEvent(msg.arg1, msg.arg2);
1254 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001255
1256 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001257 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001258 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001259
1260 case MSG_UPDATE_VIEWPORT: {
1261 synchronized (mSyncRoot) {
1262 mTempDefaultViewport.copyFrom(mDefaultViewport);
1263 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001264 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1265 mTempVirtualTouchViewports.clear();
1266 for (DisplayViewport d : mVirtualTouchViewports) {
1267 mTempVirtualTouchViewports.add(d.makeCopy());
1268 }
1269 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001270 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001271 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1272 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001273 break;
1274 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001275 }
1276 }
1277 }
1278
1279 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1280 @Override
1281 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1282 switch (event) {
1283 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1284 handleDisplayDeviceAdded(device);
1285 break;
1286
1287 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1288 handleDisplayDeviceChanged(device);
1289 break;
1290
1291 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1292 handleDisplayDeviceRemoved(device);
1293 break;
1294 }
1295 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001296
1297 @Override
1298 public void onTraversalRequested() {
1299 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001300 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001301 }
1302 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001303 }
1304
1305 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001306 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001307 private final IDisplayManagerCallback mCallback;
1308
Jeff Brownce468a32013-11-21 16:42:03 -08001309 public boolean mWifiDisplayScanRequested;
1310
Jeff Brownbd6e1502012-08-28 03:27:37 -07001311 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1312 mPid = pid;
1313 mCallback = callback;
1314 }
1315
1316 @Override
1317 public void binderDied() {
1318 if (DEBUG) {
1319 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1320 }
Jeff Brownce468a32013-11-21 16:42:03 -08001321 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001322 }
1323
1324 public void notifyDisplayEventAsync(int displayId, int event) {
1325 try {
1326 mCallback.onDisplayEvent(displayId, event);
1327 } catch (RemoteException ex) {
1328 Slog.w(TAG, "Failed to notify process "
1329 + mPid + " that displays changed, assuming it died.", ex);
1330 binderDied();
1331 }
1332 }
1333 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001334
Santos Cordonee8931e2017-04-05 10:31:15 -07001335 @VisibleForTesting
1336 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001337 /**
1338 * Returns information about the specified logical display.
1339 *
1340 * @param displayId The logical display id.
1341 * @return The logical display info, or null if the display does not exist. The
1342 * returned object must be treated as immutable.
1343 */
1344 @Override // Binder call
1345 public DisplayInfo getDisplayInfo(int displayId) {
1346 final int callingUid = Binder.getCallingUid();
1347 final long token = Binder.clearCallingIdentity();
1348 try {
1349 return getDisplayInfoInternal(displayId, callingUid);
1350 } finally {
1351 Binder.restoreCallingIdentity(token);
1352 }
1353 }
1354
1355 /**
1356 * Returns the list of all display ids.
1357 */
1358 @Override // Binder call
1359 public int[] getDisplayIds() {
1360 final int callingUid = Binder.getCallingUid();
1361 final long token = Binder.clearCallingIdentity();
1362 try {
1363 return getDisplayIdsInternal(callingUid);
1364 } finally {
1365 Binder.restoreCallingIdentity(token);
1366 }
1367 }
1368
1369 @Override // Binder call
1370 public void registerCallback(IDisplayManagerCallback callback) {
1371 if (callback == null) {
1372 throw new IllegalArgumentException("listener must not be null");
1373 }
1374
1375 final int callingPid = Binder.getCallingPid();
1376 final long token = Binder.clearCallingIdentity();
1377 try {
1378 registerCallbackInternal(callback, callingPid);
1379 } finally {
1380 Binder.restoreCallingIdentity(token);
1381 }
1382 }
1383
1384 @Override // Binder call
1385 public void startWifiDisplayScan() {
1386 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1387 "Permission required to start wifi display scans");
1388
1389 final int callingPid = Binder.getCallingPid();
1390 final long token = Binder.clearCallingIdentity();
1391 try {
1392 startWifiDisplayScanInternal(callingPid);
1393 } finally {
1394 Binder.restoreCallingIdentity(token);
1395 }
1396 }
1397
1398 @Override // Binder call
1399 public void stopWifiDisplayScan() {
1400 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1401 "Permission required to stop wifi display scans");
1402
1403 final int callingPid = Binder.getCallingPid();
1404 final long token = Binder.clearCallingIdentity();
1405 try {
1406 stopWifiDisplayScanInternal(callingPid);
1407 } finally {
1408 Binder.restoreCallingIdentity(token);
1409 }
1410 }
1411
1412 @Override // Binder call
1413 public void connectWifiDisplay(String address) {
1414 if (address == null) {
1415 throw new IllegalArgumentException("address must not be null");
1416 }
1417 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1418 "Permission required to connect to a wifi display");
1419
1420 final long token = Binder.clearCallingIdentity();
1421 try {
1422 connectWifiDisplayInternal(address);
1423 } finally {
1424 Binder.restoreCallingIdentity(token);
1425 }
1426 }
1427
1428 @Override // Binder call
1429 public void disconnectWifiDisplay() {
1430 // This request does not require special permissions.
1431 // Any app can request disconnection from the currently active wifi display.
1432 // This exception should no longer be needed once wifi display control moves
1433 // to the media router service.
1434
1435 final long token = Binder.clearCallingIdentity();
1436 try {
1437 disconnectWifiDisplayInternal();
1438 } finally {
1439 Binder.restoreCallingIdentity(token);
1440 }
1441 }
1442
1443 @Override // Binder call
1444 public void renameWifiDisplay(String address, String alias) {
1445 if (address == null) {
1446 throw new IllegalArgumentException("address must not be null");
1447 }
1448 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1449 "Permission required to rename to a wifi display");
1450
1451 final long token = Binder.clearCallingIdentity();
1452 try {
1453 renameWifiDisplayInternal(address, alias);
1454 } finally {
1455 Binder.restoreCallingIdentity(token);
1456 }
1457 }
1458
1459 @Override // Binder call
1460 public void forgetWifiDisplay(String address) {
1461 if (address == null) {
1462 throw new IllegalArgumentException("address must not be null");
1463 }
1464 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1465 "Permission required to forget to a wifi display");
1466
1467 final long token = Binder.clearCallingIdentity();
1468 try {
1469 forgetWifiDisplayInternal(address);
1470 } finally {
1471 Binder.restoreCallingIdentity(token);
1472 }
1473 }
1474
1475 @Override // Binder call
1476 public void pauseWifiDisplay() {
1477 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1478 "Permission required to pause a wifi display session");
1479
1480 final long token = Binder.clearCallingIdentity();
1481 try {
1482 pauseWifiDisplayInternal();
1483 } finally {
1484 Binder.restoreCallingIdentity(token);
1485 }
1486 }
1487
1488 @Override // Binder call
1489 public void resumeWifiDisplay() {
1490 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1491 "Permission required to resume a wifi display session");
1492
1493 final long token = Binder.clearCallingIdentity();
1494 try {
1495 resumeWifiDisplayInternal();
1496 } finally {
1497 Binder.restoreCallingIdentity(token);
1498 }
1499 }
1500
1501 @Override // Binder call
1502 public WifiDisplayStatus getWifiDisplayStatus() {
1503 // This request does not require special permissions.
1504 // Any app can get information about available wifi displays.
1505
1506 final long token = Binder.clearCallingIdentity();
1507 try {
1508 return getWifiDisplayStatusInternal();
1509 } finally {
1510 Binder.restoreCallingIdentity(token);
1511 }
1512 }
1513
1514 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001515 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001516 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001517 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1518 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001519 final long token = Binder.clearCallingIdentity();
1520 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001521 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001522 } finally {
1523 Binder.restoreCallingIdentity(token);
1524 }
1525 }
1526
1527 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001528 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001529 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001530 int width, int height, int densityDpi, Surface surface, int flags,
1531 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001532 final int callingUid = Binder.getCallingUid();
1533 if (!validatePackageName(callingUid, packageName)) {
1534 throw new SecurityException("packageName must match the calling uid");
1535 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001536 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001537 throw new IllegalArgumentException("appToken must not be null");
1538 }
1539 if (TextUtils.isEmpty(name)) {
1540 throw new IllegalArgumentException("name must be non-null and non-empty");
1541 }
1542 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1543 throw new IllegalArgumentException("width, height, and densityDpi must be "
1544 + "greater than 0");
1545 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001546 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001547 throw new IllegalArgumentException("Surface can't be single-buffered");
1548 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001549
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001550 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1551 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1552
1553 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001554 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001555 throw new IllegalArgumentException(
1556 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1557 }
Michael Wright6720be42014-07-29 19:14:16 -07001558 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001559 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1560 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001561 }
1562
Michael Wrightc39d47a2014-07-08 18:07:36 -07001563 if (projection != null) {
1564 try {
1565 if (!getProjectionService().isValidMediaProjection(projection)) {
1566 throw new SecurityException("Invalid media projection");
1567 }
Michael Wright6720be42014-07-29 19:14:16 -07001568 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001569 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001570 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001571 }
1572 }
1573
Michael Wright6720be42014-07-29 19:14:16 -07001574 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001575 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001576 if (!canProjectVideo(projection)) {
1577 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1578 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1579 + "MediaProjection token in order to create a screen sharing virtual "
1580 + "display.");
1581 }
1582 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001583 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001584 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001585 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001586 + "or an appropriate MediaProjection token to create a "
1587 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001588 }
1589 }
1590
1591 final long token = Binder.clearCallingIdentity();
1592 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001593 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1594 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001595 } finally {
1596 Binder.restoreCallingIdentity(token);
1597 }
1598 }
1599
1600 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001601 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001602 int width, int height, int densityDpi) {
1603 final long token = Binder.clearCallingIdentity();
1604 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001605 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001606 } finally {
1607 Binder.restoreCallingIdentity(token);
1608 }
1609 }
1610
1611 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001612 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001613 if (surface != null && surface.isSingleBuffered()) {
1614 throw new IllegalArgumentException("Surface can't be single-buffered");
1615 }
Jeff Brown92207df2014-04-16 13:16:07 -07001616 final long token = Binder.clearCallingIdentity();
1617 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001618 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001619 } finally {
1620 Binder.restoreCallingIdentity(token);
1621 }
1622 }
1623
1624 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001625 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001626 final long token = Binder.clearCallingIdentity();
1627 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001628 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001629 } finally {
1630 Binder.restoreCallingIdentity(token);
1631 }
1632 }
1633
1634 @Override // Binder call
1635 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001636 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001637
1638 final long token = Binder.clearCallingIdentity();
1639 try {
1640 dumpInternal(pw);
1641 } finally {
1642 Binder.restoreCallingIdentity(token);
1643 }
1644 }
1645
1646 private boolean validatePackageName(int uid, String packageName) {
1647 if (packageName != null) {
1648 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1649 if (packageNames != null) {
1650 for (String n : packageNames) {
1651 if (n.equals(packageName)) {
1652 return true;
1653 }
1654 }
1655 }
1656 }
1657 return false;
1658 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001659
1660 private boolean canProjectVideo(IMediaProjection projection) {
1661 if (projection != null) {
1662 try {
1663 if (projection.canProjectVideo()) {
1664 return true;
1665 }
1666 } catch (RemoteException e) {
1667 Slog.e(TAG, "Unable to query projection service for permissions", e);
1668 }
1669 }
1670 if (mContext.checkCallingPermission(
1671 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001672 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001673 return true;
1674 }
1675 return canProjectSecureVideo(projection);
1676 }
1677
1678 private boolean canProjectSecureVideo(IMediaProjection projection) {
1679 if (projection != null) {
1680 try {
1681 if (projection.canProjectSecureVideo()){
1682 return true;
1683 }
1684 } catch (RemoteException e) {
1685 Slog.e(TAG, "Unable to query projection service for permissions", e);
1686 }
1687 }
1688 return mContext.checkCallingPermission(
1689 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001690 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001691 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001692 }
1693
1694 private final class LocalService extends DisplayManagerInternal {
1695 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001696 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001697 SensorManager sensorManager) {
1698 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001699 DisplayBlanker blanker = new DisplayBlanker() {
1700 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001701 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001702 // The order of operations is important for legacy reasons.
1703 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001704 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001705 }
1706
1707 callbacks.onDisplayStateChange(state);
1708
1709 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001710 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001711 }
1712 }
1713 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001714 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001715 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001716 }
1717 }
1718
1719 @Override
1720 public boolean requestPowerState(DisplayPowerRequest request,
1721 boolean waitForNegativeProximity) {
1722 return mDisplayPowerController.requestPowerState(request,
1723 waitForNegativeProximity);
1724 }
1725
1726 @Override
1727 public boolean isProximitySensorAvailable() {
1728 return mDisplayPowerController.isProximitySensorAvailable();
1729 }
1730
1731 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001732 public DisplayInfo getDisplayInfo(int displayId) {
1733 return getDisplayInfoInternal(displayId, Process.myUid());
1734 }
1735
1736 @Override
1737 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1738 if (listener == null) {
1739 throw new IllegalArgumentException("listener must not be null");
1740 }
1741
1742 registerDisplayTransactionListenerInternal(listener);
1743 }
1744
1745 @Override
1746 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1747 if (listener == null) {
1748 throw new IllegalArgumentException("listener must not be null");
1749 }
1750
1751 unregisterDisplayTransactionListenerInternal(listener);
1752 }
1753
1754 @Override
1755 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1756 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1757 }
1758
1759 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001760 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1761 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1762 }
1763
1764 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001765 public void performTraversalInTransactionFromWindowManager() {
1766 performTraversalInTransactionFromWindowManagerInternal();
1767 }
1768
1769 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001770 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001771 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1772 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1773 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001774 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001775
1776 @Override
1777 public void setDisplayOffsets(int displayId, int x, int y) {
1778 setDisplayOffsetsInternal(displayId, x, y);
1779 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001780
1781 @Override
1782 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1783 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1784 }
1785
1786 @Override
1787 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1788 return isUidPresentOnDisplayInternal(uid, displayId);
1789 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001790 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001791}