use of android.location.Location in project android_frameworks_base by ResurrectionRemix.
the class LocationFudger method createCoarseLocked.
/**
* Create a coarse location.
*
* <p>Two techniques are used: random offsets and snap-to-grid.
*
* <p>First we add a random offset. This mitigates against detecting
* grid transitions. Without a random offset it is possible to detect
* a users position very accurately when they cross a grid boundary.
* The random offset changes very slowly over time, to mitigate against
* taking many location samples and averaging them out.
*
* <p>Second we snap-to-grid (quantize). This has the nice property of
* producing stable results, and mitigating against taking many samples
* to average out a random offset.
*/
private Location createCoarseLocked(Location fine) {
Location coarse = new Location(fine);
// clean all the optional information off the location, because
// this can leak detailed location information
coarse.removeBearing();
coarse.removeSpeed();
coarse.removeAltitude();
coarse.setExtras(null);
double lat = coarse.getLatitude();
double lon = coarse.getLongitude();
// wrap
lat = wrapLatitude(lat);
lon = wrapLongitude(lon);
// Step 1) apply a random offset
//
// The goal of the random offset is to prevent the application
// from determining that the device is on a grid boundary
// when it crosses from one grid to the next.
//
// We apply the offset even if the location already claims to be
// inaccurate, because it may be more accurate than claimed.
updateRandomOffsetLocked();
// perform lon first whilst lat is still within bounds
lon += metersToDegreesLongitude(mOffsetLongitudeMeters, lat);
lat += metersToDegreesLatitude(mOffsetLatitudeMeters);
if (D)
Log.d(TAG, String.format("applied offset of %.0f, %.0f (meters)", mOffsetLongitudeMeters, mOffsetLatitudeMeters));
// wrap
lat = wrapLatitude(lat);
lon = wrapLongitude(lon);
// Step 2) Snap-to-grid (quantize)
//
// This is the primary means of obfuscation. It gives nice consistent
// results and is very effective at hiding the true location
// (as long as you are not sitting on a grid boundary, which
// step 1 mitigates).
//
// Note we quantize the latitude first, since the longitude
// quantization depends on the latitude value and so leaks information
// about the latitude
double latGranularity = metersToDegreesLatitude(mGridSizeInMeters);
lat = Math.round(lat / latGranularity) * latGranularity;
double lonGranularity = metersToDegreesLongitude(mGridSizeInMeters, lat);
lon = Math.round(lon / lonGranularity) * lonGranularity;
// wrap again
lat = wrapLatitude(lat);
lon = wrapLongitude(lon);
// apply
coarse.setLatitude(lat);
coarse.setLongitude(lon);
coarse.setAccuracy(Math.max(mAccuracyInMeters, coarse.getAccuracy()));
if (D)
Log.d(TAG, "fudged " + fine + " to " + coarse);
return coarse;
}
use of android.location.Location in project android_frameworks_base by ResurrectionRemix.
the class LocationFudger method addCoarseLocationExtraLocked.
private Location addCoarseLocationExtraLocked(Location location) {
Location coarse = createCoarseLocked(location);
location.setExtraLocation(Location.EXTRA_COARSE_LOCATION, coarse);
return coarse;
}
use of android.location.Location in project android_frameworks_base by ResurrectionRemix.
the class LocationBasedCountryDetector method detectCountry.
/**
* Start detecting the country.
* <p>
* Queries the location from all location providers, then starts a thread to query the
* country from GeoCoder.
*/
@Override
public synchronized Country detectCountry() {
if (mLocationListeners != null) {
throw new IllegalStateException();
}
// Request the location from all enabled providers.
List<String> enabledProviders = getEnabledProviders();
int totalProviders = enabledProviders.size();
if (totalProviders > 0) {
mLocationListeners = new ArrayList<LocationListener>(totalProviders);
for (int i = 0; i < totalProviders; i++) {
String provider = enabledProviders.get(i);
if (isAcceptableProvider(provider)) {
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
LocationBasedCountryDetector.this.stop();
queryCountryCode(location);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
mLocationListeners.add(listener);
registerListener(provider, listener);
}
}
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
mTimer = null;
LocationBasedCountryDetector.this.stop();
// Looks like no provider could provide the location, let's try the last
// known location.
queryCountryCode(getLastKnownLocation());
}
}, getQueryLocationTimeout());
} else {
// There is no provider enabled.
queryCountryCode(getLastKnownLocation());
}
return mDetectedCountry;
}
use of android.location.Location in project android_frameworks_base by ResurrectionRemix.
the class LocationBasedCountryDetector method getLastKnownLocation.
/**
* @return the last known location from all providers
*/
protected Location getLastKnownLocation() {
final long bid = Binder.clearCallingIdentity();
try {
List<String> providers = mLocationManager.getAllProviders();
Location bestLocation = null;
for (String provider : providers) {
Location lastKnownLocation = mLocationManager.getLastKnownLocation(provider);
if (lastKnownLocation != null) {
if (bestLocation == null || bestLocation.getElapsedRealtimeNanos() < lastKnownLocation.getElapsedRealtimeNanos()) {
bestLocation = lastKnownLocation;
}
}
}
return bestLocation;
} finally {
Binder.restoreCallingIdentity(bid);
}
}
use of android.location.Location in project android_frameworks_base by ResurrectionRemix.
the class TwilightService method updateTwilightState.
private void updateTwilightState() {
// Calculate the twilight state based on the current time and location.
final long currentTimeMillis = System.currentTimeMillis();
final Location location = mLastLocation != null ? mLastLocation : mLocationManager.getLastLocation();
final TwilightState state = calculateTwilightState(location, currentTimeMillis);
if (DEBUG) {
Slog.d(TAG, "updateTwilightState: " + state);
}
// Notify listeners if the state has changed.
synchronized (mListeners) {
if (!Objects.equals(mLastTwilightState, state)) {
mLastTwilightState = state;
for (int i = mListeners.size() - 1; i >= 0; --i) {
final TwilightListener listener = mListeners.keyAt(i);
final Handler handler = mListeners.valueAt(i);
handler.post(new Runnable() {
@Override
public void run() {
listener.onTwilightStateChanged(state);
}
});
}
}
}
// Schedule an alarm to update the state at the next sunrise or sunset.
if (state != null) {
final long triggerAtMillis = state.isNight() ? state.sunriseTimeMillis() : state.sunsetTimeMillis();
mAlarmManager.setExact(AlarmManager.RTC, triggerAtMillis, TAG, this, mHandler);
}
}
Aggregations