Skip to content

Instantly share code, notes, and snippets.

@futtetennista
Created October 11, 2012 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futtetennista/3875489 to your computer and use it in GitHub Desktop.
Save futtetennista/3875489 to your computer and use it in GitHub Desktop.
Qype Location Manager
@Override
public boolean onIgnitedLocationChanged(Location location) {
return locationManager.onNewLocation(this, location, lastSearchedLocation);
}
public boolean onNewLocation(final QypeLocationAwareActivity context, Location newLocation,
Location lastSearchedLocation) {
...
// Check if the new location is too old, if so wait for a most recent location.
if (lastSearchedLocation == null) {
// Delete any pending API request with an old location and send a new request right
// away using the most up-to-date location.
if (locationHandler
.hasMessages(QypeNewLocationHandler.MSG_SEND_REQUEST_USING_UNRELIABLE_LOCATION)) {
locationHandler
.removeMessages(QypeNewLocationHandler.MSG_SEND_REQUEST_USING_UNRELIABLE_LOCATION);
}
Activity activity = (Activity) context;
// Wait some time if the location is too old, but if a new fix doesn't arrive send the
// request using the location we've got.
if (isOldLocation(newLocation)) {
DialogSupport.safeShowDialog(activity, R.id.ign_loc_dialog_wait_for_fix);
locationHandler.sendEmptyMessageDelayed(
QypeNewLocationHandler.MSG_SEND_REQUEST_USING_UNRELIABLE_LOCATION,
INTERVAL_SEND_REQUEST_USING_UNRELIABLE_LOCATION);
} else {
Dialog waitForLocationDialog = context.getWaitForLocationDialog();
if (waitForLocationDialog != null && waitForLocationDialog.isShowing()) {
activity.dismissDialog(R.id.ign_loc_dialog_wait_for_fix);
}
context.onLocationAvailable();
}
}
...
} else {
boolean diffDistanceTooBig = diffDistanceTooBig(newLocation, lastSearchedLocation);
if (diffDistanceTooBig) {
boolean hasBetterLocationMessages = locationHandler
.hasMessages(MSG_BETTER_LOCATION);
if (!hasBetterLocationMessages) {
// It's a better location, invite the user to refresh his location
locationHandler.obtainMessage(MSG_BETTER_LOCATION).sendToTarget();
} else {
locationHandler.removeMessages(MSG_BETTER_LOCATION);
Message msg = locationHandler.obtainMessage(MSG_BETTER_LOCATION);
locationHandler.sendMessageDelayed(msg,
QypeNewLocationHandler.DELAY_SHOW_BETTER_LOCATION_RELOAD_TOOLTIP);
}
}
}
...
...
return requestMoreLocationUpdates(newLocation);
}
private boolean requestMoreLocationUpdates(Location newLocation) {
double accuracy = newLocation.getAccuracy();
int desiredAccuracy = getDesiredAccuracy();
// Check if the new location is fresh and accurate enough.
return isOldLocation(newLocation) || accuracy > desiredAccuracy;
}
private boolean isOldLocation(Location location) {
long locationTime = location.getTime();
long timeDiff = getDesiredTimeDifference();
long desiredTime = System.currentTimeMillis() - timeDiff;
return locationTime < desiredTime;
}
private boolean requestMoreLocationUpdates(Location newLocation) {
double accuracy = newLocation.getAccuracy();
int desiredAccuracy = getDesiredAccuracy();
long locationTime = newLocation.getTime();
long timeDiff = getDesiredTimeDifference();
long desiredTime = System.currentTimeMillis() - timeDiff;
// Check if the new location is fresher, more accurate.
return locationTime < desiredTime || accuracy > desiredAccuracy;
}
private boolean isOldLocation(Location location) {
long locationTime = location.getTime();
long timeDiff = getDesiredTimeDifference();
long desiredTime = System.currentTimeMillis() - timeDiff;
return locationTime < desiredTime;
}
@Override
public void handleMessage(Message msg) {
ReloadTooltip reloadTooltip = context.getReloadTooltip();
switch (msg.what) {
case MSG_BETTER_LOCATION:
if (showReloadTooltip(reloadTooltip)) {
reloadTooltip.setText(R.string.msg_reload_results_on_new_location);
reloadTooltip.show(true);
sendEmptyMessageDelayed(MSG_HIDE_RELOAD_TOOLTIP, TIMEOUT_HIDE_RELOAD_TOOLTIP);
reloadTooltip.setTag(System.currentTimeMillis());
hasBetterLocation = false;
} else if (context.isLoadingData()) {
hasBetterLocation = true;
}
break;
case MSG_HIDE_RELOAD_TOOLTIP:
reloadTooltip.hide(true);
break;
case MSG_SEND_REQUEST_USING_UNRELIABLE_LOCATION:
Dialog waitForLocationDialog = context.getWaitForLocationDialog();
if (waitForLocationDialog != null && waitForLocationDialog.isShowing()) {
((Activity) context).dismissDialog(R.id.ign_loc_dialog_wait_for_fix);
}
context.onLocationAvailable();
break;
default:
super.handleMessage(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment