mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-09-30 16:52:21 +02:00
Cleanup: use Position instead of Android's location.
This commit is contained in:
+5
-15
@@ -27,7 +27,6 @@ import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.location.Location;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
@@ -861,24 +860,15 @@ public class CustomContentProviderUtilsTest {
|
||||
TrackPoint trackPoint = trackPointIterator.next();
|
||||
assertEquals(startTrackPointId.id() + i, trackPoint.getId().id());
|
||||
|
||||
checkLocation(i, trackPoint.getLocation());
|
||||
Position position = trackPoint.getPosition();
|
||||
assertEquals(TestDataUtil.INITIAL_LATITUDE + i / 10000.0, position.latitude(), 0.01);
|
||||
assertEquals(TestDataUtil.INITIAL_LONGITUDE - i / 10000.0, position.longitude(), 0.01);
|
||||
assertEquals(i / 100.0, position.horizontalAccuracy().distance_m(), 0.01);
|
||||
assertEquals(i * TestDataUtil.ALTITUDE_INTERVAL, position.altitude().toM(), 0.01);
|
||||
}
|
||||
assertFalse(trackPointIterator.hasNext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the value of a location.
|
||||
*
|
||||
* @param i the index of this location which created in the method {@link TestDataUtil#createTrack(Track.Id, int)}
|
||||
* @param location the location to be checked
|
||||
*/
|
||||
private void checkLocation(int i, Location location) {
|
||||
assertEquals(TestDataUtil.INITIAL_LATITUDE + (double) i / 10000.0, location.getLatitude(), 0.01);
|
||||
assertEquals(TestDataUtil.INITIAL_LONGITUDE - (double) i / 10000.0, location.getLongitude(), 0.01);
|
||||
assertEquals((float) i / 100.0f, location.getAccuracy(), 0.01);
|
||||
assertEquals(i * TestDataUtil.ALTITUDE_INTERVAL, location.getAltitude(), 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatIdListForUri() {
|
||||
assertEquals("", ContentProviderUtils.formatIdListForUri());
|
||||
|
||||
@@ -278,7 +278,7 @@ public class TrackRecordingActivity extends AbstractActivity implements ChooseAc
|
||||
Intent intent = IntentUtils
|
||||
.newIntent(this, MarkerEditActivity.class)
|
||||
.putExtra(MarkerEditActivity.EXTRA_TRACK_ID, trackId)
|
||||
.putExtra(MarkerEditActivity.EXTRA_LOCATION, trackPoint.getLocation());
|
||||
.putExtra(MarkerEditActivity.EXTRA_LOCATION, trackPoint.getPosition().toLocation());
|
||||
startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import androidx.annotation.Nullable;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
//TODO Use everywhere instead of android.location.Location.
|
||||
public record Position(
|
||||
@Nullable Instant time,
|
||||
@Nullable Double latitude,
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package de.dennisguse.opentracks.data.models;
|
||||
|
||||
import android.location.Location;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
@@ -157,12 +156,6 @@ public class TrackPoint {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@NonNull
|
||||
public Location getLocation() {
|
||||
return getPosition().toLocation();
|
||||
}
|
||||
|
||||
public boolean hasAltitudeGainLoss() {
|
||||
return altitudeGainLoss != null;
|
||||
}
|
||||
@@ -268,34 +261,18 @@ public class TrackPoint {
|
||||
throw new RuntimeException("Cannot compute distance.");
|
||||
}
|
||||
|
||||
return Distance.of(getLocation().distanceTo(previous.getLocation()));
|
||||
return Distance.of(getPosition().toLocation().distanceTo(previous.getPosition().toLocation()));
|
||||
}
|
||||
|
||||
public boolean fulfillsAccuracy(Distance thresholdHorizontalAccuracy) {
|
||||
return position.fulfillsAccuracy(thresholdHorizontalAccuracy);
|
||||
}
|
||||
|
||||
public Optional<Float> bearingTo(@NonNull Position dest) {
|
||||
if (!dest.hasLocation() || !hasLocation()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return bearingTo(dest.toLocation());
|
||||
}
|
||||
|
||||
public Optional<Float> bearingTo(@NonNull TrackPoint dest) {
|
||||
if (!dest.hasLocation() || !hasLocation()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return bearingTo(dest.getLocation());
|
||||
}
|
||||
|
||||
//TODO Bearing requires a location; what do we do if we don't have any?
|
||||
@Deprecated
|
||||
public Optional<Float> bearingTo(@NonNull Location dest) {
|
||||
if (!hasLocation()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(getLocation().bearingTo(dest));
|
||||
return Optional.of(getPosition().toLocation().bearingTo(dest.getPosition().toLocation()));
|
||||
}
|
||||
|
||||
// Sensor data
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ public class AltitudeCorrectionManager {
|
||||
// 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();
|
||||
Location loc = trackPoint.getPosition().toLocation();
|
||||
AltitudeConverterCompat.addMslAltitudeToLocation(context, loc);
|
||||
trackPoint.setAltitude(Altitude.EGM2008.of(LocationCompat.getMslAltitudeMeters(loc)));
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -37,7 +37,6 @@ import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.PickVisualMediaRequest;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
@@ -61,13 +60,14 @@ public class MarkerEditActivity extends AbstractActivity {
|
||||
|
||||
public static final String EXTRA_TRACK_ID = "track_id";
|
||||
public static final String EXTRA_MARKER_ID = "marker_id";
|
||||
//TODO Check if we can send a position instead.
|
||||
public static final String EXTRA_LOCATION = "location";
|
||||
|
||||
private static final String CAMERA_PHOTO_URI_KEY = "camera_photo_uri_key";
|
||||
|
||||
private static final String TAG = MarkerEditActivity.class.getSimpleName();
|
||||
private Track.Id trackId;
|
||||
private Location location;
|
||||
private Position position;
|
||||
private Marker.Id markerId;
|
||||
private Marker marker;
|
||||
|
||||
@@ -97,7 +97,7 @@ public class MarkerEditActivity extends AbstractActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
trackId = getIntent().getParcelableExtra(EXTRA_TRACK_ID);
|
||||
location = getIntent().getParcelableExtra(EXTRA_LOCATION);
|
||||
Location location = getIntent().getParcelableExtra(EXTRA_LOCATION);
|
||||
markerId = getIntent().getParcelableExtra(EXTRA_MARKER_ID);
|
||||
if ((trackId == null || location == null) && markerId == null) {
|
||||
throw new IllegalStateException("TrackId and Location must be provided or an existing markerId");
|
||||
@@ -139,8 +139,7 @@ public class MarkerEditActivity extends AbstractActivity {
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
switch (result.getResultCode()) {
|
||||
case RESULT_CANCELED ->
|
||||
Toast.makeText(this, R.string.marker_add_photo_canceled, Toast.LENGTH_LONG).show();
|
||||
case RESULT_CANCELED -> Toast.makeText(this, R.string.marker_add_photo_canceled, Toast.LENGTH_LONG).show();
|
||||
case RESULT_OK -> viewModel.onNewCameraPhoto(cameraPhotoUri,
|
||||
viewBinding.markerEditName.getText().toString(),
|
||||
viewBinding.markerEditMarkerType.getText().toString(),
|
||||
@@ -162,7 +161,9 @@ public class MarkerEditActivity extends AbstractActivity {
|
||||
});
|
||||
|
||||
if (isNewMarker) {
|
||||
createNewMarker().observe(this, this::loadMarkerData);
|
||||
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Position.of(location, Instant.now()));
|
||||
viewModel.createNewMarker(trackId, trackPoint)
|
||||
.observe(this, this::loadMarkerData);
|
||||
} else {
|
||||
loadMarkerData(markerId);
|
||||
}
|
||||
@@ -170,11 +171,6 @@ public class MarkerEditActivity extends AbstractActivity {
|
||||
setSupportActionBar(viewBinding.bottomAppBarLayout.bottomAppBar);
|
||||
}
|
||||
|
||||
private LiveData<Marker> createNewMarker() {
|
||||
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Position.of(location, Instant.now()));
|
||||
return viewModel.createNewMarker(trackId, trackPoint);
|
||||
}
|
||||
|
||||
private void loadMarkerData(Marker.Id markerId) {
|
||||
viewModel.getMarkerData(markerId).observe(this, this::loadMarkerData);
|
||||
}
|
||||
@@ -198,7 +194,7 @@ public class MarkerEditActivity extends AbstractActivity {
|
||||
super.onDestroy();
|
||||
|
||||
trackId = null;
|
||||
location = null;
|
||||
position = null;
|
||||
markerId = null;
|
||||
viewBinding = null;
|
||||
viewModel = null;
|
||||
|
||||
Reference in New Issue
Block a user