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