diff --git a/aat-android/src/main/kotlin/ch/bailu/aat/services/sensor/bluetooth_le/WheelCircumference.kt b/aat-android/src/main/kotlin/ch/bailu/aat/services/sensor/bluetooth_le/WheelCircumference.kt index d607aad89..e37110350 100644 --- a/aat-android/src/main/kotlin/ch/bailu/aat/services/sensor/bluetooth_le/WheelCircumference.kt +++ b/aat-android/src/main/kotlin/ch/bailu/aat/services/sensor/bluetooth_le/WheelCircumference.kt @@ -29,6 +29,7 @@ class WheelCircumference(private val scontext: ServiceContext, private val revol if (newLocation != null) { currentLocation = newLocation if (currentLocation.getAccuracy() <= MIN_ACCURACY && revolution.isInitialized) { + val previousLocation = previousLocation if (previousLocation == null) { reset(currentLocation) } else { diff --git a/aat-gtk/src/main/java/ch/bailu/aat_gtk/lib/GResource.java b/aat-gtk/src/main/java/ch/bailu/aat_gtk/lib/GResource.java deleted file mode 100644 index e02f56974..000000000 --- a/aat-gtk/src/main/java/ch/bailu/aat_gtk/lib/GResource.java +++ /dev/null @@ -1,28 +0,0 @@ -package ch.bailu.aat_gtk.lib; - -import java.io.IOException; - -import ch.bailu.gtk.gio.Resource; -import ch.bailu.gtk.lib.util.JavaResource; -import ch.bailu.gtk.type.Bytes; -import ch.bailu.gtk.type.exception.AllocationError; - -public class GResource { - - /** - * Load a gresource bundle from java resources path and register it - * See {@link ch.bailu.gtk.gio.Resource} for documentation on how to generate - * gresource bundles. - * - * @param path absolute path to gresource: "/gresource/app.gresource" - */ - public static void loadAndRegister(String path) { - try (var stream = (new JavaResource(path).asStream())) { - var bytes = new Bytes(stream.readAllBytes()); - var resource = Resource.newFromDataResource(ch.bailu.gtk.glib.Bytes.newStaticBytes(bytes, bytes.getLength())); - resource.register(); - } catch (IOException | AllocationError e) { - System.err.println("Load gresource failed for '" + path + "'"); - } - } -} diff --git a/aat-gtk/src/main/java/ch/bailu/aat_gtk/lib/RuntimeInfo.java b/aat-gtk/src/main/java/ch/bailu/aat_gtk/lib/RuntimeInfo.java deleted file mode 100644 index 45e981392..000000000 --- a/aat-gtk/src/main/java/ch/bailu/aat_gtk/lib/RuntimeInfo.java +++ /dev/null @@ -1,53 +0,0 @@ -package ch.bailu.aat_gtk.lib; - -import ch.bailu.gtk.lib.util.SizeLog; - -public class RuntimeInfo implements Runnable { - - private static final int MB = 1024*1024; - private static final long TIMEOUT = 5000; - private static boolean on = false; - - @Override - public void run() { - var max = new SizeLog(getIdentifier("Max")); - var total = new SizeLog(getIdentifier("Total")); - var free = new SizeLog(getIdentifier("Free")); - var used = new SizeLog(getIdentifier("Used")); - var processors = new SizeLog(getIdentifier("Processors")); - - var runtime = Runtime.getRuntime(); - - try { - Thread.sleep(TIMEOUT); - processors.log(runtime.availableProcessors()); - free.log(runtime.freeMemory() / MB); - - while (on) { - max.log(runtime.maxMemory() / MB); - total.log(runtime.totalMemory() / MB); - used.log((runtime.totalMemory() - runtime.freeMemory()) / MB); - Thread.sleep(TIMEOUT); - } - - } catch (InterruptedException e) { - on = false; - System.err.println(e.getMessage()); - } - } - - private static String getIdentifier(String name) { - return Runtime.class.getSimpleName() + ":" + name; - } - - public static synchronized void startLogging() { - if (!on) { - on = true; - new Thread(new RuntimeInfo()).start(); - } - } - - public static synchronized void stopLogging() { - on = false; - } -} diff --git a/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/app/App.kt b/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/app/App.kt index 7f48c3295..05de8c0f2 100644 --- a/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/app/App.kt +++ b/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/app/App.kt @@ -2,7 +2,6 @@ package ch.bailu.aat_gtk.app import ch.bailu.aat_gtk.config.Environment import ch.bailu.aat_gtk.config.Strings -import ch.bailu.aat_gtk.lib.GResource import ch.bailu.aat_gtk.lib.RuntimeInfo import ch.bailu.aat_gtk.preferences.GtkStorage import ch.bailu.aat_gtk.preferences.PreferenceLoadDefaults @@ -17,6 +16,7 @@ import ch.bailu.aat_lib.logger.BroadcastLoggerFactory import ch.bailu.aat_lib.logger.PrintLnLoggerFactory import ch.bailu.gtk.adw.Application import ch.bailu.gtk.gio.ApplicationFlags +import ch.bailu.gtk.lib.bridge.GResource import ch.bailu.gtk.type.Strs import org.mapsforge.map.gtk.graphics.GtkGraphicFactory import kotlin.system.exitProcess diff --git a/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/lib/RuntimeInfo.kt b/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/lib/RuntimeInfo.kt new file mode 100644 index 000000000..9d00929e6 --- /dev/null +++ b/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/lib/RuntimeInfo.kt @@ -0,0 +1,54 @@ +package ch.bailu.aat_gtk.lib + +import ch.bailu.aat_lib.util.MemSize +import ch.bailu.gtk.lib.util.SizeLog + +class RuntimeInfo : Runnable { + override fun run() { + val max = SizeLog(getIdentifier("Max")) + val total = SizeLog(getIdentifier("Total")) + val free = SizeLog(getIdentifier("Free")) + val used = SizeLog(getIdentifier("Used")) + val processors = SizeLog(getIdentifier("Processors")) + + val runtime = Runtime.getRuntime() + + try { + Thread.sleep(TIMEOUT) + processors.log(runtime.availableProcessors().toLong()) + free.log(runtime.freeMemory() / MemSize.MB) + + while (on) { + max.log(runtime.maxMemory() / MemSize.MB) + total.log(runtime.totalMemory() / MemSize.MB) + used.log((runtime.totalMemory() - runtime.freeMemory()) / MemSize.MB) + Thread.sleep(TIMEOUT) + } + } catch (e: InterruptedException) { + on = false + System.err.println(e.message) + } + } + + companion object { + private const val TIMEOUT: Long = 5000 + private var on = false + + private fun getIdentifier(name: String): String { + return Runtime::class.java.simpleName + ":" + name + } + + @Synchronized + fun startLogging() { + if (!on) { + on = true + Thread(RuntimeInfo()).start() + } + } + + @Synchronized + fun stopLogging() { + on = false + } + } +} diff --git a/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/view/graph/GtkCanvas.kt b/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/view/graph/GtkCanvas.kt index e03795ec0..64b92a585 100644 --- a/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/view/graph/GtkCanvas.kt +++ b/aat-gtk/src/main/kotlin/ch/bailu/aat_gtk/view/graph/GtkCanvas.kt @@ -33,7 +33,7 @@ class GtkCanvas (private val context: Context) : GraphCanvas { context.restore() } - override fun drawBitmap(pa: Point?, color: Int) { + override fun drawBitmap(pa: Point, color: Int) { } override fun getTextSize(): Int { @@ -68,4 +68,4 @@ class GtkCanvas (private val context: Context) : GraphCanvas { layout.unref() strText.destroy() } -} \ No newline at end of file +} diff --git a/aat-lib/build.gradle.kts b/aat-lib/build.gradle.kts index a0164473c..e2e3c15a4 100644 --- a/aat-lib/build.gradle.kts +++ b/aat-lib/build.gradle.kts @@ -64,6 +64,7 @@ dependencies { implementation("com.google.guava:guava:33.5.0-jre") implementation("com.github.MaxKellermann:beacon:v0.1") + testImplementation(kotlin("test")) } testing { diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/api/brouter/BrouterApi.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/api/brouter/BrouterApi.kt index 8e8661c20..2ca562da1 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/api/brouter/BrouterApi.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/api/brouter/BrouterApi.kt @@ -34,7 +34,7 @@ class BrouterApi(overlay: SolidOverlayInterface) : Api(overlay) { val builder = StringBuilder() var del = "" while (iterator.nextPoint()) { - builder.append("$del${iterator.point.getLongitude()},${iterator.point.getLatitude()}") + builder.append("$del${iterator.getPoint().getLongitude()},${iterator.getPoint().getLatitude()}") del = "|" } return builder.toString() diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/GpxListWriter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/GpxListWriter.kt index 231bc4a13..ecd1dd90f 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/GpxListWriter.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/GpxListWriter.kt @@ -45,7 +45,7 @@ class GpxListWriter(track: GpxList, file: Foc) : Closeable { writer.writeSegment() } } - writer.writeTrackPoint(iterator.point) + writer.writeTrackPoint(iterator.getPoint()) } } } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxBigDelta.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxBigDelta.java deleted file mode 100644 index a6e1cd87b..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxBigDelta.java +++ /dev/null @@ -1,169 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.coordinates.BoundingBoxE6; -import ch.bailu.aat_lib.gpx.attributes.GpxAttributes; -import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes; -import ch.bailu.aat_lib.gpx.interfaces.GpxBigDeltaInterface; -import ch.bailu.aat_lib.gpx.interfaces.GpxType; - -public class GpxBigDelta implements GpxBigDeltaInterface { - public final static GpxBigDelta NULL= new GpxBigDelta(GpxListAttributes.NULL); - - private float distance=0; - - private long startTime=0; - private long endTime=0; - private long pause=0; - - private GpxType type = GpxType.TRACK; - - private BoundingBoxE6 boundingBox = null; - - private final GpxListAttributes attributes; - - public GpxBigDelta(GpxListAttributes attr) { - attributes = attr; - } - - - - public void update(GpxPointNode p) { - _update(p); - - attributes.update(p); - } - - - public void updateWithPause(GpxPointNode p) { - if (getEndTime()!=0) { - long pause = p.getTimeStamp()-getEndTime(); - if (pause > 0) incPause(pause); - } - _update(p); - } - - private void _update(GpxPointNode p) { - setStartTime(p.getTimeStamp()); - setEndTime(p.getTimeStamp()); - - incDistance(p.getDistance()); - - addBounding(p.getLatitudeE6(), p.getLongitudeE6()); - } - - public void updateWithPause(GpxBigDeltaInterface delta) { - setStartTime(delta.getStartTime()); - - incPause(delta.getPause()); - incEndTime(delta.getTimeDelta()+delta.getPause()); - incDistance(delta.getDistance()); - - addBounding(delta.getBoundingBox()); - } - - - private void setStartTime(long timestamp) { - if (startTime==0) { - startTime = timestamp; - endTime = timestamp; - } - } - - private void incEndTime(long t) { - endTime += t; - } - - private void setEndTime(long timestamp) { - endTime = timestamp; - } - - - private void incPause(long p) { - pause += p; - } - - private void incDistance(float d) { - distance += d; - } - - private void addBounding(BoundingBoxE6 b) { - if (boundingBox == null) { - boundingBox = new BoundingBoxE6(b); - } else { - boundingBox.add(b); - } - } - - private void addBounding(int la, int lo) { - if (boundingBox == null) { - boundingBox = new BoundingBoxE6(la,lo); - } else { - boundingBox.add(la,lo); - } - } - - - public BoundingBoxE6 getBoundingBox() { - if (boundingBox==null) return BoundingBoxE6.NULL_BOX; - return boundingBox; - } - - - public float getSpeed() { - float average; - float sitime = ((float)getTimeDelta()) / 1000f; - - if (sitime > 0f) average = distance / sitime; - else average=0f; - - return average; - } - - - - public float getDistance() { - return distance; - } - - public long getTimeDelta() { - return (endTime-startTime)-pause; - } - - - public long getPause() { - return pause; - } - - - - public long getStartTime() { - return startTime; - } - - - public float getAcceleration() { - return 0; - } - - @Override - public long getEndTime() { - return endTime; - } - - - public void setType(GpxType t) { - type = t; - } - - - @Override - public GpxType getType() { - return type; - } - - - @Override - public GpxAttributes getAttributes() { - return attributes; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxBigDelta.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxBigDelta.kt new file mode 100644 index 000000000..b34c5e743 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxBigDelta.kt @@ -0,0 +1,150 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.coordinates.BoundingBoxE6 +import ch.bailu.aat_lib.gpx.attributes.GpxAttributes +import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes +import ch.bailu.aat_lib.gpx.interfaces.GpxBigDeltaInterface +import ch.bailu.aat_lib.gpx.interfaces.GpxType + +class GpxBigDelta(private val attributes: GpxListAttributes) : GpxBigDeltaInterface { + private var distance = 0f + + private var startTime: Long = 0 + private var endTime: Long = 0 + private var pause: Long = 0 + + private var type = GpxType.TRACK + + private var boundingBox: BoundingBoxE6? = null + + fun update(p: GpxPointNode) { + doUpdate(p) + attributes.update(p) + } + + fun updateWithPause(p: GpxPointNode) { + if (getEndTime() != 0L) { + val pause = p.getTimeStamp() - getEndTime() + if (pause > 0) incPause(pause) + } + doUpdate(p) + } + + private fun doUpdate(p: GpxPointNode) { + setStartTime(p.getTimeStamp()) + setEndTime(p.getTimeStamp()) + + incDistance(p.getDistance()) + addBounding(p.getLatitudeE6(), p.getLongitudeE6()) + } + + fun updateWithPause(delta: GpxBigDeltaInterface) { + setStartTime(delta.getStartTime()) + + incPause(delta.getPause()) + incEndTime(delta.getTimeDelta() + delta.getPause()) + incDistance(delta.getDistance()) + + addBounding(delta.getBoundingBox()) + } + + private fun setStartTime(timestamp: Long) { + if (startTime == 0L) { + startTime = timestamp + endTime = timestamp + } + } + + private fun incEndTime(t: Long) { + endTime += t + } + + private fun setEndTime(timestamp: Long) { + endTime = timestamp + } + + + private fun incPause(p: Long) { + pause += p + } + + private fun incDistance(d: Float) { + distance += d + } + + private fun addBounding(b: BoundingBoxE6) { + val boundingBox = boundingBox + if (boundingBox == null) { + this.boundingBox = BoundingBoxE6(b) + } else { + boundingBox.add(b) + } + } + + private fun addBounding(la: Int, lo: Int) { + val boundingBox = boundingBox + if (boundingBox == null) { + this.boundingBox = BoundingBoxE6(la, lo) + } else { + boundingBox.add(la, lo) + } + } + + override fun getBoundingBox(): BoundingBoxE6 { + val boundingBox = boundingBox + if (boundingBox == null) { + return BoundingBoxE6.NULL_BOX + } + return boundingBox + } + + override fun getSpeed(): Float { + val average: Float + val sitime = (getTimeDelta().toFloat()) / 1000f + + if (sitime > 0f) average = distance / sitime + else average = 0f + + return average + } + + override fun getDistance(): Float { + return distance + } + + override fun getTimeDelta(): Long { + return (endTime - startTime) - pause + } + + override fun getPause(): Long { + return pause + } + + override fun getStartTime(): Long { + return startTime + } + + override fun getAcceleration(): Float { + return 0f + } + + override fun getEndTime(): Long { + return endTime + } + + fun setType(t: GpxType) { + type = t + } + + override fun getType(): GpxType { + return type + } + + override fun getAttributes(): GpxAttributes { + return attributes + } + + companion object { + val NULL: GpxBigDelta = GpxBigDelta(GpxListAttributes.NULL) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxConstants.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxConstants.java deleted file mode 100644 index 0e185a84a..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxConstants.java +++ /dev/null @@ -1,21 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -public class GpxConstants { - public static final String QNAME_GPX = "gpx"; - public static final String QNAME_TRACK = "trk"; - public static final String QNAME_TRACK_SEGMENT = "trkseg"; - public static final String QNAME_TRACK_POINT = "trkpt"; - - public static final String QNAME_WAY_POINT = "wpt"; - public static final String QNAME_ROUTE = "rte"; - public static final String QNAME_ROUTE_POINT = "rtept"; - - public static final String QNAME_LATITUDE = "lat"; - public static final String QNAME_LONGITUDE = "lon"; - - public static final String QNAME_TIME = "time"; - public static final String QNAME_ALTITUDE = "ele"; - public static final String QNAME_EXTENSIONS = "extensions"; - - public static final String QNAME_GPXTPX_EXTENSION = "TrackPointExtension"; -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxConstants.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxConstants.kt new file mode 100644 index 000000000..a4eee5afa --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxConstants.kt @@ -0,0 +1,21 @@ +package ch.bailu.aat_lib.gpx + +object GpxConstants { + const val QNAME_GPX: String = "gpx" + const val QNAME_TRACK: String = "trk" + const val QNAME_TRACK_SEGMENT: String = "trkseg" + const val QNAME_TRACK_POINT: String = "trkpt" + + const val QNAME_WAY_POINT: String = "wpt" + const val QNAME_ROUTE: String = "rte" + const val QNAME_ROUTE_POINT: String = "rtept" + + const val QNAME_LATITUDE: String = "lat" + const val QNAME_LONGITUDE: String = "lon" + + const val QNAME_TIME: String = "time" + const val QNAME_ALTITUDE: String = "ele" + const val QNAME_EXTENSIONS: String = "extensions" + + const val QNAME_GPXTPX_EXTENSION: String = "TrackPointExtension" +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDeltaHelper.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDeltaHelper.java deleted file mode 100644 index 6782c1139..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDeltaHelper.java +++ /dev/null @@ -1,51 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import org.mapsforge.core.model.LatLong; -import org.mapsforge.core.util.LatLongUtils; - -import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaPointInterface; -import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface; - -public class GpxDeltaHelper { - public static float getDistance(GpxPointInterface a, GpxPointInterface b) { - - return getDistance( - new LatLong(a.getLatitude(), a.getLongitude()), - new LatLong(b.getLatitude(), b.getLongitude())); - - - } - - public static float getDistance(LatLong a, LatLong b) { - return (float) LatLongUtils.sphericalDistance(a, b); - } - - public static float getAcceleration(GpxDeltaPointInterface a, GpxDeltaPointInterface b) { - float deltaSpeed=b.getSpeed()-a.getSpeed(); - float deltaTime=getTimeDeltaSI(a,b); - return getAcceleration(deltaSpeed, deltaTime); - } - - private static float getAcceleration(float deltaSpeed, float deltaTime) { - if (deltaTime != 0f) return deltaSpeed / deltaTime; - else return 0f; - } - - public static float getSpeed(GpxPointInterface a, GpxPointInterface b) { - return getSpeed(getDistance(a,b),getTimeDeltaSI(a,b)); - } - - public static float getSpeed(float distance, float time) { - if (time > 0f) return (distance / time); - else return 0f; - } - - public static long getTimeDeltaMilli(GpxPointInterface a, GpxPointInterface b) { - return b.getTimeStamp() - a.getTimeStamp(); - } - - public static float getTimeDeltaSI(GpxPointInterface a, GpxPointInterface b) { - float deltaT = getTimeDeltaMilli(a,b); - return deltaT / 1000f; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDeltaHelper.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDeltaHelper.kt new file mode 100644 index 000000000..9f7eadd1c --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDeltaHelper.kt @@ -0,0 +1,53 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaPointInterface +import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface +import org.mapsforge.core.model.LatLong +import org.mapsforge.core.util.LatLongUtils + +object GpxDeltaHelper { + @JvmStatic + fun getDistance(a: GpxPointInterface, b: GpxPointInterface): Float { + return getDistance( + LatLong(a.getLatitude(), a.getLongitude()), + LatLong(b.getLatitude(), b.getLongitude()) + ) + } + + fun getDistance(a: LatLong, b: LatLong): Float { + return LatLongUtils.sphericalDistance(a, b).toFloat() + } + + @JvmStatic + fun getAcceleration(a: GpxDeltaPointInterface, b: GpxDeltaPointInterface): Float { + val deltaSpeed = b.getSpeed() - a.getSpeed() + val deltaTime = getTimeDeltaSI(a, b) + return getAcceleration(deltaSpeed, deltaTime) + } + + private fun getAcceleration(deltaSpeed: Float, deltaTime: Float): Float { + return if (deltaTime != 0f) deltaSpeed / deltaTime + else 0f + } + + fun getSpeed(a: GpxPointInterface, b: GpxPointInterface): Float { + return getSpeed(getDistance(a, b), getTimeDeltaSI(a, b)) + } + + @JvmStatic + fun getSpeed(distance: Float, time: Float): Float { + return if (time > 0f) (distance / time) + else 0f + } + + @JvmStatic + fun getTimeDeltaMilli(a: GpxPointInterface, b: GpxPointInterface): Long { + return b.getTimeStamp() - a.getTimeStamp() + } + + @JvmStatic + fun getTimeDeltaSI(a: GpxPointInterface, b: GpxPointInterface): Float { + val deltaT = getTimeDeltaMilli(a, b).toFloat() + return deltaT / 1000f + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDistanceWindow.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDistanceWindow.java deleted file mode 100644 index c27b3002c..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDistanceWindow.java +++ /dev/null @@ -1,41 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.description.DistanceDescription; -import ch.bailu.aat_lib.preferences.StorageInterface; - -public class GpxDistanceWindow extends GpxWindow { - - private final float distanceLimit; - - public GpxDistanceWindow(GpxList list) { - super((GpxPointNode) list.getPointList().getFirst()); - distanceLimit = getDistanceLimit(list.getDelta().getDistance()); - } - - - @Override - protected boolean overLimit() { - return getDistance() > distanceLimit; - } - - - public String getLimitAsString(StorageInterface s) { - if (distanceLimit > 0) - return new DistanceDescription(s).getDistanceDescription(distanceLimit); - return ""; - - } - - private static int getDistanceLimit(float distance) { - - if (distance > 60000) return 2000; - if (distance > 30000) return 1000; - if (distance > 10000) return 500; - if (distance > 5000) return 100; - if (distance > 2000) return 50; - if (distance > 1000) return 10; - if (distance > 0) return 5; - return 0; - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDistanceWindow.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDistanceWindow.kt new file mode 100644 index 000000000..e9d7cb15d --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxDistanceWindow.kt @@ -0,0 +1,30 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.description.DistanceDescription +import ch.bailu.aat_lib.preferences.StorageInterface + +class GpxDistanceWindow(list: GpxList) : GpxWindow(list.pointList.first as GpxPointNode?) { + private val distanceLimit: Float = getDistanceLimit(list.getDelta().getDistance()).toFloat() + + override fun overLimit(): Boolean { + return getDistance() > distanceLimit + } + + fun getLimitAsString(s: StorageInterface): String { + if (distanceLimit > 0) return DistanceDescription(s).getDistanceDescription(distanceLimit) + return "" + } + + companion object { + private fun getDistanceLimit(distance: Float): Int { + if (distance > 60000) return 2000 + if (distance > 30000) return 1000 + if (distance > 10000) return 500 + if (distance > 5000) return 100 + if (distance > 2000) return 50 + if (distance > 1000) return 10 + if (distance > 0) return 5 + return 0 + } + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxList.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxList.kt index c840c66fc..262e83b20 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxList.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxList.kt @@ -52,9 +52,7 @@ class GpxList(type: GpxType, attr: GpxListAttributes) { return delta } - fun setType(type: GpxType?) { + fun setType(type: GpxType) { delta.setType(type) } - - } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListIterator.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListIterator.java deleted file mode 100644 index 9670ac17e..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListIterator.java +++ /dev/null @@ -1,93 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.gpx.attributes.GpxAttributesNull; -import ch.bailu.aat_lib.gpx.linked_list.Node; -import ch.bailu.aat_lib.gpx.segmented_list.SegmentNode; - -public class GpxListIterator { - - private class PointPrimerNode extends GpxPointFirstNode { - public PointPrimerNode() { - super(GpxPoint.NULL, GpxAttributesNull.NULL); - } - - @Override - public Node getNext() { - return track.getPointList().getFirst(); - } - } - - - private class SegmentPrimerNode extends GpxSegmentNode { - public SegmentPrimerNode() { - super(new PointPrimerNode()); - } - - @Override - public Node getNext() { - return track.getSegmentList().getFirst(); - } - } - - private final GpxList track; - - private Node point = new PointPrimerNode(); - private Node segment = new SegmentPrimerNode(); - - private int inSegmentIndex=-1; - private int inTrackIndex=-1; - - public GpxListIterator(GpxList t) { - track=t; - } - - - public boolean nextPoint() { - if (setPoint(point.getNext())) { - inSegmentIndex++; - inTrackIndex++; - - if (inSegmentIndex == ((SegmentNode)segment).getSegmentSize()) - return nextSegment(); - - return true; - } - return false; - } - - - private boolean setPoint(Node n) { - if (n == null) return false; - point = n; - return true; - } - - - private boolean nextSegment() { - if (setSegment(segment.getNext())) { - inSegmentIndex=0; - return true; - } - return false; - } - - - private boolean setSegment(Node n) { - if (n == null) return false; - segment = n; - return true; - } - - public GpxPointNode getPoint() { - return (GpxPointNode)point; - } - - - public boolean isFirstInTrack() { - return inTrackIndex==0; - } - - public boolean isFirstInSegment() { - return inSegmentIndex==0; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListIterator.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListIterator.kt new file mode 100644 index 000000000..89c8a2edd --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListIterator.kt @@ -0,0 +1,67 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.gpx.attributes.GpxAttributesNull +import ch.bailu.aat_lib.gpx.linked_list.Node +import ch.bailu.aat_lib.gpx.segmented_list.SegmentNode + +class GpxListIterator(private val track: GpxList) { + private inner class PointPrimerNode : GpxPointFirstNode(GpxPoint.NULL, GpxAttributesNull.NULL) { + override var next = track.pointList.first + } + + private inner class SegmentPrimerNode : GpxSegmentNode(PointPrimerNode()) { + override var next = track.segmentList.first + } + + private var point: GpxPointNode = PointPrimerNode() + private var segment: GpxSegmentNode = SegmentPrimerNode() + + private var inSegmentIndex = -1 + private var inTrackIndex = -1 + + + fun nextPoint(): Boolean { + if (setPoint(point.next)) { + inSegmentIndex++ + inTrackIndex++ + + if (inSegmentIndex == (segment as SegmentNode).segmentSize) return nextSegment() + return true + } + return false + } + + private fun setPoint(node: Node?): Boolean { + if (node is GpxPointNode) { + point = node + return true + } + return false + } + + private fun nextSegment(): Boolean { + if (setSegment(segment.next)) { + inSegmentIndex = 0 + return true + } + return false + } + + private fun setSegment(node: Node?): Boolean { + if (node is GpxSegmentNode) { + segment = node + return true + } + return false + } + + fun getPoint(): GpxPointNode { + return point + } + + val isFirstInTrack: Boolean + get() = inTrackIndex == 0 + + val isFirstInSegment: Boolean + get() = inSegmentIndex == 0 +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListWalker.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListWalker.java deleted file mode 100644 index 38be4e628..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListWalker.java +++ /dev/null @@ -1,75 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.gpx.linked_list.Node; -import ch.bailu.aat_lib.gpx.segmented_list.SegmentNode; - - -public abstract class GpxListWalker { - private int increment=0; - private int count=0; - - - public void walkTrack(GpxList track, int inc) { - increment = inc-1; - - walkTrack(track); - } - - - public void walkTrack(GpxList track) { - increment=0; - if (doList(track)) { - - Node segment = track.getSegmentList().getFirst(); - - while (segment != null) { - walkSegment((GpxSegmentNode)segment); - segment = segment.getNext(); - } - } - } - - - private void walkSegment(GpxSegmentNode segment) { - if (doSegment(segment)) { - int count = segment.getSegmentSize(); - Node marker = segment.getMarker(); - - while( count > 0 ) { - walkMarker((GpxSegmentNode) marker); - - count -= ((SegmentNode)marker).getSegmentSize(); - marker=marker.getNext(); - } - } - } - - - private void walkMarker(GpxSegmentNode marker) { - if (doMarker(marker)) { - int count = marker.getSegmentSize(); - - Node node = marker.getFirstNode(); - - while ( count > 0 ) { - - if (this.count == 0) { - this.count=increment; - doPoint((GpxPointNode)node); - } else { - this.count--; - } - - count --; - node = node.getNext(); - - } - } - } - - - public abstract boolean doList(GpxList track); - public abstract boolean doSegment(GpxSegmentNode segment); - public abstract boolean doMarker(GpxSegmentNode marker); - public abstract void doPoint(GpxPointNode point); -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListWalker.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListWalker.kt new file mode 100644 index 000000000..f18f7ae2a --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxListWalker.kt @@ -0,0 +1,66 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.gpx.linked_list.Node +import ch.bailu.aat_lib.gpx.segmented_list.SegmentNode + +abstract class GpxListWalker { + private var increment = 0 + private var count = 0 + + fun walkTrack(track: GpxList, inc: Int) { + increment = inc - 1 + + walkTrack(track) + } + + fun walkTrack(track: GpxList) { + increment = 0 + if (doList(track)) { + var segment = track.segmentList.first + + while (segment != null) { + walkSegment(segment as GpxSegmentNode) + segment = segment.next + } + } + } + + private fun walkSegment(segment: GpxSegmentNode) { + if (doSegment(segment)) { + var count = segment.segmentSize + var marker: Node? = segment.marker + + while (count > 0) { + walkMarker((marker as GpxSegmentNode?)!!) + + count -= (marker as SegmentNode).segmentSize + marker = marker.next + } + } + } + + private fun walkMarker(marker: GpxSegmentNode) { + if (doMarker(marker)) { + var count = marker.segmentSize + var node: Node? = marker.firstNode + while (count > 0) { + if (this.count == 0) { + this.count = increment + if (node is GpxPointNode) { + doPoint(node) + } + } else { + this.count-- + } + + count-- + node = node?.next + } + } + } + + abstract fun doList(track: GpxList): Boolean + abstract fun doSegment(segment: GpxSegmentNode): Boolean + abstract fun doMarker(marker: GpxSegmentNode): Boolean + abstract fun doPoint(point: GpxPointNode) +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxNodeFinder.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxNodeFinder.java deleted file mode 100644 index 2a5d5f8a1..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxNodeFinder.java +++ /dev/null @@ -1,63 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.coordinates.BoundingBoxE6; - -public class GpxNodeFinder extends GpxListWalker { - - private final BoundingBoxE6 bounding; - private GpxPointNode node; - private int index=0; - - public GpxNodeFinder(BoundingBoxE6 b) { - bounding = b; - } - - - @Override - public boolean doList(GpxList s) { - return BoundingBoxE6.doOverlap(s.getDelta().getBoundingBox(), bounding); - } - - @Override - public boolean doSegment(GpxSegmentNode s) { - if (haveNode()) { - return false; - - } else if (BoundingBoxE6.doOverlap(s.getBoundingBox(), bounding)) { - return true; - - } else { - index = index + s.getSegmentSize(); - return false; - } - } - - @Override - public boolean doMarker(GpxSegmentNode s) { - return doSegment(s); - } - - @Override - public void doPoint(GpxPointNode point) { - if (!haveNode()) { - if (bounding.contains(point)) { - node = point; - } else { - index++; - } - } - } - - public boolean haveNode() { - return node != null; - } - - - public GpxPointNode getNode() { - return node; - } - - public int getNodeIndex() { - return index; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxNodeFinder.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxNodeFinder.kt new file mode 100644 index 000000000..968d0be97 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxNodeFinder.kt @@ -0,0 +1,44 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.coordinates.BoundingBoxE6 +import ch.bailu.aat_lib.coordinates.BoundingBoxE6.Companion.doOverlap + +class GpxNodeFinder(private val bounding: BoundingBoxE6) : GpxListWalker() { + var node: GpxPointNode? = null + private set + var nodeIndex: Int = 0 + private set + + override fun doList(list: GpxList): Boolean { + return doOverlap(list.getDelta().getBoundingBox(), bounding) + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + if (haveNode()) { + return false + } else if (doOverlap(segment.getBoundingBox(), bounding)) { + return true + } else { + this.nodeIndex = this.nodeIndex + segment.segmentSize + return false + } + } + + override fun doMarker(segment: GpxSegmentNode): Boolean { + return doSegment(segment) + } + + override fun doPoint(point: GpxPointNode) { + if (!haveNode()) { + if (bounding.contains(point)) { + node = point + } else { + this.nodeIndex++ + } + } + } + + fun haveNode(): Boolean { + return node != null + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointFirstNode.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointFirstNode.java deleted file mode 100644 index b7cae7297..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointFirstNode.java +++ /dev/null @@ -1,35 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.gpx.attributes.GpxAttributes; - -public class GpxPointFirstNode extends GpxPointNode { - - public GpxPointFirstNode(GpxPoint tp, GpxAttributes at) { - super(tp, at); - } - - - @Override - public float getSpeed() { - return 0f; - } - - - @Override - public float getAcceleration() { - return 0f; - } - - - @Override - public float getDistance() { - return 0f; - } - - - @Override - public long getTimeDelta() { - return 0; - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointFirstNode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointFirstNode.kt new file mode 100644 index 000000000..53ff41a24 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointFirstNode.kt @@ -0,0 +1,21 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.gpx.attributes.GpxAttributes + +open class GpxPointFirstNode(point: GpxPoint, attributes: GpxAttributes) : GpxPointNode(point, attributes) { + override fun getSpeed(): Float { + return 0f + } + + override fun getAcceleration(): Float { + return 0f + } + + override fun getDistance(): Float { + return 0f + } + + override fun getTimeDelta(): Long { + return 0 + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointLinkedNode.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointLinkedNode.java deleted file mode 100644 index 1d8ace406..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointLinkedNode.java +++ /dev/null @@ -1,43 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.gpx.attributes.GpxAttributes; -import ch.bailu.aat_lib.gpx.linked_list.Node; - -public class GpxPointLinkedNode extends GpxPointNode { - public final static long SIZE_IN_BYTES=4; - - private float distance; //4byte - - public GpxPointLinkedNode(GpxPoint tp, GpxAttributes at) { - super(tp, at); - } - - @Override - public void setPrevious(Node node) { - super.setPrevious(node); - distance = GpxDeltaHelper.getDistance((GpxPointNode)node, this); - } - - @Override - public float getAcceleration() { - return GpxDeltaHelper.getAcceleration((GpxPointNode)getPrevious(), this); - } - - @Override - public float getDistance() { - return distance; - } - - @Override - public float getSpeed() { - return GpxDeltaHelper.getSpeed( - distance, - GpxDeltaHelper.getTimeDeltaSI(((GpxPointNode)getPrevious()), this)); - } - - @Override - public long getTimeDelta() { - return GpxDeltaHelper.getTimeDeltaMilli((GpxPointNode)getPrevious(), this); - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointLinkedNode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointLinkedNode.kt new file mode 100644 index 000000000..c24ed8286 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointLinkedNode.kt @@ -0,0 +1,54 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.gpx.GpxDeltaHelper.getSpeed +import ch.bailu.aat_lib.gpx.attributes.GpxAttributes +import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaPointInterface +import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface +import ch.bailu.aat_lib.gpx.linked_list.Node + +class GpxPointLinkedNode(tp: GpxPoint, at: GpxAttributes) : GpxPointNode(tp, at) { + private var distance = 0f //4byte + + override var previous: Node? = null + set(node) { + field = node + if (node is GpxPointInterface) { + distance = GpxDeltaHelper.getDistance(node, this) + } + } + + override fun getAcceleration(): Float { + val node = previous + if (node is GpxDeltaPointInterface) { + return GpxDeltaHelper.getAcceleration(node, this) + } + return 0f + } + + override fun getDistance(): Float { + return distance + } + + override fun getSpeed(): Float { + val node = previous + if (node is GpxPointInterface) { + return getSpeed( + distance, + GpxDeltaHelper.getTimeDeltaSI(node, this) + ) + } + return 0f + } + + override fun getTimeDelta(): Long { + val node = previous + if (node is GpxPointInterface) { + return GpxDeltaHelper.getTimeDeltaMilli(node, this) + } + return 0 + } + + companion object { + const val SIZE_IN_BYTES: Long = 4 + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointNode.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointNode.java deleted file mode 100644 index f13b3b518..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointNode.java +++ /dev/null @@ -1,87 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import javax.annotation.Nonnull; - -import ch.bailu.aat_lib.coordinates.BoundingBoxE6; -import ch.bailu.aat_lib.gpx.attributes.GpxAttributes; -import ch.bailu.aat_lib.gpx.attributes.Keys; -import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaPointInterface; -import ch.bailu.aat_lib.gpx.linked_list.Node; - -public abstract class GpxPointNode extends Node implements GpxDeltaPointInterface { - - private final GpxAttributes attributes; - private final GpxPoint point; - - - public GpxPointNode(GpxPoint tp, GpxAttributes at) { - point = tp; - attributes = at; - } - - @Override - public float getAltitude() { - return point.getAltitude(); - } - - @Override - public double getLatitude() { - return point.getLatitude(); - } - - @Override - public double getLongitude() { - return point.getLongitude(); - } - - @Override - public long getTimeStamp() { - return point.getTimeStamp(); - } - - @Override - public int getLatitudeE6() { - return point.getLatitudeE6(); - } - - @Override - public int getLongitudeE6() { - return point.getLongitudeE6(); - } - - - @Nonnull - @Override - public String toString() { - return attributes.toString(); - } - - public GpxPoint getPoint() { - return point; - } - - @Override - public GpxAttributes getAttributes() { - return attributes; - } - - public void setAltitude(double e) { - point.setAltitude((float) e); - } - - - private static final int BOUNDING_KEY= Keys.toIndex("boundingbox"); - - - @Override - public BoundingBoxE6 getBoundingBox() { - final BoundingBoxE6 box = new BoundingBoxE6(); - - if (attributes.hasKey(BOUNDING_KEY)) { - box.add(attributes.get(BOUNDING_KEY)); - } - box.add(getPoint()); - - return box; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointNode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointNode.kt new file mode 100644 index 000000000..17238992c --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxPointNode.kt @@ -0,0 +1,63 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.coordinates.BoundingBoxE6 +import ch.bailu.aat_lib.gpx.attributes.GpxAttributes +import ch.bailu.aat_lib.gpx.attributes.Keys.Companion.toIndex +import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaPointInterface +import ch.bailu.aat_lib.gpx.linked_list.Node +import javax.annotation.Nonnull + +abstract class GpxPointNode(@JvmField val point: GpxPoint, private val attributes: GpxAttributes) : Node(), + GpxDeltaPointInterface { + override fun getAltitude(): Float { + return point.getAltitude() + } + + override fun getLatitude(): Double { + return point.getLatitude() + } + + override fun getLongitude(): Double { + return point.getLongitude() + } + + override fun getTimeStamp(): Long { + return point.getTimeStamp() + } + + override fun getLatitudeE6(): Int { + return point.getLatitudeE6() + } + + override fun getLongitudeE6(): Int { + return point.getLongitudeE6() + } + + @Nonnull + override fun toString(): String { + return attributes.toString() + } + + override fun getAttributes(): GpxAttributes { + return attributes + } + + fun setAltitude(e: Double) { + point.setAltitude(e.toFloat()) + } + + override fun getBoundingBox(): BoundingBoxE6 { + val box = BoundingBoxE6() + + if (attributes.hasKey(BOUNDING_KEY)) { + box.add(attributes[BOUNDING_KEY]) + } + box.add(this.point) + + return box + } + + companion object { + private val BOUNDING_KEY = toIndex("boundingbox") + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxSegmentNode.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxSegmentNode.java deleted file mode 100644 index 202df5c7b..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxSegmentNode.java +++ /dev/null @@ -1,89 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import ch.bailu.aat_lib.coordinates.BoundingBoxE6; -import ch.bailu.aat_lib.gpx.attributes.GpxAttributes; -import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes; -import ch.bailu.aat_lib.gpx.interfaces.GpxBigDeltaInterface; -import ch.bailu.aat_lib.gpx.interfaces.GpxType; -import ch.bailu.aat_lib.gpx.linked_list.Node; -import ch.bailu.aat_lib.gpx.segmented_list.SegmentNode; - -public class GpxSegmentNode extends SegmentNode implements GpxBigDeltaInterface { - - private final GpxBigDelta delta=new GpxBigDelta(GpxListAttributes.NULL); - - - public GpxSegmentNode(GpxPointNode n) { - super(n); - } - - - public GpxSegmentNode(GpxPointNode n, GpxSegmentNode m) { - super(n, m); - } - - - - - @Override - public void update(Node n) { - GpxPointNode node=(GpxPointNode)n; - - super.update(node); - delta.update(node); - } - - - public float getAcceleration() { - return delta.getAcceleration(); - } - - - public float getSpeed() { - return delta.getSpeed(); - } - - - public float getDistance() { - return delta.getDistance(); - } - - - - public long getPause() { - return delta.getPause(); - } - - public long getStartTime() { - return delta.getStartTime(); - } - - - public long getTimeDelta() { - return delta.getTimeDelta(); - } - - - public BoundingBoxE6 getBoundingBox() { - return delta.getBoundingBox(); - } - - - @Override - public long getEndTime() { - return delta.getEndTime(); - } - - - - @Override - public GpxType getType() { - return delta.getType(); - } - - @Override - public GpxAttributes getAttributes() { - return delta.getAttributes(); - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxSegmentNode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxSegmentNode.kt new file mode 100644 index 000000000..29d84a9c4 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxSegmentNode.kt @@ -0,0 +1,63 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.coordinates.BoundingBoxE6 +import ch.bailu.aat_lib.gpx.attributes.GpxAttributes +import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes +import ch.bailu.aat_lib.gpx.interfaces.GpxBigDeltaInterface +import ch.bailu.aat_lib.gpx.interfaces.GpxType +import ch.bailu.aat_lib.gpx.linked_list.Node +import ch.bailu.aat_lib.gpx.segmented_list.SegmentNode + +open class GpxSegmentNode : SegmentNode, GpxBigDeltaInterface { + private val delta = GpxBigDelta(GpxListAttributes.NULL) + + constructor(node: GpxPointNode) : super(node) + constructor(node: GpxPointNode, marker: GpxSegmentNode) : super(node, marker) + + override fun update(node: Node) { + val node = node as GpxPointNode + + super.update(node) + delta.update(node) + } + + override fun getAcceleration(): Float { + return delta.getAcceleration() + } + + override fun getSpeed(): Float { + return delta.getSpeed() + } + + override fun getDistance(): Float { + return delta.getDistance() + } + + override fun getPause(): Long { + return delta.getPause() + } + + override fun getStartTime(): Long { + return delta.getStartTime() + } + + override fun getTimeDelta(): Long { + return delta.getTimeDelta() + } + + override fun getBoundingBox(): BoundingBoxE6 { + return delta.getBoundingBox() + } + + override fun getEndTime(): Long { + return delta.getEndTime() + } + + override fun getType(): GpxType { + return delta.getType() + } + + override fun getAttributes(): GpxAttributes { + return delta.getAttributes() + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxWindow.java b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxWindow.java deleted file mode 100644 index 8ac5afe49..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxWindow.java +++ /dev/null @@ -1,72 +0,0 @@ -package ch.bailu.aat_lib.gpx; - -import javax.annotation.Nonnull; - -import ch.bailu.aat_lib.coordinates.BoundingBoxE6; -import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaInterface; - -public abstract class GpxWindow implements GpxDeltaInterface { - - private GpxPointNode first; - private GpxPointNode last; - - private float distance; - private long timeDeltaMillis; - - public GpxWindow(GpxPointNode node) { - first = node; - last = node; - } - - public void forward(GpxPointNode n) { - last = n; - - distance += n.getDistance(); - timeDeltaMillis += n.getTimeDelta(); - - trim(); - } - - private void trim() { - while (overLimit() && first != last && first.getNext() instanceof GpxPointNode) { - timeDeltaMillis -= first.getTimeDelta(); - distance -= first.getDistance(); - - first = (GpxPointNode) first.getNext(); - } - } - - protected abstract boolean overLimit(); - - @Override - public float getDistance() { - return distance; - } - - - @Override - public float getSpeed() { - if (timeDeltaMillis > 0) { - return (distance * 1000f) / timeDeltaMillis; - } else { - return 0; - } - } - - @Override - public float getAcceleration() { - return 0; - } - - @Override - public long getTimeDelta() { - return timeDeltaMillis; - } - - @Nonnull - @Override - public BoundingBoxE6 getBoundingBox() { - return BoundingBoxE6.NULL_BOX; - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxWindow.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxWindow.kt new file mode 100644 index 000000000..9256835d0 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/GpxWindow.kt @@ -0,0 +1,62 @@ +package ch.bailu.aat_lib.gpx + +import ch.bailu.aat_lib.coordinates.BoundingBoxE6 +import ch.bailu.aat_lib.gpx.interfaces.GpxDeltaInterface +import javax.annotation.Nonnull + +abstract class GpxWindow(private var first: GpxPointNode?) : GpxDeltaInterface { + private var last: GpxPointNode? + + private var distance = 0f + private var timeDeltaMillis: Long = 0 + + init { + last = first + } + + fun forward(node: GpxPointNode) { + last = node + + distance += node.getDistance() + timeDeltaMillis += node.getTimeDelta() + + trim() + } + + private fun trim() { + while (overLimit() && first !== last && first!!.next is GpxPointNode) { + timeDeltaMillis -= first!!.getTimeDelta() + distance -= first!!.getDistance() + + first = first!!.next as GpxPointNode? + } + } + + protected abstract fun overLimit(): Boolean + + override fun getDistance(): Float { + return distance + } + + + override fun getSpeed(): Float { + if (timeDeltaMillis > 0) { + return (distance * 1000f) / timeDeltaMillis + } else { + return 0f + } + } + + override fun getAcceleration(): Float { + return 0f + } + + override fun getTimeDelta(): Long { + return timeDeltaMillis + } + + @Nonnull + override fun getBoundingBox(): BoundingBoxE6 { + return BoundingBoxE6.NULL_BOX + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/linked_list/List.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/linked_list/List.kt index 1217e0bf4..605d0f312 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/linked_list/List.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/linked_list/List.kt @@ -14,7 +14,7 @@ class List { if (first == null) { first = node } else { - last!!.next = node + last?.next = node } last = node count++ diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/segmented_list/SegmentNode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/segmented_list/SegmentNode.kt index 1b0f81bea..25e2cdd65 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/segmented_list/SegmentNode.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/segmented_list/SegmentNode.kt @@ -2,15 +2,11 @@ package ch.bailu.aat_lib.gpx.segmented_list import ch.bailu.aat_lib.gpx.linked_list.Node -open class SegmentNode @JvmOverloads constructor( - val firstNode: Node, - val marker: SegmentNode? = null - -) : Node() { +open class SegmentNode(val firstNode: Node, val marker: SegmentNode? = null) : Node() { var segmentSize = 0 private set - open fun update(n: Node) { + open fun update(node: Node) { segmentSize++ } } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/tools/SimplifierDistance.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/tools/SimplifierDistance.kt index 29b6d29a2..89e8161ec 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/tools/SimplifierDistance.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/gpx/tools/SimplifierDistance.kt @@ -8,6 +8,7 @@ import ch.bailu.aat_lib.gpx.GpxPointFirstNode import ch.bailu.aat_lib.gpx.GpxPointNode import ch.bailu.aat_lib.gpx.GpxSegmentNode import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes +import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface class SimplifierDistance : GpxListWalker() { var newList: GpxList? = null @@ -43,7 +44,11 @@ class SimplifierDistance : GpxListWalker() { } private fun hasDistance(point: GpxPointNode): Boolean { - return GpxDeltaHelper.getDistance(lastPoint, point) >= MIN_DISTANCE + val lastPoint = lastPoint + if (lastPoint is GpxPointInterface) { + return GpxDeltaHelper.getDistance(lastPoint, point) >= MIN_DISTANCE + } + return false } private fun isLastInSegment(point: GpxPointNode): Boolean { diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/lib/color/AltitudeColorTable.java b/aat-lib/src/main/java/ch/bailu/aat_lib/lib/color/AltitudeColorTable.java deleted file mode 100644 index a0ca07b5b..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/lib/color/AltitudeColorTable.java +++ /dev/null @@ -1,97 +0,0 @@ -package ch.bailu.aat_lib.lib.color; - -import ch.bailu.aat_lib.app.AppColor; -import ch.bailu.aat_lib.util.Limit; - -public class AltitudeColorTable { - - private final static int ALTITUDE_OFFSET=500; - - private final static int GRADIENTS=12; - private final static int GRADIENT_SIZE=256; - - private final static int SIZE = GRADIENTS * GRADIENT_SIZE; - private final static int MAX=GRADIENT_SIZE-1; - - private final int[] color_table; - - private static final AltitudeColorTable INSTANCE = new AltitudeColorTable(); - - - public static AltitudeColorTable instance() { - return INSTANCE; - } - - - private AltitudeColorTable() { - color_table = new int[SIZE]; - - for (int i = 0; i < SIZE; i++) { - color_table[i]=calculateColor(i); - } - } - - private int calculateColor(int index) { - int gradient = index / GRADIENT_SIZE; - int colorIndex = index % GRADIENT_SIZE; - - return calculateColor(gradient, colorIndex); - } - - - private int calculateColor(int gradient, int i) { - int result; - - switch (gradient) { - case 0: - result=rgb(0, i, i); - break; - case 1: - case 7: - result=rgb(0, MAX-i, MAX); - break; - case 2: - case 8: - result=rgb(i, 0, MAX); - break; - case 3: - case 9: - result=rgb(MAX, 0, MAX-i); - break; - case 4: - result=rgb(MAX, i, 0); - break; - case 5: - result=rgb(MAX-i, MAX, 0); - break; - case 6: - result=rgb(0, MAX, i); - break; - case 10: - result=rgb(MAX-i,0, 0); - break; - case 11: - result=rgb(i,i, i); - break; - default: - result= AppColor.HL_ORANGE; - break; - } - return result; - } - - private int rgb(int r, int g, int b) { - return new ARGB(r,g,b).toInt(); - } - - - public int getColor(int index) { - return getColorAtIndex(index + ALTITUDE_OFFSET); - } - - private int getColorAtIndex(int index) { - index = Limit.clamp(index, 0, color_table.length-1); - return color_table[index]; - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/lib/color/AltitudeColorTable.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/lib/color/AltitudeColorTable.kt new file mode 100644 index 000000000..bb502f85e --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/lib/color/AltitudeColorTable.kt @@ -0,0 +1,57 @@ +package ch.bailu.aat_lib.lib.color + +import ch.bailu.aat_lib.app.AppColor +import ch.bailu.aat_lib.util.Limit.clamp + +object AltitudeColorTable { + private val colorTable: IntArray = IntArray(SIZE) + + init { + for (i in 0.. rgb(0, i, i) + 1, 7 -> rgb(0, MAX - i, MAX) + 2, 8 -> rgb(i, 0, MAX) + 3, 9 -> rgb(MAX, 0, MAX - i) + 4 -> rgb(MAX, i, 0) + 5 -> rgb(MAX - i, MAX, 0) + 6 -> rgb(0, MAX, i) + 10 -> rgb(MAX - i, 0, 0) + 11 -> rgb(i, i, i) + else -> AppColor.HL_ORANGE + } + } + + private fun rgb(r: Int, g: Int, b: Int): Int { + return ARGB(r, g, b).toInt() + } + + + fun getColor(index: Int): Int { + return getColorAtIndex(index + ALTITUDE_OFFSET) + } + + private fun getColorAtIndex(index: Int): Int { + var index = index + index = clamp(index, 0, colorTable.size - 1) + return colorTable[index] + } + + private const val ALTITUDE_OFFSET = 500 + private const val GRADIENTS = 12 + private const val GRADIENT_SIZE = 256 + private const val SIZE: Int = GRADIENTS * GRADIENT_SIZE + private const val MAX: Int = GRADIENT_SIZE - 1 +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/RoutePainterNode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/RoutePainterNode.kt index c84d91a5e..0eef5b26b 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/RoutePainterNode.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/RoutePainterNode.kt @@ -9,11 +9,12 @@ class RoutePainterNode(private val mcontext: MapContext, private val color: Int, : GpxListPainter(mcontext, minPixelSpace) { override fun drawEdge(nodes: TwoNodes) {} override fun drawNode(node: TwoNodes.PixelNode) { - val c: Int val altitude = node.point.getAltitude() - c = - if (altitude == ElevationProvider.NULL_ALTITUDE) color else AltitudeColorTable.instance() - .getColor(altitude.toInt()) - mcontext.draw().bitmap(mcontext.draw().getNodeBitmap(), node.pixel, c) + val altitudeColor = if (altitude == ElevationProvider.NULL_ALTITUDE) { + color + } else { + AltitudeColorTable.getColor(altitude.toInt()) + } + mcontext.draw().bitmap(mcontext.draw().getNodeBitmap(), node.pixel, altitudeColor) } } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/TrackPainter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/TrackPainter.kt index bf38cdf7f..9168b370e 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/TrackPainter.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/gpx/painter/TrackPainter.kt @@ -12,7 +12,7 @@ class TrackPainter(private val mcontext: MapContext, private val paint: Paint) override fun drawNode(node: TwoNodes.PixelNode) { val altitude = node.point.getAltitude().toInt() - val color = AltitudeColorTable.instance().getColor(altitude) + val color = AltitudeColorTable.getColor(altitude) paint.color = color } -} \ No newline at end of file +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/selector/AbsNodeSelectorLayer.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/selector/AbsNodeSelectorLayer.kt index 5cb34983b..e94838e87 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/selector/AbsNodeSelectorLayer.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/map/layer/selector/AbsNodeSelectorLayer.kt @@ -103,9 +103,10 @@ abstract class AbsNodeSelectorLayer( val finder = GpxNodeFinder(centerBounding) finder.walkTrack(info.getGpxList()) - if (finder.haveNode()) { - selectedNode = finder.node - setSelectedNode(infoID, info, finder.node, finder.nodeIndex) + val node = finder.node + if (node is GpxPointNode) { + selectedNode = node + setSelectedNode(infoID, info, node, finder.nodeIndex) break } } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/background/DownloadConfig.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/background/DownloadConfig.java deleted file mode 100644 index a9cd055cf..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/background/DownloadConfig.java +++ /dev/null @@ -1,37 +0,0 @@ -package ch.bailu.aat_lib.service.background; - -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - -import ch.bailu.aat_lib.app.AppConfig; - -public class DownloadConfig { - - private final static int TIMEOUT = 30 * 1000; - private final static String USER_AGENT_KEY = "User-Agent"; - private final String userAgentValue; - - private final static int IO_BUFFER_SIZE=8*1024; - - public DownloadConfig(AppConfig config) { - userAgentValue = config.getUserAgent(); - } - - public byte[] createBuffer() { - return new byte[IO_BUFFER_SIZE]; - } - - public HttpURLConnection openConnection(URL url) throws IOException { - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setConnectTimeout(TIMEOUT); - connection.setReadTimeout(TIMEOUT); - connection.setRequestProperty(USER_AGENT_KEY, userAgentValue); - return connection; - } - - public InputStream openInput(HttpURLConnection connection) throws IOException { - return connection.getInputStream(); - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/background/DownloadConfig.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/background/DownloadConfig.kt new file mode 100644 index 000000000..baa820edf --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/background/DownloadConfig.kt @@ -0,0 +1,35 @@ +package ch.bailu.aat_lib.service.background + +import ch.bailu.aat_lib.app.AppConfig +import java.io.IOException +import java.io.InputStream +import java.net.HttpURLConnection +import java.net.URL + +class DownloadConfig(config: AppConfig) { + private val userAgentValue: String = config.userAgent + + fun createBuffer(): ByteArray { + return ByteArray(IO_BUFFER_SIZE) + } + + @Throws(IOException::class) + fun openConnection(url: URL): HttpURLConnection { + val connection = url.openConnection() as HttpURLConnection + connection.connectTimeout = TIMEOUT + connection.readTimeout = TIMEOUT + connection.setRequestProperty(USER_AGENT_KEY, userAgentValue) + return connection + } + + @Throws(IOException::class) + fun openInput(connection: HttpURLConnection): InputStream { + return connection.getInputStream() + } + + companion object { + private const val TIMEOUT = 30 * 1000 + private const val USER_AGENT_KEY = "User-Agent" + private const val IO_BUFFER_SIZE = 8 * 1024 + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/OnObject.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/OnObject.java deleted file mode 100644 index ff3e79c35..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/OnObject.java +++ /dev/null @@ -1,35 +0,0 @@ -package ch.bailu.aat_lib.service.cache; - -import ch.bailu.aat_lib.app.AppContext; - - -public abstract class OnObject { - public OnObject(final AppContext appContext, final String id, final Class c) { - this(appContext, id, c, null); - } - - - public OnObject(final AppContext appContext, final String id, final Class c, - final Obj.Factory factory) { - - appContext.getServices().insideContext(() -> { - Obj handle; - - if (factory == null) - handle = appContext.getServices().getCacheService().getObject(id); - - else - handle = appContext.getServices().getCacheService().getObject(id, factory); - - try { - if (c.isInstance(handle)) - OnObject.this.run(handle); - - } finally { - handle.free(); - } - }); - } - - public abstract void run(Obj handle); -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/OnObject.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/OnObject.kt new file mode 100644 index 000000000..f6a7c5307 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/OnObject.kt @@ -0,0 +1,26 @@ +package ch.bailu.aat_lib.service.cache + +import ch.bailu.aat_lib.app.AppContext + +abstract class OnObject( + appContext: AppContext, id: String, c: Class<*>, + factory: Obj.Factory? = null +) { + init { + appContext.services.insideContext { + val handle =if (factory == null) { + appContext.services.getCacheService().getObject(id) + } else { + appContext.services.getCacheService().getObject(id, factory) + } + + try { + if (c.isInstance(handle)) this@OnObject.run(handle) + } finally { + handle.free() + } + } + } + + abstract fun run(handle: Obj) +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/elevation/ObjTileElevationColor.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/elevation/ObjTileElevationColor.kt index 1350edb22..754a65380 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/elevation/ObjTileElevationColor.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/elevation/ObjTileElevationColor.kt @@ -32,8 +32,7 @@ class ObjTileElevationColor(id: String, b: MapTileInterface, t: Tile, split: Int if (newOffset != offset) { offset = newOffset - color = AltitudeColorTable.instance() - .getColor(demtile.getElevation(line + offset).toInt()) + color = AltitudeColorTable.getColor(demtile.getElevation(line + offset).toInt()) } bitmap[c] = color diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/gpx/ObjGpxEditable.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/gpx/ObjGpxEditable.kt index 83c61b438..dd605f04e 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/gpx/ObjGpxEditable.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/cache/gpx/ObjGpxEditable.kt @@ -57,7 +57,7 @@ class ObjGpxEditable(id: String, private val file: Foc, sc: AppContext) : ObjGpx EditorInterface { private var editor = GpxEditor(GpxList.NULL_ROUTE) private var modified = false - fun loadIntoEditor(list: GpxList?) { + fun loadIntoEditor(list: GpxList) { editor = GpxEditor(list) modified = false modified(false) diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/EditorRing.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/EditorRing.java deleted file mode 100644 index 627a4273b..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/EditorRing.java +++ /dev/null @@ -1,52 +0,0 @@ -package ch.bailu.aat_lib.service.editor; - -public final class EditorRing { - private final NodeEditor[] ring = new NodeEditor[10]; - private int index=0; - private int undoable=0; - private int redoable=0; - - - public EditorRing(NodeEditor s) { - set(s); - } - - - public void add(NodeEditor s) { - index++; if (index >= ring.length) index = 0; - - undoable = Math.min(undoable+1, ring.length); - redoable=0; - - ring[index] = s; - } - - public void set(NodeEditor s) { - ring[index] = s; - } - - public NodeEditor get() { - return ring[index]; - } - - - public boolean undo() { - if (undoable > 0) { - undoable--; redoable++; - index--; if (index < 0) index = ring.length-1; - return true; - } - return false; - } - - - public boolean redo() { - if (redoable > 0) { - undoable++; - redoable--; - index = (index+1) % ring.length; - return true; - } - return false; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/EditorRing.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/EditorRing.kt new file mode 100644 index 000000000..03e0fdade --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/EditorRing.kt @@ -0,0 +1,61 @@ +package ch.bailu.aat_lib.service.editor + +import kotlin.math.min + +class EditorRing(s: NodeEditor) { + private val ring = ArrayList(RING_SIZE) + private var index = 0 + private var undoable = 0 + private var redoable = 0 + + init { + ring.add(s) + } + + fun add(s: NodeEditor) { + index++ + if (index >= RING_SIZE) index = 0 + + undoable = min(undoable + 1, RING_SIZE) + redoable = 0 + + if (index < ring.size) { + ring[index] = s + } else { + ring.add(s) + } + } + + fun set(s: NodeEditor) { + ring[index] = s + } + + fun get(): NodeEditor { + return ring[index] + } + + fun undo(): Boolean { + if (undoable > 0) { + undoable-- + redoable++ + index-- + if (index < 0) index = ring.size - 1 + return true + } + return false + } + + fun redo(): Boolean { + if (redoable > 0) { + undoable++ + redoable-- + index = (index + 1) % ring.size + return true + } + return false + } + + companion object { + const val RING_SIZE = 10 + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/GpxEditor.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/GpxEditor.java deleted file mode 100644 index 8d0ee889d..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/GpxEditor.java +++ /dev/null @@ -1,97 +0,0 @@ -package ch.bailu.aat_lib.service.editor; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface; -import ch.bailu.aat_lib.gpx.interfaces.GpxType; - -public final class GpxEditor { - - private final EditorRing ring; - - public GpxEditor(GpxList list) { - if (list.getPointList().size() > 0) { - ring = new EditorRing((new NodeEditor((GpxPointNode) list.getPointList().getFirst(), - list))); - } else { - ring = new EditorRing(new NodeEditor()); - } - } - - public void select(GpxPointNode point, GpxList list) { - ring.set(new NodeEditor(point, list)); - } - - public void clear() { - ring.add(new NodeEditor()); - } - - public void unlinkSelectedNode() { - ring.add(ring.get().unlink()); - } - - public void insertNode(GpxPointInterface point) { - ring.add(ring.get().insert(point)); - } - - public void moveSelectedUp() { - GpxPointInterface point = ring.get().getPoint(); - - ring.add(ring.get().unlink()); - ring.set(ring.get().previous()); - ring.set(ring.get().insert(point)); - } - - public void moveSelectedDown() { - GpxPointInterface point = ring.get().getPoint(); - - ring.add(ring.get().unlink()); - ring.set(ring.get().next()); - ring.set(ring.get().insert(point)); - - } - - public GpxList getList() { - return ring.get().getList(); - } - - public GpxPointNode getSelectedPoint() { - return ring.get().getPoint(); - } - - public void setType(GpxType type) { - ring.get().getList().setType(type); - } - - public boolean undo() { - return ring.undo(); - } - - public boolean redo() { - return ring.redo(); - } - - public void simplify() { - ring.add(ring.get().simplify()); - } - - public void fix() { - ring.add(ring.get().fix()); - } - - public void inverse() { - ring.add(ring.get().inverse()); - } - - public void attach(GpxList toAttach) { - ring.add(ring.get().attach(toAttach)); - } - - public void cutPreceding() { - ring.add(ring.get().cutPreciding()); - } - - public void cutRemaining() { - ring.add(ring.get().cutRemaining()); - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/GpxEditor.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/GpxEditor.kt new file mode 100644 index 000000000..4b344f595 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/GpxEditor.kt @@ -0,0 +1,93 @@ +package ch.bailu.aat_lib.service.editor + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface +import ch.bailu.aat_lib.gpx.interfaces.GpxType + +class GpxEditor(list: GpxList) { + private val ring: EditorRing = if (list.pointList.size() > 0) { + EditorRing( + (NodeEditor( + (list.pointList.first as GpxPointNode?)!!, + list + )) + ) + } else { + EditorRing(NodeEditor()) + } + + fun select(point: GpxPointNode, list: GpxList) { + ring.set(NodeEditor(point, list)) + } + + fun clear() { + ring.add(NodeEditor()) + } + + fun unlinkSelectedNode() { + ring.add(ring.get().unlink()) + } + + fun insertNode(point: GpxPointInterface) { + ring.add(ring.get().insert(point)) + } + + fun moveSelectedUp() { + val point: GpxPointInterface = ring.get().point + + ring.add(ring.get().unlink()) + ring.set(ring.get().previous()) + ring.set(ring.get().insert(point)) + } + + fun moveSelectedDown() { + val point: GpxPointInterface = ring.get().point + + ring.add(ring.get().unlink()) + ring.set(ring.get().next()) + ring.set(ring.get().insert(point)) + } + + val list: GpxList + get() = ring.get().list + + val selectedPoint: GpxPointNode + get() = ring.get().point + + fun setType(type: GpxType) { + ring.get().list.setType(type) + } + + fun undo(): Boolean { + return ring.undo() + } + + fun redo(): Boolean { + return ring.redo() + } + + fun simplify() { + ring.add(ring.get().simplify()) + } + + fun fix() { + ring.add(ring.get().fix()) + } + + fun inverse() { + ring.add(ring.get().inverse()) + } + + fun attach(toAttach: GpxList) { + ring.add(ring.get().attach(toAttach)) + } + + fun cutPreceding() { + ring.add(ring.get().cutPreceding()) + } + + fun cutRemaining() { + ring.add(ring.get().cutRemaining()) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/NodeEditor.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/NodeEditor.java deleted file mode 100644 index 1e30a6aea..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/NodeEditor.java +++ /dev/null @@ -1,371 +0,0 @@ -package ch.bailu.aat_lib.service.editor; - -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.tools.Attacher; -import ch.bailu.aat_lib.gpx.tools.Copier; -import ch.bailu.aat_lib.gpx.tools.Inverser; -import ch.bailu.aat_lib.gpx.tools.SimplifierBearing; -import ch.bailu.aat_lib.gpx.tools.SimplifierDistance; -import ch.bailu.aat_lib.gpx.tools.TimeStampFixer; -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxPoint; -import ch.bailu.aat_lib.gpx.GpxPointFirstNode; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; -import ch.bailu.aat_lib.gpx.attributes.GpxAttributesNull; -import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes; -import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface; -import ch.bailu.aat_lib.gpx.interfaces.GpxType; - -public final class NodeEditor { - private final GpxList gpxList; - private final GpxPointNode node; - - - public NodeEditor() { - this(GpxType.ROUTE); - } - - public NodeEditor(GpxType t) { - gpxList = new GpxList(t, GpxListAttributes.NULL); - node = new GpxPointFirstNode(GpxPoint.NULL, GpxAttributesNull.NULL); - } - - public NodeEditor(GpxPointNode n, GpxList l) { - node = n; - gpxList = l; - } - - - public GpxPointNode getPoint() { - return node; - } - - - public GpxList getList() { - return gpxList; - } - - public NodeEditor simplify() { - SimplifierDistance distance = new SimplifierDistance(); - distance.walkTrack(gpxList); - - SimplifierBearing bearing = new SimplifierBearing(); - bearing.walkTrack(distance.getNewList()); - - return toEditor(bearing.getNewList()); - } - - - - public NodeEditor fix() { - TimeStampFixer fixer = new TimeStampFixer(); - fixer.walkTrack(gpxList); - - return toEditor(fixer.getNewList()); - } - - - public NodeEditor inverse() { - Inverser inverser = new Inverser(gpxList); - - return toEditor(inverser.getNewList()); - } - - public NodeEditor attach(GpxList toAttach) { - Copier copier = new Copier(); - copier.walkTrack(gpxList); - - Attacher attacher = new Attacher(copier.getNewList()); - attacher.walkTrack(toAttach); - - return toEditor(attacher.getNewList()); - } - - - private static NodeEditor toEditor(GpxList list) { - if (list.getPointList().size()>0) { - return new NodeEditor( - (GpxPointNode)list.getPointList().getFirst(), list); - } else { - return new NodeEditor(list.getDelta().getType()); - } - } - - public NodeEditor unlink() { - Unlinker unlinker = new Unlinker(); - unlinker.walkTrack(gpxList); - - return unlinker.getNewNode(); - } - - - public NodeEditor insert(GpxPointInterface point) { - Inserter inserter = new Inserter(point); - inserter.walkTrack(gpxList); - - return inserter.getNewNode(); - } - - - public NodeEditor previous() { - GpxPointNode newNode = (GpxPointNode)node.getPrevious(); - - if (newNode == null) return this; - return new NodeEditor(newNode, gpxList); - } - - - public NodeEditor next() { - GpxPointNode newNode = (GpxPointNode)node.getNext(); - - if (newNode == null) return this; - return new NodeEditor(newNode, gpxList); - } - - public NodeEditor cutPreciding() { - PrecedingCutter cutter = new PrecedingCutter(); - cutter.walkTrack(gpxList); - - return saveReturn(cutter.getNewNode()); - } - - - private NodeEditor saveReturn(NodeEditor n) { - if (n == null) return this; - return n; - } - - - public NodeEditor cutRemaining() { - RemainingCutter cutter = new RemainingCutter(); - cutter.walkTrack(gpxList); - - return saveReturn(cutter.getNewNode()); - } - - - private class Unlinker extends GpxListWalker { - private final GpxList newList = new GpxList(gpxList.getDelta().getType(), GpxListAttributes.NULL); - - private boolean startSegment=false; - private NodeEditor newNode = null; - - @Override - public boolean doList(GpxList track) { - return true; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - startSegment=true; - return true; - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - @Override - public void doPoint(GpxPointNode pointNode) { - if (pointNode == node) { - if (newList.getPointList().size()>0) { - newNode = new NodeEditor( - (GpxPointNode)newList.getPointList().getLast(), newList); - } - } else { - if (startSegment) { - newList.appendToNewSegment(pointNode.getPoint(), pointNode.getAttributes()); - startSegment=false; - } else { - newList.appendToCurrentSegment(pointNode.getPoint(), pointNode.getAttributes()); - } - } - } - - public NodeEditor getNewNode() { - if (newNode == null) { - if (newList.getPointList().size()>0) { - newNode = new NodeEditor( - (GpxPointNode)newList.getPointList().getFirst(), newList); - } else { - newNode = new NodeEditor(newList.getDelta().getType()); - } - } - return newNode; - } - } - - - private class Inserter extends GpxListWalker { - private final GpxList newList = new GpxList(gpxList.getDelta().getType(), - GpxListAttributes.NULL); - - private NodeEditor newNode = new NodeEditor(gpxList.getDelta().getType()); - private boolean startSegment=false; - private final GpxPointInterface newPoint; - - - public Inserter(GpxPointInterface point) { - newPoint = point; - } - - @Override - public boolean doList(GpxList track) { - return true; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - startSegment=true; - return true; - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - @Override - public void doPoint(GpxPointNode point) { - if (startSegment) { - newList.appendToNewSegment(point.getPoint(), point.getAttributes()); - startSegment=false; - } else { - newList.appendToCurrentSegment(point.getPoint(), point.getAttributes()); - } - - if (point == node) { - newList.appendToCurrentSegment(new GpxPoint(newPoint), - GpxAttributesNull.NULL); - newNode = insertNewPoint(); - } - } - - public NodeEditor getNewNode() { - if (newList.getPointList().size() == 0) { - newList.appendToCurrentSegment(new GpxPoint(newPoint), - GpxAttributesNull.NULL); - newNode = insertNewPoint(); - } - return newNode; - } - - private NodeEditor insertNewPoint() { - return new NodeEditor( - (GpxPointNode) newList.getPointList().getLast(), newList); - } - } - - - - - - - private class RemainingCutter extends GpxListWalker { - private final GpxList newList = new GpxList(gpxList.getDelta().getType(), GpxListAttributes.NULL); - - private boolean startSegment=false; - - private boolean goOn=true; - - @Override - public boolean doList(GpxList track) { - return goOn; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - startSegment=true; - return goOn; - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return goOn; - } - - @Override - public void doPoint(GpxPointNode pointNode) { - if (goOn) { - copyNode(pointNode); - if (pointNode == node) { - goOn = false; - } - } - } - - public void copyNode(GpxPointNode pointNode) { - if (startSegment) { - newList.appendToNewSegment(pointNode.getPoint(), pointNode.getAttributes()); - startSegment=false; - } else { - newList.appendToCurrentSegment(pointNode.getPoint(), pointNode.getAttributes()); - } - } - - - public NodeEditor getNewNode() { - if (newList.getPointList().size()>0) { - return new NodeEditor( - (GpxPointNode)newList.getPointList().getLast(), newList); - } - return null; - } - } - - - private class PrecedingCutter extends GpxListWalker { - private final GpxList newList = new GpxList(gpxList.getDelta().getType(), GpxListAttributes.NULL); - - private boolean startSegment=false; - private boolean start=false; - - @Override - public boolean doList(GpxList track) { - return true; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - startSegment=true; - return true; - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - @Override - public void doPoint(GpxPointNode pointNode) { - - if (pointNode == node) { - start = true; - - } - - if (start) { - copyNode(pointNode); - } - } - - public void copyNode(GpxPointNode pointNode) { - if (startSegment) { - newList.appendToNewSegment(pointNode.getPoint(), pointNode.getAttributes()); - startSegment=false; - } else { - newList.appendToCurrentSegment(pointNode.getPoint(), pointNode.getAttributes()); - } - } - - public NodeEditor getNewNode() { - if (newList.getPointList().size()>0) { - return new NodeEditor( - (GpxPointNode)newList.getPointList().getFirst(), newList); - } - return null; - } - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/NodeEditor.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/NodeEditor.kt new file mode 100644 index 000000000..8c9805c42 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/editor/NodeEditor.kt @@ -0,0 +1,346 @@ +package ch.bailu.aat_lib.service.editor + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPoint +import ch.bailu.aat_lib.gpx.GpxPointFirstNode +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode +import ch.bailu.aat_lib.gpx.attributes.GpxAttributesNull +import ch.bailu.aat_lib.gpx.attributes.GpxListAttributes +import ch.bailu.aat_lib.gpx.interfaces.GpxPointInterface +import ch.bailu.aat_lib.gpx.interfaces.GpxType +import ch.bailu.aat_lib.gpx.tools.Attacher +import ch.bailu.aat_lib.gpx.tools.Copier +import ch.bailu.aat_lib.gpx.tools.Inverser +import ch.bailu.aat_lib.gpx.tools.SimplifierBearing +import ch.bailu.aat_lib.gpx.tools.SimplifierDistance +import ch.bailu.aat_lib.gpx.tools.TimeStampFixer + +class NodeEditor { + val list: GpxList + val point: GpxPointNode + + + @JvmOverloads + constructor(type: GpxType = GpxType.ROUTE) { + this.list = GpxList(type, GpxListAttributes.NULL) + this.point = GpxPointFirstNode(GpxPoint.NULL, GpxAttributesNull.NULL) + } + + constructor(node: GpxPointNode, list: GpxList) { + this.point = node + this.list = list + } + + fun simplify(): NodeEditor { + val distance = SimplifierDistance() + distance.walkTrack(this.list) + + val bearing = SimplifierBearing() + val distanceList = distance.newList + if (distanceList is GpxList) { + bearing.walkTrack(distanceList) + val bearingList = bearing.newList + if (bearingList is GpxList) { + return toEditor(bearingList) + } + } + return this + } + + fun fix(): NodeEditor { + val fixer = TimeStampFixer() + fixer.walkTrack(this.list) + val list = fixer.newList + if (list is GpxList) { + return toEditor(list) + } + return this + } + + + fun inverse(): NodeEditor { + val inverser = Inverser(this.list) + return toEditor(inverser.newList) + } + + fun attach(toAttach: GpxList): NodeEditor { + val copier = Copier() + copier.walkTrack(this.list) + + val attacher = Attacher(copier.newList!!) + attacher.walkTrack(toAttach) + + return toEditor(attacher.newList) + } + + fun unlink(): NodeEditor { + val unlinker = Unlinker() + unlinker.walkTrack(this.list) + + return unlinker.getNewNode() + } + + fun insert(point: GpxPointInterface): NodeEditor { + val inserter = Inserter(point) + inserter.walkTrack(this.list) + + return inserter.getNewNode() + } + + fun previous(): NodeEditor { + val newNode = point.previous as GpxPointNode? + + if (newNode == null) return this + return NodeEditor(newNode, this.list) + } + + fun next(): NodeEditor { + val newNode = point.next as GpxPointNode? + + if (newNode == null) return this + return NodeEditor(newNode, this.list) + } + + fun cutPreceding(): NodeEditor { + val cutter = PrecedingCutter() + cutter.walkTrack(this.list) + + return saveReturn(cutter.newNode) + } + + private fun saveReturn(node: NodeEditor?): NodeEditor { + if (node == null) return this + return node + } + + fun cutRemaining(): NodeEditor { + val cutter = RemainingCutter() + cutter.walkTrack(this.list) + + return saveReturn(cutter.newNode) + } + + private inner class Unlinker : GpxListWalker() { + private val newList = GpxList(list.getDelta().getType(), GpxListAttributes.NULL) + + private var startSegment = false + private var newNode: NodeEditor? = null + + override fun doList(track: GpxList): Boolean { + return true + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + startSegment = true + return true + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + override fun doPoint(pointNode: GpxPointNode) { + if (pointNode === this@NodeEditor.point) { + if (newList.pointList.size() > 0) { + newNode = NodeEditor( + (newList.pointList.last as GpxPointNode?)!!, newList + ) + } + } else { + if (startSegment) { + newList.appendToNewSegment(pointNode.point, pointNode.getAttributes()) + startSegment = false + } else { + newList.appendToCurrentSegment(pointNode.point, pointNode.getAttributes()) + } + } + } + + fun getNewNode(): NodeEditor { + if (newNode == null) { + if (newList.pointList.size() > 0) { + newNode = NodeEditor( + (newList.pointList.first as GpxPointNode?)!!, newList + ) + } else { + newNode = NodeEditor(newList.getDelta().getType()) + } + } + return newNode!! + } + } + + + private inner class Inserter(private val newPoint: GpxPointInterface) : GpxListWalker() { + private val newList = GpxList( + list.getDelta().getType(), + GpxListAttributes.NULL + ) + + private var newNode = NodeEditor(list.getDelta().getType()) + private var startSegment = false + + + override fun doList(track: GpxList): Boolean { + return true + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + startSegment = true + return true + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + override fun doPoint(point: GpxPointNode) { + if (startSegment) { + newList.appendToNewSegment(point.point, point.getAttributes()) + startSegment = false + } else { + newList.appendToCurrentSegment(point.point, point.getAttributes()) + } + + if (point === this@NodeEditor.point) { + newList.appendToCurrentSegment( + GpxPoint(newPoint), + GpxAttributesNull.NULL + ) + newNode = insertNewPoint() + } + } + + fun getNewNode(): NodeEditor { + if (newList.pointList.size() == 0) { + newList.appendToCurrentSegment( + GpxPoint(newPoint), + GpxAttributesNull.NULL + ) + newNode = insertNewPoint() + } + return newNode + } + + fun insertNewPoint(): NodeEditor { + return NodeEditor( + (newList.pointList.last as GpxPointNode?)!!, newList + ) + } + } + + + private inner class RemainingCutter : GpxListWalker() { + private val newList = GpxList(list.getDelta().getType(), GpxListAttributes.NULL) + + private var startSegment = false + + private var goOn = true + + override fun doList(track: GpxList): Boolean { + return goOn + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + startSegment = true + return goOn + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return goOn + } + + override fun doPoint(pointNode: GpxPointNode) { + if (goOn) { + copyNode(pointNode) + if (pointNode === this@NodeEditor.point) { + goOn = false + } + } + } + + fun copyNode(pointNode: GpxPointNode) { + if (startSegment) { + newList.appendToNewSegment(pointNode.point, pointNode.getAttributes()) + startSegment = false + } else { + newList.appendToCurrentSegment(pointNode.point, pointNode.getAttributes()) + } + } + + + val newNode: NodeEditor? + get() { + if (newList.pointList.size() > 0) { + return NodeEditor( + (newList.pointList.last as GpxPointNode?)!!, newList + ) + } + return null + } + } + + + private inner class PrecedingCutter : GpxListWalker() { + private val newList = GpxList(list.getDelta().getType(), GpxListAttributes.NULL) + + private var startSegment = false + private var start = false + + override fun doList(track: GpxList): Boolean { + return true + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + startSegment = true + return true + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + override fun doPoint(pointNode: GpxPointNode) { + if (pointNode === this@NodeEditor.point) { + start = true + } + + if (start) { + copyNode(pointNode) + } + } + + fun copyNode(pointNode: GpxPointNode) { + if (startSegment) { + newList.appendToNewSegment(pointNode.point, pointNode.getAttributes()) + startSegment = false + } else { + newList.appendToCurrentSegment(pointNode.point, pointNode.getAttributes()) + } + } + + val newNode: NodeEditor? + get() { + if (newList.pointList.size() > 0) { + return NodeEditor( + (newList.pointList.first as GpxPointNode?)!!, newList + ) + } + return null + } + } + + companion object { + private fun toEditor(list: GpxList): NodeEditor { + if (list.pointList.size() > 0) { + return NodeEditor( + (list.pointList.first as GpxPointNode?)!!, list + ) + } else { + return NodeEditor(list.getDelta().getType()) + } + } + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMap.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMap.java deleted file mode 100644 index 18d5233e2..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMap.java +++ /dev/null @@ -1,45 +0,0 @@ -package ch.bailu.aat_lib.service.icons; - -import java.util.HashMap; - -import ch.bailu.aat_lib.util.WithStatusText; - -public final class IconMap implements WithStatusText { - public static class Icon { - public final String svg; - - public Icon(String file_name) { - svg = IconMapService.SVG_DIRECTORY + file_name + IconMapService.SVG_SUFFIX; - } - } - - private final HashMap> key_list = - new HashMap<>(); - - - public void add(int key, String value, String file_name) { - HashMap value_list = key_list.get(key); - - if (value_list == null) { - value_list = new HashMap<>(); - } - - value_list.put(value, new Icon(file_name)); - key_list.put(key, value_list); - } - - public Icon get(int keyIndex, String value) { - final HashMap value_list=key_list.get(keyIndex); - - if (value_list == null) { - return null; - } - return value_list.get(value); - } - - @Override - public void appendStatusText(StringBuilder builder) { - builder.append("IconMap (key_list) size: ").append(key_list.size()).append("
"); - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMap.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMap.kt new file mode 100644 index 000000000..d2ea924b3 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMap.kt @@ -0,0 +1,31 @@ +package ch.bailu.aat_lib.service.icons + +import ch.bailu.aat_lib.util.WithStatusText + +class IconMap : WithStatusText { + + class Icon(fileName: String) { + val svg = IconMapService.SVG_DIRECTORY + fileName + IconMapService.SVG_SUFFIX + } + + private val keyList = HashMap>() + + fun add(key: Int, value: String, fileName: String) { + val valueList = keyList.get(key) ?: HashMap() + valueList.put(value, Icon(fileName)) + keyList.put(key, valueList) + } + + fun get(keyIndex: Int, value: String): Icon? { + val valueList = keyList.get(keyIndex) + + if (valueList == null) { + return null + } + return valueList[value] + } + + override fun appendStatusText(builder: StringBuilder) { + builder.append("IconMap (key_list) size: ").append(keyList.size).append("
") + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapParser.java b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapParser.java deleted file mode 100644 index 272f8ec6b..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapParser.java +++ /dev/null @@ -1,69 +0,0 @@ -package ch.bailu.aat_lib.service.icons; - -import java.io.IOException; - -import ch.bailu.aat_lib.gpx.attributes.Keys; -import ch.bailu.aat_lib.file.xml.parser.util.Stream; -import ch.bailu.foc.Foc; - - -public final class IconMapParser { - private final static int ICON=0, KEY=1, VALUE=2, END=3, MAX=4; - - private final String[] entries = new String[MAX]; - private int entry=0; - - private final StringBuilder buffer = new StringBuilder(); - - IconMapParser(Foc file, IconMap map) throws IOException { - Stream stream = new Stream(file); - - stream.read(); - - while(true) { - if (stream.haveA('#')) stream.to('\n'); - - if (stream.haveA('\n')) { - addEntry(map); - stream.read(); - - } else if (stream.haveCharacter()) { - parseSubEntry(stream); - - } else if (stream.haveEOF()){ - break; - - } else { - stream.read(); - } - } - } - - private void parseSubEntry(Stream stream) throws IOException { - buffer.setLength(0); - - while( - stream.haveA('_') || - stream.haveA('/') || - stream.haveCharacter() || - stream.haveDigit()) - { - buffer.append((char)stream.get()); - stream.read(); - } - - entries[entry]=buffer.toString(); - - if (entry < END) { - entry++; - } - } - - private void addEntry(IconMap map) { - if (entry==END) { - map.add(Keys.toIndex(entries[KEY]), entries[VALUE], entries[ICON]); - } - entry=0; - - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapParser.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapParser.kt new file mode 100644 index 000000000..f1c81f0c7 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapParser.kt @@ -0,0 +1,66 @@ +package ch.bailu.aat_lib.service.icons + +import ch.bailu.aat_lib.file.xml.parser.util.Stream +import ch.bailu.aat_lib.gpx.attributes.Keys +import ch.bailu.foc.Foc +import java.io.IOException + +class IconMapParser(file: Foc, map: IconMap) { + private val entries = arrayOfNulls(MAX) + private var entry = 0 + private val buffer = StringBuilder() + + init { + val stream = Stream(file) + stream.read() + + while (!stream.haveEOF()) { + if (stream.haveA('#'.code)) { + stream.to('\n'.code) + } else if (stream.haveA('\n'.code)) { + addEntry(map) + stream.read() + } else if (stream.haveCharacter()) { + parseSubEntry(stream) + } else { + stream.read() + } + } + } + + @Throws(IOException::class) + private fun parseSubEntry(stream: Stream) { + buffer.setLength(0) + + while (stream.haveA('_'.code) || stream.haveA('/'.code) || stream.haveCharacter() || stream.haveDigit()) { + buffer.append(stream.get().toChar()) + stream.read() + } + + entries[entry] = buffer.toString() + + if (entry < END) { + entry++ + } + } + + private fun addEntry(map: IconMap) { + if (entry == END) { + val key = entries[KEY] + val value = entries[VALUE] + val icon = entries[ICON] + if (key != null && value != null && icon != null) { + map.add(Keys.toIndex(key), value, icon) + } + } + entry = 0 + } + + companion object { + private const val ICON = 0 + private const val KEY = 1 + private const val VALUE = 2 + private const val END = 3 + private const val MAX = 4 + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapService.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapService.kt index dd9bab5d0..1e74cc1d8 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapService.kt +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/service/icons/IconMapService.kt @@ -34,7 +34,7 @@ class IconMapService(sc: ServicesInterface, focFactory: FocFactory) : VirtualSer } override fun toAssetPath(key: Int, value: String): String? { - val icon: IconMap.Icon? = map[key, value] + val icon: IconMap.Icon? = map.get(key, value) return icon?.svg } @@ -45,8 +45,10 @@ class IconMapService(sc: ServicesInterface, focFactory: FocFactory) : VirtualSer private fun getIconEntry(attr: GpxAttributes): IconMap.Icon? { for (i in 0 until attr.size()) { - val icon = map[attr.getKeyAt(i), attr.getAt(i)] - if (icon != null) return icon + val icon = map.get(attr.getKeyAt(i), attr.getAt(i)) + if (icon != null) { + return icon + } } return getIconEntryNominatimType(attr) } @@ -55,7 +57,7 @@ class IconMapService(sc: ServicesInterface, focFactory: FocFactory) : VirtualSer if (attr.hasKey(N_KEY_KEY) && attr.hasKey(N_KEY_VALUE)) { val key = toIndex(attr[N_KEY_KEY]) val value = attr[N_KEY_VALUE] - return map[key, value] + return map.get(key, value) } return null } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/DateUtil.java b/aat-lib/src/main/java/ch/bailu/aat_lib/util/DateUtil.java deleted file mode 100644 index 2615b866c..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/util/DateUtil.java +++ /dev/null @@ -1,14 +0,0 @@ -package ch.bailu.aat_lib.util; - -public class DateUtil { - - public static final long SECONDS_IN_MINUTE = 60; - public static final long MINUTES_IN_HOUR = 60; - public static final long HOURS_IN_DAY = 24; - public static final long DAYS_IN_YEAR = 365; - public static final long MILLIS_IN_SECOND = 1000; - - public static final long MILLIS_IN_HOUR = MINUTES_IN_HOUR * SECONDS_IN_MINUTE * MILLIS_IN_SECOND; - public static final long MILLIS_IN_DAY = MILLIS_IN_HOUR * HOURS_IN_DAY; - public static final long MILLIS_IN_YEAR = MILLIS_IN_DAY * DAYS_IN_YEAR; -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/DateUtil.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/util/DateUtil.kt new file mode 100644 index 000000000..76b21c7d1 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/util/DateUtil.kt @@ -0,0 +1,13 @@ +package ch.bailu.aat_lib.util + +object DateUtil { + const val SECONDS_IN_MINUTE: Long = 60 + const val MINUTES_IN_HOUR: Long = 60 + const val HOURS_IN_DAY: Long = 24 + const val DAYS_IN_YEAR: Long = 365 + const val MILLIS_IN_SECOND: Long = 1000 + + const val MILLIS_IN_HOUR: Long = MINUTES_IN_HOUR * SECONDS_IN_MINUTE * MILLIS_IN_SECOND + const val MILLIS_IN_DAY: Long = MILLIS_IN_HOUR * HOURS_IN_DAY + const val MILLIS_IN_YEAR: Long = MILLIS_IN_DAY * DAYS_IN_YEAR +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/Limit.java b/aat-lib/src/main/java/ch/bailu/aat_lib/util/Limit.java deleted file mode 100644 index 2e62cae3c..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/util/Limit.java +++ /dev/null @@ -1,39 +0,0 @@ -package ch.bailu.aat_lib.util; - -public class Limit { - - public static int clamp(int val, int min, int max) { - val = Math.max(val, min); - val = Math.min(val, max); - return val; - } - - public static int smallest(Integer value, Integer ...values) { - for(Integer v : values) { - value = Math.min(value, v); - } - return value; - } - - public static int biggest(Integer value, Integer ...values) { - for(Integer val : values) { - value = Math.max(value, val); - } - return value; - } - - public static double smallest(Double value, Double ...values) { - for(Double v : values) { - value = Math.min(value, v); - } - return value; - } - - public static double biggest(Double value, Double ...values) { - for(Double v : values) { - value = Math.max(value, v); - } - return value; - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/Limit.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/util/Limit.kt new file mode 100644 index 000000000..94349af56 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/util/Limit.kt @@ -0,0 +1,50 @@ +package ch.bailu.aat_lib.util + +import kotlin.math.max +import kotlin.math.min + +object Limit { + @JvmStatic + fun clamp(value: Int, min: Int, max: Int): Int { + var result = value + result = max(result, min) + result = min(result, max) + return result + } + + @JvmStatic + fun smallest(value: Int, vararg values: Int): Int { + var result = value + for (v in values) { + result = min(result, v) + } + return result + } + + @JvmStatic + fun biggest(value: Int, vararg values: Int): Int { + var result = value + for (v in values) { + result = max(result, v) + } + return result + } + + @JvmStatic + fun smallest(value: Double, vararg values: Double): Double { + var value = value + for (v in values) { + value = min(value, v) + } + return value + } + + @JvmStatic + fun biggest(value: Double, vararg values: Double): Double { + var value = value + for (v in values) { + value = max(value, v) + } + return value + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/MemSize.java b/aat-lib/src/main/java/ch/bailu/aat_lib/util/MemSize.java deleted file mode 100644 index fb2213887..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/util/MemSize.java +++ /dev/null @@ -1,67 +0,0 @@ -package ch.bailu.aat_lib.util; - -import java.text.DecimalFormat; - -public class MemSize { - public final static long KB=1024; - public final static long MB=1024*KB; - public final static long GB=1024*MB; - - public final static DecimalFormat dec = new DecimalFormat("0.00"); - - public final static long[] ldivider = { - 1, KB, MB, GB - }; - - public final static double[] ddivider = { - 1, KB, MB, GB - }; - - public final static String[] unit = { - "B", "K", "M", "G", - }; - - - public static StringBuilder describe(StringBuilder out, double size) { - int i = ddivider.length; - - while (i>0) { - i--; - if (Math.abs(size) >= ddivider[i]) - break; - } - out.append(dec.format(size / ddivider[i])); - out.append(unit[i]); - return out; - } - - - - public static String describe(StringBuilder out, long size) { - int i = ldivider.length; - - while (i>0) { - i--; - if (Math.abs(size) >= ldivider[i]) - break; - } - - out.append(size/ ldivider[i]); - out.append(unit[i]); - return out.toString(); - } - - - public static long round(long size) { - int i = ldivider.length; - - while (i>0) { - i--; - if (Math.abs(size) >= ldivider[i]) - break; - } - - size = Math.round(size / (double)ldivider[i]); - return size * ldivider[i]; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/MemSize.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/util/MemSize.kt new file mode 100644 index 000000000..8f46889dd --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/util/MemSize.kt @@ -0,0 +1,54 @@ +package ch.bailu.aat_lib.util + +import java.text.DecimalFormat +import kotlin.math.abs +import kotlin.math.roundToLong + +object MemSize { + const val KB: Long = 1024 + const val MB: Long = 1024 * KB + const val GB: Long = 1024 * MB + + val dec: DecimalFormat = DecimalFormat("0.00") + val ldivider: LongArray = longArrayOf(1, KB, MB, GB) + val ddivider: DoubleArray = doubleArrayOf(1.0, KB.toDouble(), MB.toDouble(), GB.toDouble()) + val unit: Array = arrayOf("B", "K", "M", "G") + + fun describe(out: StringBuilder, size: Double): StringBuilder { + var i = ddivider.size + + while (i > 0) { + i-- + if (abs(size) >= ddivider[i]) break + } + out.append(dec.format(size / ddivider[i])) + out.append(unit[i]) + return out + } + + fun describe(out: StringBuilder, size: Long): String { + var i = ldivider.size + + while (i > 0) { + i-- + if (abs(size) >= ldivider[i]) break + } + + out.append(size / ldivider[i]) + out.append(unit[i]) + return out.toString() + } + + fun round(size: Long): Long { + var size = size + var i = ldivider.size + + while (i > 0) { + i-- + if (abs(size) >= ldivider[i]) break + } + + size = (size / ldivider[i].toDouble()).roundToLong() + return size * ldivider[i] + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/util/Timer.java b/aat-lib/src/main/java/ch/bailu/aat_lib/util/Timer.kt similarity index 68% rename from aat-lib/src/main/java/ch/bailu/aat_lib/util/Timer.java rename to aat-lib/src/main/java/ch/bailu/aat_lib/util/Timer.kt index fcb99efcb..5c13e076e 100644 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/util/Timer.java +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/util/Timer.kt @@ -1,15 +1,15 @@ -package ch.bailu.aat_lib.util; +package ch.bailu.aat_lib.util /** * Interface for UI Thread Timer */ -public interface Timer { +interface Timer { /** * Call the provided lambda after a specific interval. * The lambda gets called from the UI Thread * @param run function to call * @param interval delay before calling function */ - void kick(long interval, Runnable run); - void cancel(); + fun kick(interval: Long, run: Runnable) + fun cancel() } diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/cockpit/Layouter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/cockpit/Layouter.java deleted file mode 100644 index c17e1433a..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/cockpit/Layouter.java +++ /dev/null @@ -1,124 +0,0 @@ -package ch.bailu.aat_lib.view.cockpit; - -import java.util.List; - -import ch.bailu.aat_lib.description.ContentDescription; - -public class Layouter { - private static final int MAX_CHARS_PER_LINE=50; - private int parent_width=0; - private int parent_height=0; - - private final Placer placer = new Placer((index, x, y, w, h) -> {}); - private final Placer realPlacer; - - private final List items; - - - public Layouter(List items, DoPlacement doPlacement) { - this.items = items; - this.realPlacer = new Placer(doPlacement); - } - - public void layout(int w, int h) { - int chars = 1; - - parent_width = w; - parent_height = h; - - while (chars < MAX_CHARS_PER_LINE) { - if (tryPlacement(chars)) { - doPlacement(chars); - break; - } else chars++; - } - } - - - private boolean tryPlacement(int charsPerLine) { - placer.place(charsPerLine); - return placeItems(placer); - } - - private boolean doPlacement(int charsPerLine) { - realPlacer.place(charsPerLine); - return placeItems(realPlacer); - } - - private boolean placeItems(Placer p) { - boolean works=true; - - final int size = items.size(); - for (int i=0; i< size && works; i++) { - works = p.placeItem(i); - } - return works; - } - - private class Placer { - private static final int MIN_CHARS = 4; - private static final int RATIO=3; - private int char_width, char_height, xpos, ypos; - - private final DoPlacement doPlacement; - - public Placer(DoPlacement doPlacement) { - this.doPlacement = doPlacement; - } - - public void place(int charsPerLine) { - xpos=ypos=0; - calculateCharGeometry(charsPerLine); - } - - private void calculateCharGeometry(int charsPerLine) { - char_width=parent_width/charsPerLine; - - char_height=char_width*RATIO; - if (char_height > parent_height) { - char_height=parent_height; - char_width=char_height/RATIO; - } - } - - public boolean placeItem(int index) { - boolean works=true; - int width=getWidthOfView(index); - - if (width>parent_width) { - works=false; - } else if (width+xpos > parent_width) { - works=addLine(); - if (works) works = placeItem(index); - } else { - setGeometry(index, width, char_height); - xpos += width; - } - return works; - } - - protected void setGeometry(int index, int width, int height) { - doPlacement.setGeometry(index, getXPos(),getYPos(), getXPos()+width, getYPos()+height); - } - - protected int getXPos() {return xpos;} - protected int getYPos() {return ypos;} - - private boolean addLine() { - ypos+=char_height; - xpos=0; - return ((char_height + ypos) <= parent_height); - } - - - private int getWidthOfView(int index) { - int len= items.get(index).getValue().length(); - len = Math.max(len, MIN_CHARS); - return len*char_width; - } - } - - public interface DoPlacement { - void setGeometry(int index, int x, int y, int x2, int y2); - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/cockpit/Layouter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/cockpit/Layouter.kt new file mode 100644 index 000000000..7183566fc --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/cockpit/Layouter.kt @@ -0,0 +1,114 @@ +package ch.bailu.aat_lib.view.cockpit + +import ch.bailu.aat_lib.description.ContentDescription +import kotlin.math.max + +class Layouter(private val items: MutableList, doPlacement: DoPlacement) { + private var parentWidth = 0 + private var parentHeight = 0 + + private val placer: Placer = Placer({ index: Int, x: Int, y: Int, w: Int, h: Int -> }) + private val realPlacer: Placer = Placer(doPlacement) + + fun layout(w: Int, h: Int) { + var chars = 1 + + parentWidth = w + parentHeight = h + + while (chars < MAX_CHARS_PER_LINE) { + if (tryPlacement(chars)) { + doPlacement(chars) + break + } else chars++ + } + } + + private fun tryPlacement(charsPerLine: Int): Boolean { + placer.place(charsPerLine) + return placeItems(placer) + } + + private fun doPlacement(charsPerLine: Int): Boolean { + realPlacer.place(charsPerLine) + return placeItems(realPlacer) + } + + private fun placeItems(p: Placer): Boolean { + var works = true + + val size = items.size + var i = 0 + while (i < size && works) { + works = p.placeItem(i) + i++ + } + return works + } + + private inner class Placer(private val doPlacement: DoPlacement) { + private var charWidth = 0 + private var charHeight = 0 + private var xPos: Int = 0 + private var yPos: Int = 0 + + fun place(charsPerLine: Int) { + this.yPos = 0 + this.xPos = 0 + calculateCharGeometry(charsPerLine) + } + + fun calculateCharGeometry(charsPerLine: Int) { + charWidth = parentWidth / charsPerLine + + charHeight = charWidth * RATIO + if (charHeight > parentHeight) { + charHeight = parentHeight + charWidth = charHeight / RATIO + } + } + + fun placeItem(index: Int): Boolean { + var works = true + val width = getWidthOfView(index) + + if (width > parentWidth) { + works = false + } else if (width + this.xPos > parentWidth) { + works = addLine() + if (works) works = placeItem(index) + } else { + setGeometry(index, width, charHeight) + this.xPos += width + } + return works + } + + private fun setGeometry(index: Int, width: Int, height: Int) { + doPlacement.setGeometry(index, this.xPos, this.yPos, this.xPos + width, this.yPos + height) + } + + fun addLine(): Boolean { + this.yPos += charHeight + this.xPos = 0 + return ((charHeight + this.yPos) <= parentHeight) + } + + fun getWidthOfView(index: Int): Int { + var len = items[index].getValue().length + len = max(len, MIN_CHARS) + return len * charWidth + } + } + + fun interface DoPlacement { + fun setGeometry(index: Int, x: Int, y: Int, x2: Int, y2: Int) + } + + companion object { + private const val MAX_CHARS_PER_LINE = 50 + private const val MIN_CHARS = 4 + private const val RATIO = 3 + + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Config.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Config.java deleted file mode 100644 index 8baf23303..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Config.java +++ /dev/null @@ -1,5 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -public class Config { - public final static int SAMPLE_WIDTH_PIXEL=5; -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Config.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Config.kt new file mode 100644 index 000000000..b1c830ec1 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Config.kt @@ -0,0 +1,5 @@ +package ch.bailu.aat_lib.view.graph + +object Config { + const val SAMPLE_WIDTH_PIXEL = 5 +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceAltitudePlotter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceAltitudePlotter.java deleted file mode 100644 index ae15f0ba4..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceAltitudePlotter.java +++ /dev/null @@ -1,71 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.preferences.general.SolidUnit; -import ch.bailu.aat_lib.resources.Res; -import ch.bailu.aat_lib.lib.color.ColorInterface; - -public class DistanceAltitudePlotter extends Plotter { - - private final SolidUnit sunit; - private final Segment segment = new Segment(); - - public DistanceAltitudePlotter(SolidUnit sunit) { - this.sunit = sunit; - } - - @Override - public void plot(GraphCanvas canvas, PlotterConfig config) { - - boolean markerMode = config.getList().getMarkerList().size() > config.getWidth() / Config.SAMPLE_WIDTH_PIXEL; - - DistanceWalker distances = new DistanceWalker(segment); - distances.walkTrack(config.getList()); - - int km_factor = (int) (distances.getDistanceDelta()/1000) + 1; - - GraphPlotter plotter = new GraphPlotter(canvas, config.getWidth(), config.getHeight(), 1000 * km_factor); - - GpxListWalker painter, scaleGenerator; - - int minDistance = (int)distances.getDistanceDelta() / Math.max(config.getWidth(), 1); - if (segment.isValid()) { - painter = new GraphPainterLimit(plotter, segment, minDistance); - scaleGenerator = new ScaleGeneratorSegmented(plotter, segment); - - } else if (markerMode) { - painter = new GraphPainterMarkerMode(plotter, minDistance); - scaleGenerator = new ScaleGeneratorMarkerMode(plotter); - } else { - painter = new GraphPainter(plotter, minDistance); - scaleGenerator = new ScaleGenerator(plotter); - - } - - scaleGenerator.walkTrack(config.getList()); - plotter.roundYScale(50); - - - painter.walkTrack(config.getList()); - - - new SegmentNodePainter(plotter, distances.getDistanceOffset()).walkTrack(config.getList()); - if (config.getIndex() > -1) { - new IndexPainter(plotter, config.getIndex(), distances.getDistanceOffset()).walkTrack(config.getList()); - } - - plotter.drawXScale(5, sunit.getDistanceFactor(), config.isXLabelVisible()); - plotter.drawYScale(5, sunit.getAltitudeFactor(), true); - - } - - @Override - public void initLabels(LabelInterface labels) { - labels.setText(ColorInterface.WHITE, Res.str().altitude(), sunit.getAltitudeUnit()); - } - - @Override - public void setLimit(int firstPoint, int lastPoint) { - segment.setLimit(firstPoint, lastPoint); - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceAltitudePlotter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceAltitudePlotter.kt new file mode 100644 index 000000000..8c33ba1cd --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceAltitudePlotter.kt @@ -0,0 +1,70 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.lib.color.ColorInterface +import ch.bailu.aat_lib.preferences.general.SolidUnit +import ch.bailu.aat_lib.resources.Res.str +import kotlin.math.max + +class DistanceAltitudePlotter(private val sunit: SolidUnit) : Plotter() { + private val segment = Segment() + + override fun plot(canvas: GraphCanvas, config: PlotterConfig) { + val markerMode = + config.getList().markerList.size() > config.getWidth() / Config.SAMPLE_WIDTH_PIXEL + + val distances = DistanceWalker(segment) + distances.walkTrack(config.getList()) + + val kmFactor = (distances.distanceDelta / 1000).toInt() + 1 + + val plotter = GraphPlotter( + canvas, + config.getWidth(), + config.getHeight(), + (1000 * kmFactor).toFloat() + ) + + val painter: GpxListWalker? + val scaleGenerator: GpxListWalker? + + val minDistance = distances.distanceDelta.toInt() / max(config.getWidth(), 1) + if (segment.isValid()) { + painter = GraphPainterLimit(plotter, segment, minDistance) + scaleGenerator = ScaleGeneratorSegmented(plotter, segment) + } else if (markerMode) { + painter = GraphPainterMarkerMode(plotter, minDistance) + scaleGenerator = ScaleGeneratorMarkerMode(plotter) + } else { + painter = GraphPainter(plotter, minDistance) + scaleGenerator = ScaleGenerator(plotter) + } + + scaleGenerator.walkTrack(config.getList()) + plotter.roundYScale(50) + + + painter.walkTrack(config.getList()) + + + SegmentNodePainter(plotter, distances.distanceOffset).walkTrack(config.getList()) + if (config.getIndex() > -1) { + IndexPainter( + plotter, + config.getIndex(), + distances.distanceOffset + ).walkTrack(config.getList()) + } + + plotter.drawXScale(5, sunit.distanceFactor, config.isXLabelVisible()) + plotter.drawYScale(5, sunit.altitudeFactor, true) + } + + override fun initLabels(labels: LabelInterface) { + labels.setText(ColorInterface.WHITE, str().altitude(), sunit.altitudeUnit) + } + + override fun setLimit(firstPoint: Int, lastPoint: Int) { + segment.setLimit(firstPoint, lastPoint) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPainter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPainter.java deleted file mode 100644 index 4dfe48b44..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPainter.java +++ /dev/null @@ -1,102 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.app.AppColor; -import ch.bailu.aat_lib.gpx.GpxDistanceWindow; -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; -import ch.bailu.aat_lib.gpx.attributes.AutoPause; - -public class DistanceSpeedPainter extends GpxListWalker { - - private final float minDistance; - private final GpxDistanceWindow window; - private final AutoPause autoPause; - private final GraphPlotter[] plotter; - - private float totalDistance=0; - private long totalTime=0; - - private float distanceOfSample=0; - private long timeOfSample=0; - - - public DistanceSpeedPainter(GraphPlotter[] plotter, - AutoPause autoPause, - float minDistance, - GpxDistanceWindow window) { - this.plotter = plotter; - this.autoPause = autoPause; - this.minDistance = minDistance; - this.window = window; - } - - - - @Override - public boolean doList(GpxList track) { - return true; - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - - @Override - public void doPoint(GpxPointNode point) { - window.forward(point); - autoPause.update(point); - increment(point.getDistance(), point.getTimeDelta()); - plotIfDistance(); - } - - public void increment(float distance, long time) { - distanceOfSample += distance; - timeOfSample += time; - } - - - public void plotIfDistance() { - if (distanceOfSample >= minDistance) { - totalTime+=timeOfSample; - totalDistance += distanceOfSample; - - plotTotalAverage(); - plotAverage(); - - timeOfSample=0; - distanceOfSample=0; - } - } - - - private void plotAverage() { - if (window.getTimeDelta() > 0) { - float avg=window.getSpeed(); - plotter[0].plotData(totalDistance, avg, AppColor.HL_ORANGE); - } - } - - - private void plotTotalAverage() { - long timeDelta = totalTime - autoPause.getPauseTime(); - - if (timeDelta > 0) { - float avg = totalDistance / totalTime * 1000; - plotter[1].plotData(totalDistance, avg, AppColor.HL_GREEN); - - float avgAp=totalDistance / timeDelta * 1000; - plotter[2].plotData(totalDistance, avgAp, AppColor.HL_BLUE); - - } - } - - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return true; - } -} \ No newline at end of file diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPainter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPainter.kt new file mode 100644 index 000000000..3382a6d53 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPainter.kt @@ -0,0 +1,82 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.app.AppColor +import ch.bailu.aat_lib.gpx.GpxDistanceWindow +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode +import ch.bailu.aat_lib.gpx.attributes.AutoPause + +class DistanceSpeedPainter( + private val plotter: List, + private val autoPause: AutoPause, + private val minDistance: Float, + private val window: GpxDistanceWindow) + : GpxListWalker() { + + private var totalDistance = 0f + private var totalTime: Long = 0 + private var distanceOfSample = 0f + private var timeOfSample: Long = 0 + + override fun doList(track: GpxList): Boolean { + return true + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + override fun doPoint(point: GpxPointNode) { + window.forward(point) + autoPause.update(point) + increment(point.getDistance(), point.getTimeDelta()) + plotIfDistance() + } + + fun increment(distance: Float, time: Long) { + distanceOfSample += distance + timeOfSample += time + } + + + fun plotIfDistance() { + if (distanceOfSample >= minDistance) { + totalTime += timeOfSample + totalDistance += distanceOfSample + + plotTotalAverage() + plotAverage() + + timeOfSample = 0 + distanceOfSample = 0f + } + } + + + private fun plotAverage() { + if (window.getTimeDelta() > 0) { + val avg = window.getSpeed() + plotter[0].plotData(totalDistance, avg, AppColor.HL_ORANGE) + } + } + + + private fun plotTotalAverage() { + val timeDelta = totalTime - autoPause.getPauseTime() + + if (timeDelta > 0) { + val avg = totalDistance / totalTime * 1000 + plotter[1].plotData(totalDistance, avg, AppColor.HL_GREEN) + + val avgAp = totalDistance / timeDelta * 1000 + plotter[2].plotData(totalDistance, avgAp, AppColor.HL_BLUE) + } + } + + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return true + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPlotter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPlotter.java deleted file mode 100644 index 262501039..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceSpeedPlotter.java +++ /dev/null @@ -1,88 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.app.AppColor; -import ch.bailu.aat_lib.description.AverageSpeedDescription; -import ch.bailu.aat_lib.description.AverageSpeedDescriptionAP; -import ch.bailu.aat_lib.gpx.GpxDistanceWindow; -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.attributes.AutoPause; -import ch.bailu.aat_lib.gpx.attributes.MaxSpeed; -import ch.bailu.aat_lib.preferences.SolidAutopause; -import ch.bailu.aat_lib.preferences.StorageInterface; -import ch.bailu.aat_lib.preferences.general.SolidPostprocessedAutopause; -import ch.bailu.aat_lib.preferences.general.SolidUnit; -import ch.bailu.aat_lib.preferences.presets.SolidPreset; -import ch.bailu.aat_lib.resources.Res; -import ch.bailu.aat_lib.lib.color.ColorInterface; - -public class DistanceSpeedPlotter extends Plotter{ - - private final StorageInterface storage; - private final SolidUnit sunit; - - public DistanceSpeedPlotter(SolidUnit sunit) { - this.storage = sunit.getStorage(); - this.sunit = sunit; - } - - - @Override - public void plot(GraphCanvas canvas, PlotterConfig config) { - GraphPlotter[] plotter = initPlotter(canvas, config.getWidth(), config.getHeight(), config.getList()); - GpxDistanceWindow window = new GpxDistanceWindow(config.getList()); - - config.getLabels().setText(AppColor.HL_ORANGE, window.getLimitAsString(storage)); - - new DistanceSpeedPainter(plotter, getAutoPause(), getMinDistance(config.getList(), config.getWidth()), window) - .walkTrack(config.getList()); - - plotter[0].drawXScale(5, sunit.getDistanceFactor(), config.isXLabelVisible()); - plotter[0].drawYScale(5, sunit.getSpeedFactor(), false); - } - - @Override - public void initLabels(LabelInterface labels) { - labels.setText(ColorInterface.WHITE, Res.str().speed(), sunit.getSpeedUnit()); - labels.setText(AppColor.HL_BLUE, new AverageSpeedDescriptionAP(sunit.getStorage()).getLabel()); - labels.setText(AppColor.HL_GREEN, new AverageSpeedDescription(sunit.getStorage()).getLabel()); - } - - @Override - public void setLimit(int firstPoint, int lastPoint) { - - } - - - private float getMinDistance(GpxList list, int width) { - float distForOnePixel = list.getDelta().getDistance() / Math.max(width, 1); - return distForOnePixel * Config.SAMPLE_WIDTH_PIXEL; - } - - - private AutoPause getAutoPause() { - int preset = new SolidPreset(storage).getIndex(); - final SolidAutopause spause = new SolidPostprocessedAutopause(storage, preset); - return new AutoPause.Time( - spause.getTriggerSpeed(), - spause.getTriggerLevelMillis()); - } - - private GraphPlotter[] initPlotter(GraphCanvas canvas, int widht, int height, GpxList list) { - int km_factor = (int) (list.getDelta().getDistance()/1000) + 1; - int xscale = km_factor * 1000; - - float maxSpeed = list.getDelta().getAttributes().getAsFloat((MaxSpeed.INDEX_MAX_SPEED)); - return initPlotter(canvas, xscale, widht, height, maxSpeed); - } - - - private GraphPlotter[] initPlotter(GraphCanvas canvas, int xscale, int width, int height, float maxSpeed) { - GraphPlotter[] result = new GraphPlotter[3]; - for (int i=0; i { + val kmFactor = (list.getDelta().getDistance() / 1000).toInt() + 1 + val xscale = kmFactor * 1000 + val maxSpeed = list.getDelta().getAttributes().getAsFloat((MaxSpeed.INDEX_MAX_SPEED)) + return initPlotter(canvas, xscale, width, height, maxSpeed) + } + private fun initPlotter( + canvas: GraphCanvas, + xscale: Int, + width: Int, + height: Int, + maxSpeed: Float + ): List { + val result = ArrayList(PLOT_COUNT) + repeat(PLOT_COUNT) { + val plotter = GraphPlotter(canvas, width, height, xscale.toFloat()) + plotter.includeInYScale(0f) + plotter.includeInYScale(maxSpeed) + result.add(plotter) + } + return result + } + + companion object { + const val PLOT_COUNT = 3 + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceWalker.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceWalker.java deleted file mode 100644 index cb24f2780..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceWalker.java +++ /dev/null @@ -1,76 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; - -public class DistanceWalker extends GpxListWalker { - private int index = 0; - private float dstDelta = 0f; - private float dstOffset = 0f; - private final Segment segment; - - public DistanceWalker(Segment segment) { - this.segment = segment; - } - - @Override - public boolean doList(GpxList track) { - if (segment.isValid()) { - return true; - } else { - dstDelta = track.getDelta().getDistance(); - return false; - } - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return doDelta(segment.getSegmentSize(), segment.getDistance()); - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return doDelta(marker.getSegmentSize(), marker.getDistance()); - } - - - private boolean doDelta(int size, float distance) { - - if (segment.isAfter(index)) { - return false; - - } else { - int nextIndex = index + size; - - if (segment.isBefore(nextIndex)) { - index = nextIndex; - dstOffset += distance; - return false; - } - } - return true; - } - - - @Override - public void doPoint(GpxPointNode point) { - if (segment.isBefore(index)) { - dstOffset += point.getDistance(); - - } else if (!segment.isAfter(index)) { - dstDelta += point.getDistance(); - } - index++; - } - - - public float getDistanceDelta() { - return dstDelta; - } - - public float getDistanceOffset() { - return dstOffset; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceWalker.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceWalker.kt new file mode 100644 index 000000000..69f308f66 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/DistanceWalker.kt @@ -0,0 +1,55 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode + +class DistanceWalker(private val segment: Segment) : GpxListWalker() { + private var index = 0 + var distanceDelta: Float = 0f + private set + var distanceOffset: Float = 0f + private set + + override fun doList(track: GpxList): Boolean { + if (segment.isValid()) { + return true + } else { + this.distanceDelta = track.getDelta().getDistance() + return false + } + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return doDelta(segment.segmentSize, segment.getDistance()) + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return doDelta(marker.segmentSize, marker.getDistance()) + } + + private fun doDelta(size: Int, distance: Float): Boolean { + if (segment.isAfter(index)) { + return false + } else { + val nextIndex = index + size + + if (segment.isBefore(nextIndex)) { + index = nextIndex + this.distanceOffset += distance + return false + } + } + return true + } + + override fun doPoint(point: GpxPointNode) { + if (segment.isBefore(index)) { + this.distanceOffset += point.getDistance() + } else if (!segment.isAfter(index)) { + this.distanceDelta += point.getDistance() + } + index++ + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphCanvas.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphCanvas.java deleted file mode 100644 index 53e9b7e6a..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphCanvas.java +++ /dev/null @@ -1,11 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.util.Point; - -public interface GraphCanvas { - void drawLine(int x1, int y1, int x2, int y2); - void drawLine(Point pa, Point pb, int color); - void drawText(String text, int x, int y); - void drawBitmap(Point pa, int color); - int getTextSize(); -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphCanvas.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphCanvas.kt new file mode 100644 index 000000000..891574aa9 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphCanvas.kt @@ -0,0 +1,11 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.util.Point + +interface GraphCanvas { + fun drawLine(x1: Int, y1: Int, x2: Int, y2: Int) + fun drawLine(pa: Point, pb: Point, color: Int) + fun drawText(text: String, x: Int, y: Int) + fun drawBitmap(pa: Point, color: Int) + fun getTextSize(): Int +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainter.java deleted file mode 100644 index 61ba39960..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainter.java +++ /dev/null @@ -1,64 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; -import ch.bailu.aat_lib.lib.color.AltitudeColorTable; - -public class GraphPainter extends GpxListWalker { - private final GraphPlotter plotter; - - private float distance=0; - private float summaryDistance=0; - private final float minDistance; - - - public GraphPainter(GraphPlotter p, int md) { - plotter=p; - minDistance=md * Config.SAMPLE_WIDTH_PIXEL; - } - - - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - - @Override - public void doPoint(GpxPointNode point) { - incrementSummaryDistance(point.getDistance()); - plotIfDistance(point); - } - - - public void incrementSummaryDistance(float distance) { - summaryDistance += distance; - } - - public void plotIfDistance(GpxPointNode point) { - if (summaryDistance >= minDistance) { - int altitude = (int)point.getAltitude(); - - distance+=summaryDistance; - summaryDistance=0; - - plotter.plotData(distance, altitude, AltitudeColorTable.instance().getColor(altitude)); - } - } - - - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return true; - } - - @Override - public boolean doList(GpxList track) { - return true; - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainter.kt new file mode 100644 index 000000000..a69d53f92 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainter.kt @@ -0,0 +1,49 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode +import ch.bailu.aat_lib.lib.color.AltitudeColorTable + +open class GraphPainter(private val plotter: GraphPlotter, md: Int) : GpxListWalker() { + private var distance = 0f + private var summaryDistance = 0f + private val minDistance: Float = (md * Config.SAMPLE_WIDTH_PIXEL).toFloat() + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + override fun doPoint(point: GpxPointNode) { + incrementSummaryDistance(point.getDistance()) + plotIfDistance(point) + } + + fun incrementSummaryDistance(distance: Float) { + summaryDistance += distance + } + + fun plotIfDistance(point: GpxPointNode) { + if (summaryDistance >= minDistance) { + val altitude = point.getAltitude().toInt() + + distance += summaryDistance + summaryDistance = 0f + + plotter.plotData( + distance, + altitude.toFloat(), + AltitudeColorTable.getColor(altitude) + ) + } + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return true + } + + override fun doList(track: GpxList): Boolean { + return true + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterLimit.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterLimit.java deleted file mode 100644 index 8c13e8e37..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterLimit.java +++ /dev/null @@ -1,21 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxPointNode; - -public class GraphPainterLimit extends GraphPainter { - private int index = 0; - private final Segment segment; - - public GraphPainterLimit(GraphPlotter p, Segment segment, int md) { - super(p, md); - this.segment = segment; - } - - @Override - public void doPoint(GpxPointNode point) { - if (segment.isInside(index)) { - super.doPoint(point); - } - index++; - } -} \ No newline at end of file diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterLimit.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterLimit.kt new file mode 100644 index 000000000..2f5146494 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterLimit.kt @@ -0,0 +1,15 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxPointNode + +class GraphPainterLimit(p: GraphPlotter, private val segment: Segment, md: Int) : + GraphPainter(p, md) { + private var index = 0 + + override fun doPoint(point: GpxPointNode) { + if (segment.isInside(index)) { + super.doPoint(point) + } + index++ + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterMarkerMode.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterMarkerMode.java deleted file mode 100644 index 1459a4901..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterMarkerMode.java +++ /dev/null @@ -1,21 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; - -public class GraphPainterMarkerMode extends GraphPainter { - - public GraphPainterMarkerMode(GraphPlotter p, int md) { - super(p, md); - } - - - @Override - public boolean doMarker(GpxSegmentNode marker) { - if (marker.getFirstNode() != null) { - plotIfDistance((GpxPointNode)marker.getFirstNode()); - incrementSummaryDistance(marker.getDistance()); - } - return false; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterMarkerMode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterMarkerMode.kt new file mode 100644 index 000000000..e116cf1e8 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPainterMarkerMode.kt @@ -0,0 +1,14 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode + +class GraphPainterMarkerMode(p: GraphPlotter, md: Int) : GraphPainter(p, md) { + override fun doMarker(marker: GpxSegmentNode): Boolean { + if (marker.firstNode is GpxPointNode) { + plotIfDistance(marker.firstNode) + incrementSummaryDistance(marker.getDistance()) + } + return false + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPlotter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPlotter.java deleted file mode 100644 index ac59087b0..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPlotter.java +++ /dev/null @@ -1,123 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.util.Point; - -public class GraphPlotter { - private final int text_size; - - private final Scaler xscaler; - private final InvertetOffsetScaler yscaler; - - private final GraphCanvas canvas; - - private final int width; - private final int height; - - private Point pointA=new Point(-5,-5), pointB = new Point(-5,-5); - - - public GraphPlotter(GraphCanvas c, int w, int h, float xScale) { - width=w; - height=h; - canvas = c; - - text_size = canvas.getTextSize(); - xscaler = new Scaler(width, xScale); - yscaler = new InvertetOffsetScaler(height); - - } - - public void roundYScale(int roundTo) { - yscaler.round(roundTo); - } - - public void inlcudeInYScale(float value) { - yscaler.addValue(value); - } - - public void drawYScale(int lines, float factor, boolean drawFirstValue) { - lines = Math.min(height / (text_size*2), lines); - lines = Math.max(1, lines); - - float space = yscaler.getRealDistance() / lines; - space = Math.max(1, space); - - for (float x=yscaler.getRealOffset(); x<= yscaler.getRealTop(); x+=space) { - drawHorizontalLine(x,factor, drawFirstValue); - } - } - - private void drawHorizontalLine(float value, float factor, boolean drawFirstValue) { - int pixel = (int)yscaler.scale(value); - - canvas.drawLine(0, pixel, width, pixel); - - if (drawFirstValue || pixel < height-text_size) - drawScaleText(0,pixel, String.valueOf((int) (value*factor)) ); - } - - private void drawScaleText(int x,int y, String value) { - int w = value.length()*text_size; - int h = text_size; - - if ((x+w) > width) x = width - w; - if ((y-h) < 0) y=h; - - canvas.drawText(value, x, y); - } - - public void drawXScale(int lines, float factor, boolean drawText) { - lines = Math.min(width / (text_size*4), lines); - lines = Math.max(1, lines); - - float space=xscaler.getReal()/lines; - space = Math.max(1, space); - - for (float x=0; x <= xscaler.getReal(); x+=space) { - drawVerticalLine(x, factor, drawText); - } - - } - - private void drawVerticalLine(float value, float factor, boolean drawText) { - int pixel = (int)xscaler.scale(value); - - canvas.drawLine(pixel, 0 , pixel, height); - - if (drawText && pixel > text_size) { - String text = String.valueOf((int) (value * factor)); - drawScaleText(pixel-text_size, height, text); - } - } - - - public int plotData(float xvalue, float yvalue, int color) { - int delta; - scaleToPixel(xvalue, yvalue); - - delta = Math.max(pointA.x - pointB.x, 0); - - if (delta > 1) { - if (pointB.x < 0) pointB.y=pointA.y; - canvas.drawLine(pointB, pointA, color); - switchPoints(); - } - return delta; - } - - private void scaleToPixel(float xvalue, float yvalue) { - pointA.x=(int)xscaler.scale(xvalue); - pointA.y=(int)yscaler.scale(yvalue); - } - - private void switchPoints() { - Point t=pointB; - pointB=pointA; - pointA=t; - } - - public void plotPoint(float xvalue, float yvalue, int color) { - scaleToPixel(xvalue,yvalue); - canvas.drawBitmap(pointA, color); - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPlotter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPlotter.kt new file mode 100644 index 000000000..e53e79273 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/GraphPlotter.kt @@ -0,0 +1,123 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.util.Point +import kotlin.math.max +import kotlin.math.min + +class GraphPlotter( + private val canvas: GraphCanvas, + private val width: Int, + private val height: Int, + xScale: Float +) { + private val textSize: Int = canvas.getTextSize() + + private val xScaler: Scaler = Scaler(width.toFloat(), xScale) + private val yScaler: InvertedOffsetScaler = InvertedOffsetScaler(height) + + private var pointA = Point(-5, -5) + private var pointB = Point(-5, -5) + + fun roundYScale(roundTo: Int) { + yScaler.round(roundTo) + } + + fun includeInYScale(value: Float) { + yScaler.addValue(value) + } + + fun drawYScale(lines: Int, factor: Float, drawFirstValue: Boolean) { + var lines = lines + lines = min(height / (textSize * 2), lines) + lines = max(1, lines) + + var space = yScaler.getRealDistance() / lines + space = max(1f, space) + + var x = yScaler.realOffset + while (x <= yScaler.realTop) { + drawHorizontalLine(x, factor, drawFirstValue) + x += space + } + } + + private fun drawHorizontalLine(value: Float, factor: Float, drawFirstValue: Boolean) { + val pixel = yScaler.scale(value).toInt() + + canvas.drawLine(0, pixel, width, pixel) + + if (drawFirstValue || pixel < height - textSize) drawScaleText( + 0, + pixel, + (value * factor).toInt().toString() + ) + } + + private fun drawScaleText(x: Int, y: Int, value: String) { + var x = x + var y = y + val w = value.length * textSize + val h = textSize + + if ((x + w) > width) x = width - w + if ((y - h) < 0) y = h + + canvas.drawText(value, x, y) + } + + fun drawXScale(lines: Int, factor: Float, drawText: Boolean) { + var lines = lines + lines = min(width / (textSize * 4), lines) + lines = max(1, lines) + + var space = xScaler.real / lines + space = max(1f, space) + + var x = 0f + while (x <= xScaler.real) { + drawVerticalLine(x, factor, drawText) + x += space + } + } + + private fun drawVerticalLine(value: Float, factor: Float, drawText: Boolean) { + val pixel = xScaler.scale(value).toInt() + + canvas.drawLine(pixel, 0, pixel, height) + + if (drawText && pixel > textSize) { + val text = (value * factor).toInt().toString() + drawScaleText(pixel - textSize, height, text) + } + } + + + fun plotData(xValue: Float, yValue: Float, color: Int): Int { + scaleToPixel(xValue, yValue) + + val delta: Int = max(pointA.x - pointB.x, 0) + + if (delta > 1) { + if (pointB.x < 0) pointB.y = pointA.y + canvas.drawLine(pointB, pointA, color) + switchPoints() + } + return delta + } + + private fun scaleToPixel(xvalue: Float, yvalue: Float) { + pointA.x = xScaler.scale(xvalue).toInt() + pointA.y = yScaler.scale(yvalue).toInt() + } + + private fun switchPoints() { + val t = pointB + pointB = pointA + pointA = t + } + + fun plotPoint(xvalue: Float, yvalue: Float, color: Int) { + scaleToPixel(xvalue, yvalue) + canvas.drawBitmap(pointA, color) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/IndexPainter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/IndexPainter.java deleted file mode 100644 index a9834d671..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/IndexPainter.java +++ /dev/null @@ -1,66 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; -import ch.bailu.aat_lib.map.MapColor; - -public class IndexPainter extends GpxListWalker { - - private float distance = 0f; - private final float offset; - private int index = 0; - - private final int nodeIndex; - private final GraphPlotter plotter; - - public IndexPainter(GraphPlotter p, int n, float offset) { - nodeIndex = n; - plotter = p; - this.offset = offset; - } - - @Override - public boolean doList(GpxList track) { - return true; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return doDelta(segment); - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return doDelta(marker); - } - - - private boolean doDelta(GpxSegmentNode segment) { - if (index + segment.getSegmentSize() <= nodeIndex) { - index += segment.getSegmentSize(); - distance += segment.getDistance(); - return false; - } - return true; - } - - @Override - public void doPoint(GpxPointNode point) { - if (index == nodeIndex) { - distance += point.getDistance(); - plotPoint(point, distance - offset); - index++; - - } else if (index < nodeIndex) { - distance += point.getDistance(); - index++; - } - } - - private void plotPoint(GpxPointNode point, float distance) { - plotter.plotPoint(distance, (float) point.getAltitude(), - MapColor.NODE_SELECTED); - } -} \ No newline at end of file diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/IndexPainter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/IndexPainter.kt new file mode 100644 index 000000000..9fb583ef0 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/IndexPainter.kt @@ -0,0 +1,56 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode +import ch.bailu.aat_lib.map.MapColor + +class IndexPainter( + private val plotter: GraphPlotter, + private val nodeIndex: Int, + private val offset: Float +) : GpxListWalker() { + private var distance = 0f + private var index = 0 + + override fun doList(track: GpxList): Boolean { + return true + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return doDelta(segment) + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return doDelta(marker) + } + + + private fun doDelta(segment: GpxSegmentNode): Boolean { + if (index + segment.segmentSize <= nodeIndex) { + index += segment.segmentSize + distance += segment.getDistance() + return false + } + return true + } + + override fun doPoint(point: GpxPointNode) { + if (index == nodeIndex) { + distance += point.getDistance() + plotPoint(point, distance - offset) + index++ + } else if (index < nodeIndex) { + distance += point.getDistance() + index++ + } + } + + private fun plotPoint(point: GpxPointNode, distance: Float) { + plotter.plotPoint( + distance, point.getAltitude(), + MapColor.NODE_SELECTED + ) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/InvertedOffsetScaler.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/InvertedOffsetScaler.kt new file mode 100644 index 000000000..ed0cbd090 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/InvertedOffsetScaler.kt @@ -0,0 +1,42 @@ +package ch.bailu.aat_lib.view.graph + +import kotlin.math.floor +import kotlin.math.max +import kotlin.math.min + + +class InvertedOffsetScaler(pixel: Int) { + private val scaler: Scaler = Scaler(pixel.toFloat()) + + var realOffset: Float = Float.Companion.MAX_VALUE + private set + var realTop: Float = 0f + private set + + fun addValue(v: Float) { + this.realOffset = min(this.realOffset, v) + this.realTop = max(this.realTop, v) + scaler.init(this.realTop - this.realOffset) + } + + fun scale(v: Float): Float { + return scaler.scale - (scaler.scale(v - this.realOffset)) + } + + fun getRealDistance(): Float { + return this.realTop - this.realOffset + } + + fun round(roundTo: Int) { + var d = (this.realTop / roundTo).toDouble() + d = floor(d) + this.realTop = (d * roundTo).toFloat() + roundTo + + d = (this.realOffset / roundTo).toDouble() + d = floor(d) + this.realOffset = d.toFloat() * roundTo + + addValue(this.realTop) + addValue(this.realOffset) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/InvertetOffsetScaler.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/InvertetOffsetScaler.java deleted file mode 100644 index 15f1a034b..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/InvertetOffsetScaler.java +++ /dev/null @@ -1,47 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - - -public class InvertetOffsetScaler { - - private final Scaler scaler; - private float min=Float.MAX_VALUE,max=0; - public InvertetOffsetScaler(int pixel) { - scaler=new Scaler(pixel); - } - - public void addValue(float v) { - min = Math.min(min,v); - max = Math.max(max,v); - scaler.init(max-min); - } - - public float scale(float v) { - return scaler.getScale()-(scaler.scale(v-min)); - } - - public float getRealDistance() { - return max-min; - } - public float getRealOffset() { - return min; - } - - public float getRealTop() { - return max; - } - - public void round(int roundTo) { - double - d = max/roundTo; - d = Math.floor(d); - max = (float)(d*roundTo)+roundTo; - - d = min/roundTo; - d = Math.floor(d); - min = (float)d*roundTo; - - addValue(max); - addValue(min); - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/LabelInterface.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/LabelInterface.java deleted file mode 100644 index 1fdd4ccaa..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/LabelInterface.java +++ /dev/null @@ -1,6 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -public interface LabelInterface { - void setText(int color, String text); - void setText(int color, String text, String unit); -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/LabelInterface.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/LabelInterface.kt new file mode 100644 index 000000000..fc92b4582 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/LabelInterface.kt @@ -0,0 +1,6 @@ +package ch.bailu.aat_lib.view.graph + +interface LabelInterface { + fun setText(color: Int, text: String) + fun setText(color: Int, text: String, unit: String) +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Plotter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Plotter.java deleted file mode 100644 index 727249aab..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Plotter.java +++ /dev/null @@ -1,7 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -public abstract class Plotter { - public abstract void plot(GraphCanvas canvas, PlotterConfig config); - public abstract void initLabels(LabelInterface labels); - public abstract void setLimit(int firstPoint, int lastPoint); -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Plotter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Plotter.kt new file mode 100644 index 000000000..b02c4fa63 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Plotter.kt @@ -0,0 +1,7 @@ +package ch.bailu.aat_lib.view.graph + +abstract class Plotter { + abstract fun plot(canvas: GraphCanvas, config: PlotterConfig) + abstract fun initLabels(labels: LabelInterface) + abstract fun setLimit(firstPoint: Int, lastPoint: Int) +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/PlotterConfig.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/PlotterConfig.java deleted file mode 100644 index c7c4876f9..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/PlotterConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; - -public interface PlotterConfig { - int getWidth(); - int getHeight(); - - GpxList getList(); - int getIndex(); - - boolean isXLabelVisible(); - - LabelInterface getLabels(); -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/PlotterConfig.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/PlotterConfig.kt new file mode 100644 index 000000000..5e4178a83 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/PlotterConfig.kt @@ -0,0 +1,12 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList + +interface PlotterConfig { + fun getWidth(): Int + fun getHeight(): Int + fun getList(): GpxList + fun getIndex(): Int + fun isXLabelVisible(): Boolean + fun getLabels(): LabelInterface +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGenerator.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGenerator.java deleted file mode 100644 index 12ce12bee..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGenerator.java +++ /dev/null @@ -1,35 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; - -public class ScaleGenerator extends GpxListWalker { - private final GraphPlotter plotter; - - public ScaleGenerator(GraphPlotter p) { - plotter=p; - } - - @Override - public void doPoint(GpxPointNode point) { - plotter.inlcudeInYScale((float)point.getAltitude()); - } - - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return true; - } - - @Override - public boolean doList(GpxList track) { - return true; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGenerator.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGenerator.kt new file mode 100644 index 000000000..2089742ec --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGenerator.kt @@ -0,0 +1,24 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode + +open class ScaleGenerator(private val plotter: GraphPlotter) : GpxListWalker() { + override fun doPoint(point: GpxPointNode) { + plotter.includeInYScale(point.getAltitude()) + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return true + } + + override fun doList(track: GpxList): Boolean { + return true + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorMarkerMode.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorMarkerMode.java deleted file mode 100644 index 99ac878f7..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorMarkerMode.java +++ /dev/null @@ -1,20 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; - -public class ScaleGeneratorMarkerMode extends ScaleGenerator { - - public ScaleGeneratorMarkerMode(GraphPlotter p) { - super(p); - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - GpxPointNode point = (GpxPointNode) marker.getFirstNode(); - if (point != null) { - doPoint(point); - } - return false; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorMarkerMode.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorMarkerMode.kt new file mode 100644 index 000000000..5516cf452 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorMarkerMode.kt @@ -0,0 +1,14 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode + +class ScaleGeneratorMarkerMode(p: GraphPlotter) : ScaleGenerator(p) { + override fun doMarker(marker: GpxSegmentNode): Boolean { + val point = marker.firstNode + if (point is GpxPointNode) { + doPoint(point) + } + return false + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorSegmented.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorSegmented.java deleted file mode 100644 index b02e20b97..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorSegmented.java +++ /dev/null @@ -1,48 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; - -public class ScaleGeneratorSegmented extends ScaleGenerator { - private int index = 0; - private final Segment segment; - - public ScaleGeneratorSegmented(GraphPlotter p, Segment segment) { - super(p); - this.segment = segment; - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return doDelta(marker.getSegmentSize()); - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return doDelta(segment.getSegmentSize()); - } - - private boolean doDelta(int size) { - - if (segment.isAfter(index)) { - return false; - - } else { - int nextIndex = index + size; - - if (segment.isBefore(nextIndex)) { - index = nextIndex; - return false; - } - } - return true; - } - - @Override - public void doPoint(GpxPointNode point) { - if (segment.isInside(index)) { - super.doPoint(point); - } - index++; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorSegmented.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorSegmented.kt new file mode 100644 index 000000000..55ffcea5c --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/ScaleGeneratorSegmented.kt @@ -0,0 +1,37 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode + +class ScaleGeneratorSegmented(p: GraphPlotter, private val segment: Segment) : ScaleGenerator(p) { + private var index = 0 + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return doDelta(marker.segmentSize) + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return doDelta(segment.segmentSize) + } + + private fun doDelta(size: Int): Boolean { + if (segment.isAfter(index)) { + return false + } else { + val nextIndex = index + size + + if (segment.isBefore(nextIndex)) { + index = nextIndex + return false + } + } + return true + } + + override fun doPoint(point: GpxPointNode) { + if (segment.isInside(index)) { + super.doPoint(point) + } + index++ + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Scaler.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Scaler.java deleted file mode 100644 index 5b254bbfb..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Scaler.java +++ /dev/null @@ -1,37 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -public class Scaler { - private float scale=1; - private float real=1; - - - public Scaler(float scale) { - if (scale != 0f) this.scale=scale; - } - - - public Scaler(float scale, float real) { - this.scale=scale; - if (real !=0f) this.real=real; - } - - public void init(float real) { - if (real !=0f) this.real=real; - } - - public float scale(float realValue) { - return (scale/real)*realValue; - } - - public float backScale(float scaledValue) { - return scaledValue / (scale/real); - } - - public float getScale() { - return scale; - } - - public float getReal() { - return real; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Scaler.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Scaler.kt new file mode 100644 index 000000000..dcf4b5382 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Scaler.kt @@ -0,0 +1,35 @@ +package ch.bailu.aat_lib.view.graph + +class Scaler(scale: Float = 1f, real: Float = 1f) { + val scale: Float + var real: Float + private set + + init { + if (scale != 0f) { + this.scale = scale + } else { + this.scale = 1f + } + + if (real != 0f) { + this.real = real + } else { + this.real = 1f + } + } + + fun init(real: Float) { + if (real != 0f) { + this.real = real + } + } + + fun scale(realValue: Float): Float { + return (scale / real) * realValue + } + + fun backScale(scaledValue: Float): Float { + return scaledValue / (scale / real) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Segment.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Segment.java deleted file mode 100644 index 7a588a46a..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Segment.java +++ /dev/null @@ -1,28 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - - -public class Segment { - private int start = -1; - private int end = -1; - - public void setLimit(int start, int end) { - this.start = start; - this.end = end; - } - - public boolean isValid() { - return start > -1 && end > start; - } - - public boolean isAfter(int index) { - return index > end; - } - - public boolean isBefore(int index) { - return index < start; - } - - public boolean isInside(int index) { - return index >= start && index <= end; - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Segment.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Segment.kt new file mode 100644 index 000000000..0cc9771d7 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/Segment.kt @@ -0,0 +1,28 @@ +package ch.bailu.aat_lib.view.graph + + +class Segment { + private var start = -1 + private var end = -1 + + fun setLimit(start: Int, end: Int) { + this.start = start + this.end = end + } + + fun isValid(): Boolean { + return start > -1 && end > start + } + + fun isAfter(index: Int): Boolean { + return index > end + } + + fun isBefore(index: Int): Boolean { + return index < start + } + + fun isInside(index: Int): Boolean { + return index >= start && index <= end + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SegmentNodePainter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SegmentNodePainter.java deleted file mode 100644 index ff1bd073d..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SegmentNodePainter.java +++ /dev/null @@ -1,51 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; -import ch.bailu.aat_lib.map.MapColor; - -public class SegmentNodePainter extends GpxListWalker { - - private float distance; - - private final GraphPlotter plotter; - - public SegmentNodePainter(GraphPlotter p, float offset) { - plotter = p; - distance = 0f - offset; - - } - - @Override - public boolean doList(GpxList track) { - return true; - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - if (segment.getSegmentSize() > 0 && distance > 0f) { - GpxPointNode node = (GpxPointNode) segment.getFirstNode(); - plotPoint(node, distance + node.getDistance()); - } - - distance += segment.getDistance(); - return false; - - } - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return false; - } - - - @Override - public void doPoint(GpxPointNode point) {} - - private void plotPoint(GpxPointNode point, float distance) { - plotter.plotPoint(distance, (float) point.getAltitude(), - MapColor.NODE_NEUTRAL); - } -} \ No newline at end of file diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SegmentNodePainter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SegmentNodePainter.kt new file mode 100644 index 000000000..86306c2e1 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SegmentNodePainter.kt @@ -0,0 +1,38 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode +import ch.bailu.aat_lib.map.MapColor + +class SegmentNodePainter(private val plotter: GraphPlotter, offset: Float) : GpxListWalker() { + private var distance: Float = 0f - offset + + override fun doList(track: GpxList): Boolean { + return true + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + if (segment.segmentSize > 0 && distance > 0f) { + val node = segment.firstNode as GpxPointNode + plotPoint(node, distance + node.getDistance()) + } + + distance += segment.getDistance() + return false + } + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return false + } + + override fun doPoint(point: GpxPointNode) {} + + private fun plotPoint(point: GpxPointNode, distance: Float) { + plotter.plotPoint( + distance, point.getAltitude(), + MapColor.NODE_NEUTRAL + ) + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmEntry.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmEntry.java deleted file mode 100644 index 0d1014299..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmEntry.java +++ /dev/null @@ -1,71 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.description.ContentDescription; -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.attributes.SampleRate; - -public class SpmEntry { - private final int color; - private final String label; - private final String unit; - - private final int maxKey; - private final int[] keys; - - private GraphPlotter plotter; - - private float summaryDistance=0; - - - public SpmEntry(int color, ContentDescription description, int maxKey, int... keys) { - this(color, description.getLabel(), description.getUnit(), maxKey, keys); - } - - public SpmEntry(int color, String label, String unit, int maxKey, int... keys) { - this.color = color; - this.label = label; - this.unit = unit; - this.maxKey = maxKey; - this.keys = keys; - - } - - - public void setLabelText(LabelInterface labels) { - labels.setText(color, label, unit); - } - - - public void setPlotter(int kmFactor, GraphCanvas canvas, int width, int height) { - plotter = new GraphPlotter(canvas, width, height, 1000 * kmFactor); - } - - public GraphPlotter getPlotter() { - return plotter; - } - - public int getMax(GpxList list) { - return list.getDelta().getAttributes().getAsInteger(maxKey); - } - - public void incrementSummaryDistance(float distance) { - summaryDistance += distance; - } - - - public void plotIfDistance(GpxPointNode point, float minDistance, float distance) { - if (summaryDistance >= minDistance) { - - final int value = SampleRate.getValue(point.getAttributes(), keys); - - if (value > 0) { - //distance += summaryDistance; - summaryDistance = 0; - - plotter.plotData(distance, value, color); - } - } - - } -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmEntry.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmEntry.kt new file mode 100644 index 000000000..84082e3c2 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmEntry.kt @@ -0,0 +1,57 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.description.ContentDescription +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.attributes.SampleRate.Companion.getValue + +class SpmEntry( + private val color: Int, + private val label: String, + private val unit: String, + private val maxKey: Int, + private vararg val keys: Int +) { + private var plotter: GraphPlotter? = null + private var summaryDistance = 0f + + constructor(color: Int, description: ContentDescription, maxKey: Int, vararg keys: Int) : this( + color, + description.getLabel(), + description.getUnit(), + maxKey, + *keys + ) + + fun setLabelText(labels: LabelInterface) { + labels.setText(color, label, unit) + } + + fun setPlotter(kmFactor: Int, canvas: GraphCanvas, width: Int, height: Int) { + plotter = GraphPlotter(canvas, width, height, (1000 * kmFactor).toFloat()) + } + + fun getPlotter(): GraphPlotter? { + return plotter + } + + fun getMax(list: GpxList): Int { + return list.getDelta().getAttributes().getAsInteger(maxKey) + } + + fun incrementSummaryDistance(distance: Float) { + summaryDistance += distance + } + + fun plotIfDistance(point: GpxPointNode, minDistance: Float, distance: Float) { + if (summaryDistance >= minDistance) { + val value = getValue(point.getAttributes(), *keys) + + if (value > 0) { + //distance += summaryDistance; + summaryDistance = 0f + plotter?.plotData(distance, value.toFloat(), color) + } + } + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmGraphPainter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmGraphPainter.java deleted file mode 100644 index aa6987d41..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmGraphPainter.java +++ /dev/null @@ -1,47 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.gpx.GpxList; -import ch.bailu.aat_lib.gpx.GpxListWalker; -import ch.bailu.aat_lib.gpx.GpxPointNode; -import ch.bailu.aat_lib.gpx.GpxSegmentNode; - -public class SpmGraphPainter extends GpxListWalker { - private final SpmEntry[] entries; - - private float distance=0; - private final float minDistance; - - - public SpmGraphPainter(SpmEntry[] entries, int minDistance) { - this.entries = entries; - this.minDistance = minDistance; - - } - - - @Override - public boolean doMarker(GpxSegmentNode marker) { - return true; - } - - - @Override - public void doPoint(GpxPointNode point) { - for (SpmEntry e : entries) { - distance += point.getDistance(); - - e.incrementSummaryDistance(point.getDistance()); - e.plotIfDistance(point, minDistance, distance); - } - } - - @Override - public boolean doSegment(GpxSegmentNode segment) { - return true; - } - - @Override - public boolean doList(GpxList track) { - return true; - } -} \ No newline at end of file diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmGraphPainter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmGraphPainter.kt new file mode 100644 index 000000000..bff00f3e3 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmGraphPainter.kt @@ -0,0 +1,33 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.gpx.GpxList +import ch.bailu.aat_lib.gpx.GpxListWalker +import ch.bailu.aat_lib.gpx.GpxPointNode +import ch.bailu.aat_lib.gpx.GpxSegmentNode + +class SpmGraphPainter(private val entries: Array, minDistance: Int) : GpxListWalker() { + private var distance = 0f + private val minDistance: Float = minDistance.toFloat() + + override fun doMarker(marker: GpxSegmentNode): Boolean { + return true + } + + + override fun doPoint(point: GpxPointNode) { + for (e in entries) { + distance += point.getDistance() + + e.incrementSummaryDistance(point.getDistance()) + e.plotIfDistance(point, minDistance, distance) + } + } + + override fun doSegment(segment: GpxSegmentNode): Boolean { + return true + } + + override fun doList(track: GpxList): Boolean { + return true + } +} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmPlotter.java b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmPlotter.java deleted file mode 100644 index 976b7ee30..000000000 --- a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmPlotter.java +++ /dev/null @@ -1,79 +0,0 @@ -package ch.bailu.aat_lib.view.graph; - -import ch.bailu.aat_lib.app.AppColor; -import ch.bailu.aat_lib.description.CadenceDescription; -import ch.bailu.aat_lib.description.HeartRateDescription; -import ch.bailu.aat_lib.description.StepRateDescription; -import ch.bailu.aat_lib.gpx.attributes.SampleRate; -import ch.bailu.aat_lib.preferences.general.SolidUnit; - -public class SpmPlotter extends Plotter { - - public final static SpmEntry[] ENTRIES = createEntries(); - private final SolidUnit sunit; - - public SpmPlotter(SolidUnit sunit) { - this.sunit = sunit; - } - - private static SpmEntry[] createEntries() { - return new SpmEntry[]{ - new SpmEntry(AppColor.HL_BLUE, - new HeartRateDescription(), - SampleRate.HeartRate.INDEX_MAX_HR, - SampleRate.HeartRate.GPX_KEYS), - - new SpmEntry(AppColor.HL_GREEN, - new CadenceDescription(), - SampleRate.Cadence.INDEX_MAX_CADENCE, - SampleRate.Cadence.GPX_KEYS), - - - new SpmEntry(AppColor.HL_ORANGE, - new StepRateDescription(), - SampleRate.StepsRate.INDEX_MAX_SPM, - SampleRate.StepsRate.GPX_KEYS) - }; - } - - @Override - public void plot(GraphCanvas canvas, PlotterConfig config) { - int max = 0; - int min = 25; - int km_factor = (int) (config.getList().getDelta().getDistance() / 1000) + 1; - - for (SpmEntry e : ENTRIES) { - e.setPlotter(km_factor, canvas, config.getWidth(), config.getHeight()); - max = Math.max(max, e.getMax(config.getList())); - } - - if (max > 0) { - for (SpmEntry e: ENTRIES) { - e.getPlotter().inlcudeInYScale(min); - e.getPlotter().inlcudeInYScale(max); - e.getPlotter().roundYScale(25); - } - - new SpmGraphPainter( - ENTRIES, - (int)config.getList().getDelta().getDistance() / Math.max(config.getWidth(),1) - ).walkTrack(config.getList()); - - ENTRIES[0].getPlotter().drawYScale(5, 1, false); - } - - ENTRIES[0].getPlotter().drawXScale(5, sunit.getDistanceFactor(), config.isXLabelVisible()); - } - - @Override - public void initLabels(LabelInterface labels) { - for (SpmEntry e : SpmPlotter.ENTRIES) - e.setLabelText(labels); - } - - @Override - public void setLimit(int firstPoint, int lastPoint) { - - } - -} diff --git a/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmPlotter.kt b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmPlotter.kt new file mode 100644 index 000000000..31d11e725 --- /dev/null +++ b/aat-lib/src/main/java/ch/bailu/aat_lib/view/graph/SpmPlotter.kt @@ -0,0 +1,71 @@ +package ch.bailu.aat_lib.view.graph + +import ch.bailu.aat_lib.app.AppColor +import ch.bailu.aat_lib.description.CadenceDescription +import ch.bailu.aat_lib.description.HeartRateDescription +import ch.bailu.aat_lib.description.StepRateDescription +import ch.bailu.aat_lib.gpx.attributes.SampleRate +import ch.bailu.aat_lib.preferences.general.SolidUnit +import kotlin.math.max + +class SpmPlotter(private val sunit: SolidUnit) : Plotter() { + override fun plot(canvas: GraphCanvas, config: PlotterConfig) { + var max = 0 + val min = 25 + val kmFactor = (config.getList().getDelta().getDistance() / 1000).toInt() + 1 + + for (e in ENTRIES) { + e.setPlotter(kmFactor, canvas, config.getWidth(), config.getHeight()) + max = max(max, e.getMax(config.getList())) + } + + if (max > 0) { + for (e in ENTRIES) { + e.getPlotter()?.includeInYScale(min.toFloat()) + e.getPlotter()?.includeInYScale(max.toFloat()) + e.getPlotter()?.roundYScale(25) + } + + SpmGraphPainter( + ENTRIES, + config.getList().getDelta().getDistance().toInt() / max(config.getWidth(), 1) + ).walkTrack(config.getList()) + + ENTRIES[0].getPlotter()?.drawYScale(5, 1f, false) + } + + ENTRIES[0].getPlotter()?.drawXScale(5, sunit.distanceFactor, config.isXLabelVisible()) + } + + override fun initLabels(labels: LabelInterface) { + for (e in ENTRIES) e.setLabelText(labels) + } + + override fun setLimit(firstPoint: Int, lastPoint: Int) {} + + companion object { + val ENTRIES: Array = createEntries() + private fun createEntries(): Array { + return arrayOf( + SpmEntry( + AppColor.HL_BLUE, + HeartRateDescription(), + SampleRate.HeartRate.INDEX_MAX_HR, + *SampleRate.HeartRate.GPX_KEYS + ), + SpmEntry( + AppColor.HL_GREEN, + CadenceDescription(), + SampleRate.Cadence.INDEX_MAX_CADENCE, + *SampleRate.Cadence.GPX_KEYS + ), + SpmEntry( + AppColor.HL_ORANGE, + StepRateDescription(), + SampleRate.StepsRate.INDEX_MAX_SPM, + *SampleRate.StepsRate.GPX_KEYS + ) + ) + } + } +} diff --git a/aat-lib/src/test/java/ch/bailu/aat_lib/BearingTest.java b/aat-lib/src/test/java/ch/bailu/aat_lib/BearingTest.java deleted file mode 100644 index 50c3173a5..000000000 --- a/aat-lib/src/test/java/ch/bailu/aat_lib/BearingTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package ch.bailu.aat_lib; - - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import ch.bailu.aat_lib.coordinates.CoordinatesTest; -import ch.bailu.aat_lib.coordinates.LatLongE6; -import ch.bailu.aat_lib.gpx.tools.SimplifierBearing; - - -public class BearingTest { - - @BeforeAll - public static void init() { - CoordinatesTest.init(); - } - - - /** - * Compared with results from - * ... - * ... - */ - @Test - public void testBearing() { - final float bearing = 96.51f; - - LatLongE6 kansasCity = new LatLongE6(39.099912, -94.581213); - LatLongE6 stLouis = new LatLongE6(38.627089, -90.200203); - - assertEquals (bearing, SimplifierBearing.getBearing(kansasCity, stLouis),0.9f); - } -} diff --git a/aat-lib/src/test/java/ch/bailu/aat_lib/TestLimit.java b/aat-lib/src/test/java/ch/bailu/aat_lib/TestLimit.java deleted file mode 100644 index 3352e87b7..000000000 --- a/aat-lib/src/test/java/ch/bailu/aat_lib/TestLimit.java +++ /dev/null @@ -1,21 +0,0 @@ -package ch.bailu.aat_lib; - -import org.junit.jupiter.api.Test; - -import ch.bailu.aat_lib.util.Limit; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class TestLimit { - @Test - public void testLimit() { - assertEquals(20, Limit.clamp(22, 1, 20)); - assertEquals(1, Limit.clamp(-200, 1, 20)); - assertEquals(-4, Limit.smallest(5,3,6,234,7,-4, 45345)); - assertEquals(-5.3, Limit.smallest(-5.3,3d,6d,234d,7d,-4.3433, 45345.34)); - - assertEquals(45345, Limit.biggest(5,3,6,234,7,-4, 45345)); - assertEquals(45345.34, Limit.biggest(-5.3,3d,6d,234d,7d,-4.3433, 45345.34)); - - } -} diff --git a/aat-lib/src/test/java/ch/bailu/aat_lib/TestTest.java b/aat-lib/src/test/java/ch/bailu/aat_lib/TestTest.java deleted file mode 100644 index f0c54ad31..000000000 --- a/aat-lib/src/test/java/ch/bailu/aat_lib/TestTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package ch.bailu.aat_lib; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class TestTest { - - boolean initValue = false; - - @BeforeEach - public void init() { - initValue = true; - } - - @Test - public void testTest() { - assertEquals(true, initValue); - } - - @AfterEach - public void reset() { - initValue = false; - } -} diff --git a/aat-lib/src/test/java/ch/bailu/aat_lib/utli/LimitTest.java b/aat-lib/src/test/java/ch/bailu/aat_lib/utli/LimitTest.java deleted file mode 100644 index 27b516bef..000000000 --- a/aat-lib/src/test/java/ch/bailu/aat_lib/utli/LimitTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package ch.bailu.aat_lib.utli; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -import ch.bailu.aat_lib.util.Limit; - -public class LimitTest { - - @Test - public void testLimit() { - assertEquals(5, Limit.biggest(4, 5)); - assertEquals(4, Limit.smallest(4, 5)); - assertEquals(5, Limit.clamp(5,4, 5)); - assertEquals(4, Limit.clamp(5,4, 4)); - - assertEquals(10, Limit.biggest(4, 5, 10)); - } -} diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/coordinates/CoordinatesTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/coordinates/CoordinatesTest.kt index ea702139c..e2f60adc5 100644 --- a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/coordinates/CoordinatesTest.kt +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/coordinates/CoordinatesTest.kt @@ -297,7 +297,6 @@ class CoordinatesTest { } companion object { - @JvmStatic @BeforeAll fun init() { diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/file/json/GpxListReaderJsonTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/file/json/GpxListReaderJsonTest.kt index 3c9c8c805..16c45db39 100644 --- a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/file/json/GpxListReaderJsonTest.kt +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/file/json/GpxListReaderJsonTest.kt @@ -105,8 +105,8 @@ class GpxListReaderJsonTest { var index = 0 val iterator = GpxListIterator(result) while (iterator.nextPoint()) { - assertEquals(expected[index++], iterator.point.getLatitude()) - assertEquals(expected[index++], iterator.point.getLongitude()) + assertEquals(expected[index++], iterator.getPoint().getLatitude()) + assertEquals(expected[index++], iterator.getPoint().getLongitude()) } assertEquals(expected.size, index) assertEquals(expectedType, result.getDelta().getType()) diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/service/editor/EditorRingTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/service/editor/EditorRingTest.kt new file mode 100644 index 000000000..410185480 --- /dev/null +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/service/editor/EditorRingTest.kt @@ -0,0 +1,31 @@ +package ch.bailu.aat_lib.service.editor + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +class EditorRingTest { + + @Test + fun testEditorRing() { + val editor = NodeEditor() + val editor2 = NodeEditor() + val ring = EditorRing(editor) + + assertEquals(editor, ring.get()) + + ring.add(editor2) + assertEquals(editor2, ring.get()) + + ring.undo() + assertEquals(editor, ring.get()) + + ring.undo() + assertEquals(editor, ring.get()) + + ring.redo() + assertEquals(editor2, ring.get()) + + ring.redo() + assertEquals(editor2, ring.get()) + } +} diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/service/icons/IconMapParserTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/service/icons/IconMapParserTest.kt new file mode 100644 index 000000000..ad562bb37 --- /dev/null +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/service/icons/IconMapParserTest.kt @@ -0,0 +1,31 @@ +package ch.bailu.aat_lib.service.icons + +import ch.bailu.aat_lib.gpx.attributes.Keys +import ch.bailu.foc_extended.FocResource +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +class IconMapParserTest { + + @Test + fun testMapParser() { + val map = IconMap() + IconMapParser(FocResource("icons/iconmap.txt"), map) + + val icon1 = map.get(Keys.toIndex("tourism"), "camp_site") + val icon2 = map.get(Keys.toIndex("tourism"), "caravan_site") + val icon3 = map.get(Keys.toIndex("tourism"), "alpine_hut") + val icon4 = map.get(Keys.toIndex("tourism"), "camp_side") + val icon5 = map.get(Keys.toIndex("guest_house"), "bed_and_breakfast") + + assertEquals("icons/accommodation/camping.svg", icon1?.svg) + assertEquals("icons/accommodation/caravan_park.svg", icon2?.svg) + assertEquals("icons/accommodation/alpinehut.svg", icon3?.svg) + assertNull(icon4?.svg) + assertEquals("icons/accommodation/bed_and_breakfast.svg", icon5?.svg) + + val builder = StringBuilder() + map.appendStatusText(builder) + assertEquals("IconMap (key_list) size: 2
", builder.toString()) + } +} diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/BearingTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/BearingTest.kt new file mode 100644 index 000000000..0398015ea --- /dev/null +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/BearingTest.kt @@ -0,0 +1,34 @@ +package ch.bailu.aat_lib.util + +import ch.bailu.aat_lib.coordinates.LatLongE6 +import ch.bailu.aat_lib.gpx.tools.SimplifierBearing +import ch.bailu.aat_lib.mock.MockAppConfig +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test + +class BearingTest { + /** + * Compared with results from + * [...](https://rechneronline.de/geo-coordinates/#distance) + * [...](https://www.igismap.com/map-tool/bearing-angle) + */ + @Test + fun testBearing() { + val bearing = 96.51f + + val kansasCity = LatLongE6(39.099912, -94.581213) + val stLouis = LatLongE6(38.627089, -90.200203) + + Assertions.assertEquals(bearing, + SimplifierBearing.Companion.getBearing(kansasCity, stLouis), 0.9f) + } + + companion object { + @JvmStatic + @BeforeAll + fun init() { + MockAppConfig.init() + } + } +} diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/LimitTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/LimitTest.kt new file mode 100644 index 000000000..37ed8fd86 --- /dev/null +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/LimitTest.kt @@ -0,0 +1,28 @@ +package ch.bailu.aat_lib.util + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class LimitTest { + @Test + fun testLimit() { + Assertions.assertEquals(5, Limit.biggest(4, 5)) + Assertions.assertEquals(4, Limit.smallest(4, 5)) + Assertions.assertEquals(5, Limit.clamp(5, 4, 5)) + Assertions.assertEquals(4, Limit.clamp(5, 4, 4)) + + Assertions.assertEquals(10, Limit.biggest(4, 5, 10)) + } + + + @Test + fun testLimit2() { + Assertions.assertEquals(20, Limit.clamp(22, 1, 20)) + Assertions.assertEquals(1, Limit.clamp(-200, 1, 20)) + Assertions.assertEquals(-4, Limit.smallest(5, 3, 6, 234, 7, -4, 45345)) + Assertions.assertEquals(-5.3, Limit.smallest(-5.3, 3.0, 6.0, 234.0, 7.0, -4.3433, 45345.34)) + + Assertions.assertEquals(45345, Limit.biggest(5, 3, 6, 234, 7, -4, 45345)) + Assertions.assertEquals(45345.34, Limit.biggest(-5.3, 3.0, 6.0, 234.0, 7.0, -4.3433, 45345.34)) + } +} diff --git a/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/TestTest.kt b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/TestTest.kt new file mode 100644 index 000000000..d6fe2d16b --- /dev/null +++ b/aat-lib/src/test/kotlin/ch/bailu/aat_lib/util/TestTest.kt @@ -0,0 +1,25 @@ +package ch.bailu.aat_lib.util + +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test + +class TestTest { + var initValue: Boolean = false + + @BeforeEach + fun init() { + initValue = true + } + + @Test + fun testTest() { + Assertions.assertEquals(true, initValue) + } + + @AfterEach + fun reset() { + initValue = false + } +} diff --git a/aat-lib/src/test/resources/icons/iconmap.txt b/aat-lib/src/test/resources/icons/iconmap.txt new file mode 100644 index 000000000..0a298d991 --- /dev/null +++ b/aat-lib/src/test/resources/icons/iconmap.txt @@ -0,0 +1,12 @@ +# Map OSM key-value pairs to filenames from the SJJB icon set. +# Original icon set is available from http://www.sjjb.co.uk/mapicons/introduction. +# +# +# prefix of png-file osm key osm value + +accommodation/alpinehut tourism alpine_hut +accommodation/bed_and_breakfast2 +accommodation/bed_and_breakfast guest_house bed_and_breakfast +accommodation/camping tourism camp_site +accommodation/caravan_park tourism caravan_site +accommodation/chalet2