use of android.location.LocationManager in project Osmand by osmandapp.
the class NavigationService method onDestroy.
@Override
public void onDestroy() {
super.onDestroy();
final OsmandApplication app = (OsmandApplication) getApplication();
app.setNavigationService(null);
usedBy = 0;
// remove updates
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.removeUpdates(this);
} catch (SecurityException e) {
// $NON-NLS-1$
Log.d(PlatformUtil.TAG, "Location service permission not granted");
}
if (!isContinuous()) {
WakeLock lock = getLock(this);
if (lock.isHeld()) {
lock.release();
}
}
if (pendingIntent != null) {
// remove alarm
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
// remove notification
stopForeground(Boolean.TRUE);
app.getNotificationHelper().updateTopNotification();
app.runInUIThread(new Runnable() {
@Override
public void run() {
app.getNotificationHelper().refreshNotifications();
}
}, 500);
}
use of android.location.LocationManager in project Osmand by osmandapp.
the class NavigationService method onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
final OsmandApplication app = (OsmandApplication) getApplication();
settings = app.getSettings();
usedBy = intent.getIntExtra(USAGE_INTENT, 0);
serviceOffInterval = intent.getIntExtra(USAGE_OFF_INTERVAL, 0);
if ((usedBy & USED_BY_NAVIGATION) != 0) {
serviceOffInterval = 0;
}
// use only gps provider
serviceOffProvider = LocationManager.GPS_PROVIDER;
serviceError = serviceOffInterval / 5;
// 1. not more than 12 mins
serviceError = Math.min(serviceError, 12 * 60 * 1000);
// 2. not less than 30 seconds
serviceError = Math.max(serviceError, 30 * 1000);
// 3. not more than serviceOffInterval
serviceError = Math.min(serviceError, serviceOffInterval);
locationProvider = app.getLocationProvider();
app.setNavigationService(this);
// requesting
if (isContinuous()) {
// request location updates
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(serviceOffProvider, 0, 0, NavigationService.this);
} catch (SecurityException e) {
Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show();
// $NON-NLS-1$
Log.d(PlatformUtil.TAG, "Location service permission not granted");
} catch (IllegalArgumentException e) {
Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show();
// $NON-NLS-1$
Log.d(PlatformUtil.TAG, "GPS location provider not available");
}
} else {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, OnNavigationServiceAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent);
}
// registering icon at top level
// Leave icon visible even for navigation for proper display
Notification notification = app.getNotificationHelper().buildTopNotification();
if (notification != null) {
startForeground(OsmandNotification.TOP_NOTIFICATION_SERVICE_ID, notification);
app.getNotificationHelper().refreshNotifications();
}
return START_REDELIVER_INTENT;
}
use of android.location.LocationManager in project Osmand by osmandapp.
the class OsmAndLocationProvider method resumeAllUpdates.
public void resumeAllUpdates() {
final LocationManager service = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE);
if (app.getSettings().isInternetConnectionAvailable()) {
if (System.currentTimeMillis() - app.getSettings().AGPS_DATA_LAST_TIME_DOWNLOADED.get() > AGPS_TO_REDOWNLOAD) {
// force an updated check for internet connectivity here before destroying A-GPS-data
if (app.getSettings().isInternetConnectionAvailable(true)) {
redownloadAGPS();
}
}
}
if (isLocationPermissionAvailable(app)) {
service.addGpsStatusListener(getGpsStatusListener(service));
try {
service.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_TIMEOUT_REQUEST, GPS_DIST_REQUEST, gpsListener);
} catch (IllegalArgumentException e) {
// $NON-NLS-1$
Log.d(PlatformUtil.TAG, "GPS location provider not available");
}
// try to always ask for network provide : it is faster way to find location
List<String> providers = service.getProviders(true);
if (providers == null) {
return;
}
for (String provider : providers) {
if (provider == null || provider.equals(LocationManager.GPS_PROVIDER)) {
continue;
}
try {
NetworkListener networkListener = new NetworkListener();
service.requestLocationUpdates(provider, GPS_TIMEOUT_REQUEST, GPS_DIST_REQUEST, networkListener);
networkListeners.add(networkListener);
} catch (IllegalArgumentException e) {
// $NON-NLS-1$
Log.d(PlatformUtil.TAG, provider + " location provider not available");
}
}
}
}
use of android.location.LocationManager in project Osmand by osmandapp.
the class OsmAndLocationProvider method redownloadAGPS.
public void redownloadAGPS() {
try {
final LocationManager service = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE);
service.sendExtraCommand(LocationManager.GPS_PROVIDER, "delete_aiding_data", null);
Bundle bundle = new Bundle();
service.sendExtraCommand("gps", "force_xtra_injection", bundle);
service.sendExtraCommand("gps", "force_time_injection", bundle);
app.getSettings().AGPS_DATA_LAST_TIME_DOWNLOADED.set(System.currentTimeMillis());
} catch (Exception e) {
app.getSettings().AGPS_DATA_LAST_TIME_DOWNLOADED.set(0L);
e.printStackTrace();
}
}
use of android.location.LocationManager in project Osmand by osmandapp.
the class SampleLocationProvider method getFirstTimeRunDefaultLocation.
public net.osmand.Location getFirstTimeRunDefaultLocation() {
if (!isLocationPermissionAvailable(app)) {
return null;
}
LocationManager service = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE);
List<String> ps = service.getProviders(true);
if (ps == null) {
return null;
}
List<String> providers = new ArrayList<String>(ps);
// note, passive provider is from API_LEVEL 8 but it is a constant, we can check for it.
// constant should not be changed in future
// LocationManager.PASSIVE_PROVIDER
int passiveFirst = providers.indexOf("passive");
// put passive provider to first place
if (passiveFirst > -1) {
providers.add(0, providers.remove(passiveFirst));
}
// find location
for (String provider : providers) {
net.osmand.Location location = convertLocation(service.getLastKnownLocation(provider), app);
if (location != null) {
return location;
}
}
return null;
}
Aggregations