I am running a foreground service which shows a system overlay. I want to detect change in status bar visibility.
So for example, if any video is being watched or any game is being played, the running activity is in full screen mode and status bar is hidden. But when a user swipes from top or any notification arrives, the status bar is visible.
I want to detect this changes in the status bar's visibility. I have gone through this another post which discussed a similar use case. Not much help. Detect Status Bar Visibility / TYPE_SYSTEM_OVERLAY not resizing automatically
I am using the code below to inflate my system overlay
private fun setupListenerToDetectStatusBar() {
val p = WindowManager.LayoutParams()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
p.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
else
p.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
p.gravity = Gravity.RIGHT or Gravity.TOP
p.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
p.width = 1
p.height = ViewGroup.LayoutParams.MATCH_PARENT
p.format = PixelFormat.OPAQUE
helperWnd = View(ctx) //View helperWnd;
windowManager.addView(helperWnd, p)
Log.d("status-bar", "added-global-layout listener")
helperWnd?.viewTreeObserver?.addOnGlobalLayoutListener(layoutListener)
helperWnd?.setBackgroundColor(Color.BLACK)
helperWnd?.setOnSystemUiVisibilityChangeListener{
Log.d("status-bar", "UI visibility: $it $isFullScreen")
}
}
The GlobalLayoutListener is ineffective and doesn't detect any change while the systemUIVisibilityListener, however deprecated, detects the app going full screen either when the app is launched or the notification shade is open and closed.
I also tried detecting this by using Accessibility service. The change in the visibility of status bar is not being detected by any of the methods. I am able to detect all the other the events in the service.
val LOG_TAG = "MyAccessibilityService"
var recordScreen = false
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
event?.let {
var canIgnore = false
val eventPackage = it.packageName
logger.log("onAccessibilityEvent: A event package $eventPackage")
}
}
Is there any other way this can be achieved?