Skip to content

Commit d63ce3d

Browse files
committed
Implement GPX track color (#74)
1 parent e6d2905 commit d63ce3d

11 files changed

Lines changed: 80 additions & 16 deletions

File tree

‎app/src/main/java/org/nitri/opentopo/MapFragment.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
725725
setBaseMap()
726726
}
727727
syncMapRotationPreference()
728+
overlayHelper?.updateGpxTrackColor(requireContext().defaultGpxTrackColor())
728729
if (!sharedPreferences.getBoolean(PREF_KML_ENABLED, true)) {
729730
clearKml()
730731
listener?.clearKml()

‎app/src/main/java/org/nitri/opentopo/SettingsActivity.kt‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import org.nitri.opentopo.ui.color.APP_COLOR_PALETTE
6161
import org.nitri.opentopo.ui.color.ColorPickerGrid
6262
import org.nitri.opentopo.ui.color.ColorPreference
6363
import org.nitri.opentopo.ui.color.DEFAULT_MARKER_COLOR
64+
import org.nitri.opentopo.ui.color.DEFAULT_GPX_TRACK_COLOR
65+
import org.nitri.opentopo.ui.color.OPAQUE_APP_COLOR_PALETTE
6466
import org.nitri.opentopo.ui.theme.OpenTopoTheme
6567
import org.nitri.opentopo.util.Utils
6668
import org.nitri.opentopo.util.importOpenTopoMapZipToSqliteCache
@@ -139,12 +141,32 @@ class SettingsActivity : AppCompatActivity() {
139141
val tapCompassToRotatePref = findPreference<Preference>(PREF_TAP_COMPASS_TO_ROTATE)
140142
val importTilesPref = findPreference<Preference>("import_tiles")
141143
val defaultMarkerColorPref = findPreference<ColorPreference>(PREF_DEFAULT_MARKER_COLOR)
144+
val gpxTrackColorPref = findPreference<ColorPreference>(PREF_GPX_TRACK_COLOR)
142145

143146
defaultMarkerColorPref?.let { pref ->
144147
val currentColor = prefs.getInt(PREF_DEFAULT_MARKER_COLOR, DEFAULT_MARKER_COLOR)
145148
pref.setColor(currentColor)
146149
pref.setOnPreferenceClickListener {
147-
showColorPickerDialog(PREF_DEFAULT_MARKER_COLOR, R.string.pref_default_marker_color_title)
150+
showColorPickerDialog(
151+
prefKey = PREF_DEFAULT_MARKER_COLOR,
152+
titleResId = R.string.pref_default_marker_color_title,
153+
defaultColor = DEFAULT_MARKER_COLOR,
154+
colors = APP_COLOR_PALETTE
155+
)
156+
true
157+
}
158+
}
159+
160+
gpxTrackColorPref?.let { pref ->
161+
val currentColor = prefs.getInt(PREF_GPX_TRACK_COLOR, DEFAULT_GPX_TRACK_COLOR)
162+
pref.setColor(currentColor)
163+
pref.setOnPreferenceClickListener {
164+
showColorPickerDialog(
165+
prefKey = PREF_GPX_TRACK_COLOR,
166+
titleResId = R.string.pref_gpx_track_color_title,
167+
defaultColor = DEFAULT_GPX_TRACK_COLOR,
168+
colors = OPAQUE_APP_COLOR_PALETTE
169+
)
148170
true
149171
}
150172
}
@@ -318,10 +340,15 @@ class SettingsActivity : AppCompatActivity() {
318340
dialog.show()
319341
}
320342

321-
private fun showColorPickerDialog(prefKey: String, titleResId: Int) {
343+
private fun showColorPickerDialog(
344+
prefKey: String,
345+
titleResId: Int,
346+
defaultColor: Int,
347+
colors: List<Int>
348+
) {
322349
val context = requireContext()
323350
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
324-
val currentColor = prefs.getInt(prefKey, DEFAULT_MARKER_COLOR)
351+
val currentColor = prefs.getInt(prefKey, defaultColor)
325352

326353
val dialog = AlertDialog.Builder(context, R.style.AlertDialogTheme)
327354
.setTitle(titleResId)
@@ -337,7 +364,7 @@ class SettingsActivity : AppCompatActivity() {
337364
setContent {
338365
OpenTopoTheme(dynamicColor = false) {
339366
ColorPickerGrid(
340-
colors = APP_COLOR_PALETTE,
367+
colors = colors,
341368
selectedColor = currentColor,
342369
onColorSelected = { selectedColor ->
343370
prefs.edit { putInt(prefKey, selectedColor) }
@@ -377,6 +404,7 @@ class SettingsActivity : AppCompatActivity() {
377404
const val PREF_ORS_API_KEY = "ors_api_key"
378405
const val PREF_ORS_PROFILE = "ors_profile"
379406
const val PREF_DEFAULT_MARKER_COLOR = "default_marker_color"
407+
const val PREF_GPX_TRACK_COLOR = "gpx_track_color"
380408
const val ACTION_API_KEY_CHANGED = "org.nitri.opentopo.API_KEY_CHANGED"
381409
}
382410
}
@@ -386,6 +414,11 @@ fun Context.defaultMarkerColor(): Int {
386414
return prefs.getInt(SettingsActivity.PREF_DEFAULT_MARKER_COLOR, DEFAULT_MARKER_COLOR)
387415
}
388416

417+
fun Context.defaultGpxTrackColor(): Int {
418+
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
419+
return prefs.getInt(SettingsActivity.PREF_GPX_TRACK_COLOR, org.nitri.opentopo.ui.color.DEFAULT_GPX_TRACK_COLOR)
420+
}
421+
389422
@OptIn(ExperimentalMaterial3Api::class)
390423
@Composable
391424
private fun SettingsScreen(onBack: () -> Unit) {

‎app/src/main/java/org/nitri/opentopo/overlay/OverlayHelper.kt‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.nitri.opentopo.model.MarkerModel
1717
import org.nitri.opentopo.nearby.entity.NearbyItem
1818
import org.osmdroid.tileprovider.MapTileProviderBasic
1919
import org.nitri.opentopo.view.MarkerEditorDialog
20+
import org.nitri.opentopo.defaultGpxTrackColor
2021
import org.osmdroid.tileprovider.tilesource.ITileSource
2122
import org.osmdroid.tileprovider.tilesource.XYTileSource
2223
import org.osmdroid.util.GeoPoint
@@ -165,7 +166,7 @@ class OverlayHelper(
165166

166167
private var markerInfoWindow: MarkerInfoWindow? = null
167168

168-
private var trackOverlay: TrackOverlay? = null
169+
private val trackOverlays = mutableListOf<TrackOverlay>()
169170
private var kmlOverlay: FolderOverlay? = null
170171
private val mapMarkers = ArrayList<Marker>()
171172

@@ -181,9 +182,12 @@ class OverlayHelper(
181182
val tracks = gpx.tracks
182183
val routes = gpx.routes
183184

185+
val trackColor = mContext.defaultGpxTrackColor()
186+
184187
if (!tracks.isNullOrEmpty()) {
185188
tracks.forEach { track ->
186-
trackOverlay = TrackOverlay(mContext, track)
189+
val trackOverlay = TrackOverlay(track, trackColor)
190+
trackOverlays.add(trackOverlay)
187191
mMapView?.overlays?.add(0, trackOverlay)
188192
}
189193
}
@@ -229,10 +233,10 @@ class OverlayHelper(
229233
*/
230234
fun clearGpx() {
231235
mMapView?.let { mapView ->
232-
trackOverlay?.also {
236+
trackOverlays.forEach {
233237
mapView.overlays.remove(it)
234-
trackOverlay = null
235238
}
239+
trackOverlays.clear()
236240
wayPointOverlay?.also {
237241
mapView.overlays.remove(it)
238242
wayPointOverlay = null
@@ -337,7 +341,12 @@ class OverlayHelper(
337341
* @return GPX layer present
338342
*/
339343
fun hasGpx(): Boolean {
340-
return trackOverlay != null || wayPointOverlay != null
344+
return trackOverlays.isNotEmpty() || wayPointOverlay != null
345+
}
346+
347+
fun updateGpxTrackColor(color: Int) {
348+
trackOverlays.forEach { it.updateTrackColor(color) }
349+
mMapView?.invalidate()
341350
}
342351

343352
fun setTilesOverlay(overlay: Int) {

‎app/src/main/java/org/nitri/opentopo/overlay/TrackOverlay.kt‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.nitri.opentopo.overlay
22

3-
import android.content.Context
43
import android.graphics.Canvas
54
import android.graphics.Paint
65
import android.graphics.Path
76
import android.graphics.Point
87
import android.util.Log
9-
import androidx.core.content.ContextCompat
108
import io.ticofab.androidgpxparser.parser.domain.Track
119
import io.ticofab.androidgpxparser.parser.domain.TrackSegment
12-
import org.nitri.opentopo.R
1310
import org.osmdroid.util.GeoPoint
1411
import org.osmdroid.views.MapView
1512
import org.osmdroid.views.overlay.Overlay
@@ -19,21 +16,26 @@ import kotlin.math.sqrt
1916
/**
2017
* Layer to display a track
2118
*
22-
* @param context
2319
* @param track
20+
* @param trackColor
2421
* @see Track
2522
*/
2623
class TrackOverlay(
27-
private val context: Context,
28-
private val track: Track
24+
private val track: Track,
25+
private var trackColor: Int
2926
) : Overlay() {
3027

3128
private val mPointsSegments: MutableList<List<Point>> = ArrayList()
29+
30+
fun updateTrackColor(color: Int) {
31+
trackColor = color
32+
}
33+
3234
override fun draw(canvas: Canvas, osmv: MapView, shadow: Boolean) {
3335

3436
//Log.d(TAG, "Zoom: " + osmv.getZoomLevelDouble());
3537
val routePaint = Paint()
36-
routePaint.color = ContextCompat.getColor(context, R.color.colorTrack)
38+
routePaint.color = trackColor
3739
routePaint.isAntiAlias = true
3840
routePaint.alpha = 204
3941
routePaint.style = Paint.Style.STROKE

‎app/src/main/java/org/nitri/opentopo/ui/color/AppColorPalette.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.compose.ui.graphics.Color
44
import androidx.compose.ui.graphics.luminance
55

66
const val DEFAULT_MARKER_COLOR: Int = 0xCCCC3F34.toInt()
7+
const val DEFAULT_GPX_TRACK_COLOR: Int = 0xFFF44336.toInt()
78

89
val APP_COLOR_PALETTE: List<Int> = listOf(
910
0xFFF44336.toInt(), // Red
@@ -28,6 +29,8 @@ val APP_COLOR_PALETTE: List<Int> = listOf(
2829
DEFAULT_MARKER_COLOR // Legacy Red
2930
)
3031

32+
val OPAQUE_APP_COLOR_PALETTE: List<Int> = APP_COLOR_PALETTE.filter { (it ushr 24) == 0xFF }
33+
3134
/**
3235
* Returns a contrast color (Black or White) for a given background color.
3336
*/

‎app/src/main/res/values-de/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<string name="settings">Einstellungen</string>
9797
<string name="screen_header">Bildschirm</string>
9898
<string name="cache_header">Cache</string>
99+
<string name="gpx_header">GPX</string>
99100

100101
<string name="kml_header">KML</string>
101102
<string name="kml_enable">KML-Unterstützung aktivieren</string>
@@ -138,6 +139,7 @@
138139
<string name="marker_color">Farbe</string>
139140
<string name="select_color">Farbe wählen</string>
140141
<string name="marker_color_description">Markerfarbe</string>
142+
<string name="pref_gpx_track_color_title">GPX-Trackfarbe</string>
141143
<string name="import_gpx">GPX importieren</string>
142144
<string name="select_all">Alle auswählen</string>
143145
<string name="export_selected">Ausgewählte exportieren</string>

‎app/src/main/res/values-es/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<string name="settings">Configuración</string>
9797
<string name="screen_header">Pantalla</string>
9898
<string name="cache_header">Caché</string>
99+
<string name="gpx_header">GPX</string>
99100

100101
<string name="kml_header">KML</string>
101102
<string name="kml_enable">Activar soporte KML</string>
@@ -138,6 +139,7 @@
138139
<string name="marker_color">Color</string>
139140
<string name="select_color">Seleccionar un color</string>
140141
<string name="marker_color_description">Color del marcador</string>
142+
<string name="pref_gpx_track_color_title">Color de track GPX</string>
141143
<string name="import_gpx">Importar GPX</string>
142144
<string name="select_all">Seleccionar todo</string>
143145
<string name="export_selected">Exportar seleccionados</string>

‎app/src/main/res/values-fr/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<string name="settings">Paramètres</string>
9797
<string name="screen_header">Écran</string>
9898
<string name="cache_header">Cache</string>
99+
<string name="gpx_header">GPX</string>
99100

100101
<string name="kml_header">KML</string>
101102
<string name="kml_enable">Activer le support KML</string>
@@ -138,6 +139,7 @@
138139
<string name="marker_color">Couleur</string>
139140
<string name="select_color">Sélectionner une couleur</string>
140141
<string name="marker_color_description">Couleur du marqueur</string>
142+
<string name="pref_gpx_track_color_title">Couleur de trace GPX</string>
141143
<string name="import_gpx">Importer GPX</string>
142144
<string name="select_all">Tout sélectionner</string>
143145
<string name="export_selected">Exporter la sélection</string>

‎app/src/main/res/values-nl/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<string name="settings">Instellingen</string>
9797
<string name="screen_header">Scherm</string>
9898
<string name="cache_header">Cache</string>
99+
<string name="gpx_header">GPX</string>
99100

100101
<string name="kml_header">KML</string>
101102
<string name="kml_enable">KML-ondersteuning inschakelen</string>
@@ -138,6 +139,7 @@
138139
<string name="marker_color">Kleur</string>
139140
<string name="select_color">Selecteer een kleur</string>
140141
<string name="marker_color_description">Markering kleur</string>
142+
<string name="pref_gpx_track_color_title">GPX-trackkleur</string>
141143
<string name="import_gpx">Importeer GPX</string>
142144
<string name="select_all">Alles selecteren</string>
143145
<string name="export_selected">Geselecteerde exporteren</string>

‎app/src/main/res/values/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<string name="settings">Settings</string>
109109
<string name="screen_header">Screen</string>
110110
<string name="cache_header">Cache</string>
111+
<string name="gpx_header">GPX</string>
111112

112113
<string name="kml_header">KML</string>
113114
<string name="kml_enable">Enable KML support</string>
@@ -150,6 +151,7 @@
150151
<string name="marker_color">Color</string>
151152
<string name="select_color">Select a color</string>
152153
<string name="marker_color_description">Marker color</string>
154+
<string name="pref_gpx_track_color_title">GPX track color</string>
153155
<string name="import_gpx">Import GPX</string>
154156
<string name="select_all">Select all</string>
155157
<string name="export_selected">Export selected</string>

0 commit comments

Comments
 (0)