TouristMIDlet.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;

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

import com.nokia.example.location.touristroute.model.ConfigurationProvider;
import com.nokia.example.location.touristroute.model.ProviderStatusListener;
import com.nokia.example.location.touristroute.model.TouristData;
import com.nokia.example.location.touristroute.ui.MessageUI;
import com.nokia.example.location.touristroute.ui.TouristUI;

/**
 * Tourist Route MIDlet class.
 */
public class TouristMIDlet extends MIDlet implements ProviderStatusListener
{
    /** A static reference to Display object. */
    private static Display display = null;

    /** A Reference to TouristData. */
    private TouristData data = null;

    /** Lock object */
    private Object mutex = new Object();

    public TouristMIDlet()
    {
        super();
    }

    protected void startApp() throws MIDletStateChangeException
    {
        display = Display.getDisplay(this);

        if (ConfigurationProvider.isLocationApiSupported())
        {
            ConfigurationProvider.getInstance().autoSearch(this);
        }
        else
        {
            MessageUI.showApiNotSupported();
        }
    }

    protected void pauseApp()
    {
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException
    {
    }

    /**
     * Getter method for Display reference.
     *
     * @return reference to Display object.
     */
    public static Display getDisplay()
    {
        return display;
    }

    /**
     * Event indicating location provider is selected. MIDlet use may therefore
     * begin.
     *
     * @see com.nokia.example.location.tourist.model.ProviderSelectedListener#providerSelectedEvent()
     */
    public void providerSelectedEvent()
    {
        // Attempt to acquire the mutex
        synchronized (mutex)
        {
            // Start scanning location updates. Also set the TouristData
            // reference data.
            MessageUI.showLocationProviderState();

            // Inform the user that MIDlet is looking for location data.
            data = new TouristData((ProviderStatusListener) this);
        }
    }

    /**
     * Event indication about the first location update. This method sets
     * Tourist UI visible. By using mutex object, we ensure TouristaData (data)
     * is created on providerSelectedEvent.
     *
     * @see com.nokia.example.location.tourist.model.ProviderStatusListener#firstLocationUpdateEvent()
     */
    public void firstLocationUpdateEvent()
    {
        // Attempt to acquire the mutex
        synchronized (mutex)
        {
            TouristUI ui = new TouristUI(data);

            data.setTouristUI(ui);
            display.setCurrent(ui);
        }
    }
}