1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 05:02:26 -05:00

Merge pull request #590

WebView: Open links in external Browser
This commit is contained in:
cketti 2015-04-04 01:20:08 +02:00
commit 017ae1d2f3

View File

@ -5,11 +5,14 @@ import java.io.InputStream;
import java.util.Stack; import java.util.Stack;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Build.VERSION_CODES; import android.os.Build.VERSION_CODES;
import android.provider.Browser;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.webkit.WebResourceRequest; import android.webkit.WebResourceRequest;
@ -48,6 +51,38 @@ public abstract class K9WebViewClient extends WebViewClient {
this.part = part; this.part = part;
} }
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
Uri uri = Uri.parse(url);
if (CID_SCHEME.equals(uri.getScheme())) {
return false;
}
Context context = webView.getContext();
Intent intent = createBrowserViewIntent(uri, context);
addActivityFlags(intent);
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());
intent.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true);
return intent;
}
protected abstract void addActivityFlags(Intent intent);
protected WebResourceResponse shouldInterceptRequest(WebView webView, Uri uri) { protected WebResourceResponse shouldInterceptRequest(WebView webView, Uri uri) {
if (!CID_SCHEME.equals(uri.getScheme())) { if (!CID_SCHEME.equals(uri.getScheme())) {
return RESULT_DO_NOT_INTERCEPT; return RESULT_DO_NOT_INTERCEPT;
@ -99,16 +134,21 @@ public abstract class K9WebViewClient extends WebViewClient {
} }
@SuppressWarnings("deprecation")
private static class PreLollipopWebViewClient extends K9WebViewClient { private static class PreLollipopWebViewClient extends K9WebViewClient {
protected PreLollipopWebViewClient(Part part) { protected PreLollipopWebViewClient(Part part) {
super(part); super(part);
} }
@SuppressWarnings("deprecation")
@Override @Override
public WebResourceResponse shouldInterceptRequest(WebView webView, String url) { public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
return shouldInterceptRequest(webView, Uri.parse(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) @TargetApi(VERSION_CODES.LOLLIPOP)
@ -121,5 +161,10 @@ public abstract class K9WebViewClient extends WebViewClient {
public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest request) { public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest request) {
return shouldInterceptRequest(webView, request.getUrl()); return shouldInterceptRequest(webView, request.getUrl());
} }
@Override
protected void addActivityFlags(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
}
} }
} }