FileDocCategorySizeDatePackage
WMAFramework.javaAPI DocWireless Messaging API10441Mon Nov 10 21:03:44 GMT 2003wmafw

WMAFramework

public class WMAFramework extends Object implements CommandListener, IncomingMessageListener, Runnable
Implements a WMA-based short messaging framework.

Fields Summary
private static WMAFramework
instance
/ /** Singleton
private static int
DEFAULT_PORT
Default Server Port Number
private static NewMessageHandler
messageHandler
MessageHandler
private Command
thCmd
Command for thread processing
private IncomingMessageListener
incomingMessageListener
New Message Listener
private MessageConnection
mc
WMA Message Connection
protected Display
display
/ /** LCDUI Display context
private MessageInboxScreen
messageInboxScreen
Message inbox screen
private MessageDetailsScreen
messageDetailsScreen
Message summary screen
private NewMessageScreen
newMessageScreen
New message screen
private Displayable
backScreen
Return (to caller application) screen
Constructors Summary
private WMAFramework(Display d)
/ /** Constructor.

param
d is the display context.


           
                 
       
                    
       
        display = d;

        if (messageInboxScreen == null)
            messageInboxScreen = new MessageInboxScreen(display, this);

        if (messageDetailsScreen == null)
            messageDetailsScreen = new MessageDetailsScreen(display, this);

        if (newMessageScreen == null)
            newMessageScreen = new NewMessageScreen(display, this);
    
Methods Summary
private voidalertSegmentationError()
Alerts the user of segmentation error.

        // Alert the user...
    
public voidcommandAction(Command c, Displayable d)
Command/button listener.

param
c the LCDUI Command to process
param
d the Displayable source of the Command

        Thread th = new Thread(this);
        thCmd = c;
        th.start();
    
public voidcreateServerConnection()
Creates a server message connection

        String url;
        if (mc == null) {
            try {

                String p = System.getProperty("WMAFW-ServerPort");
                if (p == null)
                    url = "sms://:"+DEFAULT_PORT;
                else
                    url = "sms://:"+p.trim();

                mc = (MessageConnection) Connector.open(url);
                messageHandler = new NewMessageHandler(mc);
                messageHandler.setIncomingMessageListener(this);
                messageHandler.start();
            } catch (Exception e) {
                System.out.println("createMessageConnection Exception: " + e);
            }
        }
    
public static wmafw.WMAFrameworkgetInstance(Display d)
/ /** Returns the singleton instance of the WMAFramework.

param
d is the LCDUI Display context.
return
the singleton instance of WMAFramework.

        if (instance == null) instance = new WMAFramework(d);
        return instance;
    
public java.lang.StringgetSMSC()
Returns the active SMSC used by WMA Framework.

return
the active SMSC.

        return System.getProperty("wireless.messaging.sms.smsc");
    
public voidonBinaryMessage(java.lang.String from, byte[] message)
Notification Listener entry point

param
from is the message sender.
param
message is the received binary message.

        //  1. create, queue, persist message
        ShortMessage sm = new ShortMessage(from, null, message);
        inbox.addElement(sm);
        //  2. notify application
        incomingMessageListener.onBinaryMessage(from, message);
    
public voidonTextMessage(java.lang.String from, java.lang.String message)
/ /** Notification Listener entry point

param
from is the message sender.
param
message is the received text message.

        //  1. create, queue, persist message
        ShortMessage sm = new ShortMessage(from, message, null);
        inbox.addElement(sm);
        messageInboxScreen.setInbox(inbox);
        //  2. notify application
        incomingMessageListener.onTextMessage(from, message);
    
public voidrun()
Runnable entry point. Processes UI commands, providing all navigation at the application level. Note that navigation at the WMAFramework level is provided by the WMAFramework.

        if (thCmd == MessageInboxScreen.VIEW) {
            try {
                int i = messageInboxScreen.getMenuSelection();
                if (i != -1) {
                    ShortMessage sm = (ShortMessage) inbox.elementAt(i);
                    messageDetailsScreen.showScreen(sm.from, sm.text);
                }
            } catch (Exception e) {
            }
        } else if (thCmd == MessageInboxScreen.BACK) {
            display.setCurrent(backScreen);

        } else if (thCmd == MessageInboxScreen.RESET) {
            inbox.removeAllElements();
            messageInboxScreen.setInbox(inbox);
        } else if (thCmd == MessageInboxScreen.NEW) {
            newMessageScreen.showScreen("", display.getCurrent());

        } else if (thCmd == MessageDetailsScreen.BACK) {
            messageInboxScreen.showScreen(inbox);

        } else if (thCmd == MessageDetailsScreen.REPLY) {
            newMessageScreen.showScreen(messageDetailsScreen.getFrom(), display.getCurrent());

        } else if (thCmd == NewMessageScreen.BACK) {
            display.setCurrent(newMessageScreen.getBackScreen());

        } else if (thCmd == NewMessageScreen.SEND) {
            sendTextMessage(newMessageScreen.getFieldTo(),newMessageScreen.getFieldMessage());
            display.setCurrent(newMessageScreen.getBackScreen());
        }
    
public voidsendBinaryMessage(java.lang.String address, byte[] message)
Sends a binary message

param
address is the receipient's address.
param
message is the message to send.

        // To be implemented...
    
public voidsendTextMessage(java.lang.String address, java.lang.String message)
Sends a text message

param
address is the receipient's address.
param
message is the message to send.

        if (address.startsWith("sms") == false) {
            address = "sms://"+address;
        }
        try {
            TextMessage tmsg = (TextMessage)mc.newMessage(MessageConnection.TEXT_MESSAGE);
            if (address != null) tmsg.setAddress(address);
            tmsg.setPayloadText(message);
            // Make sure the message can be sent before trying...
            int segcount = mc.numberOfSegments(tmsg);
            if (segcount == 0) {
                //  Message is too long for the underlying protocol...
                alertSegmentationError();
            } else
                //  Send the message.
                mc.send(tmsg);
        } catch(Exception e) {
            //  Handle the exception...
            System.out.println("Exception during sendTextMessage " + e);
        }
    
public voidsetIncomingMessageListener(IncomingMessageListener listener)
Sets the WMAFramework incoming message nofitication listener.

param
listener is the NotificationListener to set.

        incomingMessageListener = listener;
    
public voidshowInbox(Displayable backScreen)
Makes the WMAFramework Inbox Screen visible.

param
backScreen is the Displayable to return to.

        this.backScreen = backScreen;
        messageInboxScreen.showScreen(inbox);
    
public voidstopFramework()
Stops the WMA Framework

        instance = null;
        messageInboxScreen = null;
        messageDetailsScreen = null;
        newMessageScreen = null;
        messageHandler.stop();
        messageHandler = null;
        try {
            mc.setMessageListener(null);
            if (mc != null) {
                mc.close();
                mc = null;
            }
        } catch (Exception e) {
            System.out.println("stopFramework Exception: " + e);
        }