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