import com.qualcomm.qjae.gps.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
* GPS Tester Midlet v1.0
* Copyright (C) 2004 - 2005 Yakov Shafranovich (blog /at/ shaftek [.] org)
* 
*/

public class GpsTester extends MIDlet
    implements CommandListener, GpsEventListener
{

    public GpsTester()
    {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", 1, 1);
        startCommand = new Command("Start LBS", 1, 2);
        infoCommand = new Command("Info", 1, 3);
        getCommand = new Command("Get Location", 1, 4);
        stopCommand = new Command("Stop LBS", 1, 5);
        mainText = new TextBox("GpsTester", "This MIDlet tests the LBS functionality of Sprint PCS cell phones.\nCopyright (C) 2004 Yakov Shafranovich.\nwww.shaftek.org", 1024, 0);
        errTicker = new Ticker("No Errors");
    }

    public void startApp()
    {
        mainText.addCommand(exitCommand);
        mainText.addCommand(infoCommand);
        mainText.addCommand(startCommand);
        mainText.addCommand(getCommand);
        mainText.addCommand(stopCommand);
        mainText.setCommandListener(this);
        mainText.setTicker(errTicker);
        display.setCurrent(mainText);
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean flag)
    {
    }

    public void commandAction(Command command, Displayable displayable)
    {
        if(command == exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        } else
        if(command == infoCommand)
        {
            errTicker.setString("Info");
            if(Gps.getServerType() == 0)
                mainText.setString("Server Type = Default");
            else
                mainText.setString("Server Type = Custom, Adress = " + Gps.getCustomServerIPAddress() + ":" + Gps.getCustomServerIPPort());
            if(Gps.getTransportProtocol() == Gps.getTransportProtocol())
                mainText.setString(mainText.getString() + ", Protocol = TCP/IP");
            else
                mainText.setString(mainText.getString() + ", Protocol = DataBurst");
        } else
        if(command == startCommand)
            Gps.startSession(this);
        else
        if(command == getCommand)
            Gps.requestPosition(1);
        else
        if(command == stopCommand)
            Gps.stopSession();
    }

    public void handleEvent(GpsEvent gpsevent)
    {
        if(gpsevent instanceof GpsPositionEvent)
        {
            GpsPositionEvent gpspositionevent = (GpsPositionEvent)gpsevent;
            errTicker.setString("Lat: " + gpspositionevent.getLatitude() + " / Long: " + gpspositionevent.getLongitude());
        } else
        if(gpsevent instanceof GpsErrorEvent)
        {
            GpsErrorEvent gpserrorevent = (GpsErrorEvent)gpsevent;
            errTicker.setString("GPS Error: " + gpserrorevent.getMessage());
        }
    }

    private Command exitCommand;
    private Command infoCommand;
    private Command startCommand;
    private Command getCommand;
    private Command stopCommand;
    private TextBox mainText;
    private Ticker errTicker;
    private Display display;
}
