|
1 | 1 | package app.revanced.integrations.swipecontrols |
2 | 2 |
|
3 | 3 | import android.app.Activity |
| 4 | +import android.os.Build |
4 | 5 | import android.os.Bundle |
5 | 6 | import android.view.KeyEvent |
6 | 7 | import android.view.MotionEvent |
@@ -68,7 +69,57 @@ class SwipeControlsHostActivity : Activity() { |
68 | 69 |
|
69 | 70 | override fun onCreate(savedInstanceState: Bundle?) { |
70 | 71 | 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 | + } |
71 | 118 |
|
| 119 | + /** |
| 120 | + * initializes controllers, only call once |
| 121 | + */ |
| 122 | + private fun initialize() { |
72 | 123 | // create controllers |
73 | 124 | LogHelper.info(this.javaClass, "initializing swipe controls controllers") |
74 | 125 | config = SwipeControlsConfigurationProvider(this) |
@@ -102,36 +153,15 @@ class SwipeControlsHostActivity : Activity() { |
102 | 153 | currentHost = WeakReference(this) |
103 | 154 | } |
104 | 155 |
|
105 | | - override fun onStart() { |
106 | | - super.onStart() |
107 | | - |
108 | | - // (re) attach overlay |
| 156 | + /** |
| 157 | + * (re) attaches swipe overlays |
| 158 | + */ |
| 159 | + private fun reAttachOverlays() { |
109 | 160 | LogHelper.info(this.javaClass, "attaching swipe controls overlay") |
110 | 161 | contentRoot.removeView(overlay) |
111 | 162 | contentRoot.addView(overlay) |
112 | 163 | } |
113 | 164 |
|
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 | | - |
135 | 165 | /** |
136 | 166 | * called when the player type changes |
137 | 167 | * |
|
0 commit comments