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