diff --git a/displaylib/src/com/android/app/displaylib/DisplayRepository.kt b/displaylib/src/com/android/app/displaylib/DisplayRepository.kt index 4617ca3c..cfc04f16 100644 --- a/displaylib/src/com/android/app/displaylib/DisplayRepository.kt +++ b/displaylib/src/com/android/app/displaylib/DisplayRepository.kt @@ -361,9 +361,7 @@ constructor( connectedDisplayIds .map { connectedDisplayIds -> traceSection("$TAG#filteringExternalDisplays") { - connectedDisplayIds - .filter { id -> getDisplayType(id) == Display.TYPE_EXTERNAL } - .toSet() + connectedDisplayIds.filter { id -> isExternallyPresented(id) }.toSet() } } .flowOn(backgroundCoroutineDispatcher) @@ -372,6 +370,30 @@ constructor( private fun getDisplayType(displayId: Int): Int? = traceSection("$TAG#getDisplayType") { displayManager.getDisplay(displayId)?.type } + /** + * Whether this display is shown to the user on something other than the built-in screen, and + * so is worth asking about when it connects. + * + * [Display.TYPE_EXTERNAL] is the physical case. A virtual display counts when it carries + * `FLAG_ALLOWS_CONTENT_MODE_SWITCH`, which means its owner is driving a real sink - a cast + * receiver, say - and the display can genuinely be either a mirror or a second desktop. That + * is exactly the question the connection dialog exists to ask, and without this it was only + * ever asked about displays with a cable. + * + * Deliberately narrow: an ordinary virtual display, of which there are many and most of them + * invisible, does not carry that flag and is not offered. + */ + private fun isExternallyPresented(displayId: Int): Boolean = + traceSection("$TAG#isExternallyPresented") { + val display = displayManager.getDisplay(displayId) ?: return@traceSection false + when (display.type) { + Display.TYPE_EXTERNAL -> true + Display.TYPE_VIRTUAL -> + (display.flags and Display.FLAG_ALLOWS_CONTENT_MODE_SWITCH) != 0 + else -> false + } + } + private fun getDisplayFromDisplayManager(displayId: Int): Display? = traceSection("$TAG#getDisplay") { displayManager.getDisplay(displayId) }