From bd2f80597eb243a45d76e5db8c6eee09996db92c Mon Sep 17 00:00:00 2001 From: Craig Russell Date: Fri, 9 Mar 2018 11:35:25 +0000 Subject: [PATCH 1/2] Add flag so that users' typed text isn't used for personalisation --- ...{NestedWebView.kt => DuckDuckGoWebView.kt} | 39 +++++++++++++++---- .../main/res/layout/fragment_browser_tab.xml | 2 +- .../include_duckduckgo_browser_webview.xml | 4 +- 3 files changed, 34 insertions(+), 11 deletions(-) rename app/src/main/java/com/duckduckgo/app/browser/{NestedWebView.kt => DuckDuckGoWebView.kt} (76%) diff --git a/app/src/main/java/com/duckduckgo/app/browser/NestedWebView.kt b/app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt similarity index 76% rename from app/src/main/java/com/duckduckgo/app/browser/NestedWebView.kt rename to app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt index 57ab51aa1179..ddd692b7caa7 100644 --- a/app/src/main/java/com/duckduckgo/app/browser/NestedWebView.kt +++ b/app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt @@ -17,24 +17,31 @@ package com.duckduckgo.app.browser import android.annotation.SuppressLint +import android.annotation.TargetApi import android.content.Context +import android.os.Build import android.support.v4.view.NestedScrollingChild import android.support.v4.view.NestedScrollingChildHelper import android.support.v4.view.ViewCompat import android.util.AttributeSet import android.view.MotionEvent +import android.view.inputmethod.BaseInputConnection +import android.view.inputmethod.EditorInfo +import android.view.inputmethod.InputConnection import android.webkit.WebView /** - * NestedWebView which allows the WebView to hide the toolbar when placed in a CoordinatorLayout + * 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 * - * Based on https://github.com/takahirom/webview-in-coordinatorlayout + * Originally based on https://github.com/takahirom/webview-in-coordinatorlayout for scrolling behaviour */ -class NestedWebView : WebView, NestedScrollingChild { +class DuckDuckGoWebView : WebView, NestedScrollingChild { private var lastY: Int = 0 private val scrollOffset = IntArray(2) private val scrollConsumed = IntArray(2) - private var nestedOffetY: Int = 0 + private var nestedOffsetY: Int = 0 private var nestedScrollHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this) constructor(context: Context) : this(context, null) @@ -43,6 +50,22 @@ class NestedWebView : WebView, NestedScrollingChild { isNestedScrollingEnabled = true } + override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection { + val inputConnection = BaseInputConnection(this, false) + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + addNoPersonalisedFlag(outAttrs) + } + + return inputConnection + } + + + @TargetApi(Build.VERSION_CODES.O) + private fun addNoPersonalisedFlag(outAttrs: EditorInfo) { + outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING + } + @SuppressLint("ClickableViewAccessibility") override fun onTouchEvent(ev: MotionEvent): Boolean { var returnValue = false @@ -50,10 +73,10 @@ class NestedWebView : WebView, NestedScrollingChild { val event = MotionEvent.obtain(ev) val action = event.actionMasked if (action == MotionEvent.ACTION_DOWN) { - nestedOffetY = 0 + nestedOffsetY = 0 } val eventY = event.y.toInt() - event.offsetLocation(0f, nestedOffetY.toFloat()) + event.offsetLocation(0f, nestedOffsetY.toFloat()) when (action) { MotionEvent.ACTION_MOVE -> { @@ -63,14 +86,14 @@ class NestedWebView : WebView, NestedScrollingChild { deltaY -= scrollConsumed[1] lastY = eventY - scrollOffset[1] event.offsetLocation(0f, (-scrollOffset[1]).toFloat()) - nestedOffetY += scrollOffset[1] + nestedOffsetY += scrollOffset[1] } returnValue = super.onTouchEvent(event) if (dispatchNestedScroll(0, scrollOffset[1], 0, deltaY, scrollOffset)) { event.offsetLocation(0f, scrollOffset[1].toFloat()) - nestedOffetY += scrollOffset[1] + nestedOffsetY += scrollOffset[1] lastY -= scrollOffset[1] } } diff --git a/app/src/main/res/layout/fragment_browser_tab.xml b/app/src/main/res/layout/fragment_browser_tab.xml index d31ee5843294..932200994e8c 100644 --- a/app/src/main/res/layout/fragment_browser_tab.xml +++ b/app/src/main/res/layout/fragment_browser_tab.xml @@ -104,7 +104,7 @@ android:elevation="4dp" android:fontFamily="sans-serif-medium" android:hint="@string/omnibarInputHint" - android:imeOptions="flagNoExtractUi|actionDone" + android:imeOptions="flagNoExtractUi|actionDone|flagNoPersonalizedLearning" android:inputType="textUri|textNoSuggestions" android:maxLines="1" android:paddingBottom="4dp" diff --git a/app/src/main/res/layout/include_duckduckgo_browser_webview.xml b/app/src/main/res/layout/include_duckduckgo_browser_webview.xml index f6041a4c73d6..bd8e5518ce57 100644 --- a/app/src/main/res/layout/include_duckduckgo_browser_webview.xml +++ b/app/src/main/res/layout/include_duckduckgo_browser_webview.xml @@ -14,7 +14,7 @@ ~ limitations under the License. --> - - \ No newline at end of file + \ No newline at end of file From 25254b33b000c636e1d4a9cc29a26eb7dc146a95 Mon Sep 17 00:00:00 2001 From: Craig Russell Date: Fri, 9 Mar 2018 15:38:45 +0000 Subject: [PATCH 2/2] Respect text field type by using super's input connection --- .../java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt b/app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt index ddd692b7caa7..d92bedfd8bdd 100644 --- a/app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt +++ b/app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt @@ -25,7 +25,6 @@ import android.support.v4.view.NestedScrollingChildHelper import android.support.v4.view.ViewCompat import android.util.AttributeSet import android.view.MotionEvent -import android.view.inputmethod.BaseInputConnection import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputConnection import android.webkit.WebView @@ -50,8 +49,8 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild { isNestedScrollingEnabled = true } - override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection { - val inputConnection = BaseInputConnection(this, false) + override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? { + val inputConnection = super.onCreateInputConnection(outAttrs) ?: return null if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { addNoPersonalisedFlag(outAttrs) @@ -63,7 +62,7 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild { @TargetApi(Build.VERSION_CODES.O) private fun addNoPersonalisedFlag(outAttrs: EditorInfo) { - outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING + outAttrs.imeOptions = outAttrs.imeOptions or EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING } @SuppressLint("ClickableViewAccessibility")