forked from upstream-mirrors/OpenTracks
Removed LocationManagerConnector.
This commit is contained in:
@@ -1,67 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012 Google Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
||||||
* use this file except in compliance with the License. You may obtain a copy of
|
|
||||||
* the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
* License for the specific language governing permissions and limitations under
|
|
||||||
* the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.dennisguse.opentracks.services;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.location.LocationListener;
|
|
||||||
import android.location.LocationManager;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.Looper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles connection to {@link LocationManager}.
|
|
||||||
*
|
|
||||||
* @author Jimmy Shih
|
|
||||||
*/
|
|
||||||
public class LocationManagerConnector {
|
|
||||||
|
|
||||||
private final Handler handler;
|
|
||||||
private final LocationManager locationManager;
|
|
||||||
|
|
||||||
public LocationManagerConnector(Context context, Looper looper) {
|
|
||||||
this.handler = new Handler(looper);
|
|
||||||
|
|
||||||
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Requests location updates.
|
|
||||||
*
|
|
||||||
* @param minTime the minimal time
|
|
||||||
* @param minDistance the minimal distance
|
|
||||||
* @param locationListener the location listener
|
|
||||||
*/
|
|
||||||
public void requestLocationUpdates(final long minTime, final float minDistance, final LocationListener locationListener) {
|
|
||||||
handler.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
@SuppressLint("MissingPermission")
|
|
||||||
public void run() {
|
|
||||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes location updates.
|
|
||||||
*
|
|
||||||
* @param locationListener the location listener
|
|
||||||
*/
|
|
||||||
public void removeLocationUpdates(final LocationListener locationListener) {
|
|
||||||
locationManager.removeUpdates(locationListener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -29,7 +29,6 @@ import android.location.LocationListener;
|
|||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.PowerManager.WakeLock;
|
import android.os.PowerManager.WakeLock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -85,8 +84,7 @@ public class TrackRecordingService extends Service {
|
|||||||
// The following variables are set in onCreate:
|
// The following variables are set in onCreate:
|
||||||
private ExecutorService executorService;
|
private ExecutorService executorService;
|
||||||
private ContentProviderUtils contentProviderUtils;
|
private ContentProviderUtils contentProviderUtils;
|
||||||
private Handler handler;
|
private LocationManager locationManager;
|
||||||
private LocationManagerConnector locationManagerConnector;
|
|
||||||
private PeriodicTaskExecutor voiceExecutor;
|
private PeriodicTaskExecutor voiceExecutor;
|
||||||
private TrackRecordingServiceNotificationManager notificationManager;
|
private TrackRecordingServiceNotificationManager notificationManager;
|
||||||
private LocationListenerPolicy locationListenerPolicy;
|
private LocationListenerPolicy locationListenerPolicy;
|
||||||
@@ -156,10 +154,7 @@ public class TrackRecordingService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(final Location location) {
|
public void onLocationChanged(final Location location) {
|
||||||
if (locationManagerConnector == null
|
if (executorService == null || executorService.isShutdown() || executorService.isTerminated()) {
|
||||||
|| executorService == null
|
|
||||||
|| executorService.isShutdown()
|
|
||||||
|| executorService.isTerminated()) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
executorService.submit(new Runnable() {
|
executorService.submit(new Runnable() {
|
||||||
@@ -191,11 +186,9 @@ public class TrackRecordingService extends Service {
|
|||||||
super.onCreate();
|
super.onCreate();
|
||||||
executorService = Executors.newSingleThreadExecutor();
|
executorService = Executors.newSingleThreadExecutor();
|
||||||
contentProviderUtils = new ContentProviderUtils(this);
|
contentProviderUtils = new ContentProviderUtils(this);
|
||||||
handler = new Handler();
|
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||||
locationManagerConnector = new LocationManagerConnector(this, handler.getLooper());
|
|
||||||
voiceExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory());
|
voiceExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory());
|
||||||
|
|
||||||
|
|
||||||
notificationManager = new TrackRecordingServiceNotificationManager(this);
|
notificationManager = new TrackRecordingServiceNotificationManager(this);
|
||||||
|
|
||||||
// onSharedPreferenceChanged might not set recordingTrackId.
|
// onSharedPreferenceChanged might not set recordingTrackId.
|
||||||
@@ -238,6 +231,7 @@ public class TrackRecordingService extends Service {
|
|||||||
showNotification(false); //TODO Why?
|
showNotification(false); //TODO Why?
|
||||||
|
|
||||||
unregisterLocationListener();
|
unregisterLocationListener();
|
||||||
|
locationManager = null;
|
||||||
|
|
||||||
PreferencesUtils.unregister(this, sharedPreferenceChangeListener);
|
PreferencesUtils.unregister(this, sharedPreferenceChangeListener);
|
||||||
|
|
||||||
@@ -247,7 +241,6 @@ public class TrackRecordingService extends Service {
|
|||||||
voiceExecutor = null;
|
voiceExecutor = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
locationManagerConnector = null;
|
|
||||||
contentProviderUtils = null;
|
contentProviderUtils = null;
|
||||||
|
|
||||||
binder.detachFromService();
|
binder.detachFromService();
|
||||||
@@ -698,25 +691,25 @@ public class TrackRecordingService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void registerLocationListener() {
|
private void registerLocationListener() {
|
||||||
if (locationManagerConnector == null) {
|
if (locationManager == null) {
|
||||||
Log.e(TAG, "locationManager is null.");
|
Log.e(TAG, "locationManager is null.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
long interval = locationListenerPolicy.getDesiredPollingInterval();
|
long interval = locationListenerPolicy.getDesiredPollingInterval();
|
||||||
locationManagerConnector.requestLocationUpdates(interval, locationListenerPolicy.getMinDistance_m(), locationListener);
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, locationListenerPolicy.getMinDistance_m(), locationListener);
|
||||||
currentRecordingInterval = interval;
|
currentRecordingInterval = interval;
|
||||||
} catch (RuntimeException e) {
|
} catch (SecurityException e) {
|
||||||
Log.e(TAG, "Could not register location listener.", e);
|
Log.e(TAG, "Could not register location listener; permissions not granted.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unregisterLocationListener() {
|
private void unregisterLocationListener() {
|
||||||
if (locationManagerConnector == null) {
|
if (locationManager == null) {
|
||||||
Log.e(TAG, "locationManager is null.");
|
Log.e(TAG, "locationManager is null.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
locationManagerConnector.removeLocationUpdates(locationListener);
|
locationManager.removeUpdates(locationListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showNotification(boolean isGpsStarted) {
|
private void showNotification(boolean isGpsStarted) {
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
|
|||||||
@Override
|
@Override
|
||||||
public void startGps() {
|
public void startGps() {
|
||||||
trackRecordingService.tryStartGps();
|
trackRecordingService.tryStartGps();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopGps() {
|
public void stopGps() {
|
||||||
|
|||||||
Reference in New Issue
Block a user