forked from upstream-mirrors/OpenTracks
Improve MyTracksLocationManager thus LocationClient is only started when necessary.
This commit is contained in:
@@ -388,7 +388,7 @@ public class SearchListActivity extends AbstractSendToGoogleActivity
|
|||||||
setTitle(textQuery);
|
setTitle(textQuery);
|
||||||
|
|
||||||
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
||||||
this, Looper.myLooper());
|
this, Looper.myLooper(), true);
|
||||||
LocationListener locationListener = new LocationListener() {
|
LocationListener locationListener = new LocationListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(final Location location) {
|
public void onLocationChanged(final Location location) {
|
||||||
|
|||||||
@@ -478,7 +478,7 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
|
|||||||
return ApiAdapterFactory.getApiAdapter().handleSearchMenuSelection(this);
|
return ApiAdapterFactory.getApiAdapter().handleSearchMenuSelection(this);
|
||||||
case R.id.track_list_start_gps:
|
case R.id.track_list_start_gps:
|
||||||
MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
||||||
this, Looper.myLooper());
|
this, Looper.myLooper(), false);
|
||||||
if (!myTracksLocationManager.isGpsProviderEnabled()) {
|
if (!myTracksLocationManager.isGpsProviderEnabled()) {
|
||||||
intent = GoogleLocationUtils.isAvailable(TrackListActivity.this) ? new Intent(
|
intent = GoogleLocationUtils.isAvailable(TrackListActivity.this) ? new Intent(
|
||||||
GoogleLocationUtils.ACTION_GOOGLE_LOCATION_SETTINGS)
|
GoogleLocationUtils.ACTION_GOOGLE_LOCATION_SETTINGS)
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
||||||
getActivity(), Looper.myLooper());
|
getActivity(), Looper.myLooper(), true);
|
||||||
if (!myTracksLocationManager.isAllowed()) {
|
if (!myTracksLocationManager.isAllowed()) {
|
||||||
String setting = getString(
|
String setting = getString(
|
||||||
GoogleLocationUtils.isAvailable(getActivity()) ? R.string.gps_google_location_settings
|
GoogleLocationUtils.isAvailable(getActivity()) ? R.string.gps_google_location_settings
|
||||||
@@ -533,7 +533,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
TrackDataType.SAMPLED_OUT_TRACK_POINTS_TABLE, TrackDataType.PREFERENCE));
|
TrackDataType.SAMPLED_OUT_TRACK_POINTS_TABLE, TrackDataType.PREFERENCE));
|
||||||
|
|
||||||
MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
||||||
getActivity(), Looper.myLooper());
|
getActivity(), Looper.myLooper(), false);
|
||||||
boolean isGpsProviderEnabled = myTracksLocationManager.isGpsProviderEnabled();
|
boolean isGpsProviderEnabled = myTracksLocationManager.isGpsProviderEnabled();
|
||||||
|
|
||||||
if (googleMap != null) {
|
if (googleMap != null) {
|
||||||
|
|||||||
+16
-7
@@ -107,10 +107,10 @@ public class MyTracksLocationManager {
|
|||||||
private final Looper looper;
|
private final Looper looper;
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
private final LocationClient locationClient;
|
private final LocationClient locationClient;
|
||||||
|
|
||||||
private final LocationManager locationManager;
|
private final LocationManager locationManager;
|
||||||
private final ContentResolver contentResolver;
|
private final ContentResolver contentResolver;
|
||||||
private final GoogleSettingsObserver observer;
|
private final GoogleSettingsObserver observer;
|
||||||
|
|
||||||
private boolean isAvailable;
|
private boolean isAvailable;
|
||||||
private boolean isAllowed;
|
private boolean isAllowed;
|
||||||
private LocationListener requestLastLocation;
|
private LocationListener requestLastLocation;
|
||||||
@@ -118,15 +118,21 @@ public class MyTracksLocationManager {
|
|||||||
private float requestLocationUpdatesDistance;
|
private float requestLocationUpdatesDistance;
|
||||||
private long requestLocationUpdatesTime;
|
private long requestLocationUpdatesTime;
|
||||||
|
|
||||||
public MyTracksLocationManager(Context context, Looper looper) {
|
public MyTracksLocationManager(Context context, Looper looper, boolean enableLocaitonClient) {
|
||||||
this.looper = looper;
|
this.looper = looper;
|
||||||
handler = new Handler(looper);
|
handler = new Handler(looper);
|
||||||
locationClient = new LocationClient(context, connectionCallbacks, onConnectionFailedListener);
|
|
||||||
locationClient.connect();
|
if (enableLocaitonClient) {
|
||||||
|
locationClient = new LocationClient(context, connectionCallbacks, onConnectionFailedListener);
|
||||||
|
locationClient.connect();
|
||||||
|
} else {
|
||||||
|
locationClient = null;
|
||||||
|
}
|
||||||
|
|
||||||
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||||
contentResolver = context.getContentResolver();
|
contentResolver = context.getContentResolver();
|
||||||
observer = new GoogleSettingsObserver();
|
observer = new GoogleSettingsObserver();
|
||||||
|
|
||||||
isAvailable = GoogleLocationUtils.isAvailable(context);
|
isAvailable = GoogleLocationUtils.isAvailable(context);
|
||||||
isAllowed = isUseLocationForServicesOn();
|
isAllowed = isUseLocationForServicesOn();
|
||||||
|
|
||||||
@@ -138,7 +144,9 @@ public class MyTracksLocationManager {
|
|||||||
* Closes the {@link MyTracksLocationManager}.
|
* Closes the {@link MyTracksLocationManager}.
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
locationClient.disconnect();
|
if (locationClient != null) {
|
||||||
|
locationClient.disconnect();
|
||||||
|
}
|
||||||
contentResolver.unregisterContentObserver(observer);
|
contentResolver.unregisterContentObserver(observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +194,8 @@ public class MyTracksLocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests location updates.
|
* Requests location updates. This is an ongoing request, thus the caller
|
||||||
|
* needs to check the status of {@link #isAllowed}.
|
||||||
*
|
*
|
||||||
* @param minTime the minimal time
|
* @param minTime the minimal time
|
||||||
* @param minDistance the minimal distance
|
* @param minDistance the minimal distance
|
||||||
@@ -215,7 +224,7 @@ public class MyTracksLocationManager {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
requestLocationUpdates = null;
|
requestLocationUpdates = null;
|
||||||
if (locationClient.isConnected()) {
|
if (locationClient != null && locationClient.isConnected()) {
|
||||||
locationClient.removeLocationUpdates(locationListener);
|
locationClient.removeLocationUpdates(locationListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -285,7 +285,7 @@ public class TrackRecordingService extends Service {
|
|||||||
super.onCreate();
|
super.onCreate();
|
||||||
context = this;
|
context = this;
|
||||||
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
|
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
|
||||||
myTracksLocationManager = new MyTracksLocationManager(this, Looper.myLooper());
|
myTracksLocationManager = new MyTracksLocationManager(this, Looper.myLooper(), true);
|
||||||
activityRecognitionPendingIntent = PendingIntent.getService(context, 0,
|
activityRecognitionPendingIntent = PendingIntent.getService(context, 0,
|
||||||
new Intent(context, ActivityRecognitionIntentService.class),
|
new Intent(context, ActivityRecognitionIntentService.class),
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|||||||
Reference in New Issue
Block a user