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.sms;
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 SMS MessageConnection
044: */
045: public class SMSReceive extends MIDlet implements CommandListener,
046: Runnable, MessageListener {
047: /** user interface command for indicating Exit request. */
048: Command exitCommand = new Command("Exit", Command.EXIT, 2);
049:
050: /** user interface command for indicating Reply request */
051: Command replyCommand = new Command("Reply", Command.OK, 1);
052:
053: /** user interface text box for the contents of the fetched URL. */
054: Alert content;
055:
056: /** current display. */
057: Display display;
058:
059: /** instance of a thread for asynchronous networking and user interface. */
060: Thread thread;
061:
062: /** Connections detected at start up. */
063: String[] connections;
064:
065: /** Flag to signal end of processing. */
066: boolean done;
067:
068: /** The port on which we listen for SMS messages */
069: String smsPort;
070:
071: /** SMS message connection for inbound text messages. */
072: MessageConnection smsconn;
073:
074: /** Current message read from the network. */
075: Message msg;
076:
077: /** Address of the message's sender */
078: String senderAddress;
079:
080: /** Alert that is displayed when replying */
081: Alert sendingMessageAlert;
082:
083: /** Prompts for and sends the text reply */
084: SMSSender sender;
085:
086: /** The screen to display when we return from being paused */
087: Displayable resumeScreen;
088:
089: /**
090: * Initialize the MIDlet with the current display object and
091: * graphical components.
092: */
093: public SMSReceive() {
094: smsPort = getAppProperty("SMS-Port");
095:
096: display = Display.getDisplay(this );
097:
098: content = new Alert("SMS Receive");
099: content.setTimeout(Alert.FOREVER);
100: content.addCommand(exitCommand);
101: content.setCommandListener(this );
102: content.setString("Receiving...");
103:
104: sendingMessageAlert = new Alert("SMS", null, null,
105: AlertType.INFO);
106: sendingMessageAlert.setTimeout(5000);
107: sendingMessageAlert.setCommandListener(this );
108:
109: sender = new SMSSender(smsPort, display, content,
110: sendingMessageAlert);
111:
112: resumeScreen = content;
113: }
114:
115: /**
116: * Start creates the thread to do the MessageConnection receive
117: * text.
118: * It should return immediately to keep the dispatcher
119: * from hanging.
120: */
121: public void startApp() {
122: /** SMS connection to be read. */
123: String smsConnection = "sms://:" + smsPort;
124:
125: /** Open the message connection. */
126: if (smsconn == null) {
127: try {
128: smsconn = (MessageConnection) Connector
129: .open(smsConnection);
130: smsconn.setMessageListener(this );
131: } catch (IOException ioe) {
132: ioe.printStackTrace();
133: }
134: }
135:
136: /** Initialize the text if we were started manually. */
137: connections = PushRegistry.listConnections(true);
138:
139: if ((connections == null) || (connections.length == 0)) {
140: content.setString("Waiting for SMS on port " + smsPort
141: + "...");
142: }
143:
144: done = false;
145: thread = new Thread(this );
146: thread.start();
147:
148: display.setCurrent(resumeScreen);
149: }
150:
151: /**
152: * Notification that a message arrived.
153: * @param conn the connection with messages available
154: */
155: public void notifyIncomingMessage(MessageConnection conn) {
156: if (thread == null) {
157: done = false;
158: thread = new Thread(this );
159: thread.start();
160: }
161: }
162:
163: /** Message reading thread. */
164: public void run() {
165: /** Check for sms connection. */
166: try {
167: msg = smsconn.receive();
168:
169: if (msg != null) {
170: senderAddress = msg.getAddress();
171: content.setTitle("From: " + senderAddress);
172:
173: if (msg instanceof TextMessage) {
174: content.setString(((TextMessage) msg)
175: .getPayloadText());
176: } else {
177: StringBuffer buf = new StringBuffer();
178: byte[] data = ((BinaryMessage) msg)
179: .getPayloadData();
180:
181: for (int i = 0; i < data.length; i++) {
182: int intData = (int) data[i] & 0xFF;
183:
184: if (intData < 0x10) {
185: buf.append("0");
186: }
187:
188: buf.append(Integer.toHexString(intData));
189: buf.append(' ');
190: }
191:
192: content.setString(buf.toString());
193: }
194:
195: content.addCommand(replyCommand);
196: display.setCurrent(content);
197: }
198: } catch (IOException e) {
199: // e.printStackTrace();
200: }
201: }
202:
203: /**
204: * Pause signals the thread to stop by clearing the thread field.
205: * If stopped before done with the iterations it will
206: * be restarted from scratch later.
207: */
208: public void pauseApp() {
209: done = true;
210: thread = null;
211: resumeScreen = display.getCurrent();
212: }
213:
214: /**
215: * Destroy must cleanup everything. The thread is signaled
216: * to stop and no result is produced.
217: * @param unconditional true if a forced shutdown was requested
218: */
219: public void destroyApp(boolean unconditional) {
220: done = true;
221: thread = null;
222:
223: if (smsconn != null) {
224: try {
225: smsconn.close();
226: } catch (IOException e) {
227: // Ignore any errors on shutdown
228: }
229: }
230: }
231:
232: /**
233: * Respond to commands, including exit
234: * @param c user interface command requested
235: * @param s screen object initiating the request
236: */
237: public void commandAction(Command c, Displayable s) {
238: try {
239: if ((c == exitCommand) || (c == Alert.DISMISS_COMMAND)) {
240: destroyApp(false);
241: notifyDestroyed();
242: } else if (c == replyCommand) {
243: reply();
244: }
245: } catch (Exception ex) {
246: ex.printStackTrace();
247: }
248: }
249:
250: /**
251: * Allow the user to reply to the received message
252: */
253: private void reply() {
254: // remove the leading "sms://" for displaying the destination address
255: String address = senderAddress.substring(6);
256: String statusMessage = "Sending message to " + address + "...";
257: sendingMessageAlert.setString(statusMessage);
258: sender.promptAndSend(senderAddress);
259: }
260: }
|