Skip to content
This repository was archived by the owner on Aug 16, 2025. It is now read-only.

Commit d56ee12

Browse files
committed
code cleanup
1 parent 75044bf commit d56ee12

File tree

4 files changed

+29
-66
lines changed

4 files changed

+29
-66
lines changed

src/main/kotlin/de/storchp/opentracks/osmplugin/MapsActivity.kt

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import android.widget.Toast
2323
import androidx.activity.OnBackPressedCallback
2424
import androidx.appcompat.app.AlertDialog
2525
import androidx.core.content.ContextCompat
26+
import androidx.core.view.children
2627
import androidx.documentfile.provider.DocumentFile
2728
import de.storchp.opentracks.osmplugin.MapsActivity.OpenTracksContentObserver
2829
import de.storchp.opentracks.osmplugin.dashboardapi.APIConstants
29-
import de.storchp.opentracks.osmplugin.dashboardapi.Track
3030
import de.storchp.opentracks.osmplugin.dashboardapi.TrackReader
3131
import de.storchp.opentracks.osmplugin.dashboardapi.TrackpointReader
3232
import de.storchp.opentracks.osmplugin.dashboardapi.TrackpointsBySegments
33-
import de.storchp.opentracks.osmplugin.dashboardapi.Waypoint
3433
import de.storchp.opentracks.osmplugin.dashboardapi.WaypointReader
3534
import de.storchp.opentracks.osmplugin.databinding.ActivityMapsBinding
3635
import de.storchp.opentracks.osmplugin.maps.MovementDirection
@@ -39,7 +38,6 @@ import de.storchp.opentracks.osmplugin.utils.DEFAULT_TRACK_COLOR_MORE
3938
import de.storchp.opentracks.osmplugin.utils.MapMode
4039
import de.storchp.opentracks.osmplugin.utils.MapUtils
4140
import de.storchp.opentracks.osmplugin.utils.PreferencesUtils
42-
import de.storchp.opentracks.osmplugin.utils.StatisticElement
4341
import de.storchp.opentracks.osmplugin.utils.TrackColorMode
4442
import de.storchp.opentracks.osmplugin.utils.TrackPointsDebug
4543
import de.storchp.opentracks.osmplugin.utils.TrackStatistics
@@ -88,11 +86,8 @@ import java.lang.Exception
8886
import java.lang.RuntimeException
8987
import java.util.ArrayList
9088
import java.util.Collections
91-
import java.util.Comparator
9289
import java.util.Objects
9390
import java.util.concurrent.atomic.AtomicInteger
94-
import java.util.function.Consumer
95-
import java.util.function.ToIntFunction
9691
import java.util.zip.ZipInputStream
9792

9893

@@ -219,35 +214,18 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
219214
tracksUri = APIConstants.getTracksUri(uris)
220215
trackPointsUri = APIConstants.getTrackPointsUri(uris)
221216
waypointsUri = APIConstants.getWaypointsUri(uris)
222-
keepScreenOn(
223-
intent.getBooleanExtra(
224-
EXTRAS_SHOULD_KEEP_SCREEN_ON,
225-
false
226-
)
227-
)
228-
showOnLockScreen(
229-
intent.getBooleanExtra(
230-
EXTRAS_SHOW_WHEN_LOCKED,
231-
false
232-
)
233-
)
234-
showFullscreen(
235-
intent.getBooleanExtra(
236-
EXTRAS_SHOW_FULLSCREEN,
237-
false
238-
)
239-
)
240-
isOpenTracksRecordingThisTrack = intent.getBooleanExtra(
241-
EXTRAS_OPENTRACKS_IS_RECORDING_THIS_TRACK,
242-
false
243-
)
217+
keepScreenOn(intent.getBooleanExtra(EXTRAS_SHOULD_KEEP_SCREEN_ON, false))
218+
showOnLockScreen(intent.getBooleanExtra(EXTRAS_SHOW_WHEN_LOCKED, false))
219+
showFullscreen(intent.getBooleanExtra(EXTRAS_SHOW_FULLSCREEN, false))
220+
isOpenTracksRecordingThisTrack =
221+
intent.getBooleanExtra(EXTRAS_OPENTRACKS_IS_RECORDING_THIS_TRACK, false)
244222

245223
trackPointsUri?.let { readTrackpoints(it, false, protocolVersion) }
246224
readTracks(tracksUri!!)
247225
waypointsUri?.let { readWaypoints(it) }
248226
} else if ("geo" == intent.scheme && intent.data != null) {
249227
WaypointReader.fromGeoUri(intent.data.toString())
250-
?.let { waypoint: Waypoint ->
228+
?.let { waypoint ->
251229
val marker = MapUtils.createTappableMarker(this, waypoint)
252230
waypointsLayer!!.addItem(marker)
253231
val pos = map.getMapPosition()
@@ -421,7 +399,7 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
421399
.filter { file -> file.exists() }
422400
.forEach { file: File -> readMapFile(tileSource, mapsCount, file) }
423401

424-
if (mapsCount.get() == 0 && !mapFiles.isEmpty()) {
402+
if (mapsCount.get() == 0 && mapFiles.isNotEmpty()) {
425403
Toast.makeText(
426404
this,
427405
R.string.error_loading_offline_map,
@@ -688,7 +666,7 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
688666
if (update && endPos != null) {
689667
myPos = endPos
690668
map.render()
691-
} else if (!latLongs.isEmpty()) {
669+
} else if (latLongs.isNotEmpty()) {
692670
boundingBox = BoundingBox(latLongs).extendMargin(1.2f).also {
693671
myPos = it.getCenterPoint()
694672
}
@@ -734,6 +712,7 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
734712
if (waypointsLayer != null) {
735713
layers.remove(waypointsLayer)
736714
}
715+
737716
waypointsLayer = createWaypointsLayer()
738717
map.layers().add(waypointsLayer)
739718
lastWaypointId = 0
@@ -828,44 +807,32 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
828807
}
829808

830809
private fun readTracks(data: Uri) {
831-
val tracks: List<Track> = TrackReader.readTracks(contentResolver, data)
832-
if (!tracks.isEmpty()) {
810+
val tracks = TrackReader.readTracks(contentResolver, data)
811+
if (tracks.isNotEmpty()) {
833812
val statistics = TrackStatistics(tracks)
834813
removeStatisticElements()
835814
PreferencesUtils.getStatisticElements()
836-
.stream()
837-
.sorted(Comparator.comparingInt<StatisticElement?>(ToIntFunction { obj: StatisticElement? -> obj!!.ordinal }))
838-
.forEach { se: StatisticElement? ->
839-
addStatisticElement(
840-
se!!.getText(
841-
this,
842-
statistics
843-
)
844-
)
845-
}
815+
.sortedBy { it.ordinal }
816+
.forEach { addStatisticElement(it.getText(this, statistics)) }
846817
}
847818
}
848819

849820
private fun removeStatisticElements() {
850-
val childsToRemove = ArrayList<View?>()
851-
for (i in 0 until binding.map.statisticsLayout.childCount) {
852-
val childView = binding.map.statisticsLayout.getChildAt(i)
853-
if (childView is TextView) {
854-
childsToRemove.add(childView)
821+
binding.map.statisticsLayout.children
822+
.filterIsInstance<TextView>()
823+
.forEach { view ->
824+
binding.map.statisticsLayout.removeView(view)
825+
binding.map.statistics.removeView(view)
855826
}
856-
}
857-
childsToRemove.forEach((Consumer { view: View? ->
858-
binding.map.statisticsLayout.removeView(view)
859-
binding.map.statistics.removeView(view)
860-
}))
861827
}
862828

863829
private fun addStatisticElement(text: String?) {
864-
val textView = TextView(this)
865-
textView.setId(View.generateViewId())
866-
textView.text = Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT)
867-
textView.setTextColor(getColor(R.color.track_statistic))
868-
textView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10f)
830+
val textView = TextView(this).apply {
831+
setId(View.generateViewId())
832+
this.text = Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT)
833+
setTextColor(getColor(R.color.track_statistic))
834+
setTextSize(TypedValue.COMPLEX_UNIT_PT, 10f)
835+
}
869836
binding.map.statisticsLayout.addView(textView)
870837
binding.map.statistics.addView(textView)
871838
}
@@ -935,11 +902,7 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
935902
)
936903
}
937904
} catch (se: SecurityException) {
938-
Log.e(
939-
TAG,
940-
"Error on registering OpenTracksContentObserver",
941-
se
942-
)
905+
Log.e(TAG, "Error on registering OpenTracksContentObserver", se)
943906
Toast.makeText(
944907
this,
945908
R.string.error_reg_content_observer,

src/main/kotlin/de/storchp/opentracks/osmplugin/dashboardapi/Trackpoint.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ object TrackpointReader {
112112
if (lastTrackPoint.isPause) {
113113
debug.trackpointsPause++
114114
if (lastTrackPoint.latLong == null) {
115-
if (!segment!!.isEmpty()) {
115+
if (segment!!.isNotEmpty()) {
116116
val previousTrackpoint = segment[segment.size - 1]
117117
previousTrackpoint.latLong?.let {
118118
segment.add(

src/main/kotlin/de/storchp/opentracks/osmplugin/utils/MapUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object MapUtils {
9797

9898
if (n > 2) {
9999
stack.push(intArrayOf(0, (n - 1)))
100-
while (!stack.isEmpty()) {
100+
while (stack.isNotEmpty()) {
101101
current = stack.pop()
102102
maxDist = 0.0
103103
idx = current[0] + 1

src/main/kotlin/de/storchp/opentracks/osmplugin/utils/TrackStatistics.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TrackStatistics(tracks: List<Track>) {
3434
private set
3535

3636
init {
37-
if (!tracks.isEmpty()) {
37+
if (tracks.isNotEmpty()) {
3838
val first = tracks[0]
3939
category = first.category.toString()
4040
startTimeEpochMillis = first.startTimeEpochMillis

0 commit comments

Comments
 (0)