Broke out classes, tried to add "open URL" support. Still some issues.
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.j2me;
18
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;
34
35 /**
36  * <p>The actual reader application {@link MIDlet}.</p>
37  *
38  * @author Sean Owen (srowen@google.com)
39  */
40 public final class ZXingMIDlet extends MIDlet {
41
42   private Player player;
43   private VideoControl videoControl;
44
45   Player getPlayer() {
46     return player;
47   }
48
49   VideoControl getVideoControl() {
50     return videoControl;
51   }
52
53   protected void startApp() throws MIDletStateChangeException {
54     try {
55       player = Manager.createPlayer("capture://video");
56       player.realize();
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);
64       /*
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);
70         }
71         if (focusControl.isMacroSupported()) {
72           focusControl.setMacro(true);
73         }
74       } else {
75         System.out.println("FocusControl not supported");
76       }
77        */
78       Display.getDisplay(this).setCurrent(canvas);
79       player.start();
80     } catch (IOException ioe) {
81       throw new MIDletStateChangeException(ioe.toString());
82     } catch (MediaException me) {
83       throw new MIDletStateChangeException(me.toString());
84     }
85   }
86
87   protected void pauseApp() {
88     if (player != null) {
89       try {
90         player.stop();
91       } catch (MediaException me) {
92         // continue?
93         showError(me);        
94       }
95     }
96   }
97
98   protected void destroyApp(boolean unconditional) {
99     if (player != null) {
100       videoControl = null;      
101       try {
102         player.stop();
103       } catch (MediaException me) {
104         // continue
105       }
106       player.deallocate();
107       player.close();
108       player = null;
109     }
110   }
111
112   void stop() {
113     destroyApp(false);
114     notifyDestroyed();
115   }
116
117   // Convenience methods to show dialogs
118
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)) {
129           try {
130             if (platformRequest(text)) {
131               // Successfully opened URL; exit
132               stop();
133             }
134           } catch (ConnectionNotFoundException cnfe) {
135             showError(cnfe);
136           }
137         }
138       }
139     };
140     alert.setCommandListener(listener);
141     showAlert(alert);
142   }
143
144   void showAlert(String title, String text) {
145     Alert alert = new Alert(title, text, null, AlertType.INFO);
146     alert.setTimeout(Alert.FOREVER);
147     showAlert(alert);
148   }
149
150   void showError(Throwable t) {
151     showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
152   }
153
154   private void showAlert(Alert alert) {
155     Display display = Display.getDisplay(this);
156     display.setCurrent(alert, display.getCurrent());
157   }
158
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);
166     } else {
167       showAlert("Barcode detected", text);
168     }
169   }
170
171   private static boolean maybeURLWithoutScheme(String text) {
172     return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
173   }
174
175 }