Explictly add Yes/No commands to "Open xxx" dialog to ensure that both options show...
[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           Command yes = new Command("Yes", Command.OK, 1);
118     alert.addCommand(yes);
119     Command no = new Command("No", Command.CANCEL, 1);
120     alert.addCommand(no);
121     CommandListener listener = new CommandListener() {
122       public void commandAction(Command command, Displayable displayable) {
123         if (command.getCommandType() == Command.OK) {
124           try {
125             platformRequest(uri);
126           } catch (ConnectionNotFoundException cnfe) {
127             showError(cnfe);
128           } finally {
129             stop();
130           }
131         } else {
132           // cancel
133           Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
134         }
135       }
136     };
137     alert.setCommandListener(listener);
138     showAlert(alert);
139   }
140
141   private void showAlert(String title, String text) {
142     Alert alert = new Alert(title, text, null, AlertType.INFO);
143     alert.setTimeout(Alert.FOREVER);
144     showAlert(alert);
145   }
146
147   void showError(Throwable t) {
148           showError(t.getMessage());
149   }
150
151         void showError(String message) {
152                 showAlert(new Alert("Error", message, null, AlertType.ERROR));
153         }
154
155   private void showAlert(Alert alert) {
156     Display display = Display.getDisplay(this);
157     display.setCurrent(alert, canvas);
158   }
159
160   void handleDecodedText(String text) {
161     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
162     ParsedReaderResultType type = result.getType();
163     if (type.equals(ParsedReaderResultType.URI)) {
164       String uri = ((URIParsedResult) result).getURI();
165       showOpenURL("Open web page?", uri, uri);
166     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
167       String uri = ((BookmarkDoCoMoResult) result).getURI();
168       showOpenURL("Open web page?", uri, uri);
169     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
170       String email = ((EmailDoCoMoResult) result).getTo();
171       showOpenURL("Compose e-mail?", email, "mailto:" + email);
172     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
173       String email = ((EmailAddressResult) result).getEmailAddress();
174       showOpenURL("Compose e-mail?", email, "mailto:" + email);
175     } else {
176       showAlert("Barcode detected", result.getDisplayResult());
177     }
178   }
179
180 }