b4ebb8e3b6e22afe0954ed22a6fc10277168ba20
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
1 /*
2  * Copyright 2007 ZXing authors
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.EmailAddressParsedResult;
21 import com.google.zxing.client.result.ParsedResult;
22 import com.google.zxing.client.result.ParsedResultType;
23 import com.google.zxing.client.result.ResultParser;
24 import com.google.zxing.client.result.SMSParsedResult;
25 import com.google.zxing.client.result.TelParsedResult;
26 import com.google.zxing.client.result.ProductParsedResult;
27 import com.google.zxing.client.result.URIParsedResult;
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
49  */
50 public final class ZXingMIDlet extends MIDlet {
51
52   private static final int ALERT_TIMEOUT_MS = 5 * 1000;
53
54   private Canvas canvas;
55   private Player player;
56   private VideoControl videoControl;
57
58   Displayable getCanvas() {
59     return canvas;
60   }
61
62   Player getPlayer() {
63     return player;
64   }
65
66   VideoControl getVideoControl() {
67     return videoControl;
68   }
69
70   protected void startApp() throws MIDletStateChangeException {
71     try {
72       player = createPlayer();
73       player.realize();
74       MultimediaManager multimediaManager = new AdvancedMultimediaManager();
75       // Comment line above / uncomment below to make the basic version
76       //MultimediaManager multimediaManager = new DefaultMultimediaManager();
77       multimediaManager.setZoom(player);
78       multimediaManager.setExposure(player);
79       videoControl = (VideoControl) player.getControl("VideoControl");
80       canvas = new VideoCanvas(this);
81       canvas.setFullScreenMode(true);
82       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
83       videoControl.setDisplayLocation(0, 0);
84       videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
85       videoControl.setVisible(true);
86       player.start();
87       Display.getDisplay(this).setCurrent(canvas);
88     } catch (IOException ioe) {
89       throw new MIDletStateChangeException(ioe.toString());
90     } catch (MediaException me) {
91       throw new MIDletStateChangeException(me.toString());
92     }
93   }
94
95   private static Player createPlayer() throws IOException, MediaException {
96     // Try a workaround for Nokias, which want to use capture://image in some cases
97     Player player = null;
98     String platform = System.getProperty("microedition.platform");
99     if (platform != null && platform.indexOf("Nokia") >= 0) {
100       try {
101         player = Manager.createPlayer("capture://image");
102       } catch (MediaException me) {
103         // if this fails, just continue with capture://video
104       } catch (Error e) {
105         // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
106         // We should still try to continue
107       }
108     }
109     if (player == null) {
110       player = Manager.createPlayer("capture://video");
111     }
112     return player;
113   }
114
115   protected void pauseApp() {
116     if (player != null) {
117       try {
118         player.stop();
119       } catch (MediaException me) {
120         // continue?
121         showError(me);
122       }
123     }
124   }
125
126   protected void destroyApp(boolean unconditional) {
127     if (player != null) {
128       videoControl = null;
129       try {
130         player.stop();
131       } catch (MediaException me) {
132         // continue
133       }
134       player.deallocate();
135       player.close();
136       player = null;
137     }
138   }
139
140   void stop() {
141     destroyApp(false);
142     notifyDestroyed();
143   }
144
145   // Convenience methods to show dialogs
146
147   private void showOpenURL(String title, String display, final String uri) {
148     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
149     alert.setTimeout(ALERT_TIMEOUT_MS);
150     Command yes = new Command("Yes", Command.OK, 1);
151     alert.addCommand(yes);
152     Command no = new Command("No", Command.CANCEL, 1);
153     alert.addCommand(no);
154     CommandListener listener = new CommandListener() {
155       public void commandAction(Command command, Displayable displayable) {
156         if (command.getCommandType() == Command.OK) {
157           try {
158             platformRequest(uri);
159           } catch (ConnectionNotFoundException cnfe) {
160             showError(cnfe);
161           } finally {
162             stop();
163           }
164         } else {
165           // cancel
166           Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
167         }
168       }
169     };
170     alert.setCommandListener(listener);
171     showAlert(alert);
172   }
173
174   private void showAlert(String title, String text) {
175     Alert alert = new Alert(title, text, null, AlertType.INFO);
176     alert.setTimeout(ALERT_TIMEOUT_MS);
177     showAlert(alert);
178   }
179
180   void showError(Throwable t) {
181     String message = t.getMessage();
182     if (message != null && message.length() > 0) {
183       showError(message);
184     } else {
185       showError(t.toString());
186     }
187   }
188
189   void showError(String message) {
190     showAlert(new Alert("Error", message, null, AlertType.ERROR));
191   }
192
193   private void showAlert(Alert alert) {
194     Display display = Display.getDisplay(this);
195     display.setCurrent(alert, canvas);
196   }
197
198   void handleDecodedText(Result theResult) {
199     ParsedResult result = ResultParser.parseResult(theResult);
200     ParsedResultType type = result.getType();
201     if (type.equals(ParsedResultType.URI)) {
202       String uri = ((URIParsedResult) result).getURI();
203       showOpenURL("Open Web Page?", uri, uri);
204     } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
205       EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
206       showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
207     } else if (type.equals(ParsedResultType.SMS)) {
208       SMSParsedResult smsResult = (SMSParsedResult) result;
209       showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
210     } else if (type.equals(ParsedResultType.PRODUCT)) {
211       ProductParsedResult productResult = (ProductParsedResult) result;
212       String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
213       showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
214     } else if (type.equals(ParsedResultType.TEL)) {
215       TelParsedResult telResult = (TelParsedResult) result;
216       showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
217     } else {
218       showAlert("Barcode Detected", result.getDisplayResult());
219     }
220   }
221
222 }