49bcf270e9c195386993a4deb44d222cda410223
[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 com.google.zxing.Result;
20 import com.google.zxing.client.result.BookmarkDoCoMoParsedResult;
21 import com.google.zxing.client.result.EmailAddressParsedResult;
22 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
23 import com.google.zxing.client.result.ParsedReaderResult;
24 import com.google.zxing.client.result.ParsedReaderResultType;
25 import com.google.zxing.client.result.UPCParsedResult;
26 import com.google.zxing.client.result.URIParsedResult;
27 import com.google.zxing.client.result.URLTOParsedResult;
28
29 import javax.microedition.io.ConnectionNotFoundException;
30 import javax.microedition.lcdui.Alert;
31 import javax.microedition.lcdui.AlertType;
32 import javax.microedition.lcdui.Canvas;
33 import javax.microedition.lcdui.Command;
34 import javax.microedition.lcdui.CommandListener;
35 import javax.microedition.lcdui.Display;
36 import javax.microedition.lcdui.Displayable;
37 import javax.microedition.media.Manager;
38 import javax.microedition.media.MediaException;
39 import javax.microedition.media.Player;
40 import javax.microedition.media.control.VideoControl;
41 import javax.microedition.midlet.MIDlet;
42 import javax.microedition.midlet.MIDletStateChangeException;
43 import java.io.IOException;
44
45 /**
46  * <p>The actual reader application {@link MIDlet}.</p>
47  *
48  * @author Sean Owen (srowen@google.com)
49  */
50 public final class ZXingMIDlet extends MIDlet {
51
52   private Canvas canvas;
53   private Player player;
54   private VideoControl videoControl;
55
56   Canvas getCanvas() {
57     return canvas;
58   }
59
60   Player getPlayer() {
61     return player;
62   }
63
64   VideoControl getVideoControl() {
65     return videoControl;
66   }
67
68   protected void startApp() throws MIDletStateChangeException {
69     try {
70       player = createPlayer();
71       player.realize();
72       AdvancedMultimediaManager.setZoom(player);
73       videoControl = (VideoControl) player.getControl("VideoControl");
74       canvas = new VideoCanvas(this);
75       canvas.setFullScreenMode(true);
76       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
77       videoControl.setDisplayLocation(0, 0);
78       videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
79       videoControl.setVisible(true);
80       player.start();
81       Display.getDisplay(this).setCurrent(canvas);
82     } catch (IOException ioe) {
83       throw new MIDletStateChangeException(ioe.toString());
84     } catch (MediaException me) {
85       throw new MIDletStateChangeException(me.toString());
86     }
87   }
88
89   private static Player createPlayer() throws IOException, MediaException {
90     // Try a workaround for Nokias, which want to use capture://image in some cases
91     Player player = null;
92     String platform = System.getProperty("microedition.platform");
93     if (platform != null && platform.indexOf("Nokia") >= 0) {
94       try {
95         player = Manager.createPlayer("capture://image");
96       } catch (MediaException me) {
97         // if this fails, just continue with capture://video
98       } catch (Error e) {
99         // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
100         // We should still try to continue
101       }
102     }
103     if (player == null) {
104       player = Manager.createPlayer("capture://video");
105     }
106     return player;
107   }
108
109   protected void pauseApp() {
110     if (player != null) {
111       try {
112         player.stop();
113       } catch (MediaException me) {
114         // continue?
115         showError(me);
116       }
117     }
118   }
119
120   protected void destroyApp(boolean unconditional) {
121     if (player != null) {
122       videoControl = null;
123       try {
124         player.stop();
125       } catch (MediaException me) {
126         // continue
127       }
128       player.deallocate();
129       player.close();
130       player = null;
131     }
132   }
133
134   void stop() {
135     destroyApp(false);
136     notifyDestroyed();
137   }
138
139   // Convenience methods to show dialogs
140
141   private void showOpenURL(String title, final String display, final String uri) {
142     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
143     alert.setTimeout(Alert.FOREVER);
144     Command yes = new Command("Yes", Command.OK, 1);
145     alert.addCommand(yes);
146     Command no = new Command("No", Command.CANCEL, 1);
147     alert.addCommand(no);
148     CommandListener listener = new CommandListener() {
149       public void commandAction(Command command, Displayable displayable) {
150         if (command.getCommandType() == Command.OK) {
151           try {
152             platformRequest(uri);
153           } catch (ConnectionNotFoundException cnfe) {
154             showError(cnfe);
155           } finally {
156             stop();
157           }
158         } else {
159           // cancel
160           Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
161         }
162       }
163     };
164     alert.setCommandListener(listener);
165     showAlert(alert);
166   }
167
168   private void showAlert(String title, String text) {
169     Alert alert = new Alert(title, text, null, AlertType.INFO);
170     alert.setTimeout(Alert.FOREVER);
171     showAlert(alert);
172   }
173
174   void showError(Throwable t) {
175     String message = t.getMessage();
176     if (message != null && message.length() > 0) {
177       showError(message);
178     } else {
179       showError(t.toString());
180     }
181   }
182
183   void showError(String message) {
184     showAlert(new Alert("Error", message, null, AlertType.ERROR));
185   }
186
187   private void showAlert(Alert alert) {
188     Display display = Display.getDisplay(this);
189     display.setCurrent(alert, canvas);
190   }
191
192   void handleDecodedText(Result theResult) {
193     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
194     ParsedReaderResultType type = result.getType();
195     if (type.equals(ParsedReaderResultType.URI)) {
196       String uri = ((URIParsedResult) result).getURI();
197       showOpenURL("Open Web Page?", uri, uri);
198     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
199       String uri = ((BookmarkDoCoMoParsedResult) result).getURI();
200       showOpenURL("Open Web Page?", uri, uri);      
201     } else if (type.equals(ParsedReaderResultType.URLTO)) {
202       String uri = ((URLTOParsedResult) result).getURI();
203       showOpenURL("Open Web Page?", uri, uri);
204     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
205       String email = ((EmailDoCoMoParsedResult) result).getTo();
206       showOpenURL("Compose E-mail?", email, "mailto:" + email);
207     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
208       String email = ((EmailAddressParsedResult) result).getEmailAddress();
209       showOpenURL("Compose E-mail?", email, "mailto:" + email);
210     //} else if (type.equals(ParsedReaderResultType.GEO)) {
211     //  GeoParsedResult geoResult = (GeoParsedResult) result;
212     //  showOpenURL("Open In Google Maps?", geoResult.getDisplayResult(), geoResult.getGoogleMapsURI());
213     } else if (type.equals(ParsedReaderResultType.UPC)) {
214       String upc = ((UPCParsedResult) result).getUPC();
215       String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
216       showOpenURL("Look Up Barcode Online?", upc, uri);
217     } else {
218       showAlert("Barcode Detected", result.getDisplayResult());
219     }
220   }
221
222 }