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

Commit 4c72ac1

Browse files
authored
fix(swipe-controls): crash on SDK below 24 (#157)
1 parent cdca962 commit 4c72ac1

1 file changed

Lines changed: 55 additions & 25 deletions

File tree

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

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

33
import android.app.Activity
4+
import android.os.Build
45
import android.os.Bundle
56
import android.view.KeyEvent
67
import android.view.MotionEvent
@@ -68,7 +69,57 @@ class SwipeControlsHostActivity : Activity() {
6869

6970
override fun onCreate(savedInstanceState: Bundle?) {
7071
super.onCreate(savedInstanceState)
72+
initialize()
73+
}
74+
75+
override fun onStart() {
76+
super.onStart()
77+
reAttachOverlays()
78+
}
79+
80+
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
81+
ensureInitialized()
82+
return if ((ev != null) && gesture.submitTouchEvent(ev)) true else {
83+
super.dispatchTouchEvent(ev)
84+
}
85+
}
86+
87+
override fun dispatchKeyEvent(ev: KeyEvent?): Boolean {
88+
ensureInitialized()
89+
return if ((ev != null) && keys.onKeyEvent(ev)) true else {
90+
super.dispatchKeyEvent(ev)
91+
}
92+
}
93+
94+
/**
95+
* dispatch a touch event to downstream views
96+
*
97+
* @param event the event to dispatch
98+
* @return was the event consumed?
99+
*/
100+
fun dispatchDownstreamTouchEvent(event: MotionEvent) =
101+
super.dispatchTouchEvent(event)
102+
103+
/**
104+
* ensures that swipe controllers are initialized and attached.
105+
* on some ROMs with SDK <= 23, [onCreate] and [onStart] may not be called correctly.
106+
* see https://github.com/revanced/revanced-patches/issues/446
107+
*/
108+
private fun ensureInitialized() {
109+
if (!this::config.isInitialized) {
110+
LogHelper.printException(
111+
this.javaClass,
112+
"swipe controls were not initialized in onCreate, initializing on-the-fly (SDK is ${Build.VERSION.SDK_INT})"
113+
)
114+
initialize()
115+
reAttachOverlays()
116+
}
117+
}
71118

119+
/**
120+
* initializes controllers, only call once
121+
*/
122+
private fun initialize() {
72123
// create controllers
73124
LogHelper.info(this.javaClass, "initializing swipe controls controllers")
74125
config = SwipeControlsConfigurationProvider(this)
@@ -102,36 +153,15 @@ class SwipeControlsHostActivity : Activity() {
102153
currentHost = WeakReference(this)
103154
}
104155

105-
override fun onStart() {
106-
super.onStart()
107-
108-
// (re) attach overlay
156+
/**
157+
* (re) attaches swipe overlays
158+
*/
159+
private fun reAttachOverlays() {
109160
LogHelper.info(this.javaClass, "attaching swipe controls overlay")
110161
contentRoot.removeView(overlay)
111162
contentRoot.addView(overlay)
112163
}
113164

114-
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
115-
return if ((ev != null) && gesture.submitTouchEvent(ev)) true else {
116-
super.dispatchTouchEvent(ev)
117-
}
118-
}
119-
120-
override fun dispatchKeyEvent(ev: KeyEvent?): Boolean {
121-
return if ((ev != null) && keys.onKeyEvent(ev)) true else {
122-
super.dispatchKeyEvent(ev)
123-
}
124-
}
125-
126-
/**
127-
* dispatch a touch event to downstream views
128-
*
129-
* @param event the event to dispatch
130-
* @return was the event consumed?
131-
*/
132-
fun dispatchDownstreamTouchEvent(event: MotionEvent) =
133-
super.dispatchTouchEvent(event)
134-
135165
/**
136166
* called when the player type changes
137167
*

0 commit comments

Comments
 (0)