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:
039: import javax.wireless.messaging.*;
040:
041: /**
042: * Prompts for text and sends it via an SMS MessageConnection
043: */
044: public class SMSSender implements CommandListener, Runnable {
045: /** user interface command for indicating Send request */
046: Command sendCommand = new Command("Send", Command.OK, 1);
047:
048: /** user interface command for going back to the previous screen */
049: Command backCommand = new Command("Back", Command.BACK, 2);
050:
051: /** Display to use. */
052: Display display;
053:
054: /** The port on which we send SMS messages */
055: String smsPort;
056:
057: /** The URL to send the message to */
058: String destinationAddress;
059:
060: /** Area where the user enters a message to send */
061: TextBox messageBox;
062:
063: /** Where to return if the user hits "Back" */
064: Displayable backScreen;
065:
066: /** Displayed when a message is being sent */
067: Displayable sendingScreen;
068:
069: /**
070: * Initialize the MIDlet with the current display object and
071: * graphical components.
072: */
073: public SMSSender(String smsPort, Display display,
074: Displayable backScreen, Displayable sendingScreen) {
075: this .smsPort = smsPort;
076: this .display = display;
077: this .destinationAddress = null;
078: this .backScreen = backScreen;
079: this .sendingScreen = sendingScreen;
080:
081: messageBox = new TextBox("Enter Message", null, 65535,
082: TextField.ANY);
083: messageBox.addCommand(backCommand);
084: messageBox.addCommand(sendCommand);
085: messageBox.setCommandListener(this );
086: }
087:
088: /**
089: * Prompt for message and send it
090: */
091: public void promptAndSend(String destinationAddress) {
092: this .destinationAddress = destinationAddress;
093: display.setCurrent(messageBox);
094: }
095:
096: /**
097: * Respond to commands, including exit
098: * @param c user interface command requested
099: * @param s screen object initiating the request
100: */
101: public void commandAction(Command c, Displayable s) {
102: try {
103: if (c == backCommand) {
104: display.setCurrent(backScreen);
105: } else if (c == sendCommand) {
106: display.setCurrent(sendingScreen);
107: new Thread(this ).start();
108: }
109: } catch (Exception ex) {
110: ex.printStackTrace();
111: }
112: }
113:
114: /**
115: * Send the message. Called on a separate thread so we don't have
116: * contention for the display
117: */
118: public void run() {
119: String address = destinationAddress + ":" + smsPort;
120:
121: MessageConnection smsconn = null;
122:
123: try {
124: /** Open the message connection. */
125: smsconn = (MessageConnection) Connector.open(address);
126:
127: TextMessage txtmessage = (TextMessage) smsconn
128: .newMessage(MessageConnection.TEXT_MESSAGE);
129: txtmessage.setAddress(address);
130: txtmessage.setPayloadText(messageBox.getString());
131: smsconn.send(txtmessage);
132: } catch (Throwable t) {
133: System.out.println("Send caught: ");
134: t.printStackTrace();
135: }
136:
137: if (smsconn != null) {
138: try {
139: smsconn.close();
140: } catch (IOException ioe) {
141: System.out.println("Closing connection caught: ");
142: ioe.printStackTrace();
143: }
144: }
145: }
146: }
|