/*
* ociScreen.java
*
* Copyright (C) 2008 by delta_foxtrot@users.sourceforge.net
* http://gpsavenger.sf.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.blackberry.api.browser.*;
public class ociScreen extends MainScreen
{
private RadioStatusListener radioStatusListener = null;
private GPS doGPS = null;
private LabelField lf = new LabelField("Click on the Start GPS menu item to start logging locations");
private LabelField lf2 = new LabelField("");
public ociScreen()
{
super();
LabelField title = new LabelField("OpenCellID 4 BlackBerry", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
doGPS = new GPS();
radioStatusListener = new RadioStatusListener()
{
public void signalLevel(int level)
{
onRadioStatusChanged();
}
public void networkStarted(int networkId, int service)
{
onRadioStatusChanged();
}
public void baseStationChange()
{
onRadioStatusChanged();
}
public void radioTurnedOff()
{
onRadioStatusChanged();
}
public void pdpStateChange(int apn, int state, int cause)
{
onRadioStatusChanged();
}
public void networkStateChange(int state)
{
onRadioStatusChanged();
}
public void networkScanComplete(boolean success)
{
onRadioStatusChanged();
}
/*
public void mobilityManagementEvent(int eventCode, int cause)
{
onRadioStatusChanged();
}
*/
public void networkServiceChange(int networkId, int service)
{
onRadioStatusChanged();
}
};
Application.getApplication().addRadioListener(radioStatusListener);
add(lf);
add(lf2);
}
public boolean onClose()
{
System.exit(0);
return(true);
}
MenuItem gpsRunMenu = new MenuItem("Start GPS", 30, 30)
{
public void run()
{
if(gpsRunMenu.toString().equals("Start GPS"))
{
doGPS.startLocationUpdate();
gpsRunMenu.setText("Stop GPS");
lf.setText("Starting GPS, please wait while a lock is achieved.");
} else {
doGPS.stopLocationUpdate();
gpsRunMenu.setText("Start GPS");
doGPS.latitude = -999.0;
doGPS.longitude = -999.0;
lf.setText("Click on the Start GPS menu item to start logging locations");
}
}
};
MenuItem websiteMenu = new MenuItem("OCI4BB Homepage", 40, 40)
{
public void run()
{
Browser.getDefaultSession().displayPage("http://oci4bb.sourceforge.net");
}
};
MenuItem aboutMenu = new MenuItem("About", 50, 50)
{
public void run()
{
Browser.getDefaultSession().displayPage("http://www.opencellid.org");
}
};
MenuItem quitMenu = new MenuItem("Quit", 60, 60)
{
public void run()
{
onClose();
}
};
protected void makeMenu(Menu menu, int instance)
{
menu.add(gpsRunMenu);
menu.add(websiteMenu);
menu.add(aboutMenu);
menu.add(quitMenu);
}
protected void onRadioStatusChanged()
{
System.out.println("lat: "+doGPS.latitude);
System.out.println("lon: "+doGPS.longitude);
System.out.println("last: "+doGPS.lastupdate);
System.out.println("time: "+(System.currentTimeMillis() / 1000));
if(doGPS.latitude < -90 || doGPS.latitude > 90 ||
doGPS.longitude < -180 || doGPS.longitude > 180 ||
doGPS.lastupdate < ((System.currentTimeMillis() / 1000) - 120) ||
!ServerCommunication.getGPRSConnectionAvailable())
{
lf2.setText("Still waiting for lock...");
return;
}
String string = "http://www.opencellid.org/measure/add";
string += "?key=9c46be0fe0d6b163f75b22a4acbc6a50";
string += "&userkey="+ociApp.getkey();
string += "&mnc="+RadioInfo.getMNC(RadioInfo.getCurrentNetworkIndex());
string += "&mcc="+Integer.toHexString(GPRSInfo.getCellInfo().getMCC());
string += "&lac="+GPRSInfo.getCellInfo().getLAC();
string += "&cellid="+GPRSInfo.getCellInfo().getCellId();
string += "&lat="+doGPS.latitude;
string += "&lon="+doGPS.longitude;
string += "&signal="+RadioInfo.getSignalLevel();
lf.setText(string);
System.out.println("url: "+string);
String reply = ServerCommunication.http_get(string);
lf2.setText(reply);
}
}