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