Issue 131: reuse Alert objects
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
index 9c9f5a3..f07d813 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package com.google.zxing.client.j2me;
 
-import com.google.zxing.client.result.BookmarkDoCoMoResult;
-import com.google.zxing.client.result.EmailAddressResult;
-import com.google.zxing.client.result.EmailDoCoMoResult;
-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.Result;
+import com.google.zxing.client.result.EmailAddressParsedResult;
+import com.google.zxing.client.result.ParsedResult;
+import com.google.zxing.client.result.ParsedResultType;
+import com.google.zxing.client.result.ResultParser;
+import com.google.zxing.client.result.SMSParsedResult;
+import com.google.zxing.client.result.TelParsedResult;
+import com.google.zxing.client.result.ProductParsedResult;
 import com.google.zxing.client.result.URIParsedResult;
 
 import javax.microedition.io.ConnectionNotFoundException;
@@ -43,13 +45,21 @@ import java.io.IOException;
 /**
  * <p>The actual reader application {@link MIDlet}.</p>
  *
- * @author Sean Owen (srowen@google.com)
+ * @author Sean Owen
  */
 public final class ZXingMIDlet extends MIDlet {
 
+  private static final int ALERT_TIMEOUT_MS = 5 * 1000;
+
   private Canvas canvas;
   private Player player;
   private VideoControl videoControl;
+  private Alert confirmation;
+  private Alert alert;
+
+  Displayable getCanvas() {
+    return canvas;
+  }
 
   Player getPlayer() {
     return player;
@@ -59,11 +69,19 @@ public final class ZXingMIDlet extends MIDlet {
     return videoControl;
   }
 
+  static MultimediaManager buildMultimediaManager() {
+    return new AdvancedMultimediaManager();
+    // Comment line above / uncomment below to make the basic version
+    // return new DefaultMultimediaManager();
+  }
+
   protected void startApp() throws MIDletStateChangeException {
     try {
       player = createPlayer();
       player.realize();
-      AdvancedMultimediaManager.setZoom(player);
+      MultimediaManager multimediaManager = buildMultimediaManager();
+      multimediaManager.setZoom(player);
+      multimediaManager.setExposure(player);
       videoControl = (VideoControl) player.getControl("VideoControl");
       canvas = new VideoCanvas(this);
       canvas.setFullScreenMode(true);
@@ -78,24 +96,38 @@ public final class ZXingMIDlet extends MIDlet {
     } catch (MediaException me) {
       throw new MIDletStateChangeException(me.toString());
     }
+
+    // Set up one confirmation and alert object to re-use
+    confirmation = new Alert(null);
+    confirmation.setType(AlertType.CONFIRMATION);
+    confirmation.setTimeout(ALERT_TIMEOUT_MS);
+    Command yes = new Command("Yes", Command.OK, 1);
+    confirmation.addCommand(yes);
+    Command no = new Command("No", Command.CANCEL, 1);
+    confirmation.addCommand(no);
+    alert = new Alert(null);
+    alert.setTimeout(ALERT_TIMEOUT_MS);
   }
 
-       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 +135,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) {
@@ -129,13 +161,9 @@ public final class ZXingMIDlet extends MIDlet {
 
   // Convenience methods to show dialogs
 
-  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);
-    alert.addCommand(yes);
-    Command no = new Command("No", Command.CANCEL, 1);
-    alert.addCommand(no);
+  private void showOpenURL(String title, String display, final String uri) {
+    confirmation.setTitle(title);
+    confirmation.setString(display);
     CommandListener listener = new CommandListener() {
       public void commandAction(Command command, Displayable displayable) {
         if (command.getCommandType() == Command.OK) {
@@ -148,17 +176,18 @@ public final class ZXingMIDlet extends MIDlet {
           }
         } else {
           // cancel
-          Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
+          Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
         }
       }
     };
-    alert.setCommandListener(listener);
-    showAlert(alert);
+    confirmation.setCommandListener(listener);
+    showAlert(confirmation);
   }
 
   private void showAlert(String title, String text) {
-    Alert alert = new Alert(title, text, null, AlertType.INFO);
-    alert.setTimeout(Alert.FOREVER);
+    alert.setTitle(title);
+    alert.setString(text);
+    alert.setType(AlertType.INFO);
     showAlert(alert);
   }
 
@@ -171,36 +200,39 @@ public final class ZXingMIDlet extends MIDlet {
     }
   }
 
-       void showError(String message) {
-               showAlert(new Alert("Error", message, null, AlertType.ERROR));
-       }
+  void showError(String message) {
+    alert.setTitle("Error");
+    alert.setString(message);
+    alert.setType(AlertType.ERROR);
+    showAlert(alert);
+  }
 
   private void showAlert(Alert alert) {
     Display display = Display.getDisplay(this);
     display.setCurrent(alert, canvas);
   }
 
-  void handleDecodedText(String text) {
-    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
-    ParsedReaderResultType type = result.getType();
-    if (type.equals(ParsedReaderResultType.URI)) {
+  void handleDecodedText(Result theResult) {
+    ParsedResult result = ResultParser.parseResult(theResult);
+    ParsedResultType type = result.getType();
+    if (type.equals(ParsedResultType.URI)) {
       String uri = ((URIParsedResult) result).getURI();
-      showOpenURL("Open web page?", uri, uri);
-    } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
-      String uri = ((BookmarkDoCoMoResult) 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);
-    } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
-      String email = ((EmailAddressResult) result).getEmailAddress();
-      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);
+      showOpenURL("Open Web Page?", uri, uri);
+    } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
+      EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
+      showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
+    } else if (type.equals(ParsedResultType.SMS)) {
+      SMSParsedResult smsResult = (SMSParsedResult) result;
+      showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
+    } else if (type.equals(ParsedResultType.PRODUCT)) {
+      ProductParsedResult productResult = (ProductParsedResult) result;
+      String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
+      showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
+    } else if (type.equals(ParsedResultType.TEL)) {
+      TelParsedResult telResult = (TelParsedResult) result;
+      showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
     } else {
-      showAlert("Barcode detected", result.getDisplayResult());
+      showAlert("Barcode Detected", result.getDisplayResult());
     }
   }