blob: 93e0bd5b45c5c41fc3cb6a5bf889b2dff012b715 [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;
Dan Gittik7a32fba2018-03-28 12:19:38 +010064import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070065import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010066import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070067import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070068import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070069import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010070import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000071import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010072import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070073import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080074import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070075import android.util.Slog;
76import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070077import android.view.Display;
78import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070079import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080080import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070081
Michael Wrighteef0e132017-11-21 17:57:52 +000082import com.android.internal.util.Preconditions;
Jorim Jaggied7993b2017-03-28 18:50:01 +010083import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080084import com.android.server.DisplayThread;
85import com.android.server.LocalServices;
86import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070087import com.android.server.UiThread;
Adrian Roose99bc052017-11-20 17:55:31 +010088import com.android.server.wm.WindowManagerInternal;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020089import com.android.server.wm.SurfaceAnimationThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070090
91import java.io.FileDescriptor;
92import java.io.PrintWriter;
93import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070094import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070095import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080096import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070097
98/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070099 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700100 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700101 * The {@link DisplayManagerService} manages the global lifecycle of displays,
102 * decides how to configure logical displays based on the physical display devices currently
103 * attached, sends notifications to the system and to applications when the state
104 * changes, and so on.
105 * </p><p>
106 * The display manager service relies on a collection of {@link DisplayAdapter} components,
107 * for discovering and configuring physical display devices attached to the system.
108 * There are separate display adapters for each manner that devices are attached:
109 * one display adapter for built-in local displays, one for simulated non-functional
110 * displays when the system is headless, one for simulated overlay displays used for
111 * development, one for wifi displays, etc.
112 * </p><p>
113 * Display adapters are only weakly coupled to the display manager service.
114 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700115 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700116 * by the display manager service. This separation of concerns is important for
117 * two main reasons. First, it neatly encapsulates the responsibilities of these
118 * two classes: display adapters handle individual display devices whereas
119 * the display manager service handles the global state. Second, it eliminates
120 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700121 * </p>
122 *
123 * <h3>Synchronization</h3>
124 * <p>
125 * Because the display manager may be accessed by multiple threads, the synchronization
126 * story gets a little complicated. In particular, the window manager may call into
127 * the display manager while holding a surface transaction with the expectation that
128 * it can apply changes immediately. Unfortunately, that means we can't just do
129 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700130 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700131 * To make this work, all of the objects that belong to the display manager must
132 * use the same lock. We call this lock the synchronization root and it has a unique
133 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
134 * named with the "Locked" suffix.
135 * </p><p>
136 * Where things get tricky is that the display manager is not allowed to make
137 * any potentially reentrant calls, especially into the window manager. We generally
138 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700139 * </p>
140 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800141public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700142 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700143 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700144
Jeff Brownbbd28a22012-09-20 16:47:15 -0700145 // When this system property is set to 0, WFD is forcibly disabled on boot.
146 // When this system property is set to 1, WFD is forcibly enabled on boot.
147 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
148 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
149
Jeff Brownbd6e1502012-08-28 03:27:37 -0700150 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
151
Santos Cordonc22c5632017-06-21 16:03:49 -0700152 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700153 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
154 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700155 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700156 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100157 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000158 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700159
Jeff Brownb880d882014-02-10 19:47:07 -0800160 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700161 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700162 private final Handler mUiHandler;
163 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800164 private WindowManagerInternal mWindowManagerInternal;
165 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700166 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700167
168 // The synchronization root for the display manager.
169 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800170 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
171 // into WindowManagerService methods that require mWindowMap while holding this unless you are
172 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700173 private final SyncRoot mSyncRoot = new SyncRoot();
174
175 // True if in safe mode.
176 // This option may disable certain display adapters.
177 public boolean mSafeMode;
178
179 // True if we are in a special boot mode where only core applications and
180 // services should be started. This option may disable certain display adapters.
181 public boolean mOnlyCore;
182
Jeff Brown27f1d672012-10-17 18:32:34 -0700183 // True if the display manager service should pretend there is only one display
184 // and only tell applications about the existence of the default logical display.
185 // The display manager can still mirror content to secondary displays but applications
186 // cannot present unique content on those displays.
187 // Used for demonstration purposes only.
188 private final boolean mSingleDisplayDemoMode;
189
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700190 // All callback records indexed by calling process id.
191 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700192 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700193
Jeff Brownbd6e1502012-08-28 03:27:37 -0700194 // List of all currently registered display adapters.
195 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
196
197 // List of all currently connected display devices.
198 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
199
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700200 // List of all logical displays indexed by logical display id.
201 private final SparseArray<LogicalDisplay> mLogicalDisplays =
202 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700203 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
204
Jeff Brown7f3994e2012-12-04 14:04:28 -0800205 // List of all display transaction listeners.
206 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
207 new CopyOnWriteArrayList<DisplayTransactionListener>();
208
Jeff Brownad9ef192014-04-08 17:26:30 -0700209 // Display power controller.
210 private DisplayPowerController mDisplayPowerController;
211
Jeff Brown037c33e2014-04-09 00:31:55 -0700212 // The overall display state, independent of changes that might influence one
213 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700214 private int mGlobalDisplayState = Display.STATE_ON;
215
216 // The overall display brightness.
217 // For now, this only applies to the built-in display but we may split it up eventually.
218 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700219
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700220 // Set to true when there are pending display changes that have yet to be applied
221 // to the surface flinger state.
222 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700223
Jeff Browne08ae382012-09-07 20:36:36 -0700224 // The Wifi display adapter, or null if not registered.
225 private WifiDisplayAdapter mWifiDisplayAdapter;
226
Jeff Brownce468a32013-11-21 16:42:03 -0800227 // The number of active wifi display scan requests.
228 private int mWifiDisplayScanRequestCount;
229
Jeff Browna506a6e2013-06-04 00:02:38 -0700230 // The virtual display adapter, or null if not registered.
231 private VirtualDisplayAdapter mVirtualDisplayAdapter;
232
Michael Wrighteef0e132017-11-21 17:57:52 +0000233 // The User ID of the current user
234 private @UserIdInt int mCurrentUserId;
235
Michael Wrighteedcbf12017-08-16 23:14:54 +0100236 // The stable device screen height and width. These are not tied to a specific display, even
237 // the default display, because they need to be stable over the course of the device's entire
238 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
239 // device).
240 private Point mStableDisplaySize = new Point();
241
Jeff Brownd728bf52012-09-08 18:05:28 -0700242 // Viewports of the default display and the display that should receive touch
243 // input from an external source. Used by the input system.
244 private final DisplayViewport mDefaultViewport = new DisplayViewport();
245 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700246 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700247
Jeff Brown89d55462012-09-19 11:33:42 -0700248 // Persistent data store for all internal settings maintained by the display manager service.
249 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
250
Jeff Brownbd6e1502012-08-28 03:27:37 -0700251 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700252 // May be used outside of the lock but only on the handler thread.
253 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700254
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700255 // Temporary display info, used for comparing display configurations.
256 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
257
Jeff Brownd728bf52012-09-08 18:05:28 -0700258 // Temporary viewports, used when sending new viewport information to the
259 // input system. May be used outside of the lock but only on the handler thread.
260 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
261 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700262 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700263
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700264 // The default color mode for default displays. Overrides the usual
265 // Display.Display.COLOR_MODE_DEFAULT for displays with the
266 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
267 private final int mDefaultDisplayDefaultColorMode;
268
Jeff Browne75926d2014-09-18 15:24:49 -0700269 // Temporary list of deferred work to perform when setting the display state.
270 // Only used by requestDisplayState. The field is self-synchronized and only
271 // intended for use inside of the requestGlobalDisplayStateInternal function.
272 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
273
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800274 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
275 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
276
Santos Cordonee8931e2017-04-05 10:31:15 -0700277 private final Injector mInjector;
278
Jeff Brownb880d882014-02-10 19:47:07 -0800279 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700280 this(context, new Injector());
281 }
282
283 @VisibleForTesting
284 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800285 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700286 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800287 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800288 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700289 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700290 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700291 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700292 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000293 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700294
295 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
296 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000297 mCurrentUserId = UserHandle.USER_SYSTEM;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700298 }
299
300 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100301 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000302 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
303 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100304 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
305 Process.THREAD_GROUP_TOP_APP);
306 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
307 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200308 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
309 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700310 }
311
Jeff Brown4ccb8232014-01-16 22:16:42 -0800312 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800313 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700314 // We need to pre-load the persistent data store so it's ready before the default display
315 // adapter is up so that we have it's configuration. We could load it lazily, but since
316 // 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 -0700317 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100318 synchronized(mSyncRoot) {
319 mPersistentDataStore.loadIfNeeded();
320 loadStableDisplayValuesLocked();
321 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700322 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800323
324 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
325 true /*allowIsolated*/);
326 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700327 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800328 }
329
330 @Override
331 public void onBootPhase(int phase) {
332 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
333 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700334 long timeout = SystemClock.uptimeMillis()
335 + mInjector.getDefaultDisplayDelayTimeout();
336 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
337 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800338 long delay = timeout - SystemClock.uptimeMillis();
339 if (delay <= 0) {
340 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700341 + "to be initialized. DefaultDisplay="
342 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
343 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800344 }
345 if (DEBUG) {
346 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
347 }
348 try {
349 mSyncRoot.wait(delay);
350 } catch (InterruptedException ex) {
351 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700352 }
353 }
354 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700355 }
356
Michael Wrighteef0e132017-11-21 17:57:52 +0000357 @Override
358 public void onSwitchUser(@UserIdInt int newUserId) {
359 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
360 synchronized (mSyncRoot) {
361 if (mCurrentUserId != newUserId) {
362 mCurrentUserId = newUserId;
363 BrightnessConfiguration config =
364 mPersistentDataStore.getBrightnessConfiguration(userSerial);
365 mDisplayPowerController.setBrightnessConfiguration(config);
366 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000367 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000368 }
369 }
370
Jeff Brown4ccb8232014-01-16 22:16:42 -0800371 // TODO: Use dependencies or a boot phase
372 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700373 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800374 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
375 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700376 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700377 }
378 }
379
380 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700381 * Called when the system is ready to go.
382 */
383 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700384 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700385 mSafeMode = safeMode;
386 mOnlyCore = onlyCore;
387 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700388
Jeff Brownbd6e1502012-08-28 03:27:37 -0700389 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100390 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700391 }
392
Santos Cordonee8931e2017-04-05 10:31:15 -0700393 @VisibleForTesting
394 Handler getDisplayHandler() {
395 return mHandler;
396 }
397
Michael Wrighteedcbf12017-08-16 23:14:54 +0100398 private void loadStableDisplayValuesLocked() {
399 final Point size = mPersistentDataStore.getStableDisplaySize();
400 if (size.x > 0 && size.y > 0) {
401 // Just set these values directly so we don't write the display persistent data again
402 // unnecessarily
403 mStableDisplaySize.set(size.x, size.y);
404 } else {
405 final Resources res = mContext.getResources();
406 final int width = res.getInteger(
407 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
408 final int height = res.getInteger(
409 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
410 if (width > 0 && height > 0) {
411 setStableDisplaySizeLocked(width, height);
412 }
413 }
414 }
415
416 private Point getStableDisplaySizeInternal() {
417 Point r = new Point();
418 synchronized (mSyncRoot) {
419 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
420 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
421 }
422 }
423 return r;
424 }
425
Jeff Brown4ccb8232014-01-16 22:16:42 -0800426 private void registerDisplayTransactionListenerInternal(
427 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800428 // List is self-synchronized copy-on-write.
429 mDisplayTransactionListeners.add(listener);
430 }
431
Jeff Brown4ccb8232014-01-16 22:16:42 -0800432 private void unregisterDisplayTransactionListenerInternal(
433 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800434 // List is self-synchronized copy-on-write.
435 mDisplayTransactionListeners.remove(listener);
436 }
437
Jeff Brown4ccb8232014-01-16 22:16:42 -0800438 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700439 int displayId, DisplayInfo info) {
440 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700441 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700442 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700443 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700444 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700445 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700446 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700447 }
448 }
449 }
450
Andrii Kuliancd097992017-03-23 18:31:59 -0700451 /**
452 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
453 */
454 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
455 synchronized (mSyncRoot) {
456 final LogicalDisplay display = mLogicalDisplays.get(displayId);
457 if (display != null) {
458 display.getNonOverrideDisplayInfoLocked(outInfo);
459 }
460 }
461 }
462
Santos Cordonee8931e2017-04-05 10:31:15 -0700463 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800464 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700465 synchronized (mSyncRoot) {
466 if (!mPendingTraversal) {
467 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700468 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700469 mPendingTraversal = false;
470
Robert Carrae606b42018-02-15 15:36:23 -0800471 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700472 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800473
474 // List is self-synchronized copy-on-write.
475 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
476 listener.onDisplayTransaction();
477 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700478 }
479
Jeff Brown5d6443b2015-04-10 20:15:01 -0700480 private void requestGlobalDisplayStateInternal(int state, int brightness) {
481 if (state == Display.STATE_UNKNOWN) {
482 state = Display.STATE_ON;
483 }
484 if (state == Display.STATE_OFF) {
485 brightness = PowerManager.BRIGHTNESS_OFF;
486 } else if (brightness < 0) {
487 brightness = PowerManager.BRIGHTNESS_DEFAULT;
488 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
489 brightness = PowerManager.BRIGHTNESS_ON;
490 }
491
Jeff Browne75926d2014-09-18 15:24:49 -0700492 synchronized (mTempDisplayStateWorkQueue) {
493 try {
494 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700495 // Note that we do not need to schedule traversals here although it
496 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700497 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700498 if (mGlobalDisplayState == state
499 && mGlobalDisplayBrightness == brightness) {
500 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700501 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700502
503 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
504 + Display.stateToString(state)
505 + ", brightness=" + brightness + ")");
506 mGlobalDisplayState = state;
507 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700508 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700509 }
510
511 // Setting the display power state can take hundreds of milliseconds
512 // to complete so we defer the most expensive part of the work until
513 // after we have exited the critical section to avoid blocking other
514 // threads for a long time.
515 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
516 mTempDisplayStateWorkQueue.get(i).run();
517 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700518 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700519 } finally {
520 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700521 }
522 }
523 }
524
Jeff Brown4ccb8232014-01-16 22:16:42 -0800525 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700526 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800527 LogicalDisplay display = mLogicalDisplays.get(displayId);
528 if (display != null) {
529 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800530 if (info.hasAccess(callingUid)
531 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800532 return info;
533 }
534 }
535 return null;
536 }
537 }
538
539 private int[] getDisplayIdsInternal(int callingUid) {
540 synchronized (mSyncRoot) {
541 final int count = mLogicalDisplays.size();
542 int[] displayIds = new int[count];
543 int n = 0;
544 for (int i = 0; i < count; i++) {
545 LogicalDisplay display = mLogicalDisplays.valueAt(i);
546 DisplayInfo info = display.getDisplayInfoLocked();
547 if (info.hasAccess(callingUid)) {
548 displayIds[n++] = mLogicalDisplays.keyAt(i);
549 }
550 }
551 if (n != count) {
552 displayIds = Arrays.copyOfRange(displayIds, 0, n);
553 }
554 return displayIds;
555 }
556 }
557
558 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
559 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700560 if (mCallbacks.get(callingPid) != null) {
561 throw new SecurityException("The calling process has already "
562 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700563 }
564
Jeff Brownbd6e1502012-08-28 03:27:37 -0700565 CallbackRecord record = new CallbackRecord(callingPid, callback);
566 try {
567 IBinder binder = callback.asBinder();
568 binder.linkToDeath(record, 0);
569 } catch (RemoteException ex) {
570 // give up
571 throw new RuntimeException(ex);
572 }
573
574 mCallbacks.put(callingPid, record);
575 }
576 }
577
Jeff Brownce468a32013-11-21 16:42:03 -0800578 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700579 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800580 mCallbacks.remove(record.mPid);
581 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700582 }
583 }
584
Jeff Brown4ccb8232014-01-16 22:16:42 -0800585 private void startWifiDisplayScanInternal(int callingPid) {
586 synchronized (mSyncRoot) {
587 CallbackRecord record = mCallbacks.get(callingPid);
588 if (record == null) {
589 throw new IllegalStateException("The calling process has not "
590 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700591 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800592 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700593 }
594 }
595
Jeff Brownce468a32013-11-21 16:42:03 -0800596 private void startWifiDisplayScanLocked(CallbackRecord record) {
597 if (!record.mWifiDisplayScanRequested) {
598 record.mWifiDisplayScanRequested = true;
599 if (mWifiDisplayScanRequestCount++ == 0) {
600 if (mWifiDisplayAdapter != null) {
601 mWifiDisplayAdapter.requestStartScanLocked();
602 }
603 }
604 }
605 }
606
Jeff Brown4ccb8232014-01-16 22:16:42 -0800607 private void stopWifiDisplayScanInternal(int callingPid) {
608 synchronized (mSyncRoot) {
609 CallbackRecord record = mCallbacks.get(callingPid);
610 if (record == null) {
611 throw new IllegalStateException("The calling process has not "
612 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800613 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800614 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800615 }
616 }
617
618 private void stopWifiDisplayScanLocked(CallbackRecord record) {
619 if (record.mWifiDisplayScanRequested) {
620 record.mWifiDisplayScanRequested = false;
621 if (--mWifiDisplayScanRequestCount == 0) {
622 if (mWifiDisplayAdapter != null) {
623 mWifiDisplayAdapter.requestStopScanLocked();
624 }
625 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700626 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800627 + mWifiDisplayScanRequestCount);
628 mWifiDisplayScanRequestCount = 0;
629 }
630 }
631 }
632
Jeff Brown4ccb8232014-01-16 22:16:42 -0800633 private void connectWifiDisplayInternal(String address) {
634 synchronized (mSyncRoot) {
635 if (mWifiDisplayAdapter != null) {
636 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700637 }
Jeff Browne08ae382012-09-07 20:36:36 -0700638 }
639 }
640
Jeff Brown4ccb8232014-01-16 22:16:42 -0800641 private void pauseWifiDisplayInternal() {
642 synchronized (mSyncRoot) {
643 if (mWifiDisplayAdapter != null) {
644 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700645 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700646 }
647 }
648
Jeff Brown4ccb8232014-01-16 22:16:42 -0800649 private void resumeWifiDisplayInternal() {
650 synchronized (mSyncRoot) {
651 if (mWifiDisplayAdapter != null) {
652 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700653 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700654 }
655 }
656
Jeff Brown4ccb8232014-01-16 22:16:42 -0800657 private void disconnectWifiDisplayInternal() {
658 synchronized (mSyncRoot) {
659 if (mWifiDisplayAdapter != null) {
660 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700661 }
Jeff Browne08ae382012-09-07 20:36:36 -0700662 }
663 }
664
Jeff Brown4ccb8232014-01-16 22:16:42 -0800665 private void renameWifiDisplayInternal(String address, String alias) {
666 synchronized (mSyncRoot) {
667 if (mWifiDisplayAdapter != null) {
668 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700669 }
Jeff Brown89d55462012-09-19 11:33:42 -0700670 }
671 }
672
Jeff Brown4ccb8232014-01-16 22:16:42 -0800673 private void forgetWifiDisplayInternal(String address) {
674 synchronized (mSyncRoot) {
675 if (mWifiDisplayAdapter != null) {
676 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700677 }
Jeff Brown89d55462012-09-19 11:33:42 -0700678 }
679 }
680
Jeff Brown4ccb8232014-01-16 22:16:42 -0800681 private WifiDisplayStatus getWifiDisplayStatusInternal() {
682 synchronized (mSyncRoot) {
683 if (mWifiDisplayAdapter != null) {
684 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700685 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800686 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700687 }
688 }
689
Michael Wright1c9977b2016-07-12 13:30:10 -0700690 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100691 synchronized (mSyncRoot) {
692 LogicalDisplay display = mLogicalDisplays.get(displayId);
693 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700694 display.getRequestedColorModeLocked() != colorMode) {
695 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100696 scheduleTraversalLocked(false);
697 }
698 }
699 }
700
Bryan Mawhinney462e29d2018-03-22 15:52:41 +0000701 private void setSaturationLevelInternal(float level) {
702 if (level < 0 || level > 1) {
703 throw new IllegalArgumentException("Saturation level must be between 0 and 1");
704 }
705 float[] matrix = (level == 1.0f ? null : computeSaturationMatrix(level));
706 DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
707 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION, matrix);
708 }
709
710 private static float[] computeSaturationMatrix(float saturation) {
711 float desaturation = 1.0f - saturation;
712 float[] luminance = {0.231f * desaturation, 0.715f * desaturation, 0.072f * desaturation};
713 float[] matrix = {
714 luminance[0] + saturation, luminance[0], luminance[0], 0,
715 luminance[1], luminance[1] + saturation, luminance[1], 0,
716 luminance[2], luminance[2], luminance[2] + saturation, 0,
717 0, 0, 0, 1
718 };
719 return matrix;
720 }
721
Michael Wright75ee9fc2014-09-01 19:55:22 -0700722 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700723 IMediaProjection projection, int callingUid, String packageName, String name, int width,
724 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800725 synchronized (mSyncRoot) {
726 if (mVirtualDisplayAdapter == null) {
727 Slog.w(TAG, "Rejecting request to create private virtual display "
728 + "because the virtual display adapter is not available.");
729 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700730 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800731
732 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700733 callback, projection, callingUid, packageName, name, width, height, densityDpi,
734 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800735 if (device == null) {
736 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700737 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700738
Jeff Brown4ccb8232014-01-16 22:16:42 -0800739 handleDisplayDeviceAddedLocked(device);
740 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
741 if (display != null) {
742 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700743 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800744
745 // Something weird happened and the logical display was not created.
746 Slog.w(TAG, "Rejecting request to create virtual display "
747 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700748 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800749 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700750 }
751 return -1;
752 }
753
Michael Wright01e840f2014-06-26 16:03:25 -0700754 private void resizeVirtualDisplayInternal(IBinder appToken,
755 int width, int height, int densityDpi) {
756 synchronized (mSyncRoot) {
757 if (mVirtualDisplayAdapter == null) {
758 return;
759 }
760
761 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
762 }
763 }
764
Jeff Brown92207df2014-04-16 13:16:07 -0700765 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
766 synchronized (mSyncRoot) {
767 if (mVirtualDisplayAdapter == null) {
768 return;
769 }
770
771 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
772 }
773 }
774
Jeff Brown4ccb8232014-01-16 22:16:42 -0800775 private void releaseVirtualDisplayInternal(IBinder appToken) {
776 synchronized (mSyncRoot) {
777 if (mVirtualDisplayAdapter == null) {
778 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700779 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700780
Jeff Brown4ccb8232014-01-16 22:16:42 -0800781 DisplayDevice device =
782 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
783 if (device != null) {
784 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700785 }
786 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700787 }
788
Santos Cordonc22c5632017-06-21 16:03:49 -0700789 private void registerDefaultDisplayAdapters() {
790 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700791 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700792 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800793 registerDisplayAdapterLocked(new LocalDisplayAdapter(
794 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700795
796 // Standalone VR devices rely on a virtual display as their primary display for
797 // 2D UI. We register virtual display adapter along side the main display adapter
798 // here so that it is ready by the time the system sends the home Intent for
799 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
800 // the virtual display inside VR before any VR-specific apps even run.
801 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
802 mHandler, mDisplayAdapterListener);
803 if (mVirtualDisplayAdapter != null) {
804 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
805 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700806 }
807 }
808
809 private void registerAdditionalDisplayAdapters() {
810 synchronized (mSyncRoot) {
811 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700812 registerOverlayDisplayAdapterLocked();
813 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700814 }
815 }
816 }
817
Jeff Brown89d55462012-09-19 11:33:42 -0700818 private void registerOverlayDisplayAdapterLocked() {
819 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
820 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
821 }
822
823 private void registerWifiDisplayAdapterLocked() {
824 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700825 com.android.internal.R.bool.config_enableWifiDisplay)
826 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700827 mWifiDisplayAdapter = new WifiDisplayAdapter(
828 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
829 mPersistentDataStore);
830 registerDisplayAdapterLocked(mWifiDisplayAdapter);
831 }
832 }
833
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700834 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
835 // In safe mode, we disable non-essential display adapters to give the user
836 // an opportunity to fix broken settings or other problems that might affect
837 // system stability.
838 // In only-core mode, we disable non-essential display adapters to minimize
839 // the number of dependencies that are started while in this mode and to
840 // prevent problems that might occur due to the device being encrypted.
841 return !mSafeMode && !mOnlyCore;
842 }
843
844 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
845 mDisplayAdapters.add(adapter);
846 adapter.registerLocked();
847 }
848
Jeff Brownbd6e1502012-08-28 03:27:37 -0700849 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700850 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700851 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700852 }
853 }
854
Jeff Browna506a6e2013-06-04 00:02:38 -0700855 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700856 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700857 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700858 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700859 return;
860 }
861
Jeff Brown10acf6d2015-04-14 14:20:47 -0700862 Slog.i(TAG, "Display device added: " + info);
863 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700864
865 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700866 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700867 Runnable work = updateDisplayStateLocked(device);
868 if (work != null) {
869 work.run();
870 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700871 scheduleTraversalLocked(false);
872 }
873
Jeff Brownbd6e1502012-08-28 03:27:37 -0700874 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700875 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700876 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700877 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700878 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700879 return;
880 }
881
Jeff Brown10acf6d2015-04-14 14:20:47 -0700882 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
883 if (diff == DisplayDeviceInfo.DIFF_STATE) {
884 Slog.i(TAG, "Display device changed state: \"" + info.name
885 + "\", " + Display.stateToString(info.state));
886 } else if (diff != 0) {
887 Slog.i(TAG, "Display device changed: " + info);
888 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700889 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
890 try {
891 mPersistentDataStore.setColorMode(device, info.colorMode);
892 } finally {
893 mPersistentDataStore.saveIfNeeded();
894 }
895 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700896 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700897
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700898 device.applyPendingDisplayDeviceInfoChangesLocked();
899 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700900 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700901 }
902 }
903 }
904
Jeff Brownbd6e1502012-08-28 03:27:37 -0700905 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700906 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700907 handleDisplayDeviceRemovedLocked(device);
908 }
909 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700910
Jeff Browna506a6e2013-06-04 00:02:38 -0700911 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700912 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700913 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700914 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700915 return;
916 }
917
Jeff Brown10acf6d2015-04-14 14:20:47 -0700918 Slog.i(TAG, "Display device removed: " + info);
919 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700920
Jeff Browna506a6e2013-06-04 00:02:38 -0700921 updateLogicalDisplaysLocked();
922 scheduleTraversalLocked(false);
923 }
924
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700925 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700926 final int count = mDisplayDevices.size();
927 for (int i = 0; i < count; i++) {
928 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700929 Runnable runnable = updateDisplayStateLocked(device);
930 if (runnable != null) {
931 workQueue.add(runnable);
932 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700933 }
934 }
935
Jeff Browne75926d2014-09-18 15:24:49 -0700936 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700937 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700938 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700939 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
940 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700941 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700942 }
Jeff Browne75926d2014-09-18 15:24:49 -0700943 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700944 }
945
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700946 // Adds a new logical display based on the given display device.
947 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700948 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700949 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
950 boolean isDefault = (deviceInfo.flags
951 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
952 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
953 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
954 isDefault = false;
955 }
956
Jeff Brown27f1d672012-10-17 18:32:34 -0700957 if (!isDefault && mSingleDisplayDemoMode) {
958 Slog.i(TAG, "Not creating a logical display for a secondary display "
959 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700960 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700961 }
962
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700963 final int displayId = assignDisplayIdLocked(isDefault);
964 final int layerStack = assignLayerStackLocked(displayId);
965
Jeff Brownd728bf52012-09-08 18:05:28 -0700966 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700967 display.updateLocked(mDisplayDevices);
968 if (!display.isValidLocked()) {
969 // This should never happen currently.
970 Slog.w(TAG, "Ignoring display device because the logical display "
971 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700972 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700973 }
974
Michael Wrighteedcbf12017-08-16 23:14:54 +0100975 configureColorModeLocked(display, device);
976 if (isDefault) {
977 recordStableDisplayStatsIfNeededLocked(display);
978 }
979
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700980 mLogicalDisplays.put(displayId, display);
981
982 // Wake up waitForDefaultDisplay.
983 if (isDefault) {
984 mSyncRoot.notifyAll();
985 }
986
987 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700988 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700989 }
990
991 private int assignDisplayIdLocked(boolean isDefault) {
992 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
993 }
994
995 private int assignLayerStackLocked(int displayId) {
996 // Currently layer stacks and display ids are the same.
997 // This need not be the case.
998 return displayId;
999 }
1000
Michael Wrighteedcbf12017-08-16 23:14:54 +01001001 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1002 if (display.getPrimaryDisplayDeviceLocked() == device) {
1003 int colorMode = mPersistentDataStore.getColorMode(device);
1004 if (colorMode == Display.COLOR_MODE_INVALID) {
1005 if ((device.getDisplayDeviceInfoLocked().flags
1006 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1007 colorMode = mDefaultDisplayDefaultColorMode;
1008 } else {
1009 colorMode = Display.COLOR_MODE_DEFAULT;
1010 }
1011 }
1012 display.setRequestedColorModeLocked(colorMode);
1013 }
1014 }
1015
1016 // If we've never recorded stable device stats for this device before and they aren't
1017 // explicitly configured, go ahead and record the stable device stats now based on the status
1018 // of the default display at first boot.
1019 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1020 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1021 DisplayInfo info = d.getDisplayInfoLocked();
1022 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1023 }
1024 }
1025
1026 private void setStableDisplaySizeLocked(int width, int height) {
1027 mStableDisplaySize = new Point(width, height);
1028 try {
1029 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1030 } finally {
1031 mPersistentDataStore.saveIfNeeded();
1032 }
1033 }
1034
Michael Wrighteef0e132017-11-21 17:57:52 +00001035 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001036 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001037 @Nullable String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001038 final int userSerial = getUserManager().getUserSerialNumber(userId);
1039 synchronized (mSyncRoot) {
1040 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001041 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1042 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001043 } finally {
1044 mPersistentDataStore.saveIfNeeded();
1045 }
1046 if (userId == mCurrentUserId) {
1047 mDisplayPowerController.setBrightnessConfiguration(c);
1048 }
1049 }
1050 }
1051
1052 private void loadBrightnessConfiguration() {
1053 synchronized (mSyncRoot) {
1054 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1055 BrightnessConfiguration config =
1056 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1057 mDisplayPowerController.setBrightnessConfiguration(config);
1058 }
1059 }
1060
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001061 // Updates all existing logical displays given the current set of display devices.
1062 // Removes invalid logical displays.
1063 // Sends notifications if needed.
1064 private boolean updateLogicalDisplaysLocked() {
1065 boolean changed = false;
1066 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1067 final int displayId = mLogicalDisplays.keyAt(i);
1068 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1069
1070 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1071 display.updateLocked(mDisplayDevices);
1072 if (!display.isValidLocked()) {
1073 mLogicalDisplays.removeAt(i);
1074 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1075 changed = true;
1076 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1077 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1078 changed = true;
1079 }
1080 }
1081 return changed;
1082 }
1083
Robert Carrae606b42018-02-15 15:36:23 -08001084 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001085 // Clear all viewports before configuring displays so that we can keep
1086 // track of which ones we have configured.
1087 clearViewportsLocked();
1088
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001089 // Configure each display device.
1090 final int count = mDisplayDevices.size();
1091 for (int i = 0; i < count; i++) {
1092 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001093 configureDisplayLocked(t, device);
1094 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001095 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001096
1097 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001098 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001099 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1100 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001101 }
1102
Michael Wright3f145a22014-07-22 19:46:03 -07001103 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001104 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001105 synchronized (mSyncRoot) {
1106 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001107 if (display == null) {
1108 return;
1109 }
1110 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001111 if (DEBUG) {
1112 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1113 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1114 }
1115
Craig Mautner722285e2012-09-07 13:55:58 -07001116 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001117 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001118 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001119 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1120 // Scan supported modes returned by display.getInfo() to find a mode with the same
1121 // size as the default display mode but with the specified refresh rate instead.
1122 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1123 requestedRefreshRate);
1124 }
1125 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001126 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001127 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001128 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001129 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001130 scheduleTraversalLocked(inTraversal);
1131 }
Craig Mautner722285e2012-09-07 13:55:58 -07001132 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001133 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001134
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001135 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1136 synchronized (mSyncRoot) {
1137 LogicalDisplay display = mLogicalDisplays.get(displayId);
1138 if (display == null) {
1139 return;
1140 }
1141 if (display.getDisplayOffsetXLocked() != x
1142 || display.getDisplayOffsetYLocked() != y) {
1143 if (DEBUG) {
1144 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1145 + x + ", " + y + ")");
1146 }
1147 display.setDisplayOffsetsLocked(x, y);
1148 scheduleTraversalLocked(false);
1149 }
1150 }
1151 }
1152
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001153 // Updates the lists of UIDs that are present on displays.
1154 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1155 synchronized (mSyncRoot) {
1156 mDisplayAccessUIDs.clear();
1157 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1158 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1159 newDisplayAccessUIDs.valueAt(i));
1160 }
1161 }
1162 }
1163
1164 // Checks if provided UID's content is present on the display and UID has access to it.
1165 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1166 synchronized (mSyncRoot) {
1167 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1168 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1169 }
1170 }
1171
Jeff Brownd728bf52012-09-08 18:05:28 -07001172 private void clearViewportsLocked() {
1173 mDefaultViewport.valid = false;
1174 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001175 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001176 }
1177
Robert Carrae606b42018-02-15 15:36:23 -08001178 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001179 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1180 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001181
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001182 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001183 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001184 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001185 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001186 if (display != null && !display.hasContentLocked()) {
1187 // If the display does not have any content of its own, then
1188 // automatically mirror the default logical display contents.
1189 display = null;
1190 }
1191 if (display == null) {
1192 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1193 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001194 }
1195
1196 // Apply the logical display configuration to the display device.
1197 if (display == null) {
1198 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001199 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001200 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001201 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001202 }
Robert Carrae606b42018-02-15 15:36:23 -08001203 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001204
1205 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001206 if (!mDefaultViewport.valid
1207 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1208 setViewportLocked(mDefaultViewport, display, device);
1209 }
1210 if (!mExternalTouchViewport.valid
1211 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1212 setViewportLocked(mExternalTouchViewport, display, device);
1213 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001214
1215 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1216 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1217 setViewportLocked(viewport, display, device);
1218 }
1219 }
1220
1221 /** Gets the virtual device viewport or creates it if not yet created. */
1222 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1223 DisplayViewport viewport;
1224 final int count = mVirtualTouchViewports.size();
1225 for (int i = 0; i < count; i++) {
1226 viewport = mVirtualTouchViewports.get(i);
1227 if (uniqueId.equals(viewport.uniqueId)) {
1228 return viewport;
1229 }
1230 }
1231
1232 viewport = new DisplayViewport();
1233 viewport.uniqueId = uniqueId;
1234 mVirtualTouchViewports.add(viewport);
1235 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001236 }
1237
1238 private static void setViewportLocked(DisplayViewport viewport,
1239 LogicalDisplay display, DisplayDevice device) {
1240 viewport.valid = true;
1241 viewport.displayId = display.getDisplayIdLocked();
1242 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001243 }
1244
1245 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1246 final int count = mLogicalDisplays.size();
1247 for (int i = 0; i < count; i++) {
1248 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1249 if (display.getPrimaryDisplayDeviceLocked() == device) {
1250 return display;
1251 }
1252 }
1253 return null;
1254 }
1255
Jeff Brownbd6e1502012-08-28 03:27:37 -07001256 private void sendDisplayEventLocked(int displayId, int event) {
1257 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1258 mHandler.sendMessage(msg);
1259 }
1260
Robert Carrae606b42018-02-15 15:36:23 -08001261 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001262 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001263 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001264 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001265 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001266 if (!inTraversal) {
1267 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1268 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001269 }
1270 }
1271
1272 // Runs on Handler thread.
1273 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001274 private void deliverDisplayEvent(int displayId, int event) {
1275 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001276 Slog.d(TAG, "Delivering display event: displayId="
1277 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001278 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001279
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001280 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001281 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001282 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001283 count = mCallbacks.size();
1284 mTempCallbacks.clear();
1285 for (int i = 0; i < count; i++) {
1286 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001287 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001288 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001289
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001290 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001291 for (int i = 0; i < count; i++) {
1292 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1293 }
1294 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001295 }
1296
Michael Wrightc39d47a2014-07-08 18:07:36 -07001297 private IMediaProjectionManager getProjectionService() {
1298 if (mProjectionService == null) {
1299 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1300 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1301 }
1302 return mProjectionService;
1303 }
1304
Michael Wrighteef0e132017-11-21 17:57:52 +00001305 private UserManager getUserManager() {
1306 return mContext.getSystemService(UserManager.class);
1307 }
1308
Jeff Brown4ccb8232014-01-16 22:16:42 -08001309 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001310 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001311
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001312 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001313 pw.println(" mOnlyCode=" + mOnlyCore);
1314 pw.println(" mSafeMode=" + mSafeMode);
1315 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001316 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001317 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1318 pw.println(" mDefaultViewport=" + mDefaultViewport);
1319 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001320 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001321 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001322 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001323 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001324 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1325
Jeff Brown9e316a12012-10-08 19:17:06 -07001326
Jeff Brownbd6e1502012-08-28 03:27:37 -07001327 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001328 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001329
1330 pw.println();
1331 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001332 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001333 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001334 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001335 }
Craig Mautner9de49362012-08-02 14:30:30 -07001336
Jeff Brownbd6e1502012-08-28 03:27:37 -07001337 pw.println();
1338 pw.println("Display Devices: size=" + mDisplayDevices.size());
1339 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001340 pw.println(" " + device.getDisplayDeviceInfoLocked());
1341 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001342 }
1343
1344 final int logicalDisplayCount = mLogicalDisplays.size();
1345 pw.println();
1346 pw.println("Logical Displays: size=" + logicalDisplayCount);
1347 for (int i = 0; i < logicalDisplayCount; i++) {
1348 int displayId = mLogicalDisplays.keyAt(i);
1349 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1350 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001351 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001352 }
Jeff Brownce468a32013-11-21 16:42:03 -08001353
1354 final int callbackCount = mCallbacks.size();
1355 pw.println();
1356 pw.println("Callbacks: size=" + callbackCount);
1357 for (int i = 0; i < callbackCount; i++) {
1358 CallbackRecord callback = mCallbacks.valueAt(i);
1359 pw.println(" " + i + ": mPid=" + callback.mPid
1360 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1361 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001362
1363 if (mDisplayPowerController != null) {
1364 mDisplayPowerController.dump(pw);
1365 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001366
1367 pw.println();
1368 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001369 }
1370 }
1371
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001372 /**
1373 * This is the object that everything in the display manager locks on.
1374 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1375 * clear that the object belongs to the display manager service and that it is
1376 * a unique object with a special purpose.
1377 */
1378 public static final class SyncRoot {
1379 }
1380
Santos Cordonee8931e2017-04-05 10:31:15 -07001381 @VisibleForTesting
1382 static class Injector {
1383 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1384 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1385 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1386 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001387
1388 long getDefaultDisplayDelayTimeout() {
1389 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1390 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001391 }
1392
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001393 @VisibleForTesting
1394 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1395 synchronized (mSyncRoot) {
1396 LogicalDisplay display = mLogicalDisplays.get(displayId);
1397 if (display != null) {
1398 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1399 return displayDevice.getDisplayDeviceInfoLocked();
1400 }
1401 return null;
1402 }
1403 }
1404
Jeff Brownbd6e1502012-08-28 03:27:37 -07001405 private final class DisplayManagerHandler extends Handler {
1406 public DisplayManagerHandler(Looper looper) {
1407 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001408 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001409
Jeff Brownbd6e1502012-08-28 03:27:37 -07001410 @Override
1411 public void handleMessage(Message msg) {
1412 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001413 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1414 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001415 break;
1416
1417 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1418 registerAdditionalDisplayAdapters();
1419 break;
1420
1421 case MSG_DELIVER_DISPLAY_EVENT:
1422 deliverDisplayEvent(msg.arg1, msg.arg2);
1423 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001424
1425 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001426 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001427 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001428
1429 case MSG_UPDATE_VIEWPORT: {
1430 synchronized (mSyncRoot) {
1431 mTempDefaultViewport.copyFrom(mDefaultViewport);
1432 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001433 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1434 mTempVirtualTouchViewports.clear();
1435 for (DisplayViewport d : mVirtualTouchViewports) {
1436 mTempVirtualTouchViewports.add(d.makeCopy());
1437 }
1438 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001439 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001440 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1441 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001442 break;
1443 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001444
Michael Wrighteef0e132017-11-21 17:57:52 +00001445 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1446 loadBrightnessConfiguration();
1447 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001448 }
1449 }
1450 }
1451
1452 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1453 @Override
1454 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1455 switch (event) {
1456 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1457 handleDisplayDeviceAdded(device);
1458 break;
1459
1460 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1461 handleDisplayDeviceChanged(device);
1462 break;
1463
1464 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1465 handleDisplayDeviceRemoved(device);
1466 break;
1467 }
1468 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001469
1470 @Override
1471 public void onTraversalRequested() {
1472 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001473 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001474 }
1475 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001476 }
1477
1478 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001479 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001480 private final IDisplayManagerCallback mCallback;
1481
Jeff Brownce468a32013-11-21 16:42:03 -08001482 public boolean mWifiDisplayScanRequested;
1483
Jeff Brownbd6e1502012-08-28 03:27:37 -07001484 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1485 mPid = pid;
1486 mCallback = callback;
1487 }
1488
1489 @Override
1490 public void binderDied() {
1491 if (DEBUG) {
1492 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1493 }
Jeff Brownce468a32013-11-21 16:42:03 -08001494 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001495 }
1496
1497 public void notifyDisplayEventAsync(int displayId, int event) {
1498 try {
1499 mCallback.onDisplayEvent(displayId, event);
1500 } catch (RemoteException ex) {
1501 Slog.w(TAG, "Failed to notify process "
1502 + mPid + " that displays changed, assuming it died.", ex);
1503 binderDied();
1504 }
1505 }
1506 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001507
Santos Cordonee8931e2017-04-05 10:31:15 -07001508 @VisibleForTesting
1509 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001510 /**
1511 * Returns information about the specified logical display.
1512 *
1513 * @param displayId The logical display id.
1514 * @return The logical display info, or null if the display does not exist. The
1515 * returned object must be treated as immutable.
1516 */
1517 @Override // Binder call
1518 public DisplayInfo getDisplayInfo(int displayId) {
1519 final int callingUid = Binder.getCallingUid();
1520 final long token = Binder.clearCallingIdentity();
1521 try {
1522 return getDisplayInfoInternal(displayId, callingUid);
1523 } finally {
1524 Binder.restoreCallingIdentity(token);
1525 }
1526 }
1527
1528 /**
1529 * Returns the list of all display ids.
1530 */
1531 @Override // Binder call
1532 public int[] getDisplayIds() {
1533 final int callingUid = Binder.getCallingUid();
1534 final long token = Binder.clearCallingIdentity();
1535 try {
1536 return getDisplayIdsInternal(callingUid);
1537 } finally {
1538 Binder.restoreCallingIdentity(token);
1539 }
1540 }
1541
Michael Wrighteedcbf12017-08-16 23:14:54 +01001542 /**
1543 * Returns the stable device display size, in pixels.
1544 */
1545 @Override // Binder call
1546 public Point getStableDisplaySize() {
1547 final long token = Binder.clearCallingIdentity();
1548 try {
1549 return getStableDisplaySizeInternal();
1550 } finally {
1551 Binder.restoreCallingIdentity(token);
1552 }
1553 }
1554
Jeff Brown4ccb8232014-01-16 22:16:42 -08001555 @Override // Binder call
1556 public void registerCallback(IDisplayManagerCallback callback) {
1557 if (callback == null) {
1558 throw new IllegalArgumentException("listener must not be null");
1559 }
1560
1561 final int callingPid = Binder.getCallingPid();
1562 final long token = Binder.clearCallingIdentity();
1563 try {
1564 registerCallbackInternal(callback, callingPid);
1565 } finally {
1566 Binder.restoreCallingIdentity(token);
1567 }
1568 }
1569
1570 @Override // Binder call
1571 public void startWifiDisplayScan() {
1572 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1573 "Permission required to start wifi display scans");
1574
1575 final int callingPid = Binder.getCallingPid();
1576 final long token = Binder.clearCallingIdentity();
1577 try {
1578 startWifiDisplayScanInternal(callingPid);
1579 } finally {
1580 Binder.restoreCallingIdentity(token);
1581 }
1582 }
1583
1584 @Override // Binder call
1585 public void stopWifiDisplayScan() {
1586 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1587 "Permission required to stop wifi display scans");
1588
1589 final int callingPid = Binder.getCallingPid();
1590 final long token = Binder.clearCallingIdentity();
1591 try {
1592 stopWifiDisplayScanInternal(callingPid);
1593 } finally {
1594 Binder.restoreCallingIdentity(token);
1595 }
1596 }
1597
1598 @Override // Binder call
1599 public void connectWifiDisplay(String address) {
1600 if (address == null) {
1601 throw new IllegalArgumentException("address must not be null");
1602 }
1603 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1604 "Permission required to connect to a wifi display");
1605
1606 final long token = Binder.clearCallingIdentity();
1607 try {
1608 connectWifiDisplayInternal(address);
1609 } finally {
1610 Binder.restoreCallingIdentity(token);
1611 }
1612 }
1613
1614 @Override // Binder call
1615 public void disconnectWifiDisplay() {
1616 // This request does not require special permissions.
1617 // Any app can request disconnection from the currently active wifi display.
1618 // This exception should no longer be needed once wifi display control moves
1619 // to the media router service.
1620
1621 final long token = Binder.clearCallingIdentity();
1622 try {
1623 disconnectWifiDisplayInternal();
1624 } finally {
1625 Binder.restoreCallingIdentity(token);
1626 }
1627 }
1628
1629 @Override // Binder call
1630 public void renameWifiDisplay(String address, String alias) {
1631 if (address == null) {
1632 throw new IllegalArgumentException("address must not be null");
1633 }
1634 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1635 "Permission required to rename to a wifi display");
1636
1637 final long token = Binder.clearCallingIdentity();
1638 try {
1639 renameWifiDisplayInternal(address, alias);
1640 } finally {
1641 Binder.restoreCallingIdentity(token);
1642 }
1643 }
1644
1645 @Override // Binder call
1646 public void forgetWifiDisplay(String address) {
1647 if (address == null) {
1648 throw new IllegalArgumentException("address must not be null");
1649 }
1650 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1651 "Permission required to forget to a wifi display");
1652
1653 final long token = Binder.clearCallingIdentity();
1654 try {
1655 forgetWifiDisplayInternal(address);
1656 } finally {
1657 Binder.restoreCallingIdentity(token);
1658 }
1659 }
1660
1661 @Override // Binder call
1662 public void pauseWifiDisplay() {
1663 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1664 "Permission required to pause a wifi display session");
1665
1666 final long token = Binder.clearCallingIdentity();
1667 try {
1668 pauseWifiDisplayInternal();
1669 } finally {
1670 Binder.restoreCallingIdentity(token);
1671 }
1672 }
1673
1674 @Override // Binder call
1675 public void resumeWifiDisplay() {
1676 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1677 "Permission required to resume a wifi display session");
1678
1679 final long token = Binder.clearCallingIdentity();
1680 try {
1681 resumeWifiDisplayInternal();
1682 } finally {
1683 Binder.restoreCallingIdentity(token);
1684 }
1685 }
1686
1687 @Override // Binder call
1688 public WifiDisplayStatus getWifiDisplayStatus() {
1689 // This request does not require special permissions.
1690 // Any app can get information about available wifi displays.
1691
1692 final long token = Binder.clearCallingIdentity();
1693 try {
1694 return getWifiDisplayStatusInternal();
1695 } finally {
1696 Binder.restoreCallingIdentity(token);
1697 }
1698 }
1699
1700 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001701 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001702 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001703 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1704 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001705 final long token = Binder.clearCallingIdentity();
1706 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001707 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001708 } finally {
1709 Binder.restoreCallingIdentity(token);
1710 }
1711 }
1712
1713 @Override // Binder call
Bryan Mawhinney462e29d2018-03-22 15:52:41 +00001714 public void setSaturationLevel(float level) {
1715 mContext.enforceCallingOrSelfPermission(
1716 Manifest.permission.CONTROL_DISPLAY_SATURATION,
1717 "Permission required to set display saturation level");
1718 final long token = Binder.clearCallingIdentity();
1719 try {
1720 setSaturationLevelInternal(level);
1721 } finally {
1722 Binder.restoreCallingIdentity(token);
1723 }
1724 }
1725
1726 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001727 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001728 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001729 int width, int height, int densityDpi, Surface surface, int flags,
1730 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001731 final int callingUid = Binder.getCallingUid();
1732 if (!validatePackageName(callingUid, packageName)) {
1733 throw new SecurityException("packageName must match the calling uid");
1734 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001735 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001736 throw new IllegalArgumentException("appToken must not be null");
1737 }
1738 if (TextUtils.isEmpty(name)) {
1739 throw new IllegalArgumentException("name must be non-null and non-empty");
1740 }
1741 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1742 throw new IllegalArgumentException("width, height, and densityDpi must be "
1743 + "greater than 0");
1744 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001745 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001746 throw new IllegalArgumentException("Surface can't be single-buffered");
1747 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001748
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001749 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1750 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1751
1752 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001753 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001754 throw new IllegalArgumentException(
1755 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1756 }
Michael Wright6720be42014-07-29 19:14:16 -07001757 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001758 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1759 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001760 }
1761
Michael Wrightc39d47a2014-07-08 18:07:36 -07001762 if (projection != null) {
1763 try {
1764 if (!getProjectionService().isValidMediaProjection(projection)) {
1765 throw new SecurityException("Invalid media projection");
1766 }
Michael Wright6720be42014-07-29 19:14:16 -07001767 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001768 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001769 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001770 }
1771 }
1772
Michael Wright6720be42014-07-29 19:14:16 -07001773 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001774 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001775 if (!canProjectVideo(projection)) {
1776 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1777 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1778 + "MediaProjection token in order to create a screen sharing virtual "
1779 + "display.");
1780 }
1781 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001782 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001783 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001784 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001785 + "or an appropriate MediaProjection token to create a "
1786 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001787 }
1788 }
1789
1790 final long token = Binder.clearCallingIdentity();
1791 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001792 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1793 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001794 } finally {
1795 Binder.restoreCallingIdentity(token);
1796 }
1797 }
1798
1799 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001800 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001801 int width, int height, int densityDpi) {
1802 final long token = Binder.clearCallingIdentity();
1803 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001804 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001805 } finally {
1806 Binder.restoreCallingIdentity(token);
1807 }
1808 }
1809
1810 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001811 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001812 if (surface != null && surface.isSingleBuffered()) {
1813 throw new IllegalArgumentException("Surface can't be single-buffered");
1814 }
Jeff Brown92207df2014-04-16 13:16:07 -07001815 final long token = Binder.clearCallingIdentity();
1816 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001817 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001818 } finally {
1819 Binder.restoreCallingIdentity(token);
1820 }
1821 }
1822
1823 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001824 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001825 final long token = Binder.clearCallingIdentity();
1826 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001827 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001828 } finally {
1829 Binder.restoreCallingIdentity(token);
1830 }
1831 }
1832
1833 @Override // Binder call
1834 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001835 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001836
1837 final long token = Binder.clearCallingIdentity();
1838 try {
1839 dumpInternal(pw);
1840 } finally {
1841 Binder.restoreCallingIdentity(token);
1842 }
1843 }
1844
Kenny Guy22bd0442017-10-26 00:15:54 +01001845 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001846 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001847 mContext.enforceCallingOrSelfPermission(
1848 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1849 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001850
1851 final int callingUid = Binder.getCallingUid();
1852 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
1853 final int mode = appOpsManager.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
1854 callingUid, callingPackage);
1855 final boolean hasUsageStats;
1856 if (mode == AppOpsManager.MODE_DEFAULT) {
1857 // The default behavior here is to check if PackageManager has given the app
1858 // permission.
1859 hasUsageStats = mContext.checkCallingPermission(
1860 Manifest.permission.PACKAGE_USAGE_STATS)
1861 == PackageManager.PERMISSION_GRANTED;
1862 } else {
1863 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1864 }
1865
1866 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001867 final long token = Binder.clearCallingIdentity();
1868 try {
Michael Wright144aac92017-12-21 18:37:41 +00001869 synchronized (mSyncRoot) {
1870 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
1871 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001872 } finally {
1873 Binder.restoreCallingIdentity(token);
1874 }
1875 }
1876
Michael Wrighteef0e132017-11-21 17:57:52 +00001877 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00001878 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
1879 mContext.enforceCallingOrSelfPermission(
1880 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
1881 "Permission required to to access ambient light stats.");
1882 final int callingUid = Binder.getCallingUid();
1883 final int userId = UserHandle.getUserId(callingUid);
1884 final long token = Binder.clearCallingIdentity();
1885 try {
1886 synchronized (mSyncRoot) {
1887 return mDisplayPowerController.getAmbientBrightnessStats(userId);
1888 }
1889 } finally {
1890 Binder.restoreCallingIdentity(token);
1891 }
1892 }
1893
1894 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00001895 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00001896 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001897 mContext.enforceCallingOrSelfPermission(
1898 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1899 "Permission required to change the display's brightness configuration");
1900 if (userId != UserHandle.getCallingUserId()) {
1901 mContext.enforceCallingOrSelfPermission(
1902 Manifest.permission.INTERACT_ACROSS_USERS,
1903 "Permission required to change the display brightness"
1904 + " configuration of another user");
1905 }
Kenny Guy05ce8092018-01-17 13:44:20 +00001906 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
1907 packageName = null;
1908 }
Michael Wrighteef0e132017-11-21 17:57:52 +00001909 final long token = Binder.clearCallingIdentity();
1910 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001911 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001912 } finally {
1913 Binder.restoreCallingIdentity(token);
1914 }
1915 }
1916
Michael Wrightd8460232018-01-16 18:04:59 +00001917 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00001918 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
1919 mContext.enforceCallingOrSelfPermission(
1920 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1921 "Permission required to read the display's brightness configuration");
1922 if (userId != UserHandle.getCallingUserId()) {
1923 mContext.enforceCallingOrSelfPermission(
1924 Manifest.permission.INTERACT_ACROSS_USERS,
1925 "Permission required to read the display brightness"
1926 + " configuration of another user");
1927 }
1928 final long token = Binder.clearCallingIdentity();
1929 try {
1930 final int userSerial = getUserManager().getUserSerialNumber(userId);
1931 synchronized (mSyncRoot) {
1932 BrightnessConfiguration config =
1933 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1934 if (config == null) {
1935 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
1936 }
1937 return config;
1938 }
1939 } finally {
1940 Binder.restoreCallingIdentity(token);
1941 }
1942 }
1943
1944 @Override // Binder call
1945 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
1946 mContext.enforceCallingOrSelfPermission(
1947 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1948 "Permission required to read the display's default brightness configuration");
1949 final long token = Binder.clearCallingIdentity();
1950 try {
1951 synchronized (mSyncRoot) {
1952 return mDisplayPowerController.getDefaultBrightnessConfiguration();
1953 }
1954 } finally {
1955 Binder.restoreCallingIdentity(token);
1956 }
1957 }
1958
1959 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00001960 public void setTemporaryBrightness(int brightness) {
1961 mContext.enforceCallingOrSelfPermission(
1962 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
1963 "Permission required to set the display's brightness");
1964 final long token = Binder.clearCallingIdentity();
1965 try {
1966 synchronized (mSyncRoot) {
1967 mDisplayPowerController.setTemporaryBrightness(brightness);
1968 }
1969 } finally {
1970 Binder.restoreCallingIdentity(token);
1971 }
1972 }
1973
1974 @Override // Binder call
1975 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
1976 mContext.enforceCallingOrSelfPermission(
1977 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
1978 "Permission required to set the display's auto brightness adjustment");
1979 final long token = Binder.clearCallingIdentity();
1980 try {
1981 synchronized (mSyncRoot) {
1982 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
1983 }
1984 } finally {
1985 Binder.restoreCallingIdentity(token);
1986 }
1987 }
1988
Dan Gittik7a32fba2018-03-28 12:19:38 +01001989 @Override // Binder call
1990 public void onShellCommand(FileDescriptor in, FileDescriptor out,
1991 FileDescriptor err, String[] args, ShellCallback callback,
1992 ResultReceiver resultReceiver) {
1993 final long token = Binder.clearCallingIdentity();
1994 try {
1995 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
1996 command.exec(this, in, out, err, args, callback, resultReceiver);
1997 } finally {
1998 Binder.restoreCallingIdentity(token);
1999 }
2000 }
2001
2002 void setBrightness(int brightness) {
2003 Settings.System.putIntForUser(mContext.getContentResolver(),
2004 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2005 }
2006
2007 void resetBrightnessConfiguration() {
2008 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2009 mContext.getPackageName());
2010 }
2011
Jeff Brown4ccb8232014-01-16 22:16:42 -08002012 private boolean validatePackageName(int uid, String packageName) {
2013 if (packageName != null) {
2014 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2015 if (packageNames != null) {
2016 for (String n : packageNames) {
2017 if (n.equals(packageName)) {
2018 return true;
2019 }
2020 }
2021 }
2022 }
2023 return false;
2024 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002025
2026 private boolean canProjectVideo(IMediaProjection projection) {
2027 if (projection != null) {
2028 try {
2029 if (projection.canProjectVideo()) {
2030 return true;
2031 }
2032 } catch (RemoteException e) {
2033 Slog.e(TAG, "Unable to query projection service for permissions", e);
2034 }
2035 }
2036 if (mContext.checkCallingPermission(
2037 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002038 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002039 return true;
2040 }
2041 return canProjectSecureVideo(projection);
2042 }
2043
2044 private boolean canProjectSecureVideo(IMediaProjection projection) {
2045 if (projection != null) {
2046 try {
2047 if (projection.canProjectSecureVideo()){
2048 return true;
2049 }
2050 } catch (RemoteException e) {
2051 Slog.e(TAG, "Unable to query projection service for permissions", e);
2052 }
2053 }
2054 return mContext.checkCallingPermission(
2055 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002056 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002057 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002058 }
2059
2060 private final class LocalService extends DisplayManagerInternal {
2061 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002062 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002063 SensorManager sensorManager) {
2064 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002065 DisplayBlanker blanker = new DisplayBlanker() {
2066 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002067 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002068 // The order of operations is important for legacy reasons.
2069 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002070 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002071 }
2072
2073 callbacks.onDisplayStateChange(state);
2074
2075 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002076 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002077 }
2078 }
2079 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002080 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002081 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002082 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002083
2084 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002085 }
2086
2087 @Override
2088 public boolean requestPowerState(DisplayPowerRequest request,
2089 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002090 synchronized (mSyncRoot) {
2091 return mDisplayPowerController.requestPowerState(request,
2092 waitForNegativeProximity);
2093 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002094 }
2095
2096 @Override
2097 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002098 synchronized (mSyncRoot) {
2099 return mDisplayPowerController.isProximitySensorAvailable();
2100 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002101 }
2102
2103 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002104 public DisplayInfo getDisplayInfo(int displayId) {
2105 return getDisplayInfoInternal(displayId, Process.myUid());
2106 }
2107
2108 @Override
2109 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2110 if (listener == null) {
2111 throw new IllegalArgumentException("listener must not be null");
2112 }
2113
2114 registerDisplayTransactionListenerInternal(listener);
2115 }
2116
2117 @Override
2118 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2119 if (listener == null) {
2120 throw new IllegalArgumentException("listener must not be null");
2121 }
2122
2123 unregisterDisplayTransactionListenerInternal(listener);
2124 }
2125
2126 @Override
2127 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2128 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2129 }
2130
2131 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002132 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2133 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2134 }
2135
2136 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002137 public void performTraversal(SurfaceControl.Transaction t) {
2138 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002139 }
2140
2141 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002142 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002143 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2144 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2145 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002146 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002147
2148 @Override
2149 public void setDisplayOffsets(int displayId, int x, int y) {
2150 setDisplayOffsetsInternal(displayId, x, y);
2151 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002152
2153 @Override
2154 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2155 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2156 }
2157
2158 @Override
2159 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2160 return isUidPresentOnDisplayInternal(uid, displayId);
2161 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002162
2163 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002164 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002165 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002166 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002167 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002168 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002169
2170 @Override
2171 public void onOverlayChanged() {
2172 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002173 for (int i = 0; i < mDisplayDevices.size(); i++) {
2174 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002175 }
2176 }
2177 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002178 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002179}