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