Implemented possible workaround for Nokias that want to use capture://image and may...
[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 = createPlayer();
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         private static Player createPlayer() throws IOException, MediaException {
83                 // Try a workaround for Nokias, which want to use capture://image in some cases
84                 Player player = null;
85                 String platform = System.getProperty("microedition.platform");
86                 if (platform != null && platform.indexOf("Nokia") >= 0) {
87                         try {
88                                 player = Manager.createPlayer("capture://image");
89                         } catch (MediaException me) {
90                                 // if this fails, just continue with capture://video
91                         }
92                 }
93                 if (player == null) {
94                         player = Manager.createPlayer("capture://video");
95                 }
96                 return player;
97         }
98
99   protected void pauseApp() {
100     if (player != null) {
101       try {
102         player.stop();
103       } catch (MediaException me) {
104         // continue?
105         showError(me);        
106       }
107     }
108   }
109
110   protected void destroyApp(boolean unconditional) {
111     if (player != null) {
112       videoControl = null;      
113       try {
114         player.stop();
115       } catch (MediaException me) {
116         // continue
117       }
118       player.deallocate();
119       player.close();
120       player = null;
121     }
122   }
123
124   void stop() {
125     destroyApp(false);
126     notifyDestroyed();
127   }
128
129   // Convenience methods to show dialogs
130
131   private void showOpenURL(String title, final String display, final String uri) {
132     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
133     alert.setTimeout(Alert.FOREVER);
134           Command yes = new Command("Yes", Command.OK, 1);
135     alert.addCommand(yes);
136     Command no = new Command("No", Command.CANCEL, 1);
137     alert.addCommand(no);
138     CommandListener listener = new CommandListener() {
139       public void commandAction(Command command, Displayable displayable) {
140         if (command.getCommandType() == Command.OK) {
141           try {
142             platformRequest(uri);
143           } catch (ConnectionNotFoundException cnfe) {
144             showError(cnfe);
145           } finally {
146             stop();
147           }
148         } else {
149           // cancel
150           Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
151         }
152       }
153     };
154     alert.setCommandListener(listener);
155     showAlert(alert);
156   }
157
158   private void showAlert(String title, String text) {
159     Alert alert = new Alert(title, text, null, AlertType.INFO);
160     alert.setTimeout(Alert.FOREVER);
161     showAlert(alert);
162   }
163
164   void showError(Throwable t) {
165           showError(t.getMessage());
166   }
167
168         void showError(String message) {
169                 showAlert(new Alert("Error", message, null, AlertType.ERROR));
170         }
171
172   private void showAlert(Alert alert) {
173     Display display = Display.getDisplay(this);
174     display.setCurrent(alert, canvas);
175   }
176
177   void handleDecodedText(String text) {
178     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
179     ParsedReaderResultType type = result.getType();
180     if (type.equals(ParsedReaderResultType.URI)) {
181       String uri = ((URIParsedResult) result).getURI();
182       showOpenURL("Open web page?", uri, uri);
183     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
184       String uri = ((BookmarkDoCoMoResult) result).getURI();
185       showOpenURL("Open web page?", uri, uri);
186     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
187       String email = ((EmailDoCoMoResult) result).getTo();
188       showOpenURL("Compose e-mail?", email, "mailto:" + email);
189     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
190       String email = ((EmailAddressResult) result).getEmailAddress();
191       showOpenURL("Compose e-mail?", email, "mailto:" + email);
192     } else {
193       showAlert("Barcode detected", result.getDisplayResult());
194     }
195   }
196
197 }