blob: 9861ea7355704e13fd5c12a6adbd5216bdf06fda [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;
Kenny Guy05ce8092018-01-17 13:44:20 +000032import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000033import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000034import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070035import android.content.Context;
36import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010037import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010038import android.content.res.Resources;
39import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070040import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000041import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010042import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000043import android.hardware.display.BrightnessConfiguration;
Jeff Brownbd6e1502012-08-28 03:27:37 -070044import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080045import android.hardware.display.DisplayManagerInternal;
46import android.hardware.display.DisplayViewport;
47import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070048import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070049import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070050import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070051import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080052import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070053import android.media.projection.IMediaProjection;
54import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070055import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070056import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070057import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080058import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070059import android.os.Looper;
60import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070061import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080062import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070063import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070064import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070065import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070066import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070067import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010068import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000069import android.os.UserManager;
Jeff Browna506a6e2013-06-04 00:02:38 -070070import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080071import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070072import android.util.Slog;
73import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070074import android.view.Display;
75import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070076import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080077import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070078
Michael Wrighteef0e132017-11-21 17:57:52 +000079import com.android.internal.util.Preconditions;
Jorim Jaggied7993b2017-03-28 18:50:01 +010080import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080081import com.android.server.DisplayThread;
82import com.android.server.LocalServices;
83import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070084import com.android.server.UiThread;
Adrian Roose99bc052017-11-20 17:55:31 +010085import com.android.server.wm.WindowManagerInternal;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020086import com.android.server.wm.SurfaceAnimationThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070087
88import java.io.FileDescriptor;
89import java.io.PrintWriter;
90import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070091import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070092import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080093import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070094
95/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070096 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070097 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070098 * The {@link DisplayManagerService} manages the global lifecycle of displays,
99 * decides how to configure logical displays based on the physical display devices currently
100 * attached, sends notifications to the system and to applications when the state
101 * changes, and so on.
102 * </p><p>
103 * The display manager service relies on a collection of {@link DisplayAdapter} components,
104 * for discovering and configuring physical display devices attached to the system.
105 * There are separate display adapters for each manner that devices are attached:
106 * one display adapter for built-in local displays, one for simulated non-functional
107 * displays when the system is headless, one for simulated overlay displays used for
108 * development, one for wifi displays, etc.
109 * </p><p>
110 * Display adapters are only weakly coupled to the display manager service.
111 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700112 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700113 * by the display manager service. This separation of concerns is important for
114 * two main reasons. First, it neatly encapsulates the responsibilities of these
115 * two classes: display adapters handle individual display devices whereas
116 * the display manager service handles the global state. Second, it eliminates
117 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700118 * </p>
119 *
120 * <h3>Synchronization</h3>
121 * <p>
122 * Because the display manager may be accessed by multiple threads, the synchronization
123 * story gets a little complicated. In particular, the window manager may call into
124 * the display manager while holding a surface transaction with the expectation that
125 * it can apply changes immediately. Unfortunately, that means we can't just do
126 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700127 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700128 * To make this work, all of the objects that belong to the display manager must
129 * use the same lock. We call this lock the synchronization root and it has a unique
130 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
131 * named with the "Locked" suffix.
132 * </p><p>
133 * Where things get tricky is that the display manager is not allowed to make
134 * any potentially reentrant calls, especially into the window manager. We generally
135 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700136 * </p>
137 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800138public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700139 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700140 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700141
Jeff Brownbbd28a22012-09-20 16:47:15 -0700142 // When this system property is set to 0, WFD is forcibly disabled on boot.
143 // When this system property is set to 1, WFD is forcibly enabled on boot.
144 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
145 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
146
Jeff Brownbd6e1502012-08-28 03:27:37 -0700147 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
148
Santos Cordonc22c5632017-06-21 16:03:49 -0700149 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700150 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
151 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700152 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700153 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100154 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000155 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700156
Jeff Brownb880d882014-02-10 19:47:07 -0800157 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700158 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700159 private final Handler mUiHandler;
160 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800161 private WindowManagerInternal mWindowManagerInternal;
162 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700163 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700164
165 // The synchronization root for the display manager.
166 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800167 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
168 // into WindowManagerService methods that require mWindowMap while holding this unless you are
169 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700170 private final SyncRoot mSyncRoot = new SyncRoot();
171
172 // True if in safe mode.
173 // This option may disable certain display adapters.
174 public boolean mSafeMode;
175
176 // True if we are in a special boot mode where only core applications and
177 // services should be started. This option may disable certain display adapters.
178 public boolean mOnlyCore;
179
Jeff Brown27f1d672012-10-17 18:32:34 -0700180 // True if the display manager service should pretend there is only one display
181 // and only tell applications about the existence of the default logical display.
182 // The display manager can still mirror content to secondary displays but applications
183 // cannot present unique content on those displays.
184 // Used for demonstration purposes only.
185 private final boolean mSingleDisplayDemoMode;
186
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700187 // All callback records indexed by calling process id.
188 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700189 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700190
Jeff Brownbd6e1502012-08-28 03:27:37 -0700191 // List of all currently registered display adapters.
192 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
193
194 // List of all currently connected display devices.
195 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
196
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700197 // List of all logical displays indexed by logical display id.
198 private final SparseArray<LogicalDisplay> mLogicalDisplays =
199 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700200 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
201
Jeff Brown7f3994e2012-12-04 14:04:28 -0800202 // List of all display transaction listeners.
203 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
204 new CopyOnWriteArrayList<DisplayTransactionListener>();
205
Jeff Brownad9ef192014-04-08 17:26:30 -0700206 // Display power controller.
207 private DisplayPowerController mDisplayPowerController;
208
Jeff Brown037c33e2014-04-09 00:31:55 -0700209 // The overall display state, independent of changes that might influence one
210 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700211 private int mGlobalDisplayState = Display.STATE_ON;
212
213 // The overall display brightness.
214 // For now, this only applies to the built-in display but we may split it up eventually.
215 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700216
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700217 // Set to true when there are pending display changes that have yet to be applied
218 // to the surface flinger state.
219 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700220
Jeff Browne08ae382012-09-07 20:36:36 -0700221 // The Wifi display adapter, or null if not registered.
222 private WifiDisplayAdapter mWifiDisplayAdapter;
223
Jeff Brownce468a32013-11-21 16:42:03 -0800224 // The number of active wifi display scan requests.
225 private int mWifiDisplayScanRequestCount;
226
Jeff Browna506a6e2013-06-04 00:02:38 -0700227 // The virtual display adapter, or null if not registered.
228 private VirtualDisplayAdapter mVirtualDisplayAdapter;
229
Michael Wrighteef0e132017-11-21 17:57:52 +0000230 // The User ID of the current user
231 private @UserIdInt int mCurrentUserId;
232
Michael Wrighteedcbf12017-08-16 23:14:54 +0100233 // The stable device screen height and width. These are not tied to a specific display, even
234 // the default display, because they need to be stable over the course of the device's entire
235 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
236 // device).
237 private Point mStableDisplaySize = new Point();
238
Jeff Brownd728bf52012-09-08 18:05:28 -0700239 // Viewports of the default display and the display that should receive touch
240 // input from an external source. Used by the input system.
241 private final DisplayViewport mDefaultViewport = new DisplayViewport();
242 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700243 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700244
Jeff Brown89d55462012-09-19 11:33:42 -0700245 // Persistent data store for all internal settings maintained by the display manager service.
246 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
247
Jeff Brownbd6e1502012-08-28 03:27:37 -0700248 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700249 // May be used outside of the lock but only on the handler thread.
250 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700251
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700252 // Temporary display info, used for comparing display configurations.
253 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
254
Jeff Brownd728bf52012-09-08 18:05:28 -0700255 // Temporary viewports, used when sending new viewport information to the
256 // input system. May be used outside of the lock but only on the handler thread.
257 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
258 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700259 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700260
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700261 // The default color mode for default displays. Overrides the usual
262 // Display.Display.COLOR_MODE_DEFAULT for displays with the
263 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
264 private final int mDefaultDisplayDefaultColorMode;
265
Jeff Browne75926d2014-09-18 15:24:49 -0700266 // Temporary list of deferred work to perform when setting the display state.
267 // Only used by requestDisplayState. The field is self-synchronized and only
268 // intended for use inside of the requestGlobalDisplayStateInternal function.
269 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
270
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800271 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
272 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
273
Santos Cordonee8931e2017-04-05 10:31:15 -0700274 private final Injector mInjector;
275
Jeff Brownb880d882014-02-10 19:47:07 -0800276 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700277 this(context, new Injector());
278 }
279
280 @VisibleForTesting
281 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800282 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700283 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800284 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800285 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700286 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700287 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700288 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700289 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000290 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700291
292 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
293 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000294 mCurrentUserId = UserHandle.USER_SYSTEM;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700295 }
296
297 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100298 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000299 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
300 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100301 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
302 Process.THREAD_GROUP_TOP_APP);
303 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
304 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200305 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
306 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700307 }
308
Jeff Brown4ccb8232014-01-16 22:16:42 -0800309 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800310 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700311 // We need to pre-load the persistent data store so it's ready before the default display
312 // adapter is up so that we have it's configuration. We could load it lazily, but since
313 // 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 -0700314 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100315 synchronized(mSyncRoot) {
316 mPersistentDataStore.loadIfNeeded();
317 loadStableDisplayValuesLocked();
318 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700319 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800320
321 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
322 true /*allowIsolated*/);
323 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700324 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800325 }
326
327 @Override
328 public void onBootPhase(int phase) {
329 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
330 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700331 long timeout = SystemClock.uptimeMillis()
332 + mInjector.getDefaultDisplayDelayTimeout();
333 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
334 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800335 long delay = timeout - SystemClock.uptimeMillis();
336 if (delay <= 0) {
337 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700338 + "to be initialized. DefaultDisplay="
339 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
340 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800341 }
342 if (DEBUG) {
343 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
344 }
345 try {
346 mSyncRoot.wait(delay);
347 } catch (InterruptedException ex) {
348 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700349 }
350 }
351 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700352 }
353
Michael Wrighteef0e132017-11-21 17:57:52 +0000354 @Override
355 public void onSwitchUser(@UserIdInt int newUserId) {
356 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
357 synchronized (mSyncRoot) {
358 if (mCurrentUserId != newUserId) {
359 mCurrentUserId = newUserId;
360 BrightnessConfiguration config =
361 mPersistentDataStore.getBrightnessConfiguration(userSerial);
362 mDisplayPowerController.setBrightnessConfiguration(config);
363 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000364 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000365 }
366 }
367
Jeff Brown4ccb8232014-01-16 22:16:42 -0800368 // TODO: Use dependencies or a boot phase
369 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700370 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800371 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
372 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700373 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700374 }
375 }
376
377 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700378 * Called when the system is ready to go.
379 */
380 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700381 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700382 mSafeMode = safeMode;
383 mOnlyCore = onlyCore;
384 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700385
Jeff Brownbd6e1502012-08-28 03:27:37 -0700386 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100387 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700388 }
389
Santos Cordonee8931e2017-04-05 10:31:15 -0700390 @VisibleForTesting
391 Handler getDisplayHandler() {
392 return mHandler;
393 }
394
Michael Wrighteedcbf12017-08-16 23:14:54 +0100395 private void loadStableDisplayValuesLocked() {
396 final Point size = mPersistentDataStore.getStableDisplaySize();
397 if (size.x > 0 && size.y > 0) {
398 // Just set these values directly so we don't write the display persistent data again
399 // unnecessarily
400 mStableDisplaySize.set(size.x, size.y);
401 } else {
402 final Resources res = mContext.getResources();
403 final int width = res.getInteger(
404 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
405 final int height = res.getInteger(
406 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
407 if (width > 0 && height > 0) {
408 setStableDisplaySizeLocked(width, height);
409 }
410 }
411 }
412
413 private Point getStableDisplaySizeInternal() {
414 Point r = new Point();
415 synchronized (mSyncRoot) {
416 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
417 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
418 }
419 }
420 return r;
421 }
422
Jeff Brown4ccb8232014-01-16 22:16:42 -0800423 private void registerDisplayTransactionListenerInternal(
424 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800425 // List is self-synchronized copy-on-write.
426 mDisplayTransactionListeners.add(listener);
427 }
428
Jeff Brown4ccb8232014-01-16 22:16:42 -0800429 private void unregisterDisplayTransactionListenerInternal(
430 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800431 // List is self-synchronized copy-on-write.
432 mDisplayTransactionListeners.remove(listener);
433 }
434
Jeff Brown4ccb8232014-01-16 22:16:42 -0800435 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700436 int displayId, DisplayInfo info) {
437 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700438 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700439 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700440 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700441 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700442 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700443 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700444 }
445 }
446 }
447
Andrii Kuliancd097992017-03-23 18:31:59 -0700448 /**
449 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
450 */
451 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
452 synchronized (mSyncRoot) {
453 final LogicalDisplay display = mLogicalDisplays.get(displayId);
454 if (display != null) {
455 display.getNonOverrideDisplayInfoLocked(outInfo);
456 }
457 }
458 }
459
Santos Cordonee8931e2017-04-05 10:31:15 -0700460 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800461 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700462 synchronized (mSyncRoot) {
463 if (!mPendingTraversal) {
464 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700465 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700466 mPendingTraversal = false;
467
Robert Carrae606b42018-02-15 15:36:23 -0800468 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700469 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800470
471 // List is self-synchronized copy-on-write.
472 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
473 listener.onDisplayTransaction();
474 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700475 }
476
Jeff Brown5d6443b2015-04-10 20:15:01 -0700477 private void requestGlobalDisplayStateInternal(int state, int brightness) {
478 if (state == Display.STATE_UNKNOWN) {
479 state = Display.STATE_ON;
480 }
481 if (state == Display.STATE_OFF) {
482 brightness = PowerManager.BRIGHTNESS_OFF;
483 } else if (brightness < 0) {
484 brightness = PowerManager.BRIGHTNESS_DEFAULT;
485 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
486 brightness = PowerManager.BRIGHTNESS_ON;
487 }
488
Jeff Browne75926d2014-09-18 15:24:49 -0700489 synchronized (mTempDisplayStateWorkQueue) {
490 try {
491 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700492 // Note that we do not need to schedule traversals here although it
493 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700494 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700495 if (mGlobalDisplayState == state
496 && mGlobalDisplayBrightness == brightness) {
497 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700498 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700499
500 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
501 + Display.stateToString(state)
502 + ", brightness=" + brightness + ")");
503 mGlobalDisplayState = state;
504 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700505 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700506 }
507
508 // Setting the display power state can take hundreds of milliseconds
509 // to complete so we defer the most expensive part of the work until
510 // after we have exited the critical section to avoid blocking other
511 // threads for a long time.
512 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
513 mTempDisplayStateWorkQueue.get(i).run();
514 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700515 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700516 } finally {
517 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700518 }
519 }
520 }
521
Jeff Brown4ccb8232014-01-16 22:16:42 -0800522 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700523 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800524 LogicalDisplay display = mLogicalDisplays.get(displayId);
525 if (display != null) {
526 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800527 if (info.hasAccess(callingUid)
528 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800529 return info;
530 }
531 }
532 return null;
533 }
534 }
535
536 private int[] getDisplayIdsInternal(int callingUid) {
537 synchronized (mSyncRoot) {
538 final int count = mLogicalDisplays.size();
539 int[] displayIds = new int[count];
540 int n = 0;
541 for (int i = 0; i < count; i++) {
542 LogicalDisplay display = mLogicalDisplays.valueAt(i);
543 DisplayInfo info = display.getDisplayInfoLocked();
544 if (info.hasAccess(callingUid)) {
545 displayIds[n++] = mLogicalDisplays.keyAt(i);
546 }
547 }
548 if (n != count) {
549 displayIds = Arrays.copyOfRange(displayIds, 0, n);
550 }
551 return displayIds;
552 }
553 }
554
555 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
556 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700557 if (mCallbacks.get(callingPid) != null) {
558 throw new SecurityException("The calling process has already "
559 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700560 }
561
Jeff Brownbd6e1502012-08-28 03:27:37 -0700562 CallbackRecord record = new CallbackRecord(callingPid, callback);
563 try {
564 IBinder binder = callback.asBinder();
565 binder.linkToDeath(record, 0);
566 } catch (RemoteException ex) {
567 // give up
568 throw new RuntimeException(ex);
569 }
570
571 mCallbacks.put(callingPid, record);
572 }
573 }
574
Jeff Brownce468a32013-11-21 16:42:03 -0800575 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700576 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800577 mCallbacks.remove(record.mPid);
578 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700579 }
580 }
581
Jeff Brown4ccb8232014-01-16 22:16:42 -0800582 private void startWifiDisplayScanInternal(int callingPid) {
583 synchronized (mSyncRoot) {
584 CallbackRecord record = mCallbacks.get(callingPid);
585 if (record == null) {
586 throw new IllegalStateException("The calling process has not "
587 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700588 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800589 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700590 }
591 }
592
Jeff Brownce468a32013-11-21 16:42:03 -0800593 private void startWifiDisplayScanLocked(CallbackRecord record) {
594 if (!record.mWifiDisplayScanRequested) {
595 record.mWifiDisplayScanRequested = true;
596 if (mWifiDisplayScanRequestCount++ == 0) {
597 if (mWifiDisplayAdapter != null) {
598 mWifiDisplayAdapter.requestStartScanLocked();
599 }
600 }
601 }
602 }
603
Jeff Brown4ccb8232014-01-16 22:16:42 -0800604 private void stopWifiDisplayScanInternal(int callingPid) {
605 synchronized (mSyncRoot) {
606 CallbackRecord record = mCallbacks.get(callingPid);
607 if (record == null) {
608 throw new IllegalStateException("The calling process has not "
609 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800610 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800611 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800612 }
613 }
614
615 private void stopWifiDisplayScanLocked(CallbackRecord record) {
616 if (record.mWifiDisplayScanRequested) {
617 record.mWifiDisplayScanRequested = false;
618 if (--mWifiDisplayScanRequestCount == 0) {
619 if (mWifiDisplayAdapter != null) {
620 mWifiDisplayAdapter.requestStopScanLocked();
621 }
622 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700623 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800624 + mWifiDisplayScanRequestCount);
625 mWifiDisplayScanRequestCount = 0;
626 }
627 }
628 }
629
Jeff Brown4ccb8232014-01-16 22:16:42 -0800630 private void connectWifiDisplayInternal(String address) {
631 synchronized (mSyncRoot) {
632 if (mWifiDisplayAdapter != null) {
633 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700634 }
Jeff Browne08ae382012-09-07 20:36:36 -0700635 }
636 }
637
Jeff Brown4ccb8232014-01-16 22:16:42 -0800638 private void pauseWifiDisplayInternal() {
639 synchronized (mSyncRoot) {
640 if (mWifiDisplayAdapter != null) {
641 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700642 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700643 }
644 }
645
Jeff Brown4ccb8232014-01-16 22:16:42 -0800646 private void resumeWifiDisplayInternal() {
647 synchronized (mSyncRoot) {
648 if (mWifiDisplayAdapter != null) {
649 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700650 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700651 }
652 }
653
Jeff Brown4ccb8232014-01-16 22:16:42 -0800654 private void disconnectWifiDisplayInternal() {
655 synchronized (mSyncRoot) {
656 if (mWifiDisplayAdapter != null) {
657 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700658 }
Jeff Browne08ae382012-09-07 20:36:36 -0700659 }
660 }
661
Jeff Brown4ccb8232014-01-16 22:16:42 -0800662 private void renameWifiDisplayInternal(String address, String alias) {
663 synchronized (mSyncRoot) {
664 if (mWifiDisplayAdapter != null) {
665 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700666 }
Jeff Brown89d55462012-09-19 11:33:42 -0700667 }
668 }
669
Jeff Brown4ccb8232014-01-16 22:16:42 -0800670 private void forgetWifiDisplayInternal(String address) {
671 synchronized (mSyncRoot) {
672 if (mWifiDisplayAdapter != null) {
673 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700674 }
Jeff Brown89d55462012-09-19 11:33:42 -0700675 }
676 }
677
Jeff Brown4ccb8232014-01-16 22:16:42 -0800678 private WifiDisplayStatus getWifiDisplayStatusInternal() {
679 synchronized (mSyncRoot) {
680 if (mWifiDisplayAdapter != null) {
681 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700682 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800683 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700684 }
685 }
686
Michael Wright1c9977b2016-07-12 13:30:10 -0700687 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100688 synchronized (mSyncRoot) {
689 LogicalDisplay display = mLogicalDisplays.get(displayId);
690 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700691 display.getRequestedColorModeLocked() != colorMode) {
692 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100693 scheduleTraversalLocked(false);
694 }
695 }
696 }
697
Michael Wright75ee9fc2014-09-01 19:55:22 -0700698 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700699 IMediaProjection projection, int callingUid, String packageName, String name, int width,
700 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800701 synchronized (mSyncRoot) {
702 if (mVirtualDisplayAdapter == null) {
703 Slog.w(TAG, "Rejecting request to create private virtual display "
704 + "because the virtual display adapter is not available.");
705 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700706 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707
708 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700709 callback, projection, callingUid, packageName, name, width, height, densityDpi,
710 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800711 if (device == null) {
712 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700713 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700714
Jeff Brown4ccb8232014-01-16 22:16:42 -0800715 handleDisplayDeviceAddedLocked(device);
716 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
717 if (display != null) {
718 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700719 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800720
721 // Something weird happened and the logical display was not created.
722 Slog.w(TAG, "Rejecting request to create virtual display "
723 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700724 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800725 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700726 }
727 return -1;
728 }
729
Michael Wright01e840f2014-06-26 16:03:25 -0700730 private void resizeVirtualDisplayInternal(IBinder appToken,
731 int width, int height, int densityDpi) {
732 synchronized (mSyncRoot) {
733 if (mVirtualDisplayAdapter == null) {
734 return;
735 }
736
737 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
738 }
739 }
740
Jeff Brown92207df2014-04-16 13:16:07 -0700741 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
742 synchronized (mSyncRoot) {
743 if (mVirtualDisplayAdapter == null) {
744 return;
745 }
746
747 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
748 }
749 }
750
Jeff Brown4ccb8232014-01-16 22:16:42 -0800751 private void releaseVirtualDisplayInternal(IBinder appToken) {
752 synchronized (mSyncRoot) {
753 if (mVirtualDisplayAdapter == null) {
754 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700755 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700756
Jeff Brown4ccb8232014-01-16 22:16:42 -0800757 DisplayDevice device =
758 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
759 if (device != null) {
760 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700761 }
762 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700763 }
764
Santos Cordonc22c5632017-06-21 16:03:49 -0700765 private void registerDefaultDisplayAdapters() {
766 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700767 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700768 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800769 registerDisplayAdapterLocked(new LocalDisplayAdapter(
770 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700771
772 // Standalone VR devices rely on a virtual display as their primary display for
773 // 2D UI. We register virtual display adapter along side the main display adapter
774 // here so that it is ready by the time the system sends the home Intent for
775 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
776 // the virtual display inside VR before any VR-specific apps even run.
777 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
778 mHandler, mDisplayAdapterListener);
779 if (mVirtualDisplayAdapter != null) {
780 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
781 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700782 }
783 }
784
785 private void registerAdditionalDisplayAdapters() {
786 synchronized (mSyncRoot) {
787 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700788 registerOverlayDisplayAdapterLocked();
789 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700790 }
791 }
792 }
793
Jeff Brown89d55462012-09-19 11:33:42 -0700794 private void registerOverlayDisplayAdapterLocked() {
795 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
796 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
797 }
798
799 private void registerWifiDisplayAdapterLocked() {
800 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700801 com.android.internal.R.bool.config_enableWifiDisplay)
802 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700803 mWifiDisplayAdapter = new WifiDisplayAdapter(
804 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
805 mPersistentDataStore);
806 registerDisplayAdapterLocked(mWifiDisplayAdapter);
807 }
808 }
809
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700810 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
811 // In safe mode, we disable non-essential display adapters to give the user
812 // an opportunity to fix broken settings or other problems that might affect
813 // system stability.
814 // In only-core mode, we disable non-essential display adapters to minimize
815 // the number of dependencies that are started while in this mode and to
816 // prevent problems that might occur due to the device being encrypted.
817 return !mSafeMode && !mOnlyCore;
818 }
819
820 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
821 mDisplayAdapters.add(adapter);
822 adapter.registerLocked();
823 }
824
Jeff Brownbd6e1502012-08-28 03:27:37 -0700825 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700826 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700827 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700828 }
829 }
830
Jeff Browna506a6e2013-06-04 00:02:38 -0700831 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700832 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700833 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700834 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700835 return;
836 }
837
Jeff Brown10acf6d2015-04-14 14:20:47 -0700838 Slog.i(TAG, "Display device added: " + info);
839 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700840
841 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700842 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700843 Runnable work = updateDisplayStateLocked(device);
844 if (work != null) {
845 work.run();
846 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700847 scheduleTraversalLocked(false);
848 }
849
Jeff Brownbd6e1502012-08-28 03:27:37 -0700850 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700851 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700852 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700853 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700854 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700855 return;
856 }
857
Jeff Brown10acf6d2015-04-14 14:20:47 -0700858 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
859 if (diff == DisplayDeviceInfo.DIFF_STATE) {
860 Slog.i(TAG, "Display device changed state: \"" + info.name
861 + "\", " + Display.stateToString(info.state));
862 } else if (diff != 0) {
863 Slog.i(TAG, "Display device changed: " + info);
864 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700865 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
866 try {
867 mPersistentDataStore.setColorMode(device, info.colorMode);
868 } finally {
869 mPersistentDataStore.saveIfNeeded();
870 }
871 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700872 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700873
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700874 device.applyPendingDisplayDeviceInfoChangesLocked();
875 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700876 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700877 }
878 }
879 }
880
Jeff Brownbd6e1502012-08-28 03:27:37 -0700881 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700882 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700883 handleDisplayDeviceRemovedLocked(device);
884 }
885 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700886
Jeff Browna506a6e2013-06-04 00:02:38 -0700887 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700888 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700889 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700890 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700891 return;
892 }
893
Jeff Brown10acf6d2015-04-14 14:20:47 -0700894 Slog.i(TAG, "Display device removed: " + info);
895 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700896
Jeff Browna506a6e2013-06-04 00:02:38 -0700897 updateLogicalDisplaysLocked();
898 scheduleTraversalLocked(false);
899 }
900
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700901 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700902 final int count = mDisplayDevices.size();
903 for (int i = 0; i < count; i++) {
904 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700905 Runnable runnable = updateDisplayStateLocked(device);
906 if (runnable != null) {
907 workQueue.add(runnable);
908 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700909 }
910 }
911
Jeff Browne75926d2014-09-18 15:24:49 -0700912 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700913 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700914 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700915 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
916 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700917 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700918 }
Jeff Browne75926d2014-09-18 15:24:49 -0700919 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700920 }
921
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700922 // Adds a new logical display based on the given display device.
923 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700924 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700925 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
926 boolean isDefault = (deviceInfo.flags
927 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
928 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
929 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
930 isDefault = false;
931 }
932
Jeff Brown27f1d672012-10-17 18:32:34 -0700933 if (!isDefault && mSingleDisplayDemoMode) {
934 Slog.i(TAG, "Not creating a logical display for a secondary display "
935 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700936 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700937 }
938
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700939 final int displayId = assignDisplayIdLocked(isDefault);
940 final int layerStack = assignLayerStackLocked(displayId);
941
Jeff Brownd728bf52012-09-08 18:05:28 -0700942 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700943 display.updateLocked(mDisplayDevices);
944 if (!display.isValidLocked()) {
945 // This should never happen currently.
946 Slog.w(TAG, "Ignoring display device because the logical display "
947 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700948 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700949 }
950
Michael Wrighteedcbf12017-08-16 23:14:54 +0100951 configureColorModeLocked(display, device);
952 if (isDefault) {
953 recordStableDisplayStatsIfNeededLocked(display);
954 }
955
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700956 mLogicalDisplays.put(displayId, display);
957
958 // Wake up waitForDefaultDisplay.
959 if (isDefault) {
960 mSyncRoot.notifyAll();
961 }
962
963 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700964 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700965 }
966
967 private int assignDisplayIdLocked(boolean isDefault) {
968 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
969 }
970
971 private int assignLayerStackLocked(int displayId) {
972 // Currently layer stacks and display ids are the same.
973 // This need not be the case.
974 return displayId;
975 }
976
Michael Wrighteedcbf12017-08-16 23:14:54 +0100977 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
978 if (display.getPrimaryDisplayDeviceLocked() == device) {
979 int colorMode = mPersistentDataStore.getColorMode(device);
980 if (colorMode == Display.COLOR_MODE_INVALID) {
981 if ((device.getDisplayDeviceInfoLocked().flags
982 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
983 colorMode = mDefaultDisplayDefaultColorMode;
984 } else {
985 colorMode = Display.COLOR_MODE_DEFAULT;
986 }
987 }
988 display.setRequestedColorModeLocked(colorMode);
989 }
990 }
991
992 // If we've never recorded stable device stats for this device before and they aren't
993 // explicitly configured, go ahead and record the stable device stats now based on the status
994 // of the default display at first boot.
995 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
996 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
997 DisplayInfo info = d.getDisplayInfoLocked();
998 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
999 }
1000 }
1001
1002 private void setStableDisplaySizeLocked(int width, int height) {
1003 mStableDisplaySize = new Point(width, height);
1004 try {
1005 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1006 } finally {
1007 mPersistentDataStore.saveIfNeeded();
1008 }
1009 }
1010
Michael Wrighteef0e132017-11-21 17:57:52 +00001011 private void setBrightnessConfigurationForUserInternal(
Kenny Guy05ce8092018-01-17 13:44:20 +00001012 @NonNull BrightnessConfiguration c, @UserIdInt int userId,
1013 @Nullable String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001014 final int userSerial = getUserManager().getUserSerialNumber(userId);
1015 synchronized (mSyncRoot) {
1016 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001017 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1018 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001019 } finally {
1020 mPersistentDataStore.saveIfNeeded();
1021 }
1022 if (userId == mCurrentUserId) {
1023 mDisplayPowerController.setBrightnessConfiguration(c);
1024 }
1025 }
1026 }
1027
1028 private void loadBrightnessConfiguration() {
1029 synchronized (mSyncRoot) {
1030 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1031 BrightnessConfiguration config =
1032 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1033 mDisplayPowerController.setBrightnessConfiguration(config);
1034 }
1035 }
1036
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001037 // Updates all existing logical displays given the current set of display devices.
1038 // Removes invalid logical displays.
1039 // Sends notifications if needed.
1040 private boolean updateLogicalDisplaysLocked() {
1041 boolean changed = false;
1042 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1043 final int displayId = mLogicalDisplays.keyAt(i);
1044 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1045
1046 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1047 display.updateLocked(mDisplayDevices);
1048 if (!display.isValidLocked()) {
1049 mLogicalDisplays.removeAt(i);
1050 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1051 changed = true;
1052 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1053 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1054 changed = true;
1055 }
1056 }
1057 return changed;
1058 }
1059
Robert Carrae606b42018-02-15 15:36:23 -08001060 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001061 // Clear all viewports before configuring displays so that we can keep
1062 // track of which ones we have configured.
1063 clearViewportsLocked();
1064
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001065 // Configure each display device.
1066 final int count = mDisplayDevices.size();
1067 for (int i = 0; i < count; i++) {
1068 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001069 configureDisplayLocked(t, device);
1070 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001071 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001072
1073 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001074 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001075 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1076 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001077 }
1078
Michael Wright3f145a22014-07-22 19:46:03 -07001079 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001080 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001081 synchronized (mSyncRoot) {
1082 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001083 if (display == null) {
1084 return;
1085 }
1086 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001087 if (DEBUG) {
1088 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1089 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1090 }
1091
Craig Mautner722285e2012-09-07 13:55:58 -07001092 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001093 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001094 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001095 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1096 // Scan supported modes returned by display.getInfo() to find a mode with the same
1097 // size as the default display mode but with the specified refresh rate instead.
1098 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1099 requestedRefreshRate);
1100 }
1101 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001102 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001103 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001104 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001105 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001106 scheduleTraversalLocked(inTraversal);
1107 }
Craig Mautner722285e2012-09-07 13:55:58 -07001108 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001109 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001110
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001111 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1112 synchronized (mSyncRoot) {
1113 LogicalDisplay display = mLogicalDisplays.get(displayId);
1114 if (display == null) {
1115 return;
1116 }
1117 if (display.getDisplayOffsetXLocked() != x
1118 || display.getDisplayOffsetYLocked() != y) {
1119 if (DEBUG) {
1120 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1121 + x + ", " + y + ")");
1122 }
1123 display.setDisplayOffsetsLocked(x, y);
1124 scheduleTraversalLocked(false);
1125 }
1126 }
1127 }
1128
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001129 // Updates the lists of UIDs that are present on displays.
1130 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1131 synchronized (mSyncRoot) {
1132 mDisplayAccessUIDs.clear();
1133 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1134 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1135 newDisplayAccessUIDs.valueAt(i));
1136 }
1137 }
1138 }
1139
1140 // Checks if provided UID's content is present on the display and UID has access to it.
1141 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1142 synchronized (mSyncRoot) {
1143 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1144 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1145 }
1146 }
1147
Jeff Brownd728bf52012-09-08 18:05:28 -07001148 private void clearViewportsLocked() {
1149 mDefaultViewport.valid = false;
1150 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001151 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001152 }
1153
Robert Carrae606b42018-02-15 15:36:23 -08001154 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001155 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1156 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001157
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001158 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001159 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001160 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001161 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001162 if (display != null && !display.hasContentLocked()) {
1163 // If the display does not have any content of its own, then
1164 // automatically mirror the default logical display contents.
1165 display = null;
1166 }
1167 if (display == null) {
1168 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1169 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001170 }
1171
1172 // Apply the logical display configuration to the display device.
1173 if (display == null) {
1174 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001175 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001176 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001177 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001178 }
Robert Carrae606b42018-02-15 15:36:23 -08001179 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001180
1181 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001182 if (!mDefaultViewport.valid
1183 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1184 setViewportLocked(mDefaultViewport, display, device);
1185 }
1186 if (!mExternalTouchViewport.valid
1187 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1188 setViewportLocked(mExternalTouchViewport, display, device);
1189 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001190
1191 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1192 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1193 setViewportLocked(viewport, display, device);
1194 }
1195 }
1196
1197 /** Gets the virtual device viewport or creates it if not yet created. */
1198 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1199 DisplayViewport viewport;
1200 final int count = mVirtualTouchViewports.size();
1201 for (int i = 0; i < count; i++) {
1202 viewport = mVirtualTouchViewports.get(i);
1203 if (uniqueId.equals(viewport.uniqueId)) {
1204 return viewport;
1205 }
1206 }
1207
1208 viewport = new DisplayViewport();
1209 viewport.uniqueId = uniqueId;
1210 mVirtualTouchViewports.add(viewport);
1211 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001212 }
1213
1214 private static void setViewportLocked(DisplayViewport viewport,
1215 LogicalDisplay display, DisplayDevice device) {
1216 viewport.valid = true;
1217 viewport.displayId = display.getDisplayIdLocked();
1218 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001219 }
1220
1221 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1222 final int count = mLogicalDisplays.size();
1223 for (int i = 0; i < count; i++) {
1224 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1225 if (display.getPrimaryDisplayDeviceLocked() == device) {
1226 return display;
1227 }
1228 }
1229 return null;
1230 }
1231
Jeff Brownbd6e1502012-08-28 03:27:37 -07001232 private void sendDisplayEventLocked(int displayId, int event) {
1233 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1234 mHandler.sendMessage(msg);
1235 }
1236
Robert Carrae606b42018-02-15 15:36:23 -08001237 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001238 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001239 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001240 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001241 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001242 if (!inTraversal) {
1243 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1244 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001245 }
1246 }
1247
1248 // Runs on Handler thread.
1249 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001250 private void deliverDisplayEvent(int displayId, int event) {
1251 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001252 Slog.d(TAG, "Delivering display event: displayId="
1253 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001254 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001255
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001256 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001257 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001258 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001259 count = mCallbacks.size();
1260 mTempCallbacks.clear();
1261 for (int i = 0; i < count; i++) {
1262 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001263 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001264 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001265
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001266 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001267 for (int i = 0; i < count; i++) {
1268 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1269 }
1270 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001271 }
1272
Michael Wrightc39d47a2014-07-08 18:07:36 -07001273 private IMediaProjectionManager getProjectionService() {
1274 if (mProjectionService == null) {
1275 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1276 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1277 }
1278 return mProjectionService;
1279 }
1280
Michael Wrighteef0e132017-11-21 17:57:52 +00001281 private UserManager getUserManager() {
1282 return mContext.getSystemService(UserManager.class);
1283 }
1284
Jeff Brown4ccb8232014-01-16 22:16:42 -08001285 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001286 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001287
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001288 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001289 pw.println(" mOnlyCode=" + mOnlyCore);
1290 pw.println(" mSafeMode=" + mSafeMode);
1291 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001292 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001293 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1294 pw.println(" mDefaultViewport=" + mDefaultViewport);
1295 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001296 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001297 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001298 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001299 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001300 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1301
Jeff Brown9e316a12012-10-08 19:17:06 -07001302
Jeff Brownbd6e1502012-08-28 03:27:37 -07001303 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001304 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001305
1306 pw.println();
1307 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001308 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001309 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001310 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001311 }
Craig Mautner9de49362012-08-02 14:30:30 -07001312
Jeff Brownbd6e1502012-08-28 03:27:37 -07001313 pw.println();
1314 pw.println("Display Devices: size=" + mDisplayDevices.size());
1315 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001316 pw.println(" " + device.getDisplayDeviceInfoLocked());
1317 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001318 }
1319
1320 final int logicalDisplayCount = mLogicalDisplays.size();
1321 pw.println();
1322 pw.println("Logical Displays: size=" + logicalDisplayCount);
1323 for (int i = 0; i < logicalDisplayCount; i++) {
1324 int displayId = mLogicalDisplays.keyAt(i);
1325 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1326 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001327 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001328 }
Jeff Brownce468a32013-11-21 16:42:03 -08001329
1330 final int callbackCount = mCallbacks.size();
1331 pw.println();
1332 pw.println("Callbacks: size=" + callbackCount);
1333 for (int i = 0; i < callbackCount; i++) {
1334 CallbackRecord callback = mCallbacks.valueAt(i);
1335 pw.println(" " + i + ": mPid=" + callback.mPid
1336 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1337 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001338
1339 if (mDisplayPowerController != null) {
1340 mDisplayPowerController.dump(pw);
1341 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001342
1343 pw.println();
1344 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001345 }
1346 }
1347
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001348 /**
1349 * This is the object that everything in the display manager locks on.
1350 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1351 * clear that the object belongs to the display manager service and that it is
1352 * a unique object with a special purpose.
1353 */
1354 public static final class SyncRoot {
1355 }
1356
Santos Cordonee8931e2017-04-05 10:31:15 -07001357 @VisibleForTesting
1358 static class Injector {
1359 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1360 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1361 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1362 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001363
1364 long getDefaultDisplayDelayTimeout() {
1365 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1366 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001367 }
1368
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001369 @VisibleForTesting
1370 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1371 synchronized (mSyncRoot) {
1372 LogicalDisplay display = mLogicalDisplays.get(displayId);
1373 if (display != null) {
1374 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1375 return displayDevice.getDisplayDeviceInfoLocked();
1376 }
1377 return null;
1378 }
1379 }
1380
Jeff Brownbd6e1502012-08-28 03:27:37 -07001381 private final class DisplayManagerHandler extends Handler {
1382 public DisplayManagerHandler(Looper looper) {
1383 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001384 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001385
Jeff Brownbd6e1502012-08-28 03:27:37 -07001386 @Override
1387 public void handleMessage(Message msg) {
1388 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001389 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1390 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001391 break;
1392
1393 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1394 registerAdditionalDisplayAdapters();
1395 break;
1396
1397 case MSG_DELIVER_DISPLAY_EVENT:
1398 deliverDisplayEvent(msg.arg1, msg.arg2);
1399 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001400
1401 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001402 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001403 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001404
1405 case MSG_UPDATE_VIEWPORT: {
1406 synchronized (mSyncRoot) {
1407 mTempDefaultViewport.copyFrom(mDefaultViewport);
1408 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001409 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1410 mTempVirtualTouchViewports.clear();
1411 for (DisplayViewport d : mVirtualTouchViewports) {
1412 mTempVirtualTouchViewports.add(d.makeCopy());
1413 }
1414 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001415 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001416 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1417 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001418 break;
1419 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001420
Michael Wrighteef0e132017-11-21 17:57:52 +00001421 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1422 loadBrightnessConfiguration();
1423 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001424 }
1425 }
1426 }
1427
1428 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1429 @Override
1430 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1431 switch (event) {
1432 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1433 handleDisplayDeviceAdded(device);
1434 break;
1435
1436 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1437 handleDisplayDeviceChanged(device);
1438 break;
1439
1440 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1441 handleDisplayDeviceRemoved(device);
1442 break;
1443 }
1444 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001445
1446 @Override
1447 public void onTraversalRequested() {
1448 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001449 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001450 }
1451 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001452 }
1453
1454 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001455 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001456 private final IDisplayManagerCallback mCallback;
1457
Jeff Brownce468a32013-11-21 16:42:03 -08001458 public boolean mWifiDisplayScanRequested;
1459
Jeff Brownbd6e1502012-08-28 03:27:37 -07001460 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1461 mPid = pid;
1462 mCallback = callback;
1463 }
1464
1465 @Override
1466 public void binderDied() {
1467 if (DEBUG) {
1468 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1469 }
Jeff Brownce468a32013-11-21 16:42:03 -08001470 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001471 }
1472
1473 public void notifyDisplayEventAsync(int displayId, int event) {
1474 try {
1475 mCallback.onDisplayEvent(displayId, event);
1476 } catch (RemoteException ex) {
1477 Slog.w(TAG, "Failed to notify process "
1478 + mPid + " that displays changed, assuming it died.", ex);
1479 binderDied();
1480 }
1481 }
1482 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001483
Santos Cordonee8931e2017-04-05 10:31:15 -07001484 @VisibleForTesting
1485 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001486 /**
1487 * Returns information about the specified logical display.
1488 *
1489 * @param displayId The logical display id.
1490 * @return The logical display info, or null if the display does not exist. The
1491 * returned object must be treated as immutable.
1492 */
1493 @Override // Binder call
1494 public DisplayInfo getDisplayInfo(int displayId) {
1495 final int callingUid = Binder.getCallingUid();
1496 final long token = Binder.clearCallingIdentity();
1497 try {
1498 return getDisplayInfoInternal(displayId, callingUid);
1499 } finally {
1500 Binder.restoreCallingIdentity(token);
1501 }
1502 }
1503
1504 /**
1505 * Returns the list of all display ids.
1506 */
1507 @Override // Binder call
1508 public int[] getDisplayIds() {
1509 final int callingUid = Binder.getCallingUid();
1510 final long token = Binder.clearCallingIdentity();
1511 try {
1512 return getDisplayIdsInternal(callingUid);
1513 } finally {
1514 Binder.restoreCallingIdentity(token);
1515 }
1516 }
1517
Michael Wrighteedcbf12017-08-16 23:14:54 +01001518 /**
1519 * Returns the stable device display size, in pixels.
1520 */
1521 @Override // Binder call
1522 public Point getStableDisplaySize() {
1523 final long token = Binder.clearCallingIdentity();
1524 try {
1525 return getStableDisplaySizeInternal();
1526 } finally {
1527 Binder.restoreCallingIdentity(token);
1528 }
1529 }
1530
Jeff Brown4ccb8232014-01-16 22:16:42 -08001531 @Override // Binder call
1532 public void registerCallback(IDisplayManagerCallback callback) {
1533 if (callback == null) {
1534 throw new IllegalArgumentException("listener must not be null");
1535 }
1536
1537 final int callingPid = Binder.getCallingPid();
1538 final long token = Binder.clearCallingIdentity();
1539 try {
1540 registerCallbackInternal(callback, callingPid);
1541 } finally {
1542 Binder.restoreCallingIdentity(token);
1543 }
1544 }
1545
1546 @Override // Binder call
1547 public void startWifiDisplayScan() {
1548 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1549 "Permission required to start wifi display scans");
1550
1551 final int callingPid = Binder.getCallingPid();
1552 final long token = Binder.clearCallingIdentity();
1553 try {
1554 startWifiDisplayScanInternal(callingPid);
1555 } finally {
1556 Binder.restoreCallingIdentity(token);
1557 }
1558 }
1559
1560 @Override // Binder call
1561 public void stopWifiDisplayScan() {
1562 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1563 "Permission required to stop wifi display scans");
1564
1565 final int callingPid = Binder.getCallingPid();
1566 final long token = Binder.clearCallingIdentity();
1567 try {
1568 stopWifiDisplayScanInternal(callingPid);
1569 } finally {
1570 Binder.restoreCallingIdentity(token);
1571 }
1572 }
1573
1574 @Override // Binder call
1575 public void connectWifiDisplay(String address) {
1576 if (address == null) {
1577 throw new IllegalArgumentException("address must not be null");
1578 }
1579 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1580 "Permission required to connect to a wifi display");
1581
1582 final long token = Binder.clearCallingIdentity();
1583 try {
1584 connectWifiDisplayInternal(address);
1585 } finally {
1586 Binder.restoreCallingIdentity(token);
1587 }
1588 }
1589
1590 @Override // Binder call
1591 public void disconnectWifiDisplay() {
1592 // This request does not require special permissions.
1593 // Any app can request disconnection from the currently active wifi display.
1594 // This exception should no longer be needed once wifi display control moves
1595 // to the media router service.
1596
1597 final long token = Binder.clearCallingIdentity();
1598 try {
1599 disconnectWifiDisplayInternal();
1600 } finally {
1601 Binder.restoreCallingIdentity(token);
1602 }
1603 }
1604
1605 @Override // Binder call
1606 public void renameWifiDisplay(String address, String alias) {
1607 if (address == null) {
1608 throw new IllegalArgumentException("address must not be null");
1609 }
1610 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1611 "Permission required to rename to a wifi display");
1612
1613 final long token = Binder.clearCallingIdentity();
1614 try {
1615 renameWifiDisplayInternal(address, alias);
1616 } finally {
1617 Binder.restoreCallingIdentity(token);
1618 }
1619 }
1620
1621 @Override // Binder call
1622 public void forgetWifiDisplay(String address) {
1623 if (address == null) {
1624 throw new IllegalArgumentException("address must not be null");
1625 }
1626 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1627 "Permission required to forget to a wifi display");
1628
1629 final long token = Binder.clearCallingIdentity();
1630 try {
1631 forgetWifiDisplayInternal(address);
1632 } finally {
1633 Binder.restoreCallingIdentity(token);
1634 }
1635 }
1636
1637 @Override // Binder call
1638 public void pauseWifiDisplay() {
1639 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1640 "Permission required to pause a wifi display session");
1641
1642 final long token = Binder.clearCallingIdentity();
1643 try {
1644 pauseWifiDisplayInternal();
1645 } finally {
1646 Binder.restoreCallingIdentity(token);
1647 }
1648 }
1649
1650 @Override // Binder call
1651 public void resumeWifiDisplay() {
1652 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1653 "Permission required to resume a wifi display session");
1654
1655 final long token = Binder.clearCallingIdentity();
1656 try {
1657 resumeWifiDisplayInternal();
1658 } finally {
1659 Binder.restoreCallingIdentity(token);
1660 }
1661 }
1662
1663 @Override // Binder call
1664 public WifiDisplayStatus getWifiDisplayStatus() {
1665 // This request does not require special permissions.
1666 // Any app can get information about available wifi displays.
1667
1668 final long token = Binder.clearCallingIdentity();
1669 try {
1670 return getWifiDisplayStatusInternal();
1671 } finally {
1672 Binder.restoreCallingIdentity(token);
1673 }
1674 }
1675
1676 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001677 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001678 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001679 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1680 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001681 final long token = Binder.clearCallingIdentity();
1682 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001683 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001684 } finally {
1685 Binder.restoreCallingIdentity(token);
1686 }
1687 }
1688
1689 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001690 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001691 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001692 int width, int height, int densityDpi, Surface surface, int flags,
1693 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001694 final int callingUid = Binder.getCallingUid();
1695 if (!validatePackageName(callingUid, packageName)) {
1696 throw new SecurityException("packageName must match the calling uid");
1697 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001698 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001699 throw new IllegalArgumentException("appToken must not be null");
1700 }
1701 if (TextUtils.isEmpty(name)) {
1702 throw new IllegalArgumentException("name must be non-null and non-empty");
1703 }
1704 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1705 throw new IllegalArgumentException("width, height, and densityDpi must be "
1706 + "greater than 0");
1707 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001708 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001709 throw new IllegalArgumentException("Surface can't be single-buffered");
1710 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001711
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001712 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1713 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1714
1715 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001716 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001717 throw new IllegalArgumentException(
1718 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1719 }
Michael Wright6720be42014-07-29 19:14:16 -07001720 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001721 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1722 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001723 }
1724
Michael Wrightc39d47a2014-07-08 18:07:36 -07001725 if (projection != null) {
1726 try {
1727 if (!getProjectionService().isValidMediaProjection(projection)) {
1728 throw new SecurityException("Invalid media projection");
1729 }
Michael Wright6720be42014-07-29 19:14:16 -07001730 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001731 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001732 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001733 }
1734 }
1735
Michael Wright6720be42014-07-29 19:14:16 -07001736 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001737 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001738 if (!canProjectVideo(projection)) {
1739 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1740 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1741 + "MediaProjection token in order to create a screen sharing virtual "
1742 + "display.");
1743 }
1744 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001745 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001746 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001747 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001748 + "or an appropriate MediaProjection token to create a "
1749 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001750 }
1751 }
1752
1753 final long token = Binder.clearCallingIdentity();
1754 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001755 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1756 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001757 } finally {
1758 Binder.restoreCallingIdentity(token);
1759 }
1760 }
1761
1762 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001763 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001764 int width, int height, int densityDpi) {
1765 final long token = Binder.clearCallingIdentity();
1766 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001767 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001768 } finally {
1769 Binder.restoreCallingIdentity(token);
1770 }
1771 }
1772
1773 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001774 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001775 if (surface != null && surface.isSingleBuffered()) {
1776 throw new IllegalArgumentException("Surface can't be single-buffered");
1777 }
Jeff Brown92207df2014-04-16 13:16:07 -07001778 final long token = Binder.clearCallingIdentity();
1779 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001780 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001781 } finally {
1782 Binder.restoreCallingIdentity(token);
1783 }
1784 }
1785
1786 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001787 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001788 final long token = Binder.clearCallingIdentity();
1789 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001790 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001791 } finally {
1792 Binder.restoreCallingIdentity(token);
1793 }
1794 }
1795
1796 @Override // Binder call
1797 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001798 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001799
1800 final long token = Binder.clearCallingIdentity();
1801 try {
1802 dumpInternal(pw);
1803 } finally {
1804 Binder.restoreCallingIdentity(token);
1805 }
1806 }
1807
Kenny Guy22bd0442017-10-26 00:15:54 +01001808 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001809 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001810 mContext.enforceCallingOrSelfPermission(
1811 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1812 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001813
1814 final int callingUid = Binder.getCallingUid();
1815 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
1816 final int mode = appOpsManager.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
1817 callingUid, callingPackage);
1818 final boolean hasUsageStats;
1819 if (mode == AppOpsManager.MODE_DEFAULT) {
1820 // The default behavior here is to check if PackageManager has given the app
1821 // permission.
1822 hasUsageStats = mContext.checkCallingPermission(
1823 Manifest.permission.PACKAGE_USAGE_STATS)
1824 == PackageManager.PERMISSION_GRANTED;
1825 } else {
1826 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1827 }
1828
1829 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001830 final long token = Binder.clearCallingIdentity();
1831 try {
Michael Wright144aac92017-12-21 18:37:41 +00001832 synchronized (mSyncRoot) {
1833 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
1834 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001835 } finally {
1836 Binder.restoreCallingIdentity(token);
1837 }
1838 }
1839
Michael Wrighteef0e132017-11-21 17:57:52 +00001840 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00001841 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
1842 mContext.enforceCallingOrSelfPermission(
1843 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
1844 "Permission required to to access ambient light stats.");
1845 final int callingUid = Binder.getCallingUid();
1846 final int userId = UserHandle.getUserId(callingUid);
1847 final long token = Binder.clearCallingIdentity();
1848 try {
1849 synchronized (mSyncRoot) {
1850 return mDisplayPowerController.getAmbientBrightnessStats(userId);
1851 }
1852 } finally {
1853 Binder.restoreCallingIdentity(token);
1854 }
1855 }
1856
1857 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00001858 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00001859 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001860 mContext.enforceCallingOrSelfPermission(
1861 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1862 "Permission required to change the display's brightness configuration");
1863 if (userId != UserHandle.getCallingUserId()) {
1864 mContext.enforceCallingOrSelfPermission(
1865 Manifest.permission.INTERACT_ACROSS_USERS,
1866 "Permission required to change the display brightness"
1867 + " configuration of another user");
1868 }
Kenny Guy05ce8092018-01-17 13:44:20 +00001869 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
1870 packageName = null;
1871 }
Michael Wrighteef0e132017-11-21 17:57:52 +00001872 final long token = Binder.clearCallingIdentity();
1873 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001874 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001875 } finally {
1876 Binder.restoreCallingIdentity(token);
1877 }
1878 }
1879
Michael Wrightd8460232018-01-16 18:04:59 +00001880 @Override // Binder call
1881 public void setTemporaryBrightness(int brightness) {
1882 mContext.enforceCallingOrSelfPermission(
1883 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
1884 "Permission required to set the display's brightness");
1885 final long token = Binder.clearCallingIdentity();
1886 try {
1887 synchronized (mSyncRoot) {
1888 mDisplayPowerController.setTemporaryBrightness(brightness);
1889 }
1890 } finally {
1891 Binder.restoreCallingIdentity(token);
1892 }
1893 }
1894
1895 @Override // Binder call
1896 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
1897 mContext.enforceCallingOrSelfPermission(
1898 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
1899 "Permission required to set the display's auto brightness adjustment");
1900 final long token = Binder.clearCallingIdentity();
1901 try {
1902 synchronized (mSyncRoot) {
1903 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
1904 }
1905 } finally {
1906 Binder.restoreCallingIdentity(token);
1907 }
1908 }
1909
Jeff Brown4ccb8232014-01-16 22:16:42 -08001910 private boolean validatePackageName(int uid, String packageName) {
1911 if (packageName != null) {
1912 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1913 if (packageNames != null) {
1914 for (String n : packageNames) {
1915 if (n.equals(packageName)) {
1916 return true;
1917 }
1918 }
1919 }
1920 }
1921 return false;
1922 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001923
1924 private boolean canProjectVideo(IMediaProjection projection) {
1925 if (projection != null) {
1926 try {
1927 if (projection.canProjectVideo()) {
1928 return true;
1929 }
1930 } catch (RemoteException e) {
1931 Slog.e(TAG, "Unable to query projection service for permissions", e);
1932 }
1933 }
1934 if (mContext.checkCallingPermission(
1935 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001936 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001937 return true;
1938 }
1939 return canProjectSecureVideo(projection);
1940 }
1941
1942 private boolean canProjectSecureVideo(IMediaProjection projection) {
1943 if (projection != null) {
1944 try {
1945 if (projection.canProjectSecureVideo()){
1946 return true;
1947 }
1948 } catch (RemoteException e) {
1949 Slog.e(TAG, "Unable to query projection service for permissions", e);
1950 }
1951 }
1952 return mContext.checkCallingPermission(
1953 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001954 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001955 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001956 }
1957
1958 private final class LocalService extends DisplayManagerInternal {
1959 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001960 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001961 SensorManager sensorManager) {
1962 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001963 DisplayBlanker blanker = new DisplayBlanker() {
1964 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001965 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001966 // The order of operations is important for legacy reasons.
1967 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001968 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001969 }
1970
1971 callbacks.onDisplayStateChange(state);
1972
1973 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001974 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001975 }
1976 }
1977 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001978 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001979 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001980 }
Michael Wrighteef0e132017-11-21 17:57:52 +00001981
1982 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07001983 }
1984
1985 @Override
1986 public boolean requestPowerState(DisplayPowerRequest request,
1987 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001988 synchronized (mSyncRoot) {
1989 return mDisplayPowerController.requestPowerState(request,
1990 waitForNegativeProximity);
1991 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001992 }
1993
1994 @Override
1995 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00001996 synchronized (mSyncRoot) {
1997 return mDisplayPowerController.isProximitySensorAvailable();
1998 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001999 }
2000
2001 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002002 public DisplayInfo getDisplayInfo(int displayId) {
2003 return getDisplayInfoInternal(displayId, Process.myUid());
2004 }
2005
2006 @Override
2007 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2008 if (listener == null) {
2009 throw new IllegalArgumentException("listener must not be null");
2010 }
2011
2012 registerDisplayTransactionListenerInternal(listener);
2013 }
2014
2015 @Override
2016 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2017 if (listener == null) {
2018 throw new IllegalArgumentException("listener must not be null");
2019 }
2020
2021 unregisterDisplayTransactionListenerInternal(listener);
2022 }
2023
2024 @Override
2025 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2026 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2027 }
2028
2029 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002030 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2031 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2032 }
2033
2034 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002035 public void performTraversal(SurfaceControl.Transaction t) {
2036 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002037 }
2038
2039 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002040 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002041 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2042 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2043 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002044 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002045
2046 @Override
2047 public void setDisplayOffsets(int displayId, int x, int y) {
2048 setDisplayOffsetsInternal(displayId, x, y);
2049 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002050
2051 @Override
2052 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2053 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2054 }
2055
2056 @Override
2057 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2058 return isUidPresentOnDisplayInternal(uid, displayId);
2059 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002060
2061 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002062 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002063 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002064 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002065 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002066 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002067
2068 @Override
2069 public void onOverlayChanged() {
2070 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002071 for (int i = 0; i < mDisplayDevices.size(); i++) {
2072 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002073 }
2074 }
2075 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002076 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002077}