001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.obex.demo;
033:
034: import javax.microedition.lcdui.Alert;
035: import javax.microedition.lcdui.Command;
036: import javax.microedition.lcdui.CommandListener;
037: import javax.microedition.lcdui.Display;
038: import javax.microedition.lcdui.Displayable;
039: import javax.microedition.lcdui.Form;
040: import javax.microedition.lcdui.Gauge;
041: import javax.microedition.lcdui.Image;
042: import javax.microedition.lcdui.ImageItem;
043: import javax.microedition.lcdui.Item;
044: import javax.microedition.lcdui.StringItem;
045:
046: /**
047: * @version
048: */
049: final class GUIImageReceiver implements CommandListener {
050: /** Shows if debug prints should be done. */
051: private static final boolean DEBUG = false;
052:
053: /** DEBUG only: keeps the class name for debug. */
054: private static final String cn = "GUIImageReceiver";
055:
056: /** Keeps a reference to a parent MIDlet. */
057: private ObexDemoMIDlet parent;
058:
059: /** Soft button for stopping download and returning to the mane screen. */
060: private Command stopCommand = new Command("Stop", Command.BACK, 1);
061:
062: /** Soft button for returning to the main screen of demo. */
063: private Command backCommand = new Command("Back", Command.STOP, 1);
064:
065: /** Soft button for confirm Image downloading. */
066: private Command confirmCommand = new Command("Yes", Command.OK, 1);
067:
068: /** Soft button for reject Image downloading. */
069: private Command rejectCommand = new Command("No", Command.CANCEL, 1);
070:
071: /** StringItem shows string about connection waiting state. */
072: private StringItem waitingItem = new StringItem(
073: "Waiting for connection", "");
074:
075: /** ImageItem shows image on the screen */
076: private ImageItem imageItem = new ImageItem("Image ...", null,
077: Item.LAYOUT_CENTER, null);
078:
079: /** Gauge to indicate progress of downloading process. */
080: private Gauge downloadingItem = new Gauge("Downloading Image ...",
081: false, 256, 0);
082:
083: /** Form to show current state of receiver and to show a picture. */
084: private Form receiverForm = new Form("Receive Image");
085:
086: /** synchronization object to prevent downloading during permission alert.*/
087: private Object alertLock = new Object();
088:
089: /** Indicate that user choose to download image or not. */
090: private boolean download = false;
091:
092: /** Indicate that user has made a choose. */
093: private boolean choose = false;
094:
095: /** Reference to Obex receiver part */
096: private ObexImageReceiver obexReceiver = null;
097:
098: /** Constructor initialize receiverForm to show states of receiving. */
099: GUIImageReceiver(ObexDemoMIDlet parent) {
100: this .parent = parent;
101: obexReceiver = new ObexImageReceiver(this );
102: waitingItem.setLayout(Item.LAYOUT_CENTER);
103: receiverForm.append(waitingItem);
104: receiverForm.addCommand(backCommand);
105: receiverForm.setCommandListener(this );
106: Display.getDisplay(parent).setCurrent(receiverForm);
107: (new Thread(obexReceiver)).start();
108: }
109:
110: /**
111: * Responds to commands used on this displayable.
112: *
113: * @param c command object source of action.
114: * @param d screen object containing the item the action was performed on.
115: */
116: public void commandAction(Command c, Displayable d) {
117: if (c == stopCommand) {
118: obexReceiver.stop(false);
119:
120: return;
121: } else if (c == backCommand) {
122: obexReceiver.stop(true);
123: parent.show();
124:
125: return;
126: } else if (c == confirmCommand) {
127: synchronized (alertLock) {
128: download = true;
129: choose = true;
130: alertLock.notify();
131: }
132: } else if (c == rejectCommand) {
133: synchronized (alertLock) {
134: download = false;
135: choose = true;
136: alertLock.notify();
137: }
138: }
139: }
140:
141: /** Ascs user to download image. */
142: boolean askPermission(String imageName, int imageLength) {
143: boolean download = false;
144:
145: // ask permission to download image
146: Alert alert = new Alert("Connected!",
147: "Incoming image:\nName = " + imageName
148: + "\nLength = " + imageLength
149: + "\nWould you like to receive it ?", null,
150: null);
151: alert.setTimeout(Alert.FOREVER);
152: alert.addCommand(confirmCommand);
153: alert.addCommand(rejectCommand);
154: alert.setCommandListener(this );
155: Display.getDisplay(parent).setCurrent(alert);
156:
157: synchronized (alertLock) {
158: if (choose == false) {
159: try {
160: alertLock.wait();
161: } catch (InterruptedException ie) {
162: // don't wait then
163: }
164: }
165:
166: choose = false;
167: download = this .download;
168: }
169:
170: Display.getDisplay(parent).setCurrent(receiverForm);
171:
172: return download;
173: }
174:
175: /**
176: * Shows string with waiting for connection message.
177: */
178: void showWaiting() {
179: receiverForm.set(0, waitingItem);
180: receiverForm.removeCommand(stopCommand);
181: receiverForm.addCommand(backCommand);
182: downloadingItem.setValue(0);
183: }
184:
185: /**
186: * Shows progress bar of image downloading
187: */
188: void showProgress(int maxValue) {
189: downloadingItem.setValue(0);
190: downloadingItem.setMaxValue(maxValue);
191: receiverForm.set(0, downloadingItem);
192: receiverForm.removeCommand(backCommand);
193: receiverForm.addCommand(stopCommand);
194: }
195:
196: /**
197: * Update progress of image downloading
198: */
199: void updateProgress(int value) {
200: downloadingItem.setValue(value);
201: }
202:
203: /**
204: * Shows downloaded image.
205: */
206: void showImage(byte[] imageData) {
207: Image image = Image.createImage(imageData, 0, imageData.length);
208: imageItem.setImage(image);
209: receiverForm.set(0, imageItem);
210: receiverForm.removeCommand(stopCommand);
211: receiverForm.addCommand(backCommand);
212: }
213:
214: /**
215: * Shows alert with "not ready" message.
216: */
217: void canNotConnectMessage() {
218: Alert alert = new Alert("Warning",
219: "Can not connect to any sender", null, null);
220: alert.setTimeout(5000);
221: Display.getDisplay(parent).setCurrent(alert, receiverForm);
222: }
223:
224: /**
225: * Shows alert with "stop" message.
226: */
227: void stopMessage() {
228: Alert alert = new Alert("Warning",
229: "Sender stopped image uploading", null, null);
230: alert.setTimeout(5000);
231: Display.getDisplay(parent).setCurrent(alert, receiverForm);
232: }
233:
234: /**
235: * Stops receiving process in OBEX part.
236: */
237: void stop() {
238: obexReceiver.stop(true);
239: }
240: }
|