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