Created
September 23, 2012 21:33
-
-
Save criccomini/3773117 to your computer and use it in GitHub Desktop.
Blackberry Storm GPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Timer; | |
import java.util.TimerTask; | |
import javax.microedition.location.Criteria; | |
import javax.microedition.location.Location; | |
import javax.microedition.location.LocationException; | |
import javax.microedition.location.LocationListener; | |
import javax.microedition.location.LocationProvider; | |
import javax.microedition.location.QualifiedCoordinates; | |
import net.rim.device.api.system.Application; | |
import net.rim.device.api.system.Bitmap; | |
import net.rim.device.api.ui.Field; | |
import net.rim.device.api.ui.FieldChangeListener; | |
import net.rim.device.api.ui.Manager; | |
import net.rim.device.api.ui.Screen; | |
import net.rim.device.api.ui.Ui; | |
import net.rim.device.api.ui.UiEngine; | |
import net.rim.device.api.ui.component.ButtonField; | |
import net.rim.device.api.ui.component.Dialog; | |
import net.rim.device.api.ui.component.LabelField; | |
import net.rim.device.api.ui.component.RichTextField; | |
import net.rim.device.api.ui.container.MainScreen; | |
import bbtracker.uitl.BBTHTTPClient; | |
public class BBTMainScreen extends MainScreen { | |
public final static String LOCATION_URL = "YOUR SCRIPT LOCATION"; | |
private RichTextField logField; | |
private String log; | |
private long lastGPSRead; | |
public BBTMainScreen() { | |
log = ""; | |
setTitle(new LabelField("BBTracker GPS Fix", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH)); | |
ButtonField btn = new ButtonField("follow logs", ButtonField.CONSUME_CLICK); | |
btn.setChangeListener(new ButtonListener()); | |
this.add(btn); | |
logField = new RichTextField("asdf"); | |
this.add(logField); | |
lastGPSRead = System.currentTimeMillis(); | |
new LocationTracker(); | |
} | |
public boolean onClose() { | |
Application.getApplication().requestBackground(); | |
return false; | |
} | |
final class ButtonListener implements FieldChangeListener { | |
public void fieldChanged(Field field, int context) { | |
UiEngine ui = Ui.getUiEngine(); | |
Screen screen = new Dialog(Dialog.D_OK, BBTMainScreen.this.log, Dialog.OK, | |
Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.VERTICAL_SCROLL); | |
ui.queueStatus(screen, 1, true); | |
} | |
} | |
class LocationTracker extends TimerTask { | |
private double longitude, latitude; | |
private Timer timer; | |
private LocationProvider provider; | |
public LocationTracker() { | |
timer = new Timer(); | |
resetGPS(); | |
timer.schedule(this, 0, 60000); | |
} | |
public void resetGPS() { | |
try { | |
if(provider != null) { | |
provider.setLocationListener(null, 0, 0, 0); | |
provider.reset(); | |
provider = null; | |
} | |
Criteria criteria = new Criteria(); | |
criteria.setHorizontalAccuracy(1000); | |
criteria.setVerticalAccuracy(1000); | |
criteria.setCostAllowed(false); | |
// criteria.setAltitudeRequired(false); | |
// criteria.setSpeedAndCourseRequired(false); | |
// criteria.setAddressInfoRequired(false); | |
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW); | |
provider = LocationProvider.getInstance(criteria); | |
provider.setLocationListener(new MyLocationListener(), 10, -1, -1); | |
// reset the gps counter so we wait another 5 mins before a reset | |
lastGPSRead = System.currentTimeMillis(); | |
} catch(Exception e) { log = e.toString() + log; } | |
log = "reset gps\n" + log; | |
} | |
public void run() { | |
if(log.length() > 3000) { | |
log = log.substring(log.length() - 3000); | |
} | |
if(System.currentTimeMillis() - lastGPSRead > 300000) { | |
BBTHTTPClient.getPage(LOCATION_URL + "?longitude=-1&latitude=-1"); | |
resetGPS(); | |
BBTHTTPClient.getPage(LOCATION_URL + "?longitude=-2&latitude=-2"); | |
} | |
log = longitude + "," + latitude + "\n" + log; | |
BBTMainScreen.this.logField.setText(longitude + "," + latitude + "\n"); | |
log = BBTHTTPClient.getPage(LOCATION_URL + "?longitude=" + longitude + "&latitude=" + latitude).trim() + log; | |
} | |
private class MyLocationListener implements LocationListener { | |
public void locationUpdated(LocationProvider provider, Location location) | |
{ | |
if(location != null && location.isValid()) | |
{ | |
log = "got valid coords\n" + log; | |
QualifiedCoordinates qc = location.getQualifiedCoordinates(); | |
try { | |
// TODO: not thread safe (assignment should be done at one time) | |
LocationTracker.this.longitude = qc.getLongitude(); | |
LocationTracker.this.latitude = qc.getLatitude(); | |
lastGPSRead = System.currentTimeMillis(); | |
} catch(Exception e) { log = e.toString() + log; } | |
} | |
else | |
{ | |
log = "location is not valid..\n" + log; | |
} | |
} | |
public void providerStateChanged(LocationProvider provider, int newState) { | |
// TODO: if provider was disabled, then disable reporting | |
log = "state changed: " + newState + "\n" + log; | |
LocationTracker.this.resetGPS(); | |
// try { | |
// provider.setLocationListener(null, 0, 0, 0); | |
// provider.reset(); | |
// provider = null; | |
// provider = LocationProvider.getInstance(new Criteria()); | |
// provider.setLocationListener(new MyLocationListener(), 10, -1, -1); | |
// } catch(Exception e) { log = e.toString() + log; } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment