ProximityReminder.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.proximityreminder;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;

public class ProximityReminder
    extends MIDlet
    implements CommandListener, ProximityListener {

    private Alert timedAlert;
    private Displayable currentDisplay;
    private Command ExitCmd;
    private Command OptionsCmd;
    private Form form;

    // Menu
    private List OptionsMenu;
    private String error = "";
    private Location currentLocation = null;

    public void startApp() {
        initialize();
    }

    public void pauseApp() {
        //Nothing to do
    }

    public void destroyApp(boolean unconditional) {
        //Nothing to do
    }

    private void initialize() {
        getDisplay().setCurrent(get_form());
        SetCurrDisplay(get_form());
        currentDisplay = getDisplay().getCurrent();
    }

    public Form get_form() {
        if (form == null) {
            form = new Form(null);
            form.addCommand(get_OptionsCmd());
            form.addCommand(get_exitCmd());
        }

        form.setTicker(new Ticker("PROXIMITY REMINDER"));
        form.setCommandListener(this);

        return form;
    }

    // COMMANDS
    public Command get_exitCmd()
    {
        if (ExitCmd == null) {
            ExitCmd = new Command("Exit", Command.EXIT, 1);
        }

        return ExitCmd;
    }

    // COMMANDS
    public Command get_OptionsCmd()
    {
        if (OptionsCmd == null) {
            OptionsCmd = new Command("Options", Command.OK, 1);
        }

        return OptionsCmd;
    }

    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    boolean SetCurrDisplay(Displayable currDisp) {
        if (currDisp == null) {

            if (currentDisplay == null)
                return false;
            else
                currDisp = currentDisplay;
        }
        else {
            currentDisplay = currDisp;
        }

        getDisplay().setCurrent(currentDisplay);
        return true;
    }

    public void exitMIDlet() {
        destroyApp(true);
        notifyDestroyed();
    }

    public List get_Options() {
        if (OptionsMenu == null) {
            OptionsMenu = new List("Select Option", Choice.IMPLICIT,
                    new String[] { "Add Proximity to Current Location",
                            "Cancel" }, new Image[] { null, null });

            OptionsMenu.setCommandListener(this);
            OptionsMenu.setSelectedFlags(new boolean[] { true, false });
        }
        return OptionsMenu;
    }

    public void commandAction(Command command, Displayable displayable) {
        if (displayable == form) {
            if (command == ExitCmd) {
                exitMIDlet();
            }
            else if (command == OptionsCmd) {
                getDisplay().setCurrent(get_Options());
            }
        }

        if (displayable == OptionsMenu) {
            if (command == OptionsMenu.SELECT_COMMAND) {
                switch (OptionsMenu.getSelectedIndex()) {
                    case 0:
                        new Thread(new Runnable() {
                            public void run() {
                                getCurrentLocation();
                            }
                        }).start();
                        // flow through
                    case 1:
                        getDisplay().setCurrent(get_form());
                    break;
                }
            }
            else if (command == ExitCmd) {
                exitMIDlet();
            }
        }
    }

    // fetches the current location and adds the current location to proximity
    void getCurrentLocation() {
        try {
            // Get the provider with default criteria
            LocationProvider provider = LocationProvider.getInstance(null);

            if (provider != null) {
                // fetch the current location
                currentLocation = provider.getLocation(-1);
            }
        }
        catch (Exception e) {
            addError(e);
        }

        addproximity();
    }

    // Adds the current Location for Proxmity Updates
    void addproximity() {
        try {
            if (currentLocation != null) {
                // get the current latitude, langitude and altitude
                float alt = currentLocation.getQualifiedCoordinates().getAltitude();
                double lat = currentLocation.getQualifiedCoordinates().getLatitude();
                double lon = currentLocation.getQualifiedCoordinates().getLongitude();

                Coordinates cord1 = new Coordinates(lat, lon, alt);

                // Adds the current coordinates for proximity updates when the
                // device is within the proximity radius of 500 meters
                LocationProvider.addProximityListener(this, cord1, 500.0f);
            }
        }
        catch (SecurityException e) {
            addError(e);
        }
        catch (Exception e) {
            addError(e);
        }
    }

    public void addError(Exception e) {
        e.printStackTrace();
        error += e.getMessage() + "\n";
    }

    // This function will be called when the monitoring state changes
    public void monitoringStateChanged(boolean aState)
    {
        if (aState) {
            timedAlert = new Alert("Proximity Status", " Active", null,
                    AlertType.INFO);
            timedAlert.setTimeout(5000);
        }
        else {
            timedAlert = new Alert("Proximity Status", " NOT Active", null,
                    AlertType.INFO);
            timedAlert.setTimeout(5000);
        }
    }

    // This function will be called when the Proximity event fires
    public void proximityEvent(Coordinates aCord, Location aLoc) {
        // Displays the information using an alert
        timedAlert = new Alert("Proximity Update",
                "You are now near Location: lat = " + aCord.getLatitude()
                        + " lon =" + aCord.getLongitude(), null, AlertType.INFO);
        timedAlert.setTimeout(5000);
        currentDisplay = getDisplay().getCurrent();
        getDisplay().setCurrent(timedAlert, currentDisplay);
    }
}