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 to send text via an SMS MessageConnection
044: */
045: public class SMSSend extends MIDlet implements CommandListener {
046: /** user interface command for indicating Exit request. */
047: Command exitCommand = new Command("Exit", Command.EXIT, 2);
048:
049: /** user interface command for proceeding to the next screen */
050: Command okCommand = new Command("OK", Command.OK, 1);
051:
052: /** current display. */
053: Display display;
054:
055: /** The port on which we send SMS messages */
056: String smsPort;
057:
058: /** Area where the user enters the phone number to send the message to */
059: TextBox destinationAddressBox;
060:
061: /** Error message displayed when an invalid phone number is entered */
062: Alert errorMessageAlert;
063:
064: /** Alert that is displayed when a message is being sent */
065: Alert sendingMessageAlert;
066:
067: /** Prompts for and sends the text message */
068: SMSSender sender;
069:
070: /** The last visible screen when we paused */
071: Displayable resumeScreen = null;
072:
073: /**
074: * Initialize the MIDlet with the current display object and
075: * graphical components.
076: */
077: public SMSSend() {
078: smsPort = getAppProperty("SMS-Port");
079:
080: display = Display.getDisplay(this );
081:
082: destinationAddressBox = new TextBox("Destination Address?",
083: null, 256, TextField.PHONENUMBER);
084: destinationAddressBox.addCommand(exitCommand);
085: destinationAddressBox.addCommand(okCommand);
086: destinationAddressBox.setCommandListener(this );
087:
088: errorMessageAlert = new Alert("SMS", null, null,
089: AlertType.ERROR);
090: errorMessageAlert.setTimeout(5000);
091:
092: sendingMessageAlert = new Alert("SMS", null, null,
093: AlertType.INFO);
094: sendingMessageAlert.setTimeout(5000);
095: sendingMessageAlert.setCommandListener(this );
096:
097: sender = new SMSSender(smsPort, display, destinationAddressBox,
098: sendingMessageAlert);
099:
100: resumeScreen = destinationAddressBox;
101: }
102:
103: /**
104: * startApp should return immediately to keep the dispatcher
105: * from hanging.
106: */
107: public void startApp() {
108: display.setCurrent(resumeScreen);
109: }
110:
111: /**
112: * Remember what screen is showing
113: */
114: public void pauseApp() {
115: resumeScreen = display.getCurrent();
116: }
117:
118: /**
119: * Destroy must cleanup everything.
120: * @param unconditional true if a forced shutdown was requested
121: */
122: public void destroyApp(boolean unconditional) {
123: }
124:
125: /**
126: * Respond to commands, including exit
127: * @param c user interface command requested
128: * @param s screen object initiating the request
129: */
130: public void commandAction(Command c, Displayable s) {
131: try {
132: if ((c == exitCommand) || (c == Alert.DISMISS_COMMAND)) {
133: destroyApp(false);
134: notifyDestroyed();
135: } else if (c == okCommand) {
136: promptAndSend();
137: }
138: } catch (Exception ex) {
139: ex.printStackTrace();
140: }
141: }
142:
143: /**
144: * Prompt for and send the message
145: */
146: private void promptAndSend() {
147: String address = destinationAddressBox.getString();
148:
149: if (!SMSSend.isValidPhoneNumber(address)) {
150: errorMessageAlert.setString("Invalid phone number");
151: display
152: .setCurrent(errorMessageAlert,
153: destinationAddressBox);
154:
155: return;
156: }
157:
158: String statusMessage = "Sending message to " + address + "...";
159: sendingMessageAlert.setString(statusMessage);
160: sender.promptAndSend("sms://" + address);
161: }
162:
163: /**
164: * Check the phone number for validity
165: * Valid phone numbers contain only the digits 0 thru 9, and may contain
166: * a leading '+'.
167: */
168: private static boolean isValidPhoneNumber(String number) {
169: char[] chars = number.toCharArray();
170:
171: if (chars.length == 0) {
172: return false;
173: }
174:
175: int startPos = 0;
176:
177: // initial '+' is OK
178: if (chars[0] == '+') {
179: startPos = 1;
180: }
181:
182: for (int i = startPos; i < chars.length; ++i) {
183: if (!Character.isDigit(chars[i])) {
184: return false;
185: }
186: }
187:
188: return true;
189: }
190: }
|