From 46f74bd11cbfe42b688bcd53a609c5ca751d270f Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Thu, 26 Mar 2015 02:17:48 +0100 Subject: [PATCH 1/5] WebView: Open links in external Browser 1a20ca06f1ef8ab0d4a5952dcc614973014b50c7 connected a WebViewClient to the WebView. But as soon as a client is connected, the WebView stops handling links itself and tries to display everything on its own. Override shouldOverrideUrlLoading() and replicate what Android's default WebView does if no WebViewClient is connected to work around this. This fixes #587. --- .../com/fsck/k9/view/K9WebViewClient.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java index a52c64f5d..3b96ab8c4 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java +++ b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java @@ -5,11 +5,14 @@ import java.io.InputStream; import java.util.Stack; import android.annotation.TargetApi; +import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.Context; +import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Build.VERSION_CODES; +import android.provider.Browser; import android.text.TextUtils; import android.util.Log; import android.webkit.WebResourceRequest; @@ -48,6 +51,30 @@ public abstract class K9WebViewClient extends WebViewClient { this.part = part; } + @Override + public boolean shouldOverrideUrlLoading(WebView webView, String url) { + Context context = webView.getContext(); + Uri uri = Uri.parse(url); + if (!CID_SCHEME.equals(uri.getScheme())) { + // Replicate Android 4 android.webkit.CallbackProxy.uiOverrideUrlLoading() + // default behavior to open clicked links in external applications. + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + intent.addCategory(Intent.CATEGORY_BROWSABLE); + // If another application is running a WebView and launches the + // Browser through this Intent, we want to reuse the same window if + // possible. + intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); + try { + context.startActivity(intent); + return true; + } catch (ActivityNotFoundException ex) { + // If no application can handle the URL, assume that the + // WebView can handle it. + } + } + return false; + } + protected WebResourceResponse shouldInterceptRequest(WebView webView, Uri uri) { if (!CID_SCHEME.equals(uri.getScheme())) { return RESULT_DO_NOT_INTERCEPT; From 8bcf9b1d502c46ef5941e7ba9da51f68a8f0b190 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 4 Apr 2015 00:50:14 +0200 Subject: [PATCH 2/5] Remove comments --- k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java index 3b96ab8c4..1022d385b 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java +++ b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java @@ -56,13 +56,8 @@ public abstract class K9WebViewClient extends WebViewClient { Context context = webView.getContext(); Uri uri = Uri.parse(url); if (!CID_SCHEME.equals(uri.getScheme())) { - // Replicate Android 4 android.webkit.CallbackProxy.uiOverrideUrlLoading() - // default behavior to open clicked links in external applications. Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.addCategory(Intent.CATEGORY_BROWSABLE); - // If another application is running a WebView and launches the - // Browser through this Intent, we want to reuse the same window if - // possible. intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); try { context.startActivity(intent); From 09babb6e8826c372c2411dfdcb9e76b9c9392dcc Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 4 Apr 2015 00:55:53 +0200 Subject: [PATCH 3/5] Restructure the code a bit --- .../com/fsck/k9/view/K9WebViewClient.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java index 1022d385b..aaf02e126 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java +++ b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java @@ -53,21 +53,30 @@ public abstract class K9WebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { - Context context = webView.getContext(); Uri uri = Uri.parse(url); - if (!CID_SCHEME.equals(uri.getScheme())) { - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - intent.addCategory(Intent.CATEGORY_BROWSABLE); - intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); - try { - context.startActivity(intent); - return true; - } catch (ActivityNotFoundException ex) { - // If no application can handle the URL, assume that the - // WebView can handle it. - } + if (CID_SCHEME.equals(uri.getScheme())) { + return false; } - return false; + + Context context = webView.getContext(); + Intent intent = createBrowserViewIntent(uri, context); + + boolean overridingUrlLoading = false; + try { + context.startActivity(intent); + overridingUrlLoading = true; + } catch (ActivityNotFoundException ex) { + // If no application can handle the URL, assume that the WebView can handle it. + } + + return overridingUrlLoading; + } + + private Intent createBrowserViewIntent(Uri uri, Context context) { + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + intent.addCategory(Intent.CATEGORY_BROWSABLE); + intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); + return intent; } protected WebResourceResponse shouldInterceptRequest(WebView webView, Uri uri) { From ba8cb6c85d4d338212ac15eb9ff1455ba8bde90c Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 4 Apr 2015 00:57:47 +0200 Subject: [PATCH 4/5] Set EXTRA_CREATE_NEW_TAB to 'true' in browser view intent --- k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java index aaf02e126..95566847d 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java +++ b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java @@ -76,6 +76,7 @@ public abstract class K9WebViewClient extends WebViewClient { Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.addCategory(Intent.CATEGORY_BROWSABLE); intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); + intent.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true); return intent; } From babd3a530f51e7f88d0414d2135200542c79f6d1 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 4 Apr 2015 01:06:14 +0200 Subject: [PATCH 5/5] Add activity flags to browser view intent --- .../java/com/fsck/k9/view/K9WebViewClient.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java index 95566847d..548cf2a29 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java +++ b/k9mail/src/main/java/com/fsck/k9/view/K9WebViewClient.java @@ -60,6 +60,7 @@ public abstract class K9WebViewClient extends WebViewClient { Context context = webView.getContext(); Intent intent = createBrowserViewIntent(uri, context); + addActivityFlags(intent); boolean overridingUrlLoading = false; try { @@ -80,6 +81,8 @@ public abstract class K9WebViewClient extends WebViewClient { return intent; } + protected abstract void addActivityFlags(Intent intent); + protected WebResourceResponse shouldInterceptRequest(WebView webView, Uri uri) { if (!CID_SCHEME.equals(uri.getScheme())) { return RESULT_DO_NOT_INTERCEPT; @@ -131,16 +134,21 @@ public abstract class K9WebViewClient extends WebViewClient { } + @SuppressWarnings("deprecation") private static class PreLollipopWebViewClient extends K9WebViewClient { protected PreLollipopWebViewClient(Part part) { super(part); } - @SuppressWarnings("deprecation") @Override public WebResourceResponse shouldInterceptRequest(WebView webView, String url) { return shouldInterceptRequest(webView, Uri.parse(url)); } + + @Override + protected void addActivityFlags(Intent intent) { + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); + } } @TargetApi(VERSION_CODES.LOLLIPOP) @@ -153,5 +161,10 @@ public abstract class K9WebViewClient extends WebViewClient { public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest request) { return shouldInterceptRequest(webView, request.getUrl()); } + + @Override + protected void addActivityFlags(Intent intent) { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); + } } }