Skip to content

Commit

Permalink
feat(YouTube - Swipe controls): Save and restore brightness and add a…
Browse files Browse the repository at this point in the history
…uto-brightness toggle (#610)

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
MarcaDian and oSumAtrIX committed Apr 20, 2024
1 parent efd0301 commit 1c8e2b2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 36 deletions.
Expand Up @@ -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.
Expand Down
Expand Up @@ -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
}
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -222,4 +233,4 @@ class SwipeControlsHostActivity : Activity() {
var currentHost: WeakReference<SwipeControlsHostActivity> = WeakReference(null)
private set
}
}
}
@@ -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
Expand All @@ -26,36 +22,42 @@ 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
*/
fun restoreDefaultBrightness() {
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
}

/**
Expand Down
Expand Up @@ -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)
}
}
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 1c8e2b2

Please sign in to comment.