Issue 131: reuse Alert objects
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 20 Jan 2009 23:41:10 +0000 (23:41 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 20 Jan 2009 23:41:10 +0000 (23:41 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@829 59b500cc-1b3d-0410-9834-0bbf25fbcc57

javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java

index 7e8c2bc..f07d813 100644 (file)
@@ -54,6 +54,8 @@ public final class ZXingMIDlet extends MIDlet {
   private Canvas canvas;
   private Player player;
   private VideoControl videoControl;
+  private Alert confirmation;
+  private Alert alert;
 
   Displayable getCanvas() {
     return canvas;
@@ -94,6 +96,17 @@ 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 {
@@ -149,12 +162,8 @@ public final class ZXingMIDlet extends MIDlet {
   // Convenience methods to show dialogs
 
   private void showOpenURL(String title, String display, final String uri) {
-    Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
-    alert.setTimeout(ALERT_TIMEOUT_MS);
-    Command yes = new Command("Yes", Command.OK, 1);
-    alert.addCommand(yes);
-    Command no = new Command("No", Command.CANCEL, 1);
-    alert.addCommand(no);
+    confirmation.setTitle(title);
+    confirmation.setString(display);
     CommandListener listener = new CommandListener() {
       public void commandAction(Command command, Displayable displayable) {
         if (command.getCommandType() == Command.OK) {
@@ -171,13 +180,14 @@ public final class ZXingMIDlet extends MIDlet {
         }
       }
     };
-    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_TIMEOUT_MS);
+    alert.setTitle(title);
+    alert.setString(text);
+    alert.setType(AlertType.INFO);
     showAlert(alert);
   }
 
@@ -191,7 +201,10 @@ public final class ZXingMIDlet extends MIDlet {
   }
 
   void showError(String message) {
-    showAlert(new Alert("Error", message, null, AlertType.ERROR));
+    alert.setTitle("Error");
+    alert.setString(message);
+    alert.setType(AlertType.ERROR);
+    showAlert(alert);
   }
 
   private void showAlert(Alert alert) {