SupportedFormats.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 java.io.IOException;
import java.util.Vector;
import javax.microedition.amms.control.ImageFormatControl;
import javax.microedition.amms.control.camera.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;

public class SupportedFormats extends Canvas
        implements CommandListener {

    static final int MINIMUM_LINES_IN_DISPLAY = 9;

    public SupportedFormats(CameraMIDlet cm) {
        linestart = 0;
        midlet = cm;
        player = null;
        back = new Command("Back", 2, 0);
        key = new Command("Key", 8, 0);
        addCommand(key);
        addCommand(back);
        setCommandListener(this);
        black = 0;
        white = 0xffffff;
        purple = 0x800080;
        red = 0xff0000;
        blue = 255;
        teal = 39321;
        pink = 0xff0099;

        neutral = black;
        supported = teal;
        notSupported = red;

        offset = getWidth() * 0.10000000000000001D;
        subOffset = (int) (offset * 1.1000000000000001D);
        sub2Offset = (int) ((double) subOffset * 1.3D);
        initText();

        lineHeight = (int) (2 * Font.SIZE_SMALL);
        if (System.getProperty("microedition.platform").indexOf("sw_platform=S60") > -1) {
            lineHeight = 3 * Font.SIZE_SMALL;
        }

        lastPointerY = -1;
    }

    private void initText() {
        txt = new Vector();

        printText(1, "JSR-135 (MMAPI) V.:" + System.getProperty("microedition.media.version"), neutral);
        printText(1, "JSR-234 (AMMS) V.:" + System.getProperty("microedition.amms.version"), neutral);
        printText(1, "--- AMMS Media Capabilities --- ", neutral);

        printAmmsCapabilitySupport("music");
        printAmmsCapabilitySupport("audio3d");
        printAmmsCapabilitySupport("imageencoding");
        printAmmsCapabilitySupport("imagepostprocessing");
        printAmmsCapabilitySupport("camera");
        printAmmsCapabilitySupport("tuner");

        printText(1, "--- Camera Orientations --- ", neutral);
        printPropertyResultsSeparately("camera.orientations");

        printText(1, "--- Camera Resolutions --- ", neutral);
        printPropertyResultsSeparately("camera.resolutions");

        printText(1, "--- AMMS Features --- ", neutral);
        try {
            player = Manager.createPlayer(midlet.getPlayerType());
            player.realize();
            player.prefetch();
            GetCameraControlFeatures(player);
            GetFlashControlFeatures(player);
            GetZoomControlFeatures(player);
            GetExposureControlFeatures(player);
            GetFocusControlFeatures(player);
            GetSnapshotControlFeatures(player);
            player.close();
        }
        catch (IOException ioexception) {
        }
        catch (MediaException mediaexception) {
        }
    }

    public void GetCameraControlFeatures(Player player) {
        CameraControl cc = (CameraControl) player.getControl("javax.microedition.amms.control.camera.CameraControl");
        if (cc == null) {
            printText(1, "Camera Control", notSupported);
        }
        else {
            printText(1, "Camera Control", supported);

            int cameraRotation = cc.getCameraRotation();
            if (cameraRotation == CameraControl.ROTATE_LEFT) {
                printText(2, "Camera Rotation: ROTATE_LEFT", neutral);
            }
            else if (cameraRotation == CameraControl.ROTATE_NONE) {
                printText(2, "Camera Rotation: ROTATE_NONE", neutral);
            }
            else if (cameraRotation == CameraControl.ROTATE_RIGHT) {
                printText(2, "Camera Rotation: ROTATE_RIGHT", neutral);
            }
            else if (cameraRotation == CameraControl.UNKNOWN) {
                printText(2, "Camera Rotation: UNKNOWN", neutral);
            }

            String expModes[] = cc.getSupportedExposureModes();
            if (expModes.length > 0) {
                printText(2, "Supported exp. modes:", neutral);
                for (int i = 0; i < expModes.length; i++) {
                    printText(3, expModes[i], supported);
                }
            }

            int stillRes[] = cc.getSupportedStillResolutions();
            if (stillRes.length > 0) {
                printText(2, "Supported still resolutions:", neutral);
                for (int i = 0; i < stillRes.length; i += 2) {
                    String str = String.valueOf(stillRes[i]) + "x" + String.valueOf(stillRes[i + 1]);
                    printText(3, str, supported);
                }
            }

            int vidRes[] = cc.getSupportedVideoResolutions();
            if (vidRes.length > 0) {
                printText(2, "Supported video resolutions:", neutral);
                for (int i = 0; i < vidRes.length; i += 2) {
                    String str = String.valueOf(vidRes[i]) + "x" + String.valueOf(vidRes[i + 1]);
                    printText(3, str, supported);
                }
            }
        }
    }

    public void GetFlashControlFeatures(Player p) {
        FlashControl fc = (FlashControl) p.getControl("javax.microedition.amms.control.camera.FlashControl");
        if (fc == null) {
            printText(1, "Flash Control", notSupported);
        }
        else {

            printText(1, "Flash Control", supported);
            printText(2, "Is flash ready right now?", neutral);
            if (fc.isFlashReady()) {
                printText(3, "Yes", supported);
            }
            else {
                printText(3, "No", notSupported);
            }

            int modes[] = fc.getSupportedModes();
            if (modes.length > 0) {
                printText(2, "Supported modes:", neutral);

                for (int i = 0; i < modes.length; i++) {
                    switch (modes[i]) {
                        case 1: // '\001'
                            printText(3, "OFF", supported);
                            break;

                        case 5: // '\005'
                            printText(3, "FORCE_WITH_REDEYEREDUCE", supported);
                            break;

                        case 4: // '\004'
                            printText(3, "FORCE", supported);
                            break;

                        case 6: // '\006'
                            printText(3, "FILLIN", supported);
                            break;

                        case 3: // '\003'
                            printText(3, "AUTO_WITH_REDEYEREDUCE", supported);
                            break;

                        case 2: // '\002'
                            printText(3, "AUTO", supported);
                            break;
                    }
                }
            }

        }
    }

    public void GetZoomControlFeatures(Player p) {
        ZoomControl zc = (ZoomControl) p.getControl("javax.microedition.amms.control.camera.ZoomControl");
        if (zc == null) {
            printText(1, "Zoom Control", notSupported);
        }
        else {

            printText(1, "Zoom Control", supported);

            int tmp = zc.getDigitalZoomLevels();
            if (tmp == ZoomControl.UNKNOWN) {
                printText(2, "Digital zoom levels: UNKNOWN", neutral);
            }
            else {
                printText(2, "Digital zoom levels: " + tmp, neutral);
            }

            int zoomDigModesInt[] = new int[zc.getDigitalZoomLevels()];
            zc.setDigitalZoom(100);
            for (int i = 0; i < zoomDigModesInt.length; i++) {
                zoomDigModesInt[i] = zc.getDigitalZoom();
                printText(3, ZoomToStr(zoomDigModesInt[i]), supported);
                zc.setDigitalZoom(-1001);
            }

            tmp = zc.getOpticalZoomLevels();
            if (tmp == ZoomControl.UNKNOWN) {
                printText(2, "Optical zoom levels: UNKNOWN", neutral);
            }
            else {
                printText(2, "Optical zoom levels: " + tmp, neutral);
            }

            int zoomOptModesInt[] = new int[zc.getOpticalZoomLevels()];
            zc.setOpticalZoom(100);
            for (int i = 0; i < zoomOptModesInt.length; i++) {
                zoomOptModesInt[i] = zc.getOpticalZoom();
                printText(3, ZoomToStr(zoomOptModesInt[i]), supported);
                zc.setOpticalZoom(-1001);
            }

            tmp = zc.getMinFocalLength();
            if (tmp == ZoomControl.UNKNOWN) {
                printText(2, "Min. focal length: UNKNOWN", neutral);
            }
            else {
                printText(2, "Min. focal length: " + tmp, neutral);
            }
        }
    }

    public void GetExposureControlFeatures(Player p) {
        ExposureControl ec = (ExposureControl) p.getControl("javax.microedition.amms.control.camera.ExposureControl");
        if (ec == null) {
            printText(1, "Exposure Control", notSupported);
        }
        else {
            printText(1, "Exposure Control", supported);

            int exp[] = ec.getSupportedExposureCompensations();
            if (exp.length > 0) {
                printText(2, "Supported exp. compensations:", neutral);
                for (int i = 0; i < exp.length; i++) {
                    printText(3, String.valueOf(exp[i]), supported);
                }
            }

            String lightmeters[] = ec.getSupportedLightMeterings();
            if (lightmeters.length > 0) {
                printText(2, "Supported light meterings:", neutral);
                for (int i = 0; i < lightmeters.length; i++) {
                    printText(3, lightmeters[i], supported);
                }

            }

            int fstops[] = ec.getSupportedFStops();
            if (fstops.length > 0) {
                printText(2, "Supported FStops:", neutral);
                for (int i = 0; i < fstops.length; i++) {
                    printText(3, String.valueOf(fstops[i]), supported);
                }

            }

            int isos[] = ec.getSupportedISOs();
            if (isos.length > 0) {
                printText(2, "Supported ISOs:", neutral);
                for (int i = 0; i < isos.length; i++) {
                    printText(3, String.valueOf(isos[i]), supported);
                }

            }
            printText(2, "Min. exposure time: " + ec.getMinExposureTime(), neutral);
            printText(2, "Max. exposure time: " + ec.getMaxExposureTime(), neutral);
        }
    }

    public void GetFocusControlFeatures(Player p) {
        FocusControl ffc = (FocusControl) p.getControl("javax.microedition.amms.control.camera.FocusControl");
        if (ffc == null) {
            printText(1, "Focus Control", notSupported);
        }
        else {
            printText(1, "Focus Control", supported);

            if (ffc.isAutoFocusSupported()) {
                printText(2, "Auto focus", supported);
            }
            else {
                printText(2, "Auto focus", notSupported);
            }

            if (ffc.isManualFocusSupported()) {
                printText(2, "Manual focus", supported);
            }
            else {
                printText(2, "Manual focus", notSupported);
            }

            if (ffc.isMacroSupported()) {
                printText(2, "Macro focus", supported);
            }
            else {
                printText(2, "Macro focus", notSupported);
            }

            int tmp = ffc.getMinFocus();
            if (tmp == FocusControl.UNKNOWN) {
                printText(2, "Min. focus distance: UNKNOWN", neutral);
            }
            else {
                printText(2, "Min. focus distance: " + tmp, neutral);
            }

            printText(2, "Focus steps: " + ffc.getFocusSteps(), neutral);
        }
    }

    public void GetSnapshotControlFeatures(Player p) {
        SnapshotControl sc = (SnapshotControl) p.getControl("javax.microedition.amms.control.camera.SnapshotControl");
        if (sc == null) {
            printText(1, "Snapshot Control", notSupported);
        }
        else {
            printText(1, "Snapshot Control", supported);
            printText(2, "Default directory:", neutral);
            printText(3, sc.getDirectory(), neutral);
        }
    }

    public void GetImageFormatControlFeatures(Player p) {
        ImageFormatControl sc = (ImageFormatControl) p.getControl("javax.microedition.amms.control.ImageFormatControl");
        Text t = new Text("Image Format Control");
        if (sc == null) {
            t.SetColor(red);
            txt.addElement(t);
        }
        else {
            t.SetColor(blue);
            txt.addElement(t);
        }
    }

    public void printPropertyResultsSeparately(String property) {
        String result = System.getProperty(property) + " ";
        if (result.indexOf(":") == -1) {
            printText(2, "Not supported", notSupported);
        }
        else {
            while (result.indexOf(":") != -1) {
                String tmp = result.substring(0, result.indexOf(" "));
                result = result.substring(result.indexOf(" ") + 1);
                //System.out.println("tempt: " + tmp);
                //System.out.println("orient: " + result);
                printText(2, tmp, supported);
            }

        }
    }

    public void printAmmsCapabilitySupport(String mediaCap) {
        String printableMediaCap = "j.m.a.c.c." + mediaCap;
        if (System.getProperty("supports.mediacapabilities").indexOf(mediaCap) != -1) {
            printText(2, printableMediaCap, supported);
        }
        else {
            printText(2, printableMediaCap, notSupported);
        }
    }

    protected void printText(int levelOfText, String text, int color) {
        Text t = new Text(text);
        t.SetColor(color);
        switch (levelOfText) {
            case 2:
                t.setOffset(subOffset);
                break;
            case 3:
                t.setOffset(sub2Offset);
                break;
            case 1:
            default:
                t.setOffset(0);
                break;
        }
        txt.addElement(t);
    }

    public String ZoomToStr(int z) {
        if (z % 100 == 0) {
            return String.valueOf(z / 100) + "x";
        }
        else {
            return String.valueOf(z / 100) + "." + String.valueOf(z % 100) + "x";
        }
    }

    protected void paint(Graphics g) {
        g.setColor(white);
        g.fillRect(0, 0, getWidth(), getHeight());
        Font f = Font.getFont(0, 0, Font.SIZE_SMALL);
        g.setFont(f);
        System.out.println("linestart: " + linestart + ", txtsize: " + txt.size());
        for (int i = linestart; i < txt.size(); i++) {
            int effectiveLinenumber = i - linestart + 1;
            Text str = (Text) txt.elementAt(i);
            g.setColor(str.getColor());
            g.drawString(str.getText(), (int) (offset + (double) str.getOffset()),
                    effectiveLinenumber * lineHeight, 68);
        }

    }

    /*
     * To support Canvas scrolling on devices, pointer events are
     * overriden. For more information on this see Wiki page
     * http://www.developer.nokia.com/Community/Wiki/How_to_implement_touch_image_scrolling_in_Java_ME
     */
    protected void pointerPressed(int x, int y) {
        lastPointerY = y;
    }

    protected void pointerReleased(int x, int y) {
        lastPointerY = -1;
    }

    protected void pointerDragged(int x, int y) {
        if (lastPointerY >= 0) {
            while (lastPointerY + lineHeight < y) {
                addToLinestart(-1);
                lastPointerY = lastPointerY + lineHeight;
            }
            while (lastPointerY - lineHeight > y) {
                addToLinestart(1);
                lastPointerY = lastPointerY - lineHeight;
            }
        }
        repaint();
    }

    // Action support for non-touch devices
    public void keyPressed(int keycode) {
        int action = getGameAction(keycode);
        if (action == UP) {
            addToLinestart(-1);
        }
        else if (action == DOWN) {
            addToLinestart(1);
        }
        repaint();
    }

    public void keyRepeated(int keycode) {
        keyPressed(keycode);
    }

    // Change the number of the first line to display
    private void addToLinestart(int increment) {
        linestart = linestart + increment;
        if (linestart < 0) {
            linestart = 0;
        }
        else if (linestart > txt.size() - MINIMUM_LINES_IN_DISPLAY) {
            linestart = txt.size() - MINIMUM_LINES_IN_DISPLAY;
        }
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == back) {
            if (player != null) {
                player.close();
            }
            else {
                System.out.println("No oli se player edelleen null");
            }
            CameraMIDlet.getDisplay().setCurrent(midlet.openMenu);
        }
        else if (command == key) {
            midlet.showAlert("Color Key", "Green = supported\nRed = not supported", this);
        }
    }
    int red;
    int blue;
    int black;
    int purple;
    int white;
    int teal;
    int pink;
    int supported;
    int notSupported;
    int neutral;
    double offset;
    Vector txt;
    int linestart;
    int subOffset;
    int sub2Offset;
    int lastPointerY;
    int lineHeight;
    Command back;
    Command key;
    CameraMIDlet midlet;
    Player player;
}