Catch weird Error 136 from Nokia N70 and continue when opening capture://image
[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                         } catch (Error e) {
93         // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
94         // We should still try to continue
95       }
96                 }
97                 if (player == null) {
98                         player = Manager.createPlayer("capture://video");
99                 }
100                 return player;
101         }
102
103   protected void pauseApp() {
104     if (player != null) {
105       try {
106         player.stop();
107       } catch (MediaException me) {
108         // continue?
109         showError(me);        
110       }
111     }
112   }
113
114   protected void destroyApp(boolean unconditional) {
115     if (player != null) {
116       videoControl = null;      
117       try {
118         player.stop();
119       } catch (MediaException me) {
120         // continue
121       }
122       player.deallocate();
123       player.close();
124       player = null;
125     }
126   }
127
128   void stop() {
129     destroyApp(false);
130     notifyDestroyed();
131   }
132
133   // Convenience methods to show dialogs
134
135   private void showOpenURL(String title, final String display, final String uri) {
136     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
137     alert.setTimeout(Alert.FOREVER);
138           Command yes = new Command("Yes", Command.OK, 1);
139     alert.addCommand(yes);
140     Command no = new Command("No", Command.CANCEL, 1);
141     alert.addCommand(no);
142     CommandListener listener = new CommandListener() {
143       public void commandAction(Command command, Displayable displayable) {
144         if (command.getCommandType() == Command.OK) {
145           try {
146             platformRequest(uri);
147           } catch (ConnectionNotFoundException cnfe) {
148             showError(cnfe);
149           } finally {
150             stop();
151           }
152         } else {
153           // cancel
154           Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
155         }
156       }
157     };
158     alert.setCommandListener(listener);
159     showAlert(alert);
160   }
161
162   private void showAlert(String title, String text) {
163     Alert alert = new Alert(title, text, null, AlertType.INFO);
164     alert.setTimeout(Alert.FOREVER);
165     showAlert(alert);
166   }
167
168   void showError(Throwable t) {
169     String message = t.getMessage();
170     if (message != null && message.length() > 0) {
171       showError(message);
172     } else {
173       showError(t.toString());
174     }
175   }
176
177         void showError(String message) {
178                 showAlert(new Alert("Error", message, null, AlertType.ERROR));
179         }
180
181   private void showAlert(Alert alert) {
182     Display display = Display.getDisplay(this);
183     display.setCurrent(alert, canvas);
184   }
185
186   void handleDecodedText(String text) {
187     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
188     ParsedReaderResultType type = result.getType();
189     if (type.equals(ParsedReaderResultType.URI)) {
190       String uri = ((URIParsedResult) result).getURI();
191       showOpenURL("Open Web Page?", uri, uri);
192     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
193       String uri = ((BookmarkDoCoMoResult) result).getURI();
194       showOpenURL("Open Web Page?", uri, uri);
195     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
196       String email = ((EmailDoCoMoResult) result).getTo();
197       showOpenURL("Compose E-mail?", email, "mailto:" + email);
198     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
199       String email = ((EmailAddressResult) result).getEmailAddress();
200       showOpenURL("Compose E-mail?", email, "mailto:" + email);
201     } else if (type.equals(ParsedReaderResultType.UPC)) {
202             String upc = ((UPCParsedResult) result).getUPC();
203             String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
204             showOpenURL("Look Up Barcode Online?", upc, uri);
205     } else {
206       showAlert("Barcode Detected", result.getDisplayResult());
207     }
208   }
209
210 }