Various improvements to handling and detection of URLs in codes
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 11 Dec 2007 21:52:11 +0000 (21:52 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 11 Dec 2007 21:52:11 +0000 (21:52 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@113 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core-ext/src/com/google/zxing/client/result/URIParsedResult.java
javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java

index 745e8c2..1ba3126 100644 (file)
@@ -28,6 +28,10 @@ public final class URIParsedResult extends ParsedReaderResult {
 
   public URIParsedResult(String rawText) {
     super(ParsedReaderResultType.URI);
 
   public URIParsedResult(String rawText) {
     super(ParsedReaderResultType.URI);
+    if (rawText.startsWith("URL:")) {
+      // Sometimes a URL is prefixed with "URL:" -- support this
+      rawText = rawText.substring(4);
+    }
     try {
       uri = new URI(rawText);
     } catch (URISyntaxException urise) {
     try {
       uri = new URI(rawText);
     } catch (URISyntaxException urise) {
index ba11010..02ec677 100644 (file)
@@ -104,8 +104,8 @@ public final class ZXingMIDlet extends MIDlet {
 
   // Convenience methods to show dialogs
 
 
   // Convenience methods to show dialogs
 
-  void showYesNo(String title, final String text) {
-    Alert alert = new Alert(title, text, null, AlertType.CONFIRMATION);
+  private void showOpenURL(final String text) {
+    Alert alert = new Alert("Open web page?", text, null, AlertType.CONFIRMATION);
     alert.setTimeout(Alert.FOREVER);
     final Command cancel = new Command("Cancel", Command.CANCEL, 1);
     alert.addCommand(cancel);
     alert.setTimeout(Alert.FOREVER);
     final Command cancel = new Command("Cancel", Command.CANCEL, 1);
     alert.addCommand(cancel);
@@ -129,7 +129,7 @@ public final class ZXingMIDlet extends MIDlet {
     showAlert(alert);
   }
 
     showAlert(alert);
   }
 
-  void showAlert(String title, String text) {
+  private void showAlert(String title, String text) {
     Alert alert = new Alert(title, text, null, AlertType.INFO);
     alert.setTimeout(Alert.FOREVER);
     showAlert(alert);
     Alert alert = new Alert(title, text, null, AlertType.INFO);
     alert.setTimeout(Alert.FOREVER);
     showAlert(alert);
@@ -144,18 +144,50 @@ public final class ZXingMIDlet extends MIDlet {
     display.setCurrent(alert, canvas);
   }
 
     display.setCurrent(alert, canvas);
   }
 
+  /// TODO this whole bit needs to be merged with the code in core-ext -- this is messy and duplicative
+
   void handleDecodedText(String text) {
     // This is a crude imitation of the code found in module core-ext, which handles the contents
     // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
     // on URL parsing routines in java.net. This should be somehow worked around: TODO
     // For now, detect URLs in a simple way, and treat everything else as text
   void handleDecodedText(String text) {
     // This is a crude imitation of the code found in module core-ext, which handles the contents
     // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
     // on URL parsing routines in java.net. This should be somehow worked around: TODO
     // For now, detect URLs in a simple way, and treat everything else as text
-    if (text.startsWith("http://") || text.startsWith("https://") || maybeURLWithoutScheme(text)) {
-      showYesNo("Open web page?", text);
+    if (text.startsWith("http://") || text.startsWith("https://")) {
+      showOpenURL(text);
+    } else if (text.startsWith("HTTP://") || text.startsWith("HTTPS://")) {
+      showOpenURL(decapitalizeProtocol(text));
+    } else if (text.startsWith("URL:")) {
+      showOpenURL(decapitalizeProtocol(text.substring(4)));
+    } else if (text.startsWith("MEBKM:")) {
+      int urlIndex = text.indexOf("URL:", 6);
+      if (urlIndex >= 6) {
+        String url = text.substring( urlIndex + 4);
+        int semicolon = url.indexOf((int) ';');
+        if (semicolon >= 0) {
+          url = url.substring(0, semicolon);
+        }
+        showOpenURL(decapitalizeProtocol(url));
+      } else {
+        showAlert("Barcode detected", text);
+      }
+    } else if (maybeURLWithoutScheme(text)) {
+      showOpenURL("http://" + text);
     } else {
       showAlert("Barcode detected", text);
     }
   }
 
     } else {
       showAlert("Barcode detected", text);
     }
   }
 
+  private static String decapitalizeProtocol(String url) {
+    int protocolEnd = url.indexOf("://");
+    if (protocolEnd >= 0) {
+      return url.substring(0, protocolEnd).toLowerCase() + url.substring(protocolEnd);
+    } else {
+      return url;
+    }
+  }
+
+  /**
+   * Crudely guesses that a string may represent a URL if it has a '.' and no spaces.
+   */
   private static boolean maybeURLWithoutScheme(String text) {
     return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
   }
   private static boolean maybeURLWithoutScheme(String text) {
     return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
   }