Skip to content

Commit

Permalink
Acquire wakelock when videos are playing
Browse files Browse the repository at this point in the history
  • Loading branch information
MarmadileManteater committed Mar 3, 2024
1 parent 061945e commit d5cb978
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ class FreeTubeJavaScriptInterface {
MainActivity.showSplashScreen = false
}

@JavascriptInterface
fun acquireWakelock() {
context.wakeLock.acquire()
}

@JavascriptInterface
fun releaseWakelock() {
context.wakeLock.release()
}

/**
* @return the id of a promise on the window
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.freetubeapp.freetube

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PowerManager
import android.os.PowerManager.WakeLock
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
Expand All @@ -22,6 +25,7 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import io.freetubeapp.freetube.databinding.ActivityMainBinding
import java.net.URLEncoder
import java.util.UUID.randomUUID


class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
Expand All @@ -35,13 +39,16 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
lateinit var webView: BackgroundPlayWebView
lateinit var jsInterface: FreeTubeJavaScriptInterface
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
lateinit var wakeLock: WakeLock
companion object {
val POWER_MANAGER_TAG: String = "${randomUUID()}"
var showSplashScreen: Boolean = true
}
@Suppress("DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, POWER_MANAGER_TAG)
val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/components/ft-video-player/ft-video-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { getProxyUrl } from '../../helpers/api/invidious'
import store from '../../store'
import { STATE_PAUSED, STATE_PLAYING, updateMediaSessionState } from '../../helpers/android'
import android from 'android'

const EXPECTED_PLAY_RELATED_ERROR_MESSAGES = [
// This is thrown when `play()` called but user already viewing another page
Expand Down Expand Up @@ -688,6 +689,7 @@ export default defineComponent({
this.player.on('play', async () => {
if (process.env.IS_ANDROID) {
updateMediaSessionState(STATE_PLAYING.toString())
android.acquireWakelock()
}
if ('mediaSession' in navigator) {
navigator.mediaSession.playbackState = 'playing'
Expand All @@ -703,6 +705,7 @@ export default defineComponent({
this.player.on('pause', () => {
if (process.env.IS_ANDROID) {
updateMediaSessionState(STATE_PAUSED)
android.releaseWakelock()
}
if ('mediaSession' in navigator) {
navigator.mediaSession.playbackState = 'paused'
Expand Down

0 comments on commit d5cb978

Please sign in to comment.