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.mms;
033:
034: import java.io.IOException;
035:
036: import javax.microedition.io.*;
037: import javax.microedition.lcdui.*;
038: import javax.microedition.midlet.*;
039:
040: import javax.wireless.messaging.*;
041:
042: /**
043: * An example MIDlet displays text from an MMS MessageConnection
044: */
045: public class MMSReceive extends MIDlet implements CommandListener,
046: Runnable, MessageListener {
047: /** user interface command for indicating Exit request. */
048: private static final Command CMD_EXIT = new Command("Exit",
049: Command.EXIT, 2);
050:
051: /** user interface text box for the contents of the fetched URL. */
052: private Form content;
053:
054: /** current display. */
055: private Display display;
056:
057: /** instance of a thread for asynchronous networking and user interface. */
058: private Thread thread;
059:
060: /** Connections detected at start up. */
061: private String[] connections;
062:
063: /** Flag to signal end of processing. */
064: private boolean done;
065:
066: /** The applicationID on which we listen for MMS messages */
067: private String appID;
068:
069: /** MMS message connection for inbound text messages. */
070: private MessageConnection mmsconn;
071:
072: /** Current message read from the network. */
073: private Message msg;
074:
075: /** Address of the message's sender */
076: private String senderAddress;
077:
078: /** Alert that is displayed when replying */
079: private Alert sendingMessageAlert;
080:
081: /** The screen to display when we return from being paused */
082: private Displayable resumeScreen;
083:
084: /** The subject of the message received */
085: private String subject;
086:
087: /** The text of the received message */
088: private String contents;
089:
090: /**
091: * Initialize the MIDlet with the current display object and
092: * graphical components.
093: */
094: public MMSReceive() {
095: appID = getAppProperty("MMS-ApplicationID");
096:
097: display = Display.getDisplay(this );
098:
099: content = new Form("MMS Receive");
100: content.addCommand(CMD_EXIT);
101: content.setCommandListener(this );
102: content.append("Receiving...");
103:
104: sendingMessageAlert = new Alert("MMS", null, null,
105: AlertType.INFO);
106: sendingMessageAlert.setTimeout(5000);
107: sendingMessageAlert.setCommandListener(this );
108:
109: resumeScreen = content;
110: }
111:
112: /**
113: * Start creates the thread to do the MessageConnection receive
114: * text.
115: * It should return immediately to keep the dispatcher
116: * from hanging.
117: */
118: public void startApp() {
119: /** MMS connection to be read. */
120: String mmsConnection = "mms://:" + appID;
121:
122: /** Open the message connection. */
123: if (mmsconn == null) {
124: try {
125: mmsconn = (MessageConnection) Connector
126: .open(mmsConnection);
127: mmsconn.setMessageListener(this );
128: } catch (IOException ioe) {
129: ioe.printStackTrace();
130: }
131: }
132:
133: /** Initialize the text if we were started manually. */
134: connections = PushRegistry.listConnections(true);
135:
136: if ((connections == null) || (connections.length == 0)) {
137: content.deleteAll();
138: content.append("Waiting for MMS on applicationID " + appID
139: + "...");
140: }
141:
142: done = false;
143: thread = new Thread(this );
144: thread.start();
145:
146: display.setCurrent(resumeScreen);
147: }
148:
149: /**
150: * Notification that a message arrived.
151: * @param conn the connection with messages available
152: */
153: public void notifyIncomingMessage(MessageConnection conn) {
154: if ((thread == null) && !done) {
155: thread = new Thread(this );
156: thread.start();
157: }
158: }
159:
160: /** Message reading thread. */
161: public void run() {
162: /** Check for mms connection. */
163: try {
164: msg = mmsconn.receive();
165:
166: if (msg != null) {
167: senderAddress = msg.getAddress();
168: content.deleteAll();
169:
170: String titleStr = senderAddress.substring(6);
171: int colonPos = titleStr.indexOf(":");
172:
173: if (colonPos != -1) {
174: titleStr = titleStr.substring(0, colonPos);
175: }
176:
177: content.setTitle("From: " + titleStr);
178:
179: if (msg instanceof MultipartMessage) {
180: MultipartMessage mpm = (MultipartMessage) msg;
181: StringBuffer buff = new StringBuffer("Subject: ");
182: buff.append((subject = mpm.getSubject()));
183: buff.append("\nDate: ");
184: buff.append(mpm.getTimestamp().toString());
185: buff.append("\nContent:");
186:
187: StringItem messageItem = new StringItem("Message",
188: buff.toString());
189: messageItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
190: content.append(messageItem);
191:
192: MessagePart[] parts = mpm.getMessageParts();
193:
194: if (parts != null) {
195: for (int i = 0; i < parts.length; i++) {
196: buff = new StringBuffer();
197:
198: MessagePart mp = parts[i];
199: buff.append("Content-Type: ");
200:
201: String mimeType = mp.getMIMEType();
202: buff.append(mimeType);
203:
204: String contentLocation = mp
205: .getContentLocation();
206: buff.append("\nContent:\n");
207:
208: byte[] ba = mp.getContent();
209:
210: try {
211: Image image = Image.createImage(ba, 0,
212: ba.length);
213: content.append(buff.toString());
214:
215: ImageItem imageItem = new ImageItem(
216: contentLocation, image,
217: Item.LAYOUT_NEWLINE_AFTER,
218: contentLocation);
219: content.append(imageItem);
220: } catch (IllegalArgumentException iae) {
221: buff.append(new String(ba));
222:
223: StringItem stringItem = new StringItem(
224: "Part", buff.toString());
225: stringItem
226: .setLayout(Item.LAYOUT_NEWLINE_AFTER);
227: content.append(stringItem);
228: }
229: }
230: }
231: }
232:
233: display.setCurrent(content);
234: }
235: } catch (IOException e) {
236: e.printStackTrace();
237: }
238: }
239:
240: /**
241: * Pause signals the thread to stop by clearing the thread field.
242: * If stopped before done with the iterations it will
243: * be restarted from scratch later.
244: */
245: public void pauseApp() {
246: done = true;
247: thread = null;
248: resumeScreen = display.getCurrent();
249: }
250:
251: /**
252: * Destroy must cleanup everything. The thread is signaled
253: * to stop and no result is produced.
254: * @param unconditional true if a forced shutdown was requested
255: */
256: public void destroyApp(boolean unconditional) {
257: done = true;
258: thread = null;
259:
260: if (mmsconn != null) {
261: try {
262: mmsconn.close();
263: } catch (IOException e) {
264: // Ignore any errors on shutdown
265: }
266: }
267: }
268:
269: /**
270: * Respond to commands, including exit
271: * @param c user interface command requested
272: * @param s screen object initiating the request
273: */
274: public void commandAction(Command c, Displayable s) {
275: try {
276: if ((c == CMD_EXIT) || (c == Alert.DISMISS_COMMAND)) {
277: destroyApp(false);
278: notifyDestroyed();
279: }
280: } catch (Exception ex) {
281: ex.printStackTrace();
282: }
283: }
284: }
|