2 * Copyright 2007 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.j2me;
19 import javax.microedition.io.ConnectionNotFoundException;
20 import javax.microedition.lcdui.Alert;
21 import javax.microedition.lcdui.AlertType;
22 import javax.microedition.lcdui.Canvas;
23 import javax.microedition.lcdui.Command;
24 import javax.microedition.lcdui.CommandListener;
25 import javax.microedition.lcdui.Display;
26 import javax.microedition.lcdui.Displayable;
27 import javax.microedition.media.Manager;
28 import javax.microedition.media.MediaException;
29 import javax.microedition.media.Player;
30 import javax.microedition.media.control.VideoControl;
31 import javax.microedition.midlet.MIDlet;
32 import javax.microedition.midlet.MIDletStateChangeException;
33 import java.io.IOException;
36 * <p>The actual reader application {@link MIDlet}.</p>
38 * @author Sean Owen (srowen@google.com)
40 public final class ZXingMIDlet extends MIDlet {
42 private Player player;
43 private VideoControl videoControl;
49 VideoControl getVideoControl() {
53 protected void startApp() throws MIDletStateChangeException {
55 player = Manager.createPlayer("capture://video");
57 videoControl = (VideoControl) player.getControl("VideoControl");
58 Canvas canvas = new VideoCanvas(this);
59 canvas.setFullScreenMode(true);
60 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
61 videoControl.setDisplayLocation(0, 0);
62 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
63 videoControl.setVisible(true);
65 FocusControl focusControl = (FocusControl)
66 player.getControl("javax.microedition.amms.control.FocusControl");
67 if (focusControl != null) {
68 if (focusControl.isAutoFocusSupported()) {
69 focusControl.setFocus(FocusControl.AUTO);
71 if (focusControl.isMacroSupported()) {
72 focusControl.setMacro(true);
75 System.out.println("FocusControl not supported");
78 Display.getDisplay(this).setCurrent(canvas);
80 } catch (IOException ioe) {
81 throw new MIDletStateChangeException(ioe.toString());
82 } catch (MediaException me) {
83 throw new MIDletStateChangeException(me.toString());
87 protected void pauseApp() {
91 } catch (MediaException me) {
98 protected void destroyApp(boolean unconditional) {
103 } catch (MediaException me) {
117 // Convenience methods to show dialogs
119 void showYesNo(String title, final String text) {
120 Alert alert = new Alert(title, text, null, AlertType.INFO);
121 alert.setTimeout(Alert.FOREVER);
122 final Command yes = new Command("Yes", Command.OK, 0);
123 final Command no = new Command("No", Command.CANCEL, 0);
124 alert.addCommand(yes);
125 alert.addCommand(no);
126 CommandListener listener = new CommandListener() {
127 public void commandAction(Command command, Displayable displayable) {
128 if (command.equals(yes)) {
130 if (platformRequest(text)) {
131 // Successfully opened URL; exit
134 } catch (ConnectionNotFoundException cnfe) {
140 alert.setCommandListener(listener);
144 void showAlert(String title, String text) {
145 Alert alert = new Alert(title, text, null, AlertType.INFO);
146 alert.setTimeout(Alert.FOREVER);
150 void showError(Throwable t) {
151 showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
154 private void showAlert(Alert alert) {
155 Display display = Display.getDisplay(this);
156 display.setCurrent(alert, display.getCurrent());
159 void handleDecodedText(String text) {
160 // This is a crude imitation of the code found in module core-ext, which handles the contents
161 // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
162 // on URL parsing routines in java.net. This should be somehow worked around: TODO
163 // For now, detect URLs in a simple way, and treat everything else as text
164 if (text.startsWith("http://") || text.startsWith("https://") || maybeURLWithoutScheme(text)) {
165 showYesNo("Open URL?", text);
167 showAlert("Barcode detected", text);
171 private static boolean maybeURLWithoutScheme(String text) {
172 return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;