Compare commits

...

3 Commits

7 changed files with 110 additions and 57 deletions
@@ -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"
}
}
@@ -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;
@@ -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"
}
}
@@ -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);
@@ -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);
@@ -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
}
}
@@ -55,11 +55,15 @@ class MediaPlaybackService : Service() {
private var _notificationChannel: NotificationChannel? = null;
private var _mediaSession: MediaSessionCompat? = null;
private var _hasFocus: Boolean = false;
private var _isTransientLoss: Boolean = false;
private var _focusRequest: AudioFocusRequest? = null;
private var _audioFocusLossTime_ms: Long? = null
private var _playbackState = PlaybackStateCompat.STATE_NONE;
private val _handler = Handler(Looper.getMainLooper())
private val _audioFocusRunnable = Runnable { setAudioFocus(false) }
private var _lastAudioFocusAttempt_ms: Long? = null
private val isPlaying get() = _playbackState != PlaybackStateCompat.STATE_PAUSED &&
_playbackState != PlaybackStateCompat.STATE_STOPPED &&
_playbackState != PlaybackStateCompat.STATE_NONE &&
_playbackState != PlaybackStateCompat.STATE_ERROR
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Logger.v(TAG, "onStartCommand");
@@ -161,14 +165,7 @@ class MediaPlaybackService : Service() {
Logger.v(TAG, "closeMediaSession");
stopForeground(STOP_FOREGROUND_REMOVE);
val focusRequest = _focusRequest;
if (focusRequest != null) {
Logger.i(TAG, "Audio focus abandoned")
_handler.removeCallbacks(_audioFocusRunnable)
_audioManager?.abandonAudioFocusRequest(focusRequest);
_focusRequest = null;
}
_hasFocus = false;
abandonAudioFocus()
val notifManager = _notificationManager;
Logger.i(TAG, "Cancelling playback notification (notifManager: ${notifManager != null})");
@@ -339,27 +336,34 @@ class MediaPlaybackService : Service() {
.setState(state, pos, 1f, SystemClock.elapsedRealtime())
.build());
if(_focusRequest == null)
setAudioFocus();
_playbackState = state;
try {
setAudioFocus()
} catch (e: Throwable) {
Logger.e(TAG, "Failed to set audio focus", e)
}
}
//TODO: (TBD) This code probably more fitting inside FutoVideoPlayer, as this service is generally only used for global events
private fun setAudioFocus(createFocusRequest: Boolean = true) {
_handler.removeCallbacks(_audioFocusRunnable)
private fun setAudioFocus() {
if (!isPlaying) {
return
}
if (_hasFocus) {
Log.i(TAG, "Skipped trying to get audio focus because audio focus is already obtained.");
if (_hasFocus || _isTransientLoss) {
return;
}
if (_focusRequest == null) {
if (!createFocusRequest) {
Log.i(TAG, "Skipped trying to get audio focus because createFocusRequest = false and no focus request exists.");
return;
}
val now = System.currentTimeMillis()
val lastAudioFocusAttempt_ms = _lastAudioFocusAttempt_ms
if (lastAudioFocusAttempt_ms == null || now - lastAudioFocusAttempt_ms > 1000) {
_lastAudioFocusAttempt_ms = now
} else {
Log.v(TAG, "Skipped trying to get audio focus because gaining audio focus was recently attempted.");
return
}
if (_focusRequest == null) {
val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(_audioFocusChangeListener)
@@ -374,27 +378,40 @@ class MediaPlaybackService : Service() {
val result = _audioManager?.requestAudioFocus(_focusRequest!!)
Log.i(TAG, "Audio focus request result $result");
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
_hasFocus = true;
_hasFocus = true
_isTransientLoss = false
Log.i(TAG, "Audio focus received");
} else if (result == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
_hasFocus = false
_isTransientLoss = false
Log.i(TAG, "Audio focus delayed, waiting for focus")
} else {
_hasFocus = false
Log.i(TAG, "Audio focus not granted, retrying in 1 second")
_handler.postDelayed(_audioFocusRunnable, 1000)
_isTransientLoss = false
Log.i(TAG, "Audio focus not granted, retrying later")
}
Log.i(TAG, "Audio focus requested.");
}
private fun abandonAudioFocus() {
val focusRequest = _focusRequest;
if (focusRequest != null) {
Logger.i(TAG, "Audio focus abandoned")
_audioManager?.abandonAudioFocusRequest(focusRequest);
_focusRequest = null;
}
_hasFocus = false;
_isTransientLoss = false;
}
private val _audioFocusChangeListener =
OnAudioFocusChangeListener { focusChange ->
try {
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> {
_handler.removeCallbacks(_audioFocusRunnable)
_hasFocus = true;
_isTransientLoss = false;
Log.i(TAG, "Audio focus gained (restartPlaybackAfterLoss = ${Settings.instance.playback.restartPlaybackAfterLoss}, _audioFocusLossTime_ms = $_audioFocusLossTime_ms)");
if (Settings.instance.playback.restartPlaybackAfterLoss == 1) {
@@ -412,31 +429,27 @@ class MediaPlaybackService : Service() {
}
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
if (_playbackState != PlaybackStateCompat.STATE_PAUSED &&
_playbackState != PlaybackStateCompat.STATE_STOPPED &&
_playbackState != PlaybackStateCompat.STATE_NONE &&
_playbackState != PlaybackStateCompat.STATE_ERROR) {
if (isPlaying) {
_audioFocusLossTime_ms = System.currentTimeMillis()
}
_hasFocus = false;
_isTransientLoss = true;
MediaControlReceiver.onPauseReceived.emit();
Log.i(TAG, "Audio focus transient loss (_audioFocusLossTime_ms = ${_audioFocusLossTime_ms})");
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
Log.i(TAG, "Audio focus transient loss, can duck");
_hasFocus = true;
_isTransientLoss = true;
}
AudioManager.AUDIOFOCUS_LOSS -> {
if (_playbackState != PlaybackStateCompat.STATE_PAUSED &&
_playbackState != PlaybackStateCompat.STATE_STOPPED &&
_playbackState != PlaybackStateCompat.STATE_NONE &&
_playbackState != PlaybackStateCompat.STATE_ERROR) {
if (isPlaying) {
_audioFocusLossTime_ms = System.currentTimeMillis()
}
_hasFocus = false;
MediaControlReceiver.onPauseReceived.emit();
abandonAudioFocus();
Log.i(TAG, "Audio focus lost");
}
}