Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -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 android.hardware.display; |
| 18 | |
| 19 | import android.content.Context; |
Kenny Guy | 22bd044 | 2017-10-26 00:15:54 +0100 | [diff] [blame] | 20 | import android.content.pm.ParceledListSlice; |
Bryce Lee | 609bf65 | 2017-02-09 16:50:13 -0800 | [diff] [blame] | 21 | import android.content.res.Resources; |
Michael Wright | eedcbf1 | 2017-08-16 23:14:54 +0100 | [diff] [blame] | 22 | import android.graphics.Point; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 23 | import android.hardware.display.DisplayManager.DisplayListener; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 24 | import android.media.projection.IMediaProjection; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 25 | import android.media.projection.MediaProjection; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
| 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | import android.util.SparseArray; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 35 | import android.view.Display; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 36 | import android.view.DisplayAdjustments; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 37 | import android.view.DisplayInfo; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 38 | import android.view.Surface; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
Kenny Guy | 22bd044 | 2017-10-26 00:15:54 +0100 | [diff] [blame] | 41 | import java.util.Collections; |
| 42 | import java.util.List; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Manager communication with the display manager service on behalf of |
| 46 | * an application process. You're probably looking for {@link DisplayManager}. |
| 47 | * |
| 48 | * @hide |
| 49 | */ |
| 50 | public final class DisplayManagerGlobal { |
| 51 | private static final String TAG = "DisplayManager"; |
| 52 | private static final boolean DEBUG = false; |
| 53 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 54 | // True if display info and display ids should be cached. |
| 55 | // |
| 56 | // FIXME: The cache is currently disabled because it's unclear whether we have the |
| 57 | // necessary guarantees that the caches will always be flushed before clients |
| 58 | // attempt to observe their new state. For example, depending on the order |
| 59 | // in which the binder transactions take place, we might have a problem where |
| 60 | // an application could start processing a configuration change due to a display |
| 61 | // orientation change before the display info cache has actually been invalidated. |
| 62 | private static final boolean USE_CACHE = false; |
| 63 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 64 | public static final int EVENT_DISPLAY_ADDED = 1; |
| 65 | public static final int EVENT_DISPLAY_CHANGED = 2; |
| 66 | public static final int EVENT_DISPLAY_REMOVED = 3; |
| 67 | |
| 68 | private static DisplayManagerGlobal sInstance; |
| 69 | |
| 70 | private final Object mLock = new Object(); |
| 71 | |
| 72 | private final IDisplayManager mDm; |
| 73 | |
| 74 | private DisplayManagerCallback mCallback; |
| 75 | private final ArrayList<DisplayListenerDelegate> mDisplayListeners = |
| 76 | new ArrayList<DisplayListenerDelegate>(); |
| 77 | |
| 78 | private final SparseArray<DisplayInfo> mDisplayInfoCache = new SparseArray<DisplayInfo>(); |
| 79 | private int[] mDisplayIdCache; |
| 80 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 81 | private int mWifiDisplayScanNestCount; |
| 82 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 83 | private DisplayManagerGlobal(IDisplayManager dm) { |
| 84 | mDm = dm; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Gets an instance of the display manager global singleton. |
| 89 | * |
| 90 | * @return The display manager instance, may be null early in system startup |
| 91 | * before the display manager has been fully initialized. |
| 92 | */ |
| 93 | public static DisplayManagerGlobal getInstance() { |
| 94 | synchronized (DisplayManagerGlobal.class) { |
| 95 | if (sInstance == null) { |
| 96 | IBinder b = ServiceManager.getService(Context.DISPLAY_SERVICE); |
| 97 | if (b != null) { |
| 98 | sInstance = new DisplayManagerGlobal(IDisplayManager.Stub.asInterface(b)); |
| 99 | } |
| 100 | } |
| 101 | return sInstance; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get information about a particular logical display. |
| 107 | * |
| 108 | * @param displayId The logical display id. |
| 109 | * @return Information about the specified display, or null if it does not exist. |
| 110 | * This object belongs to an internal cache and should be treated as if it were immutable. |
| 111 | */ |
| 112 | public DisplayInfo getDisplayInfo(int displayId) { |
| 113 | try { |
| 114 | synchronized (mLock) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 115 | DisplayInfo info; |
| 116 | if (USE_CACHE) { |
| 117 | info = mDisplayInfoCache.get(displayId); |
| 118 | if (info != null) { |
| 119 | return info; |
| 120 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | info = mDm.getDisplayInfo(displayId); |
| 124 | if (info == null) { |
| 125 | return null; |
| 126 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 127 | |
| 128 | if (USE_CACHE) { |
| 129 | mDisplayInfoCache.put(displayId, info); |
| 130 | } |
| 131 | registerCallbackIfNeededLocked(); |
| 132 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 133 | if (DEBUG) { |
| 134 | Log.d(TAG, "getDisplayInfo: displayId=" + displayId + ", info=" + info); |
| 135 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 136 | return info; |
| 137 | } |
| 138 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 139 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Gets all currently valid logical display ids. |
| 145 | * |
| 146 | * @return An array containing all display ids. |
| 147 | */ |
| 148 | public int[] getDisplayIds() { |
| 149 | try { |
| 150 | synchronized (mLock) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 151 | if (USE_CACHE) { |
| 152 | if (mDisplayIdCache != null) { |
| 153 | return mDisplayIdCache; |
| 154 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 155 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 156 | |
| 157 | int[] displayIds = mDm.getDisplayIds(); |
| 158 | if (USE_CACHE) { |
| 159 | mDisplayIdCache = displayIds; |
| 160 | } |
| 161 | registerCallbackIfNeededLocked(); |
| 162 | return displayIds; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 163 | } |
| 164 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 165 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Gets information about a logical display. |
| 171 | * |
| 172 | * The display metrics may be adjusted to provide compatibility |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 173 | * for legacy applications or limited screen areas. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 174 | * |
| 175 | * @param displayId The logical display id. |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 176 | * @param daj The compatibility info and activityToken. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 177 | * @return The display object, or null if there is no display with the given id. |
| 178 | */ |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 179 | public Display getCompatibleDisplay(int displayId, DisplayAdjustments daj) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 180 | DisplayInfo displayInfo = getDisplayInfo(displayId); |
| 181 | if (displayInfo == null) { |
| 182 | return null; |
| 183 | } |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 184 | return new Display(this, displayId, displayInfo, daj); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Bryce Lee | 609bf65 | 2017-02-09 16:50:13 -0800 | [diff] [blame] | 188 | * Gets information about a logical display. |
| 189 | * |
| 190 | * The display metrics may be adjusted to provide compatibility |
| 191 | * for legacy applications or limited screen areas. |
| 192 | * |
| 193 | * @param displayId The logical display id. |
| 194 | * @param resources Resources providing compatibility info. |
| 195 | * @return The display object, or null if there is no display with the given id. |
| 196 | */ |
| 197 | public Display getCompatibleDisplay(int displayId, Resources resources) { |
| 198 | DisplayInfo displayInfo = getDisplayInfo(displayId); |
| 199 | if (displayInfo == null) { |
| 200 | return null; |
| 201 | } |
| 202 | return new Display(this, displayId, displayInfo, resources); |
| 203 | } |
| 204 | |
| 205 | /** |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 206 | * Gets information about a logical display without applying any compatibility metrics. |
| 207 | * |
| 208 | * @param displayId The logical display id. |
| 209 | * @return The display object, or null if there is no display with the given id. |
| 210 | */ |
| 211 | public Display getRealDisplay(int displayId) { |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 212 | return getCompatibleDisplay(displayId, DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); |
| 213 | } |
| 214 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 215 | public void registerDisplayListener(DisplayListener listener, Handler handler) { |
| 216 | if (listener == null) { |
| 217 | throw new IllegalArgumentException("listener must not be null"); |
| 218 | } |
| 219 | |
| 220 | synchronized (mLock) { |
| 221 | int index = findDisplayListenerLocked(listener); |
| 222 | if (index < 0) { |
| 223 | mDisplayListeners.add(new DisplayListenerDelegate(listener, handler)); |
| 224 | registerCallbackIfNeededLocked(); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public void unregisterDisplayListener(DisplayListener listener) { |
| 230 | if (listener == null) { |
| 231 | throw new IllegalArgumentException("listener must not be null"); |
| 232 | } |
| 233 | |
| 234 | synchronized (mLock) { |
| 235 | int index = findDisplayListenerLocked(listener); |
| 236 | if (index >= 0) { |
| 237 | DisplayListenerDelegate d = mDisplayListeners.get(index); |
| 238 | d.clearEvents(); |
| 239 | mDisplayListeners.remove(index); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private int findDisplayListenerLocked(DisplayListener listener) { |
| 245 | final int numListeners = mDisplayListeners.size(); |
| 246 | for (int i = 0; i < numListeners; i++) { |
| 247 | if (mDisplayListeners.get(i).mListener == listener) { |
| 248 | return i; |
| 249 | } |
| 250 | } |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | private void registerCallbackIfNeededLocked() { |
| 255 | if (mCallback == null) { |
| 256 | mCallback = new DisplayManagerCallback(); |
| 257 | try { |
| 258 | mDm.registerCallback(mCallback); |
| 259 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 260 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private void handleDisplayEvent(int displayId, int event) { |
| 266 | synchronized (mLock) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 267 | if (USE_CACHE) { |
| 268 | mDisplayInfoCache.remove(displayId); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 269 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 270 | if (event == EVENT_DISPLAY_ADDED || event == EVENT_DISPLAY_REMOVED) { |
| 271 | mDisplayIdCache = null; |
| 272 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | final int numListeners = mDisplayListeners.size(); |
| 276 | for (int i = 0; i < numListeners; i++) { |
| 277 | mDisplayListeners.get(i).sendDisplayEvent(displayId, event); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 282 | public void startWifiDisplayScan() { |
| 283 | synchronized (mLock) { |
| 284 | if (mWifiDisplayScanNestCount++ == 0) { |
| 285 | registerCallbackIfNeededLocked(); |
| 286 | try { |
| 287 | mDm.startWifiDisplayScan(); |
| 288 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 289 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | public void stopWifiDisplayScan() { |
| 296 | synchronized (mLock) { |
| 297 | if (--mWifiDisplayScanNestCount == 0) { |
| 298 | try { |
| 299 | mDm.stopWifiDisplayScan(); |
| 300 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 301 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 302 | } |
| 303 | } else if (mWifiDisplayScanNestCount < 0) { |
| 304 | Log.wtf(TAG, "Wifi display scan nest count became negative: " |
| 305 | + mWifiDisplayScanNestCount); |
| 306 | mWifiDisplayScanNestCount = 0; |
| 307 | } |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | public void connectWifiDisplay(String deviceAddress) { |
| 312 | if (deviceAddress == null) { |
| 313 | throw new IllegalArgumentException("deviceAddress must not be null"); |
| 314 | } |
| 315 | |
| 316 | try { |
| 317 | mDm.connectWifiDisplay(deviceAddress); |
| 318 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 319 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 323 | public void pauseWifiDisplay() { |
| 324 | try { |
| 325 | mDm.pauseWifiDisplay(); |
| 326 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 327 | throw ex.rethrowFromSystemServer(); |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | public void resumeWifiDisplay() { |
| 332 | try { |
| 333 | mDm.resumeWifiDisplay(); |
| 334 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 335 | throw ex.rethrowFromSystemServer(); |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 339 | public void disconnectWifiDisplay() { |
| 340 | try { |
| 341 | mDm.disconnectWifiDisplay(); |
| 342 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 343 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 347 | public void renameWifiDisplay(String deviceAddress, String alias) { |
| 348 | if (deviceAddress == null) { |
| 349 | throw new IllegalArgumentException("deviceAddress must not be null"); |
| 350 | } |
| 351 | |
| 352 | try { |
| 353 | mDm.renameWifiDisplay(deviceAddress, alias); |
| 354 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 355 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | public void forgetWifiDisplay(String deviceAddress) { |
| 360 | if (deviceAddress == null) { |
| 361 | throw new IllegalArgumentException("deviceAddress must not be null"); |
| 362 | } |
| 363 | |
| 364 | try { |
| 365 | mDm.forgetWifiDisplay(deviceAddress); |
| 366 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 367 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 371 | public WifiDisplayStatus getWifiDisplayStatus() { |
| 372 | try { |
| 373 | return mDm.getWifiDisplayStatus(); |
| 374 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 375 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 379 | public void requestColorMode(int displayId, int colorMode) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 380 | try { |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 381 | mDm.requestColorMode(displayId, colorMode); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 382 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 383 | throw ex.rethrowFromSystemServer(); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 387 | public VirtualDisplay createVirtualDisplay(Context context, MediaProjection projection, |
| 388 | String name, int width, int height, int densityDpi, Surface surface, int flags, |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 389 | VirtualDisplay.Callback callback, Handler handler, String uniqueId) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 390 | if (TextUtils.isEmpty(name)) { |
| 391 | throw new IllegalArgumentException("name must be non-null and non-empty"); |
| 392 | } |
| 393 | if (width <= 0 || height <= 0 || densityDpi <= 0) { |
| 394 | throw new IllegalArgumentException("width, height, and densityDpi must be " |
| 395 | + "greater than 0"); |
| 396 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 397 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 398 | VirtualDisplayCallback callbackWrapper = new VirtualDisplayCallback(callback, handler); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 399 | IMediaProjection projectionToken = projection != null ? projection.getProjection() : null; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 400 | int displayId; |
| 401 | try { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 402 | displayId = mDm.createVirtualDisplay(callbackWrapper, projectionToken, |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 403 | context.getPackageName(), name, width, height, densityDpi, surface, flags, |
| 404 | uniqueId); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 405 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 406 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 407 | } |
| 408 | if (displayId < 0) { |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 409 | Log.e(TAG, "Could not create virtual display: " + name); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 410 | return null; |
| 411 | } |
| 412 | Display display = getRealDisplay(displayId); |
| 413 | if (display == null) { |
| 414 | Log.wtf(TAG, "Could not obtain display info for newly created " |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 415 | + "virtual display: " + name); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 416 | try { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 417 | mDm.releaseVirtualDisplay(callbackWrapper); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 418 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 419 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 420 | } |
| 421 | return null; |
| 422 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 423 | return new VirtualDisplay(this, display, callbackWrapper, surface); |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 426 | public void setVirtualDisplaySurface(IVirtualDisplayCallback token, Surface surface) { |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 427 | try { |
| 428 | mDm.setVirtualDisplaySurface(token, surface); |
| 429 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 430 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 431 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 434 | public void resizeVirtualDisplay(IVirtualDisplayCallback token, |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 435 | int width, int height, int densityDpi) { |
| 436 | try { |
| 437 | mDm.resizeVirtualDisplay(token, width, height, densityDpi); |
| 438 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 439 | throw ex.rethrowFromSystemServer(); |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 443 | public void releaseVirtualDisplay(IVirtualDisplayCallback token) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 444 | try { |
| 445 | mDm.releaseVirtualDisplay(token); |
| 446 | } catch (RemoteException ex) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 447 | throw ex.rethrowFromSystemServer(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Michael Wright | eedcbf1 | 2017-08-16 23:14:54 +0100 | [diff] [blame] | 451 | /** |
| 452 | * Gets the stable device display size, in pixels. |
| 453 | */ |
| 454 | public Point getStableDisplaySize() { |
| 455 | try { |
| 456 | return mDm.getStableDisplaySize(); |
| 457 | } catch (RemoteException ex) { |
| 458 | throw ex.rethrowFromSystemServer(); |
| 459 | } |
| 460 | } |
| 461 | |
Kenny Guy | 22bd044 | 2017-10-26 00:15:54 +0100 | [diff] [blame] | 462 | /** |
| 463 | * Retrieves brightness change events. |
| 464 | */ |
Kenny Guy | 29aa30e | 2017-11-30 13:43:46 +0000 | [diff] [blame] | 465 | public List<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) { |
Kenny Guy | 22bd044 | 2017-10-26 00:15:54 +0100 | [diff] [blame] | 466 | try { |
Kenny Guy | 29aa30e | 2017-11-30 13:43:46 +0000 | [diff] [blame] | 467 | ParceledListSlice<BrightnessChangeEvent> events = |
| 468 | mDm.getBrightnessEvents(callingPackage); |
Kenny Guy | 22bd044 | 2017-10-26 00:15:54 +0100 | [diff] [blame] | 469 | if (events == null) { |
| 470 | return Collections.emptyList(); |
| 471 | } |
| 472 | return events.getList(); |
| 473 | } catch (RemoteException ex) { |
| 474 | throw ex.rethrowFromSystemServer(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Set brightness but don't add a BrightnessChangeEvent |
| 480 | * STOPSHIP remove when adaptive brightness accepts curves. |
| 481 | */ |
| 482 | public void setBrightness(int brightness) { |
| 483 | try { |
| 484 | mDm.setBrightness(brightness); |
| 485 | } catch (RemoteException ex) { |
| 486 | throw ex.rethrowFromSystemServer(); |
| 487 | } |
| 488 | } |
| 489 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 490 | private final class DisplayManagerCallback extends IDisplayManagerCallback.Stub { |
| 491 | @Override |
| 492 | public void onDisplayEvent(int displayId, int event) { |
| 493 | if (DEBUG) { |
| 494 | Log.d(TAG, "onDisplayEvent: displayId=" + displayId + ", event=" + event); |
| 495 | } |
| 496 | handleDisplayEvent(displayId, event); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | private static final class DisplayListenerDelegate extends Handler { |
| 501 | public final DisplayListener mListener; |
| 502 | |
| 503 | public DisplayListenerDelegate(DisplayListener listener, Handler handler) { |
| 504 | super(handler != null ? handler.getLooper() : Looper.myLooper(), null, true /*async*/); |
| 505 | mListener = listener; |
| 506 | } |
| 507 | |
| 508 | public void sendDisplayEvent(int displayId, int event) { |
| 509 | Message msg = obtainMessage(event, displayId, 0); |
| 510 | sendMessage(msg); |
| 511 | } |
| 512 | |
| 513 | public void clearEvents() { |
| 514 | removeCallbacksAndMessages(null); |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void handleMessage(Message msg) { |
| 519 | switch (msg.what) { |
| 520 | case EVENT_DISPLAY_ADDED: |
| 521 | mListener.onDisplayAdded(msg.arg1); |
| 522 | break; |
| 523 | case EVENT_DISPLAY_CHANGED: |
| 524 | mListener.onDisplayChanged(msg.arg1); |
| 525 | break; |
| 526 | case EVENT_DISPLAY_REMOVED: |
| 527 | mListener.onDisplayRemoved(msg.arg1); |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 532 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 533 | private final static class VirtualDisplayCallback extends IVirtualDisplayCallback.Stub { |
| 534 | private VirtualDisplayCallbackDelegate mDelegate; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 535 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 536 | public VirtualDisplayCallback(VirtualDisplay.Callback callback, Handler handler) { |
| 537 | if (callback != null) { |
| 538 | mDelegate = new VirtualDisplayCallbackDelegate(callback, handler); |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 539 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 543 | public void onPaused() { |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 544 | if (mDelegate != null) { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 545 | mDelegate.sendEmptyMessage(VirtualDisplayCallbackDelegate.MSG_DISPLAY_PAUSED); |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 546 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 550 | public void onResumed() { |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 551 | if (mDelegate != null) { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 552 | mDelegate.sendEmptyMessage(VirtualDisplayCallbackDelegate.MSG_DISPLAY_RESUMED); |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 553 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | @Override // Binder call |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 557 | public void onStopped() { |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 558 | if (mDelegate != null) { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 559 | mDelegate.sendEmptyMessage(VirtualDisplayCallbackDelegate.MSG_DISPLAY_STOPPED); |
Craig Mautner | 3d0c57a | 2014-07-21 14:18:29 -0700 | [diff] [blame] | 560 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 564 | private final static class VirtualDisplayCallbackDelegate extends Handler { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 565 | public static final int MSG_DISPLAY_PAUSED = 0; |
| 566 | public static final int MSG_DISPLAY_RESUMED = 1; |
| 567 | public static final int MSG_DISPLAY_STOPPED = 2; |
| 568 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 569 | private final VirtualDisplay.Callback mCallback; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 570 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 571 | public VirtualDisplayCallbackDelegate(VirtualDisplay.Callback callback, |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 572 | Handler handler) { |
| 573 | super(handler != null ? handler.getLooper() : Looper.myLooper(), null, true /*async*/); |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 574 | mCallback = callback; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void handleMessage(Message msg) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 579 | switch (msg.what) { |
| 580 | case MSG_DISPLAY_PAUSED: |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 581 | mCallback.onPaused(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 582 | break; |
| 583 | case MSG_DISPLAY_RESUMED: |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 584 | mCallback.onResumed(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 585 | break; |
| 586 | case MSG_DISPLAY_STOPPED: |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 587 | mCallback.onStopped(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | } |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 592 | } |