blob: 8269042da24a7eefe2b93e83b94736db34ba6e36 [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
Santos Cordonc22c5632017-06-21 16:03:49 -0700135 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700136 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 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100272 // android.display and android.anim is critical to user experience and we should make sure
273 // it is not in the default foregroup groups, add it to top-app to make sure it uses all the
274 // cores and scheduling settings for top-app when it runs.
275 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
276 Process.THREAD_GROUP_TOP_APP);
277 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
278 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700279 }
280
Jeff Brown4ccb8232014-01-16 22:16:42 -0800281 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800282 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700283 // We need to pre-load the persistent data store so it's ready before the default display
284 // adapter is up so that we have it's configuration. We could load it lazily, but since
285 // we're going to have to read it in eventually we may as well do it here rather than after
Santos Cordonc22c5632017-06-21 16:03:49 -0700286 // we've waited for the display to register itself with us.
Michael Wright1c9977b2016-07-12 13:30:10 -0700287 mPersistentDataStore.loadIfNeeded();
Santos Cordonc22c5632017-06-21 16:03:49 -0700288 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800289
290 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
291 true /*allowIsolated*/);
292 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700293 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800294 }
295
296 @Override
297 public void onBootPhase(int phase) {
298 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
299 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700300 long timeout = SystemClock.uptimeMillis()
301 + mInjector.getDefaultDisplayDelayTimeout();
302 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
303 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800304 long delay = timeout - SystemClock.uptimeMillis();
305 if (delay <= 0) {
306 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700307 + "to be initialized. DefaultDisplay="
308 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
309 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800310 }
311 if (DEBUG) {
312 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
313 }
314 try {
315 mSyncRoot.wait(delay);
316 } catch (InterruptedException ex) {
317 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700318 }
319 }
320 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700321 }
322
Jeff Brown4ccb8232014-01-16 22:16:42 -0800323 // TODO: Use dependencies or a boot phase
324 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700325 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800326 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
327 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700328 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700329 }
330 }
331
332 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700333 * Called when the system is ready to go.
334 */
335 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700336 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700337 mSafeMode = safeMode;
338 mOnlyCore = onlyCore;
339 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700340
Jeff Brownbd6e1502012-08-28 03:27:37 -0700341 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
342 }
343
Santos Cordonee8931e2017-04-05 10:31:15 -0700344 @VisibleForTesting
345 Handler getDisplayHandler() {
346 return mHandler;
347 }
348
Jeff Brown4ccb8232014-01-16 22:16:42 -0800349 private void registerDisplayTransactionListenerInternal(
350 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800351 // List is self-synchronized copy-on-write.
352 mDisplayTransactionListeners.add(listener);
353 }
354
Jeff Brown4ccb8232014-01-16 22:16:42 -0800355 private void unregisterDisplayTransactionListenerInternal(
356 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800357 // List is self-synchronized copy-on-write.
358 mDisplayTransactionListeners.remove(listener);
359 }
360
Jeff Brown4ccb8232014-01-16 22:16:42 -0800361 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700362 int displayId, DisplayInfo info) {
363 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700364 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700365 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700366 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700367 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700368 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700369 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700370 }
371 }
372 }
373
Andrii Kuliancd097992017-03-23 18:31:59 -0700374 /**
375 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
376 */
377 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
378 synchronized (mSyncRoot) {
379 final LogicalDisplay display = mLogicalDisplays.get(displayId);
380 if (display != null) {
381 display.getNonOverrideDisplayInfoLocked(outInfo);
382 }
383 }
384 }
385
Santos Cordonee8931e2017-04-05 10:31:15 -0700386 @VisibleForTesting
387 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700388 synchronized (mSyncRoot) {
389 if (!mPendingTraversal) {
390 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700391 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700392 mPendingTraversal = false;
393
394 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700395 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800396
397 // List is self-synchronized copy-on-write.
398 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
399 listener.onDisplayTransaction();
400 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700401 }
402
Jeff Brown5d6443b2015-04-10 20:15:01 -0700403 private void requestGlobalDisplayStateInternal(int state, int brightness) {
404 if (state == Display.STATE_UNKNOWN) {
405 state = Display.STATE_ON;
406 }
407 if (state == Display.STATE_OFF) {
408 brightness = PowerManager.BRIGHTNESS_OFF;
409 } else if (brightness < 0) {
410 brightness = PowerManager.BRIGHTNESS_DEFAULT;
411 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
412 brightness = PowerManager.BRIGHTNESS_ON;
413 }
414
Jeff Browne75926d2014-09-18 15:24:49 -0700415 synchronized (mTempDisplayStateWorkQueue) {
416 try {
417 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700418 // Note that we do not need to schedule traversals here although it
419 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700420 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700421 if (mGlobalDisplayState == state
422 && mGlobalDisplayBrightness == brightness) {
423 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700424 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700425
426 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
427 + Display.stateToString(state)
428 + ", brightness=" + brightness + ")");
429 mGlobalDisplayState = state;
430 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700431 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700432 }
433
434 // Setting the display power state can take hundreds of milliseconds
435 // to complete so we defer the most expensive part of the work until
436 // after we have exited the critical section to avoid blocking other
437 // threads for a long time.
438 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
439 mTempDisplayStateWorkQueue.get(i).run();
440 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700441 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700442 } finally {
443 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700444 }
445 }
446 }
447
Jeff Brown4ccb8232014-01-16 22:16:42 -0800448 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700449 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800450 LogicalDisplay display = mLogicalDisplays.get(displayId);
451 if (display != null) {
452 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800453 if (info.hasAccess(callingUid)
454 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800455 return info;
456 }
457 }
458 return null;
459 }
460 }
461
462 private int[] getDisplayIdsInternal(int callingUid) {
463 synchronized (mSyncRoot) {
464 final int count = mLogicalDisplays.size();
465 int[] displayIds = new int[count];
466 int n = 0;
467 for (int i = 0; i < count; i++) {
468 LogicalDisplay display = mLogicalDisplays.valueAt(i);
469 DisplayInfo info = display.getDisplayInfoLocked();
470 if (info.hasAccess(callingUid)) {
471 displayIds[n++] = mLogicalDisplays.keyAt(i);
472 }
473 }
474 if (n != count) {
475 displayIds = Arrays.copyOfRange(displayIds, 0, n);
476 }
477 return displayIds;
478 }
479 }
480
481 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
482 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700483 if (mCallbacks.get(callingPid) != null) {
484 throw new SecurityException("The calling process has already "
485 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700486 }
487
Jeff Brownbd6e1502012-08-28 03:27:37 -0700488 CallbackRecord record = new CallbackRecord(callingPid, callback);
489 try {
490 IBinder binder = callback.asBinder();
491 binder.linkToDeath(record, 0);
492 } catch (RemoteException ex) {
493 // give up
494 throw new RuntimeException(ex);
495 }
496
497 mCallbacks.put(callingPid, record);
498 }
499 }
500
Jeff Brownce468a32013-11-21 16:42:03 -0800501 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700502 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800503 mCallbacks.remove(record.mPid);
504 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700505 }
506 }
507
Jeff Brown4ccb8232014-01-16 22:16:42 -0800508 private void startWifiDisplayScanInternal(int callingPid) {
509 synchronized (mSyncRoot) {
510 CallbackRecord record = mCallbacks.get(callingPid);
511 if (record == null) {
512 throw new IllegalStateException("The calling process has not "
513 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700514 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800515 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700516 }
517 }
518
Jeff Brownce468a32013-11-21 16:42:03 -0800519 private void startWifiDisplayScanLocked(CallbackRecord record) {
520 if (!record.mWifiDisplayScanRequested) {
521 record.mWifiDisplayScanRequested = true;
522 if (mWifiDisplayScanRequestCount++ == 0) {
523 if (mWifiDisplayAdapter != null) {
524 mWifiDisplayAdapter.requestStartScanLocked();
525 }
526 }
527 }
528 }
529
Jeff Brown4ccb8232014-01-16 22:16:42 -0800530 private void stopWifiDisplayScanInternal(int callingPid) {
531 synchronized (mSyncRoot) {
532 CallbackRecord record = mCallbacks.get(callingPid);
533 if (record == null) {
534 throw new IllegalStateException("The calling process has not "
535 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800536 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800537 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800538 }
539 }
540
541 private void stopWifiDisplayScanLocked(CallbackRecord record) {
542 if (record.mWifiDisplayScanRequested) {
543 record.mWifiDisplayScanRequested = false;
544 if (--mWifiDisplayScanRequestCount == 0) {
545 if (mWifiDisplayAdapter != null) {
546 mWifiDisplayAdapter.requestStopScanLocked();
547 }
548 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700549 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800550 + mWifiDisplayScanRequestCount);
551 mWifiDisplayScanRequestCount = 0;
552 }
553 }
554 }
555
Jeff Brown4ccb8232014-01-16 22:16:42 -0800556 private void connectWifiDisplayInternal(String address) {
557 synchronized (mSyncRoot) {
558 if (mWifiDisplayAdapter != null) {
559 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700560 }
Jeff Browne08ae382012-09-07 20:36:36 -0700561 }
562 }
563
Jeff Brown4ccb8232014-01-16 22:16:42 -0800564 private void pauseWifiDisplayInternal() {
565 synchronized (mSyncRoot) {
566 if (mWifiDisplayAdapter != null) {
567 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700568 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700569 }
570 }
571
Jeff Brown4ccb8232014-01-16 22:16:42 -0800572 private void resumeWifiDisplayInternal() {
573 synchronized (mSyncRoot) {
574 if (mWifiDisplayAdapter != null) {
575 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700576 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700577 }
578 }
579
Jeff Brown4ccb8232014-01-16 22:16:42 -0800580 private void disconnectWifiDisplayInternal() {
581 synchronized (mSyncRoot) {
582 if (mWifiDisplayAdapter != null) {
583 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700584 }
Jeff Browne08ae382012-09-07 20:36:36 -0700585 }
586 }
587
Jeff Brown4ccb8232014-01-16 22:16:42 -0800588 private void renameWifiDisplayInternal(String address, String alias) {
589 synchronized (mSyncRoot) {
590 if (mWifiDisplayAdapter != null) {
591 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700592 }
Jeff Brown89d55462012-09-19 11:33:42 -0700593 }
594 }
595
Jeff Brown4ccb8232014-01-16 22:16:42 -0800596 private void forgetWifiDisplayInternal(String address) {
597 synchronized (mSyncRoot) {
598 if (mWifiDisplayAdapter != null) {
599 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700600 }
Jeff Brown89d55462012-09-19 11:33:42 -0700601 }
602 }
603
Jeff Brown4ccb8232014-01-16 22:16:42 -0800604 private WifiDisplayStatus getWifiDisplayStatusInternal() {
605 synchronized (mSyncRoot) {
606 if (mWifiDisplayAdapter != null) {
607 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700608 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800609 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700610 }
611 }
612
Michael Wright1c9977b2016-07-12 13:30:10 -0700613 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100614 synchronized (mSyncRoot) {
615 LogicalDisplay display = mLogicalDisplays.get(displayId);
616 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700617 display.getRequestedColorModeLocked() != colorMode) {
618 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100619 scheduleTraversalLocked(false);
620 }
621 }
622 }
623
Michael Wright75ee9fc2014-09-01 19:55:22 -0700624 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700625 IMediaProjection projection, int callingUid, String packageName, String name, int width,
626 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800627 synchronized (mSyncRoot) {
628 if (mVirtualDisplayAdapter == null) {
629 Slog.w(TAG, "Rejecting request to create private virtual display "
630 + "because the virtual display adapter is not available.");
631 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700632 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800633
634 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700635 callback, projection, callingUid, packageName, name, width, height, densityDpi,
636 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800637 if (device == null) {
638 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700639 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700640
Jeff Brown4ccb8232014-01-16 22:16:42 -0800641 handleDisplayDeviceAddedLocked(device);
642 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
643 if (display != null) {
644 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700645 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800646
647 // Something weird happened and the logical display was not created.
648 Slog.w(TAG, "Rejecting request to create virtual display "
649 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700650 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800651 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700652 }
653 return -1;
654 }
655
Michael Wright01e840f2014-06-26 16:03:25 -0700656 private void resizeVirtualDisplayInternal(IBinder appToken,
657 int width, int height, int densityDpi) {
658 synchronized (mSyncRoot) {
659 if (mVirtualDisplayAdapter == null) {
660 return;
661 }
662
663 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
664 }
665 }
666
Jeff Brown92207df2014-04-16 13:16:07 -0700667 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
668 synchronized (mSyncRoot) {
669 if (mVirtualDisplayAdapter == null) {
670 return;
671 }
672
673 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
674 }
675 }
676
Jeff Brown4ccb8232014-01-16 22:16:42 -0800677 private void releaseVirtualDisplayInternal(IBinder appToken) {
678 synchronized (mSyncRoot) {
679 if (mVirtualDisplayAdapter == null) {
680 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700681 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700682
Jeff Brown4ccb8232014-01-16 22:16:42 -0800683 DisplayDevice device =
684 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
685 if (device != null) {
686 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700687 }
688 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700689 }
690
Santos Cordonc22c5632017-06-21 16:03:49 -0700691 private void registerDefaultDisplayAdapters() {
692 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700693 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700694 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800695 registerDisplayAdapterLocked(new LocalDisplayAdapter(
696 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700697
698 // Standalone VR devices rely on a virtual display as their primary display for
699 // 2D UI. We register virtual display adapter along side the main display adapter
700 // here so that it is ready by the time the system sends the home Intent for
701 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
702 // the virtual display inside VR before any VR-specific apps even run.
703 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
704 mHandler, mDisplayAdapterListener);
705 if (mVirtualDisplayAdapter != null) {
706 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
707 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700708 }
709 }
710
711 private void registerAdditionalDisplayAdapters() {
712 synchronized (mSyncRoot) {
713 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700714 registerOverlayDisplayAdapterLocked();
715 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700716 }
717 }
718 }
719
Jeff Brown89d55462012-09-19 11:33:42 -0700720 private void registerOverlayDisplayAdapterLocked() {
721 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
722 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
723 }
724
725 private void registerWifiDisplayAdapterLocked() {
726 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700727 com.android.internal.R.bool.config_enableWifiDisplay)
728 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700729 mWifiDisplayAdapter = new WifiDisplayAdapter(
730 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
731 mPersistentDataStore);
732 registerDisplayAdapterLocked(mWifiDisplayAdapter);
733 }
734 }
735
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700736 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
737 // In safe mode, we disable non-essential display adapters to give the user
738 // an opportunity to fix broken settings or other problems that might affect
739 // system stability.
740 // In only-core mode, we disable non-essential display adapters to minimize
741 // the number of dependencies that are started while in this mode and to
742 // prevent problems that might occur due to the device being encrypted.
743 return !mSafeMode && !mOnlyCore;
744 }
745
746 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
747 mDisplayAdapters.add(adapter);
748 adapter.registerLocked();
749 }
750
Jeff Brownbd6e1502012-08-28 03:27:37 -0700751 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700752 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700753 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700754 }
755 }
756
Jeff Browna506a6e2013-06-04 00:02:38 -0700757 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700758 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700759 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700760 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700761 return;
762 }
763
Jeff Brown10acf6d2015-04-14 14:20:47 -0700764 Slog.i(TAG, "Display device added: " + info);
765 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700766
767 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700768 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700769 Runnable work = updateDisplayStateLocked(device);
770 if (work != null) {
771 work.run();
772 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700773 if (display != null && display.getPrimaryDisplayDeviceLocked() == device) {
774 int colorMode = mPersistentDataStore.getColorMode(device);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700775 if (colorMode == Display.COLOR_MODE_INVALID) {
776 if ((device.getDisplayDeviceInfoLocked().flags
777 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
778 colorMode = mDefaultDisplayDefaultColorMode;
779 } else {
780 colorMode = Display.COLOR_MODE_DEFAULT;
781 }
782 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700783 display.setRequestedColorModeLocked(colorMode);
784 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700785 scheduleTraversalLocked(false);
786 }
787
Jeff Brownbd6e1502012-08-28 03:27:37 -0700788 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700789 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700790 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700791 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700792 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700793 return;
794 }
795
Jeff Brown10acf6d2015-04-14 14:20:47 -0700796 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
797 if (diff == DisplayDeviceInfo.DIFF_STATE) {
798 Slog.i(TAG, "Display device changed state: \"" + info.name
799 + "\", " + Display.stateToString(info.state));
800 } else if (diff != 0) {
801 Slog.i(TAG, "Display device changed: " + info);
802 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700803 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
804 try {
805 mPersistentDataStore.setColorMode(device, info.colorMode);
806 } finally {
807 mPersistentDataStore.saveIfNeeded();
808 }
809 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700810 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700811
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700812 device.applyPendingDisplayDeviceInfoChangesLocked();
813 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700814 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700815 }
816 }
817 }
818
Jeff Brownbd6e1502012-08-28 03:27:37 -0700819 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700820 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700821 handleDisplayDeviceRemovedLocked(device);
822 }
823 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700824
Jeff Browna506a6e2013-06-04 00:02:38 -0700825 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700826 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700827 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700828 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700829 return;
830 }
831
Jeff Brown10acf6d2015-04-14 14:20:47 -0700832 Slog.i(TAG, "Display device removed: " + info);
833 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700834
Jeff Browna506a6e2013-06-04 00:02:38 -0700835 updateLogicalDisplaysLocked();
836 scheduleTraversalLocked(false);
837 }
838
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700839 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700840 final int count = mDisplayDevices.size();
841 for (int i = 0; i < count; i++) {
842 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700843 Runnable runnable = updateDisplayStateLocked(device);
844 if (runnable != null) {
845 workQueue.add(runnable);
846 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700847 }
848 }
849
Jeff Browne75926d2014-09-18 15:24:49 -0700850 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700851 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700852 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700853 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
854 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700855 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700856 }
Jeff Browne75926d2014-09-18 15:24:49 -0700857 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700858 }
859
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700860 // Adds a new logical display based on the given display device.
861 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700862 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700863 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
864 boolean isDefault = (deviceInfo.flags
865 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
866 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
867 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
868 isDefault = false;
869 }
870
Jeff Brown27f1d672012-10-17 18:32:34 -0700871 if (!isDefault && mSingleDisplayDemoMode) {
872 Slog.i(TAG, "Not creating a logical display for a secondary display "
873 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700874 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700875 }
876
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700877 final int displayId = assignDisplayIdLocked(isDefault);
878 final int layerStack = assignLayerStackLocked(displayId);
879
Jeff Brownd728bf52012-09-08 18:05:28 -0700880 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700881 display.updateLocked(mDisplayDevices);
882 if (!display.isValidLocked()) {
883 // This should never happen currently.
884 Slog.w(TAG, "Ignoring display device because the logical display "
885 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700886 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700887 }
888
889 mLogicalDisplays.put(displayId, display);
890
891 // Wake up waitForDefaultDisplay.
892 if (isDefault) {
893 mSyncRoot.notifyAll();
894 }
895
896 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700897 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700898 }
899
900 private int assignDisplayIdLocked(boolean isDefault) {
901 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
902 }
903
904 private int assignLayerStackLocked(int displayId) {
905 // Currently layer stacks and display ids are the same.
906 // This need not be the case.
907 return displayId;
908 }
909
910 // Updates all existing logical displays given the current set of display devices.
911 // Removes invalid logical displays.
912 // Sends notifications if needed.
913 private boolean updateLogicalDisplaysLocked() {
914 boolean changed = false;
915 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
916 final int displayId = mLogicalDisplays.keyAt(i);
917 LogicalDisplay display = mLogicalDisplays.valueAt(i);
918
919 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
920 display.updateLocked(mDisplayDevices);
921 if (!display.isValidLocked()) {
922 mLogicalDisplays.removeAt(i);
923 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
924 changed = true;
925 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
926 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
927 changed = true;
928 }
929 }
930 return changed;
931 }
932
933 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700934 // Clear all viewports before configuring displays so that we can keep
935 // track of which ones we have configured.
936 clearViewportsLocked();
937
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700938 // Configure each display device.
939 final int count = mDisplayDevices.size();
940 for (int i = 0; i < count; i++) {
941 DisplayDevice device = mDisplayDevices.get(i);
942 configureDisplayInTransactionLocked(device);
943 device.performTraversalInTransactionLocked();
944 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700945
946 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800947 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700948 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
949 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700950 }
951
Michael Wright3f145a22014-07-22 19:46:03 -0700952 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700953 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700954 synchronized (mSyncRoot) {
955 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700956 if (display == null) {
957 return;
958 }
959 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700960 if (DEBUG) {
961 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
962 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
963 }
964
Craig Mautner722285e2012-09-07 13:55:58 -0700965 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700966 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700967 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700968 if (requestedModeId == 0 && requestedRefreshRate != 0) {
969 // Scan supported modes returned by display.getInfo() to find a mode with the same
970 // size as the default display mode but with the specified refresh rate instead.
971 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
972 requestedRefreshRate);
973 }
974 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700975 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700976 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700977 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700978 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700979 scheduleTraversalLocked(inTraversal);
980 }
Craig Mautner722285e2012-09-07 13:55:58 -0700981 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700982 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700983
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800984 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
985 synchronized (mSyncRoot) {
986 LogicalDisplay display = mLogicalDisplays.get(displayId);
987 if (display == null) {
988 return;
989 }
990 if (display.getDisplayOffsetXLocked() != x
991 || display.getDisplayOffsetYLocked() != y) {
992 if (DEBUG) {
993 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
994 + x + ", " + y + ")");
995 }
996 display.setDisplayOffsetsLocked(x, y);
997 scheduleTraversalLocked(false);
998 }
999 }
1000 }
1001
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001002 // Updates the lists of UIDs that are present on displays.
1003 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1004 synchronized (mSyncRoot) {
1005 mDisplayAccessUIDs.clear();
1006 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1007 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1008 newDisplayAccessUIDs.valueAt(i));
1009 }
1010 }
1011 }
1012
1013 // Checks if provided UID's content is present on the display and UID has access to it.
1014 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1015 synchronized (mSyncRoot) {
1016 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1017 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1018 }
1019 }
1020
Jeff Brownd728bf52012-09-08 18:05:28 -07001021 private void clearViewportsLocked() {
1022 mDefaultViewport.valid = false;
1023 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001024 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001025 }
1026
1027 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001028 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1029 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001030
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001031 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001032 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001033 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001034 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001035 if (display != null && !display.hasContentLocked()) {
1036 // If the display does not have any content of its own, then
1037 // automatically mirror the default logical display contents.
1038 display = null;
1039 }
1040 if (display == null) {
1041 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1042 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001043 }
1044
1045 // Apply the logical display configuration to the display device.
1046 if (display == null) {
1047 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001048 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001049 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001050 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001051 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001052 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001053
1054 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001055 if (!mDefaultViewport.valid
1056 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1057 setViewportLocked(mDefaultViewport, display, device);
1058 }
1059 if (!mExternalTouchViewport.valid
1060 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1061 setViewportLocked(mExternalTouchViewport, display, device);
1062 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001063
1064 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1065 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1066 setViewportLocked(viewport, display, device);
1067 }
1068 }
1069
1070 /** Gets the virtual device viewport or creates it if not yet created. */
1071 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1072 DisplayViewport viewport;
1073 final int count = mVirtualTouchViewports.size();
1074 for (int i = 0; i < count; i++) {
1075 viewport = mVirtualTouchViewports.get(i);
1076 if (uniqueId.equals(viewport.uniqueId)) {
1077 return viewport;
1078 }
1079 }
1080
1081 viewport = new DisplayViewport();
1082 viewport.uniqueId = uniqueId;
1083 mVirtualTouchViewports.add(viewport);
1084 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001085 }
1086
1087 private static void setViewportLocked(DisplayViewport viewport,
1088 LogicalDisplay display, DisplayDevice device) {
1089 viewport.valid = true;
1090 viewport.displayId = display.getDisplayIdLocked();
1091 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001092 }
1093
1094 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1095 final int count = mLogicalDisplays.size();
1096 for (int i = 0; i < count; i++) {
1097 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1098 if (display.getPrimaryDisplayDeviceLocked() == device) {
1099 return display;
1100 }
1101 }
1102 return null;
1103 }
1104
Jeff Brownbd6e1502012-08-28 03:27:37 -07001105 private void sendDisplayEventLocked(int displayId, int event) {
1106 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1107 mHandler.sendMessage(msg);
1108 }
1109
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001110 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1111 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001112 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001113 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001114 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001115 if (!inTraversal) {
1116 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1117 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001118 }
1119 }
1120
1121 // Runs on Handler thread.
1122 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001123 private void deliverDisplayEvent(int displayId, int event) {
1124 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001125 Slog.d(TAG, "Delivering display event: displayId="
1126 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001127 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001128
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001129 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001130 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001131 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001132 count = mCallbacks.size();
1133 mTempCallbacks.clear();
1134 for (int i = 0; i < count; i++) {
1135 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001136 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001137 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001138
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001139 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001140 for (int i = 0; i < count; i++) {
1141 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1142 }
1143 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001144 }
1145
Michael Wrightc39d47a2014-07-08 18:07:36 -07001146 private IMediaProjectionManager getProjectionService() {
1147 if (mProjectionService == null) {
1148 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1149 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1150 }
1151 return mProjectionService;
1152 }
1153
Jeff Brown4ccb8232014-01-16 22:16:42 -08001154 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001155 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001156
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001157 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001158 pw.println(" mOnlyCode=" + mOnlyCore);
1159 pw.println(" mSafeMode=" + mSafeMode);
1160 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001161 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001162 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1163 pw.println(" mDefaultViewport=" + mDefaultViewport);
1164 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001165 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001166 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001167 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001168 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001169
Jeff Brownbd6e1502012-08-28 03:27:37 -07001170 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001171 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001172
1173 pw.println();
1174 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001175 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001176 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001177 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001178 }
Craig Mautner9de49362012-08-02 14:30:30 -07001179
Jeff Brownbd6e1502012-08-28 03:27:37 -07001180 pw.println();
1181 pw.println("Display Devices: size=" + mDisplayDevices.size());
1182 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001183 pw.println(" " + device.getDisplayDeviceInfoLocked());
1184 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001185 }
1186
1187 final int logicalDisplayCount = mLogicalDisplays.size();
1188 pw.println();
1189 pw.println("Logical Displays: size=" + logicalDisplayCount);
1190 for (int i = 0; i < logicalDisplayCount; i++) {
1191 int displayId = mLogicalDisplays.keyAt(i);
1192 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1193 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001194 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001195 }
Jeff Brownce468a32013-11-21 16:42:03 -08001196
1197 final int callbackCount = mCallbacks.size();
1198 pw.println();
1199 pw.println("Callbacks: size=" + callbackCount);
1200 for (int i = 0; i < callbackCount; i++) {
1201 CallbackRecord callback = mCallbacks.valueAt(i);
1202 pw.println(" " + i + ": mPid=" + callback.mPid
1203 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1204 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001205
1206 if (mDisplayPowerController != null) {
1207 mDisplayPowerController.dump(pw);
1208 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001209
1210 pw.println();
1211 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001212 }
1213 }
1214
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001215 /**
1216 * This is the object that everything in the display manager locks on.
1217 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1218 * clear that the object belongs to the display manager service and that it is
1219 * a unique object with a special purpose.
1220 */
1221 public static final class SyncRoot {
1222 }
1223
Santos Cordonee8931e2017-04-05 10:31:15 -07001224 @VisibleForTesting
1225 static class Injector {
1226 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1227 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1228 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1229 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001230
1231 long getDefaultDisplayDelayTimeout() {
1232 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1233 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001234 }
1235
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001236 @VisibleForTesting
1237 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1238 synchronized (mSyncRoot) {
1239 LogicalDisplay display = mLogicalDisplays.get(displayId);
1240 if (display != null) {
1241 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1242 return displayDevice.getDisplayDeviceInfoLocked();
1243 }
1244 return null;
1245 }
1246 }
1247
Jeff Brownbd6e1502012-08-28 03:27:37 -07001248 private final class DisplayManagerHandler extends Handler {
1249 public DisplayManagerHandler(Looper looper) {
1250 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001251 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001252
Jeff Brownbd6e1502012-08-28 03:27:37 -07001253 @Override
1254 public void handleMessage(Message msg) {
1255 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001256 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1257 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001258 break;
1259
1260 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1261 registerAdditionalDisplayAdapters();
1262 break;
1263
1264 case MSG_DELIVER_DISPLAY_EVENT:
1265 deliverDisplayEvent(msg.arg1, msg.arg2);
1266 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001267
1268 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001269 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001270 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001271
1272 case MSG_UPDATE_VIEWPORT: {
1273 synchronized (mSyncRoot) {
1274 mTempDefaultViewport.copyFrom(mDefaultViewport);
1275 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001276 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1277 mTempVirtualTouchViewports.clear();
1278 for (DisplayViewport d : mVirtualTouchViewports) {
1279 mTempVirtualTouchViewports.add(d.makeCopy());
1280 }
1281 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001282 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001283 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1284 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001285 break;
1286 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001287 }
1288 }
1289 }
1290
1291 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1292 @Override
1293 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1294 switch (event) {
1295 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1296 handleDisplayDeviceAdded(device);
1297 break;
1298
1299 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1300 handleDisplayDeviceChanged(device);
1301 break;
1302
1303 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1304 handleDisplayDeviceRemoved(device);
1305 break;
1306 }
1307 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001308
1309 @Override
1310 public void onTraversalRequested() {
1311 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001312 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001313 }
1314 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001315 }
1316
1317 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001318 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001319 private final IDisplayManagerCallback mCallback;
1320
Jeff Brownce468a32013-11-21 16:42:03 -08001321 public boolean mWifiDisplayScanRequested;
1322
Jeff Brownbd6e1502012-08-28 03:27:37 -07001323 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1324 mPid = pid;
1325 mCallback = callback;
1326 }
1327
1328 @Override
1329 public void binderDied() {
1330 if (DEBUG) {
1331 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1332 }
Jeff Brownce468a32013-11-21 16:42:03 -08001333 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001334 }
1335
1336 public void notifyDisplayEventAsync(int displayId, int event) {
1337 try {
1338 mCallback.onDisplayEvent(displayId, event);
1339 } catch (RemoteException ex) {
1340 Slog.w(TAG, "Failed to notify process "
1341 + mPid + " that displays changed, assuming it died.", ex);
1342 binderDied();
1343 }
1344 }
1345 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001346
Santos Cordonee8931e2017-04-05 10:31:15 -07001347 @VisibleForTesting
1348 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001349 /**
1350 * Returns information about the specified logical display.
1351 *
1352 * @param displayId The logical display id.
1353 * @return The logical display info, or null if the display does not exist. The
1354 * returned object must be treated as immutable.
1355 */
1356 @Override // Binder call
1357 public DisplayInfo getDisplayInfo(int displayId) {
1358 final int callingUid = Binder.getCallingUid();
1359 final long token = Binder.clearCallingIdentity();
1360 try {
1361 return getDisplayInfoInternal(displayId, callingUid);
1362 } finally {
1363 Binder.restoreCallingIdentity(token);
1364 }
1365 }
1366
1367 /**
1368 * Returns the list of all display ids.
1369 */
1370 @Override // Binder call
1371 public int[] getDisplayIds() {
1372 final int callingUid = Binder.getCallingUid();
1373 final long token = Binder.clearCallingIdentity();
1374 try {
1375 return getDisplayIdsInternal(callingUid);
1376 } finally {
1377 Binder.restoreCallingIdentity(token);
1378 }
1379 }
1380
1381 @Override // Binder call
1382 public void registerCallback(IDisplayManagerCallback callback) {
1383 if (callback == null) {
1384 throw new IllegalArgumentException("listener must not be null");
1385 }
1386
1387 final int callingPid = Binder.getCallingPid();
1388 final long token = Binder.clearCallingIdentity();
1389 try {
1390 registerCallbackInternal(callback, callingPid);
1391 } finally {
1392 Binder.restoreCallingIdentity(token);
1393 }
1394 }
1395
1396 @Override // Binder call
1397 public void startWifiDisplayScan() {
1398 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1399 "Permission required to start wifi display scans");
1400
1401 final int callingPid = Binder.getCallingPid();
1402 final long token = Binder.clearCallingIdentity();
1403 try {
1404 startWifiDisplayScanInternal(callingPid);
1405 } finally {
1406 Binder.restoreCallingIdentity(token);
1407 }
1408 }
1409
1410 @Override // Binder call
1411 public void stopWifiDisplayScan() {
1412 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1413 "Permission required to stop wifi display scans");
1414
1415 final int callingPid = Binder.getCallingPid();
1416 final long token = Binder.clearCallingIdentity();
1417 try {
1418 stopWifiDisplayScanInternal(callingPid);
1419 } finally {
1420 Binder.restoreCallingIdentity(token);
1421 }
1422 }
1423
1424 @Override // Binder call
1425 public void connectWifiDisplay(String address) {
1426 if (address == null) {
1427 throw new IllegalArgumentException("address must not be null");
1428 }
1429 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1430 "Permission required to connect to a wifi display");
1431
1432 final long token = Binder.clearCallingIdentity();
1433 try {
1434 connectWifiDisplayInternal(address);
1435 } finally {
1436 Binder.restoreCallingIdentity(token);
1437 }
1438 }
1439
1440 @Override // Binder call
1441 public void disconnectWifiDisplay() {
1442 // This request does not require special permissions.
1443 // Any app can request disconnection from the currently active wifi display.
1444 // This exception should no longer be needed once wifi display control moves
1445 // to the media router service.
1446
1447 final long token = Binder.clearCallingIdentity();
1448 try {
1449 disconnectWifiDisplayInternal();
1450 } finally {
1451 Binder.restoreCallingIdentity(token);
1452 }
1453 }
1454
1455 @Override // Binder call
1456 public void renameWifiDisplay(String address, String alias) {
1457 if (address == null) {
1458 throw new IllegalArgumentException("address must not be null");
1459 }
1460 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1461 "Permission required to rename to a wifi display");
1462
1463 final long token = Binder.clearCallingIdentity();
1464 try {
1465 renameWifiDisplayInternal(address, alias);
1466 } finally {
1467 Binder.restoreCallingIdentity(token);
1468 }
1469 }
1470
1471 @Override // Binder call
1472 public void forgetWifiDisplay(String address) {
1473 if (address == null) {
1474 throw new IllegalArgumentException("address must not be null");
1475 }
1476 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1477 "Permission required to forget to a wifi display");
1478
1479 final long token = Binder.clearCallingIdentity();
1480 try {
1481 forgetWifiDisplayInternal(address);
1482 } finally {
1483 Binder.restoreCallingIdentity(token);
1484 }
1485 }
1486
1487 @Override // Binder call
1488 public void pauseWifiDisplay() {
1489 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1490 "Permission required to pause a wifi display session");
1491
1492 final long token = Binder.clearCallingIdentity();
1493 try {
1494 pauseWifiDisplayInternal();
1495 } finally {
1496 Binder.restoreCallingIdentity(token);
1497 }
1498 }
1499
1500 @Override // Binder call
1501 public void resumeWifiDisplay() {
1502 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1503 "Permission required to resume a wifi display session");
1504
1505 final long token = Binder.clearCallingIdentity();
1506 try {
1507 resumeWifiDisplayInternal();
1508 } finally {
1509 Binder.restoreCallingIdentity(token);
1510 }
1511 }
1512
1513 @Override // Binder call
1514 public WifiDisplayStatus getWifiDisplayStatus() {
1515 // This request does not require special permissions.
1516 // Any app can get information about available wifi displays.
1517
1518 final long token = Binder.clearCallingIdentity();
1519 try {
1520 return getWifiDisplayStatusInternal();
1521 } finally {
1522 Binder.restoreCallingIdentity(token);
1523 }
1524 }
1525
1526 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001527 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001528 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001529 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1530 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001531 final long token = Binder.clearCallingIdentity();
1532 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001533 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001534 } finally {
1535 Binder.restoreCallingIdentity(token);
1536 }
1537 }
1538
1539 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001540 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001541 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001542 int width, int height, int densityDpi, Surface surface, int flags,
1543 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001544 final int callingUid = Binder.getCallingUid();
1545 if (!validatePackageName(callingUid, packageName)) {
1546 throw new SecurityException("packageName must match the calling uid");
1547 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001548 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001549 throw new IllegalArgumentException("appToken must not be null");
1550 }
1551 if (TextUtils.isEmpty(name)) {
1552 throw new IllegalArgumentException("name must be non-null and non-empty");
1553 }
1554 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1555 throw new IllegalArgumentException("width, height, and densityDpi must be "
1556 + "greater than 0");
1557 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001558 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001559 throw new IllegalArgumentException("Surface can't be single-buffered");
1560 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001561
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001562 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1563 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1564
1565 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001566 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001567 throw new IllegalArgumentException(
1568 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1569 }
Michael Wright6720be42014-07-29 19:14:16 -07001570 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001571 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1572 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001573 }
1574
Michael Wrightc39d47a2014-07-08 18:07:36 -07001575 if (projection != null) {
1576 try {
1577 if (!getProjectionService().isValidMediaProjection(projection)) {
1578 throw new SecurityException("Invalid media projection");
1579 }
Michael Wright6720be42014-07-29 19:14:16 -07001580 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001581 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001582 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001583 }
1584 }
1585
Michael Wright6720be42014-07-29 19:14:16 -07001586 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001587 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001588 if (!canProjectVideo(projection)) {
1589 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1590 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1591 + "MediaProjection token in order to create a screen sharing virtual "
1592 + "display.");
1593 }
1594 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001595 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001596 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001597 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001598 + "or an appropriate MediaProjection token to create a "
1599 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001600 }
1601 }
1602
1603 final long token = Binder.clearCallingIdentity();
1604 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001605 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1606 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001607 } finally {
1608 Binder.restoreCallingIdentity(token);
1609 }
1610 }
1611
1612 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001613 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001614 int width, int height, int densityDpi) {
1615 final long token = Binder.clearCallingIdentity();
1616 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001617 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001618 } finally {
1619 Binder.restoreCallingIdentity(token);
1620 }
1621 }
1622
1623 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001624 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001625 if (surface != null && surface.isSingleBuffered()) {
1626 throw new IllegalArgumentException("Surface can't be single-buffered");
1627 }
Jeff Brown92207df2014-04-16 13:16:07 -07001628 final long token = Binder.clearCallingIdentity();
1629 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001630 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001631 } finally {
1632 Binder.restoreCallingIdentity(token);
1633 }
1634 }
1635
1636 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001637 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001638 final long token = Binder.clearCallingIdentity();
1639 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001640 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001641 } finally {
1642 Binder.restoreCallingIdentity(token);
1643 }
1644 }
1645
1646 @Override // Binder call
1647 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001648 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001649
1650 final long token = Binder.clearCallingIdentity();
1651 try {
1652 dumpInternal(pw);
1653 } finally {
1654 Binder.restoreCallingIdentity(token);
1655 }
1656 }
1657
1658 private boolean validatePackageName(int uid, String packageName) {
1659 if (packageName != null) {
1660 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1661 if (packageNames != null) {
1662 for (String n : packageNames) {
1663 if (n.equals(packageName)) {
1664 return true;
1665 }
1666 }
1667 }
1668 }
1669 return false;
1670 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001671
1672 private boolean canProjectVideo(IMediaProjection projection) {
1673 if (projection != null) {
1674 try {
1675 if (projection.canProjectVideo()) {
1676 return true;
1677 }
1678 } catch (RemoteException e) {
1679 Slog.e(TAG, "Unable to query projection service for permissions", e);
1680 }
1681 }
1682 if (mContext.checkCallingPermission(
1683 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001684 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001685 return true;
1686 }
1687 return canProjectSecureVideo(projection);
1688 }
1689
1690 private boolean canProjectSecureVideo(IMediaProjection projection) {
1691 if (projection != null) {
1692 try {
1693 if (projection.canProjectSecureVideo()){
1694 return true;
1695 }
1696 } catch (RemoteException e) {
1697 Slog.e(TAG, "Unable to query projection service for permissions", e);
1698 }
1699 }
1700 return mContext.checkCallingPermission(
1701 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001702 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001703 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001704 }
1705
1706 private final class LocalService extends DisplayManagerInternal {
1707 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001708 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001709 SensorManager sensorManager) {
1710 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001711 DisplayBlanker blanker = new DisplayBlanker() {
1712 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001713 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001714 // The order of operations is important for legacy reasons.
1715 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001716 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001717 }
1718
1719 callbacks.onDisplayStateChange(state);
1720
1721 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001722 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001723 }
1724 }
1725 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001726 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001727 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001728 }
1729 }
1730
1731 @Override
1732 public boolean requestPowerState(DisplayPowerRequest request,
1733 boolean waitForNegativeProximity) {
1734 return mDisplayPowerController.requestPowerState(request,
1735 waitForNegativeProximity);
1736 }
1737
1738 @Override
1739 public boolean isProximitySensorAvailable() {
1740 return mDisplayPowerController.isProximitySensorAvailable();
1741 }
1742
1743 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001744 public DisplayInfo getDisplayInfo(int displayId) {
1745 return getDisplayInfoInternal(displayId, Process.myUid());
1746 }
1747
1748 @Override
1749 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1750 if (listener == null) {
1751 throw new IllegalArgumentException("listener must not be null");
1752 }
1753
1754 registerDisplayTransactionListenerInternal(listener);
1755 }
1756
1757 @Override
1758 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1759 if (listener == null) {
1760 throw new IllegalArgumentException("listener must not be null");
1761 }
1762
1763 unregisterDisplayTransactionListenerInternal(listener);
1764 }
1765
1766 @Override
1767 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1768 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1769 }
1770
1771 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001772 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1773 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1774 }
1775
1776 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001777 public void performTraversalInTransactionFromWindowManager() {
1778 performTraversalInTransactionFromWindowManagerInternal();
1779 }
1780
1781 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001782 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001783 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1784 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1785 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001786 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001787
1788 @Override
1789 public void setDisplayOffsets(int displayId, int x, int y) {
1790 setDisplayOffsetsInternal(displayId, x, y);
1791 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001792
1793 @Override
1794 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1795 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1796 }
1797
1798 @Override
1799 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1800 return isUidPresentOnDisplayInternal(uid, displayId);
1801 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001802 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001803}