Finally, much better support for auto-focus, other UI fixes. Now requires JSR-234.
[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 javax.microedition.amms.control.camera.ZoomControl;
34 import java.io.IOException;
35
36 /**
37  * <p>The actual reader application {@link MIDlet}.</p>
38  *
39  * @author Sean Owen (srowen@google.com)
40  */
41 public final class ZXingMIDlet extends MIDlet {
42
43   private static final int MAX_ZOOM = 200;
44
45   private Canvas canvas;
46   private Player player;
47   private VideoControl videoControl;
48
49   Player getPlayer() {
50     return player;
51   }
52
53   VideoControl getVideoControl() {
54     return videoControl;
55   }
56
57   protected void startApp() throws MIDletStateChangeException {
58     try {
59       player = Manager.createPlayer("capture://video");
60       player.realize();
61       setZoom(player);
62       videoControl = (VideoControl) player.getControl("VideoControl");
63       canvas = new VideoCanvas(this);
64       canvas.setFullScreenMode(true);
65       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
66       videoControl.setDisplayLocation(0, 0);
67       videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
68       videoControl.setVisible(true);
69       player.start();
70       Display.getDisplay(this).setCurrent(canvas);
71     } catch (IOException ioe) {
72       throw new MIDletStateChangeException(ioe.toString());
73     } catch (MediaException me) {
74       throw new MIDletStateChangeException(me.toString());
75     }
76   }
77
78   private static void setZoom(Player player) {
79     // zoom up to 2x if possible
80     ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
81     if (zoomControl != null) {
82       int maxZoom = zoomControl.getMaxOpticalZoom();
83       if (maxZoom > 100) {
84         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
85       } else {
86         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
87         if (maxDigitalZoom > 100) {
88           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
89         }
90       }
91     }
92   }
93
94   protected void pauseApp() {
95     if (player != null) {
96       try {
97         player.stop();
98       } catch (MediaException me) {
99         // continue?
100         showError(me);        
101       }
102     }
103   }
104
105   protected void destroyApp(boolean unconditional) {
106     if (player != null) {
107       videoControl = null;      
108       try {
109         player.stop();
110       } catch (MediaException me) {
111         // continue
112       }
113       player.deallocate();
114       player.close();
115       player = null;
116     }
117   }
118
119   void stop() {
120     destroyApp(false);
121     notifyDestroyed();
122   }
123
124   // Convenience methods to show dialogs
125
126   void showYesNo(String title, final String text) {
127     Alert alert = new Alert(title, text, null, AlertType.CONFIRMATION);
128     alert.setTimeout(Alert.FOREVER);
129     final Command cancel = new Command("Cancel", Command.CANCEL, 1);
130     alert.addCommand(cancel);
131     CommandListener listener = new CommandListener() {
132       public void commandAction(Command command, Displayable displayable) {
133         if (command.getCommandType() == Command.OK) {
134           try {
135             platformRequest(text);
136           } catch (ConnectionNotFoundException cnfe) {
137             showError(cnfe);
138           } finally {
139             stop();
140           }
141         } else {
142           // cancel
143           Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
144         }
145       }
146     };
147     alert.setCommandListener(listener);
148     showAlert(alert);
149   }
150
151   void showAlert(String title, String text) {
152     Alert alert = new Alert(title, text, null, AlertType.INFO);
153     alert.setTimeout(Alert.FOREVER);
154     showAlert(alert);
155   }
156
157   void showError(Throwable t) {
158     showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
159   }
160
161   private void showAlert(Alert alert) {
162     Display display = Display.getDisplay(this);
163     display.setCurrent(alert, canvas);
164   }
165
166   void handleDecodedText(String text) {
167     // This is a crude imitation of the code found in module core-ext, which handles the contents
168     // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
169     // on URL parsing routines in java.net. This should be somehow worked around: TODO
170     // For now, detect URLs in a simple way, and treat everything else as text
171     if (text.startsWith("http://") || text.startsWith("https://") || maybeURLWithoutScheme(text)) {
172       showYesNo("Open web page?", text);
173     } else {
174       showAlert("Barcode detected", text);
175     }
176   }
177
178   private static boolean maybeURLWithoutScheme(String text) {
179     return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
180   }
181
182 }