checkpoint

This commit is contained in:
voussoir 2023-03-11 16:43:05 -08:00
parent 62675e1b97
commit 9830ebf1e7
5 changed files with 94 additions and 21 deletions

View file

@ -30,6 +30,14 @@ class Database(trackbook: Trackbook)
Log.i("VOUSSOIR", "Database.open: Calling all listeners") Log.i("VOUSSOIR", "Database.open: Calling all listeners")
} }
fun begin_transaction()
{
if (! connection.inTransaction())
{
connection.beginTransaction()
}
}
fun commit() fun commit()
{ {
if (! this.ready) if (! this.ready)
@ -57,37 +65,48 @@ class Database(trackbook: Trackbook)
put("sat", trkpt.numberSatellites) put("sat", trkpt.numberSatellites)
put("ele", trkpt.altitude) put("ele", trkpt.altitude)
} }
if (! connection.inTransaction()) begin_transaction()
{
connection.beginTransaction()
}
connection.insert("trkpt", null, values) connection.insert("trkpt", null, values)
} }
fun insert_homepoint(name: String, latitude: Double, longitude: Double, radius: Double) fun insert_homepoint(id: Long, name: String, latitude: Double, longitude: Double, radius: Double)
{ {
Log.i("VOUSSOIR", "Database.insert_homepoint") Log.i("VOUSSOIR", "Database.insert_homepoint")
val values = ContentValues().apply { val values = ContentValues().apply {
put("id", id)
put("lat", latitude) put("lat", latitude)
put("lon", longitude) put("lon", longitude)
put("radius", radius) put("radius", radius)
put("name", name) put("name", name)
} }
if (! connection.inTransaction()) begin_transaction()
{
connection.beginTransaction()
}
connection.insert("homepoints", null, values) connection.insert("homepoints", null, values)
commit() commit()
trackbook.load_homepoints() trackbook.load_homepoints()
} }
fun delete_homepoint(id: Long)
{
Log.i("VOUSSOIR", "Database.delete_homepoint")
begin_transaction()
connection.delete("homepoints", "id = ?", arrayOf(id.toString()))
commit()
}
fun update_homepoint(id: Long, name: String, radius: Double)
{
Log.i("VOUSSOIR", "Database.update_homepoint")
begin_transaction()
connection.rawQuery("UPDATE homepoints SET name = ?, radius = ? WHERE id = ?", arrayOf(name, radius.toString(), id.toString()))
commit()
}
private fun initialize_tables() private fun initialize_tables()
{ {
this.connection.beginTransaction() this.connection.beginTransaction()
this.connection.execSQL("CREATE TABLE IF NOT EXISTS meta(name TEXT PRIMARY KEY, value TEXT)") this.connection.execSQL("CREATE TABLE IF NOT EXISTS meta(name TEXT PRIMARY KEY, value TEXT)")
this.connection.execSQL("CREATE TABLE IF NOT EXISTS trkpt(lat REAL NOT NULL, lon REAL NOT NULL, time INTEGER NOT NULL, accuracy REAL, device_id INTEGER NOT NULL, ele INTEGER, sat INTEGER, PRIMARY KEY(lat, lon, time, device_id))") this.connection.execSQL("CREATE TABLE IF NOT EXISTS trkpt(lat REAL NOT NULL, lon REAL NOT NULL, time INTEGER NOT NULL, accuracy REAL, device_id INTEGER NOT NULL, ele INTEGER, sat INTEGER, PRIMARY KEY(lat, lon, time, device_id))")
this.connection.execSQL("CREATE TABLE IF NOT EXISTS homepoints(lat REAL NOT NULL, lon REAL NOT NULL, radius REAL NOT NULL, name TEXT, PRIMARY KEY(lat, lon))") this.connection.execSQL("CREATE TABLE IF NOT EXISTS homepoints(id INTEGER PRIMARY KEY, lat REAL NOT NULL, lon REAL NOT NULL, radius REAL NOT NULL, name TEXT)")
this.connection.setTransactionSuccessful() this.connection.setTransactionSuccessful()
this.connection.endTransaction() this.connection.endTransaction()
} }

View file

@ -2,7 +2,7 @@ package org.y20k.trackbook
import android.location.Location import android.location.Location
import java.util.* import java.util.*
class Homepoint(val latitude: Double, val longitude: Double, val radius: Double, val name: String) class Homepoint(val id: Long, val latitude: Double, val longitude: Double, val radius: Double, val name: String)
{ {
val location: Location = this.to_location() val location: Location = this.to_location()

View file

@ -33,6 +33,7 @@ import android.view.WindowManager
import android.widget.Button import android.widget.Button
import android.widget.EditText import android.widget.EditText
import android.widget.TextView import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
@ -159,9 +160,8 @@ class MapFragment : Fragment()
compassOverlay.setCompassCenter((screen_width / densityScalingFactor) - 36f, 36f) compassOverlay.setCompassCenter((screen_width / densityScalingFactor) - 36f, 36f)
mapView.overlays.add(compassOverlay) mapView.overlays.add(compassOverlay)
val app: Trackbook = (requireContext().applicationContext as Trackbook) trackbook.load_homepoints()
app.load_homepoints() create_homepoint_overlays(requireContext(), mapView, trackbook.homepoints)
create_homepoint_overlays(requireContext(), mapView, app.homepoints)
if (database_changed_listener !in trackbook.database_changed_listeners) if (database_changed_listener !in trackbook.database_changed_listeners)
{ {
trackbook.database_changed_listeners.add(database_changed_listener) trackbook.database_changed_listeners.add(database_changed_listener)
@ -206,9 +206,15 @@ class MapFragment : Fragment()
dialog.cancel() dialog.cancel()
} }
save_button.setOnClickListener { save_button.setOnClickListener {
app.database.insert_homepoint(name=name_input.text.toString(), latitude=point.latitude, longitude=point.longitude, radius=radius_input.text.toString().toDouble()) trackbook.database.insert_homepoint(
app.load_homepoints() id=random_long(),
create_homepoint_overlays(requireContext(), mapView, app.homepoints) name=name_input.text.toString(),
latitude=point.latitude,
longitude=point.longitude,
radius=radius_input.text.toString().toDouble(),
)
trackbook.load_homepoints()
create_homepoint_overlays(requireContext(), mapView, trackbook.homepoints)
dialog.dismiss() dialog.dismiss()
} }
@ -483,7 +489,6 @@ class MapFragment : Fragment()
fun create_homepoint_overlays(context: Context, map_view: MapView, homepoints: List<Homepoint>) fun create_homepoint_overlays(context: Context, map_view: MapView, homepoints: List<Homepoint>)
{ {
Log.i("VOUSSOIR", "MapFragmentLayoutHolder.createHomepointOverlays") Log.i("VOUSSOIR", "MapFragmentLayoutHolder.createHomepointOverlays")
val overlayItems: java.util.ArrayList<OverlayItem> = java.util.ArrayList<OverlayItem>()
val newMarker: Drawable = ContextCompat.getDrawable(context, R.drawable.ic_homepoint_24dp)!! val newMarker: Drawable = ContextCompat.getDrawable(context, R.drawable.ic_homepoint_24dp)!!
@ -497,10 +502,51 @@ class MapFragment : Fragment()
p.outlinePaint.color = Color.argb(0, 0, 0, 0) p.outlinePaint.color = Color.argb(0, 0, 0, 0)
homepoints_overlays.add(p) homepoints_overlays.add(p)
val overlayItems: java.util.ArrayList<OverlayItem> = java.util.ArrayList<OverlayItem>()
val overlayItem: OverlayItem = createOverlayItem(context, homepoint.location.latitude, homepoint.location.longitude, homepoint.location.accuracy, homepoint.location.provider.toString(), homepoint.location.time) val overlayItem: OverlayItem = createOverlayItem(context, homepoint.location.latitude, homepoint.location.longitude, homepoint.location.accuracy, homepoint.location.provider.toString(), homepoint.location.time)
overlayItem.setMarker(newMarker) overlayItem.setMarker(newMarker)
overlayItems.add(overlayItem) overlayItems.add(overlayItem)
homepoints_overlays.add(createOverlay(context, overlayItems)) val homepoint_overlay = ItemizedIconOverlay<OverlayItem>(context, overlayItems,
object : ItemizedIconOverlay.OnItemGestureListener<OverlayItem> {
override fun onItemSingleTapUp(index: Int, item: OverlayItem): Boolean
{
return false
}
override fun onItemLongPress(index: Int, item: OverlayItem): Boolean
{
Log.i("VOUSSOIR", "MapFragment homepoint.longpress")
val dialog = Dialog(activity as Context)
dialog.setContentView(R.layout.dialog_homepoint)
dialog.setTitle("Homepoint")
(dialog.findViewById(R.id.homepoint_dialog_title) as TextView).text = "Edit homepoint"
val name_input: EditText = dialog.findViewById(R.id.homepoint_name_input)
name_input.setText(homepoint.name)
val radius_input: EditText = dialog.findViewById(R.id.homepoint_radius_input)
radius_input.setText(homepoint.radius.toString())
val delete_button: Button = dialog.findViewById(R.id.homepoint_delete_cancel_button)
val save_button: Button = dialog.findViewById(R.id.homepoint_save_button)
delete_button.text = "Delete"
delete_button.setOnClickListener {
trackbook.database.delete_homepoint(homepoint.id)
trackbook.load_homepoints()
create_homepoint_overlays(requireContext(), mapView, trackbook.homepoints)
dialog.dismiss()
}
save_button.setOnClickListener {
trackbook.database.update_homepoint(homepoint.id, name=name_input.text.toString(), radius=radius_input.text.toString().toDouble())
trackbook.load_homepoints()
create_homepoint_overlays(requireContext(), mapView, trackbook.homepoints)
dialog.dismiss()
}
dialog.show()
return true
}
}
)
homepoints_overlays.add(homepoint_overlay)
} }
for (ov in homepoints_overlays) for (ov in homepoints_overlays)

View file

@ -104,10 +104,11 @@ class Trackbook(): Application() {
return@iterator return@iterator
} }
val cursor: Cursor = database.connection.rawQuery( val cursor: Cursor = database.connection.rawQuery(
"SELECT lat, lon, radius, name FROM homepoints", "SELECT id, lat, lon, radius, name FROM homepoints",
arrayOf() arrayOf()
) )
Log.i("VOUSSOIR", "Trackbook.homepoint_generator: Got ${cursor.count} homepoints.") Log.i("VOUSSOIR", "Trackbook.homepoint_generator: Got ${cursor.count} homepoints.")
val COLUMN_ID = cursor.getColumnIndex("id")
val COLUMN_LAT = cursor.getColumnIndex("lat") val COLUMN_LAT = cursor.getColumnIndex("lat")
val COLUMN_LON = cursor.getColumnIndex("lon") val COLUMN_LON = cursor.getColumnIndex("lon")
val COLUMN_RADIUS = cursor.getColumnIndex("radius") val COLUMN_RADIUS = cursor.getColumnIndex("radius")
@ -117,6 +118,7 @@ class Trackbook(): Application() {
while (cursor.moveToNext()) while (cursor.moveToNext())
{ {
val homepoint = Homepoint( val homepoint = Homepoint(
id=cursor.getLong(COLUMN_ID),
latitude=cursor.getDouble(COLUMN_LAT), latitude=cursor.getDouble(COLUMN_LAT),
longitude=cursor.getDouble(COLUMN_LON), longitude=cursor.getDouble(COLUMN_LON),
radius=cursor.getDouble(COLUMN_RADIUS), radius=cursor.getDouble(COLUMN_RADIUS),

View file

@ -13,6 +13,7 @@ import java.lang.Math.abs
import java.security.SecureRandom import java.security.SecureRandom
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import kotlin.random.Random.Default.nextBits
val iso8601_format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US) val iso8601_format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US)
private val RNG = SecureRandom() private val RNG = SecureRandom()
@ -27,6 +28,11 @@ fun random_int(): Int
return abs(RNG.nextInt()) return abs(RNG.nextInt())
} }
fun random_long(): Long
{
return abs(RNG.nextLong())
}
fun random_device_id(): String fun random_device_id(): String
{ {
return "myphone" + random_int() return "myphone" + random_int()