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