MainView.java

/**
 * Copyright (c) 2013 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.nnaclientexample;

import com.nokia.notifications.NotificationEnvironment;
import com.nokia.notifications.NotificationError;
import com.nokia.notifications.NotificationException;
import com.nokia.notifications.NotificationInfo;
import com.nokia.notifications.NotificationMessage;
import com.nokia.notifications.NotificationSession;
import com.nokia.notifications.NotificationSessionFactory;
import com.nokia.notifications.NotificationSessionListener;
import com.nokia.notifications.NotificationState;

import java.util.Calendar;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;


/**
 * This class displays NNA events and possible error messages. The class is also
 * responsible for controlling the whole MIDlet.
 */
public class MainView 
		extends Form 
		implements CommandListener,
				   NotificationSessionListener {

    // Constants
    private static final int ERROR = 0;
    private static final int STATUS = 1;
    private static final int NOTIFICATION_ID = 2;
    private static final int NOTIFICATION_MESSAGE = 3;
    private static final int INFO_MESSAGE = 4;
    private static final String[] MESSAGE_TYPE_STRING = {
            "Error", "Status", "Notification ID", "Notification", "Info", "JSR-211 Invocation"
            };
    private static final String ABOUT_TEXT = "This Example uses JSR-211 for Nokia Notifications to display ungrouped notifications.";
    private static final String APPLICATION_ID = "com.example";
    private static final String SERVICE_ID = "example.com";

    // Members
    private Main main = null;
    private String pressedId = null;
    private NotificationSession session = null;
    
    private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private final Command registerCommand = new Command("Register", Command.OK, 1);
    private final Command unregisterCommand = new Command("Unregister", Command.OK, 2);
    private final Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
    private final Command getIdCommand = new Command("Get Notification ID", Command.OK, 1);
    
    //Public constant for JSR-211 logs
    public static final int INVOCATION_RECEIVED = 5;
    
    /**
     * Constructor.
     */
    public MainView(Main main) {
        super("NNA Ungrouping Example");

        this.main = main;

        addCommand(registerCommand);
        addCommand(exitCommand);
        setCommandListener(this);

        StringItem aboutItem = new StringItem("About", ABOUT_TEXT);
        append(aboutItem);

        addLogMessage(STATUS, "Offline");

        try {
            session = NotificationSessionFactory.openSession(main, // The MIDlet instance
                    SERVICE_ID, // Service ID (Deprecated)
                    APPLICATION_ID, // Application ID
                    this); // NotificationSessionListener
        } catch (NotificationException ne) {
            addLogMessage(ERROR, ne.toString());
        }
    }

    public void setPressedItemId(String id) {
        this.pressedId = id;
    }

    // Call backs from NotificationSessionListener interface -->

    /**
     * @see com.nokia.notifications.NotificationSessionListener#infoReceived(NotificationInfo)
     */
    public void infoReceived(NotificationInfo info) {
        String notificationId = info.getNotificationId();

        if (notificationId.length() == 0) {
            addLogMessage(ERROR, "Received notification ID is empty!");
        } else {
            addLogMessage(NOTIFICATION_ID, notificationId);
            System.out.println("Notification ID: " + notificationId);
        }

        removeCommand(getIdCommand);
    }

    /**
     * @see com.nokia.notifications.NotificationSessionListener#messageReceived(NotificationMessage)
     */
    public void messageReceived(NotificationMessage message) {
        String itemId = message.getNotificationItemId();

        if ((this.pressedId != null) && this.pressedId.equals(itemId)) {
            addLogMessage(INFO_MESSAGE,
                "MIDLet was opened using JSR-211 by user selecting following message:");
        }

        addLogMessage(NOTIFICATION_MESSAGE, message.getPayload().getData());
    }

    /**
     * @see com.nokia.notifications.NotificationSessionListener#stateChanged(NotificationState)
     */
    public void stateChanged(NotificationState state) {
        switch (state.getSessionState()) {
        case NotificationState.STATE_OFFLINE:
            deleteAll();
            removeCommand(unregisterCommand);
            removeCommand(cancelCommand);
            addCommand(registerCommand);
            addLogMessage(STATUS, "Offline");

            break;

        case NotificationState.STATE_CONNECTING:
            deleteAll();
            removeCommand(registerCommand);
            addCommand(cancelCommand);
            addLogMessage(STATUS, "Connecting");

            break;

        case NotificationState.STATE_ONLINE:
            deleteAll();
            removeCommand(registerCommand);
            removeCommand(cancelCommand);
            addCommand(getIdCommand);
            addCommand(unregisterCommand);

            int env = -1;

            try {
                env = session.getEnvironment();
            } catch (NotificationException ne) {
                addLogMessage(ERROR, ne.toString());
            }

            switch (env) {
            case NotificationEnvironment.SANDBOX:
                addLogMessage(STATUS,
                    "Online (Sandbox)\n" + "applicationId:" + APPLICATION_ID);

                break;

            case NotificationEnvironment.GLOBAL_PRODUCTION:
                addLogMessage(STATUS,
                    "Online (Production)\n" + "applicationId:" +
                    APPLICATION_ID);

                break;

            case NotificationEnvironment.CHINA_PRODUCTION:
                addLogMessage(STATUS,
                    "Online (China Production)\n" + "applicationId:" +
                    APPLICATION_ID);

                break;

            case NotificationEnvironment.OTHER_ENVIRONMENT:
                addLogMessage(STATUS,
                    "Online (Others)\n" + "applicationId:" + APPLICATION_ID);

                break;
            }

            break;

        default:
            addLogMessage(STATUS, "Unknown");

            break;
        }

        final int error = state.getSessionError();

        if (error != NotificationError.ERROR_NONE) {
            addLogMessage(ERROR, notificationStateErrorCodeToString(error));
        }
    }

    // <-- Call backs from NotificationSessionListener interface

    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(Command, Displayable)
     */
    public void commandAction(Command command, Displayable display) {
        if (command == registerCommand) {
            try {
                session.registerApplication();
            } catch (NotificationException ne) {
                addLogMessage(ERROR, ne.toString());
            }
        } else if ((command == cancelCommand) ||
                (command == unregisterCommand)) {
            removeCommand(getIdCommand);

            try {
                session.unregisterApplication();
            } catch (NotificationException ne) {
                addLogMessage(ERROR, ne.toString());
            }
        } else if (command == getIdCommand) {
            try {
                session.getNotificationInformation();
            } catch (NotificationException ne) {
                addLogMessage(ERROR, ne.toString());
            }
        } else if (command == exitCommand) {
            session.close();
            main.quit();
        }
    }

    /**
     * Creates a new string item with the current time stamp and the given
     * message and appends it into the view.
     * @param messageType The message type.
     * @param message The message itself.
     */
    public void addLogMessage(int messageType, String message) {
        Calendar calendar = Calendar.getInstance();
        StringBuffer time = new StringBuffer().append(calendar.get(
                    Calendar.HOUR_OF_DAY)).append(":");

        if (calendar.get(Calendar.MINUTE) < 10) {
            time.append('0');
        }

        time.append(calendar.get(Calendar.MINUTE)).append(":");

        if (calendar.get(Calendar.SECOND) < 10) {
            time.append('0');
        }

        time.append(calendar.get(Calendar.SECOND));

        String messageTypeString = null;

        try {
            messageTypeString = MESSAGE_TYPE_STRING[messageType];
        } catch (ArrayIndexOutOfBoundsException e) {
            messageTypeString = "???";
        }

        StringItem item = new StringItem(time.toString() + " " +
                messageTypeString, message, StringItem.PLAIN);
        append(item);
    }

    /**
     * Provides an error message matching the given error code.
     * @param errorCode The error code from NotificationState::getSessionError().
     * @return A newly created string containing the error message.
     */
    private String notificationStateErrorCodeToString(final int errorCode) {
        String errorMessage = null;

        switch (errorCode) {
        case NotificationError.ERROR_APPLICATION_ID_CONFLICT:
            errorMessage = "The Application ID has already been registered by another MIDlet.";

            break;

        case NotificationError.ERROR_APPLICATION_ID_INVALID:
            errorMessage = "The Application ID is either empty or more than 255 characters long.";

            break;

        case NotificationError.ERROR_AUTHENTICATION_FAILED:
            errorMessage = "The device is not able to fetch the authentication details from the Notification server.";

            break;

        case NotificationError.ERROR_CONNECTION_DISABLED_BY_USER:
            errorMessage = "The user has disabled connections like WiFi and cellular network.";

            break;

        case NotificationError.ERROR_DISABLED_BY_USER:
            errorMessage = "The user has disabled  notifications.";

            break;

        case NotificationError.ERROR_NO_NETWORK:
            errorMessage = "The Notification Enabler has lost connection with the Notification server.";

            break;

        case NotificationError.ERROR_NONE:
            errorMessage = "No error.";

            break;

        case NotificationError.ERROR_NOT_ALLOWED:
            errorMessage = "NotificationSession is not in correct state.";

            break;

        case NotificationError.ERROR_NOT_KNOWN:
            errorMessage = "Other Notification error.";

            break;

        case NotificationError.ERROR_NOT_REGISTERED:
            errorMessage = "MIDlet is not registered, but is calling for a function that requires registration.";

            break;

        case NotificationError.ERROR_REGISTER_FAILED:
            errorMessage = "Registration failed because there is not enough space available in the device memory to store the settings.";

            break;

        case NotificationError.ERROR_SERVICE_UNAVAILABLE:
            errorMessage = "Notification Server is not available.";

            break;

        case NotificationError.ERROR_SESSION_CLOSED:
            errorMessage = "NotificationSession function call failed because session was closed.";

            break;

        default:
            errorMessage = "Unknown error.";

            break;
        }

        return errorMessage;
    }
}

Last updated 9 October 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×