Skip to content

Commit ff00ccd

Browse files
committed
Bring back GPX feature
1 parent df05be2 commit ff00ccd

File tree

12 files changed

+117
-82
lines changed

12 files changed

+117
-82
lines changed

app/src/main/java/de/stephanlindauer/criticalmaps/fragments/MapFragment.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import de.stephanlindauer.criticalmaps.events.NewLocationEvent;
5858
import de.stephanlindauer.criticalmaps.events.NewServerResponseEvent;
5959
import de.stephanlindauer.criticalmaps.handler.GetLocationHandler;
60-
// import de.stephanlindauer.criticalmaps.handler.ShowGpxHandler;
60+
import de.stephanlindauer.criticalmaps.handler.ShowGpxHandler;
6161
import de.stephanlindauer.criticalmaps.managers.LocationUpdateManager;
6262
import de.stephanlindauer.criticalmaps.model.OtherUsersLocationModel;
6363
import de.stephanlindauer.criticalmaps.model.OwnLocationModel;
@@ -90,10 +90,8 @@ public class MapFragment extends Fragment {
9090
@Inject
9191
LocationUpdateManager locationUpdateManager;
9292

93-
/*
9493
@Inject
9594
ShowGpxHandler showGpxHandler;
96-
*/
9795

9896
@Inject
9997
SharedPreferences sharedPreferences;
@@ -216,6 +214,8 @@ public void getOutline(View view, Outline outline) {
216214
mapStyle = style;
217215
MapViewUtils.setupSourcesAndLayers(getActivity(), mapStyle);
218216

217+
showGpxHandler.showGpx(mapStyle);
218+
219219
// trigger fake location update in case fix was acquired already during map init
220220
handleNewLocation(NEW_LOCATION_EVENT);
221221
});
@@ -234,8 +234,6 @@ public void getOutline(View view, Outline outline) {
234234
isInitialLocationSet = savedState.getBoolean(KEY_INITIAL_LOCATION_SET, false);
235235
}
236236

237-
// showGpxHandler.showGpx(mapView);
238-
239237
if (!LocationUpdateManager.checkPermission()) {
240238
zoomToLocation(defaultGeoPoint, NO_GPS_PERMISSION_ZOOM_LEVEL);
241239
}

app/src/main/java/de/stephanlindauer/criticalmaps/fragments/SettingsFragment.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package de.stephanlindauer.criticalmaps.fragments;
22

3-
// import android.annotation.SuppressLint;
4-
// import android.app.Activity;
3+
import android.annotation.SuppressLint;
4+
import android.app.Activity;
55
import android.content.Intent;
66
import android.content.SharedPreferences;
7-
// import android.database.Cursor;
8-
// import android.net.Uri;
7+
import android.database.Cursor;
8+
import android.net.Uri;
99
import android.os.Bundle;
10-
// import android.provider.OpenableColumns;
10+
import android.provider.OpenableColumns;
1111
import android.text.format.Formatter;
1212
import android.view.LayoutInflater;
1313
import android.view.View;
@@ -22,15 +22,15 @@
2222
import de.stephanlindauer.criticalmaps.App;
2323
import de.stephanlindauer.criticalmaps.R;
2424
import de.stephanlindauer.criticalmaps.databinding.FragmentSettingsBinding;
25-
// import de.stephanlindauer.criticalmaps.handler.ChooseGpxFileHandler;
25+
import de.stephanlindauer.criticalmaps.handler.ChooseGpxFileHandler;
2626
import de.stephanlindauer.criticalmaps.prefs.SharedPrefsKeys;
2727
import de.stephanlindauer.criticalmaps.model.StorageLocation;
28-
// import de.stephanlindauer.criticalmaps.vo.RequestCodes;
28+
import de.stephanlindauer.criticalmaps.vo.RequestCodes;
2929
import info.metadude.android.typedpreferences.BooleanPreference;
30-
// import info.metadude.android.typedpreferences.StringPreference;
30+
import info.metadude.android.typedpreferences.StringPreference;
3131
import timber.log.Timber;
3232

33-
// import static de.stephanlindauer.criticalmaps.utils.GpxUtils.persistPermissionOnFile;
33+
import static de.stephanlindauer.criticalmaps.utils.GpxUtils.persistPermissionOnFile;
3434

3535
import org.maplibre.android.offline.OfflineManager;
3636

@@ -70,37 +70,33 @@ public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
7070

7171
updateClearCachePref();
7272
updateStorageGraph();
73-
// updateGpxFileName();
73+
updateGpxFileName();
7474

7575
binding.settingsShowOnLockscreenCheckbox.setChecked(
7676
new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_ON_LOCKSCREEN).get());
7777

7878
binding.settingsKeepScreenOnCheckbox.setChecked(
7979
new BooleanPreference(sharedPreferences, SharedPrefsKeys.KEEP_SCREEN_ON).get());
8080

81-
/*
8281
binding.settingsShowGpxCheckbox.setChecked(
8382
new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_GPX).get());
84-
*/
8583

8684
binding.settingsClearCacheButton.setOnClickListener(v -> handleClearCacheClicked());
8785

8886
binding.settingsShowOnLockscreenCheckbox.setOnCheckedChangeListener(
8987
(buttonView, isChecked) -> handleShowOnLockscreenChecked(isChecked));
9088
binding.settingsKeepScreenOnCheckbox.setOnCheckedChangeListener(
9189
(buttonView, isChecked) -> handleKeepScreenOnChecked(isChecked));
92-
/*
90+
9391
binding.settingsShowGpxCheckbox.setOnCheckedChangeListener(
9492
(buttonView, isChecked) -> handleShowTrack(isChecked));
9593
binding.settingsChooseGpxContainer.setOnClickListener(v -> handleChooseTrackClicked());
96-
*/
9794
}
9895

9996
@Override
10097
public void onActivityResult(int requestCode, int resultCode, Intent data) {
10198
super.onActivityResult(requestCode, resultCode, data);
10299

103-
/*
104100
if (requestCode == RequestCodes.CHOOSE_GPX_RESULT_CODE && resultCode == Activity.RESULT_OK) {
105101
Uri fileUri = data.getData();
106102
if (fileUri == null) {
@@ -112,7 +108,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
112108
persistPermissionOnFile(data, app.getContentResolver());
113109
updateGpxFileName();
114110
}
115-
*/
116111
}
117112

118113
private void updateStorageGraph() {
@@ -141,7 +136,6 @@ private void updateClearCachePref() {
141136
Formatter.formatShortFileSize(getActivity(), currentSize)));
142137
}
143138

144-
/*
145139
@SuppressLint("Range")
146140
private void updateGpxFileName() {
147141
String gpxFile = new StringPreference(
@@ -157,7 +151,6 @@ private void updateGpxFileName() {
157151

158152
binding.settingsChooseGpxSummaryText.setText(filename);
159153
}
160-
*/
161154

162155
void handleClearCacheClicked() {
163156
OfflineManager.getInstance(getActivity()).clearAmbientCache(new OfflineManager.FileSourceCallback() {
@@ -185,7 +178,6 @@ void handleKeepScreenOnChecked(boolean isChecked) {
185178
sharedPreferences, SharedPrefsKeys.KEEP_SCREEN_ON).set(isChecked);
186179
}
187180

188-
/*
189181
void handleShowTrack(boolean isChecked) {
190182
new BooleanPreference(
191183
sharedPreferences, SharedPrefsKeys.SHOW_GPX).set(isChecked);
@@ -194,7 +186,6 @@ void handleShowTrack(boolean isChecked) {
194186
void handleChooseTrackClicked() {
195187
new ChooseGpxFileHandler(this).openChooser();
196188
}
197-
*/
198189

199190
@Override
200191
public void onDestroyView() {

app/src/main/java/de/stephanlindauer/criticalmaps/handler/ShowGpxHandler.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
/*
21
package de.stephanlindauer.criticalmaps.handler;
32

43
import android.content.SharedPreferences;
5-
import android.graphics.Color;
64
import android.net.Uri;
75
import android.widget.Toast;
86

9-
import org.osmdroid.views.MapView;
10-
import org.osmdroid.views.overlay.Marker;
11-
import org.osmdroid.views.overlay.Polyline;
7+
import com.google.gson.JsonObject;
8+
9+
import org.maplibre.android.geometry.LatLng;
10+
import org.maplibre.android.maps.Style;
11+
import org.maplibre.android.style.sources.GeoJsonSource;
12+
13+
import org.maplibre.geojson.Feature;
14+
import org.maplibre.geojson.FeatureCollection;
15+
import org.maplibre.geojson.LineString;
16+
import org.maplibre.geojson.Point;
1217
import org.xml.sax.SAXException;
1318

1419
import java.io.IOException;
1520
import java.io.InputStream;
21+
import java.util.ArrayList;
1622

1723
import javax.inject.Inject;
1824
import javax.xml.parsers.ParserConfigurationException;
1925

2026
import de.stephanlindauer.criticalmaps.App;
2127
import de.stephanlindauer.criticalmaps.R;
2228
import de.stephanlindauer.criticalmaps.model.gpx.GpxModel;
23-
import de.stephanlindauer.criticalmaps.model.gpx.GpxPoi;
2429
import de.stephanlindauer.criticalmaps.model.gpx.GpxTrack;
2530
import de.stephanlindauer.criticalmaps.prefs.SharedPrefsKeys;
2631
import de.stephanlindauer.criticalmaps.utils.GpxReader;
@@ -43,7 +48,7 @@ public ShowGpxHandler(SharedPreferences sharedPreferences, GpxModel gpxModel, Ap
4348
this.gpxReader = gpxReader;
4449
}
4550

46-
public void showGpx(MapView mapView) {
51+
public void showGpx(Style mapStyle) {
4752
boolean showTrack = new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_GPX).get();
4853
if (!showTrack) {
4954
return;
@@ -54,7 +59,7 @@ public void showGpx(MapView mapView) {
5459
readFile(gpxUri);
5560
}
5661

57-
showModelOnMap(mapView);
62+
showModelOnMap(mapStyle);
5863
}
5964

6065
private void readFile(String gpxUri) {
@@ -66,29 +71,44 @@ private void readFile(String gpxUri) {
6671
}
6772
}
6873

69-
private void showModelOnMap(MapView mapView) {
74+
private void showModelOnMap(Style mapStyle) {
75+
addTracksToMap(mapStyle);
76+
addPoisToMap(mapStyle);
77+
}
78+
79+
private void addTracksToMap(Style mapStyle) {
80+
ArrayList<Feature> features = new ArrayList<>();
81+
7082
for (GpxTrack track : gpxModel.getTracks()) {
71-
addTrackToMap(mapView, track);
72-
}
83+
ArrayList<Point> points = new ArrayList<>();
84+
85+
for (LatLng location : track.getWaypoints()) {
86+
points.add(Point.fromLngLat(location.getLongitude(), location.getLatitude()));
87+
}
7388

74-
for (GpxPoi poi : gpxModel.getPoiList()) {
75-
addPoiToMap(mapView, poi);
89+
JsonObject properties = new JsonObject();
90+
properties.addProperty("label", track.getName());
91+
features.add(Feature.fromGeometry(LineString.fromLngLats(points), properties));
7692
}
77-
}
7893

79-
private void addTrackToMap(MapView mapView, GpxTrack track) {
80-
Polyline trackLine = new Polyline(mapView);
81-
trackLine.setPoints(track.getWaypoints());
82-
trackLine.setTitle(track.getName());
83-
trackLine.getOutlinePaint().setColor(Color.RED);
84-
mapView.getOverlayManager().add(trackLine);
94+
GeoJsonSource gpxTrackSource =
95+
(GeoJsonSource) mapStyle.getSource("gpxTrackSource");
96+
gpxTrackSource.setGeoJson(FeatureCollection.fromFeatures(features));
8597
}
8698

87-
private void addPoiToMap(MapView mapView, GpxPoi poi) {
88-
Marker marker = new Marker(mapView);
89-
marker.setPosition(poi.getPosition());
90-
marker.setTitle(poi.getName());
91-
mapView.getOverlayManager().add(marker);
99+
private void addPoisToMap(Style mapStyle) {
100+
ArrayList<Feature> features = new ArrayList<>();
101+
102+
gpxModel.getPoiList().forEach((gpxPoi) -> {
103+
LatLng location = gpxPoi.getPosition();
104+
JsonObject properties = new JsonObject();
105+
properties.addProperty("label", gpxPoi.getName());
106+
features.add(Feature.fromGeometry(
107+
Point.fromLngLat(location.getLongitude(), location.getLatitude()), properties));
108+
});
109+
110+
GeoJsonSource gpxPoiSource =
111+
(GeoJsonSource) mapStyle.getSource("gpxPoiSource");
112+
gpxPoiSource.setGeoJson(FeatureCollection.fromFeatures(features));
92113
}
93114
}
94-
*/

app/src/main/java/de/stephanlindauer/criticalmaps/model/gpx/GpxModel.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/*
21
package de.stephanlindauer.criticalmaps.model.gpx;
32

43
import java.util.ArrayList;
@@ -48,4 +47,3 @@ public void clear() {
4847
uri = null;
4948
}
5049
}
51-
*/
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
/*
21
package de.stephanlindauer.criticalmaps.model.gpx;
32

4-
import org.osmdroid.util.GeoPoint;
3+
import org.maplibre.android.geometry.LatLng;
54

65
public class GpxPoi {
76

87
private final String name;
9-
private final GeoPoint position;
8+
private final LatLng position;
109

11-
public GpxPoi(String name, GeoPoint position) {
10+
public GpxPoi(String name, LatLng position) {
1211
this.name = name;
1312
this.position = position;
1413
}
@@ -17,8 +16,7 @@ public String getName() {
1716
return name;
1817
}
1918

20-
public GeoPoint getPosition() {
19+
public LatLng getPosition() {
2120
return position;
2221
}
2322
}
24-
*/
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
/*
21
package de.stephanlindauer.criticalmaps.model.gpx;
32

4-
import org.osmdroid.util.GeoPoint;
3+
import org.maplibre.android.geometry.LatLng;
54

65
import java.util.List;
76

87
public class GpxTrack {
98

109
private final String name;
11-
private final List<GeoPoint> waypoints;
10+
private final List<LatLng> waypoints;
1211

13-
public GpxTrack(String name, List<GeoPoint> waypoints) {
12+
public GpxTrack(String name, List<LatLng> waypoints) {
1413
this.name = name;
1514
this.waypoints = waypoints;
1615
}
@@ -19,8 +18,7 @@ public String getName() {
1918
return name;
2019
}
2120

22-
public List<GeoPoint> getWaypoints() {
21+
public List<LatLng> getWaypoints() {
2322
return waypoints;
2423
}
2524
}
26-
*/

app/src/main/java/de/stephanlindauer/criticalmaps/prefs/SharedPrefsKeys.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ public interface SharedPrefsKeys {
1111
BuildConfig.APPLICATION_ID + ".SHOW_ON_LOCKSCREEN";
1212
String KEEP_SCREEN_ON =
1313
BuildConfig.APPLICATION_ID + ".KEEP_SCREEN_ON";
14-
/*
1514
String SHOW_GPX =
1615
BuildConfig.APPLICATION_ID + ".SHOW_GPX";
1716
String GPX_FILE =
1817
BuildConfig.APPLICATION_ID + ".GPX_FILE";
19-
*/
2018
}

0 commit comments

Comments
 (0)