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