use of net.rim.device.api.gps.BlackBerryLocation in project Samples-for-Java by blackberry.
the class SimpleLocationProvider method locationUpdated.
/********************* END OF PRIVATE METHODS **********************/
/******************************** LocationListener IMPLEMENTATION ***************************************/
/**
* This is used by this API internally and must NOT be called.
*/
public void locationUpdated(LocationProvider provider, Location location) {
if (provider != this.locationProviderReference) {
// if this is an update from an old location provider that is in the process of being stopped, ignore the update.
return;
}
BlackBerryLocation bbLocation = (BlackBerryLocation) location;
if (bbLocation != null && bbLocation.isValid() && (bbLocation.getQualifiedCoordinates().getLatitude() != 0 && bbLocation.getQualifiedCoordinates().getLongitude() != 0)) {
log("Tracking: Acquired valid location - " + location.getQualifiedCoordinates().getLatitude() + ", " + location.getQualifiedCoordinates().getLongitude());
this.location = (BlackBerryLocation) location;
/**
* retryAttempt should not be reset in MODE_OPTIMAL when currentOptimalModeIsGeolocation
* because last GPS fix might have failed and this is only a fall back to Geolocation that has passed.
* We still need to preserve the retryAttempt until a GPS fix works in MODE_OPTIMAL. Otherwise
* we will simply waste battery by trying GPS fixes too frequently when it is not available.
*/
if (mode != MODE_OPTIMAL || (mode == MODE_OPTIMAL && !currentOptimalModeIsGeolocation)) {
retryAttempt = 0;
log("Tracking: retryAttempt reset to 0");
}
/** Update lastValidFixTime */
lastValidFixTime = System.currentTimeMillis();
log("Tracking: lastValidFixTime updated.");
/** Trigger events via SimpleLocationListener */
if (bbLocation.getGPSMode() == GPSInfo.GPS_MODE_AUTONOMOUS) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_GPS_LOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_GPS_LOCATION");
} else //#ifdef BlackBerrySDK5.0.0
if (bbLocation.getGPSMode() == GPSInfo.GPS_MODE_CELLSITE) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_CELL_GEOLOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_CELL_GEOLOCATION");
} else //#ifdef BlackBerrySDK6.0.0
if (bbLocation.getGPSMode() == LocationInfo.GEOLOCATION_MODE_CELL) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_CELL_GEOLOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_CELL_GEOLOCATION");
} else if (bbLocation.getGPSMode() == LocationInfo.GEOLOCATION_MODE_WLAN) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_WLAN_GEOLOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_WLAN_GEOLOCATION");
} else //#endif
{
// This should not occur
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_UNKNOWN_MODE, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_UNKNOWN_MODE");
}
/** Create a TimerTask to switch to MODE_GPS when appropriate */
if (mode == MODE_OPTIMAL && currentOptimalModeIsGeolocation && isModeAvailable(MODE_GPS)) {
if (restartTimer == null) {
log("Tracking: Scheduling switch to GPS");
switchToGPS = true;
restartTimer = new Timer();
restartTask = new RestartTask();
long nextRetry = getNextRetryDelay();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: GPS will be attempted after " + (nextRetry / 1000) + " seconds.");
}
}
} else {
log("Tracking: Invalid fix.");
/** If Geolocation fails in MODE_OPTIMAL, try GPS (if available) after waiting an appropriate interval */
if (mode == MODE_OPTIMAL && currentOptimalModeIsGeolocation && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > geolocationTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
if (!isModeAvailable(MODE_GPS)) {
currentOptimalModeIsGeolocation = true;
log("Cannot switch to GPS because MODE_GPS is not available.");
} else {
currentOptimalModeIsGeolocation = false;
log("Switching to GPS");
}
long nextRetry = getNextRetryDelay();
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: Next attempt after " + (nextRetry / 1000) + " seconds.");
} else /** If GPS fails in MODE_OPTIMAL, try Geolocation (if available) immediately */
if (mode == MODE_OPTIMAL && !currentOptimalModeIsGeolocation && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > gpsTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
long nextRetry = 0;
if (!isModeAvailable(MODE_GEOLOCATION)) {
currentOptimalModeIsGeolocation = false;
nextRetry = getNextRetryDelay();
log("Cannot switch to Geolocation because MODE_GEOLOCATION is not available.");
} else {
currentOptimalModeIsGeolocation = true;
nextRetry = 0;
log("Switching to Geolocation.");
}
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: Next attempt after " + (nextRetry / 1000) + " seconds.");
} else /** If Geolocation fails, retry after waiting an appropriate interval */
if ((mode == MODE_GEOLOCATION || //#ifdef BlackBerrySDK6.0.0
mode == MODE_GEOLOCATION_CELL || mode == MODE_GEOLOCATION_WLAN) && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > geolocationTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
long nextRetry = getNextRetryDelay();
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: Geolocation will be attempted after " + (nextRetry / 1000) + " seconds.");
} else /** If GPS fails, retry after waiting an appropriate interval */
if (mode == MODE_GPS && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > gpsTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
long nextRetry = getNextRetryDelay();
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: GPS will be attempted after " + (nextRetry / 1000) + " seconds.");
}
}
}
Aggregations