forked from duckduckgo/Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DuckDuckGoWebView.kt
145 lines (117 loc) · 5.49 KB
/
DuckDuckGoWebView.kt
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
/*
* Copyright (c) 2017 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.duckduckgo.app.browser
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.webkit.WebView
import androidx.core.view.NestedScrollingChild
import androidx.core.view.NestedScrollingChildHelper
import androidx.core.view.ViewCompat
/**
* WebView subclass which allows the WebView to
* - hide the toolbar when placed in a CoordinatorLayout
* - add the flag so that users' typing isn't used for personalisation
*
* Originally based on https://github.com/takahirom/webview-in-coordinatorlayout for scrolling behaviour
*/
class DuckDuckGoWebView : WebView, NestedScrollingChild {
private var lastY: Int = 0
private val scrollOffset = IntArray(2)
private val scrollConsumed = IntArray(2)
private var nestedOffsetY: Int = 0
private var nestedScrollHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
isNestedScrollingEnabled = true
}
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
val inputConnection = super.onCreateInputConnection(outAttrs) ?: return null
addNoPersonalisedFlag(outAttrs)
return inputConnection
}
private fun addNoPersonalisedFlag(outAttrs: EditorInfo) {
outAttrs.imeOptions = outAttrs.imeOptions or IME_FLAG_NO_PERSONALIZED_LEARNING
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(ev: MotionEvent): Boolean {
var returnValue = false
val event = MotionEvent.obtain(ev)
val action = event.actionMasked
if (action == MotionEvent.ACTION_DOWN) {
nestedOffsetY = 0
}
val eventY = event.y.toInt()
event.offsetLocation(0f, nestedOffsetY.toFloat())
when (action) {
MotionEvent.ACTION_MOVE -> {
var deltaY = lastY - eventY
if (dispatchNestedPreScroll(0, deltaY, scrollConsumed, scrollOffset)) {
deltaY -= scrollConsumed[1]
lastY = eventY - scrollOffset[1]
event.offsetLocation(0f, (-scrollOffset[1]).toFloat())
nestedOffsetY += scrollOffset[1]
}
returnValue = super.onTouchEvent(event)
if (scrollY == 0) {
// Give parents control (required for SwipeRefreshLayout to work)
stopNestedScroll()
} else if (dispatchNestedScroll(0, scrollOffset[1], 0, deltaY, scrollOffset)) {
event.offsetLocation(0f, scrollOffset[1].toFloat())
nestedOffsetY += scrollOffset[1]
lastY -= scrollOffset[1]
}
}
MotionEvent.ACTION_DOWN -> {
returnValue = super.onTouchEvent(event)
lastY = eventY
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
}
else -> {
returnValue = super.onTouchEvent(event)
stopNestedScroll()
}
}
return returnValue
}
override fun setNestedScrollingEnabled(enabled: Boolean) {
nestedScrollHelper.isNestedScrollingEnabled = enabled
}
override fun stopNestedScroll() {
nestedScrollHelper.stopNestedScroll()
}
override fun isNestedScrollingEnabled(): Boolean = nestedScrollHelper.isNestedScrollingEnabled
override fun startNestedScroll(axes: Int): Boolean = nestedScrollHelper.startNestedScroll(axes)
override fun hasNestedScrollingParent(): Boolean = nestedScrollHelper.hasNestedScrollingParent()
override fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: IntArray?): Boolean =
nestedScrollHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow)
override fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?): Boolean =
nestedScrollHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
override fun dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean): Boolean =
nestedScrollHelper.dispatchNestedFling(velocityX, velocityY, consumed)
override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean =
nestedScrollHelper.dispatchNestedPreFling(velocityX, velocityY)
companion object {
/*
* Taken from EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING
* We can't use that value directly as it was only added on Oreo, but we can apply the value anyway.
*/
private const val IME_FLAG_NO_PERSONALIZED_LEARNING = 0x1000000
}
}