From c4ce671a87d3d93f837979b1901c8d36a71a6b30 Mon Sep 17 00:00:00 2001 From: Koen J Date: Tue, 3 Sep 2024 19:59:27 +0200 Subject: [PATCH] Fixed crash on Android 10 related to showing and hiding system UI when entering fullscreen. Made Platform comments the default. --- .../AdvancedOrientationListener.kt | 16 +++++-- .../java/com/futo/platformplayer/Settings.kt | 2 +- .../SimpleOrientationListener.kt | 17 +++++-- .../java/com/futo/platformplayer/Utility.kt | 2 - .../platformplayer/activities/MainActivity.kt | 3 ++ .../mainactivity/main/VideoDetailFragment.kt | 44 ++++++++++++++----- 6 files changed, 62 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/futo/platformplayer/AdvancedOrientationListener.kt b/app/src/main/java/com/futo/platformplayer/AdvancedOrientationListener.kt index acc0baca..2b0897d5 100644 --- a/app/src/main/java/com/futo/platformplayer/AdvancedOrientationListener.kt +++ b/app/src/main/java/com/futo/platformplayer/AdvancedOrientationListener.kt @@ -77,10 +77,14 @@ class AdvancedOrientationListener(private val activity: Activity, private val li lastStableOrientation = newOrientation lifecycleScope.launch(Dispatchers.Main) { - delay(stabilityThresholdTime) - if (newOrientation == lastStableOrientation) { - lastOrientation = newOrientation - onOrientationChanged.emit(newOrientation) + try { + delay(stabilityThresholdTime) + if (newOrientation == lastStableOrientation) { + lastOrientation = newOrientation + onOrientationChanged.emit(newOrientation) + } + } catch (e: Throwable) { + Logger.i(TAG, "Failed to trigger onOrientationChanged", e) } } } @@ -111,4 +115,8 @@ class AdvancedOrientationListener(private val activity: Activity, private val li fun stopListening() { sensorManager.unregisterListener(sensorListener) } + + companion object { + private val TAG = "AdvancedOrientationListener" + } } diff --git a/app/src/main/java/com/futo/platformplayer/Settings.kt b/app/src/main/java/com/futo/platformplayer/Settings.kt index eb83e298..4c205620 100644 --- a/app/src/main/java/com/futo/platformplayer/Settings.kt +++ b/app/src/main/java/com/futo/platformplayer/Settings.kt @@ -487,7 +487,7 @@ class Settings : FragmentedStorageFileJson() { class CommentSettings { @FormField(R.string.default_comment_section, FieldForm.DROPDOWN, -1, 0) @DropdownFieldOptionsId(R.array.comment_sections) - var defaultCommentSection: Int = 0; + var defaultCommentSection: Int = 1; @FormField(R.string.bad_reputation_comments_fading, FieldForm.TOGGLE, R.string.bad_reputation_comments_fading_description, 0) var badReputationCommentsFading: Boolean = true; diff --git a/app/src/main/java/com/futo/platformplayer/SimpleOrientationListener.kt b/app/src/main/java/com/futo/platformplayer/SimpleOrientationListener.kt index 24c86095..6d231f9d 100644 --- a/app/src/main/java/com/futo/platformplayer/SimpleOrientationListener.kt +++ b/app/src/main/java/com/futo/platformplayer/SimpleOrientationListener.kt @@ -5,6 +5,7 @@ import android.content.pm.ActivityInfo import android.hardware.SensorManager import android.view.OrientationEventListener import com.futo.platformplayer.constructs.Event1 +import com.futo.platformplayer.logging.Logger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -34,10 +35,14 @@ class SimpleOrientationListener( lastStableOrientation = newOrientation lifecycleScope.launch(Dispatchers.Main) { - delay(stabilityThresholdTime) - if (newOrientation == lastStableOrientation) { - lastOrientation = newOrientation - onOrientationChanged.emit(newOrientation) + try { + delay(stabilityThresholdTime) + if (newOrientation == lastStableOrientation) { + lastOrientation = newOrientation + onOrientationChanged.emit(newOrientation) + } + } catch (e: Throwable) { + Logger.i(TAG, "Failed to trigger onOrientationChanged", e) } } } @@ -52,4 +57,8 @@ class SimpleOrientationListener( fun stopListening() { orientationListener.disable() } + + companion object { + private val TAG = "SimpleOrientationListener" + } } \ No newline at end of file diff --git a/app/src/main/java/com/futo/platformplayer/Utility.kt b/app/src/main/java/com/futo/platformplayer/Utility.kt index 7476c600..6e2dc7b8 100644 --- a/app/src/main/java/com/futo/platformplayer/Utility.kt +++ b/app/src/main/java/com/futo/platformplayer/Utility.kt @@ -147,8 +147,6 @@ fun InputStream.copyToOutputStream(inputStreamLength: Long, outputStream: Output @Suppress("DEPRECATION") fun Activity.setNavigationBarColorAndIcons() { window.navigationBarColor = ContextCompat.getColor(this, android.R.color.black); - if (Settings.instance.playback.allowVideoToGoUnderCutout) - window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { window.insetsController?.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS); diff --git a/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt b/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt index 61153ccf..bd998d38 100644 --- a/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt +++ b/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt @@ -14,6 +14,7 @@ import android.os.StrictMode.VmPolicy import android.util.Log import android.util.TypedValue import android.view.View +import android.view.WindowManager import android.widget.FrameLayout import android.widget.ImageView import androidx.activity.result.ActivityResult @@ -253,6 +254,8 @@ class MainActivity : AppCompatActivity, IWithResultLauncher { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setNavigationBarColorAndIcons(); + if (Settings.instance.playback.allowVideoToGoUnderCutout) + window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES runBlocking { StatePlatform.instance.updateAvailableClients(this@MainActivity); diff --git a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/VideoDetailFragment.kt b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/VideoDetailFragment.kt index bb41c48e..f9c0e280 100644 --- a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/VideoDetailFragment.kt +++ b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/VideoDetailFragment.kt @@ -2,6 +2,7 @@ package com.futo.platformplayer.fragment.mainactivity.main import android.content.pm.ActivityInfo import android.content.res.Configuration +import android.os.Build import android.os.Bundle import android.os.Handler import android.util.Log @@ -10,6 +11,7 @@ import android.view.View import android.view.ViewGroup import android.view.WindowInsets import android.view.WindowInsetsController +import android.view.WindowManager import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.view.WindowCompat import androidx.lifecycle.lifecycleScope @@ -426,22 +428,42 @@ class VideoDetailFragment : MainFragment { onMaximized.clear(); } - private fun hideSystemUI() { - WindowCompat.setDecorFitsSystemWindows(requireActivity().window, false) - activity?.window?.insetsController?.let { controller -> - controller.hide(WindowInsets.Type.statusBars()) - controller.hide(WindowInsets.Type.systemBars()) - controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + WindowCompat.setDecorFitsSystemWindows(requireActivity().window, false) + activity?.window?.insetsController?.let { controller -> + controller.hide(WindowInsets.Type.statusBars()) + controller.hide(WindowInsets.Type.systemBars()) + controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + } + } else { + @Suppress("DEPRECATION") + activity?.window?.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + @Suppress("DEPRECATION") + activity?.window?.decorView?.systemUiVisibility = ( + View.SYSTEM_UI_FLAG_FULLSCREEN + or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY + ) } } private fun showSystemUI() { - WindowCompat.setDecorFitsSystemWindows(requireActivity().window, true) - activity?.window?.insetsController?.let { controller -> - controller.show(WindowInsets.Type.statusBars()) - controller.show(WindowInsets.Type.systemBars()) - controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + WindowCompat.setDecorFitsSystemWindows(requireActivity().window, true) + activity?.window?.insetsController?.let { controller -> + controller.show(WindowInsets.Type.statusBars()) + controller.show(WindowInsets.Type.systemBars()) + controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT + } + } else { + @Suppress("DEPRECATION") + activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) + @Suppress("DEPRECATION") + activity?.window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE } }