OSDN Git Service

Catch OperationUnsupportedException when linkifying using WebView
authorAaron Whyte <awhyte@google.com>
Fri, 6 Jun 2014 23:41:38 +0000 (16:41 -0700)
committerJustin Koh <justinkoh@google.com>
Tue, 10 Jun 2014 00:59:03 +0000 (17:59 -0700)
This fixes a CTS test for Wearable. We cannot check for FEATURE_WEBVIEW, because
there's no way to get a PackageManager from within these static methods.
Bug: 15131296

Change-Id: I7bf7564b6209f330a413ed54a94be1e07fedb30d

core/java/android/text/util/Linkify.java

index deb138d..c1341e1 100644 (file)
@@ -465,32 +465,39 @@ public class Linkify {
         String address;
         int base = 0;
 
-        while ((address = WebView.findAddress(string)) != null) {
-            int start = string.indexOf(address);
+        try {
+            while ((address = WebView.findAddress(string)) != null) {
+                int start = string.indexOf(address);
 
-            if (start < 0) {
-                break;
-            }
+                if (start < 0) {
+                    break;
+                }
 
-            LinkSpec spec = new LinkSpec();
-            int length = address.length();
-            int end = start + length;
-            
-            spec.start = base + start;
-            spec.end = base + end;
-            string = string.substring(end);
-            base += end;
-
-            String encodedAddress = null;
-
-            try {
-                encodedAddress = URLEncoder.encode(address,"UTF-8");
-            } catch (UnsupportedEncodingException e) {
-                continue;
-            }
+                LinkSpec spec = new LinkSpec();
+                int length = address.length();
+                int end = start + length;
 
-            spec.url = "geo:0,0?q=" + encodedAddress;
-            links.add(spec);
+                spec.start = base + start;
+                spec.end = base + end;
+                string = string.substring(end);
+                base += end;
+
+                String encodedAddress = null;
+
+                try {
+                    encodedAddress = URLEncoder.encode(address,"UTF-8");
+                } catch (UnsupportedEncodingException e) {
+                    continue;
+                }
+
+                spec.url = "geo:0,0?q=" + encodedAddress;
+                links.add(spec);
+            }
+        } catch (UnsupportedOperationException e) {
+            // findAddress may fail with an unsupported exception on platforms without a WebView.
+            // In this case, we will not append anything to the links variable: it would have died
+            // in WebView.findAddress.
+            return;
         }
     }