Skip to content

Commit

Permalink
Only keep screen on while videos are playing
Browse files Browse the repository at this point in the history
  • Loading branch information
MarmadileManteater committed Apr 14, 2024
1 parent 76c9b61 commit f5f9a3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String> = HashMap()

companion object {
Expand Down Expand Up @@ -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}'] || []")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/ft-video-player/ft-video-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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())
}
})
Expand Down

0 comments on commit f5f9a3e

Please sign in to comment.