checkpoint
This commit is contained in:
		
							parent
							
								
									62675e1b97
								
							
						
					
					
						commit
						9830ebf1e7
					
				
					 5 changed files with 94 additions and 21 deletions
				
			
		|  | @ -30,6 +30,14 @@ class Database(trackbook: Trackbook) | |||
|         Log.i("VOUSSOIR", "Database.open: Calling all listeners") | ||||
|     } | ||||
| 
 | ||||
|     fun begin_transaction() | ||||
|     { | ||||
|         if (! connection.inTransaction()) | ||||
|         { | ||||
|             connection.beginTransaction() | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fun commit() | ||||
|     { | ||||
|         if (! this.ready) | ||||
|  | @ -57,37 +65,48 @@ class Database(trackbook: Trackbook) | |||
|             put("sat", trkpt.numberSatellites) | ||||
|             put("ele", trkpt.altitude) | ||||
|         } | ||||
|         if (! connection.inTransaction()) | ||||
|         { | ||||
|             connection.beginTransaction() | ||||
|         } | ||||
|         begin_transaction() | ||||
|         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") | ||||
|         val values = ContentValues().apply { | ||||
|             put("id", id) | ||||
|             put("lat", latitude) | ||||
|             put("lon", longitude) | ||||
|             put("radius", radius) | ||||
|             put("name", name) | ||||
|         } | ||||
|         if (! connection.inTransaction()) | ||||
|         { | ||||
|             connection.beginTransaction() | ||||
|         } | ||||
|         begin_transaction() | ||||
|         connection.insert("homepoints", null, values) | ||||
|         commit() | ||||
|         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() | ||||
|     { | ||||
|         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 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.endTransaction() | ||||
|     } | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ package org.y20k.trackbook | |||
| import android.location.Location | ||||
| 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() | ||||
| 
 | ||||
|  |  | |||
|  | @ -33,6 +33,7 @@ import android.view.WindowManager | |||
| import android.widget.Button | ||||
| import android.widget.EditText | ||||
| import android.widget.TextView | ||||
| import android.widget.Toast | ||||
| import androidx.activity.result.contract.ActivityResultContracts.RequestPermission | ||||
| import androidx.core.content.ContextCompat | ||||
| import androidx.core.view.isVisible | ||||
|  | @ -159,9 +160,8 @@ class MapFragment : Fragment() | |||
|         compassOverlay.setCompassCenter((screen_width / densityScalingFactor) - 36f, 36f) | ||||
|         mapView.overlays.add(compassOverlay) | ||||
| 
 | ||||
|         val app: Trackbook = (requireContext().applicationContext as Trackbook) | ||||
|         app.load_homepoints() | ||||
|         create_homepoint_overlays(requireContext(), mapView, app.homepoints) | ||||
|         trackbook.load_homepoints() | ||||
|         create_homepoint_overlays(requireContext(), mapView, trackbook.homepoints) | ||||
|         if (database_changed_listener !in trackbook.database_changed_listeners) | ||||
|         { | ||||
|             trackbook.database_changed_listeners.add(database_changed_listener) | ||||
|  | @ -206,9 +206,15 @@ class MapFragment : Fragment() | |||
|                     dialog.cancel() | ||||
|                 } | ||||
|                 save_button.setOnClickListener { | ||||
|                     app.database.insert_homepoint(name=name_input.text.toString(), latitude=point.latitude, longitude=point.longitude, radius=radius_input.text.toString().toDouble()) | ||||
|                     app.load_homepoints() | ||||
|                     create_homepoint_overlays(requireContext(), mapView, app.homepoints) | ||||
|                     trackbook.database.insert_homepoint( | ||||
|                         id=random_long(), | ||||
|                         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() | ||||
|                 } | ||||
| 
 | ||||
|  | @ -483,7 +489,6 @@ class MapFragment : Fragment() | |||
|     fun create_homepoint_overlays(context: Context, map_view: MapView, homepoints: List<Homepoint>) | ||||
|     { | ||||
|         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)!! | ||||
| 
 | ||||
|  | @ -497,10 +502,51 @@ class MapFragment : Fragment() | |||
|             p.outlinePaint.color = Color.argb(0, 0, 0, 0) | ||||
|             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) | ||||
|             overlayItem.setMarker(newMarker) | ||||
|             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) | ||||
|  |  | |||
|  | @ -104,10 +104,11 @@ class Trackbook(): Application() { | |||
|             return@iterator | ||||
|         } | ||||
|         val cursor: Cursor = database.connection.rawQuery( | ||||
|             "SELECT lat, lon, radius, name FROM homepoints", | ||||
|             "SELECT id, lat, lon, radius, name FROM homepoints", | ||||
|             arrayOf() | ||||
|         ) | ||||
|         Log.i("VOUSSOIR", "Trackbook.homepoint_generator: Got ${cursor.count} homepoints.") | ||||
|         val COLUMN_ID = cursor.getColumnIndex("id") | ||||
|         val COLUMN_LAT = cursor.getColumnIndex("lat") | ||||
|         val COLUMN_LON = cursor.getColumnIndex("lon") | ||||
|         val COLUMN_RADIUS = cursor.getColumnIndex("radius") | ||||
|  | @ -117,6 +118,7 @@ class Trackbook(): Application() { | |||
|             while (cursor.moveToNext()) | ||||
|             { | ||||
|                 val homepoint = Homepoint( | ||||
|                     id=cursor.getLong(COLUMN_ID), | ||||
|                     latitude=cursor.getDouble(COLUMN_LAT), | ||||
|                     longitude=cursor.getDouble(COLUMN_LON), | ||||
|                     radius=cursor.getDouble(COLUMN_RADIUS), | ||||
|  |  | |||
|  | @ -13,6 +13,7 @@ import java.lang.Math.abs | |||
| import java.security.SecureRandom | ||||
| import java.text.SimpleDateFormat | ||||
| import java.util.* | ||||
| import kotlin.random.Random.Default.nextBits | ||||
| 
 | ||||
| val iso8601_format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US) | ||||
| private val RNG = SecureRandom() | ||||
|  | @ -27,7 +28,12 @@ fun random_int(): Int | |||
|     return abs(RNG.nextInt()) | ||||
| } | ||||
| 
 | ||||
| fun random_long(): Long | ||||
| { | ||||
|     return abs(RNG.nextLong()) | ||||
| } | ||||
| 
 | ||||
| fun random_device_id(): String | ||||
| { | ||||
|     return "myphone" + random_int() | ||||
| } | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue