Prevent device from dimming or sleeping screen while video/audio playing (but really) (#4168)

For reasons not totally clear to me, Github marked #4160 as "merged" and
will not let me reopen it.

I believe this should be included for 24.1 because it fixes a 24.0
regression in the media player.

I have tested this newest commit in a number of ways, and in my testing
I find (1) when viewing an image, it sleeps after about a minute (2)
when viewing video, it stays awake indefinitely (3) this is true whether
the image/video was opened directly, or reached by swiping from another
attachment. I have not tested swiping to/from audio but I am confident
it will work the same.
This commit is contained in:
Konrad Pozniak 2023-12-13 19:23:11 +01:00 committed by GitHub
commit 331b13621e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -35,6 +35,7 @@ import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.core.app.ShareCompat
@ -124,6 +125,7 @@ class ViewMediaActivity : BaseActivity(), HasAndroidInjector, ViewImageFragment.
binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
binding.toolbar.title = getPageTitle(position)
adjustScreenWakefulness()
}
})
@ -155,6 +157,8 @@ class ViewMediaActivity : BaseActivity(), HasAndroidInjector, ViewImageFragment.
window.sharedElementEnterTransition.removeListener(this)
}
})
adjustScreenWakefulness()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -342,6 +346,15 @@ class ViewMediaActivity : BaseActivity(), HasAndroidInjector, ViewImageFragment.
shareFile(file, mimeType)
}
// Prevent this activity from dimming or sleeping the screen if, and only if, it is playing video or audio
private fun adjustScreenWakefulness() {
if (attachments!![binding.viewPager.currentItem].attachment.type == Attachment.Type.IMAGE) {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
override fun androidInjector() = androidInjector
companion object {