Keep screen on fixes.

This commit is contained in:
Koen 2023-11-17 16:44:16 +01:00
commit 0bba7fa373
10 changed files with 54 additions and 11 deletions

View file

@ -426,6 +426,9 @@ class Settings : FragmentedStorageFileJson() {
@Serializable(with = FlexibleBooleanSerializer::class) @Serializable(with = FlexibleBooleanSerializer::class)
var enabled: Boolean = true; var enabled: Boolean = true;
@FormField(R.string.keep_screen_on, FieldForm.TOGGLE, R.string.keep_screen_on_while_casting, 1)
@Serializable(with = FlexibleBooleanSerializer::class)
var keepScreenOn: Boolean = true;
/*TODO: Should we have a different casting quality? /*TODO: Should we have a different casting quality?
@FormField("Preferred Casting Quality", FieldForm.DROPDOWN, "", 3) @FormField("Preferred Casting Quality", FieldForm.DROPDOWN, "", 3)

View file

@ -95,6 +95,8 @@ class AutoUpdateDialog(context: Context?) : AlertDialog(context) {
_buttonUpdate.visibility = Button.GONE; _buttonUpdate.visibility = Button.GONE;
setCancelable(false); setCancelable(false);
setCanceledOnTouchOutside(false); setCanceledOnTouchOutside(false);
Logger.i(TAG, "Keep screen on set update")
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_text.text = context.resources.getText(R.string.downloading_update); _text.text = context.resources.getText(R.string.downloading_update);
@ -178,6 +180,7 @@ class AutoUpdateDialog(context: Context?) : AlertDialog(context) {
} }
} finally { } finally {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
Logger.i(TAG, "Keep screen on unset install")
window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} }
} }

View file

@ -134,6 +134,8 @@ class ImportDialog : AlertDialog {
setCancelable(false); setCancelable(false);
setCanceledOnTouchOutside(false); setCanceledOnTouchOutside(false);
Logger.i(TAG, "Keep screen on set import")
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_updateSpinner.drawable?.assume<Animatable>()?.start(); _updateSpinner.drawable?.assume<Animatable>()?.start();
@ -201,6 +203,7 @@ class ImportDialog : AlertDialog {
} catch (e: Throwable) { } catch (e: Throwable) {
Logger.e(TAG, "Failed to update import UI.", e) Logger.e(TAG, "Failed to update import UI.", e)
} finally { } finally {
Logger.i(TAG, "Keep screen on unset update")
window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} }
} }

View file

@ -144,6 +144,7 @@ class MigrateDialog : AlertDialog {
setCancelable(false); setCancelable(false);
setCanceledOnTouchOutside(false); setCanceledOnTouchOutside(false);
Logger.i(TAG, "Keep screen on set restore")
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_updateSpinner.drawable?.assume<Animatable>()?.start(); _updateSpinner.drawable?.assume<Animatable>()?.start();
@ -214,6 +215,7 @@ class MigrateDialog : AlertDialog {
} catch (e: Throwable) { } catch (e: Throwable) {
Logger.e(TAG, "Failed to update import UI.", e) Logger.e(TAG, "Failed to update import UI.", e)
} finally { } finally {
Logger.i(TAG, "Keep screen on unset restore")
window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} }
} }

View file

@ -68,8 +68,6 @@ class VideoDetailFragment : MainFragment {
super.onShownWithView(parameter, isBack); super.onShownWithView(parameter, isBack);
Logger.i(TAG, "onShownWithView parameter=$parameter") Logger.i(TAG, "onShownWithView parameter=$parameter")
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if(parameter is IPlatformVideoDetails) if(parameter is IPlatformVideoDetails)
_viewDetail?.setVideoDetails(parameter, true); _viewDetail?.setVideoDetails(parameter, true);
else if (parameter is IPlatformVideo) else if (parameter is IPlatformVideo)
@ -176,7 +174,6 @@ class VideoDetailFragment : MainFragment {
_viewDetail?.onStop(); _viewDetail?.onStop();
close(); close();
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
StatePlayer.instance.clearQueue(); StatePlayer.instance.clearQueue();
StatePlayer.instance.setPlayerClosed(); StatePlayer.instance.setPlayerClosed();
} }

View file

@ -22,6 +22,7 @@ import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.WindowManager
import android.widget.* import android.widget.*
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@ -59,6 +60,7 @@ import com.futo.platformplayer.casting.StateCasting
import com.futo.platformplayer.constructs.Event0 import com.futo.platformplayer.constructs.Event0
import com.futo.platformplayer.constructs.Event1 import com.futo.platformplayer.constructs.Event1
import com.futo.platformplayer.constructs.TaskHandler import com.futo.platformplayer.constructs.TaskHandler
import com.futo.platformplayer.dialogs.AutoUpdateDialog
import com.futo.platformplayer.downloads.VideoLocal import com.futo.platformplayer.downloads.VideoLocal
import com.futo.platformplayer.engine.exceptions.ScriptAgeException import com.futo.platformplayer.engine.exceptions.ScriptAgeException
import com.futo.platformplayer.engine.exceptions.ScriptException import com.futo.platformplayer.engine.exceptions.ScriptException
@ -216,6 +218,9 @@ class VideoDetailView : ConstraintLayout {
private var _lastAudioSource: IAudioSource? = null; private var _lastAudioSource: IAudioSource? = null;
private var _lastSubtitleSource: ISubtitleSource? = null; private var _lastSubtitleSource: ISubtitleSource? = null;
private var _isCasting: Boolean = false; private var _isCasting: Boolean = false;
var isPlaying: Boolean = false
private set;
var lastPositionMilliseconds: Long = 0 var lastPositionMilliseconds: Long = 0
private set; private set;
private var _historicalPosition: Long = 0; private var _historicalPosition: Long = 0;
@ -600,6 +605,8 @@ class VideoDetailView : ConstraintLayout {
_lastSubtitleSource = null; _lastSubtitleSource = null;
video = null; video = null;
_playbackTracker = null; _playbackTracker = null;
Logger.i(TAG, "Keep screen on unset onClose")
fragment.activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}; };
_layoutResume.setOnClickListener { _layoutResume.setOnClickListener {
@ -1680,14 +1687,28 @@ class VideoDetailView : ConstraintLayout {
if(playing) { if(playing) {
_minimize_controls_pause.visibility = View.VISIBLE; _minimize_controls_pause.visibility = View.VISIBLE;
_minimize_controls_play.visibility = View.GONE; _minimize_controls_play.visibility = View.GONE;
if (_isCasting) {
if (Settings.instance.casting.keepScreenOn) {
Logger.i(TAG, "Keep screen on set handlePlayChanged casting")
fragment.activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} else {
Logger.i(TAG, "Keep screen on set handlePlayChanged player")
fragment.activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} }
else { else {
_minimize_controls_pause.visibility = View.GONE; _minimize_controls_pause.visibility = View.GONE;
_minimize_controls_play.visibility = View.VISIBLE; _minimize_controls_play.visibility = View.VISIBLE;
Logger.i(TAG, "Keep screen on unset handlePlayChanged")
fragment.activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} }
isPlaying = playing;
onPlayChanged.emit(playing); onPlayChanged.emit(playing);
updateTracker(_player.position, playing, true); updateTracker(lastPositionMilliseconds, playing, true);
} }
private fun handleSelectVideoTrack(videoSource: IVideoSource) { private fun handleSelectVideoTrack(videoSource: IVideoSource) {
@ -2031,7 +2052,8 @@ class VideoDetailView : ConstraintLayout {
StatePlaylists.instance.updateHistoryPosition(v, true, (positionMilliseconds.toFloat() / 1000.0f).toLong()); StatePlaylists.instance.updateHistoryPosition(v, true, (positionMilliseconds.toFloat() / 1000.0f).toLong());
_lastPositionSaveTime = currentTime; _lastPositionSaveTime = currentTime;
} }
updateTracker(positionMilliseconds, _player.playing, false);
updateTracker(positionMilliseconds, isPlaying, false);
} }
private fun updateTracker(positionMs: Long, isPlaying: Boolean, forceUpdate: Boolean = false) { private fun updateTracker(positionMs: Long, isPlaying: Boolean, forceUpdate: Boolean = false) {

View file

@ -295,7 +295,7 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
Logger.i(TAG, "Updated chapter to [${_currentChapter?.name}] with speed ${delta}ms (${pos - (_currentChapter?.timeStart?.times(1000)?.toLong() ?: 0)}ms late [${_currentChapter?.timeStart}s])"); Logger.i(TAG, "Updated chapter to [${_currentChapter?.name}] with speed ${delta}ms (${pos - (_currentChapter?.timeStart?.times(1000)?.toLong() ?: 0)}ms late [${_currentChapter?.timeStart}s])");
} }
} }
if(playingCached) if(playing)
updateChaptersLoop(loopId); updateChaptersLoop(loopId);
else else
_currentChapterLoopActive = false; _currentChapterLoopActive = false;

View file

@ -1,6 +1,7 @@
package com.futo.platformplayer.views.video package com.futo.platformplayer.views.video
import android.content.Context import android.content.Context
import android.media.session.PlaybackState
import android.net.Uri import android.net.Uri
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.RelativeLayout import android.widget.RelativeLayout
@ -60,8 +61,7 @@ abstract class FutoVideoPlayerBase : RelativeLayout {
private set; private set;
val exoPlayerStateName: String; val exoPlayerStateName: String;
protected var playingCached: Boolean = false; var playing: Boolean = false;
val playing: Boolean get() = exoPlayer?.player?.playWhenReady ?: false;
val position: Long get() = exoPlayer?.player?.currentPosition ?: 0; val position: Long get() = exoPlayer?.player?.currentPosition ?: 0;
val duration: Long get() = exoPlayer?.player?.duration ?: 0; val duration: Long get() = exoPlayer?.player?.duration ?: 0;
@ -99,12 +99,23 @@ abstract class FutoVideoPlayerBase : RelativeLayout {
} }
} }
} }
updatePlaying();
} }
override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) { override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
super.onPlayWhenReadyChanged(playWhenReady, reason) super.onPlayWhenReadyChanged(playWhenReady, reason)
onPlayChanged.emit(playWhenReady); updatePlaying();
playingCached = playWhenReady; }
fun updatePlaying() {
val newPlaying = exoPlayer?.let { it.player.playWhenReady && it.player.playbackState != Player.STATE_ENDED && it.player.playbackState != Player.STATE_IDLE } ?: false
if (newPlaying == playing) {
return;
}
playing = newPlaying;
onPlayChanged.emit(playing);
} }
override fun onVideoSizeChanged(videoSize: VideoSize) { override fun onVideoSizeChanged(videoSize: VideoSize) {

View file

@ -60,6 +60,8 @@
<string name="view_all">View all</string> <string name="view_all">View all</string>
<string name="creators">Creators</string> <string name="creators">Creators</string>
<string name="enabled">Enabled</string> <string name="enabled">Enabled</string>
<string name="keep_screen_on">Keep screen on</string>
<string name="keep_screen_on_while_casting">Keep screen on while casting</string>
<string name="discover">Discover</string> <string name="discover">Discover</string>
<string name="find_new_video_sources_to_add">Find new video sources to add</string> <string name="find_new_video_sources_to_add">Find new video sources to add</string>
<string name="these_sources_have_been_disabled">These sources have been disabled</string> <string name="these_sources_have_been_disabled">These sources have been disabled</string>

@ -1 +1 @@
Subproject commit 4f89b4072f4473ff0ffac1711023dffd20f0a868 Subproject commit 8f10daba1ef9cbcd99f3c640d86808f8c94aa84a