Improve handling of MultimediaManager to make it a bit easier to make a 'basic' build
[zxing.git] / javame / src / com / google / zxing / client / j2me / SnapshotThread.java
index 18b8de4..3411222 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.
@@ -28,54 +28,66 @@ import javax.microedition.media.Player;
 import javax.microedition.media.control.VideoControl;
 
 /**
- * @author Sean Owen (srowen@google.com)
+ * Thread which does the work of capturing a frame and decoding it.
+ *
+ * @author Sean Owen
  */
-final class SnapshotThread extends Thread {
-
-  private static SnapshotThread currentThread;
+final class SnapshotThread implements Runnable {
 
   private final ZXingMIDlet zXingMIDlet;
+  private final Object waitLock;
+  private volatile boolean done;
+  private final MultimediaManager multimediaManager;
 
   SnapshotThread(ZXingMIDlet zXingMIDlet) {
     this.zXingMIDlet = zXingMIDlet;
+    waitLock = new Object();
+    done = false;
+    multimediaManager = ZXingMIDlet.buildMultimediaManager();
   }
 
-  static synchronized void startThread(ZXingMIDlet zXingMIDlet) {
-    if (currentThread == null) {
-      currentThread = new SnapshotThread(zXingMIDlet);
-      currentThread.start();
+  void continueRun() {
+    synchronized (waitLock) {
+      waitLock.notifyAll();
     }
   }
 
-  public void run() {
-    Player player = zXingMIDlet.getPlayer();
-    try {
-      AdvancedMultimediaManager.setFocus(player);
+  private void waitForSignal() {
+    synchronized (waitLock) {
       try {
-        player.stop();
-      } catch (MediaException me) {
+        waitLock.wait();
+      } catch (InterruptedException ie) {
         // continue
       }
-      byte[] snapshot = takeSnapshot();
-      Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
-      MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
-      Reader reader = new MultiFormatReader();
-      Result result = reader.decode(source);
-      zXingMIDlet.handleDecodedText(result.getText());
-    } catch (ReaderException re) {
-      // Show a friendlier message on a mere failure to read the barcode
-      zXingMIDlet.showError("Sorry, no barcode was found.");
-    } catch (Throwable t) {
-      zXingMIDlet.showError(t);
-    } finally {
+    }
+  }
+
+  void stop() {
+    done = true;
+    continueRun();
+  }
+
+  public void run() {
+    Player player = zXingMIDlet.getPlayer();
+    do {
+      waitForSignal();
       try {
-        player.start();
+        multimediaManager.setFocus(player);
+        byte[] snapshot = takeSnapshot();
+        Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
+        MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
+        Reader reader = new MultiFormatReader();
+        Result result = reader.decode(source);
+        zXingMIDlet.handleDecodedText(result);
+      } catch (ReaderException re) {
+        // Show a friendlier message on a mere failure to read the barcode
+        zXingMIDlet.showError("Sorry, no barcode was found.");
       } catch (MediaException me) {
-        // continue
+        zXingMIDlet.showError(me);
+      } catch (RuntimeException re) {
+        zXingMIDlet.showError(re);
       }
-      currentThread = null;
-    }
-
+    } while (!done);
   }
 
   private byte[] takeSnapshot() throws MediaException {