AMMSMIDlet.java

/*
 * Copyright © 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.midp.examples.jsr234.mansion;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * AMMSMIDlet class.
 *
 */
public class AMMSMIDlet extends MIDlet implements CommandListener {

    Display display;
    MansionCanvas canvas;
    Alert alert;
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private Command backCommand = new Command("Back", Command.BACK, 1);
    private Command reverbCommand = new Command("Reverb", Command.OK, 1);
    private Command helpCommand = new Command("Help", Command.HELP, 1);
    private Form helpScreen;

    /*
     * Create the canvas
     */
    public AMMSMIDlet() {
        display = Display.getDisplay(this);
        canvas = new MansionCanvas(display);
        canvas.addCommand(exitCommand);
        canvas.addCommand(reverbCommand);
        canvas.addCommand(helpCommand);
        canvas.setCommandListener(this);
    }

    public void startApp() throws MIDletStateChangeException {
        canvas.start();
        if (!supports3DAudio()) {
            showAlert("About 3D sound effects", "Your device doesn't have "
                    + "support for the 3D audio part of the AMMS API. Because "
                    + "of this you will hear application sounds, but the 3D "
                    + "effects of those will not work.", canvas);
        }
    }

    public void pauseApp() {
        canvas.pause();
    }

    public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        canvas.destroy();
    }

    /*
     * Respond to a command issued on the Canvas.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == backCommand) {
            canvas.start();
        } else if (c == helpCommand) {
            canvas.pause();
            showHelp();
        } else if (c == reverbCommand) {
            canvas.reverbButtonPressed();
        } else if (c == exitCommand) {
            try {
                destroyApp(false);
                notifyDestroyed();
            } catch (MIDletStateChangeException ex) {
            }
        }
    }

    /*
     * Put up the help screen. Create it if necessary. Add only the Resume
     * command.
     */
    void showHelp() {
        if (helpScreen == null) {
            helpScreen = new Form("Walking Help");

            if (canvas.hasPointerEvents()) {
                // Device has a touch screen
                helpScreen.append("Move forward or backward by swiping up "
                        + "or down. Turn by swiping left or right. Change the "
                        + "current reverb setting manually by selecting "
                        + "Reverb or checkmark icon.\n");
                String keyboardType = System.getProperty("com.nokia.keyboard.type");
                if (!keyboardType.equals("None")) {
                    // Device has a keypad as well
                    helpScreen.append("You can also use number keys 2, 4, "
                            + "5, 6 and 8 on your keypad.\n");
                }
            } else {
                // Non-touch device
                helpScreen.append("^ = walk forward\n");
                helpScreen.append("v = walk backwards\n");
                helpScreen.append("< = turn left\n");
                helpScreen.append("> = turn right\n");
                helpScreen.append("middle button = change reverb manually\n");
            }

            helpScreen.addCommand(backCommand);
            helpScreen.setCommandListener(this);
        }
        display.setCurrent(helpScreen);
    }
    
    private boolean supports3DAudio() {
        return (System.getProperty("supports.mediacapabilities").indexOf("audio3d") != -1);
    }
    
    public void showAlert(String title, String message, Displayable nextDisp) {
        alert = new Alert(title);
        alert.setString(message);
        alert.setTimeout(-2);
        if (nextDisp != null) {
            display.setCurrent(alert, nextDisp);
        }
        else {
            display.setCurrent(alert);
            alert.setCommandListener(this);
        }
    }
}