CameraMIDlet.java

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

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

public class CameraMIDlet extends MIDlet
        implements CommandListener {
	// Porting MIDlet from Series 40 full touch to Nokia Asha software platform (1.0)
	public static final String PLATFORM_UI = "com.nokia.mid.ui.version";
    public static final String PLATFORM_2_0 = "1.6";
    
    public CameraMIDlet() {
        display = Display.getDisplay(this);
        
        exitcmd = new Command("Exit", Command.EXIT, 0);
        // Porting MIDlet from Series 40 full touch to Nokia Asha software platform (1.0)
        if(System.getProperty(PLATFORM_UI).startsWith(PLATFORM_2_0)){
        	selcmd = new Command("Select", Command.OK, 0);
        }else{
        	selcmd = new Command("Select", Command.ITEM, 0);
        }
        
        openMenu = new List("AMMS API support", 3);
        openMenu.deleteAll();
        openMenu.append("Supported Features", null);
        openMenu.append("Camera Control", null);
        openMenu.append("Exposure Control", null);
        openMenu.append("Flash Control", null);
        openMenu.append("Focus Control", null);
        openMenu.append("Snapshot Control", null);
        openMenu.append("Zoom Control", null);
        openMenu.addCommand(exitcmd);
        openMenu.setSelectCommand(selcmd);
        openMenu.setCommandListener(this);
        
        suppFeat = 0;
        cameracontrol = 1;
        exposurecontrol = 2;
        flashcontrol = 3;
        focuscontrol = 4;
        snapshotcontrol = 5;
        zoomcontrol = 6;
        allcontrols = 7;
        
        System.out.println(System.getProperty("microedition.platform"));
    }

    protected void destroyApp(boolean flag)
            throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }

    protected void startApp()
            throws MIDletStateChangeException {
        display.setCurrent(openMenu);
    }

    public static Display getDisplay() {
        return display;
    }

    public void showAlert(String title, String message, Displayable nextDisp) {
        alert = new Alert(title);
        alert.setString(message);
        alert.setTimeout(-2);
        if (nextDisp != null) {
            getDisplay().setCurrent(alert, nextDisp);
        }
        else {
            getDisplay().setCurrent(alert);
            alert.setCommandListener(this);
        }
    }

    /**
     * The device type can be
     * determined by checking for a support for BMP image type camera output.
     * For more information about this, please check the following wiki page:
     * http://www.developer.nokia.com/Community/Wiki/CS001259_-_Taking_a_picture_in_Java_ME
     *
     * @return "capture://image" for S40, "capture:
     */
    public String getPlayerType() {
        if (checkEncodingSupport()) {
             return "capture://video";
        }
        else {
            // Series 40 device
            return "capture://image";
        }
    }

    /**
     * Checking encodings for camera snapshot.
     * png and image/bmp encodings whereas Series 40 devices don't support them.
     * The encoding support is used for determining the platform specific
     * capture locator for camera snapshot.
     *
     * @return
     */
    private boolean checkEncodingSupport() {
        String encodings = System.getProperty("video.snapshot.encodings");
        return (encodings != null) && (encodings.indexOf("png") != -1) && 
                (encodings.indexOf("image/bmp") != -1);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitcmd) {
            notifyDestroyed();
        }
        else if (c == selcmd && d == openMenu) {
            int index = openMenu.getSelectedIndex();
            if (index == suppFeat) {
                display.setCurrent(new SupportedFormats(this));
            }
            else if (index == cameracontrol) {
                TestCameraControl testcc = new TestCameraControl(this);
                if (testcc.isSupported()) {
                    getDisplay().setCurrent(((TestControl) (testcc)).canvas);
                }
                else {
                    showAlert("Camera Control not supported", 
                            "Camera Control is not supported on this phone.", 
                            openMenu);
                }
            }
            else if (index == flashcontrol) {
                TestFlashControl testfc = new TestFlashControl(this);
                if (testfc.isSupported()) {
                    getDisplay().setCurrent(((TestControl) (testfc)).canvas);
                }
                else {
                    showAlert("Flash Control not supported", 
                            "Flash Control is not supported on this phone.", 
                            openMenu);
                }
            }
            else if (index == focuscontrol) {
                TestFocusControl testfocus = new TestFocusControl(this);
                if (testfocus.isSupported()) {
                    getDisplay().setCurrent(((TestControl) (testfocus)).canvas);
                }
                else {
                    showAlert("Control not supported", 
                            "Focus Control is not supported on this phone.", 
                            openMenu);
                }
            }
            else if (index == zoomcontrol) {
                TestZoomControl testzoom = new TestZoomControl(this);
                if (testzoom.isSupported()) {
                    getDisplay().setCurrent(((TestControl) (testzoom)).canvas);
                }
                else {
                    showAlert("Control not supported", 
                            "Zoom Control is not supported on this phone.", 
                            openMenu);
                }
            }
            else if (index == snapshotcontrol) {
                TestSnapshotControl tsc = new TestSnapshotControl(this);
                if (tsc.isSupported()) {
                    getDisplay().setCurrent(((TestControl) (tsc)).canvas);
                }
                else {
                    showAlert("Control not supported", 
                            "Snapshot Control is not supported on this phone.", 
                            openMenu);
                }
            }
            else if (index == exposurecontrol) {
                TestExposureControl ec = new TestExposureControl(this);
                if (ec.isSupported()) {
                    getDisplay().setCurrent(((TestControl) (ec)).canvas);
                }
                else {
                    showAlert("Control not supported", 
                            "Exposure Control is not supported on this phone.", 
                            openMenu);
                }
            }
        }
    }
    private static Display display = null;
    Alert alert;
    List openMenu;
    Command exitcmd;
    Command selcmd;
    int suppFeat;
    int cameracontrol;
    int flashcontrol;
    int focuscontrol;
    int snapshotcontrol;
    int zoomcontrol;
    int exposurecontrol;
    int allcontrols;
}