SendScreen.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 java.io.UnsupportedEncodingException;
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.TextField;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.SizeExceededException;

import com.nokia.mid.ui.IconCommand;

// This form displays the content of the captured image
// and fields to add the recipient address and the
// text of the message
class SendScreen extends Form implements CommandListener {

    private final MMSMIDlet midlet;
    //private final Command commandSend = new Command("Send", Command.ITEM, 1);
    private final IconCommand commandSend
        = new IconCommand("Send", Command.ITEM, 1, IconCommand.ICON_OK);
    private final Command commandBack = new Command("Back", Command.BACK, 0);
    private TextField messageText;
    private TextField recipientPhone;
    private byte[] messageImage;

    SendScreen(MMSMIDlet midlet) {
        super("MMS Message");
        this.midlet = midlet;
        addCommand(commandSend);
        addCommand(commandBack);

        setCommandListener(this);
    }

    void initializeComposeCanvas(byte[] pngImage) {
        messageText = new TextField("Text: ", null, 256, TextField.ANY);
        recipientPhone = new TextField("To: ", null, 256, TextField.PHONENUMBER);
        messageImage = pngImage;
        Image tempImage = Image.createImage(pngImage, 0, pngImage.length);
        ImageItem imageItem = new ImageItem(null, tempImage,
                Item.LAYOUT_CENTER, null);

        deleteAll();
        append(messageText);
        append(recipientPhone);
        append(imageItem);

        midlet.showSendScreen();
    }

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

        if (c == commandSend) {
            sendMMS();
        }
    }

    private void sendMMS() {
        String recipientAddress = "mms://" + recipientPhone.getString() + ":"
                + midlet.getApplicationID();

        try {
            // get the byte content of the message and
            // the captured image
            byte[] textBytes = messageText.getString().getBytes("UTF-8");
            byte[] imageBytes = messageImage;

            // Create the message part containting the image
            // and give the appropriate MIME-type and id
            MessagePart imagePart = new MessagePart(imageBytes, 0,
                    imageBytes.length, "image/jpeg", "id0", "snapshot image",
                    null);

            // Create the message part containting the text
            // and give the appropriate MIME-type and id
            MessagePart textPart = new MessagePart(textBytes, 0,
                    textBytes.length, "text/plain", "id1", "message text",
                    "UTF-8");

            midlet.showInfo("Sending message...");
            midlet.sendMessage(recipientAddress, imagePart, textPart);
        } catch (UnsupportedEncodingException uee) {
            midlet.showError("Encoding not supported. " + uee.getMessage());
        } catch (SizeExceededException see) {
            midlet.showError("Message part is too big. " + see.getMessage());
        }
    }
}