Can't back when using AccessibilityService

28 Views Asked by At

I am using AccessibilityService and adding a view to the window in onServiceConnected. It's strange that I can't navigate back when the accessibility service is open.

Here is a screen recording of when the problem occurred: enter image description here

Here are my codes for AccessibilityService:

class BaoziService : AccessibilityService() {

    private val floatingWindow = FloatingWindow(this)

    override fun onCreate() {
        super.onCreate()
        floatingWindow.onCreate()
    }

    override fun onServiceConnected() {
        super.onServiceConnected()

        floatingWindow.setContent {
            Box(contentAlignment = Alignment.Center) {
                Text(text = "", fontSize = 48.sp)
            }
        }
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        Log.d("BaoziService", "onAccessibilityEvent: $event")
    }

    override fun onInterrupt() {

    }

    override fun onDestroy() {
        super.onDestroy()
        floatingWindow.onDestroy()
    }


}

And FloatingWindow implementation is as follows:

class FloatingWindow(private val context: Context) {
    private lateinit var windowManager: WindowManager
    private lateinit var container: ComposeView
    private val lifecycleHelper: LifecycleHelper by lazy {
        LifecycleHelper()
    }

    @CallSuper
    fun onCreate() {
        lifecycleHelper.onCreate()
    }

    fun setContent(content: @Composable () -> Unit) {
        if (this::windowManager.isInitialized.not() && this::container.isInitialized.not()) {
            windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            container = ComposeView(context).apply {
                lifecycleHelper.onAttach(this)
            }

            val lp = WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT
            )

            windowManager.addView(container, lp)
        }

        container.setContent(content)
    }

    @CallSuper
    fun onDestroy() {
        lifecycleHelper.onDestroy()
        if (this::windowManager.isInitialized && this::container.isInitialized)
            windowManager.removeView(container)
    }
}

I tried commenting out windowManager.addView(container, lp), and the navigate back function was restored.

0

There are 0 best solutions below