FileDocCategorySizeDatePackage
WMAMIDlet.javaAPI DocWireless Messaging API6441Thu Oct 09 23:10:36 BST 2003wmasample

WMAMIDlet.java

/*
 *  WMAMIDlet.java
 *
 *  Author: C. Enrique Ortiz, October 2003
 *
 *  Companion source code to "Advanced Messaging Coding with JSR 120".
 *
 *  Based on original work by C. Enrique Ortiz.
 *
 *  COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2003.
 *  The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
 *  The use of the software is subject to the terms of the end-user license
 *  agreement which accompanies or is included with the software. The software is
 *  provided "as is" and Sony Ericsson specifically disclaim any warranty or
 *  condition whatsoever regarding merchantability or fitness for a specific
 *  purpose, title or non-infringement. No warranty of any kind is made in
 *  relation to the condition, suitability, availability, accuracy, reliability,
 *  merchantability and/or non-infringement of the software provided herein.
 */

package wmasample;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import wmafw.WMAFramework;
import wmafw.IncomingMessageListener;

/**
 *  WMAMIDlet.java implement a simple WMA Framework Client.
 */
public class WMAMIDlet extends MIDlet
implements IncomingMessageListener, CommandListener, Runnable
{
    /*****************************/
    /* Command Thread Processing */
    /*****************************/

    /** Command for thread processing */
    private Command thCmd;
    private static final int SELECTION_EMAIL    = 0;
    private static final int SELECTION_CALENDAR = 1;
    private static final int SELECTION_CONTACTS = 2;

    /***************/
    /*  UI related */
    /***************/

    /** LCDUI Display context */
    protected Display display;
    /** LCDUI Display context */
    private MainMenuScreen mainMenu;

    /** WMAFramework Instance*/
    WMAFramework wmafw;

    /**
     * Constructor
     */
    public WMAMIDlet() {
    }

    /**
     * Initial state
     */
    public void startApp() throws MIDletStateChangeException {
        try {
            //  Get the LCDI Display context
            if (display == null) {
                display = Display.getDisplay(this);
            }

            //  Initialize the Application's Main Menu
            if (mainMenu == null) {
                mainMenu = new MainMenuScreen(display, this);
            }

            if (mainMenu != null) mainMenu.showMenu();

            //  Get the WMA Framework Singleton Instance
            if (wmafw == null) {
                wmafw = WMAFramework.getInstance(display);
                if (wmafw != null) {
                    wmafw.setIncomingMessageListener(this);
                    wmafw.createServerConnection();
                    mainMenu.setTicker(UIConstants.TXT_ACTIVE_SMSC + wmafw.getSMSC());
                }
            }

        } catch(Exception e) {
            //  If some kind of error ocurred, throw MIDlet exception.
            throw new MIDletStateChangeException(UIConstants.TXT_ERROR_STARTING + e);
        }
    }

    /**
     * Paused state.  Release resources (connection, threads, etc).
     */
    public void pauseApp() {
    }

    /**
     *  Destroy state.  Release resources (connection, threads, etc).
     *  @param uc If true when this method is called, the MIDlet must
     *  cleanup and release all resources. If false the MIDlet may throw
     *  MIDletStateChangeException  to indicate it does not want to be destroyed at
     *  this time.
     */
    public void destroyApp(boolean uc) throws MIDletStateChangeException {
        cleanUp();
    }

    /**
     * Clean up
     */
    private void cleanUp() {
        display = null;
        mainMenu = null;
        if (wmafw != null) {
            wmafw.stopFramework();
            wmafw = null;
        }
    }

    /**
     *  Command/button listener
     *  @param c the LCDUI Command to process
     *  @param d the Displayable source of the Command
     */
    public void commandAction(Command c, Displayable d) {
        Thread th = new Thread(this);
        thCmd = c;
        th.start();
    } // commandAction()

    /**
     * Runnable entry point, controls the application (WMAMIDlet) navigation.
     */
    public void run () {
        try {
            if (thCmd == MainMenuScreen.EXIT) {
                cleanUp();
                notifyDestroyed();
            } else if (thCmd == MainMenuScreen.SELECT) {
                int index = mainMenu.getMenuSelection();
                if (index == SELECTION_EMAIL) {
                    if (wmafw != null) wmafw.showInbox(mainMenu.getScreen());
                    mainMenu.setTicker(""); // reset ticker
                } else if (index == SELECTION_CALENDAR) {
                    Alert alert = new Alert(UIConstants.MSG_NI, UIConstants.MSG_CALENDAR_TBI, null, AlertType.INFO);
                    display.setCurrent(alert);
                } else if (index == SELECTION_CONTACTS) {
                    Alert alert = new Alert(UIConstants.MSG_NI, UIConstants.MSG_CONTACTS_TBI, null, AlertType.INFO);
                    display.setCurrent(alert);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("WMAMIDlet.run Exception " + e);
        }
    }

    /***************************************************************/
    /* onTextMessage and onBinaryMessage are the WMA Framework's   */
    /*  incoming message notification callbacks. Applications can  */
    /*  used this methods to react to new incoming short messages. */
    /***************************************************************/

     /**
      * New Text Message Notification Callback.
      * @param from is the address of the sender
      * @param message is the received text message
      */
    public void onTextMessage(String from, String message) {
        if (from != null) mainMenu.setTicker(UIConstants.TXT_NEW_TEXT_MSG + from);
    }

    /**
     * New Binary Message Notification Callback.
     * @param from is the address of the sender
     * @param message is the received binary message
     */
    public void onBinaryMessage(String from, byte[] message) {
        if (from != null) mainMenu.setTicker(UIConstants.TXT_NEW_BIN_MSG + from);
    }

} //  WMAMIDlet