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 javax.microedition.io.*;
035: import javax.microedition.lcdui.*;
036: import javax.microedition.midlet.*;
037:
038: import javax.wireless.messaging.*;
039:
040: /**
041: * An example MIDlet to send text via an MMS MessageConnection
042: */
043: public class MMSSend extends MIDlet implements CommandListener {
044: /** user interface command for indicating Exit request. */
045: private static Command CMD_EXIT = new Command("Exit", Command.EXIT,
046: 2);
047:
048: /** user interface command for sending the message */
049: private static Command CMD_SEND = new Command("Send", Command.ITEM,
050: 1);
051:
052: /** user interface command for adding message's part */
053: private static Command CMD_ADD_PART = new Command("Add Part",
054: Command.ITEM, 1);
055:
056: /** current display. */
057: private Display display;
058:
059: /** The application-ID on which we send MMS messages */
060: private String appID;
061:
062: /** Area where the user enters the subject of the message */
063: private TextField subjectField;
064:
065: /** Area where the user enters the phone number to send the message to */
066: private TextField destinationField;
067:
068: /** Area where the user enters the phone number to send the message to */
069: private StringItem partsLabel;
070:
071: /** Error message displayed when an invalid phone number is entered */
072: private Alert errorMessageAlert;
073:
074: /** Alert that is displayed when a message is being sent */
075: private Alert sendingMessageAlert;
076:
077: /** The last visible screen when we paused */
078: private Displayable resumeScreen = null;
079: private MMSMessage message;
080: private PartsDialog partsDialog;
081:
082: /**
083: * Initialize the MIDlet with the current display object and
084: * graphical components.
085: */
086: public MMSSend() {
087: appID = getAppProperty("MMS-ApplicationID");
088:
089: display = Display.getDisplay(this );
090:
091: Form mainForm = new Form("New MMS");
092:
093: subjectField = new TextField("Subject:", null, 256,
094: TextField.ANY);
095: mainForm.append(subjectField);
096:
097: destinationField = new TextField("Destination Address: ",
098: "mms://", 256, TextField.ANY);
099: mainForm.append(destinationField);
100:
101: partsLabel = new StringItem("Parts:", "0");
102: mainForm.append(partsLabel);
103:
104: mainForm.addCommand(CMD_EXIT);
105: mainForm.addCommand(CMD_SEND);
106: mainForm.addCommand(CMD_ADD_PART);
107: mainForm.setCommandListener(this );
108:
109: errorMessageAlert = new Alert("MMS", null, null,
110: AlertType.ERROR);
111: errorMessageAlert.setTimeout(5000);
112:
113: sendingMessageAlert = new Alert("MMS", null, null,
114: AlertType.INFO);
115: sendingMessageAlert.setTimeout(5000);
116: sendingMessageAlert.setCommandListener(this );
117:
118: resumeScreen = mainForm;
119:
120: message = new MMSMessage();
121: }
122:
123: /**
124: * startApp should return immediately to keep the dispatcher
125: * from hanging.
126: */
127: public void startApp() {
128: display.setCurrent(resumeScreen);
129: }
130:
131: /**
132: * Remember what screen is showing
133: */
134: public void pauseApp() {
135: resumeScreen = display.getCurrent();
136: }
137:
138: /**
139: * Destroy must cleanup everything.
140: * @param unconditional true if a forced shutdown was requested
141: */
142: public void destroyApp(boolean unconditional) {
143: }
144:
145: /**
146: * Respond to commands, including exit
147: * @param c user interface command requested
148: * @param s screen object initiating the request
149: */
150: public void commandAction(Command c, Displayable s) {
151: try {
152: if ((c == CMD_EXIT) || (c == Alert.DISMISS_COMMAND)) {
153: destroyApp(false);
154: notifyDestroyed();
155: } else if (c == CMD_ADD_PART) {
156: if (partsDialog == null) {
157: partsDialog = new PartsDialog(this );
158: }
159:
160: partsDialog.show();
161: } else if (c == CMD_SEND) {
162: promptAndSend();
163: }
164: } catch (Exception ex) {
165: ex.printStackTrace();
166: }
167: }
168:
169: void show() {
170: partsLabel.setText(Integer.toString(partsDialog.counter));
171: display.setCurrent(resumeScreen);
172: }
173:
174: Display getDisplay() {
175: return display;
176: }
177:
178: MMSMessage getMessage() {
179: return message;
180: }
181:
182: /**
183: * Prompt for and send the message
184: */
185: private void promptAndSend() {
186: try {
187: String address = destinationField.getString();
188: message.setSubject(subjectField.getString());
189: message.setDestination(address);
190:
191: String statusMessage = "Sending message to " + address
192: + "...";
193: sendingMessageAlert.setString(statusMessage);
194:
195: new SenderThread(message, appID).start();
196: } catch (IllegalArgumentException iae) {
197: errorMessageAlert.setString(iae.getMessage());
198: display.setCurrent(errorMessageAlert);
199: }
200: }
201: }
|