Commit Simon's changes for Issue 134

git-svn-id: https://zxing.googlecode.com/svn/trunk@838 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-02-01 11:44:56 +00:00
parent db61519508
commit 270edc5785
3 changed files with 450 additions and 316 deletions

View file

@ -0,0 +1,96 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.j2me;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
/**
* The Menu form simply adds Command Listener functionality
* to the standard List User Interface component.
*
* @author Simon Flannery (simon.flannery@gmail.com)
*/
final class Menu extends List implements CommandListener {
private final ZXingMIDlet zXingMIDlet;
private Command cancelCommand;
private Command barcodeCommand;
/**
* Creates a new Search List and initialises all components.
*
* @param parent The Parent ZXing MIDlet.
* @param title The title of the List.
* @param item The caption of the item action Command.
*/
Menu(ZXingMIDlet parent, String title, String item) {
super(title, IMPLICIT); // Set the title of the form
zXingMIDlet = parent;
// Build the UI components
cancelCommand = new Command("Cancel", Command.CANCEL, 0);
barcodeCommand = new Command(item, Command.ITEM, 0);
addCommand(cancelCommand);
addCommand(barcodeCommand);
setCommandListener(this);
}
/**
* A convenience method for getting the selected option item.
*
* @return The selected option represented as a String. If no option is
* selected, then the empty string is returned.
*/
public String getSelectedString() {
String result = "";
if (getSelectedIndex() != -1) {
result = getString(getSelectedIndex());
}
return result;
}
/**
* A convenience method for removing all items from the list.
* While the size of the list does not equal zero, the first item of the list is deleted.
*/
public void clear() {
while (size() != 0) { // Delete the first-most element until there is no first-most element
delete(0);
}
}
/**
* CommandListener Required Implementation for capturing soft key presses.
* This is where all call back methods (of the MIDlet) are serviced.
*
* @param command The command requiring attention.
* @param displayable The Display.
*/
public void commandAction(Command command, Displayable displayable) {
if (command == cancelCommand) { /* Detecting the soft key press. */
Display.getDisplay(zXingMIDlet).setCurrent(zXingMIDlet.getCanvas());
} else if (command == barcodeCommand || command == SELECT_COMMAND) {
if (getSelectedIndex() != -1) {
zXingMIDlet.itemRequest();
}
}
}
}

View file

@ -27,10 +27,12 @@ import javax.microedition.lcdui.Graphics;
* This class manages decoding via {@link SnapshotThread}.
*
* @author Sean Owen
* @author Simon Flannery
*/
final class VideoCanvas extends Canvas implements CommandListener {
private static final Command exit = new Command("Exit", Command.EXIT, 1);
private static final Command history = new Command("History", Command.ITEM, 0);
private final ZXingMIDlet zXingMIDlet;
private final SnapshotThread snapshotThread;
@ -38,6 +40,7 @@ final class VideoCanvas extends Canvas implements CommandListener {
VideoCanvas(ZXingMIDlet zXingMIDlet) {
this.zXingMIDlet = zXingMIDlet;
addCommand(exit);
addCommand(history);
setCommandListener(this);
snapshotThread = new SnapshotThread(zXingMIDlet);
new Thread(snapshotThread).start();
@ -58,7 +61,9 @@ final class VideoCanvas extends Canvas implements CommandListener {
public void commandAction(Command command, Displayable displayable) {
int type = command.getCommandType();
if (type == Command.EXIT || type == Command.STOP || type == Command.BACK || type == Command.CANCEL) {
if (command == history) {
zXingMIDlet.historyRequest();
} else if (type == Command.EXIT || type == Command.STOP || type == Command.BACK || type == Command.CANCEL) {
snapshotThread.stop();
zXingMIDlet.stop();
}

View file

@ -42,11 +42,13 @@ import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import java.io.IOException;
import java.util.Vector;
/**
* <p>The actual reader application {@link MIDlet}.</p>
*
* @author Sean Owen
* @author Simon Flannery
*/
public final class ZXingMIDlet extends MIDlet {
@ -57,6 +59,8 @@ public final class ZXingMIDlet extends MIDlet {
private VideoControl videoControl;
private Alert confirmation;
private Alert alert;
private Menu history;
private Vector resultHistory;
Displayable getCanvas() {
return canvas;
@ -79,8 +83,12 @@ public final class ZXingMIDlet extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
try {
Image image = Image.createImage("/res/zxing-icon.png");
SplashThread splash = new SplashThread(this, 2000, image);
Displayable splash = new SplashThread(this, 2000, image);
Display.getDisplay(this).setCurrent(splash);
resultHistory = new Vector(5);
history = new Menu(this, "Scan History", "Use");
player = createPlayer();
player.realize();
MultimediaManager multimediaManager = buildMultimediaManager();
@ -115,7 +123,7 @@ public final class ZXingMIDlet extends MIDlet {
videoControl.setVisible(true);
player.start();
} catch (MediaException me) {
// continue
showError(me);
}
Display.getDisplay(this).setCurrent(canvas);
}
@ -170,6 +178,10 @@ public final class ZXingMIDlet extends MIDlet {
notifyDestroyed();
}
void historyRequest() {
Display.getDisplay(this).setCurrent(history);
}
// Convenience methods to show dialogs
private void showOpenURL(String title, String display, final String uri) {
@ -223,8 +235,7 @@ public final class ZXingMIDlet extends MIDlet {
display.setCurrent(alert, canvas);
}
void handleDecodedText(Result theResult) {
ParsedResult result = ResultParser.parseResult(theResult);
void barcodeAction(ParsedResult result) {
ParsedResultType type = result.getType();
if (type.equals(ParsedResultType.URI)) {
String uri = ((URIParsedResult) result).getURI();
@ -247,4 +258,26 @@ public final class ZXingMIDlet extends MIDlet {
}
}
void itemRequest() {
ParsedResult result = (ParsedResult) resultHistory.elementAt(history.getSelectedIndex());
barcodeAction(result);
}
void handleDecodedText(Result theResult) {
ParsedResult result = ResultParser.parseResult(theResult);
String resultString = result.toString();
int i = 0;
while (i < resultHistory.size()) {
if (resultString.equals(resultHistory.elementAt(i).toString())) {
break;
}
i++;
}
if (i == resultHistory.size()) {
resultHistory.addElement(result);
history.append(result.getDisplayResult(), null);
}
barcodeAction(result);
}
}