MMSMIDlet.java
/*
* Copyright © 2011 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.mmsmidlet;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.MultipartMessage;
import javax.wireless.messaging.SizeExceededException;
// Main MIDlet class.
// This controls the user interface and the MMS connection
public class MMSMIDlet extends MIDlet implements MessageListener {
private final String APPLICATION_ID = "mmsdemo";
private CameraScreen cameraScreen = null;
private ReceiveScreen receiveScreen;
private SendScreen sendScreen;
private InfoScreen infoScreen;
private Displayable resumeDisplay = null;
private MessageConnection messageConnection;
private Message nextMessage = null;
public MMSMIDlet() {
}
public void startApp() {
if (resumeDisplay == null) {
// Start the MMS connection
startConnection(this);
// Create the user interface
cameraScreen = new CameraScreen(this);
infoScreen = new InfoScreen();
sendScreen = new SendScreen(this);
Display.getDisplay(this).setCurrent(cameraScreen);
resumeDisplay = cameraScreen;
cameraScreen.start();
} else {
Display.getDisplay(this).setCurrent(resumeDisplay);
}
}
public void pauseApp() {
if (Display.getDisplay(this).getCurrent() == cameraScreen) {
cameraScreen.stop();
}
}
public void destroyApp(boolean unconditional) {
if (Display.getDisplay(this).getCurrent() == cameraScreen) {
cameraScreen.stop();
}
}
void exitApplication() {
closeConnection();
destroyApp(false);
notifyDestroyed();
}
private synchronized void receive(Message incomingMessage) {
if (receiveScreen == null) {
receiveScreen = new ReceiveScreen(this);
}
receiveScreen.setMessage(incomingMessage);
Display.getDisplay(this).setCurrent(receiveScreen);
}
public void notifyIncomingMessage(MessageConnection conn) {
// Callback for inbound message.
// Start a new thread to receive the message.
new Thread() {
public void run() {
try {
Message incomingMessage = messageConnection.receive();
// this may be called multiple times if
// multiple messages arrive simultaneously
if (incomingMessage != null) {
receive(incomingMessage);
}
} catch (IOException ioe) {
showError("Exception while receiving message: "
+ ioe.getMessage());
}
}
}.start();
}
void sendMessage(String recipientAddress, MessagePart imagePart,
MessagePart textPart) {
try {
// The MMS message is constructed here.
// It is a multipart message consisting of two parts:
// image and text.
MultipartMessage mmsMessage = (MultipartMessage) messageConnection
.newMessage(MessageConnection.MULTIPART_MESSAGE);
mmsMessage.setAddress(recipientAddress);
mmsMessage.addMessagePart(imagePart);
mmsMessage.addMessagePart(textPart);
nextMessage = mmsMessage;
// Send the message in another thread
new Thread() {
public void run() {
try {
messageConnection.send(nextMessage);
} catch (IOException ioe) {
showError("Exception while sending message: "
+ ioe.getMessage());
}
}
}.start();
} catch (SizeExceededException see) {
showError("Message size is too big.");
} catch (IllegalArgumentException iae) {
showError("Phone number is missing.");
}
}
// Return the application id, either from the
// jad file or from a hardcoded value.
String getApplicationID() {
String applicationID = this.getAppProperty("Application-ID");
return applicationID == null ? APPLICATION_ID : applicationID;
}
// Upon capturing an image, show the compose screen
void imageCaptured(byte[] imageData) {
cameraScreen.stop();
resumeDisplay = sendScreen;
Display.getDisplay(this).setCurrent(sendScreen);
sendScreen.initializeComposeCanvas(imageData);
}
// Shows the screen capture camera
void showCameraScreen() {
resumeDisplay = cameraScreen;
Display.getDisplay(this).setCurrent(cameraScreen);
cameraScreen.start();
}
// Shows the incoming message screen
void showReceiveScreen() {
resumeDisplay = receiveScreen;
Display.getDisplay(this).setCurrent(receiveScreen);
}
void showSendScreen() {
resumeDisplay = sendScreen;
Display.getDisplay(this).setCurrent(sendScreen);
}
void resumeDisplay() {
Display.getDisplay(this).setCurrent(resumeDisplay);
}
// Displays the info screen
void showInfo(String messageString) {
infoScreen.showInfo(messageString, Display.getDisplay(this));
}
// Displays the error screen
void showError(String messageString) {
infoScreen.showError(messageString, Display.getDisplay(this));
}
// Closes the message connection when the applications
// is stopped
private void closeConnection() {
if (messageConnection != null) {
try {
messageConnection.close();
} catch (IOException ioe) {
// Ignore errors on shutdown
}
}
}
// Starts the message connection object
private void startConnection(final MMSMIDlet mmsmidlet) {
if (messageConnection == null) {
// Open connection in a new thread so that it doesn't
// block if a security permission request is shown
new Thread() {
public void run() {
try {
String mmsConnection = "mms://:" + getApplicationID();
messageConnection = (MessageConnection) Connector
.open(mmsConnection);
messageConnection.setMessageListener(mmsmidlet);
} catch (IOException ioe) {
showError("Exception while opening message connection: "
+ ioe.getMessage());
}
}
}.start();
}
}
}