fix(swipe-controls): crash on SDK below 24 (#157)

This commit is contained in:
Chris 2022-09-25 17:52:36 +02:00 committed by GitHub
parent cdca96224a
commit 4c72ac1cd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package app.revanced.integrations.swipecontrols
import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.MotionEvent
@ -68,7 +69,57 @@ class SwipeControlsHostActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initialize()
}
override fun onStart() {
super.onStart()
reAttachOverlays()
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
ensureInitialized()
return if ((ev != null) && gesture.submitTouchEvent(ev)) true else {
super.dispatchTouchEvent(ev)
}
}
override fun dispatchKeyEvent(ev: KeyEvent?): Boolean {
ensureInitialized()
return if ((ev != null) && keys.onKeyEvent(ev)) true else {
super.dispatchKeyEvent(ev)
}
}
/**
* dispatch a touch event to downstream views
*
* @param event the event to dispatch
* @return was the event consumed?
*/
fun dispatchDownstreamTouchEvent(event: MotionEvent) =
super.dispatchTouchEvent(event)
/**
* ensures that swipe controllers are initialized and attached.
* on some ROMs with SDK <= 23, [onCreate] and [onStart] may not be called correctly.
* see https://github.com/revanced/revanced-patches/issues/446
*/
private fun ensureInitialized() {
if (!this::config.isInitialized) {
LogHelper.printException(
this.javaClass,
"swipe controls were not initialized in onCreate, initializing on-the-fly (SDK is ${Build.VERSION.SDK_INT})"
)
initialize()
reAttachOverlays()
}
}
/**
* initializes controllers, only call once
*/
private fun initialize() {
// create controllers
LogHelper.info(this.javaClass, "initializing swipe controls controllers")
config = SwipeControlsConfigurationProvider(this)
@ -102,36 +153,15 @@ class SwipeControlsHostActivity : Activity() {
currentHost = WeakReference(this)
}
override fun onStart() {
super.onStart()
// (re) attach overlay
/**
* (re) attaches swipe overlays
*/
private fun reAttachOverlays() {
LogHelper.info(this.javaClass, "attaching swipe controls overlay")
contentRoot.removeView(overlay)
contentRoot.addView(overlay)
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
return if ((ev != null) && gesture.submitTouchEvent(ev)) true else {
super.dispatchTouchEvent(ev)
}
}
override fun dispatchKeyEvent(ev: KeyEvent?): Boolean {
return if ((ev != null) && keys.onKeyEvent(ev)) true else {
super.dispatchKeyEvent(ev)
}
}
/**
* dispatch a touch event to downstream views
*
* @param event the event to dispatch
* @return was the event consumed?
*/
fun dispatchDownstreamTouchEvent(event: MotionEvent) =
super.dispatchTouchEvent(event)
/**
* called when the player type changes
*