Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.display; |
| 18 | |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 19 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR; |
| 20 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY; |
| 21 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; |
| 22 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE; |
| 23 | import static android.hardware.display.DisplayManager |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 24 | .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD; |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 25 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 27 | import com.android.internal.util.DumpUtils; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 28 | import com.android.internal.util.IndentingPrintWriter; |
| 29 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 30 | import android.Manifest; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 31 | import android.annotation.NonNull; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 32 | import android.content.Context; |
| 33 | import android.content.pm.PackageManager; |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 34 | import android.hardware.SensorManager; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 35 | import android.hardware.display.DisplayManagerGlobal; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 36 | import android.hardware.display.DisplayManagerInternal; |
| 37 | import android.hardware.display.DisplayViewport; |
| 38 | import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 39 | import android.hardware.display.IDisplayManager; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 40 | import android.hardware.display.IDisplayManagerCallback; |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 41 | import android.hardware.display.IVirtualDisplayCallback; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 42 | import android.hardware.display.WifiDisplayStatus; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 43 | import android.hardware.input.InputManagerInternal; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 44 | import android.media.projection.IMediaProjection; |
| 45 | import android.media.projection.IMediaProjectionManager; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 46 | import android.os.Binder; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 47 | import android.os.Handler; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 48 | import android.os.IBinder; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 49 | import android.os.IBinder.DeathRecipient; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 50 | import android.os.Looper; |
| 51 | import android.os.Message; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 52 | import android.os.PowerManager; |
Craig Mautner | 4504de5 | 2013-12-20 09:06:56 -0800 | [diff] [blame] | 53 | import android.os.Process; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 54 | import android.os.RemoteException; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 55 | import android.os.ServiceManager; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 56 | import android.os.SystemClock; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 57 | import android.os.SystemProperties; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 58 | import android.os.Trace; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 59 | import android.text.TextUtils; |
Andrii Kulian | fb1bf69 | 2017-01-17 11:17:34 -0800 | [diff] [blame] | 60 | import android.util.IntArray; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 61 | import android.util.Slog; |
| 62 | import android.util.SparseArray; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 63 | import android.view.Display; |
| 64 | import android.view.DisplayInfo; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 65 | import android.view.Surface; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 66 | import android.view.WindowManagerInternal; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 67 | |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 68 | import com.android.server.AnimationThread; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 69 | import com.android.server.DisplayThread; |
| 70 | import com.android.server.LocalServices; |
| 71 | import com.android.server.SystemService; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 72 | import com.android.server.UiThread; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 73 | |
| 74 | import java.io.FileDescriptor; |
| 75 | import java.io.PrintWriter; |
| 76 | import java.util.ArrayList; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 77 | import java.util.Arrays; |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 78 | import java.util.List; |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 79 | import java.util.concurrent.CopyOnWriteArrayList; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 82 | * Manages attached displays. |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 83 | * <p> |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 84 | * The {@link DisplayManagerService} manages the global lifecycle of displays, |
| 85 | * decides how to configure logical displays based on the physical display devices currently |
| 86 | * attached, sends notifications to the system and to applications when the state |
| 87 | * changes, and so on. |
| 88 | * </p><p> |
| 89 | * The display manager service relies on a collection of {@link DisplayAdapter} components, |
| 90 | * for discovering and configuring physical display devices attached to the system. |
| 91 | * There are separate display adapters for each manner that devices are attached: |
| 92 | * one display adapter for built-in local displays, one for simulated non-functional |
| 93 | * displays when the system is headless, one for simulated overlay displays used for |
| 94 | * development, one for wifi displays, etc. |
| 95 | * </p><p> |
| 96 | * Display adapters are only weakly coupled to the display manager service. |
| 97 | * Display adapters communicate changes in display device state to the display manager |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 98 | * service asynchronously via a {@link DisplayAdapter.Listener} registered |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 99 | * by the display manager service. This separation of concerns is important for |
| 100 | * two main reasons. First, it neatly encapsulates the responsibilities of these |
| 101 | * two classes: display adapters handle individual display devices whereas |
| 102 | * the display manager service handles the global state. Second, it eliminates |
| 103 | * the potential for deadlocks resulting from asynchronous display device discovery. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 104 | * </p> |
| 105 | * |
| 106 | * <h3>Synchronization</h3> |
| 107 | * <p> |
| 108 | * Because the display manager may be accessed by multiple threads, the synchronization |
| 109 | * story gets a little complicated. In particular, the window manager may call into |
| 110 | * the display manager while holding a surface transaction with the expectation that |
| 111 | * it can apply changes immediately. Unfortunately, that means we can't just do |
| 112 | * everything asynchronously (*grump*). |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 113 | * </p><p> |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 114 | * To make this work, all of the objects that belong to the display manager must |
| 115 | * use the same lock. We call this lock the synchronization root and it has a unique |
| 116 | * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are |
| 117 | * named with the "Locked" suffix. |
| 118 | * </p><p> |
| 119 | * Where things get tricky is that the display manager is not allowed to make |
| 120 | * any potentially reentrant calls, especially into the window manager. We generally |
| 121 | * avoid this by making all potentially reentrant out-calls asynchronous. |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 122 | * </p> |
| 123 | */ |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 124 | public final class DisplayManagerService extends SystemService { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 125 | private static final String TAG = "DisplayManagerService"; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 126 | private static final boolean DEBUG = false; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 127 | |
Jeff Brown | bbd28a2 | 2012-09-20 16:47:15 -0700 | [diff] [blame] | 128 | // When this system property is set to 0, WFD is forcibly disabled on boot. |
| 129 | // When this system property is set to 1, WFD is forcibly enabled on boot. |
| 130 | // Otherwise WFD is enabled according to the value of config_enableWifiDisplay. |
| 131 | private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable"; |
| 132 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 133 | private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000; |
| 134 | |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 135 | private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 136 | private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2; |
| 137 | private static final int MSG_DELIVER_DISPLAY_EVENT = 3; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 138 | private static final int MSG_REQUEST_TRAVERSAL = 4; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 139 | private static final int MSG_UPDATE_VIEWPORT = 5; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 140 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 141 | private final Context mContext; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 142 | private final DisplayManagerHandler mHandler; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 143 | private final Handler mUiHandler; |
| 144 | private final DisplayAdapterListener mDisplayAdapterListener; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 145 | private WindowManagerInternal mWindowManagerInternal; |
| 146 | private InputManagerInternal mInputManagerInternal; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 147 | private IMediaProjectionManager mProjectionService; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 148 | |
| 149 | // The synchronization root for the display manager. |
| 150 | // This lock guards most of the display manager's state. |
Craig Mautner | 0bf6ec9 | 2012-12-18 08:33:27 -0800 | [diff] [blame] | 151 | // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call |
| 152 | // into WindowManagerService methods that require mWindowMap while holding this unless you are |
| 153 | // very very sure that no deadlock can occur. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 154 | private final SyncRoot mSyncRoot = new SyncRoot(); |
| 155 | |
| 156 | // True if in safe mode. |
| 157 | // This option may disable certain display adapters. |
| 158 | public boolean mSafeMode; |
| 159 | |
| 160 | // True if we are in a special boot mode where only core applications and |
| 161 | // services should be started. This option may disable certain display adapters. |
| 162 | public boolean mOnlyCore; |
| 163 | |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 164 | // True if the display manager service should pretend there is only one display |
| 165 | // and only tell applications about the existence of the default logical display. |
| 166 | // The display manager can still mirror content to secondary displays but applications |
| 167 | // cannot present unique content on those displays. |
| 168 | // Used for demonstration purposes only. |
| 169 | private final boolean mSingleDisplayDemoMode; |
| 170 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 171 | // All callback records indexed by calling process id. |
| 172 | public final SparseArray<CallbackRecord> mCallbacks = |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 173 | new SparseArray<CallbackRecord>(); |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 174 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 175 | // List of all currently registered display adapters. |
| 176 | private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>(); |
| 177 | |
| 178 | // List of all currently connected display devices. |
| 179 | private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>(); |
| 180 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 181 | // List of all logical displays indexed by logical display id. |
| 182 | private final SparseArray<LogicalDisplay> mLogicalDisplays = |
| 183 | new SparseArray<LogicalDisplay>(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 184 | private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1; |
| 185 | |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 186 | // List of all display transaction listeners. |
| 187 | private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners = |
| 188 | new CopyOnWriteArrayList<DisplayTransactionListener>(); |
| 189 | |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 190 | // Display power controller. |
| 191 | private DisplayPowerController mDisplayPowerController; |
| 192 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 193 | // The overall display state, independent of changes that might influence one |
| 194 | // display or another in particular. |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 195 | private int mGlobalDisplayState = Display.STATE_ON; |
| 196 | |
| 197 | // The overall display brightness. |
| 198 | // For now, this only applies to the built-in display but we may split it up eventually. |
| 199 | private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 200 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 201 | // Set to true when there are pending display changes that have yet to be applied |
| 202 | // to the surface flinger state. |
| 203 | private boolean mPendingTraversal; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 204 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 205 | // The Wifi display adapter, or null if not registered. |
| 206 | private WifiDisplayAdapter mWifiDisplayAdapter; |
| 207 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 208 | // The number of active wifi display scan requests. |
| 209 | private int mWifiDisplayScanRequestCount; |
| 210 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 211 | // The virtual display adapter, or null if not registered. |
| 212 | private VirtualDisplayAdapter mVirtualDisplayAdapter; |
| 213 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 214 | // Viewports of the default display and the display that should receive touch |
| 215 | // input from an external source. Used by the input system. |
| 216 | private final DisplayViewport mDefaultViewport = new DisplayViewport(); |
| 217 | private final DisplayViewport mExternalTouchViewport = new DisplayViewport(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 218 | private final ArrayList<DisplayViewport> mVirtualTouchViewports = new ArrayList<>(); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 219 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 220 | // Persistent data store for all internal settings maintained by the display manager service. |
| 221 | private final PersistentDataStore mPersistentDataStore = new PersistentDataStore(); |
| 222 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 223 | // Temporary callback list, used when sending display events to applications. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 224 | // May be used outside of the lock but only on the handler thread. |
| 225 | private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 226 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 227 | // Temporary display info, used for comparing display configurations. |
| 228 | private final DisplayInfo mTempDisplayInfo = new DisplayInfo(); |
| 229 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 230 | // Temporary viewports, used when sending new viewport information to the |
| 231 | // input system. May be used outside of the lock but only on the handler thread. |
| 232 | private final DisplayViewport mTempDefaultViewport = new DisplayViewport(); |
| 233 | private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 234 | private final ArrayList<DisplayViewport> mTempVirtualTouchViewports = new ArrayList<>(); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 235 | |
Damien Bargiacchi | 4364bbf | 2016-11-01 21:44:20 -0700 | [diff] [blame] | 236 | // The default color mode for default displays. Overrides the usual |
| 237 | // Display.Display.COLOR_MODE_DEFAULT for displays with the |
| 238 | // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set. |
| 239 | private final int mDefaultDisplayDefaultColorMode; |
| 240 | |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 241 | // Temporary list of deferred work to perform when setting the display state. |
| 242 | // Only used by requestDisplayState. The field is self-synchronized and only |
| 243 | // intended for use inside of the requestGlobalDisplayStateInternal function. |
| 244 | private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>(); |
| 245 | |
Andrii Kulian | fb1bf69 | 2017-01-17 11:17:34 -0800 | [diff] [blame] | 246 | // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs. |
| 247 | private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>(); |
| 248 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 249 | private final Injector mInjector; |
| 250 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 251 | public DisplayManagerService(Context context) { |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 252 | this(context, new Injector()); |
| 253 | } |
| 254 | |
| 255 | @VisibleForTesting |
| 256 | DisplayManagerService(Context context, Injector injector) { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 257 | super(context); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 258 | mInjector = injector; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 259 | mContext = context; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 260 | mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper()); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 261 | mUiHandler = UiThread.getHandler(); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 262 | mDisplayAdapterListener = new DisplayAdapterListener(); |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 263 | mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false); |
Damien Bargiacchi | 4364bbf | 2016-11-01 21:44:20 -0700 | [diff] [blame] | 264 | mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger( |
| 265 | com.android.internal.R.integer.config_defaultDisplayDefaultColorMode); |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 266 | |
| 267 | PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 268 | mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting(); |
Joel Fernandes | 2d314e1 | 2017-04-04 16:32:15 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | public void setupSchedulerPolicies() { |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 272 | // android.display and android.anim is critical to user experience and we should make sure |
| 273 | // it is not in the default foregroup groups, add it to top-app to make sure it uses all the |
| 274 | // cores and scheduling settings for top-app when it runs. |
| 275 | Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(), |
| 276 | Process.THREAD_GROUP_TOP_APP); |
| 277 | Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(), |
| 278 | Process.THREAD_GROUP_TOP_APP); |
Craig Mautner | 4f67ba6 | 2012-08-02 11:23:00 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 281 | @Override |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 282 | public void onStart() { |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 283 | // We need to pre-load the persistent data store so it's ready before the default display |
| 284 | // adapter is up so that we have it's configuration. We could load it lazily, but since |
| 285 | // we're going to have to read it in eventually we may as well do it here rather than after |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 286 | // we've waited for the display to register itself with us. |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 287 | mPersistentDataStore.loadIfNeeded(); |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 288 | mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 289 | |
| 290 | publishBinderService(Context.DISPLAY_SERVICE, new BinderService(), |
| 291 | true /*allowIsolated*/); |
| 292 | publishLocalService(DisplayManagerInternal.class, new LocalService()); |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 293 | publishLocalService(DisplayTransformManager.class, new DisplayTransformManager()); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void onBootPhase(int phase) { |
| 298 | if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) { |
| 299 | synchronized (mSyncRoot) { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 300 | long timeout = SystemClock.uptimeMillis() |
| 301 | + mInjector.getDefaultDisplayDelayTimeout(); |
| 302 | while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null || |
| 303 | mVirtualDisplayAdapter == null) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 304 | long delay = timeout - SystemClock.uptimeMillis(); |
| 305 | if (delay <= 0) { |
| 306 | throw new RuntimeException("Timeout waiting for default display " |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 307 | + "to be initialized. DefaultDisplay=" |
| 308 | + mLogicalDisplays.get(Display.DEFAULT_DISPLAY) |
| 309 | + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 310 | } |
| 311 | if (DEBUG) { |
| 312 | Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay); |
| 313 | } |
| 314 | try { |
| 315 | mSyncRoot.wait(delay); |
| 316 | } catch (InterruptedException ex) { |
| 317 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 323 | // TODO: Use dependencies or a boot phase |
| 324 | public void windowManagerAndInputReady() { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 325 | synchronized (mSyncRoot) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 326 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
| 327 | mInputManagerInternal = LocalServices.getService(InputManagerInternal.class); |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 328 | scheduleTraversalLocked(false); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 333 | * Called when the system is ready to go. |
| 334 | */ |
| 335 | public void systemReady(boolean safeMode, boolean onlyCore) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 336 | synchronized (mSyncRoot) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 337 | mSafeMode = safeMode; |
| 338 | mOnlyCore = onlyCore; |
| 339 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 340 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 341 | mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS); |
| 342 | } |
| 343 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 344 | @VisibleForTesting |
| 345 | Handler getDisplayHandler() { |
| 346 | return mHandler; |
| 347 | } |
| 348 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 349 | private void registerDisplayTransactionListenerInternal( |
| 350 | DisplayTransactionListener listener) { |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 351 | // List is self-synchronized copy-on-write. |
| 352 | mDisplayTransactionListeners.add(listener); |
| 353 | } |
| 354 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 355 | private void unregisterDisplayTransactionListenerInternal( |
| 356 | DisplayTransactionListener listener) { |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 357 | // List is self-synchronized copy-on-write. |
| 358 | mDisplayTransactionListeners.remove(listener); |
| 359 | } |
| 360 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 361 | private void setDisplayInfoOverrideFromWindowManagerInternal( |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 362 | int displayId, DisplayInfo info) { |
| 363 | synchronized (mSyncRoot) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 364 | LogicalDisplay display = mLogicalDisplays.get(displayId); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 365 | if (display != null) { |
Jeff Brown | ef981a4 | 2013-08-07 14:13:37 -0700 | [diff] [blame] | 366 | if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 367 | sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED); |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 368 | scheduleTraversalLocked(false); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 369 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Andrii Kulian | cd09799 | 2017-03-23 18:31:59 -0700 | [diff] [blame] | 374 | /** |
| 375 | * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo) |
| 376 | */ |
| 377 | private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) { |
| 378 | synchronized (mSyncRoot) { |
| 379 | final LogicalDisplay display = mLogicalDisplays.get(displayId); |
| 380 | if (display != null) { |
| 381 | display.getNonOverrideDisplayInfoLocked(outInfo); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 386 | @VisibleForTesting |
| 387 | void performTraversalInTransactionFromWindowManagerInternal() { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 388 | synchronized (mSyncRoot) { |
| 389 | if (!mPendingTraversal) { |
| 390 | return; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 391 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 392 | mPendingTraversal = false; |
| 393 | |
| 394 | performTraversalInTransactionLocked(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 395 | } |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 396 | |
| 397 | // List is self-synchronized copy-on-write. |
| 398 | for (DisplayTransactionListener listener : mDisplayTransactionListeners) { |
| 399 | listener.onDisplayTransaction(); |
| 400 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 403 | private void requestGlobalDisplayStateInternal(int state, int brightness) { |
| 404 | if (state == Display.STATE_UNKNOWN) { |
| 405 | state = Display.STATE_ON; |
| 406 | } |
| 407 | if (state == Display.STATE_OFF) { |
| 408 | brightness = PowerManager.BRIGHTNESS_OFF; |
| 409 | } else if (brightness < 0) { |
| 410 | brightness = PowerManager.BRIGHTNESS_DEFAULT; |
| 411 | } else if (brightness > PowerManager.BRIGHTNESS_ON) { |
| 412 | brightness = PowerManager.BRIGHTNESS_ON; |
| 413 | } |
| 414 | |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 415 | synchronized (mTempDisplayStateWorkQueue) { |
| 416 | try { |
| 417 | // Update the display state within the lock. |
Jeff Brown | 8e5d33e | 2015-06-10 13:05:50 -0700 | [diff] [blame] | 418 | // Note that we do not need to schedule traversals here although it |
| 419 | // may happen as a side-effect of displays changing state. |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 420 | synchronized (mSyncRoot) { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 421 | if (mGlobalDisplayState == state |
| 422 | && mGlobalDisplayBrightness == brightness) { |
| 423 | return; // no change |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 424 | } |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 425 | |
| 426 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState(" |
| 427 | + Display.stateToString(state) |
| 428 | + ", brightness=" + brightness + ")"); |
| 429 | mGlobalDisplayState = state; |
| 430 | mGlobalDisplayBrightness = brightness; |
Jeff Brown | 8e5d33e | 2015-06-10 13:05:50 -0700 | [diff] [blame] | 431 | applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue); |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Setting the display power state can take hundreds of milliseconds |
| 435 | // to complete so we defer the most expensive part of the work until |
| 436 | // after we have exited the critical section to avoid blocking other |
| 437 | // threads for a long time. |
| 438 | for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) { |
| 439 | mTempDisplayStateWorkQueue.get(i).run(); |
| 440 | } |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 441 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 442 | } finally { |
| 443 | mTempDisplayStateWorkQueue.clear(); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 448 | private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 449 | synchronized (mSyncRoot) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 450 | LogicalDisplay display = mLogicalDisplays.get(displayId); |
| 451 | if (display != null) { |
| 452 | DisplayInfo info = display.getDisplayInfoLocked(); |
Andrii Kulian | fb1bf69 | 2017-01-17 11:17:34 -0800 | [diff] [blame] | 453 | if (info.hasAccess(callingUid) |
| 454 | || isUidPresentOnDisplayInternal(callingUid, displayId)) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 455 | return info; |
| 456 | } |
| 457 | } |
| 458 | return null; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | private int[] getDisplayIdsInternal(int callingUid) { |
| 463 | synchronized (mSyncRoot) { |
| 464 | final int count = mLogicalDisplays.size(); |
| 465 | int[] displayIds = new int[count]; |
| 466 | int n = 0; |
| 467 | for (int i = 0; i < count; i++) { |
| 468 | LogicalDisplay display = mLogicalDisplays.valueAt(i); |
| 469 | DisplayInfo info = display.getDisplayInfoLocked(); |
| 470 | if (info.hasAccess(callingUid)) { |
| 471 | displayIds[n++] = mLogicalDisplays.keyAt(i); |
| 472 | } |
| 473 | } |
| 474 | if (n != count) { |
| 475 | displayIds = Arrays.copyOfRange(displayIds, 0, n); |
| 476 | } |
| 477 | return displayIds; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) { |
| 482 | synchronized (mSyncRoot) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 483 | if (mCallbacks.get(callingPid) != null) { |
| 484 | throw new SecurityException("The calling process has already " |
| 485 | + "registered an IDisplayManagerCallback."); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 488 | CallbackRecord record = new CallbackRecord(callingPid, callback); |
| 489 | try { |
| 490 | IBinder binder = callback.asBinder(); |
| 491 | binder.linkToDeath(record, 0); |
| 492 | } catch (RemoteException ex) { |
| 493 | // give up |
| 494 | throw new RuntimeException(ex); |
| 495 | } |
| 496 | |
| 497 | mCallbacks.put(callingPid, record); |
| 498 | } |
| 499 | } |
| 500 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 501 | private void onCallbackDied(CallbackRecord record) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 502 | synchronized (mSyncRoot) { |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 503 | mCallbacks.remove(record.mPid); |
| 504 | stopWifiDisplayScanLocked(record); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 508 | private void startWifiDisplayScanInternal(int callingPid) { |
| 509 | synchronized (mSyncRoot) { |
| 510 | CallbackRecord record = mCallbacks.get(callingPid); |
| 511 | if (record == null) { |
| 512 | throw new IllegalStateException("The calling process has not " |
| 513 | + "registered an IDisplayManagerCallback."); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 514 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 515 | startWifiDisplayScanLocked(record); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 519 | private void startWifiDisplayScanLocked(CallbackRecord record) { |
| 520 | if (!record.mWifiDisplayScanRequested) { |
| 521 | record.mWifiDisplayScanRequested = true; |
| 522 | if (mWifiDisplayScanRequestCount++ == 0) { |
| 523 | if (mWifiDisplayAdapter != null) { |
| 524 | mWifiDisplayAdapter.requestStartScanLocked(); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 530 | private void stopWifiDisplayScanInternal(int callingPid) { |
| 531 | synchronized (mSyncRoot) { |
| 532 | CallbackRecord record = mCallbacks.get(callingPid); |
| 533 | if (record == null) { |
| 534 | throw new IllegalStateException("The calling process has not " |
| 535 | + "registered an IDisplayManagerCallback."); |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 536 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 537 | stopWifiDisplayScanLocked(record); |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | private void stopWifiDisplayScanLocked(CallbackRecord record) { |
| 542 | if (record.mWifiDisplayScanRequested) { |
| 543 | record.mWifiDisplayScanRequested = false; |
| 544 | if (--mWifiDisplayScanRequestCount == 0) { |
| 545 | if (mWifiDisplayAdapter != null) { |
| 546 | mWifiDisplayAdapter.requestStopScanLocked(); |
| 547 | } |
| 548 | } else if (mWifiDisplayScanRequestCount < 0) { |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 549 | Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: " |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 550 | + mWifiDisplayScanRequestCount); |
| 551 | mWifiDisplayScanRequestCount = 0; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 556 | private void connectWifiDisplayInternal(String address) { |
| 557 | synchronized (mSyncRoot) { |
| 558 | if (mWifiDisplayAdapter != null) { |
| 559 | mWifiDisplayAdapter.requestConnectLocked(address); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 560 | } |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 564 | private void pauseWifiDisplayInternal() { |
| 565 | synchronized (mSyncRoot) { |
| 566 | if (mWifiDisplayAdapter != null) { |
| 567 | mWifiDisplayAdapter.requestPauseLocked(); |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 568 | } |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 572 | private void resumeWifiDisplayInternal() { |
| 573 | synchronized (mSyncRoot) { |
| 574 | if (mWifiDisplayAdapter != null) { |
| 575 | mWifiDisplayAdapter.requestResumeLocked(); |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 576 | } |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 580 | private void disconnectWifiDisplayInternal() { |
| 581 | synchronized (mSyncRoot) { |
| 582 | if (mWifiDisplayAdapter != null) { |
| 583 | mWifiDisplayAdapter.requestDisconnectLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 584 | } |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 588 | private void renameWifiDisplayInternal(String address, String alias) { |
| 589 | synchronized (mSyncRoot) { |
| 590 | if (mWifiDisplayAdapter != null) { |
| 591 | mWifiDisplayAdapter.requestRenameLocked(address, alias); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 592 | } |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 596 | private void forgetWifiDisplayInternal(String address) { |
| 597 | synchronized (mSyncRoot) { |
| 598 | if (mWifiDisplayAdapter != null) { |
| 599 | mWifiDisplayAdapter.requestForgetLocked(address); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 600 | } |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 604 | private WifiDisplayStatus getWifiDisplayStatusInternal() { |
| 605 | synchronized (mSyncRoot) { |
| 606 | if (mWifiDisplayAdapter != null) { |
| 607 | return mWifiDisplayAdapter.getWifiDisplayStatusLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 608 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 609 | return new WifiDisplayStatus(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 613 | private void requestColorModeInternal(int displayId, int colorMode) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 614 | synchronized (mSyncRoot) { |
| 615 | LogicalDisplay display = mLogicalDisplays.get(displayId); |
| 616 | if (display != null && |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 617 | display.getRequestedColorModeLocked() != colorMode) { |
| 618 | display.setRequestedColorModeLocked(colorMode); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 619 | scheduleTraversalLocked(false); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 624 | private int createVirtualDisplayInternal(IVirtualDisplayCallback callback, |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 625 | IMediaProjection projection, int callingUid, String packageName, String name, int width, |
| 626 | int height, int densityDpi, Surface surface, int flags, String uniqueId) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 627 | synchronized (mSyncRoot) { |
| 628 | if (mVirtualDisplayAdapter == null) { |
| 629 | Slog.w(TAG, "Rejecting request to create private virtual display " |
| 630 | + "because the virtual display adapter is not available."); |
| 631 | return -1; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 632 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 633 | |
| 634 | DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked( |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 635 | callback, projection, callingUid, packageName, name, width, height, densityDpi, |
| 636 | surface, flags, uniqueId); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 637 | if (device == null) { |
| 638 | return -1; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 639 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 640 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 641 | handleDisplayDeviceAddedLocked(device); |
| 642 | LogicalDisplay display = findLogicalDisplayForDeviceLocked(device); |
| 643 | if (display != null) { |
| 644 | return display.getDisplayIdLocked(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 645 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 646 | |
| 647 | // Something weird happened and the logical display was not created. |
| 648 | Slog.w(TAG, "Rejecting request to create virtual display " |
| 649 | + "because the logical display was not created."); |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 650 | mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder()); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 651 | handleDisplayDeviceRemovedLocked(device); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 652 | } |
| 653 | return -1; |
| 654 | } |
| 655 | |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 656 | private void resizeVirtualDisplayInternal(IBinder appToken, |
| 657 | int width, int height, int densityDpi) { |
| 658 | synchronized (mSyncRoot) { |
| 659 | if (mVirtualDisplayAdapter == null) { |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi); |
| 664 | } |
| 665 | } |
| 666 | |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 667 | private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) { |
| 668 | synchronized (mSyncRoot) { |
| 669 | if (mVirtualDisplayAdapter == null) { |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface); |
| 674 | } |
| 675 | } |
| 676 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 677 | private void releaseVirtualDisplayInternal(IBinder appToken) { |
| 678 | synchronized (mSyncRoot) { |
| 679 | if (mVirtualDisplayAdapter == null) { |
| 680 | return; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 681 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 682 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 683 | DisplayDevice device = |
| 684 | mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken); |
| 685 | if (device != null) { |
| 686 | handleDisplayDeviceRemovedLocked(device); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 691 | private void registerDefaultDisplayAdapters() { |
| 692 | // Register default display adapters. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 693 | synchronized (mSyncRoot) { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 694 | // main display adapter |
Mike Lockwood | e63f6f7 | 2013-11-15 11:01:47 -0800 | [diff] [blame] | 695 | registerDisplayAdapterLocked(new LocalDisplayAdapter( |
| 696 | mSyncRoot, mContext, mHandler, mDisplayAdapterListener)); |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 697 | |
| 698 | // Standalone VR devices rely on a virtual display as their primary display for |
| 699 | // 2D UI. We register virtual display adapter along side the main display adapter |
| 700 | // here so that it is ready by the time the system sends the home Intent for |
| 701 | // early apps like SetupWizard/Launcher. In particular, SUW is displayed using |
| 702 | // the virtual display inside VR before any VR-specific apps even run. |
| 703 | mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext, |
| 704 | mHandler, mDisplayAdapterListener); |
| 705 | if (mVirtualDisplayAdapter != null) { |
| 706 | registerDisplayAdapterLocked(mVirtualDisplayAdapter); |
| 707 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | private void registerAdditionalDisplayAdapters() { |
| 712 | synchronized (mSyncRoot) { |
| 713 | if (shouldRegisterNonEssentialDisplayAdaptersLocked()) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 714 | registerOverlayDisplayAdapterLocked(); |
| 715 | registerWifiDisplayAdapterLocked(); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 720 | private void registerOverlayDisplayAdapterLocked() { |
| 721 | registerDisplayAdapterLocked(new OverlayDisplayAdapter( |
| 722 | mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler)); |
| 723 | } |
| 724 | |
| 725 | private void registerWifiDisplayAdapterLocked() { |
| 726 | if (mContext.getResources().getBoolean( |
Jeff Brown | bbd28a2 | 2012-09-20 16:47:15 -0700 | [diff] [blame] | 727 | com.android.internal.R.bool.config_enableWifiDisplay) |
| 728 | || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 729 | mWifiDisplayAdapter = new WifiDisplayAdapter( |
| 730 | mSyncRoot, mContext, mHandler, mDisplayAdapterListener, |
| 731 | mPersistentDataStore); |
| 732 | registerDisplayAdapterLocked(mWifiDisplayAdapter); |
| 733 | } |
| 734 | } |
| 735 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 736 | private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() { |
| 737 | // In safe mode, we disable non-essential display adapters to give the user |
| 738 | // an opportunity to fix broken settings or other problems that might affect |
| 739 | // system stability. |
| 740 | // In only-core mode, we disable non-essential display adapters to minimize |
| 741 | // the number of dependencies that are started while in this mode and to |
| 742 | // prevent problems that might occur due to the device being encrypted. |
| 743 | return !mSafeMode && !mOnlyCore; |
| 744 | } |
| 745 | |
| 746 | private void registerDisplayAdapterLocked(DisplayAdapter adapter) { |
| 747 | mDisplayAdapters.add(adapter); |
| 748 | adapter.registerLocked(); |
| 749 | } |
| 750 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 751 | private void handleDisplayDeviceAdded(DisplayDevice device) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 752 | synchronized (mSyncRoot) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 753 | handleDisplayDeviceAddedLocked(device); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 757 | private void handleDisplayDeviceAddedLocked(DisplayDevice device) { |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 758 | DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 759 | if (mDisplayDevices.contains(device)) { |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 760 | Slog.w(TAG, "Attempted to add already added display device: " + info); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 761 | return; |
| 762 | } |
| 763 | |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 764 | Slog.i(TAG, "Display device added: " + info); |
| 765 | device.mDebugLastLoggedDeviceInfo = info; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 766 | |
| 767 | mDisplayDevices.add(device); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 768 | LogicalDisplay display = addLogicalDisplayLocked(device); |
Jeff Brown | 0033a86 | 2014-10-08 12:06:39 -0700 | [diff] [blame] | 769 | Runnable work = updateDisplayStateLocked(device); |
| 770 | if (work != null) { |
| 771 | work.run(); |
| 772 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 773 | if (display != null && display.getPrimaryDisplayDeviceLocked() == device) { |
| 774 | int colorMode = mPersistentDataStore.getColorMode(device); |
Damien Bargiacchi | 4364bbf | 2016-11-01 21:44:20 -0700 | [diff] [blame] | 775 | if (colorMode == Display.COLOR_MODE_INVALID) { |
| 776 | if ((device.getDisplayDeviceInfoLocked().flags |
| 777 | & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) { |
| 778 | colorMode = mDefaultDisplayDefaultColorMode; |
| 779 | } else { |
| 780 | colorMode = Display.COLOR_MODE_DEFAULT; |
| 781 | } |
| 782 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 783 | display.setRequestedColorModeLocked(colorMode); |
| 784 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 785 | scheduleTraversalLocked(false); |
| 786 | } |
| 787 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 788 | private void handleDisplayDeviceChanged(DisplayDevice device) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 789 | synchronized (mSyncRoot) { |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 790 | DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 791 | if (!mDisplayDevices.contains(device)) { |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 792 | Slog.w(TAG, "Attempted to change non-existent display device: " + info); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 796 | int diff = device.mDebugLastLoggedDeviceInfo.diff(info); |
| 797 | if (diff == DisplayDeviceInfo.DIFF_STATE) { |
| 798 | Slog.i(TAG, "Display device changed state: \"" + info.name |
| 799 | + "\", " + Display.stateToString(info.state)); |
| 800 | } else if (diff != 0) { |
| 801 | Slog.i(TAG, "Display device changed: " + info); |
| 802 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 803 | if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) { |
| 804 | try { |
| 805 | mPersistentDataStore.setColorMode(device, info.colorMode); |
| 806 | } finally { |
| 807 | mPersistentDataStore.saveIfNeeded(); |
| 808 | } |
| 809 | } |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 810 | device.mDebugLastLoggedDeviceInfo = info; |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 811 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 812 | device.applyPendingDisplayDeviceInfoChangesLocked(); |
| 813 | if (updateLogicalDisplaysLocked()) { |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 814 | scheduleTraversalLocked(false); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 819 | private void handleDisplayDeviceRemoved(DisplayDevice device) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 820 | synchronized (mSyncRoot) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 821 | handleDisplayDeviceRemovedLocked(device); |
| 822 | } |
| 823 | } |
Jeff Brown | 8e5d33e | 2015-06-10 13:05:50 -0700 | [diff] [blame] | 824 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 825 | private void handleDisplayDeviceRemovedLocked(DisplayDevice device) { |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 826 | DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 827 | if (!mDisplayDevices.remove(device)) { |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 828 | Slog.w(TAG, "Attempted to remove non-existent display device: " + info); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
Jeff Brown | 10acf6d | 2015-04-14 14:20:47 -0700 | [diff] [blame] | 832 | Slog.i(TAG, "Display device removed: " + info); |
| 833 | device.mDebugLastLoggedDeviceInfo = info; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 834 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 835 | updateLogicalDisplaysLocked(); |
| 836 | scheduleTraversalLocked(false); |
| 837 | } |
| 838 | |
Jeff Brown | 8e5d33e | 2015-06-10 13:05:50 -0700 | [diff] [blame] | 839 | private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 840 | final int count = mDisplayDevices.size(); |
| 841 | for (int i = 0; i < count; i++) { |
| 842 | DisplayDevice device = mDisplayDevices.get(i); |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 843 | Runnable runnable = updateDisplayStateLocked(device); |
| 844 | if (runnable != null) { |
| 845 | workQueue.add(runnable); |
| 846 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 850 | private Runnable updateDisplayStateLocked(DisplayDevice device) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 851 | // Blank or unblank the display immediately to match the state requested |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 852 | // by the display power controller (if known). |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 853 | DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); |
| 854 | if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 855 | return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness); |
Craig Mautner | 4f67ba6 | 2012-08-02 11:23:00 -0700 | [diff] [blame] | 856 | } |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 857 | return null; |
Craig Mautner | 4f67ba6 | 2012-08-02 11:23:00 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 860 | // Adds a new logical display based on the given display device. |
| 861 | // Sends notifications if needed. |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 862 | private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 863 | DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked(); |
| 864 | boolean isDefault = (deviceInfo.flags |
| 865 | & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0; |
| 866 | if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) { |
| 867 | Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo); |
| 868 | isDefault = false; |
| 869 | } |
| 870 | |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 871 | if (!isDefault && mSingleDisplayDemoMode) { |
| 872 | Slog.i(TAG, "Not creating a logical display for a secondary display " |
| 873 | + " because single display demo mode is enabled: " + deviceInfo); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 874 | return null; |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 877 | final int displayId = assignDisplayIdLocked(isDefault); |
| 878 | final int layerStack = assignLayerStackLocked(displayId); |
| 879 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 880 | LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 881 | display.updateLocked(mDisplayDevices); |
| 882 | if (!display.isValidLocked()) { |
| 883 | // This should never happen currently. |
| 884 | Slog.w(TAG, "Ignoring display device because the logical display " |
| 885 | + "created from it was not considered valid: " + deviceInfo); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 886 | return null; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | mLogicalDisplays.put(displayId, display); |
| 890 | |
| 891 | // Wake up waitForDefaultDisplay. |
| 892 | if (isDefault) { |
| 893 | mSyncRoot.notifyAll(); |
| 894 | } |
| 895 | |
| 896 | sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 897 | return display; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | private int assignDisplayIdLocked(boolean isDefault) { |
| 901 | return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++; |
| 902 | } |
| 903 | |
| 904 | private int assignLayerStackLocked(int displayId) { |
| 905 | // Currently layer stacks and display ids are the same. |
| 906 | // This need not be the case. |
| 907 | return displayId; |
| 908 | } |
| 909 | |
| 910 | // Updates all existing logical displays given the current set of display devices. |
| 911 | // Removes invalid logical displays. |
| 912 | // Sends notifications if needed. |
| 913 | private boolean updateLogicalDisplaysLocked() { |
| 914 | boolean changed = false; |
| 915 | for (int i = mLogicalDisplays.size(); i-- > 0; ) { |
| 916 | final int displayId = mLogicalDisplays.keyAt(i); |
| 917 | LogicalDisplay display = mLogicalDisplays.valueAt(i); |
| 918 | |
| 919 | mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked()); |
| 920 | display.updateLocked(mDisplayDevices); |
| 921 | if (!display.isValidLocked()) { |
| 922 | mLogicalDisplays.removeAt(i); |
| 923 | sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED); |
| 924 | changed = true; |
| 925 | } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) { |
| 926 | sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED); |
| 927 | changed = true; |
| 928 | } |
| 929 | } |
| 930 | return changed; |
| 931 | } |
| 932 | |
| 933 | private void performTraversalInTransactionLocked() { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 934 | // Clear all viewports before configuring displays so that we can keep |
| 935 | // track of which ones we have configured. |
| 936 | clearViewportsLocked(); |
| 937 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 938 | // Configure each display device. |
| 939 | final int count = mDisplayDevices.size(); |
| 940 | for (int i = 0; i < count; i++) { |
| 941 | DisplayDevice device = mDisplayDevices.get(i); |
| 942 | configureDisplayInTransactionLocked(device); |
| 943 | device.performTraversalInTransactionLocked(); |
| 944 | } |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 945 | |
| 946 | // Tell the input system about these new viewports. |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 947 | if (mInputManagerInternal != null) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 948 | mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT); |
| 949 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 952 | private void setDisplayPropertiesInternal(int displayId, boolean hasContent, |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 953 | float requestedRefreshRate, int requestedModeId, boolean inTraversal) { |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 954 | synchronized (mSyncRoot) { |
| 955 | LogicalDisplay display = mLogicalDisplays.get(displayId); |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 956 | if (display == null) { |
| 957 | return; |
| 958 | } |
| 959 | if (display.hasContentLocked() != hasContent) { |
Jeff Brown | 33041bd | 2013-08-02 21:11:14 -0700 | [diff] [blame] | 960 | if (DEBUG) { |
| 961 | Slog.d(TAG, "Display " + displayId + " hasContent flag changed: " |
| 962 | + "hasContent=" + hasContent + ", inTraversal=" + inTraversal); |
| 963 | } |
| 964 | |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 965 | display.setHasContentLocked(hasContent); |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 966 | scheduleTraversalLocked(inTraversal); |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 967 | } |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 968 | if (requestedModeId == 0 && requestedRefreshRate != 0) { |
| 969 | // Scan supported modes returned by display.getInfo() to find a mode with the same |
| 970 | // size as the default display mode but with the specified refresh rate instead. |
| 971 | requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate( |
| 972 | requestedRefreshRate); |
| 973 | } |
| 974 | if (display.getRequestedModeIdLocked() != requestedModeId) { |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 975 | if (DEBUG) { |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 976 | Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId); |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 977 | } |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 978 | display.setRequestedModeIdLocked(requestedModeId); |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 979 | scheduleTraversalLocked(inTraversal); |
| 980 | } |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 981 | } |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 982 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 983 | |
Filip Gruszczynski | d2e8640 | 2015-02-19 13:05:03 -0800 | [diff] [blame] | 984 | private void setDisplayOffsetsInternal(int displayId, int x, int y) { |
| 985 | synchronized (mSyncRoot) { |
| 986 | LogicalDisplay display = mLogicalDisplays.get(displayId); |
| 987 | if (display == null) { |
| 988 | return; |
| 989 | } |
| 990 | if (display.getDisplayOffsetXLocked() != x |
| 991 | || display.getDisplayOffsetYLocked() != y) { |
| 992 | if (DEBUG) { |
| 993 | Slog.d(TAG, "Display " + displayId + " burn-in offset set to (" |
| 994 | + x + ", " + y + ")"); |
| 995 | } |
| 996 | display.setDisplayOffsetsLocked(x, y); |
| 997 | scheduleTraversalLocked(false); |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
Andrii Kulian | fb1bf69 | 2017-01-17 11:17:34 -0800 | [diff] [blame] | 1002 | // Updates the lists of UIDs that are present on displays. |
| 1003 | private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) { |
| 1004 | synchronized (mSyncRoot) { |
| 1005 | mDisplayAccessUIDs.clear(); |
| 1006 | for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) { |
| 1007 | mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i), |
| 1008 | newDisplayAccessUIDs.valueAt(i)); |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // Checks if provided UID's content is present on the display and UID has access to it. |
| 1014 | private boolean isUidPresentOnDisplayInternal(int uid, int displayId) { |
| 1015 | synchronized (mSyncRoot) { |
| 1016 | final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId); |
| 1017 | return displayUIDs != null && displayUIDs.indexOf(uid) != -1; |
| 1018 | } |
| 1019 | } |
| 1020 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1021 | private void clearViewportsLocked() { |
| 1022 | mDefaultViewport.valid = false; |
| 1023 | mExternalTouchViewport.valid = false; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1024 | mVirtualTouchViewports.clear(); |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | private void configureDisplayInTransactionLocked(DisplayDevice device) { |
Jeff Brown | d14c8c9 | 2014-01-07 18:13:09 -0800 | [diff] [blame] | 1028 | final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); |
| 1029 | final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 1030 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1031 | // Find the logical display that the display device is showing. |
Jeff Brown | d14c8c9 | 2014-01-07 18:13:09 -0800 | [diff] [blame] | 1032 | // Certain displays only ever show their own content. |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 1033 | LogicalDisplay display = findLogicalDisplayForDeviceLocked(device); |
Jeff Brown | d14c8c9 | 2014-01-07 18:13:09 -0800 | [diff] [blame] | 1034 | if (!ownContent) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 1035 | if (display != null && !display.hasContentLocked()) { |
| 1036 | // If the display does not have any content of its own, then |
| 1037 | // automatically mirror the default logical display contents. |
| 1038 | display = null; |
| 1039 | } |
| 1040 | if (display == null) { |
| 1041 | display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY); |
| 1042 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | // Apply the logical display configuration to the display device. |
| 1046 | if (display == null) { |
| 1047 | // TODO: no logical display for the device, blank it |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1048 | Slog.w(TAG, "Missing logical display to use for physical display device: " |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1049 | + device.getDisplayDeviceInfoLocked()); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1050 | return; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1051 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1052 | display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1053 | |
| 1054 | // Update the viewports if needed. |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1055 | if (!mDefaultViewport.valid |
| 1056 | && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) { |
| 1057 | setViewportLocked(mDefaultViewport, display, device); |
| 1058 | } |
| 1059 | if (!mExternalTouchViewport.valid |
| 1060 | && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) { |
| 1061 | setViewportLocked(mExternalTouchViewport, display, device); |
| 1062 | } |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL && !TextUtils.isEmpty(info.uniqueId)) { |
| 1065 | final DisplayViewport viewport = getVirtualTouchViewportLocked(info.uniqueId); |
| 1066 | setViewportLocked(viewport, display, device); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /** Gets the virtual device viewport or creates it if not yet created. */ |
| 1071 | private DisplayViewport getVirtualTouchViewportLocked(@NonNull String uniqueId) { |
| 1072 | DisplayViewport viewport; |
| 1073 | final int count = mVirtualTouchViewports.size(); |
| 1074 | for (int i = 0; i < count; i++) { |
| 1075 | viewport = mVirtualTouchViewports.get(i); |
| 1076 | if (uniqueId.equals(viewport.uniqueId)) { |
| 1077 | return viewport; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | viewport = new DisplayViewport(); |
| 1082 | viewport.uniqueId = uniqueId; |
| 1083 | mVirtualTouchViewports.add(viewport); |
| 1084 | return viewport; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | private static void setViewportLocked(DisplayViewport viewport, |
| 1088 | LogicalDisplay display, DisplayDevice device) { |
| 1089 | viewport.valid = true; |
| 1090 | viewport.displayId = display.getDisplayIdLocked(); |
| 1091 | device.populateViewportLocked(viewport); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) { |
| 1095 | final int count = mLogicalDisplays.size(); |
| 1096 | for (int i = 0; i < count; i++) { |
| 1097 | LogicalDisplay display = mLogicalDisplays.valueAt(i); |
| 1098 | if (display.getPrimaryDisplayDeviceLocked() == device) { |
| 1099 | return display; |
| 1100 | } |
| 1101 | } |
| 1102 | return null; |
| 1103 | } |
| 1104 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1105 | private void sendDisplayEventLocked(int displayId, int event) { |
| 1106 | Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event); |
| 1107 | mHandler.sendMessage(msg); |
| 1108 | } |
| 1109 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1110 | // Requests that performTraversalsInTransactionFromWindowManager be called at a |
| 1111 | // later time to apply changes to surfaces and displays. |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 1112 | private void scheduleTraversalLocked(boolean inTraversal) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1113 | if (!mPendingTraversal && mWindowManagerInternal != null) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1114 | mPendingTraversal = true; |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 1115 | if (!inTraversal) { |
| 1116 | mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL); |
| 1117 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | // Runs on Handler thread. |
| 1122 | // Delivers display event notifications to callbacks. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1123 | private void deliverDisplayEvent(int displayId, int event) { |
| 1124 | if (DEBUG) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1125 | Slog.d(TAG, "Delivering display event: displayId=" |
| 1126 | + displayId + ", event=" + event); |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 1127 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 1128 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1129 | // Grab the lock and copy the callbacks. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1130 | final int count; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1131 | synchronized (mSyncRoot) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1132 | count = mCallbacks.size(); |
| 1133 | mTempCallbacks.clear(); |
| 1134 | for (int i = 0; i < count; i++) { |
| 1135 | mTempCallbacks.add(mCallbacks.valueAt(i)); |
Craig Mautner | 4f67ba6 | 2012-08-02 11:23:00 -0700 | [diff] [blame] | 1136 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1137 | } |
Craig Mautner | 4f67ba6 | 2012-08-02 11:23:00 -0700 | [diff] [blame] | 1138 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1139 | // After releasing the lock, send the notifications out. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1140 | for (int i = 0; i < count; i++) { |
| 1141 | mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event); |
| 1142 | } |
| 1143 | mTempCallbacks.clear(); |
Craig Mautner | 4f67ba6 | 2012-08-02 11:23:00 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1146 | private IMediaProjectionManager getProjectionService() { |
| 1147 | if (mProjectionService == null) { |
| 1148 | IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE); |
| 1149 | mProjectionService = IMediaProjectionManager.Stub.asInterface(b); |
| 1150 | } |
| 1151 | return mProjectionService; |
| 1152 | } |
| 1153 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1154 | private void dumpInternal(PrintWriter pw) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1155 | pw.println("DISPLAY MANAGER (dumpsys display)"); |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 1156 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1157 | synchronized (mSyncRoot) { |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 1158 | pw.println(" mOnlyCode=" + mOnlyCore); |
| 1159 | pw.println(" mSafeMode=" + mSafeMode); |
| 1160 | pw.println(" mPendingTraversal=" + mPendingTraversal); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1161 | pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState)); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 1162 | pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId); |
| 1163 | pw.println(" mDefaultViewport=" + mDefaultViewport); |
| 1164 | pw.println(" mExternalTouchViewport=" + mExternalTouchViewport); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1165 | pw.println(" mVirtualTouchViewports=" + mVirtualTouchViewports); |
Damien Bargiacchi | 4364bbf | 2016-11-01 21:44:20 -0700 | [diff] [blame] | 1166 | pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode); |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 1167 | pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode); |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 1168 | pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 1169 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1170 | IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1171 | ipw.increaseIndent(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1172 | |
| 1173 | pw.println(); |
| 1174 | pw.println("Display Adapters: size=" + mDisplayAdapters.size()); |
Jeff Brown | 848c2dc | 2012-08-19 20:18:08 -0700 | [diff] [blame] | 1175 | for (DisplayAdapter adapter : mDisplayAdapters) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1176 | pw.println(" " + adapter.getName()); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1177 | adapter.dumpLocked(ipw); |
Jeff Brown | 848c2dc | 2012-08-19 20:18:08 -0700 | [diff] [blame] | 1178 | } |
Craig Mautner | 9de4936 | 2012-08-02 14:30:30 -0700 | [diff] [blame] | 1179 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1180 | pw.println(); |
| 1181 | pw.println("Display Devices: size=" + mDisplayDevices.size()); |
| 1182 | for (DisplayDevice device : mDisplayDevices) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1183 | pw.println(" " + device.getDisplayDeviceInfoLocked()); |
| 1184 | device.dumpLocked(ipw); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | final int logicalDisplayCount = mLogicalDisplays.size(); |
| 1188 | pw.println(); |
| 1189 | pw.println("Logical Displays: size=" + logicalDisplayCount); |
| 1190 | for (int i = 0; i < logicalDisplayCount; i++) { |
| 1191 | int displayId = mLogicalDisplays.keyAt(i); |
| 1192 | LogicalDisplay display = mLogicalDisplays.valueAt(i); |
| 1193 | pw.println(" Display " + displayId + ":"); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1194 | display.dumpLocked(ipw); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1195 | } |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 1196 | |
| 1197 | final int callbackCount = mCallbacks.size(); |
| 1198 | pw.println(); |
| 1199 | pw.println("Callbacks: size=" + callbackCount); |
| 1200 | for (int i = 0; i < callbackCount; i++) { |
| 1201 | CallbackRecord callback = mCallbacks.valueAt(i); |
| 1202 | pw.println(" " + i + ": mPid=" + callback.mPid |
| 1203 | + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested); |
| 1204 | } |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 1205 | |
| 1206 | if (mDisplayPowerController != null) { |
| 1207 | mDisplayPowerController.dump(pw); |
| 1208 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 1209 | |
| 1210 | pw.println(); |
| 1211 | mPersistentDataStore.dump(pw); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1215 | /** |
| 1216 | * This is the object that everything in the display manager locks on. |
| 1217 | * We make it an inner class within the {@link DisplayManagerService} to so that it is |
| 1218 | * clear that the object belongs to the display manager service and that it is |
| 1219 | * a unique object with a special purpose. |
| 1220 | */ |
| 1221 | public static final class SyncRoot { |
| 1222 | } |
| 1223 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1224 | @VisibleForTesting |
| 1225 | static class Injector { |
| 1226 | VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context, |
| 1227 | Handler handler, DisplayAdapter.Listener displayAdapterListener) { |
| 1228 | return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener); |
| 1229 | } |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 1230 | |
| 1231 | long getDefaultDisplayDelayTimeout() { |
| 1232 | return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT; |
| 1233 | } |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 1236 | @VisibleForTesting |
| 1237 | DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) { |
| 1238 | synchronized (mSyncRoot) { |
| 1239 | LogicalDisplay display = mLogicalDisplays.get(displayId); |
| 1240 | if (display != null) { |
| 1241 | DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked(); |
| 1242 | return displayDevice.getDisplayDeviceInfoLocked(); |
| 1243 | } |
| 1244 | return null; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1248 | private final class DisplayManagerHandler extends Handler { |
| 1249 | public DisplayManagerHandler(Looper looper) { |
| 1250 | super(looper, null, true /*async*/); |
Jeff Brown | 848c2dc | 2012-08-19 20:18:08 -0700 | [diff] [blame] | 1251 | } |
Jeff Brown | bf5740e | 2012-08-19 23:20:02 -0700 | [diff] [blame] | 1252 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1253 | @Override |
| 1254 | public void handleMessage(Message msg) { |
| 1255 | switch (msg.what) { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 1256 | case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS: |
| 1257 | registerDefaultDisplayAdapters(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1258 | break; |
| 1259 | |
| 1260 | case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS: |
| 1261 | registerAdditionalDisplayAdapters(); |
| 1262 | break; |
| 1263 | |
| 1264 | case MSG_DELIVER_DISPLAY_EVENT: |
| 1265 | deliverDisplayEvent(msg.arg1, msg.arg2); |
| 1266 | break; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1267 | |
| 1268 | case MSG_REQUEST_TRAVERSAL: |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1269 | mWindowManagerInternal.requestTraversalFromDisplayManager(); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1270 | break; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1271 | |
| 1272 | case MSG_UPDATE_VIEWPORT: { |
| 1273 | synchronized (mSyncRoot) { |
| 1274 | mTempDefaultViewport.copyFrom(mDefaultViewport); |
| 1275 | mTempExternalTouchViewport.copyFrom(mExternalTouchViewport); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1276 | if (!mTempVirtualTouchViewports.equals(mVirtualTouchViewports)) { |
| 1277 | mTempVirtualTouchViewports.clear(); |
| 1278 | for (DisplayViewport d : mVirtualTouchViewports) { |
| 1279 | mTempVirtualTouchViewports.add(d.makeCopy()); |
| 1280 | } |
| 1281 | } |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1282 | } |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1283 | mInputManagerInternal.setDisplayViewports(mTempDefaultViewport, |
| 1284 | mTempExternalTouchViewport, mTempVirtualTouchViewports); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1285 | break; |
| 1286 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | private final class DisplayAdapterListener implements DisplayAdapter.Listener { |
| 1292 | @Override |
| 1293 | public void onDisplayDeviceEvent(DisplayDevice device, int event) { |
| 1294 | switch (event) { |
| 1295 | case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED: |
| 1296 | handleDisplayDeviceAdded(device); |
| 1297 | break; |
| 1298 | |
| 1299 | case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED: |
| 1300 | handleDisplayDeviceChanged(device); |
| 1301 | break; |
| 1302 | |
| 1303 | case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED: |
| 1304 | handleDisplayDeviceRemoved(device); |
| 1305 | break; |
| 1306 | } |
| 1307 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1308 | |
| 1309 | @Override |
| 1310 | public void onTraversalRequested() { |
| 1311 | synchronized (mSyncRoot) { |
Craig Mautner | 65d11b3 | 2012-10-01 13:59:52 -0700 | [diff] [blame] | 1312 | scheduleTraversalLocked(false); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1313 | } |
| 1314 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | private final class CallbackRecord implements DeathRecipient { |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 1318 | public final int mPid; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1319 | private final IDisplayManagerCallback mCallback; |
| 1320 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 1321 | public boolean mWifiDisplayScanRequested; |
| 1322 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1323 | public CallbackRecord(int pid, IDisplayManagerCallback callback) { |
| 1324 | mPid = pid; |
| 1325 | mCallback = callback; |
| 1326 | } |
| 1327 | |
| 1328 | @Override |
| 1329 | public void binderDied() { |
| 1330 | if (DEBUG) { |
| 1331 | Slog.d(TAG, "Display listener for pid " + mPid + " died."); |
| 1332 | } |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 1333 | onCallbackDied(this); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | public void notifyDisplayEventAsync(int displayId, int event) { |
| 1337 | try { |
| 1338 | mCallback.onDisplayEvent(displayId, event); |
| 1339 | } catch (RemoteException ex) { |
| 1340 | Slog.w(TAG, "Failed to notify process " |
| 1341 | + mPid + " that displays changed, assuming it died.", ex); |
| 1342 | binderDied(); |
| 1343 | } |
| 1344 | } |
| 1345 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1346 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1347 | @VisibleForTesting |
| 1348 | final class BinderService extends IDisplayManager.Stub { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1349 | /** |
| 1350 | * Returns information about the specified logical display. |
| 1351 | * |
| 1352 | * @param displayId The logical display id. |
| 1353 | * @return The logical display info, or null if the display does not exist. The |
| 1354 | * returned object must be treated as immutable. |
| 1355 | */ |
| 1356 | @Override // Binder call |
| 1357 | public DisplayInfo getDisplayInfo(int displayId) { |
| 1358 | final int callingUid = Binder.getCallingUid(); |
| 1359 | final long token = Binder.clearCallingIdentity(); |
| 1360 | try { |
| 1361 | return getDisplayInfoInternal(displayId, callingUid); |
| 1362 | } finally { |
| 1363 | Binder.restoreCallingIdentity(token); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /** |
| 1368 | * Returns the list of all display ids. |
| 1369 | */ |
| 1370 | @Override // Binder call |
| 1371 | public int[] getDisplayIds() { |
| 1372 | final int callingUid = Binder.getCallingUid(); |
| 1373 | final long token = Binder.clearCallingIdentity(); |
| 1374 | try { |
| 1375 | return getDisplayIdsInternal(callingUid); |
| 1376 | } finally { |
| 1377 | Binder.restoreCallingIdentity(token); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | @Override // Binder call |
| 1382 | public void registerCallback(IDisplayManagerCallback callback) { |
| 1383 | if (callback == null) { |
| 1384 | throw new IllegalArgumentException("listener must not be null"); |
| 1385 | } |
| 1386 | |
| 1387 | final int callingPid = Binder.getCallingPid(); |
| 1388 | final long token = Binder.clearCallingIdentity(); |
| 1389 | try { |
| 1390 | registerCallbackInternal(callback, callingPid); |
| 1391 | } finally { |
| 1392 | Binder.restoreCallingIdentity(token); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | @Override // Binder call |
| 1397 | public void startWifiDisplayScan() { |
| 1398 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1399 | "Permission required to start wifi display scans"); |
| 1400 | |
| 1401 | final int callingPid = Binder.getCallingPid(); |
| 1402 | final long token = Binder.clearCallingIdentity(); |
| 1403 | try { |
| 1404 | startWifiDisplayScanInternal(callingPid); |
| 1405 | } finally { |
| 1406 | Binder.restoreCallingIdentity(token); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | @Override // Binder call |
| 1411 | public void stopWifiDisplayScan() { |
| 1412 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1413 | "Permission required to stop wifi display scans"); |
| 1414 | |
| 1415 | final int callingPid = Binder.getCallingPid(); |
| 1416 | final long token = Binder.clearCallingIdentity(); |
| 1417 | try { |
| 1418 | stopWifiDisplayScanInternal(callingPid); |
| 1419 | } finally { |
| 1420 | Binder.restoreCallingIdentity(token); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | @Override // Binder call |
| 1425 | public void connectWifiDisplay(String address) { |
| 1426 | if (address == null) { |
| 1427 | throw new IllegalArgumentException("address must not be null"); |
| 1428 | } |
| 1429 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1430 | "Permission required to connect to a wifi display"); |
| 1431 | |
| 1432 | final long token = Binder.clearCallingIdentity(); |
| 1433 | try { |
| 1434 | connectWifiDisplayInternal(address); |
| 1435 | } finally { |
| 1436 | Binder.restoreCallingIdentity(token); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | @Override // Binder call |
| 1441 | public void disconnectWifiDisplay() { |
| 1442 | // This request does not require special permissions. |
| 1443 | // Any app can request disconnection from the currently active wifi display. |
| 1444 | // This exception should no longer be needed once wifi display control moves |
| 1445 | // to the media router service. |
| 1446 | |
| 1447 | final long token = Binder.clearCallingIdentity(); |
| 1448 | try { |
| 1449 | disconnectWifiDisplayInternal(); |
| 1450 | } finally { |
| 1451 | Binder.restoreCallingIdentity(token); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | @Override // Binder call |
| 1456 | public void renameWifiDisplay(String address, String alias) { |
| 1457 | if (address == null) { |
| 1458 | throw new IllegalArgumentException("address must not be null"); |
| 1459 | } |
| 1460 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1461 | "Permission required to rename to a wifi display"); |
| 1462 | |
| 1463 | final long token = Binder.clearCallingIdentity(); |
| 1464 | try { |
| 1465 | renameWifiDisplayInternal(address, alias); |
| 1466 | } finally { |
| 1467 | Binder.restoreCallingIdentity(token); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | @Override // Binder call |
| 1472 | public void forgetWifiDisplay(String address) { |
| 1473 | if (address == null) { |
| 1474 | throw new IllegalArgumentException("address must not be null"); |
| 1475 | } |
| 1476 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1477 | "Permission required to forget to a wifi display"); |
| 1478 | |
| 1479 | final long token = Binder.clearCallingIdentity(); |
| 1480 | try { |
| 1481 | forgetWifiDisplayInternal(address); |
| 1482 | } finally { |
| 1483 | Binder.restoreCallingIdentity(token); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | @Override // Binder call |
| 1488 | public void pauseWifiDisplay() { |
| 1489 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1490 | "Permission required to pause a wifi display session"); |
| 1491 | |
| 1492 | final long token = Binder.clearCallingIdentity(); |
| 1493 | try { |
| 1494 | pauseWifiDisplayInternal(); |
| 1495 | } finally { |
| 1496 | Binder.restoreCallingIdentity(token); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | @Override // Binder call |
| 1501 | public void resumeWifiDisplay() { |
| 1502 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY, |
| 1503 | "Permission required to resume a wifi display session"); |
| 1504 | |
| 1505 | final long token = Binder.clearCallingIdentity(); |
| 1506 | try { |
| 1507 | resumeWifiDisplayInternal(); |
| 1508 | } finally { |
| 1509 | Binder.restoreCallingIdentity(token); |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | @Override // Binder call |
| 1514 | public WifiDisplayStatus getWifiDisplayStatus() { |
| 1515 | // This request does not require special permissions. |
| 1516 | // Any app can get information about available wifi displays. |
| 1517 | |
| 1518 | final long token = Binder.clearCallingIdentity(); |
| 1519 | try { |
| 1520 | return getWifiDisplayStatusInternal(); |
| 1521 | } finally { |
| 1522 | Binder.restoreCallingIdentity(token); |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | @Override // Binder call |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 1527 | public void requestColorMode(int displayId, int colorMode) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 1528 | mContext.enforceCallingOrSelfPermission( |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 1529 | Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE, |
| 1530 | "Permission required to change the display color mode"); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 1531 | final long token = Binder.clearCallingIdentity(); |
| 1532 | try { |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 1533 | requestColorModeInternal(displayId, colorMode); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 1534 | } finally { |
| 1535 | Binder.restoreCallingIdentity(token); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1540 | public int createVirtualDisplay(IVirtualDisplayCallback callback, |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1541 | IMediaProjection projection, String packageName, String name, |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1542 | int width, int height, int densityDpi, Surface surface, int flags, |
| 1543 | String uniqueId) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1544 | final int callingUid = Binder.getCallingUid(); |
| 1545 | if (!validatePackageName(callingUid, packageName)) { |
| 1546 | throw new SecurityException("packageName must match the calling uid"); |
| 1547 | } |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1548 | if (callback == null) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1549 | throw new IllegalArgumentException("appToken must not be null"); |
| 1550 | } |
| 1551 | if (TextUtils.isEmpty(name)) { |
| 1552 | throw new IllegalArgumentException("name must be non-null and non-empty"); |
| 1553 | } |
| 1554 | if (width <= 0 || height <= 0 || densityDpi <= 0) { |
| 1555 | throw new IllegalArgumentException("width, height, and densityDpi must be " |
| 1556 | + "greater than 0"); |
| 1557 | } |
Pablo Ceballos | eb3370d | 2016-08-31 15:00:17 -0700 | [diff] [blame] | 1558 | if (surface != null && surface.isSingleBuffered()) { |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 1559 | throw new IllegalArgumentException("Surface can't be single-buffered"); |
| 1560 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1561 | |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 1562 | if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) { |
| 1563 | flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR; |
| 1564 | |
| 1565 | // Public displays can't be allowed to show content when locked. |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 1566 | if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) { |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 1567 | throw new IllegalArgumentException( |
| 1568 | "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE"); |
| 1569 | } |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 1570 | } |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 1571 | if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) { |
| 1572 | flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR; |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1575 | if (projection != null) { |
| 1576 | try { |
| 1577 | if (!getProjectionService().isValidMediaProjection(projection)) { |
| 1578 | throw new SecurityException("Invalid media projection"); |
| 1579 | } |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 1580 | flags = projection.applyVirtualDisplayFlags(flags); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1581 | } catch (RemoteException e) { |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 1582 | throw new SecurityException("unable to validate media projection or flags"); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 1586 | if (callingUid != Process.SYSTEM_UID && |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 1587 | (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1588 | if (!canProjectVideo(projection)) { |
| 1589 | throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or " |
| 1590 | + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate " |
| 1591 | + "MediaProjection token in order to create a screen sharing virtual " |
| 1592 | + "display."); |
| 1593 | } |
| 1594 | } |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 1595 | if ((flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1596 | if (!canProjectSecureVideo(projection)) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1597 | throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT " |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1598 | + "or an appropriate MediaProjection token to create a " |
| 1599 | + "secure virtual display."); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | final long token = Binder.clearCallingIdentity(); |
| 1604 | try { |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 1605 | return createVirtualDisplayInternal(callback, projection, callingUid, packageName, |
| 1606 | name, width, height, densityDpi, surface, flags, uniqueId); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1607 | } finally { |
| 1608 | Binder.restoreCallingIdentity(token); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1613 | public void resizeVirtualDisplay(IVirtualDisplayCallback callback, |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 1614 | int width, int height, int densityDpi) { |
| 1615 | final long token = Binder.clearCallingIdentity(); |
| 1616 | try { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1617 | resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi); |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 1618 | } finally { |
| 1619 | Binder.restoreCallingIdentity(token); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1624 | public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) { |
Pablo Ceballos | eb3370d | 2016-08-31 15:00:17 -0700 | [diff] [blame] | 1625 | if (surface != null && surface.isSingleBuffered()) { |
| 1626 | throw new IllegalArgumentException("Surface can't be single-buffered"); |
| 1627 | } |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 1628 | final long token = Binder.clearCallingIdentity(); |
| 1629 | try { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1630 | setVirtualDisplaySurfaceInternal(callback.asBinder(), surface); |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 1631 | } finally { |
| 1632 | Binder.restoreCallingIdentity(token); |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1637 | public void releaseVirtualDisplay(IVirtualDisplayCallback callback) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1638 | final long token = Binder.clearCallingIdentity(); |
| 1639 | try { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 1640 | releaseVirtualDisplayInternal(callback.asBinder()); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1641 | } finally { |
| 1642 | Binder.restoreCallingIdentity(token); |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | @Override // Binder call |
| 1647 | public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1648 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1649 | |
| 1650 | final long token = Binder.clearCallingIdentity(); |
| 1651 | try { |
| 1652 | dumpInternal(pw); |
| 1653 | } finally { |
| 1654 | Binder.restoreCallingIdentity(token); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | private boolean validatePackageName(int uid, String packageName) { |
| 1659 | if (packageName != null) { |
| 1660 | String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid); |
| 1661 | if (packageNames != null) { |
| 1662 | for (String n : packageNames) { |
| 1663 | if (n.equals(packageName)) { |
| 1664 | return true; |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | return false; |
| 1670 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1671 | |
| 1672 | private boolean canProjectVideo(IMediaProjection projection) { |
| 1673 | if (projection != null) { |
| 1674 | try { |
| 1675 | if (projection.canProjectVideo()) { |
| 1676 | return true; |
| 1677 | } |
| 1678 | } catch (RemoteException e) { |
| 1679 | Slog.e(TAG, "Unable to query projection service for permissions", e); |
| 1680 | } |
| 1681 | } |
| 1682 | if (mContext.checkCallingPermission( |
| 1683 | android.Manifest.permission.CAPTURE_VIDEO_OUTPUT) |
Michael Wright | 0ccc2b0 | 2014-07-24 18:20:41 -0700 | [diff] [blame] | 1684 | == PackageManager.PERMISSION_GRANTED) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1685 | return true; |
| 1686 | } |
| 1687 | return canProjectSecureVideo(projection); |
| 1688 | } |
| 1689 | |
| 1690 | private boolean canProjectSecureVideo(IMediaProjection projection) { |
| 1691 | if (projection != null) { |
| 1692 | try { |
| 1693 | if (projection.canProjectSecureVideo()){ |
| 1694 | return true; |
| 1695 | } |
| 1696 | } catch (RemoteException e) { |
| 1697 | Slog.e(TAG, "Unable to query projection service for permissions", e); |
| 1698 | } |
| 1699 | } |
| 1700 | return mContext.checkCallingPermission( |
| 1701 | android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT) |
Michael Wright | 0ccc2b0 | 2014-07-24 18:20:41 -0700 | [diff] [blame] | 1702 | == PackageManager.PERMISSION_GRANTED; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 1703 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | private final class LocalService extends DisplayManagerInternal { |
| 1707 | @Override |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1708 | public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler, |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 1709 | SensorManager sensorManager) { |
| 1710 | synchronized (mSyncRoot) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1711 | DisplayBlanker blanker = new DisplayBlanker() { |
| 1712 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 1713 | public void requestDisplayState(int state, int brightness) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1714 | // The order of operations is important for legacy reasons. |
| 1715 | if (state == Display.STATE_OFF) { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 1716 | requestGlobalDisplayStateInternal(state, brightness); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | callbacks.onDisplayStateChange(state); |
| 1720 | |
| 1721 | if (state != Display.STATE_OFF) { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 1722 | requestGlobalDisplayStateInternal(state, brightness); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | }; |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 1726 | mDisplayPowerController = new DisplayPowerController( |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1727 | mContext, callbacks, handler, sensorManager, blanker); |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | @Override |
| 1732 | public boolean requestPowerState(DisplayPowerRequest request, |
| 1733 | boolean waitForNegativeProximity) { |
| 1734 | return mDisplayPowerController.requestPowerState(request, |
| 1735 | waitForNegativeProximity); |
| 1736 | } |
| 1737 | |
| 1738 | @Override |
| 1739 | public boolean isProximitySensorAvailable() { |
| 1740 | return mDisplayPowerController.isProximitySensorAvailable(); |
| 1741 | } |
| 1742 | |
| 1743 | @Override |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1744 | public DisplayInfo getDisplayInfo(int displayId) { |
| 1745 | return getDisplayInfoInternal(displayId, Process.myUid()); |
| 1746 | } |
| 1747 | |
| 1748 | @Override |
| 1749 | public void registerDisplayTransactionListener(DisplayTransactionListener listener) { |
| 1750 | if (listener == null) { |
| 1751 | throw new IllegalArgumentException("listener must not be null"); |
| 1752 | } |
| 1753 | |
| 1754 | registerDisplayTransactionListenerInternal(listener); |
| 1755 | } |
| 1756 | |
| 1757 | @Override |
| 1758 | public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) { |
| 1759 | if (listener == null) { |
| 1760 | throw new IllegalArgumentException("listener must not be null"); |
| 1761 | } |
| 1762 | |
| 1763 | unregisterDisplayTransactionListenerInternal(listener); |
| 1764 | } |
| 1765 | |
| 1766 | @Override |
| 1767 | public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) { |
| 1768 | setDisplayInfoOverrideFromWindowManagerInternal(displayId, info); |
| 1769 | } |
| 1770 | |
| 1771 | @Override |
Andrii Kulian | cd09799 | 2017-03-23 18:31:59 -0700 | [diff] [blame] | 1772 | public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) { |
| 1773 | getNonOverrideDisplayInfoInternal(displayId, outInfo); |
| 1774 | } |
| 1775 | |
| 1776 | @Override |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1777 | public void performTraversalInTransactionFromWindowManager() { |
| 1778 | performTraversalInTransactionFromWindowManagerInternal(); |
| 1779 | } |
| 1780 | |
| 1781 | @Override |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 1782 | public void setDisplayProperties(int displayId, boolean hasContent, |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 1783 | float requestedRefreshRate, int requestedMode, boolean inTraversal) { |
| 1784 | setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate, |
| 1785 | requestedMode, inTraversal); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1786 | } |
Filip Gruszczynski | d2e8640 | 2015-02-19 13:05:03 -0800 | [diff] [blame] | 1787 | |
| 1788 | @Override |
| 1789 | public void setDisplayOffsets(int displayId, int x, int y) { |
| 1790 | setDisplayOffsetsInternal(displayId, x, y); |
| 1791 | } |
Andrii Kulian | fb1bf69 | 2017-01-17 11:17:34 -0800 | [diff] [blame] | 1792 | |
| 1793 | @Override |
| 1794 | public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) { |
| 1795 | setDisplayAccessUIDsInternal(newDisplayAccessUIDs); |
| 1796 | } |
| 1797 | |
| 1798 | @Override |
| 1799 | public boolean isUidPresentOnDisplay(int uid, int displayId) { |
| 1800 | return isUidPresentOnDisplayInternal(uid, displayId); |
| 1801 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1802 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 1803 | } |