VideoOverlayMIDlet.java

/*
 * Copyright © 2011 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.overlay;
//
// Source File Name:   VideoOverlayMIDlet.java

// Testing MMAPI overlay drawing
// This MIDlet draws ControlBar on Video/Camera
// ControlBar can be used to operate VideoControl
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

public class VideoOverlayMIDlet extends MIDlet implements CommandListener {

    /** Canvas for video */
    private VideoCanvas videoCanvas;
    /** Canvas for camera viewfinder */
    private VideoCanvas cameraCanvas;
    /** Canvas for displaying snapshot */
    private DisplayCanvas displayCanvas;
    /** Exit command */
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    /** Open command to open VideoCanvas */
    private Command openCommand = new Command("Open", Command.SCREEN, 1);
    /** Main menu */
    private static List mainmenuList;
    /** VideoCanvas has camera viewfinder */
    protected static final int CAMERA = 0;
    /** VideoCanvas has video player */
    protected static final int VIDEO = 1;
    /** ControlBar buttons have icons */
    protected static final int ICON = 0;

    public VideoOverlayMIDlet() {
        videoCanvas = null;
        cameraCanvas = null;
        displayCanvas = null;
    }

    public void startApp() {
        // Creating canvases
        displayCanvas = new DisplayCanvas(this);
        cameraCanvas = new VideoCanvas(this, CAMERA);
        videoCanvas = new VideoCanvas(this, VIDEO);

        // Creating list to select VideoControl type
        mainmenuList = new List("VideoOverlayMIDlet", Choice.IMPLICIT);
        fillList();

        mainmenuList.addCommand(openCommand);
        mainmenuList.addCommand(exitCommand);
        mainmenuList.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mainmenuList);
    }

    /**
     * fillList()
     * Adds list items to main menu List
     */
    private void fillList() {
        mainmenuList.append("Camera", null);
        mainmenuList.append("Video", null);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        if (Display.getDisplay(this).getCurrent() == cameraCanvas) {
            cameraCanvas.stop();
        } else if (Display.getDisplay(this).getCurrent() == videoCanvas) {
            videoCanvas.stop();
        }
    }

    /**
     * overlayMidletExit()
     * Exiting from the MIDlet
     */
    public void overlayMidletExit() {
        destroyApp(false);
        notifyDestroyed();
    }

    /**
     * videoCanvasExit()
     * Exiting from the canvas
     * Coming back to list
     */
    void videoCanvasExit() {
        Display.getDisplay(this).setCurrent(mainmenuList);
    }

    /**
     * cameraCanvasCaptured()
     * Setting captured image on DisplayCanvas
     * @param byte pngData[] - imagedata as byte array
     */
    void cameraCanvasCaptured(byte pngData[]) {
        cameraCanvas.stop();
        Display.getDisplay(this).setCurrent(displayCanvas);
        displayCanvas.setFullScreenMode(cameraCanvas.isFullScreen());
        displayCanvas.setMode(cameraCanvas.isFullScreen());
        displayCanvas.setImage(pngData);
    }

    /**
     * videoCanvasStart(boolean camera)
     * Setting captured image on DisplayCanvas
     * @param int videotype - type of videoCanvas
     * @param int buttontype -
     * For now only one button type is defined - icon button
     */
    void videoCanvasStart(int videotype, int buttontype) {
        if (videotype == CAMERA) {
            Display.getDisplay(this).setCurrent(cameraCanvas);
            cameraCanvas.startCanvas(buttontype);
        } else {
            Display.getDisplay(this).setCurrent(videoCanvas);
            videoCanvas.startCanvas(buttontype);
        }
    }

    /**
     * displayCanvasBack()
     * Setting VideoCanvas as the current displayable and then starting VideoControl
     * @param boolean camera;
     * true - start canvas with camera;
     * false - start canvas with video
     */
    void displayCanvasBack(boolean camera) {
        if (camera) {
            Display.getDisplay(this).setCurrent(cameraCanvas);
            cameraCanvas.start();
        } else {
            Display.getDisplay(this).setCurrent(videoCanvas);
            videoCanvas.start();
        }
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            overlayMidletExit();
        } else if ((s == mainmenuList
                && c == List.SELECT_COMMAND)
                || c == openCommand) {
            int i = mainmenuList.getSelectedIndex();
            if (i == 0) {
                videoCanvasStart(CAMERA, ICON);
            } else if (i == 1) {
                videoCanvasStart(VIDEO, ICON);
            }
        }
    }
}