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 package app.revanced.integrations.swipecontrols
import android.app.Activity import android.app.Activity
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.KeyEvent import android.view.KeyEvent
import android.view.MotionEvent import android.view.MotionEvent
@ -68,7 +69,57 @@ class SwipeControlsHostActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) 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 // create controllers
LogHelper.info(this.javaClass, "initializing swipe controls controllers") LogHelper.info(this.javaClass, "initializing swipe controls controllers")
config = SwipeControlsConfigurationProvider(this) config = SwipeControlsConfigurationProvider(this)
@ -102,36 +153,15 @@ class SwipeControlsHostActivity : Activity() {
currentHost = WeakReference(this) currentHost = WeakReference(this)
} }
override fun onStart() { /**
super.onStart() * (re) attaches swipe overlays
*/
// (re) attach overlay private fun reAttachOverlays() {
LogHelper.info(this.javaClass, "attaching swipe controls overlay") LogHelper.info(this.javaClass, "attaching swipe controls overlay")
contentRoot.removeView(overlay) contentRoot.removeView(overlay)
contentRoot.addView(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 * called when the player type changes
* *