Skip to content

Commit

Permalink
Remember brightness changes on ExoPlayer (#442)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxr1998 <max.rumpf1998@gmail.com>
  • Loading branch information
CarlosOlivo and Maxr1998 authored Jun 19, 2021
1 parent 1c2f36f commit f487071
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/org/jellyfin/mobile/AppPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jellyfin.mobile
import android.content.Context
import android.content.SharedPreferences
import android.os.Environment
import android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
import androidx.core.content.edit
import org.jellyfin.mobile.settings.ExternalPlayerPackage
import org.jellyfin.mobile.settings.VideoPlayerType
Expand Down Expand Up @@ -94,6 +95,17 @@ class AppPreferences(context: Context) {
val exoPlayerAllowSwipeGestures: Boolean
get() = sharedPreferences.getBoolean(Constants.PREF_EXOPLAYER_ALLOW_SWIPE_GESTURES, true)

val exoPlayerRememberBrightness: Boolean
get() = sharedPreferences.getBoolean(Constants.PREF_EXOPLAYER_REMEMBER_BRIGHTNESS, false)

var exoPlayerBrightness: Float
get() = sharedPreferences.getFloat(Constants.PREF_EXOPLAYER_BRIGHTNESS, BRIGHTNESS_OVERRIDE_NONE)
set(value) {
sharedPreferences.edit {
putFloat(Constants.PREF_EXOPLAYER_BRIGHTNESS, value)
}
}

val exoPlayerAllowBackgroundAudio: Boolean
get() = sharedPreferences.getBoolean(Constants.PREF_EXOPLAYER_ALLOW_BACKGROUND_AUDIO, false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import android.provider.Settings
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.WindowManager
import android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
import android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
Expand Down Expand Up @@ -36,6 +37,12 @@ class PlayerGestureHelper(
private val gestureIndicatorOverlayImage: ImageView by playerBinding::gestureOverlayImage
private val gestureIndicatorOverlayProgress: ProgressBar by playerBinding::gestureOverlayProgress

init {
if (appPreferences.exoPlayerRememberBrightness) {
fragment.requireActivity().window.brightness = appPreferences.exoPlayerBrightness
}
}

/**
* Tracks whether video content should fill the screen, cutting off unwanted content on the sides.
* Useful on wide-screen phones to remove black bars from some movies.
Expand Down Expand Up @@ -152,7 +159,7 @@ class PlayerGestureHelper(
// Swiping on the left, change brightness

val window = fragment.requireActivity().window
val brightnessRange = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF..WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
val brightnessRange = BRIGHTNESS_OVERRIDE_OFF..BRIGHTNESS_OVERRIDE_FULL

// Initialize on first swipe
if (swipeGestureValueTracker == -1f) {
Expand All @@ -165,6 +172,9 @@ class PlayerGestureHelper(

swipeGestureValueTracker = (swipeGestureValueTracker + ratioChange).coerceIn(brightnessRange)
window.brightness = swipeGestureValueTracker
if (appPreferences.exoPlayerRememberBrightness) {
appPreferences.exoPlayerBrightness = swipeGestureValueTracker
}

gestureIndicatorOverlayImage.setImageResource(org.jellyfin.mobile.R.drawable.ic_brightness_white_24dp)
gestureIndicatorOverlayProgress.max = Constants.PERCENT_MAX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
import androidx.fragment.app.Fragment
import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.helpers.categoryHeader
import de.Maxr1998.modernpreferences.helpers.checkBox
import de.Maxr1998.modernpreferences.helpers.defaultOnCheckedChange
import de.Maxr1998.modernpreferences.helpers.defaultOnSelectionChange
import de.Maxr1998.modernpreferences.helpers.screen
import de.Maxr1998.modernpreferences.helpers.singleChoice
Expand All @@ -28,8 +30,9 @@ class SettingsFragment : Fragment() {

private val appPreferences: AppPreferences by inject()
private val settingsAdapter: PreferencesAdapter by lazy { PreferencesAdapter(buildSettingsScreen()) }
private lateinit var backgroundAudioPreference: Preference
private lateinit var swipeGesturesPreference: Preference
private lateinit var rememberBrightnessPreference: Preference
private lateinit var backgroundAudioPreference: Preference
private lateinit var externalPlayerChoicePreference: Preference

init {
Expand Down Expand Up @@ -78,6 +81,7 @@ class SettingsFragment : Fragment() {
initialSelection = VideoPlayerType.WEB_PLAYER
defaultOnSelectionChange { selection ->
swipeGesturesPreference.enabled = selection == VideoPlayerType.EXO_PLAYER
rememberBrightnessPreference.enabled = selection == VideoPlayerType.EXO_PLAYER
backgroundAudioPreference.enabled = selection == VideoPlayerType.EXO_PLAYER
externalPlayerChoicePreference.enabled = selection == VideoPlayerType.EXTERNAL_PLAYER
}
Expand All @@ -87,6 +91,13 @@ class SettingsFragment : Fragment() {
enabled = appPreferences.videoPlayerType == VideoPlayerType.EXO_PLAYER
defaultValue = true
}
rememberBrightnessPreference = checkBox(Constants.PREF_EXOPLAYER_REMEMBER_BRIGHTNESS) {
titleRes = R.string.pref_exoplayer_remember_brightness
enabled = appPreferences.videoPlayerType == VideoPlayerType.EXO_PLAYER
defaultOnCheckedChange { checked ->
if (!checked) appPreferences.exoPlayerBrightness = BRIGHTNESS_OVERRIDE_NONE
}
}
backgroundAudioPreference = checkBox(Constants.PREF_EXOPLAYER_ALLOW_BACKGROUND_AUDIO) {
titleRes = R.string.pref_exoplayer_allow_background_audio
summaryRes = R.string.pref_exoplayer_allow_background_audio_summary
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/jellyfin/mobile/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ object Constants {
const val PREF_MUSIC_NOTIFICATION_ALWAYS_DISMISSIBLE = "pref_music_notification_always_dismissible"
const val PREF_VIDEO_PLAYER_TYPE = "pref_video_player_type"
const val PREF_EXOPLAYER_ALLOW_SWIPE_GESTURES = "pref_exoplayer_allow_swipe_gestures"
const val PREF_EXOPLAYER_REMEMBER_BRIGHTNESS = "pref_exoplayer_remember_brightness"
const val PREF_EXOPLAYER_BRIGHTNESS = "pref_exoplayer_brightness"
const val PREF_EXOPLAYER_ALLOW_BACKGROUND_AUDIO = "pref_exoplayer_allow_background_audio"
const val PREF_EXTERNAL_PLAYER_APP = "pref_external_player_app"
const val PREF_DOWNLOAD_LOCATION = "pref_download_location"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@
<string name="video_player_web_description">The default HTML video player from the Web UI</string>
<string name="video_player_native_description">Based on ExoPlayer, supports more video formats and codecs, and is more integrated into the OS</string>
<string name="video_player_external_description">External video playback apps like MX Player and VLC</string>
<string name="pref_exoplayer_allow_brightness_volume_gesture">Brightness and volume gestures</string>
<string name="pref_exoplayer_remember_brightness">Remember display brightness</string>
<string name="pref_exoplayer_allow_background_audio">Background audio</string>
<string name="pref_exoplayer_allow_background_audio_summary">Allow playing videos in the background with audio-only</string>
<string name="pref_exoplayer_allow_brightness_volume_gesture">Brightness and volume gestures</string>

<string name="external_player_app">External player app</string>
<string name="external_player_mpv">MPV Player</string>
Expand Down

0 comments on commit f487071

Please sign in to comment.