forked from upstream-mirrors/OpenTracks
+16
-40
@@ -2,15 +2,15 @@ package de.dennisguse.opentracks.services.handlers;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.location.altitude.AltitudeConverter;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.location.LocationCompat;
|
||||
import androidx.core.location.altitude.AltitudeConverterCompat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Altitude;
|
||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||
import de.dennisguse.opentracks.util.EGM2008Utils;
|
||||
|
||||
/**
|
||||
* More infos regarding Android 34's <a href="https://issuetracker.google.com/issues/195660815#comment1">AltitudeConverter</a>.
|
||||
@@ -19,52 +19,28 @@ public class AltitudeCorrectionManager {
|
||||
|
||||
private static final String TAG = AltitudeCorrectionManager.class.getSimpleName();
|
||||
|
||||
private AltitudeConverter altitudeConverter;
|
||||
|
||||
private final EGM2008Internal altitudeConverterFallback;
|
||||
|
||||
public AltitudeCorrectionManager() {
|
||||
this.altitudeConverterFallback = new EGM2008Internal();
|
||||
this.altitudeConverter = Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE ? new AltitudeConverter() : null;
|
||||
}
|
||||
|
||||
public void correctAltitude(Context context, TrackPoint trackPoint) {
|
||||
if (!trackPoint.hasLocation() || !trackPoint.hasAltitude()) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && altitudeConverter != null) {
|
||||
|
||||
// TODO The following is doing IO and should not be done in main thread.
|
||||
// AltitudeConverterCompat uses internally a RoomDatabase that cannot be access from main thread and thus fails on version <= 34.
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
Location loc = trackPoint.getLocation();
|
||||
altitudeConverter.addMslAltitudeToLocation(context, loc);
|
||||
trackPoint.setAltitude(Altitude.EGM2008.of(loc.getMslAltitudeMeters()));
|
||||
return;
|
||||
AltitudeConverterCompat.addMslAltitudeToLocation(context, loc);
|
||||
trackPoint.setAltitude(Altitude.EGM2008.of(LocationCompat.getMslAltitudeMeters(loc)));
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Android's AltitudeConverter crashed; falling back to internal.");
|
||||
altitudeConverter = null;
|
||||
// Should we fallback
|
||||
Log.w(TAG, "Android's AltitudeConverterCompat failed with " + e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
|
||||
altitudeConverterFallback.correctAltitude(context, trackPoint);
|
||||
}
|
||||
|
||||
private static class EGM2008Internal {
|
||||
|
||||
private static final String TAG = EGM2008Internal.class.getSimpleName();
|
||||
|
||||
private EGM2008Utils.EGM2008Correction egm2008Correction;
|
||||
|
||||
public void correctAltitude(Context context, TrackPoint trackPoint) {
|
||||
if (egm2008Correction == null || !egm2008Correction.canCorrect(trackPoint.getLocation())) {
|
||||
try {
|
||||
egm2008Correction = EGM2008Utils.createCorrection(context, trackPoint.getLocation());
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Could not load altitude correction for " + trackPoint, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
trackPoint.setAltitude(egm2008Correction.correctAltitude(trackPoint.getLocation()));
|
||||
try {
|
||||
t.join(); // wait for thread to finish
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, "Android's AltitudeConverterCompat failed with " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user