Refactorings to allow raw bytes to be passed back with reader result, where applicable
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
index bba7271..fffe2ab 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.google.zxing.client.j2me;
 
+import com.google.zxing.Result;
 import com.google.zxing.client.result.BookmarkDoCoMoResult;
 import com.google.zxing.client.result.EmailAddressResult;
 import com.google.zxing.client.result.EmailDoCoMoResult;
@@ -23,6 +24,7 @@ import com.google.zxing.client.result.ParsedReaderResult;
 import com.google.zxing.client.result.ParsedReaderResultType;
 import com.google.zxing.client.result.UPCParsedResult;
 import com.google.zxing.client.result.URIParsedResult;
+import com.google.zxing.client.result.URLTOResult;
 
 import javax.microedition.io.ConnectionNotFoundException;
 import javax.microedition.lcdui.Alert;
@@ -51,6 +53,10 @@ public final class ZXingMIDlet extends MIDlet {
   private Player player;
   private VideoControl videoControl;
 
+  Canvas getCanvas() {
+    return canvas;
+  }
+
   Player getPlayer() {
     return player;
   }
@@ -80,22 +86,25 @@ public final class ZXingMIDlet extends MIDlet {
     }
   }
 
-       private static Player createPlayer() throws IOException, MediaException {
-               // Try a workaround for Nokias, which want to use capture://image in some cases
-               Player player = null;
-               String platform = System.getProperty("microedition.platform");
-               if (platform != null && platform.indexOf("Nokia") >= 0) {
-                       try {
-                               player = Manager.createPlayer("capture://image");
-                       } catch (MediaException me) {
-                               // if this fails, just continue with capture://video
-                       }
-               }
-               if (player == null) {
-                       player = Manager.createPlayer("capture://video");
-               }
-               return player;
-       }
+  private static Player createPlayer() throws IOException, MediaException {
+    // Try a workaround for Nokias, which want to use capture://image in some cases
+    Player player = null;
+    String platform = System.getProperty("microedition.platform");
+    if (platform != null && platform.indexOf("Nokia") >= 0) {
+      try {
+        player = Manager.createPlayer("capture://image");
+      } catch (MediaException me) {
+        // if this fails, just continue with capture://video
+      } catch (Error e) {
+        // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
+        // We should still try to continue
+      }
+    }
+    if (player == null) {
+      player = Manager.createPlayer("capture://video");
+    }
+    return player;
+  }
 
   protected void pauseApp() {
     if (player != null) {
@@ -103,14 +112,14 @@ public final class ZXingMIDlet extends MIDlet {
         player.stop();
       } catch (MediaException me) {
         // continue?
-        showError(me);        
+        showError(me);
       }
     }
   }
 
   protected void destroyApp(boolean unconditional) {
     if (player != null) {
-      videoControl = null;      
+      videoControl = null;
       try {
         player.stop();
       } catch (MediaException me) {
@@ -132,7 +141,7 @@ public final class ZXingMIDlet extends MIDlet {
   private void showOpenURL(String title, final String display, final String uri) {
     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
     alert.setTimeout(Alert.FOREVER);
-         Command yes = new Command("Yes", Command.OK, 1);
+    Command yes = new Command("Yes", Command.OK, 1);
     alert.addCommand(yes);
     Command no = new Command("No", Command.CANCEL, 1);
     alert.addCommand(no);
@@ -148,7 +157,7 @@ public final class ZXingMIDlet extends MIDlet {
           }
         } else {
           // cancel
-          Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
+          Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
         }
       }
     };
@@ -163,39 +172,47 @@ public final class ZXingMIDlet extends MIDlet {
   }
 
   void showError(Throwable t) {
-         showError(t.getMessage());
+    String message = t.getMessage();
+    if (message != null && message.length() > 0) {
+      showError(message);
+    } else {
+      showError(t.toString());
+    }
   }
 
-       void showError(String message) {
-               showAlert(new Alert("Error", message, null, AlertType.ERROR));
-       }
+  void showError(String message) {
+    showAlert(new Alert("Error", message, null, AlertType.ERROR));
+  }
 
   private void showAlert(Alert alert) {
     Display display = Display.getDisplay(this);
     display.setCurrent(alert, canvas);
   }
 
-  void handleDecodedText(String text) {
-    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
+  void handleDecodedText(Result theResult) {
+    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
     ParsedReaderResultType type = result.getType();
     if (type.equals(ParsedReaderResultType.URI)) {
       String uri = ((URIParsedResult) result).getURI();
-      showOpenURL("Open web page?", uri, uri);
+      showOpenURL("Open Web Page?", uri, uri);
     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
       String uri = ((BookmarkDoCoMoResult) result).getURI();
-      showOpenURL("Open web page?", uri, uri);
+      showOpenURL("Open Web Page?", uri, uri);      
+    } else if (type.equals(ParsedReaderResultType.URLTO)) {
+      String uri = ((URLTOResult) result).getURI();
+      showOpenURL("Open Web Page?", uri, uri);
     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
       String email = ((EmailDoCoMoResult) result).getTo();
-      showOpenURL("Compose e-mail?", email, "mailto:" + email);
+      showOpenURL("Compose E-mail?", email, "mailto:" + email);
     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
       String email = ((EmailAddressResult) result).getEmailAddress();
-      showOpenURL("Compose e-mail?", email, "mailto:" + email);
+      showOpenURL("Compose E-mail?", email, "mailto:" + email);
     } else if (type.equals(ParsedReaderResultType.UPC)) {
-           String upc = ((UPCParsedResult) result).getUPC();
-           String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
-           showOpenURL("Look up UPC?", upc, uri);
+      String upc = ((UPCParsedResult) result).getUPC();
+      String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
+      showOpenURL("Look Up Barcode Online?", upc, uri);
     } else {
-      showAlert("Barcode detected", result.getDisplayResult());
+      showAlert("Barcode Detected", result.getDisplayResult());
     }
   }