ReceiveScreen.java

/**
 * Copyright (c) 2012 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 javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.MultipartMessage;

// Form used to display the contents of a received message
class ReceiveScreen extends Form implements CommandListener {

    private final MMSMIDlet midlet;
    private final Command commandClose = new Command("Close", Command.ITEM, 1);

    ReceiveScreen(MMSMIDlet midlet) {
        super(null);
        this.midlet = midlet;
    }

    public void setMessage(Message mmsMessage) {
        if (mmsMessage != null) {
            createForm(mmsMessage);
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == commandClose) {
            midlet.resumeDisplay();
        }
    }

    private void createForm(Message mmsMessage) {
        deleteAll();
        setTitle("Received MMS Message");

        if (mmsMessage instanceof MultipartMessage) {
            MultipartMessage multipartMessage = (MultipartMessage) mmsMessage;

            // Display message header.
            StringItem messageHeader = new StringItem(null, "From: "
                    + mmsMessage.getAddress() + "\n" + "Sent: "
                    + multipartMessage.getTimestamp().toString());
            messageHeader.setLayout(Item.LAYOUT_NEWLINE_AFTER);
            append(messageHeader);

            MessagePart[] messageParts = multipartMessage.getMessageParts();

            // Display all parts of the message.
            for (int i = 0; i < messageParts.length; i++) {
                MessagePart messagePart = messageParts[i];
                String mimeType = messagePart.getMIMEType();
                String contentLocation = messagePart.getContentLocation();

                byte[] part = messagePart.getContent();

                if (mimeType.equals("image/jpeg")) {
                    // Message contains an image.
                    Image image = Image.createImage(part, 0, part.length);
                    ImageItem imageItem = new ImageItem(null, image,
                            Item.LAYOUT_CENTER + Item.LAYOUT_NEWLINE_AFTER,
                            contentLocation);
                    append(imageItem);
                }
                if (mimeType.equals("text/plain")) {
                    // Message contains text.
                    StringItem stringItem = new StringItem(null, new String(
                            part));
                    stringItem.setLayout(Item.LAYOUT_CENTER);
                    append(stringItem);
                }
                // Unknown MIME-types are ignored
            }
        }

        addCommand(commandClose);
        setCommandListener(this);
    }
}