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