Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 8056e2e

Browse files
authored
feat: swipe controls override volume button behaviour (#114)
1 parent 166bfd3 commit 8056e2e

9 files changed

Lines changed: 263 additions & 275 deletions

File tree

app/src/main/java/app/revanced/integrations/patches/HDRAutoBrightnessPatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.view.WindowManager;
44

55
import app.revanced.integrations.settings.SettingsEnum;
6-
import app.revanced.integrations.swipecontrols.views.SwipeControlsHostLayout;
6+
import app.revanced.integrations.swipecontrols.SwipeControlsHostActivity;
77

88
/**
99
* Patch class for 'hdr-auto-brightness' patch
@@ -27,7 +27,7 @@ public static float getHDRBrightness(float original) {
2727

2828
// override with brightness set by swipe-controls
2929
// only when swipe-controls is active and has overridden the brightness
30-
final SwipeControlsHostLayout swipeControlsHost = SwipeControlsPatch.CURRENT_HOST.get();
30+
final SwipeControlsHostActivity swipeControlsHost = SwipeControlsHostActivity.getCurrentHost().get();
3131
if (swipeControlsHost != null
3232
&& swipeControlsHost.getScreen() != null
3333
&& swipeControlsHost.getConfig().getEnableBrightnessControl()

app/src/main/java/app/revanced/integrations/patches/SwipeControlsPatch.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

app/src/main/java/app/revanced/integrations/swipecontrols/SwipeControlsConfigurationProvider.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SwipeControlsConfigurationProvider(
1515
) {
1616
//region swipe enable
1717
/**
18-
* should swipe controls be enabled? (global setting
18+
* should swipe controls be enabled? (global setting)
1919
*/
2020
val enableSwipeControls: Boolean
2121
get() = isFullscreenVideo && (enableVolumeControls || enableBrightnessControl)
@@ -39,6 +39,14 @@ class SwipeControlsConfigurationProvider(
3939
get() = PlayerType.current == PlayerType.WATCH_WHILE_FULLSCREEN
4040
//endregion
4141

42+
//region keys enable
43+
/**
44+
* should volume key controls be overwritten? (global setting)
45+
*/
46+
val overwriteVolumeKeyControls: Boolean
47+
get() = isFullscreenVideo && enableVolumeControls
48+
//endregioin
49+
4250
//region gesture adjustments
4351
/**
4452
* should press-to-swipe be enabled?
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package app.revanced.integrations.swipecontrols
2+
3+
import android.app.Activity
4+
import android.os.Bundle
5+
import android.view.KeyEvent
6+
import android.view.MotionEvent
7+
import android.view.ViewGroup
8+
import app.revanced.integrations.shared.PlayerType
9+
import app.revanced.integrations.swipecontrols.controller.AudioVolumeController
10+
import app.revanced.integrations.swipecontrols.controller.ScreenBrightnessController
11+
import app.revanced.integrations.swipecontrols.controller.SwipeZonesController
12+
import app.revanced.integrations.swipecontrols.controller.VolumeKeysController
13+
import app.revanced.integrations.swipecontrols.controller.gesture.NoPtSSwipeGestureController
14+
import app.revanced.integrations.swipecontrols.controller.gesture.SwipeGestureController
15+
import app.revanced.integrations.swipecontrols.misc.Rectangle
16+
import app.revanced.integrations.swipecontrols.views.SwipeControlsOverlayLayout
17+
import app.revanced.integrations.utils.LogHelper
18+
import java.lang.ref.WeakReference
19+
20+
/**
21+
* The main controller for volume and brightness swipe controls.
22+
* note that the superclass is overwritten to the superclass of the WatchWhileActivity at patch time
23+
*
24+
* @smali Lapp/revanced/integrations/swipecontrols/SwipeControlsHostActivity;
25+
*/
26+
class SwipeControlsHostActivity : Activity() {
27+
/**
28+
* current instance of [AudioVolumeController]
29+
*/
30+
var audio: AudioVolumeController? = null
31+
32+
/**
33+
* current instance of [ScreenBrightnessController]
34+
*/
35+
var screen: ScreenBrightnessController? = null
36+
37+
/**
38+
* current instance of [SwipeControlsConfigurationProvider]
39+
*/
40+
lateinit var config: SwipeControlsConfigurationProvider
41+
42+
/**
43+
* current instance of [SwipeControlsOverlayLayout]
44+
*/
45+
lateinit var overlay: SwipeControlsOverlayLayout
46+
47+
/**
48+
* current instance of [SwipeZonesController]
49+
*/
50+
lateinit var zones: SwipeZonesController
51+
52+
/**
53+
* main gesture controller
54+
*/
55+
private lateinit var gesture: SwipeGestureController
56+
57+
/**
58+
* main volume keys controller
59+
*/
60+
private lateinit var keys: VolumeKeysController
61+
62+
/**
63+
* current content view with id [android.R.id.content]
64+
*/
65+
private val contentRoot
66+
get() = window.decorView.findViewById<ViewGroup>(android.R.id.content)
67+
68+
override fun onCreate(savedInstanceState: Bundle?) {
69+
super.onCreate(savedInstanceState)
70+
71+
// create controllers
72+
LogHelper.info(this.javaClass, "initializing swipe controls controllers")
73+
config = SwipeControlsConfigurationProvider(this)
74+
gesture = createGestureController()
75+
keys = VolumeKeysController(this)
76+
audio = createAudioController()
77+
screen = createScreenController()
78+
79+
// create overlay
80+
SwipeControlsOverlayLayout(this).let {
81+
overlay = it
82+
contentRoot.addView(it)
83+
}
84+
85+
// create swipe zone controller
86+
zones = SwipeZonesController(this) {
87+
Rectangle(
88+
contentRoot.x.toInt(),
89+
contentRoot.y.toInt(),
90+
contentRoot.width,
91+
contentRoot.height
92+
)
93+
}
94+
95+
// listen for changes in the player type
96+
PlayerType.onChange += this::onPlayerTypeChanged
97+
98+
// set current instance reference
99+
currentHost = WeakReference(this)
100+
}
101+
102+
override fun onStart() {
103+
super.onStart()
104+
105+
// (re) attach overlay
106+
LogHelper.info(this.javaClass, "attaching swipe controls overlay")
107+
contentRoot.removeView(overlay)
108+
contentRoot.addView(overlay)
109+
}
110+
111+
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
112+
return if ((ev != null) && gesture.onTouchEvent(ev)) true else {
113+
super.dispatchTouchEvent(ev)
114+
}
115+
}
116+
117+
override fun dispatchKeyEvent(ev: KeyEvent?): Boolean {
118+
return if((ev != null) && keys.onKeyEvent(ev)) true else {
119+
super.dispatchKeyEvent(ev)
120+
}
121+
}
122+
123+
/**
124+
* dispatch a touch event to downstream views
125+
*
126+
* @param event the event to dispatch
127+
* @return was the event consumed?
128+
*/
129+
fun dispatchDownstreamTouchEvent(event: MotionEvent) =
130+
super.dispatchTouchEvent(event)
131+
132+
/**
133+
* called when the player type changes
134+
*
135+
* @param type the new player type
136+
*/
137+
private fun onPlayerTypeChanged(type: PlayerType) {
138+
when (type) {
139+
PlayerType.WATCH_WHILE_FULLSCREEN -> screen?.restore()
140+
else -> {
141+
screen?.save()
142+
screen?.restoreDefaultBrightness()
143+
}
144+
}
145+
}
146+
147+
/**
148+
* create the audio volume controller
149+
*/
150+
private fun createAudioController() =
151+
if (config.enableVolumeControls)
152+
AudioVolumeController(this) else null
153+
154+
/**
155+
* create the screen brightness controller instance
156+
*/
157+
private fun createScreenController() =
158+
if (config.enableBrightnessControl)
159+
ScreenBrightnessController(this) else null
160+
161+
/**
162+
* create the gesture controller based on settings
163+
*/
164+
private fun createGestureController() =
165+
if (config.shouldEnablePressToSwipe)
166+
SwipeGestureController(this)
167+
else NoPtSSwipeGestureController(this)
168+
169+
companion object {
170+
/**
171+
* the currently active swipe controls host.
172+
* the reference may be null!
173+
*/
174+
@JvmStatic
175+
var currentHost: WeakReference<SwipeControlsHostActivity> = WeakReference(null)
176+
private set
177+
}
178+
}

app/src/main/java/app/revanced/integrations/swipecontrols/controller/SwipeZonesController.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package app.revanced.integrations.swipecontrols.controller
22

3-
import android.content.Context
3+
import android.app.Activity
44
import android.util.TypedValue
55
import android.view.ViewGroup
66
import app.revanced.integrations.swipecontrols.misc.Rectangle
@@ -35,29 +35,28 @@ import kotlin.math.min
3535
*/
3636
@Suppress("PrivatePropertyName")
3737
class SwipeZonesController(
38-
context: Context,
39-
private val parentView: ViewGroup,
38+
private val host: Activity,
4039
private val fallbackScreenRect: () -> Rectangle
4140
) {
4241
/**
4342
* 20dp, in pixels
4443
*/
45-
private val _20dp = 20.applyDimension(context, TypedValue.COMPLEX_UNIT_DIP)
44+
private val _20dp = 20.applyDimension(host, TypedValue.COMPLEX_UNIT_DIP)
4645

4746
/**
4847
* 40dp, in pixels
4948
*/
50-
private val _40dp = 40.applyDimension(context, TypedValue.COMPLEX_UNIT_DIP)
49+
private val _40dp = 40.applyDimension(host, TypedValue.COMPLEX_UNIT_DIP)
5150

5251
/**
5352
* 80dp, in pixels
5453
*/
55-
private val _80dp = 80.applyDimension(context, TypedValue.COMPLEX_UNIT_DIP)
54+
private val _80dp = 80.applyDimension(host, TypedValue.COMPLEX_UNIT_DIP)
5655

5756
/**
5857
* id for R.id.player_view
5958
*/
60-
private val playerViewId = ReVancedUtils.getResourceIdByName(context, "id", "player_view")
59+
private val playerViewId = ReVancedUtils.getResourceIdByName(host, "id", "player_view")
6160

6261
/**
6362
* current bounding rectangle of the player
@@ -114,7 +113,7 @@ class SwipeZonesController(
114113
*/
115114
private fun maybeAttachPlayerBoundsListener() {
116115
if (playerRect != null) return
117-
parentView.findViewById<ViewGroup>(playerViewId)?.let {
116+
host.findViewById<ViewGroup>(playerViewId)?.let {
118117
onPlayerViewLayout(it)
119118
it.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
120119
onPlayerViewLayout(it)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package app.revanced.integrations.swipecontrols.controller
2+
3+
import android.view.KeyEvent
4+
import app.revanced.integrations.swipecontrols.SwipeControlsHostActivity
5+
6+
/**
7+
* controller for custom volume button behaviour
8+
*
9+
* @param controller main controller instance
10+
*/
11+
class VolumeKeysController(
12+
private val controller: SwipeControlsHostActivity
13+
) {
14+
/**
15+
* key event handler
16+
*
17+
* @param event the key event
18+
* @return consume the event?
19+
*/
20+
fun onKeyEvent(event: KeyEvent): Boolean {
21+
if(!controller.config.overwriteVolumeKeyControls) {
22+
return false
23+
}
24+
25+
return when (event.keyCode) {
26+
KeyEvent.KEYCODE_VOLUME_DOWN ->
27+
handleVolumeKeyEvent(event, false)
28+
KeyEvent.KEYCODE_VOLUME_UP ->
29+
handleVolumeKeyEvent(event, true)
30+
else -> false
31+
}
32+
}
33+
34+
/**
35+
* handle a volume up / down key event
36+
*
37+
* @param event the key event
38+
* @param volumeUp was the key pressed the volume up key?
39+
* @return consume the event?
40+
*/
41+
private fun handleVolumeKeyEvent(event: KeyEvent, volumeUp: Boolean): Boolean {
42+
if (event.action == KeyEvent.ACTION_DOWN) {
43+
controller.audio?.apply {
44+
volume += if (volumeUp) 1 else -1
45+
controller.overlay.onVolumeChanged(volume, maxVolume)
46+
}
47+
}
48+
49+
return true
50+
}
51+
}

app/src/main/java/app/revanced/integrations/swipecontrols/controller/gesture/NoPtSSwipeGestureController.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package app.revanced.integrations.swipecontrols.controller.gesture
22

3-
import android.content.Context
43
import android.view.MotionEvent
5-
import app.revanced.integrations.swipecontrols.views.SwipeControlsHostLayout
4+
import app.revanced.integrations.swipecontrols.SwipeControlsHostActivity
65

76
/**
87
* [SwipeGestureController], but with press-to-swipe disabled because a lot of people dislike the feature.
98
* If you want to change something, try to do it in [SwipeGestureController] so that both configurations can benefit from it
109
*/
11-
class NoPtSSwipeGestureController(context: Context, controller: SwipeControlsHostLayout) :
12-
SwipeGestureController(context, controller) {
10+
class NoPtSSwipeGestureController(controller: SwipeControlsHostActivity) :
11+
SwipeGestureController(controller) {
1312

1413
/**
1514
* to disable press-to-swipe, we have to become press-to-swipe

0 commit comments

Comments
 (0)