From 1c8e2b29410048a352cb6aad3dd02773459f91a0 Mon Sep 17 00:00:00 2001 From: MarcaD <152095496+MarcaDian@users.noreply.github.com> Date: Sun, 21 Apr 2024 02:59:23 +0300 Subject: [PATCH] feat(YouTube - Swipe controls): Save and restore brightness and add auto-brightness toggle (#610) Co-authored-by: oSumAtrIX --- .../youtube/settings/Settings.java | 6 +-- .../SwipeControlsConfigurationProvider.kt | 12 ++++++ .../SwipeControlsHostActivity.kt | 27 ++++++++---- .../controller/ScreenBrightnessController.kt | 42 ++++++++++--------- .../core/VolumeAndBrightnessScroller.kt | 9 +++- .../views/SwipeControlsOverlayLayout.kt | 10 +++-- 6 files changed, 70 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index 505ad4518f..b74098c72e 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -249,9 +249,9 @@ public class Settings extends BaseSettings { parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final LongSetting SWIPE_OVERLAY_TIMEOUT = new LongSetting("revanced_swipe_overlay_timeout", 500L, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); - public static final BooleanSetting SWIPE_SAVE_AND_RESTORE_BRIGHTNESS = new BooleanSetting("revanced_swipe_save_and_restore_brightness", TRUE, true, - parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); - + public static final BooleanSetting SWIPE_SAVE_AND_RESTORE_BRIGHTNESS = new BooleanSetting("revanced_swipe_save_and_restore_brightness", TRUE, true, parent(SWIPE_BRIGHTNESS)); + public static final FloatSetting SWIPE_BRIGHTNESS_VALUE = new FloatSetting("revanced_swipe_brightness_value", -1f); + public static final BooleanSetting SWIPE_LOWEST_VALUE_ENABLE_AUTO_BRIGHTNESS = new BooleanSetting("revanced_swipe_lowest_value_enable_auto_brightness", FALSE, true, parent(SWIPE_BRIGHTNESS)); // Debugging /** * When enabled, share the debug logs with care. diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 0e2d799797..893ccd5795 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -104,5 +104,17 @@ class SwipeControlsConfigurationProvider( val shouldSaveAndRestoreBrightness: Boolean get() = Settings.SWIPE_SAVE_AND_RESTORE_BRIGHTNESS.get() + /** + * should auto-brightness be enabled at the lowest value of the brightness gesture + */ + val shouldLowestValueEnableAutoBrightness: Boolean + get() = Settings.SWIPE_LOWEST_VALUE_ENABLE_AUTO_BRIGHTNESS.get() + + /** + * variable that stores the brightness gesture value in the settings + */ + var savedScreenBrightnessValue: Float + get() = Settings.SWIPE_BRIGHTNESS_VALUE.get() + set(value) = Settings.SWIPE_BRIGHTNESS_VALUE.save(value) //endregion } diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt index e6f233e830..552363144d 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt @@ -166,20 +166,31 @@ class SwipeControlsHostActivity : Activity() { contentRoot.addView(overlay) } + // Flag that indicates whether the brightness has been saved and restored default brightness + private var isBrightnessSaved = false + /** * called when the player type changes * * @param type the new player type */ private fun onPlayerTypeChanged(type: PlayerType) { - if (config.shouldSaveAndRestoreBrightness) { - when (type) { - PlayerType.WATCH_WHILE_FULLSCREEN -> screen?.restore() - else -> { - screen?.save() - screen?.restoreDefaultBrightness() - } + when { + // If saving and restoring brightness is enabled, and the player type is WATCH_WHILE_FULLSCREEN, + // and brightness has already been saved, then restore the screen brightness + config.shouldSaveAndRestoreBrightness && type == PlayerType.WATCH_WHILE_FULLSCREEN && isBrightnessSaved -> { + screen?.restore() + isBrightnessSaved = false + } + // If saving and restoring brightness is enabled, and brightness has not been saved, + // then save the current screen state, restore default brightness, and mark brightness as saved + config.shouldSaveAndRestoreBrightness && !isBrightnessSaved -> { + screen?.save() + screen?.restoreDefaultBrightness() + isBrightnessSaved = true } + // If saving and restoring brightness is disabled, simply keep the default brightness + else -> screen?.restoreDefaultBrightness() } } @@ -222,4 +233,4 @@ class SwipeControlsHostActivity : Activity() { var currentHost: WeakReference = WeakReference(null) private set } -} +} \ No newline at end of file diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt index 1cf282e892..08e2700485 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt @@ -1,21 +1,17 @@ package app.revanced.integrations.youtube.swipecontrols.controller -import android.app.Activity import android.view.WindowManager +import app.revanced.integrations.youtube.swipecontrols.SwipeControlsHostActivity import app.revanced.integrations.youtube.swipecontrols.misc.clamp /** * controller to adjust the screen brightness level * - * @param host the host activity of which the brightness is adjusted + * @param host the host activity of which the brightness is adjusted, the main controller instance */ class ScreenBrightnessController( - private val host: Activity, + val host: SwipeControlsHostActivity, ) { - /** - * screen brightness saved by [save] - */ - private var savedScreenBrightness: Float? = null /** * the current screen brightness in percent, ranging from 0.0 to 100.0 @@ -26,6 +22,12 @@ class ScreenBrightnessController( rawScreenBrightness = (value.toFloat() / 100f).clamp(0f, 1f) } + /** + * is the screen brightness set to device- default? + */ + val isDefaultBrightness + get() = (rawScreenBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) + /** * restore the screen brightness to the default device brightness */ @@ -33,29 +35,29 @@ class ScreenBrightnessController( rawScreenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE } - /** - * is the screen brightness set to device- default? - */ - val isDefaultBrightness - get() = (rawScreenBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) + // Flag that indicates whether the brightness has been restored + private var isBrightnessRestored = false /** - * save the current screen brightness, to be brought back using [restore] + * save the current screen brightness into settings, to be brought back using [restore] */ fun save() { - if (savedScreenBrightness == null) { - savedScreenBrightness = rawScreenBrightness + if (isBrightnessRestored) { + // Saves the current screen brightness value into settings + host.config.savedScreenBrightnessValue = rawScreenBrightness + // Reset the flag + isBrightnessRestored = false } } /** - * restore the screen brightness saved using [save] + * restore the screen brightness from settings saved using [save] */ fun restore() { - savedScreenBrightness?.let { - rawScreenBrightness = it - } - savedScreenBrightness = null + // Restores the screen brightness value from the saved settings + rawScreenBrightness = host.config.savedScreenBrightnessValue + // Mark that brightness has been restored + isBrightnessRestored = true } /** diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/gesture/core/VolumeAndBrightnessScroller.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/gesture/core/VolumeAndBrightnessScroller.kt index dec14b81a1..f15478ec38 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/gesture/core/VolumeAndBrightnessScroller.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/gesture/core/VolumeAndBrightnessScroller.kt @@ -77,12 +77,17 @@ class VolumeAndBrightnessScrollerImpl( ), ) { _, _, direction -> screenController?.run { - if (screenBrightness > 0 || direction > 0) { + val shouldAdjustBrightness = if (host.config.shouldLowestValueEnableAutoBrightness) { + screenBrightness > 0 || direction > 0 + } else { + screenBrightness >= 0 || direction >= 0 + } + + if (shouldAdjustBrightness) { screenBrightness += direction } else { restoreDefaultBrightness() } - overlayController.onBrightnessChanged(screenBrightness) } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index dbe2434f3c..f68fcda1e2 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -11,6 +11,7 @@ import android.view.View import android.view.ViewGroup import android.widget.RelativeLayout import android.widget.TextView +import app.revanced.integrations.shared.StringRef.str import app.revanced.integrations.shared.Utils import app.revanced.integrations.youtube.swipecontrols.SwipeControlsConfigurationProvider import app.revanced.integrations.youtube.swipecontrols.misc.SwipeControlsOverlay @@ -122,10 +123,13 @@ class SwipeControlsOverlayLayout( } override fun onBrightnessChanged(brightness: Double) { - if (brightness > 0) { + if (config.shouldLowestValueEnableAutoBrightness && brightness <= 0) { + showFeedbackView( + str("revanced_swipe_lowest_value_enable_auto_brightness_overlay_text"), + autoBrightnessIcon, + ) + } else if (brightness >= 0) { showFeedbackView("${round(brightness).toInt()}%", manualBrightnessIcon) - } else { - showFeedbackView("AUTO", autoBrightnessIcon) } }