From f4870719f7e2f6de6f80c805a81b1755ed96c480 Mon Sep 17 00:00:00 2001 From: Carlos Olivo Date: Sat, 19 Jun 2021 10:39:03 -0500 Subject: [PATCH] Remember brightness changes on ExoPlayer (#442) Co-authored-by: Maxr1998 --- .../java/org/jellyfin/mobile/AppPreferences.kt | 12 ++++++++++++ .../jellyfin/mobile/player/PlayerGestureHelper.kt | 14 ++++++++++++-- .../jellyfin/mobile/settings/SettingsFragment.kt | 13 ++++++++++++- .../java/org/jellyfin/mobile/utils/Constants.kt | 2 ++ app/src/main/res/values/strings.xml | 3 ++- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/jellyfin/mobile/AppPreferences.kt b/app/src/main/java/org/jellyfin/mobile/AppPreferences.kt index 73a8c1856..b82c00770 100644 --- a/app/src/main/java/org/jellyfin/mobile/AppPreferences.kt +++ b/app/src/main/java/org/jellyfin/mobile/AppPreferences.kt @@ -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 @@ -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) diff --git a/app/src/main/java/org/jellyfin/mobile/player/PlayerGestureHelper.kt b/app/src/main/java/org/jellyfin/mobile/player/PlayerGestureHelper.kt index 96277ec54..b15744e36 100644 --- a/app/src/main/java/org/jellyfin/mobile/player/PlayerGestureHelper.kt +++ b/app/src/main/java/org/jellyfin/mobile/player/PlayerGestureHelper.kt @@ -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 @@ -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. @@ -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) { @@ -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 diff --git a/app/src/main/java/org/jellyfin/mobile/settings/SettingsFragment.kt b/app/src/main/java/org/jellyfin/mobile/settings/SettingsFragment.kt index f1434f34a..a0fd722ca 100644 --- a/app/src/main/java/org/jellyfin/mobile/settings/SettingsFragment.kt +++ b/app/src/main/java/org/jellyfin/mobile/settings/SettingsFragment.kt @@ -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 @@ -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 { @@ -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 } @@ -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 diff --git a/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt b/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt index c6f9565d0..f062317ba 100644 --- a/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt +++ b/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt @@ -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" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 679a86fdb..fff38bc8d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -82,9 +82,10 @@ The default HTML video player from the Web UI Based on ExoPlayer, supports more video formats and codecs, and is more integrated into the OS External video playback apps like MX Player and VLC + Brightness and volume gestures + Remember display brightness Background audio Allow playing videos in the background with audio-only - Brightness and volume gestures External player app MPV Player