forked from upstream-mirrors/OpenTracks
Bugfix: NPE fixed because listeners/client were null in some cases.
TrackRecordingService: before destroying it sends GPS Status NONE to all listeners. Cleanup Logs used for debugging.
This commit is contained in:
@@ -94,16 +94,6 @@ public class TrackRecordingActivity extends AbstractActivity implements ChooseAc
|
|||||||
trackController.update(true, false);
|
trackController.update(true, false);
|
||||||
trackController.onResume(true, recordingTrackPaused);
|
trackController.onResume(true, recordingTrackPaused);
|
||||||
}
|
}
|
||||||
service.addListener(new TrackRecordingServiceCallback() {
|
|
||||||
@Override
|
|
||||||
public void onGpsStatusChange(GpsStatusValue newStatus) {
|
|
||||||
// TODO 2020-07-17 Add some visible View in the Layout to inform about this and then delete the Toast message.
|
|
||||||
// Inform through Toast the bad news: GPS is not fixed.
|
|
||||||
if (newStatus == GpsStatusValue.GPS_NONE || newStatus == GpsStatusValue.GPS_DISABLED || newStatus == GpsStatusValue.GPS_SIGNAL_LOST) {
|
|
||||||
Toast.makeText(getApplicationContext(), getString(newStatus.message), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -158,14 +158,17 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
handlerServer.stop(this);
|
|
||||||
handlerServer = null;
|
|
||||||
|
|
||||||
if (listeners != null) {
|
if (listeners != null) {
|
||||||
|
for (TrackRecordingServiceCallback listener : listeners) {
|
||||||
|
listener.onGpsStatusChange(GpsStatusValue.GPS_NONE);
|
||||||
|
}
|
||||||
listeners.clear();
|
listeners.clear();
|
||||||
listeners = null;
|
listeners = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handlerServer.stop(this);
|
||||||
|
handlerServer = null;
|
||||||
|
|
||||||
if (remoteSensorManager != null) {
|
if (remoteSensorManager != null) {
|
||||||
remoteSensorManager.stop();
|
remoteSensorManager.stop();
|
||||||
remoteSensorManager = null;
|
remoteSensorManager = null;
|
||||||
@@ -595,9 +598,11 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void newGpsStatus(GpsStatusValue gpsStatusValue) {
|
public void newGpsStatus(GpsStatusValue gpsStatusValue) {
|
||||||
notificationManager.updateContent(getString(gpsStatusValue.message));
|
if (listeners != null) {
|
||||||
for (TrackRecordingServiceCallback listener : listeners) {
|
notificationManager.updateContent(getString(gpsStatusValue.message));
|
||||||
listener.onGpsStatusChange(gpsStatusValue);
|
for (TrackRecordingServiceCallback listener : listeners) {
|
||||||
|
listener.onGpsStatusChange(gpsStatusValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.content.Context;
|
|||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||||
import de.dennisguse.opentracks.util.UnitConversions;
|
import de.dennisguse.opentracks.util.UnitConversions;
|
||||||
@@ -12,13 +11,12 @@ import de.dennisguse.opentracks.util.UnitConversions;
|
|||||||
/**
|
/**
|
||||||
* This class handle GPS status according to received locations and some thresholds.
|
* This class handle GPS status according to received locations and some thresholds.
|
||||||
*/
|
*/
|
||||||
// TODO 2020-07-17 Delete all Log.d messages before merge with main branch. For now it's useful for debugging.
|
|
||||||
class GpsStatus {
|
class GpsStatus {
|
||||||
|
|
||||||
private static final String TAG = GpsStatus.class.getSimpleName();
|
private static final String TAG = GpsStatus.class.getSimpleName();
|
||||||
|
|
||||||
// The quantity of milliseconds that GpsStatus waits from minimal interval to consider GPS lost.
|
// The quantity of milliseconds that GpsStatus waits from minimal interval to consider GPS lost.
|
||||||
private static final int SIGNAL_LOST_THRESHOLD = 10000;
|
private static final int SIGNAL_LOST_THRESHOLD = (int) (10 * UnitConversions.S_TO_MS);
|
||||||
|
|
||||||
// Threshold for accuracy.
|
// Threshold for accuracy.
|
||||||
private double signalBadThreshold;
|
private double signalBadThreshold;
|
||||||
@@ -28,7 +26,10 @@ class GpsStatus {
|
|||||||
private GpsStatusValue gpsStatus = GpsStatusValue.GPS_NONE;
|
private GpsStatusValue gpsStatus = GpsStatusValue.GPS_NONE;
|
||||||
private GpsStatusListener client;
|
private GpsStatusListener client;
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
|
// Last location. It can be null.
|
||||||
private Location lastLocation = null;
|
private Location lastLocation = null;
|
||||||
|
// The last valid (not null) location. Null value means that there have not been any location yet.
|
||||||
private Location lastValidLocation = null;
|
private Location lastValidLocation = null;
|
||||||
|
|
||||||
private class GpsStatusRunner implements Runnable {
|
private class GpsStatusRunner implements Runnable {
|
||||||
@@ -67,10 +68,10 @@ class GpsStatus {
|
|||||||
* The client that uses GpsStatus has to call this method to stop the Runnable if needed.
|
* The client that uses GpsStatus has to call this method to stop the Runnable if needed.
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
client = null;
|
||||||
if (gpsStatusRunner != null) {
|
if (gpsStatusRunner != null) {
|
||||||
gpsStatusRunner.stop();
|
gpsStatusRunner.stop();
|
||||||
gpsStatusRunner = null;
|
gpsStatusRunner = null;
|
||||||
client = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,24 +120,21 @@ class GpsStatus {
|
|||||||
private void checkStatusFromLastLocation() {
|
private void checkStatusFromLastLocation() {
|
||||||
if (System.currentTimeMillis() - lastLocation.getTime() > signalLostThreshold && gpsStatus != GpsStatusValue.GPS_SIGNAL_LOST) {
|
if (System.currentTimeMillis() - lastLocation.getTime() > signalLostThreshold && gpsStatus != GpsStatusValue.GPS_SIGNAL_LOST) {
|
||||||
// So much time without receiving signal -> signal lost.
|
// So much time without receiving signal -> signal lost.
|
||||||
Log.d(TAG, "Signal LOST. signalLostThreshold: " + signalLostThreshold + " - System.currentTimeMillis() - lastLocation.getTime() > " + (System.currentTimeMillis() - lastLocation.getTime()));
|
|
||||||
GpsStatusValue oldStatus = gpsStatus;
|
GpsStatusValue oldStatus = gpsStatus;
|
||||||
gpsStatus = GpsStatusValue.GPS_SIGNAL_LOST;
|
gpsStatus = GpsStatusValue.GPS_SIGNAL_LOST;
|
||||||
client.onGpsStatusChanged(oldStatus, gpsStatus);
|
sendStatus(oldStatus, gpsStatus);
|
||||||
stopStatusRunner();
|
stopStatusRunner();
|
||||||
} else if (lastLocation.getAccuracy() > signalBadThreshold && gpsStatus != GpsStatusValue.GPS_SIGNAL_BAD) {
|
} else if (lastLocation.getAccuracy() > signalBadThreshold && gpsStatus != GpsStatusValue.GPS_SIGNAL_BAD) {
|
||||||
// Too little accuracy -> bad signal.
|
// Too little accuracy -> bad signal.
|
||||||
Log.d(TAG, "Signal BAD. signalBadThreshold: " + signalBadThreshold + " - lastLocation.getAccuracy() = " + lastLocation.getAccuracy());
|
|
||||||
GpsStatusValue oldStatus = gpsStatus;
|
GpsStatusValue oldStatus = gpsStatus;
|
||||||
gpsStatus = GpsStatusValue.GPS_SIGNAL_BAD;
|
gpsStatus = GpsStatusValue.GPS_SIGNAL_BAD;
|
||||||
client.onGpsStatusChanged(oldStatus, gpsStatus);
|
sendStatus(oldStatus, gpsStatus);
|
||||||
startStatusRunner();
|
startStatusRunner();
|
||||||
} else if (lastLocation.getAccuracy() <= signalBadThreshold && gpsStatus != GpsStatusValue.GPS_SIGNAL_FIX) {
|
} else if (lastLocation.getAccuracy() <= signalBadThreshold && gpsStatus != GpsStatusValue.GPS_SIGNAL_FIX) {
|
||||||
Log.d(TAG, "Signal FIX. signalBadThreshold: " + signalBadThreshold + " - lastLocation.getAccuracy() = " + lastLocation.getAccuracy());
|
|
||||||
// Gps okay.
|
// Gps okay.
|
||||||
GpsStatusValue oldStatus = gpsStatus;
|
GpsStatusValue oldStatus = gpsStatus;
|
||||||
gpsStatus = GpsStatusValue.GPS_SIGNAL_FIX;
|
gpsStatus = GpsStatusValue.GPS_SIGNAL_FIX;
|
||||||
client.onGpsStatusChanged(oldStatus, gpsStatus);
|
sendStatus(oldStatus, gpsStatus);
|
||||||
startStatusRunner();
|
startStatusRunner();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,10 +147,9 @@ class GpsStatus {
|
|||||||
private void checkStatusFromLastValidLocation() {
|
private void checkStatusFromLastValidLocation() {
|
||||||
if (System.currentTimeMillis() - lastValidLocation.getTime() > signalLostThreshold) {
|
if (System.currentTimeMillis() - lastValidLocation.getTime() > signalLostThreshold) {
|
||||||
// Too much time without locations -> lost signal? (wait signalLostThreshold from last valid location).
|
// Too much time without locations -> lost signal? (wait signalLostThreshold from last valid location).
|
||||||
Log.d(TAG, "Signal LOST. signalLostThreshold: " + signalLostThreshold + " - System.currentTimeMillis() - lastValidLocation.getTime() > " + signalLostThreshold);
|
|
||||||
GpsStatusValue oldStatus = gpsStatus;
|
GpsStatusValue oldStatus = gpsStatus;
|
||||||
gpsStatus = GpsStatusValue.GPS_SIGNAL_LOST;
|
gpsStatus = GpsStatusValue.GPS_SIGNAL_LOST;
|
||||||
client.onGpsStatusChanged(oldStatus, gpsStatus);
|
sendStatus(oldStatus, gpsStatus);
|
||||||
stopStatusRunner();
|
stopStatusRunner();
|
||||||
lastValidLocation = null;
|
lastValidLocation = null;
|
||||||
}
|
}
|
||||||
@@ -168,7 +165,7 @@ class GpsStatus {
|
|||||||
if (locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
if (locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||||
GpsStatusValue oldStatus = gpsStatus;
|
GpsStatusValue oldStatus = gpsStatus;
|
||||||
gpsStatus = GpsStatusValue.GPS_ENABLED;
|
gpsStatus = GpsStatusValue.GPS_ENABLED;
|
||||||
client.onGpsStatusChanged(oldStatus, gpsStatus);
|
sendStatus(oldStatus, gpsStatus);
|
||||||
startStatusRunner();
|
startStatusRunner();
|
||||||
} else {
|
} else {
|
||||||
onGpsDisabled();
|
onGpsDisabled();
|
||||||
@@ -183,13 +180,19 @@ class GpsStatus {
|
|||||||
if (gpsStatus != GpsStatusValue.GPS_DISABLED) {
|
if (gpsStatus != GpsStatusValue.GPS_DISABLED) {
|
||||||
GpsStatusValue oldStatus = gpsStatus;
|
GpsStatusValue oldStatus = gpsStatus;
|
||||||
gpsStatus = GpsStatusValue.GPS_DISABLED;
|
gpsStatus = GpsStatusValue.GPS_DISABLED;
|
||||||
client.onGpsStatusChanged(oldStatus, gpsStatus);
|
sendStatus(oldStatus, gpsStatus);
|
||||||
lastLocation = null;
|
lastLocation = null;
|
||||||
lastValidLocation = null;
|
lastValidLocation = null;
|
||||||
stopStatusRunner();
|
stopStatusRunner();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendStatus(GpsStatusValue prev, GpsStatusValue current) {
|
||||||
|
if (client != null) {
|
||||||
|
client.onGpsStatusChanged(prev, current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void startStatusRunner() {
|
private void startStatusRunner() {
|
||||||
if (gpsStatusRunner == null) {
|
if (gpsStatusRunner == null) {
|
||||||
gpsStatusRunner = new GpsStatusRunner();
|
gpsStatusRunner = new GpsStatusRunner();
|
||||||
|
|||||||
Reference in New Issue
Block a user