-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support altitude correction via Android's native AltitudeConverter (A…
…PI34+). Fixes #1492.
- Loading branch information
1 parent
0c5c49c
commit 8c364ba
Showing
4 changed files
with
77 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/de/dennisguse/opentracks/services/handlers/AltitudeCorrectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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 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>. | ||
*/ | ||
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 >= 34 ? new AltitudeConverter() : null; | ||
} | ||
|
||
public void correctAltitude(Context context, TrackPoint trackPoint) { | ||
if (Build.VERSION.SDK_INT >= 34 && altitudeConverter != null) { | ||
try { | ||
Location loc = trackPoint.getLocation(); | ||
altitudeConverter.addMslAltitudeToLocation(context, loc); | ||
trackPoint.setAltitude(Altitude.EGM2008.of(loc.getMslAltitudeMeters())); | ||
return; | ||
} catch (IOException e) { | ||
Log.w(TAG, "Android's AltitudeConverter crashed; falling back to internal."); | ||
altitudeConverter = null; | ||
// Should we fallback | ||
} | ||
} | ||
|
||
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 (!trackPoint.hasLocation() || !trackPoint.hasAltitude()) { | ||
Log.d(TAG, "No altitude correction necessary."); | ||
return; | ||
} | ||
|
||
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())); | ||
} | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/main/java/de/dennisguse/opentracks/services/handlers/EGM2008CorrectionManager.java
This file was deleted.
Oops, something went wrong.