-
-
Notifications
You must be signed in to change notification settings - Fork 800
Expand file tree
/
Copy pathObservableWebView.kt
More file actions
215 lines (192 loc) · 8.04 KB
/
ObservableWebView.kt
File metadata and controls
215 lines (192 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package org.wikipedia.views
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.os.SystemClock
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.webkit.WebView
import org.wikipedia.concurrency.FlowEventBus
import org.wikipedia.events.WebViewInvalidateEvent
import org.wikipedia.util.DimenUtil.densityScalar
import org.wikipedia.views.FrameLayoutNavMenuTriggerer.Companion.setChildViewScrolled
import java.util.*
import kotlin.math.abs
class ObservableWebView : WebView {
fun interface OnClickListener {
fun onClick(x: Float, y: Float): Boolean
}
fun interface OnScrollChangeListener {
fun onScrollChanged(oldScrollY: Int, scrollY: Int, isHumanScroll: Boolean)
}
fun interface OnDownMotionEventListener {
fun onDownMotionEvent()
}
fun interface OnUpOrCancelMotionEventListener {
fun onUpOrCancelMotionEvent()
}
fun interface OnContentHeightChangedListener {
fun onContentHeightChanged(contentHeight: Int)
}
fun interface OnFastScrollListener {
fun onFastScroll()
}
private var onClickListeners: MutableList<OnClickListener> = ArrayList()
private var onScrollChangeListeners: MutableList<OnScrollChangeListener> = ArrayList()
private var onDownMotionEventListeners: MutableList<OnDownMotionEventListener> = ArrayList()
private var onUpOrCancelMotionEventListeners: MutableList<OnUpOrCancelMotionEventListener> = ArrayList()
private var onContentHeightChangedListeners: MutableList<OnContentHeightChangedListener> = ArrayList()
private var onFastScrollListener: OnFastScrollListener? = null
private var currentContentHeight = 0
private var lastScrollTime: Long = 0
private var totalAmountScrolled = 0
private var drawEventsWhileSwiping = 0
private var touchSlop = ViewConfiguration.get(context).scaledTouchSlop
var scrollEventsEnabled = true
var touchStartX = 0f
private set
var touchStartY = 0f
private set
var lastTouchX = 0f
private set
var lastTouchY = 0f
private set
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet? = null) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int) : super(context, attrs, defStyle)
fun addOnClickListener(onClickListener: OnClickListener) {
onClickListeners.add(onClickListener)
}
fun addOnScrollChangeListener(onScrollChangeListener: OnScrollChangeListener) {
onScrollChangeListeners.add(onScrollChangeListener)
}
fun addOnDownMotionEventListener(onDownMotionEventListener: OnDownMotionEventListener) {
onDownMotionEventListeners.add(onDownMotionEventListener)
}
fun addOnUpOrCancelMotionEventListener(onUpOrCancelMotionEventListener: OnUpOrCancelMotionEventListener) {
onUpOrCancelMotionEventListeners.add(onUpOrCancelMotionEventListener)
}
fun addOnContentHeightChangedListener(onContentHeightChangedListener: OnContentHeightChangedListener) {
onContentHeightChangedListeners.add(onContentHeightChangedListener)
}
fun clearAllListeners() {
onClickListeners.clear()
onScrollChangeListeners.clear()
onDownMotionEventListeners.clear()
onUpOrCancelMotionEventListeners.clear()
onContentHeightChangedListeners.clear()
onFastScrollListener = null
}
override fun onScrollChanged(left: Int, top: Int, oldLeft: Int, oldTop: Int) {
super.onScrollChanged(left, top, oldLeft, oldTop)
val isHumanScroll = abs(top - oldTop) < MAX_HUMAN_SCROLL
if (scrollEventsEnabled) {
onScrollChangeListeners.forEach {
it.onScrollChanged(oldTop, top, isHumanScroll)
}
}
if (!isHumanScroll) {
return
}
totalAmountScrolled += top - oldTop
if (abs(totalAmountScrolled) > FAST_SCROLL_THRESHOLD &&
onFastScrollListener != null) {
onFastScrollListener!!.onFastScroll()
totalAmountScrolled = 0
}
lastScrollTime = System.currentTimeMillis()
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.buttonState == MotionEvent.BUTTON_SECONDARY &&
event.actionMasked == MotionEvent.ACTION_DOWN) {
handleMouseRightClick(event.x, event.y)
return true
}
lastTouchX = event.x
lastTouchY = event.y
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
onDownMotionEventListeners.forEach {
it.onDownMotionEvent()
}
if (System.currentTimeMillis() - lastScrollTime > MAX_MILLIS_BETWEEN_SCROLLS) {
totalAmountScrolled = 0
}
touchStartX = event.x
touchStartY = event.y
drawEventsWhileSwiping = 0
}
MotionEvent.ACTION_UP -> {
if (abs(event.x - touchStartX) <= touchSlop &&
abs(event.y - touchStartY) <= touchSlop) {
// Fire a click event, but only if the hit test doesn't land on a hyperlink
// (i.e. only if the user clicks on whitespace or plain text in the WebView),
// since link clicks will already be passed to the LinkHandler separately.
if (hitTestResult.type == HitTestResult.UNKNOWN_TYPE) {
if (onClickListeners.any { it.onClick(event.x, event.y) }) {
return true
}
}
}
drawEventsWhileSwiping = 0
onUpOrCancelMotionEventListeners.forEach {
it.onUpOrCancelMotionEvent()
}
drawEventsWhileSwiping = 0
}
MotionEvent.ACTION_CANCEL -> {
onUpOrCancelMotionEventListeners.forEach {
it.onUpOrCancelMotionEvent()
}
drawEventsWhileSwiping = 0
}
else -> { }
}
return super.onTouchEvent(event)
}
private fun handleMouseRightClick(x: Float, y: Float) {
val eventTimeTravelMillis = 1000
post {
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis() + eventTimeTravelMillis,
MotionEvent.ACTION_DOWN, x, y, 0))
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (isInEditMode) {
return
}
drawEventsWhileSwiping++
if (drawEventsWhileSwiping > SWIPE_DRAW_TOLERANCE) {
setChildViewScrolled()
}
if (currentContentHeight != contentHeight) {
currentContentHeight = contentHeight
onContentHeightChangedListeners.forEach {
it.onContentHeightChanged(currentContentHeight)
}
}
FlowEventBus.post(INVALIDATE_EVENT)
}
override fun overScrollBy(deltaX: Int, deltaY: Int, scrollX: Int, scrollY: Int,
scrollRangeX: Int, scrollRangeY: Int, maxOverScrollX: Int,
maxOverScrollY: Int, isTouchEvent: Boolean): Boolean {
// Block horizontal scrolling entirely
return super.overScrollBy(0, deltaY, 0, scrollY, 0, scrollRangeY, 0,
maxOverScrollY, isTouchEvent)
}
override fun scrollTo(x: Int, y: Int) {
// Only allow vertical scrolling
super.scrollTo(0, y)
}
companion object {
private val INVALIDATE_EVENT = WebViewInvalidateEvent()
private val FAST_SCROLL_THRESHOLD = (1000 * densityScalar).toInt()
private val MAX_HUMAN_SCROLL = (500 * densityScalar).toInt()
private const val MAX_MILLIS_BETWEEN_SCROLLS = 500
private const val SWIPE_DRAW_TOLERANCE = 4
}
}