Reputation: 9845
As per the title, is there a way to detect when actual screen mirroring is being performed, and not via Android Auto? Some banking apps detect when mirroring and I realise that they also detect Android Auto, which isn't really sheen mirroring.
I know you can detect screen mirroring via DisplayManager.getDisplays()
and see if the return value is >1,but would Android Auto register as a display here? If so, are there ways to rule out this false positive?
Upvotes: 2
Views: 169
Reputation: 209
This approach might work for detecting actual screen mirroring vs Android Auto. Here's a simple solution that should work for most cases:
class ScreenMirrorDetector(private val context: Context) {
fun isScreenBeingMirrored(): Boolean {
val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
// Get all displays
val displays = displayManager.getDisplays()
// Check each display for mirroring flags
return displays.any { display ->
// Simple check for mirror flags
val isMirror = display.flags and DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR != 0
// Make sure it's not private (Android Auto usually is private)
val isNotPrivate = display.flags and DisplayManager.VIRTUAL_DISPLAY_FLAG_PRIVATE == 0
isMirror && isNotPrivate
}
}
}
To use it in your app:
// In your Activity or Fragment
val detector = ScreenMirrorDetector(context)
if (detector.isScreenBeingMirrored()) {
// Screen is being mirrored
}
The main trick here is that Android Auto uses different display flags than regular screen mirroring. While both will show up in getDisplays(), Android Auto typically uses private flags, while regular screen mirroring uses the auto-mirror flag.
I haven't tested this, but have seen this in discussion around android auto on Reddit or Discord, I Hope this helps! Let me know if this works đź‘Ť
Upvotes: 2