Merge "Implement shouldOverrideUrlLoading to open hyperlink on HTMLViewer."
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0c52618..5f3a124 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -17,5 +17,6 @@
<resources>
<!-- Title of the HTML Viewer activity. -->
<string name="app_label">HTML Viewer</string>
+ <string name="cannot_open_link">No application can open this link.</string>
</resources>
diff --git a/src/com/android/htmlviewer/HTMLViewerActivity.java b/src/com/android/htmlviewer/HTMLViewerActivity.java
index 8a945a9..adda32c 100644
--- a/src/com/android/htmlviewer/HTMLViewerActivity.java
+++ b/src/com/android/htmlviewer/HTMLViewerActivity.java
@@ -17,6 +17,7 @@
package com.android.htmlviewer;
import android.app.Activity;
+import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
@@ -29,9 +30,11 @@
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
+import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URISyntaxException;
import java.util.zip.GZIPInputStream;
/**
@@ -101,6 +104,38 @@
}
@Override
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
+ Intent intent;
+ // Perform generic parsing of the URI to turn it into an Intent.
+ try {
+ intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
+ } catch (URISyntaxException ex) {
+ Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage());
+ Toast.makeText(HTMLViewerActivity.this,
+ R.string.cannot_open_link, Toast.LENGTH_SHORT).show();
+ return true;
+ }
+ // Sanitize the Intent, ensuring web pages can not bypass browser
+ // security (only access to BROWSABLE activities).
+ intent.addCategory(Intent.CATEGORY_BROWSABLE);
+ intent.setComponent(null);
+ Intent selector = intent.getSelector();
+ if (selector != null) {
+ selector.addCategory(Intent.CATEGORY_BROWSABLE);
+ selector.setComponent(null);
+ }
+
+ try {
+ view.getContext().startActivity(intent);
+ } catch (ActivityNotFoundException ex) {
+ Log.w(TAG, "No application can handle " + url);
+ Toast.makeText(HTMLViewerActivity.this,
+ R.string.cannot_open_link, Toast.LENGTH_SHORT).show();
+ }
+ return true;
+ }
+
+ @Override
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
final Uri uri = request.getUrl();