Created
October 11, 2012 21:07
-
-
Save futtetennista/3875489 to your computer and use it in GitHub Desktop.
Qype Location Manager
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
@Override | |
public boolean onIgnitedLocationChanged(Location location) { | |
return locationManager.onNewLocation(this, location, lastSearchedLocation); | |
} |
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
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(); | |
} | |
} |
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
... | |
} 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); | |
} | |
} | |
} | |
... |
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
... | |
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; | |
} |
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
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; | |
} |
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
@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