From d60db29295e57021554448036f4af8e6a18ff767 Mon Sep 17 00:00:00 2001 From: Jarvis Huang Date: Tue, 22 Feb 2022 11:14:41 +0800 Subject: [PATCH] aux/android: Refactor SystemUiController --- .../monado/auxiliary/MonadoView.java | 4 +-- .../monado/auxiliary/SystemUiController.kt | 30 +++++++++---------- .../org/freedesktop/monado/ipc/Client.java | 3 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java index fe48efac6..f8dd21e64 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java @@ -51,7 +51,7 @@ public class MonadoView extends SurfaceView Activity activity; if (context instanceof Activity) { activity = (Activity) context; - systemUiController = new SystemUiController(activity); + systemUiController = new SystemUiController(activity.getWindow().getDecorView()); systemUiController.hide(); } else { activity = null; @@ -63,7 +63,7 @@ public class MonadoView extends SurfaceView super(activity); this.context = activity; this.activity = activity; - systemUiController = new SystemUiController(activity); + systemUiController = new SystemUiController(activity.getWindow().getDecorView()); systemUiController.hide(); } diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt index 3a2636a6c..4058d019c 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt @@ -8,20 +8,21 @@ */ package org.freedesktop.monado.auxiliary -import android.app.Activity import android.os.Build +import android.os.Handler +import android.os.Looper import android.view.View import android.view.WindowInsets import android.view.WindowInsetsController import androidx.annotation.RequiresApi /** Helper class that handles system ui visibility. */ -class SystemUiController(activity: Activity) { +class SystemUiController(view: View) { private val impl: Impl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - WindowInsetsControllerImpl(activity) + WindowInsetsControllerImpl(view) } else { - SystemUiVisibilityImpl(activity) + SystemUiVisibilityImpl(view) } /** Hide system ui and make fullscreen. */ @@ -29,19 +30,18 @@ class SystemUiController(activity: Activity) { impl.hide() } - private abstract class Impl(var activity: Activity) { + private abstract class Impl(var view: View) { + private val uiHandler: Handler = Handler(Looper.getMainLooper()) abstract fun hide() fun runOnUiThread(runnable: Runnable) { - activity.runOnUiThread(runnable) + uiHandler.post(runnable) } } @Suppress("DEPRECATION") - private class SystemUiVisibilityImpl(activity: Activity) : Impl(activity) { + private class SystemUiVisibilityImpl(view: View) : Impl(view) { override fun hide() { - activity.runOnUiThread { - activity.window.decorView.systemUiVisibility = FLAG_FULL_SCREEN_IMMERSIVE_STICKY - } + runOnUiThread { view.systemUiVisibility = FLAG_FULL_SCREEN_IMMERSIVE_STICKY } } companion object { @@ -59,7 +59,7 @@ class SystemUiController(activity: Activity) { init { runOnUiThread { - activity.window.decorView.setOnSystemUiVisibilityChangeListener { visibility: Int -> + view.setOnSystemUiVisibilityChangeListener { visibility: Int -> // If not fullscreen, fix it. if (0 == visibility and View.SYSTEM_UI_FLAG_FULLSCREEN) { hide() @@ -70,10 +70,10 @@ class SystemUiController(activity: Activity) { } @RequiresApi(api = Build.VERSION_CODES.R) - private class WindowInsetsControllerImpl(activity: Activity) : Impl(activity) { + private class WindowInsetsControllerImpl(view: View) : Impl(view) { override fun hide() { - activity.runOnUiThread { - val controller = activity.window.insetsController + runOnUiThread { + val controller = view.windowInsetsController controller!!.hide( WindowInsets.Type.displayCutout() or WindowInsets.Type.statusBars() or @@ -86,7 +86,7 @@ class SystemUiController(activity: Activity) { init { runOnUiThread { - activity.window.insetsController!!.addOnControllableInsetsChangedListener { + view.windowInsetsController?.addOnControllableInsetsChangedListener { _: WindowInsetsController?, typeMask: Int -> if ( diff --git a/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/Client.java b/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/Client.java index aee97dcb3..380ab5db4 100644 --- a/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/Client.java +++ b/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/Client.java @@ -191,7 +191,8 @@ public class Client implements ServiceConnection { } if (activity != null) { - systemUiController = new SystemUiController(activity); + systemUiController = + new SystemUiController(activity.getWindow().getDecorView()); systemUiController.hide(); } })