Improve handling of MultimediaManager to make it a bit easier to make a 'basic' build
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
1 /*
2  * Copyright 2007 ZXing authors
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.Result;
20 import com.google.zxing.client.result.EmailAddressParsedResult;
21 import com.google.zxing.client.result.ParsedResult;
22 import com.google.zxing.client.result.ParsedResultType;
23 import com.google.zxing.client.result.ResultParser;
24 import com.google.zxing.client.result.SMSParsedResult;
25 import com.google.zxing.client.result.TelParsedResult;
26 import com.google.zxing.client.result.ProductParsedResult;
27 import com.google.zxing.client.result.URIParsedResult;
28
29 import javax.microedition.io.ConnectionNotFoundException;
30 import javax.microedition.lcdui.Alert;
31 import javax.microedition.lcdui.AlertType;
32 import javax.microedition.lcdui.Canvas;
33 import javax.microedition.lcdui.Command;
34 import javax.microedition.lcdui.CommandListener;
35 import javax.microedition.lcdui.Display;
36 import javax.microedition.lcdui.Displayable;
37 import javax.microedition.media.Manager;
38 import javax.microedition.media.MediaException;
39 import javax.microedition.media.Player;
40 import javax.microedition.media.control.VideoControl;
41 import javax.microedition.midlet.MIDlet;
42 import javax.microedition.midlet.MIDletStateChangeException;
43 import java.io.IOException;
44
45 /**
46  * <p>The actual reader application {@link MIDlet}.</p>
47  *
48  * @author Sean Owen
49  */
50 public final class ZXingMIDlet extends MIDlet {
51
52   private static final int ALERT_TIMEOUT_MS = 5 * 1000;
53
54   private Canvas canvas;
55   private Player player;
56   private VideoControl videoControl;
57
58   Displayable getCanvas() {
59     return canvas;
60   }
61
62   Player getPlayer() {
63     return player;
64   }
65
66   VideoControl getVideoControl() {
67     return videoControl;
68   }
69
70   static MultimediaManager buildMultimediaManager() {
71     return new AdvancedMultimediaManager();
72     // Comment line above / uncomment below to make the basic version
73     // return new DefaultMultimediaManager();
74   }
75
76   protected void startApp() throws MIDletStateChangeException {
77     try {
78       player = createPlayer();
79       player.realize();
80       MultimediaManager multimediaManager = buildMultimediaManager();
81       multimediaManager.setZoom(player);
82       multimediaManager.setExposure(player);
83       videoControl = (VideoControl) player.getControl("VideoControl");
84       canvas = new VideoCanvas(this);
85       canvas.setFullScreenMode(true);
86       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
87       videoControl.setDisplayLocation(0, 0);
88       videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
89       videoControl.setVisible(true);
90       player.start();
91       Display.getDisplay(this).setCurrent(canvas);
92     } catch (IOException ioe) {
93       throw new MIDletStateChangeException(ioe.toString());
94     } catch (MediaException me) {
95       throw new MIDletStateChangeException(me.toString());
96     }
97   }
98
99   private static Player createPlayer() throws IOException, MediaException {
100     // Try a workaround for Nokias, which want to use capture://image in some cases
101     Player player = null;
102     String platform = System.getProperty("microedition.platform");
103     if (platform != null && platform.indexOf("Nokia") >= 0) {
104       try {
105         player = Manager.createPlayer("capture://image");
106       } catch (MediaException me) {
107         // if this fails, just continue with capture://video
108       } catch (Error e) {
109         // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
110         // We should still try to continue
111       }
112     }
113     if (player == null) {
114       player = Manager.createPlayer("capture://video");
115     }
116     return player;
117   }
118
119   protected void pauseApp() {
120     if (player != null) {
121       try {
122         player.stop();
123       } catch (MediaException me) {
124         // continue?
125         showError(me);
126       }
127     }
128   }
129
130   protected void destroyApp(boolean unconditional) {
131     if (player != null) {
132       videoControl = null;
133       try {
134         player.stop();
135       } catch (MediaException me) {
136         // continue
137       }
138       player.deallocate();
139       player.close();
140       player = null;
141     }
142   }
143
144   void stop() {
145     destroyApp(false);
146     notifyDestroyed();
147   }
148
149   // Convenience methods to show dialogs
150
151   private void showOpenURL(String title, String display, final String uri) {
152     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
153     alert.setTimeout(ALERT_TIMEOUT_MS);
154     Command yes = new Command("Yes", Command.OK, 1);
155     alert.addCommand(yes);
156     Command no = new Command("No", Command.CANCEL, 1);
157     alert.addCommand(no);
158     CommandListener listener = new CommandListener() {
159       public void commandAction(Command command, Displayable displayable) {
160         if (command.getCommandType() == Command.OK) {
161           try {
162             platformRequest(uri);
163           } catch (ConnectionNotFoundException cnfe) {
164             showError(cnfe);
165           } finally {
166             stop();
167           }
168         } else {
169           // cancel
170           Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
171         }
172       }
173     };
174     alert.setCommandListener(listener);
175     showAlert(alert);
176   }
177
178   private void showAlert(String title, String text) {
179     Alert alert = new Alert(title, text, null, AlertType.INFO);
180     alert.setTimeout(ALERT_TIMEOUT_MS);
181     showAlert(alert);
182   }
183
184   void showError(Throwable t) {
185     String message = t.getMessage();
186     if (message != null && message.length() > 0) {
187       showError(message);
188     } else {
189       showError(t.toString());
190     }
191   }
192
193   void showError(String message) {
194     showAlert(new Alert("Error", message, null, AlertType.ERROR));
195   }
196
197   private void showAlert(Alert alert) {
198     Display display = Display.getDisplay(this);
199     display.setCurrent(alert, canvas);
200   }
201
202   void handleDecodedText(Result theResult) {
203     ParsedResult result = ResultParser.parseResult(theResult);
204     ParsedResultType type = result.getType();
205     if (type.equals(ParsedResultType.URI)) {
206       String uri = ((URIParsedResult) result).getURI();
207       showOpenURL("Open Web Page?", uri, uri);
208     } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
209       EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
210       showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
211     } else if (type.equals(ParsedResultType.SMS)) {
212       SMSParsedResult smsResult = (SMSParsedResult) result;
213       showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
214     } else if (type.equals(ParsedResultType.PRODUCT)) {
215       ProductParsedResult productResult = (ProductParsedResult) result;
216       String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
217       showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
218     } else if (type.equals(ParsedResultType.TEL)) {
219       TelParsedResult telResult = (TelParsedResult) result;
220       showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
221     } else {
222       showAlert("Barcode Detected", result.getDisplayResult());
223     }
224   }
225
226 }