Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(YouTube - Swipe controls): Save and restore brightness and add auto-brightness toggle #610

Merged
merged 27 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a246972
feat(YouTube - Swipe controls): Remember current brightness at all times
MarcaDian Apr 5, 2024
5026c50
refactor
oSumAtrIX Apr 5, 2024
c3a9e6b
refactor
MarcaDian Apr 6, 2024
46ef6c7
comments and rename vars
MarcaDian Apr 7, 2024
00e396a
do not use var as global setting
MarcaDian Apr 8, 2024
f5ef01d
revert
MarcaDian Apr 8, 2024
cd7fe53
Merge branch 'dev' into feat/save_brightness
MarcaDian Apr 10, 2024
d317b3f
refactoring and optimization
MarcaDian Apr 10, 2024
ffd2acc
Merge remote-tracking branch 'origin/feat/save_brightness' into feat/…
MarcaDian Apr 10, 2024
cf60915
fix
MarcaDian Apr 11, 2024
7dd6a28
possibly fixed
MarcaDian Apr 11, 2024
2a1916b
I'm already sick of this shitty code 🙃
MarcaDian Apr 11, 2024
765ec07
some fix
MarcaDian Apr 12, 2024
48e734f
some fix
MarcaDian Apr 12, 2024
03802cc
Merge remote-tracking branch 'origin/feat/save_brightness' into feat/…
MarcaDian Apr 12, 2024
7aa3d1e
del unused import
MarcaDian Apr 12, 2024
48ae621
i think this is final
MarcaDian Apr 12, 2024
25d00af
lost a comment
MarcaDian Apr 12, 2024
80e0bbb
Merge remote-tracking branch 'upstream/dev' into feat/save_brightness
MarcaDian Apr 13, 2024
3a81731
Merge remote-tracking branch 'upstream/dev' into feat/save_brightness
MarcaDian Apr 16, 2024
44cd133
refactor
MarcaDian Apr 16, 2024
6ccdc70
Merge remote-tracking branch 'upstream/dev' into feat/save_brightness
MarcaDian Apr 17, 2024
84a39b6
Merge remote-tracking branch 'upstream/dev' into feat/save_brightness
MarcaDian Apr 18, 2024
7794c5c
Merge remote-tracking branch 'upstream/dev' into feat/save_brightness
MarcaDian Apr 18, 2024
1fbbfd9
Merge remote-tracking branch 'upstream/dev' into feat/save_brightness
MarcaDian Apr 20, 2024
7c0b506
refactor
MarcaDian Apr 20, 2024
cef6012
refactor
oSumAtrIX Apr 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, this can be simplified by adding get set operators and delegating getter setter properties, not a matter of this PR though.

//endregion
}
Original file line number Diff line number Diff line change
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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading