PitchRollUI.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.example.location.touristroute.ui;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.location.Orientation;

import com.nokia.example.location.touristroute.TouristMIDlet;
import com.nokia.example.location.touristroute.model.ConfigurationProvider;

/**
 * Viewer class that renders orientations pitch and roll values to meters.
 */
public class PitchRollUI extends Canvas implements CommandListener, Runnable
{
    private CompassUI compass;

    private float pitch = Float.NaN;

    private float roll = Float.NaN;

    /** Flag telling is the compass update thread active */
    private boolean threadActive = false;

    /** Sleep 1000ms during each compass update */
    private final long SLEEPTIME = 1000;

    private Command compassCmd = new Command("Compass", Command.BACK, 1);

    public PitchRollUI(CompassUI compass)
    {
        this.compass = compass;

        addCommand(compassCmd);
        setCommandListener(this);
    }

    /**
     * VM calls this method immediately prior to this Canvas being made visible
     * on the display.
     */
    protected void showNotify()
    {
        // Actives compass update thread.
        threadActive = true;
        new Thread(this).start();
    }

    /**
     * VM calls this method shortly after the Canvas has been removed from the
     * display.
     */
    protected void hideNotify()
    {
        // Stops the thread.
        threadActive = false;
    }

    /**
     * run method from Runnable interface. Updates azimuth, pitch and roll
     * values and repaints the canvas.
     *
     * If Orientation is supported on the terminal, support level may be either
     * 2D or 3D depending on the compass sensor. If the sensor providers only
     * compass azimuth, pitch and roll values are Float.NaN.
     */
    public void run()
    {
        // Run this thread until this displayable is not visiable.
        // See also hideNotify() method.
        while (threadActive)
        {
            Orientation orientation = ConfigurationProvider.getInstance()
                    .getOrientation();

            if (orientation != null)
            {
                pitch = orientation.getPitch();
                roll = orientation.getRoll();
            }

            repaint();

            try
            {
                // Pause this thread for a secord before next update.
                Thread.sleep(SLEEPTIME);
            }
            catch (InterruptedException e)
            {
            }
        }
    }

    /**
     * Renders the canvas.
     *
     * @param g -
     *            the Graphics object to be used for rendering the Canvas
     */
    protected void paint(Graphics g)
    {
        // clean up canvas
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());

        paintPitch(g);
        paintRoll(g);
    }

    /**
     * Draws a customized meter with a scale.
     */
    protected void drawMeter(Graphics g, int min, int max, int smallStep,
            int largeStep, int baseline, int margin)
    {
        double position;
        double scale = 100.0; // scale value to minimize rounding error
        int x;

        g.setColor(0x0000ff);
        g.drawLine(margin, baseline, getWidth(), baseline);

        for (int i = min; i <= max; i += smallStep)
        {
            position = (((i + max) * scale * (getWidth() - margin)) / (2 * max))
                    / scale;
            x = margin + (int) position;
            g.drawLine(x, baseline - 3, x, baseline + 3);
        }

        for (int i = min; i <= max; i += largeStep)
        {
            position = (((i + max) * scale * (getWidth() - margin)) / (2 * max))
                    / scale;
            x = margin + (int) position;
            g.drawLine(x, baseline - 5, x, baseline + 5);
        }
    }

    /**
     * Paint pitch meter and place the current pitch value to the meter.
     *
     * @param g -
     *            the Graphics object to be used for rendering the Canvas
     */
    protected void paintPitch(Graphics g)
    {
        int baseline = 10;
        int textAreaWidth = 50;
        double position;

        g.setColor(0x000000);
        g.drawString("Pitch", 0, baseline - 5, Graphics.TOP | Graphics.LEFT);
        g.drawString("-90", textAreaWidth + 1, baseline, Graphics.TOP
                | Graphics.LEFT);
        g.drawString("0", textAreaWidth + (getWidth() - textAreaWidth) / 2,
                baseline, Graphics.TOP | Graphics.HCENTER);
        g.drawString("+90", getWidth(), baseline, Graphics.TOP
                | Graphics.RIGHT);

        drawMeter(g, -90, 90, 10, 30, baseline, textAreaWidth);

        // Draw the marker only if terminals pitch is available.
        if (pitch != Float.NaN)
        {
            position = (((pitch + 90.0) * 100 * (getWidth() - textAreaWidth)) / (2 * 90)) / 100;
            g.setColor(0x000000);
            g.fillRect(textAreaWidth + (int) position - 3, baseline - 2, 5, 5);
        }
    }

    /**
     * Paint roll meter and place the current pitch value to the meter.
     *
     * @param g -
     *            the Graphics object to be used for rendering the Canvas
     */
    protected void paintRoll(Graphics g)
    {
        int baseline = 40;
        int textAreaWidth = 50;
        double position;

        g.setColor(0x000000);
        g.drawString("Roll", 0, baseline - 5, Graphics.TOP | Graphics.LEFT);
        g.drawString("-180", textAreaWidth + 1, baseline, Graphics.TOP
                | Graphics.LEFT);
        g.drawString("0", textAreaWidth + (getWidth() - textAreaWidth) / 2,
                baseline, Graphics.TOP | Graphics.HCENTER);
        g.drawString("+180", getWidth(), baseline, Graphics.TOP
                | Graphics.RIGHT);

        drawMeter(g, -180, 180, 15, 60, baseline, textAreaWidth);

        // Draw the marker only if terminals roll is available.
        if (roll != Float.NaN)
        {
            position = (((roll + 180.0) * 100 * (getWidth() - textAreaWidth)) / (2 * 180)) / 100;
            g.setColor(0x000000);
            g.fillRect(textAreaWidth + (int) position - 3, baseline - 2, 5, 5);
        }
    }

    /**
     * Event indicating when a command button is pressed.
     *
     * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
     *      javax.microedition.lcdui.Displayable)
     */
    public void commandAction(Command command, Displayable d)
    {
        if (command == compassCmd)
        {
            TouristMIDlet.getDisplay().setCurrent(compass);
        }
    }

}