mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
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.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.util.Log;
|
||||
@@ -85,8 +84,7 @@ public class TrackRecordingService extends Service {
|
||||
// The following variables are set in onCreate:
|
||||
private ExecutorService executorService;
|
||||
private ContentProviderUtils contentProviderUtils;
|
||||
private Handler handler;
|
||||
private LocationManagerConnector locationManagerConnector;
|
||||
private LocationManager locationManager;
|
||||
private PeriodicTaskExecutor voiceExecutor;
|
||||
private TrackRecordingServiceNotificationManager notificationManager;
|
||||
private LocationListenerPolicy locationListenerPolicy;
|
||||
@@ -156,10 +154,7 @@ public class TrackRecordingService extends Service {
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(final Location location) {
|
||||
if (locationManagerConnector == null
|
||||
|| executorService == null
|
||||
|| executorService.isShutdown()
|
||||
|| executorService.isTerminated()) {
|
||||
if (executorService == null || executorService.isShutdown() || executorService.isTerminated()) {
|
||||
return;
|
||||
}
|
||||
executorService.submit(new Runnable() {
|
||||
@@ -191,11 +186,9 @@ public class TrackRecordingService extends Service {
|
||||
super.onCreate();
|
||||
executorService = Executors.newSingleThreadExecutor();
|
||||
contentProviderUtils = new ContentProviderUtils(this);
|
||||
handler = new Handler();
|
||||
locationManagerConnector = new LocationManagerConnector(this, handler.getLooper());
|
||||
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
voiceExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory());
|
||||
|
||||
|
||||
notificationManager = new TrackRecordingServiceNotificationManager(this);
|
||||
|
||||
// onSharedPreferenceChanged might not set recordingTrackId.
|
||||
@@ -238,6 +231,7 @@ public class TrackRecordingService extends Service {
|
||||
showNotification(false); //TODO Why?
|
||||
|
||||
unregisterLocationListener();
|
||||
locationManager = null;
|
||||
|
||||
PreferencesUtils.unregister(this, sharedPreferenceChangeListener);
|
||||
|
||||
@@ -247,7 +241,6 @@ public class TrackRecordingService extends Service {
|
||||
voiceExecutor = null;
|
||||
}
|
||||
|
||||
locationManagerConnector = null;
|
||||
contentProviderUtils = null;
|
||||
|
||||
binder.detachFromService();
|
||||
@@ -698,25 +691,25 @@ public class TrackRecordingService extends Service {
|
||||
}
|
||||
|
||||
private void registerLocationListener() {
|
||||
if (locationManagerConnector == null) {
|
||||
if (locationManager == null) {
|
||||
Log.e(TAG, "locationManager is null.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
long interval = locationListenerPolicy.getDesiredPollingInterval();
|
||||
locationManagerConnector.requestLocationUpdates(interval, locationListenerPolicy.getMinDistance_m(), locationListener);
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, locationListenerPolicy.getMinDistance_m(), locationListener);
|
||||
currentRecordingInterval = interval;
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "Could not register location listener.", e);
|
||||
} catch (SecurityException e) {
|
||||
Log.e(TAG, "Could not register location listener; permissions not granted.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void unregisterLocationListener() {
|
||||
if (locationManagerConnector == null) {
|
||||
if (locationManager == null) {
|
||||
Log.e(TAG, "locationManager is null.");
|
||||
return;
|
||||
}
|
||||
locationManagerConnector.removeLocationUpdates(locationListener);
|
||||
locationManager.removeUpdates(locationListener);
|
||||
}
|
||||
|
||||
private void showNotification(boolean isGpsStarted) {
|
||||
|
||||
@@ -25,7 +25,6 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
|
||||
@Override
|
||||
public void startGps() {
|
||||
trackRecordingService.tryStartGps();
|
||||
|
||||
}
|
||||
|
||||
public void stopGps() {
|
||||
|
||||
Reference in New Issue
Block a user