Add K9WebViewClient to open links in (external) browser

This commit is contained in:
cketti 2015-06-09 03:07:23 +02:00
parent 9d225dc84c
commit 24c52a308f
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package com.fsck.k9.view;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Browser;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class K9WebViewClient extends WebViewClient {
public static WebViewClient newInstance() {
return new K9WebViewClient();
}
private K9WebViewClient() {}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
Uri uri = Uri.parse(url);
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 void addActivityFlags(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
}
}

View File

@ -87,6 +87,8 @@ public class MessageWebView extends RigidWebView {
// Disable network images by default. This is overridden by preferences.
blockNetworkData(true);
setWebViewClient(K9WebViewClient.newInstance());
}
/**