mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-17 15:05:03 -05:00
Add K9WebViewClient to open links in (external) browser
This commit is contained in:
parent
63ea8a107e
commit
b890903886
52
src/com/fsck/k9/view/K9WebViewClient.java
Normal file
52
src/com/fsck/k9/view/K9WebViewClient.java
Normal 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);
|
||||
}
|
||||
}
|
||||
|
@ -87,6 +87,8 @@ public class MessageWebView extends RigidWebView {
|
||||
|
||||
// Disable network images by default. This is overridden by preferences.
|
||||
blockNetworkData(true);
|
||||
|
||||
setWebViewClient(K9WebViewClient.newInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user