aux/android: Refactor SystemUiController

This commit is contained in:
Jarvis Huang 2022-02-22 11:14:41 +08:00 committed by Ryan Pavlik
parent 255a74a8e8
commit d60db29295
3 changed files with 19 additions and 18 deletions

View file

@ -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();
}

View file

@ -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 (

View file

@ -191,7 +191,8 @@ public class Client implements ServiceConnection {
}
if (activity != null) {
systemUiController = new SystemUiController(activity);
systemUiController =
new SystemUiController(activity.getWindow().getDecorView());
systemUiController.hide();
}
})