blob: c32e2205877e720cf9089262960f994a9eedbced [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;
Michael Wrighteef0e132017-11-21 17:57:52 +000032import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000033import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070034import android.content.Context;
35import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010036import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010037import android.content.res.Resources;
38import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070039import android.hardware.SensorManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010040import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000041import android.hardware.display.BrightnessConfiguration;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080043import android.hardware.display.DisplayManagerInternal;
44import android.hardware.display.DisplayViewport;
45import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070046import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070047import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070048import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070049import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080050import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070051import android.media.projection.IMediaProjection;
52import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070053import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070054import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070055import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080056import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070057import android.os.Looper;
58import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070059import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080060import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070061import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070062import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070063import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070064import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070065import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010066import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000067import android.os.UserManager;
Jeff Browna506a6e2013-06-04 00:02:38 -070068import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080069import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070070import android.util.Slog;
71import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070072import android.view.Display;
73import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070074import android.view.Surface;
75
Michael Wrighteef0e132017-11-21 17:57:52 +000076import com.android.internal.util.Preconditions;
Jorim Jaggied7993b2017-03-28 18:50:01 +010077import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080078import com.android.server.DisplayThread;
79import com.android.server.LocalServices;
80import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070081import com.android.server.UiThread;
Adrian Roose99bc052017-11-20 17:55:31 +010082import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070083
84import java.io.FileDescriptor;
85import java.io.PrintWriter;
86import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070087import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070088import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080089import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070090
91/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070092 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070093 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070094 * The {@link DisplayManagerService} manages the global lifecycle of displays,
95 * decides how to configure logical displays based on the physical display devices currently
96 * attached, sends notifications to the system and to applications when the state
97 * changes, and so on.
98 * </p><p>
99 * The display manager service relies on a collection of {@link DisplayAdapter} components,
100 * for discovering and configuring physical display devices attached to the system.
101 * There are separate display adapters for each manner that devices are attached:
102 * one display adapter for built-in local displays, one for simulated non-functional
103 * displays when the system is headless, one for simulated overlay displays used for
104 * development, one for wifi displays, etc.
105 * </p><p>
106 * Display adapters are only weakly coupled to the display manager service.
107 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700108 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700109 * by the display manager service. This separation of concerns is important for
110 * two main reasons. First, it neatly encapsulates the responsibilities of these
111 * two classes: display adapters handle individual display devices whereas
112 * the display manager service handles the global state. Second, it eliminates
113 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700114 * </p>
115 *
116 * <h3>Synchronization</h3>
117 * <p>
118 * Because the display manager may be accessed by multiple threads, the synchronization
119 * story gets a little complicated. In particular, the window manager may call into
120 * the display manager while holding a surface transaction with the expectation that
121 * it can apply changes immediately. Unfortunately, that means we can't just do
122 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700123 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700124 * To make this work, all of the objects that belong to the display manager must
125 * use the same lock. We call this lock the synchronization root and it has a unique
126 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
127 * named with the "Locked" suffix.
128 * </p><p>
129 * Where things get tricky is that the display manager is not allowed to make
130 * any potentially reentrant calls, especially into the window manager. We generally
131 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700132 * </p>
133 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800134public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700135 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700136 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700137
Jeff Brownbbd28a22012-09-20 16:47:15 -0700138 // When this system property is set to 0, WFD is forcibly disabled on boot.
139 // When this system property is set to 1, WFD is forcibly enabled on boot.
140 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
141 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
142
Jeff Brownbd6e1502012-08-28 03:27:37 -0700143 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
144
Santos Cordonc22c5632017-06-21 16:03:49 -0700145 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700146 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
147 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700148 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700149 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100150 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000151 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700152
Jeff Brownb880d882014-02-10 19:47:07 -0800153 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700154 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700155 private final Handler mUiHandler;
156 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800157 private WindowManagerInternal mWindowManagerInternal;
158 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700159 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700160
161 // The synchronization root for the display manager.
162 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800163 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
164 // into WindowManagerService methods that require mWindowMap while holding this unless you are
165 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700166 private final SyncRoot mSyncRoot = new SyncRoot();
167
168 // True if in safe mode.
169 // This option may disable certain display adapters.
170 public boolean mSafeMode;
171
172 // True if we are in a special boot mode where only core applications and
173 // services should be started. This option may disable certain display adapters.
174 public boolean mOnlyCore;
175
Jeff Brown27f1d672012-10-17 18:32:34 -0700176 // True if the display manager service should pretend there is only one display
177 // and only tell applications about the existence of the default logical display.
178 // The display manager can still mirror content to secondary displays but applications
179 // cannot present unique content on those displays.
180 // Used for demonstration purposes only.
181 private final boolean mSingleDisplayDemoMode;
182
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700183 // All callback records indexed by calling process id.
184 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700185 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700186
Jeff Brownbd6e1502012-08-28 03:27:37 -0700187 // List of all currently registered display adapters.
188 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
189
190 // List of all currently connected display devices.
191 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
192
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700193 // List of all logical displays indexed by logical display id.
194 private final SparseArray<LogicalDisplay> mLogicalDisplays =
195 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700196 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
197
Jeff Brown7f3994e2012-12-04 14:04:28 -0800198 // List of all display transaction listeners.
199 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
200 new CopyOnWriteArrayList<DisplayTransactionListener>();
201
Jeff Brownad9ef192014-04-08 17:26:30 -0700202 // Display power controller.
203 private DisplayPowerController mDisplayPowerController;
204
Jeff Brown037c33e2014-04-09 00:31:55 -0700205 // The overall display state, independent of changes that might influence one
206 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700207 private int mGlobalDisplayState = Display.STATE_ON;
208
209 // The overall display brightness.
210 // For now, this only applies to the built-in display but we may split it up eventually.
211 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700212
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700213 // Set to true when there are pending display changes that have yet to be applied
214 // to the surface flinger state.
215 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700216
Jeff Browne08ae382012-09-07 20:36:36 -0700217 // The Wifi display adapter, or null if not registered.
218 private WifiDisplayAdapter mWifiDisplayAdapter;
219
Jeff Brownce468a32013-11-21 16:42:03 -0800220 // The number of active wifi display scan requests.
221 private int mWifiDisplayScanRequestCount;
222
Jeff Browna506a6e2013-06-04 00:02:38 -0700223 // The virtual display adapter, or null if not registered.
224 private VirtualDisplayAdapter mVirtualDisplayAdapter;
225
Michael Wrighteef0e132017-11-21 17:57:52 +0000226 // The User ID of the current user
227 private @UserIdInt int mCurrentUserId;
228
Michael Wrighteedcbf12017-08-16 23:14:54 +0100229 // The stable device screen height and width. These are not tied to a specific display, even
230 // the default display, because they need to be stable over the course of the device's entire
231 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
232 // device).
233 private Point mStableDisplaySize = new Point();
234
Jeff Brownd728bf52012-09-08 18:05:28 -0700235 // Viewports of the default display and the display that should receive touch
236 // input from an external source. Used by the input system.
237 private final DisplayViewport mDefaultViewport = new DisplayViewport();
238 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700239 private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700240
Jeff Brown89d55462012-09-19 11:33:42 -0700241 // Persistent data store for all internal settings maintained by the display manager service.
242 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
243
Jeff Brownbd6e1502012-08-28 03:27:37 -0700244 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700245 // May be used outside of the lock but only on the handler thread.
246 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700247
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700248 // Temporary display info, used for comparing display configurations.
249 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
250
Jeff Brownd728bf52012-09-08 18:05:28 -0700251 // Temporary viewports, used when sending new viewport information to the
252 // input system. May be used outside of the lock but only on the handler thread.
253 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
254 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
Santos Cordonee8931e2017-04-05 10:31:15 -0700255 private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700256
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700257 // The default color mode for default displays. Overrides the usual
258 // Display.Display.COLOR_MODE_DEFAULT for displays with the
259 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
260 private final int mDefaultDisplayDefaultColorMode;
261
Jeff Browne75926d2014-09-18 15:24:49 -0700262 // Temporary list of deferred work to perform when setting the display state.
263 // Only used by requestDisplayState. The field is self-synchronized and only
264 // intended for use inside of the requestGlobalDisplayStateInternal function.
265 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
266
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800267 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
268 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
269
Santos Cordonee8931e2017-04-05 10:31:15 -0700270 private final Injector mInjector;
271
Kenny Guy22bd0442017-10-26 00:15:54 +0100272 private final BrightnessTracker mBrightnessTracker;
273
Jeff Brownb880d882014-02-10 19:47:07 -0800274 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700275 this(context, new Injector());
276 }
277
278 @VisibleForTesting
279 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800280 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700281 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800282 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800283 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700284 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700285 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700286 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700287 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000288 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700289
290 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
291 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Kenny Guy22bd0442017-10-26 00:15:54 +0100292 mBrightnessTracker = new BrightnessTracker(context, null);
Michael Wrighteef0e132017-11-21 17:57:52 +0000293 mCurrentUserId = UserHandle.USER_SYSTEM;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700294 }
295
296 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100297 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000298 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
299 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100300 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
301 Process.THREAD_GROUP_TOP_APP);
302 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
303 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700304 }
305
Jeff Brown4ccb8232014-01-16 22:16:42 -0800306 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800307 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700308 // We need to pre-load the persistent data store so it's ready before the default display
309 // adapter is up so that we have it's configuration. We could load it lazily, but since
310 // 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 -0700311 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100312 synchronized(mSyncRoot) {
313 mPersistentDataStore.loadIfNeeded();
314 loadStableDisplayValuesLocked();
315 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700316 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800317
318 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
319 true /*allowIsolated*/);
320 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700321 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800322 }
323
324 @Override
325 public void onBootPhase(int phase) {
326 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
327 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700328 long timeout = SystemClock.uptimeMillis()
329 + mInjector.getDefaultDisplayDelayTimeout();
330 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
331 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800332 long delay = timeout - SystemClock.uptimeMillis();
333 if (delay <= 0) {
334 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700335 + "to be initialized. DefaultDisplay="
336 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
337 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800338 }
339 if (DEBUG) {
340 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
341 }
342 try {
343 mSyncRoot.wait(delay);
344 } catch (InterruptedException ex) {
345 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700346 }
347 }
348 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700349 }
350
Michael Wrighteef0e132017-11-21 17:57:52 +0000351 @Override
352 public void onSwitchUser(@UserIdInt int newUserId) {
353 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
354 synchronized (mSyncRoot) {
355 if (mCurrentUserId != newUserId) {
356 mCurrentUserId = newUserId;
357 BrightnessConfiguration config =
358 mPersistentDataStore.getBrightnessConfiguration(userSerial);
359 mDisplayPowerController.setBrightnessConfiguration(config);
360 }
361 }
362 }
363
Jeff Brown4ccb8232014-01-16 22:16:42 -0800364 // TODO: Use dependencies or a boot phase
365 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700366 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800367 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
368 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700369 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700370 }
371 }
372
373 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700374 * Called when the system is ready to go.
375 */
376 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700377 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700378 mSafeMode = safeMode;
379 mOnlyCore = onlyCore;
380 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700381
Jeff Brownbd6e1502012-08-28 03:27:37 -0700382 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100383 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700384 }
385
Santos Cordonee8931e2017-04-05 10:31:15 -0700386 @VisibleForTesting
387 Handler getDisplayHandler() {
388 return mHandler;
389 }
390
Michael Wrighteedcbf12017-08-16 23:14:54 +0100391 private void loadStableDisplayValuesLocked() {
392 final Point size = mPersistentDataStore.getStableDisplaySize();
393 if (size.x > 0 && size.y > 0) {
394 // Just set these values directly so we don't write the display persistent data again
395 // unnecessarily
396 mStableDisplaySize.set(size.x, size.y);
397 } else {
398 final Resources res = mContext.getResources();
399 final int width = res.getInteger(
400 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
401 final int height = res.getInteger(
402 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
403 if (width > 0 && height > 0) {
404 setStableDisplaySizeLocked(width, height);
405 }
406 }
407 }
408
409 private Point getStableDisplaySizeInternal() {
410 Point r = new Point();
411 synchronized (mSyncRoot) {
412 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
413 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
414 }
415 }
416 return r;
417 }
418
Jeff Brown4ccb8232014-01-16 22:16:42 -0800419 private void registerDisplayTransactionListenerInternal(
420 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800421 // List is self-synchronized copy-on-write.
422 mDisplayTransactionListeners.add(listener);
423 }
424
Jeff Brown4ccb8232014-01-16 22:16:42 -0800425 private void unregisterDisplayTransactionListenerInternal(
426 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800427 // List is self-synchronized copy-on-write.
428 mDisplayTransactionListeners.remove(listener);
429 }
430
Jeff Brown4ccb8232014-01-16 22:16:42 -0800431 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700432 int displayId, DisplayInfo info) {
433 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700434 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700435 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700436 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700437 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700438 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700439 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700440 }
441 }
442 }
443
Andrii Kuliancd097992017-03-23 18:31:59 -0700444 /**
445 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
446 */
447 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
448 synchronized (mSyncRoot) {
449 final LogicalDisplay display = mLogicalDisplays.get(displayId);
450 if (display != null) {
451 display.getNonOverrideDisplayInfoLocked(outInfo);
452 }
453 }
454 }
455
Santos Cordonee8931e2017-04-05 10:31:15 -0700456 @VisibleForTesting
457 void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700458 synchronized (mSyncRoot) {
459 if (!mPendingTraversal) {
460 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700461 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700462 mPendingTraversal = false;
463
464 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700465 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800466
467 // List is self-synchronized copy-on-write.
468 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
469 listener.onDisplayTransaction();
470 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700471 }
472
Jeff Brown5d6443b2015-04-10 20:15:01 -0700473 private void requestGlobalDisplayStateInternal(int state, int brightness) {
474 if (state == Display.STATE_UNKNOWN) {
475 state = Display.STATE_ON;
476 }
477 if (state == Display.STATE_OFF) {
478 brightness = PowerManager.BRIGHTNESS_OFF;
479 } else if (brightness < 0) {
480 brightness = PowerManager.BRIGHTNESS_DEFAULT;
481 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
482 brightness = PowerManager.BRIGHTNESS_ON;
483 }
484
Jeff Browne75926d2014-09-18 15:24:49 -0700485 synchronized (mTempDisplayStateWorkQueue) {
486 try {
487 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700488 // Note that we do not need to schedule traversals here although it
489 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700490 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700491 if (mGlobalDisplayState == state
492 && mGlobalDisplayBrightness == brightness) {
493 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700494 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700495
496 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
497 + Display.stateToString(state)
498 + ", brightness=" + brightness + ")");
499 mGlobalDisplayState = state;
500 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700501 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700502 }
503
504 // Setting the display power state can take hundreds of milliseconds
505 // to complete so we defer the most expensive part of the work until
506 // after we have exited the critical section to avoid blocking other
507 // threads for a long time.
508 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
509 mTempDisplayStateWorkQueue.get(i).run();
510 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700511 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700512 } finally {
513 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700514 }
515 }
516 }
517
Jeff Brown4ccb8232014-01-16 22:16:42 -0800518 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700519 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800520 LogicalDisplay display = mLogicalDisplays.get(displayId);
521 if (display != null) {
522 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800523 if (info.hasAccess(callingUid)
524 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800525 return info;
526 }
527 }
528 return null;
529 }
530 }
531
532 private int[] getDisplayIdsInternal(int callingUid) {
533 synchronized (mSyncRoot) {
534 final int count = mLogicalDisplays.size();
535 int[] displayIds = new int[count];
536 int n = 0;
537 for (int i = 0; i < count; i++) {
538 LogicalDisplay display = mLogicalDisplays.valueAt(i);
539 DisplayInfo info = display.getDisplayInfoLocked();
540 if (info.hasAccess(callingUid)) {
541 displayIds[n++] = mLogicalDisplays.keyAt(i);
542 }
543 }
544 if (n != count) {
545 displayIds = Arrays.copyOfRange(displayIds, 0, n);
546 }
547 return displayIds;
548 }
549 }
550
551 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
552 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700553 if (mCallbacks.get(callingPid) != null) {
554 throw new SecurityException("The calling process has already "
555 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700556 }
557
Jeff Brownbd6e1502012-08-28 03:27:37 -0700558 CallbackRecord record = new CallbackRecord(callingPid, callback);
559 try {
560 IBinder binder = callback.asBinder();
561 binder.linkToDeath(record, 0);
562 } catch (RemoteException ex) {
563 // give up
564 throw new RuntimeException(ex);
565 }
566
567 mCallbacks.put(callingPid, record);
568 }
569 }
570
Jeff Brownce468a32013-11-21 16:42:03 -0800571 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700572 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800573 mCallbacks.remove(record.mPid);
574 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700575 }
576 }
577
Jeff Brown4ccb8232014-01-16 22:16:42 -0800578 private void startWifiDisplayScanInternal(int callingPid) {
579 synchronized (mSyncRoot) {
580 CallbackRecord record = mCallbacks.get(callingPid);
581 if (record == null) {
582 throw new IllegalStateException("The calling process has not "
583 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700584 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800585 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700586 }
587 }
588
Jeff Brownce468a32013-11-21 16:42:03 -0800589 private void startWifiDisplayScanLocked(CallbackRecord record) {
590 if (!record.mWifiDisplayScanRequested) {
591 record.mWifiDisplayScanRequested = true;
592 if (mWifiDisplayScanRequestCount++ == 0) {
593 if (mWifiDisplayAdapter != null) {
594 mWifiDisplayAdapter.requestStartScanLocked();
595 }
596 }
597 }
598 }
599
Jeff Brown4ccb8232014-01-16 22:16:42 -0800600 private void stopWifiDisplayScanInternal(int callingPid) {
601 synchronized (mSyncRoot) {
602 CallbackRecord record = mCallbacks.get(callingPid);
603 if (record == null) {
604 throw new IllegalStateException("The calling process has not "
605 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800606 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800607 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800608 }
609 }
610
611 private void stopWifiDisplayScanLocked(CallbackRecord record) {
612 if (record.mWifiDisplayScanRequested) {
613 record.mWifiDisplayScanRequested = false;
614 if (--mWifiDisplayScanRequestCount == 0) {
615 if (mWifiDisplayAdapter != null) {
616 mWifiDisplayAdapter.requestStopScanLocked();
617 }
618 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700619 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800620 + mWifiDisplayScanRequestCount);
621 mWifiDisplayScanRequestCount = 0;
622 }
623 }
624 }
625
Jeff Brown4ccb8232014-01-16 22:16:42 -0800626 private void connectWifiDisplayInternal(String address) {
627 synchronized (mSyncRoot) {
628 if (mWifiDisplayAdapter != null) {
629 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700630 }
Jeff Browne08ae382012-09-07 20:36:36 -0700631 }
632 }
633
Jeff Brown4ccb8232014-01-16 22:16:42 -0800634 private void pauseWifiDisplayInternal() {
635 synchronized (mSyncRoot) {
636 if (mWifiDisplayAdapter != null) {
637 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700638 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700639 }
640 }
641
Jeff Brown4ccb8232014-01-16 22:16:42 -0800642 private void resumeWifiDisplayInternal() {
643 synchronized (mSyncRoot) {
644 if (mWifiDisplayAdapter != null) {
645 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700646 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700647 }
648 }
649
Jeff Brown4ccb8232014-01-16 22:16:42 -0800650 private void disconnectWifiDisplayInternal() {
651 synchronized (mSyncRoot) {
652 if (mWifiDisplayAdapter != null) {
653 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700654 }
Jeff Browne08ae382012-09-07 20:36:36 -0700655 }
656 }
657
Jeff Brown4ccb8232014-01-16 22:16:42 -0800658 private void renameWifiDisplayInternal(String address, String alias) {
659 synchronized (mSyncRoot) {
660 if (mWifiDisplayAdapter != null) {
661 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700662 }
Jeff Brown89d55462012-09-19 11:33:42 -0700663 }
664 }
665
Jeff Brown4ccb8232014-01-16 22:16:42 -0800666 private void forgetWifiDisplayInternal(String address) {
667 synchronized (mSyncRoot) {
668 if (mWifiDisplayAdapter != null) {
669 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700670 }
Jeff Brown89d55462012-09-19 11:33:42 -0700671 }
672 }
673
Jeff Brown4ccb8232014-01-16 22:16:42 -0800674 private WifiDisplayStatus getWifiDisplayStatusInternal() {
675 synchronized (mSyncRoot) {
676 if (mWifiDisplayAdapter != null) {
677 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700678 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800679 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700680 }
681 }
682
Michael Wright1c9977b2016-07-12 13:30:10 -0700683 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100684 synchronized (mSyncRoot) {
685 LogicalDisplay display = mLogicalDisplays.get(displayId);
686 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700687 display.getRequestedColorModeLocked() != colorMode) {
688 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100689 scheduleTraversalLocked(false);
690 }
691 }
692 }
693
Michael Wright75ee9fc2014-09-01 19:55:22 -0700694 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700695 IMediaProjection projection, int callingUid, String packageName, String name, int width,
696 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800697 synchronized (mSyncRoot) {
698 if (mVirtualDisplayAdapter == null) {
699 Slog.w(TAG, "Rejecting request to create private virtual display "
700 + "because the virtual display adapter is not available.");
701 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700702 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800703
704 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700705 callback, projection, callingUid, packageName, name, width, height, densityDpi,
706 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707 if (device == null) {
708 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700709 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700710
Jeff Brown4ccb8232014-01-16 22:16:42 -0800711 handleDisplayDeviceAddedLocked(device);
712 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
713 if (display != null) {
714 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700715 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800716
717 // Something weird happened and the logical display was not created.
718 Slog.w(TAG, "Rejecting request to create virtual display "
719 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700720 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800721 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700722 }
723 return -1;
724 }
725
Michael Wright01e840f2014-06-26 16:03:25 -0700726 private void resizeVirtualDisplayInternal(IBinder appToken,
727 int width, int height, int densityDpi) {
728 synchronized (mSyncRoot) {
729 if (mVirtualDisplayAdapter == null) {
730 return;
731 }
732
733 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
734 }
735 }
736
Jeff Brown92207df2014-04-16 13:16:07 -0700737 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
738 synchronized (mSyncRoot) {
739 if (mVirtualDisplayAdapter == null) {
740 return;
741 }
742
743 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
744 }
745 }
746
Jeff Brown4ccb8232014-01-16 22:16:42 -0800747 private void releaseVirtualDisplayInternal(IBinder appToken) {
748 synchronized (mSyncRoot) {
749 if (mVirtualDisplayAdapter == null) {
750 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700751 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700752
Jeff Brown4ccb8232014-01-16 22:16:42 -0800753 DisplayDevice device =
754 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
755 if (device != null) {
756 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700757 }
758 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700759 }
760
Santos Cordonc22c5632017-06-21 16:03:49 -0700761 private void registerDefaultDisplayAdapters() {
762 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700763 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700764 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800765 registerDisplayAdapterLocked(new LocalDisplayAdapter(
766 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700767
768 // Standalone VR devices rely on a virtual display as their primary display for
769 // 2D UI. We register virtual display adapter along side the main display adapter
770 // here so that it is ready by the time the system sends the home Intent for
771 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
772 // the virtual display inside VR before any VR-specific apps even run.
773 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
774 mHandler, mDisplayAdapterListener);
775 if (mVirtualDisplayAdapter != null) {
776 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
777 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700778 }
779 }
780
781 private void registerAdditionalDisplayAdapters() {
782 synchronized (mSyncRoot) {
783 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700784 registerOverlayDisplayAdapterLocked();
785 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700786 }
787 }
788 }
789
Jeff Brown89d55462012-09-19 11:33:42 -0700790 private void registerOverlayDisplayAdapterLocked() {
791 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
792 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
793 }
794
795 private void registerWifiDisplayAdapterLocked() {
796 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700797 com.android.internal.R.bool.config_enableWifiDisplay)
798 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700799 mWifiDisplayAdapter = new WifiDisplayAdapter(
800 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
801 mPersistentDataStore);
802 registerDisplayAdapterLocked(mWifiDisplayAdapter);
803 }
804 }
805
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700806 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
807 // In safe mode, we disable non-essential display adapters to give the user
808 // an opportunity to fix broken settings or other problems that might affect
809 // system stability.
810 // In only-core mode, we disable non-essential display adapters to minimize
811 // the number of dependencies that are started while in this mode and to
812 // prevent problems that might occur due to the device being encrypted.
813 return !mSafeMode && !mOnlyCore;
814 }
815
816 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
817 mDisplayAdapters.add(adapter);
818 adapter.registerLocked();
819 }
820
Jeff Brownbd6e1502012-08-28 03:27:37 -0700821 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700822 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700823 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700824 }
825 }
826
Jeff Browna506a6e2013-06-04 00:02:38 -0700827 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700828 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700829 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700830 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700831 return;
832 }
833
Jeff Brown10acf6d2015-04-14 14:20:47 -0700834 Slog.i(TAG, "Display device added: " + info);
835 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700836
837 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700838 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700839 Runnable work = updateDisplayStateLocked(device);
840 if (work != null) {
841 work.run();
842 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700843 scheduleTraversalLocked(false);
844 }
845
Jeff Brownbd6e1502012-08-28 03:27:37 -0700846 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700847 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700848 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700849 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700850 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700851 return;
852 }
853
Jeff Brown10acf6d2015-04-14 14:20:47 -0700854 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
855 if (diff == DisplayDeviceInfo.DIFF_STATE) {
856 Slog.i(TAG, "Display device changed state: \"" + info.name
857 + "\", " + Display.stateToString(info.state));
858 } else if (diff != 0) {
859 Slog.i(TAG, "Display device changed: " + info);
860 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700861 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
862 try {
863 mPersistentDataStore.setColorMode(device, info.colorMode);
864 } finally {
865 mPersistentDataStore.saveIfNeeded();
866 }
867 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700868 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700869
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700870 device.applyPendingDisplayDeviceInfoChangesLocked();
871 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700872 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700873 }
874 }
875 }
876
Jeff Brownbd6e1502012-08-28 03:27:37 -0700877 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700878 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700879 handleDisplayDeviceRemovedLocked(device);
880 }
881 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700882
Jeff Browna506a6e2013-06-04 00:02:38 -0700883 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700884 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700885 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700886 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700887 return;
888 }
889
Jeff Brown10acf6d2015-04-14 14:20:47 -0700890 Slog.i(TAG, "Display device removed: " + info);
891 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700892
Jeff Browna506a6e2013-06-04 00:02:38 -0700893 updateLogicalDisplaysLocked();
894 scheduleTraversalLocked(false);
895 }
896
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700897 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700898 final int count = mDisplayDevices.size();
899 for (int i = 0; i < count; i++) {
900 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700901 Runnable runnable = updateDisplayStateLocked(device);
902 if (runnable != null) {
903 workQueue.add(runnable);
904 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700905 }
906 }
907
Jeff Browne75926d2014-09-18 15:24:49 -0700908 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700909 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700910 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700911 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
912 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700913 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700914 }
Jeff Browne75926d2014-09-18 15:24:49 -0700915 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700916 }
917
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700918 // Adds a new logical display based on the given display device.
919 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700920 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
922 boolean isDefault = (deviceInfo.flags
923 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
924 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
925 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
926 isDefault = false;
927 }
928
Jeff Brown27f1d672012-10-17 18:32:34 -0700929 if (!isDefault && mSingleDisplayDemoMode) {
930 Slog.i(TAG, "Not creating a logical display for a secondary display "
931 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700932 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700933 }
934
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700935 final int displayId = assignDisplayIdLocked(isDefault);
936 final int layerStack = assignLayerStackLocked(displayId);
937
Jeff Brownd728bf52012-09-08 18:05:28 -0700938 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700939 display.updateLocked(mDisplayDevices);
940 if (!display.isValidLocked()) {
941 // This should never happen currently.
942 Slog.w(TAG, "Ignoring display device because the logical display "
943 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700944 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700945 }
946
Michael Wrighteedcbf12017-08-16 23:14:54 +0100947 configureColorModeLocked(display, device);
948 if (isDefault) {
949 recordStableDisplayStatsIfNeededLocked(display);
950 }
951
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700952 mLogicalDisplays.put(displayId, display);
953
954 // Wake up waitForDefaultDisplay.
955 if (isDefault) {
956 mSyncRoot.notifyAll();
957 }
958
959 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700960 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700961 }
962
963 private int assignDisplayIdLocked(boolean isDefault) {
964 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
965 }
966
967 private int assignLayerStackLocked(int displayId) {
968 // Currently layer stacks and display ids are the same.
969 // This need not be the case.
970 return displayId;
971 }
972
Michael Wrighteedcbf12017-08-16 23:14:54 +0100973 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
974 if (display.getPrimaryDisplayDeviceLocked() == device) {
975 int colorMode = mPersistentDataStore.getColorMode(device);
976 if (colorMode == Display.COLOR_MODE_INVALID) {
977 if ((device.getDisplayDeviceInfoLocked().flags
978 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
979 colorMode = mDefaultDisplayDefaultColorMode;
980 } else {
981 colorMode = Display.COLOR_MODE_DEFAULT;
982 }
983 }
984 display.setRequestedColorModeLocked(colorMode);
985 }
986 }
987
988 // If we've never recorded stable device stats for this device before and they aren't
989 // explicitly configured, go ahead and record the stable device stats now based on the status
990 // of the default display at first boot.
991 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
992 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
993 DisplayInfo info = d.getDisplayInfoLocked();
994 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
995 }
996 }
997
998 private void setStableDisplaySizeLocked(int width, int height) {
999 mStableDisplaySize = new Point(width, height);
1000 try {
1001 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1002 } finally {
1003 mPersistentDataStore.saveIfNeeded();
1004 }
1005 }
1006
Michael Wrighteef0e132017-11-21 17:57:52 +00001007 private void setBrightnessConfigurationForUserInternal(
1008 @NonNull BrightnessConfiguration c, @UserIdInt int userId) {
1009 final int userSerial = getUserManager().getUserSerialNumber(userId);
1010 synchronized (mSyncRoot) {
1011 try {
1012 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial);
1013 } finally {
1014 mPersistentDataStore.saveIfNeeded();
1015 }
1016 if (userId == mCurrentUserId) {
1017 mDisplayPowerController.setBrightnessConfiguration(c);
1018 }
1019 }
1020 }
1021
1022 private void loadBrightnessConfiguration() {
1023 synchronized (mSyncRoot) {
1024 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1025 BrightnessConfiguration config =
1026 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1027 mDisplayPowerController.setBrightnessConfiguration(config);
1028 }
1029 }
1030
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001031 // Updates all existing logical displays given the current set of display devices.
1032 // Removes invalid logical displays.
1033 // Sends notifications if needed.
1034 private boolean updateLogicalDisplaysLocked() {
1035 boolean changed = false;
1036 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1037 final int displayId = mLogicalDisplays.keyAt(i);
1038 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1039
1040 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1041 display.updateLocked(mDisplayDevices);
1042 if (!display.isValidLocked()) {
1043 mLogicalDisplays.removeAt(i);
1044 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1045 changed = true;
1046 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
1047 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1048 changed = true;
1049 }
1050 }
1051 return changed;
1052 }
1053
1054 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -07001055 // Clear all viewports before configuring displays so that we can keep
1056 // track of which ones we have configured.
1057 clearViewportsLocked();
1058
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001059 // Configure each display device.
1060 final int count = mDisplayDevices.size();
1061 for (int i = 0; i < count; i++) {
1062 DisplayDevice device = mDisplayDevices.get(i);
1063 configureDisplayInTransactionLocked(device);
1064 device.performTraversalInTransactionLocked();
1065 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001066
1067 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001068 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001069 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1070 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001071 }
1072
Michael Wright3f145a22014-07-22 19:46:03 -07001073 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001074 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001075 synchronized (mSyncRoot) {
1076 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001077 if (display == null) {
1078 return;
1079 }
1080 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001081 if (DEBUG) {
1082 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1083 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1084 }
1085
Craig Mautner722285e2012-09-07 13:55:58 -07001086 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001087 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001088 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001089 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1090 // Scan supported modes returned by display.getInfo() to find a mode with the same
1091 // size as the default display mode but with the specified refresh rate instead.
1092 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1093 requestedRefreshRate);
1094 }
1095 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001096 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001097 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001098 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001099 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001100 scheduleTraversalLocked(inTraversal);
1101 }
Craig Mautner722285e2012-09-07 13:55:58 -07001102 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001103 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001104
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001105 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1106 synchronized (mSyncRoot) {
1107 LogicalDisplay display = mLogicalDisplays.get(displayId);
1108 if (display == null) {
1109 return;
1110 }
1111 if (display.getDisplayOffsetXLocked() != x
1112 || display.getDisplayOffsetYLocked() != y) {
1113 if (DEBUG) {
1114 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1115 + x + ", " + y + ")");
1116 }
1117 display.setDisplayOffsetsLocked(x, y);
1118 scheduleTraversalLocked(false);
1119 }
1120 }
1121 }
1122
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001123 // Updates the lists of UIDs that are present on displays.
1124 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1125 synchronized (mSyncRoot) {
1126 mDisplayAccessUIDs.clear();
1127 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1128 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1129 newDisplayAccessUIDs.valueAt(i));
1130 }
1131 }
1132 }
1133
1134 // Checks if provided UID's content is present on the display and UID has access to it.
1135 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1136 synchronized (mSyncRoot) {
1137 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1138 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1139 }
1140 }
1141
Jeff Brownd728bf52012-09-08 18:05:28 -07001142 private void clearViewportsLocked() {
1143 mDefaultViewport.valid = false;
1144 mExternalTouchViewport.valid = false;
Santos Cordonee8931e2017-04-05 10:31:15 -07001145 mVirtualTouchViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001146 }
1147
1148 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001149 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1150 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001151
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001152 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001153 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001154 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001155 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001156 if (display != null && !display.hasContentLocked()) {
1157 // If the display does not have any content of its own, then
1158 // automatically mirror the default logical display contents.
1159 display = null;
1160 }
1161 if (display == null) {
1162 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1163 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001164 }
1165
1166 // Apply the logical display configuration to the display device.
1167 if (display == null) {
1168 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001169 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001170 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001171 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001172 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001173 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -07001174
1175 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07001176 if (!mDefaultViewport.valid
1177 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1178 setViewportLocked(mDefaultViewport, display, device);
1179 }
1180 if (!mExternalTouchViewport.valid
1181 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1182 setViewportLocked(mExternalTouchViewport, display, device);
1183 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001184
1185 if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) {
1186 final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId);
1187 setViewportLocked(viewport, display, device);
1188 }
1189 }
1190
1191 /** Gets the virtual device viewport or creates it if not yet created. */
1192 private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) {
1193 DisplayViewport viewport;
1194 final int count = mVirtualTouchViewports.size();
1195 for (int i = 0; i < count; i++) {
1196 viewport = mVirtualTouchViewports.get(i);
1197 if (uniqueId.equals(viewport.uniqueId)) {
1198 return viewport;
1199 }
1200 }
1201
1202 viewport = new DisplayViewport();
1203 viewport.uniqueId = uniqueId;
1204 mVirtualTouchViewports.add(viewport);
1205 return viewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07001206 }
1207
1208 private static void setViewportLocked(DisplayViewport viewport,
1209 LogicalDisplay display, DisplayDevice device) {
1210 viewport.valid = true;
1211 viewport.displayId = display.getDisplayIdLocked();
1212 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001213 }
1214
1215 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1216 final int count = mLogicalDisplays.size();
1217 for (int i = 0; i < count; i++) {
1218 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1219 if (display.getPrimaryDisplayDeviceLocked() == device) {
1220 return display;
1221 }
1222 }
1223 return null;
1224 }
1225
Jeff Brownbd6e1502012-08-28 03:27:37 -07001226 private void sendDisplayEventLocked(int displayId, int event) {
1227 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1228 mHandler.sendMessage(msg);
1229 }
1230
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001231 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1232 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001233 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001234 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001235 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001236 if (!inTraversal) {
1237 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1238 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001239 }
1240 }
1241
1242 // Runs on Handler thread.
1243 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001244 private void deliverDisplayEvent(int displayId, int event) {
1245 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001246 Slog.d(TAG, "Delivering display event: displayId="
1247 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001248 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001249
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001250 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001251 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001252 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001253 count = mCallbacks.size();
1254 mTempCallbacks.clear();
1255 for (int i = 0; i < count; i++) {
1256 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001257 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001258 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001259
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001260 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001261 for (int i = 0; i < count; i++) {
1262 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1263 }
1264 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001265 }
1266
Michael Wrightc39d47a2014-07-08 18:07:36 -07001267 private IMediaProjectionManager getProjectionService() {
1268 if (mProjectionService == null) {
1269 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1270 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1271 }
1272 return mProjectionService;
1273 }
1274
Michael Wrighteef0e132017-11-21 17:57:52 +00001275 private UserManager getUserManager() {
1276 return mContext.getSystemService(UserManager.class);
1277 }
1278
Jeff Brown4ccb8232014-01-16 22:16:42 -08001279 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001280 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001281
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001282 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001283 pw.println(" mOnlyCode=" + mOnlyCore);
1284 pw.println(" mSafeMode=" + mSafeMode);
1285 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001286 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001287 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1288 pw.println(" mDefaultViewport=" + mDefaultViewport);
1289 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001290 pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001291 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001292 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001293 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001294 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1295
Jeff Brown9e316a12012-10-08 19:17:06 -07001296
Jeff Brownbd6e1502012-08-28 03:27:37 -07001297 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001298 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001299
1300 pw.println();
1301 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001302 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001303 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001304 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001305 }
Craig Mautner9de49362012-08-02 14:30:30 -07001306
Jeff Brownbd6e1502012-08-28 03:27:37 -07001307 pw.println();
1308 pw.println("Display Devices: size=" + mDisplayDevices.size());
1309 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001310 pw.println(" " + device.getDisplayDeviceInfoLocked());
1311 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001312 }
1313
1314 final int logicalDisplayCount = mLogicalDisplays.size();
1315 pw.println();
1316 pw.println("Logical Displays: size=" + logicalDisplayCount);
1317 for (int i = 0; i < logicalDisplayCount; i++) {
1318 int displayId = mLogicalDisplays.keyAt(i);
1319 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1320 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001321 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001322 }
Jeff Brownce468a32013-11-21 16:42:03 -08001323
1324 final int callbackCount = mCallbacks.size();
1325 pw.println();
1326 pw.println("Callbacks: size=" + callbackCount);
1327 for (int i = 0; i < callbackCount; i++) {
1328 CallbackRecord callback = mCallbacks.valueAt(i);
1329 pw.println(" " + i + ": mPid=" + callback.mPid
1330 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1331 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001332
1333 if (mDisplayPowerController != null) {
1334 mDisplayPowerController.dump(pw);
1335 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001336
1337 pw.println();
1338 mPersistentDataStore.dump(pw);
Kenny Guycfe7b702017-11-14 21:04:58 +00001339
1340 pw.println();
1341 mBrightnessTracker.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001342 }
1343 }
1344
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001345 /**
1346 * This is the object that everything in the display manager locks on.
1347 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1348 * clear that the object belongs to the display manager service and that it is
1349 * a unique object with a special purpose.
1350 */
1351 public static final class SyncRoot {
1352 }
1353
Santos Cordonee8931e2017-04-05 10:31:15 -07001354 @VisibleForTesting
1355 static class Injector {
1356 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1357 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1358 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1359 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001360
1361 long getDefaultDisplayDelayTimeout() {
1362 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1363 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001364 }
1365
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001366 @VisibleForTesting
1367 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1368 synchronized (mSyncRoot) {
1369 LogicalDisplay display = mLogicalDisplays.get(displayId);
1370 if (display != null) {
1371 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1372 return displayDevice.getDisplayDeviceInfoLocked();
1373 }
1374 return null;
1375 }
1376 }
1377
Jeff Brownbd6e1502012-08-28 03:27:37 -07001378 private final class DisplayManagerHandler extends Handler {
1379 public DisplayManagerHandler(Looper looper) {
1380 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001381 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001382
Jeff Brownbd6e1502012-08-28 03:27:37 -07001383 @Override
1384 public void handleMessage(Message msg) {
1385 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001386 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1387 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001388 break;
1389
1390 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1391 registerAdditionalDisplayAdapters();
1392 break;
1393
1394 case MSG_DELIVER_DISPLAY_EVENT:
1395 deliverDisplayEvent(msg.arg1, msg.arg2);
1396 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001397
1398 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001399 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001400 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001401
1402 case MSG_UPDATE_VIEWPORT: {
1403 synchronized (mSyncRoot) {
1404 mTempDefaultViewport.copyFrom(mDefaultViewport);
1405 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
Santos Cordonee8931e2017-04-05 10:31:15 -07001406 if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) {
1407 mTempVirtualTouchViewports.clear();
1408 for (DisplayViewport d : mVirtualTouchViewports) {
1409 mTempVirtualTouchViewports.add(d.makeCopy());
1410 }
1411 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001412 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001413 mInputManagerInternal.setDisplayViewports(mTempDefaultViewport,
1414 mTempExternalTouchViewport, mTempVirtualTouchViewports);
Jeff Brownd728bf52012-09-08 18:05:28 -07001415 break;
1416 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001417
1418 case MSG_REGISTER_BRIGHTNESS_TRACKER:
1419 mBrightnessTracker.start();
1420 break;
Michael Wrighteef0e132017-11-21 17:57:52 +00001421
1422 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1423 loadBrightnessConfiguration();
1424 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001425 }
1426 }
1427 }
1428
1429 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1430 @Override
1431 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1432 switch (event) {
1433 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1434 handleDisplayDeviceAdded(device);
1435 break;
1436
1437 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1438 handleDisplayDeviceChanged(device);
1439 break;
1440
1441 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1442 handleDisplayDeviceRemoved(device);
1443 break;
1444 }
1445 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001446
1447 @Override
1448 public void onTraversalRequested() {
1449 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001450 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001451 }
1452 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001453 }
1454
1455 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001456 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001457 private final IDisplayManagerCallback mCallback;
1458
Jeff Brownce468a32013-11-21 16:42:03 -08001459 public boolean mWifiDisplayScanRequested;
1460
Jeff Brownbd6e1502012-08-28 03:27:37 -07001461 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1462 mPid = pid;
1463 mCallback = callback;
1464 }
1465
1466 @Override
1467 public void binderDied() {
1468 if (DEBUG) {
1469 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1470 }
Jeff Brownce468a32013-11-21 16:42:03 -08001471 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001472 }
1473
1474 public void notifyDisplayEventAsync(int displayId, int event) {
1475 try {
1476 mCallback.onDisplayEvent(displayId, event);
1477 } catch (RemoteException ex) {
1478 Slog.w(TAG, "Failed to notify process "
1479 + mPid + " that displays changed, assuming it died.", ex);
1480 binderDied();
1481 }
1482 }
1483 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001484
Santos Cordonee8931e2017-04-05 10:31:15 -07001485 @VisibleForTesting
1486 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001487 /**
1488 * Returns information about the specified logical display.
1489 *
1490 * @param displayId The logical display id.
1491 * @return The logical display info, or null if the display does not exist. The
1492 * returned object must be treated as immutable.
1493 */
1494 @Override // Binder call
1495 public DisplayInfo getDisplayInfo(int displayId) {
1496 final int callingUid = Binder.getCallingUid();
1497 final long token = Binder.clearCallingIdentity();
1498 try {
1499 return getDisplayInfoInternal(displayId, callingUid);
1500 } finally {
1501 Binder.restoreCallingIdentity(token);
1502 }
1503 }
1504
1505 /**
1506 * Returns the list of all display ids.
1507 */
1508 @Override // Binder call
1509 public int[] getDisplayIds() {
1510 final int callingUid = Binder.getCallingUid();
1511 final long token = Binder.clearCallingIdentity();
1512 try {
1513 return getDisplayIdsInternal(callingUid);
1514 } finally {
1515 Binder.restoreCallingIdentity(token);
1516 }
1517 }
1518
Michael Wrighteedcbf12017-08-16 23:14:54 +01001519 /**
1520 * Returns the stable device display size, in pixels.
1521 */
1522 @Override // Binder call
1523 public Point getStableDisplaySize() {
1524 final long token = Binder.clearCallingIdentity();
1525 try {
1526 return getStableDisplaySizeInternal();
1527 } finally {
1528 Binder.restoreCallingIdentity(token);
1529 }
1530 }
1531
Jeff Brown4ccb8232014-01-16 22:16:42 -08001532 @Override // Binder call
1533 public void registerCallback(IDisplayManagerCallback callback) {
1534 if (callback == null) {
1535 throw new IllegalArgumentException("listener must not be null");
1536 }
1537
1538 final int callingPid = Binder.getCallingPid();
1539 final long token = Binder.clearCallingIdentity();
1540 try {
1541 registerCallbackInternal(callback, callingPid);
1542 } finally {
1543 Binder.restoreCallingIdentity(token);
1544 }
1545 }
1546
1547 @Override // Binder call
1548 public void startWifiDisplayScan() {
1549 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1550 "Permission required to start wifi display scans");
1551
1552 final int callingPid = Binder.getCallingPid();
1553 final long token = Binder.clearCallingIdentity();
1554 try {
1555 startWifiDisplayScanInternal(callingPid);
1556 } finally {
1557 Binder.restoreCallingIdentity(token);
1558 }
1559 }
1560
1561 @Override // Binder call
1562 public void stopWifiDisplayScan() {
1563 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1564 "Permission required to stop wifi display scans");
1565
1566 final int callingPid = Binder.getCallingPid();
1567 final long token = Binder.clearCallingIdentity();
1568 try {
1569 stopWifiDisplayScanInternal(callingPid);
1570 } finally {
1571 Binder.restoreCallingIdentity(token);
1572 }
1573 }
1574
1575 @Override // Binder call
1576 public void connectWifiDisplay(String address) {
1577 if (address == null) {
1578 throw new IllegalArgumentException("address must not be null");
1579 }
1580 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1581 "Permission required to connect to a wifi display");
1582
1583 final long token = Binder.clearCallingIdentity();
1584 try {
1585 connectWifiDisplayInternal(address);
1586 } finally {
1587 Binder.restoreCallingIdentity(token);
1588 }
1589 }
1590
1591 @Override // Binder call
1592 public void disconnectWifiDisplay() {
1593 // This request does not require special permissions.
1594 // Any app can request disconnection from the currently active wifi display.
1595 // This exception should no longer be needed once wifi display control moves
1596 // to the media router service.
1597
1598 final long token = Binder.clearCallingIdentity();
1599 try {
1600 disconnectWifiDisplayInternal();
1601 } finally {
1602 Binder.restoreCallingIdentity(token);
1603 }
1604 }
1605
1606 @Override // Binder call
1607 public void renameWifiDisplay(String address, String alias) {
1608 if (address == null) {
1609 throw new IllegalArgumentException("address must not be null");
1610 }
1611 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1612 "Permission required to rename to a wifi display");
1613
1614 final long token = Binder.clearCallingIdentity();
1615 try {
1616 renameWifiDisplayInternal(address, alias);
1617 } finally {
1618 Binder.restoreCallingIdentity(token);
1619 }
1620 }
1621
1622 @Override // Binder call
1623 public void forgetWifiDisplay(String address) {
1624 if (address == null) {
1625 throw new IllegalArgumentException("address must not be null");
1626 }
1627 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1628 "Permission required to forget to a wifi display");
1629
1630 final long token = Binder.clearCallingIdentity();
1631 try {
1632 forgetWifiDisplayInternal(address);
1633 } finally {
1634 Binder.restoreCallingIdentity(token);
1635 }
1636 }
1637
1638 @Override // Binder call
1639 public void pauseWifiDisplay() {
1640 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1641 "Permission required to pause a wifi display session");
1642
1643 final long token = Binder.clearCallingIdentity();
1644 try {
1645 pauseWifiDisplayInternal();
1646 } finally {
1647 Binder.restoreCallingIdentity(token);
1648 }
1649 }
1650
1651 @Override // Binder call
1652 public void resumeWifiDisplay() {
1653 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1654 "Permission required to resume a wifi display session");
1655
1656 final long token = Binder.clearCallingIdentity();
1657 try {
1658 resumeWifiDisplayInternal();
1659 } finally {
1660 Binder.restoreCallingIdentity(token);
1661 }
1662 }
1663
1664 @Override // Binder call
1665 public WifiDisplayStatus getWifiDisplayStatus() {
1666 // This request does not require special permissions.
1667 // Any app can get information about available wifi displays.
1668
1669 final long token = Binder.clearCallingIdentity();
1670 try {
1671 return getWifiDisplayStatusInternal();
1672 } finally {
1673 Binder.restoreCallingIdentity(token);
1674 }
1675 }
1676
1677 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001678 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001679 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001680 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1681 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001682 final long token = Binder.clearCallingIdentity();
1683 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001684 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001685 } finally {
1686 Binder.restoreCallingIdentity(token);
1687 }
1688 }
1689
1690 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001691 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001692 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001693 int width, int height, int densityDpi, Surface surface, int flags,
1694 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001695 final int callingUid = Binder.getCallingUid();
1696 if (!validatePackageName(callingUid, packageName)) {
1697 throw new SecurityException("packageName must match the calling uid");
1698 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001699 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001700 throw new IllegalArgumentException("appToken must not be null");
1701 }
1702 if (TextUtils.isEmpty(name)) {
1703 throw new IllegalArgumentException("name must be non-null and non-empty");
1704 }
1705 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1706 throw new IllegalArgumentException("width, height, and densityDpi must be "
1707 + "greater than 0");
1708 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001709 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001710 throw new IllegalArgumentException("Surface can't be single-buffered");
1711 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001712
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001713 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1714 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1715
1716 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001717 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001718 throw new IllegalArgumentException(
1719 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1720 }
Michael Wright6720be42014-07-29 19:14:16 -07001721 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001722 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1723 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001724 }
1725
Michael Wrightc39d47a2014-07-08 18:07:36 -07001726 if (projection != null) {
1727 try {
1728 if (!getProjectionService().isValidMediaProjection(projection)) {
1729 throw new SecurityException("Invalid media projection");
1730 }
Michael Wright6720be42014-07-29 19:14:16 -07001731 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001732 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001733 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001734 }
1735 }
1736
Michael Wright6720be42014-07-29 19:14:16 -07001737 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001738 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001739 if (!canProjectVideo(projection)) {
1740 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1741 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1742 + "MediaProjection token in order to create a screen sharing virtual "
1743 + "display.");
1744 }
1745 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001746 if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001747 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001748 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001749 + "or an appropriate MediaProjection token to create a "
1750 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001751 }
1752 }
1753
1754 final long token = Binder.clearCallingIdentity();
1755 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001756 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1757 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001758 } finally {
1759 Binder.restoreCallingIdentity(token);
1760 }
1761 }
1762
1763 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001764 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001765 int width, int height, int densityDpi) {
1766 final long token = Binder.clearCallingIdentity();
1767 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001768 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001769 } finally {
1770 Binder.restoreCallingIdentity(token);
1771 }
1772 }
1773
1774 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001775 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001776 if (surface != null && surface.isSingleBuffered()) {
1777 throw new IllegalArgumentException("Surface can't be single-buffered");
1778 }
Jeff Brown92207df2014-04-16 13:16:07 -07001779 final long token = Binder.clearCallingIdentity();
1780 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001781 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001782 } finally {
1783 Binder.restoreCallingIdentity(token);
1784 }
1785 }
1786
1787 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001788 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001789 final long token = Binder.clearCallingIdentity();
1790 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001791 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001792 } finally {
1793 Binder.restoreCallingIdentity(token);
1794 }
1795 }
1796
1797 @Override // Binder call
1798 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001799 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001800
1801 final long token = Binder.clearCallingIdentity();
1802 try {
1803 dumpInternal(pw);
1804 } finally {
1805 Binder.restoreCallingIdentity(token);
1806 }
1807 }
1808
Kenny Guy22bd0442017-10-26 00:15:54 +01001809 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001810 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001811 mContext.enforceCallingOrSelfPermission(
1812 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1813 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001814
1815 final int callingUid = Binder.getCallingUid();
1816 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
1817 final int mode = appOpsManager.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
1818 callingUid, callingPackage);
1819 final boolean hasUsageStats;
1820 if (mode == AppOpsManager.MODE_DEFAULT) {
1821 // The default behavior here is to check if PackageManager has given the app
1822 // permission.
1823 hasUsageStats = mContext.checkCallingPermission(
1824 Manifest.permission.PACKAGE_USAGE_STATS)
1825 == PackageManager.PERMISSION_GRANTED;
1826 } else {
1827 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1828 }
1829
1830 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001831 final long token = Binder.clearCallingIdentity();
1832 try {
Kenny Guy29aa30e2017-11-30 13:43:46 +00001833 return mBrightnessTracker.getEvents(userId, hasUsageStats);
Kenny Guy22bd0442017-10-26 00:15:54 +01001834 } finally {
1835 Binder.restoreCallingIdentity(token);
1836 }
1837 }
1838
1839 @Override // Binder call
1840 public void setBrightness(int brightness) {
1841 // STOPSHIP - remove when adaptive brightness controller accepts curves.
1842 mContext.enforceCallingOrSelfPermission(
1843 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1844 "Permission to set brightness.");
1845 int userId = UserHandle.getUserId(Binder.getCallingUid());
1846 final long token = Binder.clearCallingIdentity();
1847 try {
1848 mBrightnessTracker.setBrightness(brightness, userId);
1849 } finally {
1850 Binder.restoreCallingIdentity(token);
1851 }
1852 }
1853
Michael Wrighteef0e132017-11-21 17:57:52 +00001854 @Override // Binder call
1855 public void setBrightnessConfigurationForUser(
1856 BrightnessConfiguration c, @UserIdInt int userId) {
1857 mContext.enforceCallingOrSelfPermission(
1858 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
1859 "Permission required to change the display's brightness configuration");
1860 if (userId != UserHandle.getCallingUserId()) {
1861 mContext.enforceCallingOrSelfPermission(
1862 Manifest.permission.INTERACT_ACROSS_USERS,
1863 "Permission required to change the display brightness"
1864 + " configuration of another user");
1865 }
1866 Preconditions.checkNotNull(c);
1867 final long token = Binder.clearCallingIdentity();
1868 try {
1869 setBrightnessConfigurationForUserInternal(c, userId);
1870 } finally {
1871 Binder.restoreCallingIdentity(token);
1872 }
1873 }
1874
Jeff Brown4ccb8232014-01-16 22:16:42 -08001875 private boolean validatePackageName(int uid, String packageName) {
1876 if (packageName != null) {
1877 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1878 if (packageNames != null) {
1879 for (String n : packageNames) {
1880 if (n.equals(packageName)) {
1881 return true;
1882 }
1883 }
1884 }
1885 }
1886 return false;
1887 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001888
1889 private boolean canProjectVideo(IMediaProjection projection) {
1890 if (projection != null) {
1891 try {
1892 if (projection.canProjectVideo()) {
1893 return true;
1894 }
1895 } catch (RemoteException e) {
1896 Slog.e(TAG, "Unable to query projection service for permissions", e);
1897 }
1898 }
1899 if (mContext.checkCallingPermission(
1900 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001901 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001902 return true;
1903 }
1904 return canProjectSecureVideo(projection);
1905 }
1906
1907 private boolean canProjectSecureVideo(IMediaProjection projection) {
1908 if (projection != null) {
1909 try {
1910 if (projection.canProjectSecureVideo()){
1911 return true;
1912 }
1913 } catch (RemoteException e) {
1914 Slog.e(TAG, "Unable to query projection service for permissions", e);
1915 }
1916 }
1917 return mContext.checkCallingPermission(
1918 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001919 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001920 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001921 }
1922
1923 private final class LocalService extends DisplayManagerInternal {
1924 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001925 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001926 SensorManager sensorManager) {
1927 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001928 DisplayBlanker blanker = new DisplayBlanker() {
1929 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001930 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001931 // The order of operations is important for legacy reasons.
1932 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001933 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001934 }
1935
1936 callbacks.onDisplayStateChange(state);
1937
1938 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001939 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001940 }
1941 }
1942 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001943 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001944 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001945 }
Michael Wrighteef0e132017-11-21 17:57:52 +00001946
1947 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07001948 }
1949
1950 @Override
1951 public boolean requestPowerState(DisplayPowerRequest request,
1952 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00001953 synchronized (mSyncRoot) {
1954 return mDisplayPowerController.requestPowerState(request,
1955 waitForNegativeProximity);
1956 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001957 }
1958
1959 @Override
1960 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00001961 synchronized (mSyncRoot) {
1962 return mDisplayPowerController.isProximitySensorAvailable();
1963 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001964 }
1965
1966 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001967 public DisplayInfo getDisplayInfo(int displayId) {
1968 return getDisplayInfoInternal(displayId, Process.myUid());
1969 }
1970
1971 @Override
1972 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1973 if (listener == null) {
1974 throw new IllegalArgumentException("listener must not be null");
1975 }
1976
1977 registerDisplayTransactionListenerInternal(listener);
1978 }
1979
1980 @Override
1981 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1982 if (listener == null) {
1983 throw new IllegalArgumentException("listener must not be null");
1984 }
1985
1986 unregisterDisplayTransactionListenerInternal(listener);
1987 }
1988
1989 @Override
1990 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1991 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1992 }
1993
1994 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07001995 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
1996 getNonOverrideDisplayInfoInternal(displayId, outInfo);
1997 }
1998
1999 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002000 public void performTraversalInTransactionFromWindowManager() {
2001 performTraversalInTransactionFromWindowManagerInternal();
2002 }
2003
2004 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002005 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002006 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2007 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2008 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002009 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002010
2011 @Override
2012 public void setDisplayOffsets(int displayId, int x, int y) {
2013 setDisplayOffsetsInternal(displayId, x, y);
2014 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002015
2016 @Override
2017 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2018 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2019 }
2020
2021 @Override
2022 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2023 return isUidPresentOnDisplayInternal(uid, displayId);
2024 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002025
2026 @Override
2027 public void persistBrightnessSliderEvents() {
2028 mBrightnessTracker.persistEvents();
2029 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002030 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002031}