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