use of android.location.LocationRequest in project android_frameworks_base by ResurrectionRemix.
the class GeofenceManager method updateFences.
/**
* The geofence update loop. This function removes expired fences, then tests the most
* recently-received {@link Location} against each registered {@link GeofenceState}, sending
* {@link Intent}s for geofences that have been tripped. It also adjusts the active location
* update request with {@link LocationManager} as appropriate for any active geofences.
*/
// Runs on the handler.
private void updateFences() {
List<PendingIntent> enterIntents = new LinkedList<PendingIntent>();
List<PendingIntent> exitIntents = new LinkedList<PendingIntent>();
synchronized (mLock) {
mPendingUpdate = false;
// Remove expired fences.
removeExpiredFencesLocked();
// Get a location to work with, either received via onLocationChanged() or
// via LocationManager.getLastLocation().
Location location = getFreshLocationLocked();
// Update all fences.
// Keep track of the distance to the nearest fence.
double minFenceDistance = Double.MAX_VALUE;
boolean needUpdates = false;
for (GeofenceState state : mFences) {
if (mBlacklist.isBlacklisted(state.mPackageName)) {
if (D) {
Slog.d(TAG, "skipping geofence processing for blacklisted app: " + state.mPackageName);
}
continue;
}
int op = LocationManagerService.resolutionLevelToOp(state.mAllowedResolutionLevel);
if (op >= 0) {
if (mAppOps.noteOpNoThrow(AppOpsManager.OP_FINE_LOCATION, state.mUid, state.mPackageName) != AppOpsManager.MODE_ALLOWED) {
if (D) {
Slog.d(TAG, "skipping geofence processing for no op app: " + state.mPackageName);
}
continue;
}
}
needUpdates = true;
if (location != null) {
int event = state.processLocation(location);
if ((event & GeofenceState.FLAG_ENTER) != 0) {
enterIntents.add(state.mIntent);
}
if ((event & GeofenceState.FLAG_EXIT) != 0) {
exitIntents.add(state.mIntent);
}
// FIXME: Ideally this code should take into account the accuracy of the
// location fix that was used to calculate the distance in the first place.
// MAX_VALUE if unknown
double fenceDistance = state.getDistanceToBoundary();
if (fenceDistance < minFenceDistance) {
minFenceDistance = fenceDistance;
}
}
}
// Request or cancel location updates if needed.
if (needUpdates) {
// Request location updates.
// Compute a location update interval based on the distance to the nearest fence.
long intervalMs;
if (location != null && Double.compare(minFenceDistance, Double.MAX_VALUE) != 0) {
intervalMs = (long) Math.min(MAX_INTERVAL_MS, Math.max(MIN_INTERVAL_MS, minFenceDistance * 1000 / MAX_SPEED_M_S));
} else {
intervalMs = MIN_INTERVAL_MS;
}
if (!mReceivingLocationUpdates || mLocationUpdateInterval != intervalMs) {
mReceivingLocationUpdates = true;
mLocationUpdateInterval = intervalMs;
mLastLocationUpdate = location;
LocationRequest request = new LocationRequest();
request.setInterval(intervalMs).setFastestInterval(0);
mLocationManager.requestLocationUpdates(request, this, mHandler.getLooper());
}
} else {
// Cancel location updates.
if (mReceivingLocationUpdates) {
mReceivingLocationUpdates = false;
mLocationUpdateInterval = 0;
mLastLocationUpdate = null;
mLocationManager.removeUpdates(this);
}
}
if (D) {
Slog.d(TAG, "updateFences: location=" + location + ", mFences.size()=" + mFences.size() + ", mReceivingLocationUpdates=" + mReceivingLocationUpdates + ", mLocationUpdateInterval=" + mLocationUpdateInterval + ", mLastLocationUpdate=" + mLastLocationUpdate);
}
}
// release lock before sending intents
for (PendingIntent intent : exitIntents) {
sendIntentExit(intent);
}
for (PendingIntent intent : enterIntents) {
sendIntentEnter(intent);
}
}
use of android.location.LocationRequest in project android_frameworks_base by ResurrectionRemix.
the class LocationManagerService method requestLocationUpdates.
@Override
public void requestLocationUpdates(LocationRequest request, ILocationListener listener, PendingIntent intent, String packageName) {
if (request == null)
request = DEFAULT_LOCATION_REQUEST;
checkPackageName(packageName);
int allowedResolutionLevel = getCallerAllowedResolutionLevel();
checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel, request.getProvider());
WorkSource workSource = request.getWorkSource();
if (workSource != null && workSource.size() > 0) {
checkDeviceStatsAllowed();
}
boolean hideFromAppOps = request.getHideFromAppOps();
if (hideFromAppOps) {
checkUpdateAppOpsAllowed();
}
LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
final int pid = Binder.getCallingPid();
final int uid = Binder.getCallingUid();
// providers may use public location API's, need to clear identity
long identity = Binder.clearCallingIdentity();
try {
// We don't check for MODE_IGNORED here; we will do that when we go to deliver
// a location.
checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
synchronized (mLock) {
Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid, packageName, workSource, hideFromAppOps);
requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
}
} finally {
Binder.restoreCallingIdentity(identity);
}
}
use of android.location.LocationRequest in project android_frameworks_base by ResurrectionRemix.
the class LocationManagerService method applyRequirementsLocked.
private void applyRequirementsLocked(String provider) {
LocationProviderInterface p = mProvidersByName.get(provider);
if (p == null)
return;
ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
WorkSource worksource = new WorkSource();
ProviderRequest providerRequest = new ProviderRequest();
if (records != null) {
for (UpdateRecord record : records) {
if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
if (checkLocationAccess(record.mReceiver.mPid, record.mReceiver.mUid, record.mReceiver.mPackageName, record.mReceiver.mAllowedResolutionLevel)) {
LocationRequest locationRequest = record.mRequest;
providerRequest.locationRequests.add(locationRequest);
if (locationRequest.getInterval() < providerRequest.interval) {
providerRequest.reportLocation = true;
providerRequest.interval = locationRequest.getInterval();
}
}
}
}
if (providerRequest.reportLocation) {
// calculate who to blame for power
// This is somewhat arbitrary. We pick a threshold interval
// that is slightly higher that the minimum interval, and
// spread the blame across all applications with a request
// under that threshold.
long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
for (UpdateRecord record : records) {
if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mUid))) {
LocationRequest locationRequest = record.mRequest;
// client has no permission to receive location data.
if (!providerRequest.locationRequests.contains(locationRequest)) {
continue;
}
if (locationRequest.getInterval() <= thresholdInterval) {
if (record.mReceiver.mWorkSource != null && record.mReceiver.mWorkSource.size() > 0 && record.mReceiver.mWorkSource.getName(0) != null) {
// Assign blame to another work source.
// Can only assign blame if the WorkSource contains names.
worksource.add(record.mReceiver.mWorkSource);
} else {
// Assign blame to caller.
worksource.add(record.mReceiver.mUid, record.mReceiver.mPackageName);
}
}
}
}
}
}
if (D)
Log.d(TAG, "provider request: " + provider + " " + providerRequest);
p.setRequest(providerRequest, worksource);
}
use of android.location.LocationRequest in project android_frameworks_base by crdroidandroid.
the class LocationManagerService method requestLocationUpdates.
@Override
public void requestLocationUpdates(LocationRequest request, ILocationListener listener, PendingIntent intent, String packageName) {
if (request == null)
request = DEFAULT_LOCATION_REQUEST;
checkPackageName(packageName);
int allowedResolutionLevel = getCallerAllowedResolutionLevel();
checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel, request.getProvider());
WorkSource workSource = request.getWorkSource();
if (workSource != null && workSource.size() > 0) {
checkDeviceStatsAllowed();
}
boolean hideFromAppOps = request.getHideFromAppOps();
if (hideFromAppOps) {
checkUpdateAppOpsAllowed();
}
LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel);
final int pid = Binder.getCallingPid();
final int uid = Binder.getCallingUid();
// providers may use public location API's, need to clear identity
long identity = Binder.clearCallingIdentity();
try {
// We don't check for MODE_IGNORED here; we will do that when we go to deliver
// a location.
checkLocationAccess(pid, uid, packageName, allowedResolutionLevel);
synchronized (mLock) {
Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid, packageName, workSource, hideFromAppOps);
requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName);
}
} finally {
Binder.restoreCallingIdentity(identity);
}
}
use of android.location.LocationRequest in project android_frameworks_base by crdroidandroid.
the class ProviderRequest method writeToParcel.
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(reportLocation ? 1 : 0);
parcel.writeLong(interval);
parcel.writeInt(locationRequests.size());
for (LocationRequest request : locationRequests) {
request.writeToParcel(parcel, flags);
}
}
Aggregations