From f5f9a3edaa2912c2ea630cb64e07e9541cc91280 Mon Sep 17 00:00:00 2001 From: Emma Date: Sun, 14 Apr 2024 12:22:06 -0400 Subject: [PATCH] Only keep screen on while videos are playing MarmadileManteater/FreeTubeAndroid#335 --- .../freetube/FreeTubeJavaScriptInterface.kt | 16 ++++++++++++++++ .../java/io/freetubeapp/freetube/MainActivity.kt | 2 -- .../ft-video-player/ft-video-player.js | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt b/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt index 3b27c26010071..7cd8a04070759 100644 --- a/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt +++ b/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt @@ -14,6 +14,7 @@ import android.media.session.PlaybackState import android.media.session.PlaybackState.STATE_PAUSED import android.net.Uri import android.os.Build +import android.view.WindowManager import android.webkit.JavascriptInterface import androidx.activity.result.ActivityResult import androidx.annotation.RequiresApi @@ -32,6 +33,7 @@ class FreeTubeJavaScriptInterface { private var lastPosition: Long private var lastState: Int private var lastNotification: Notification? = null + private var keepScreenOn: Boolean = false var syncMessages: MutableMap = HashMap() companion object { @@ -578,6 +580,20 @@ class FreeTubeJavaScriptInterface { syncMessages.remove(promise) return value!! } + + fun enableKeepScreenOn() { + if (!keepScreenOn) { + keepScreenOn = true + context.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } + } + + fun disableKeepScreenOn() { + if (keepScreenOn) { + keepScreenOn = false + context.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } + } private fun addNamedCallbackToPromise(promise: String, name: String) { context.runOnUiThread { context.webView.loadUrl("javascript: window['${promise}'].callbacks = window['${promise}'].callbacks || {}; window['${promise}'].callbacks.notify = (key, message) => window['${promise}'].callbacks[key].forEach(callback => callback(message)); window['${promise}'].callbacks['${name}'] = window['${promise}'].callbacks['${name}'] || []") diff --git a/android/app/src/main/java/io/freetubeapp/freetube/MainActivity.kt b/android/app/src/main/java/io/freetubeapp/freetube/MainActivity.kt index dcd3b8651e088..06fc40f4b1b99 100644 --- a/android/app/src/main/java/io/freetubeapp/freetube/MainActivity.kt +++ b/android/app/src/main/java/io/freetubeapp/freetube/MainActivity.kt @@ -9,7 +9,6 @@ import android.os.Bundle import android.view.View import android.view.ViewGroup import android.view.ViewTreeObserver -import android.view.WindowManager import android.webkit.WebChromeClient import android.webkit.WebResourceRequest import android.webkit.WebView @@ -74,7 +73,6 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback { @Suppress("DEPRECATION") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) content = findViewById(android.R.id.content) content.viewTreeObserver.addOnPreDrawListener( object : ViewTreeObserver.OnPreDrawListener { diff --git a/src/renderer/components/ft-video-player/ft-video-player.js b/src/renderer/components/ft-video-player/ft-video-player.js index ab9bba5415efb..befe049187669 100644 --- a/src/renderer/components/ft-video-player/ft-video-player.js +++ b/src/renderer/components/ft-video-player/ft-video-player.js @@ -689,6 +689,7 @@ export default defineComponent({ this.player.on('play', async () => { if (process.env.IS_ANDROID) { + android.enableKeepScreenOn() updateMediaSessionState(STATE_PLAYING.toString()) } if ('mediaSession' in navigator) { @@ -704,6 +705,7 @@ export default defineComponent({ this.player.on('pause', () => { if (process.env.IS_ANDROID) { + android.disableKeepScreenOn() updateMediaSessionState(STATE_PAUSED) } if ('mediaSession' in navigator) { @@ -730,7 +732,6 @@ export default defineComponent({ } this.$emit('timeupdate') if (process.env.IS_ANDROID) { - // todo add code to update state of media session updateMediaSessionState(null, Math.floor(this.player.currentTime() * 1000).toString()) } })