Kaynağa Gözat

Save to dB works.

Thomas Chef 3 yıl önce
ebeveyn
işleme
c48ea4cf58

+ 1 - 0
app/build.gradle

@@ -65,6 +65,7 @@ dependencies {
     // Kotlin components
     api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
     api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
+    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3")
 
     // UI
     implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"

+ 8 - 5
app/src/main/java/com/flacksta/chef/journeygpstracker/JourneyGpsTrackerApplication.kt

@@ -5,12 +5,10 @@ import android.util.Log
 import com.flacksta.chef.journeygpstracker.database.GpsData
 import com.flacksta.chef.journeygpstracker.database.GpsPosRepository
 import com.flacksta.chef.journeygpstracker.database.GpsPosRoomDatabase
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.*
 
 class JourneyGpsTrackerApplication : Application() {
 
-    // No need to cancel this scope as it'll be torn down with the process
     val applicationScope = CoroutineScope(SupervisorJob())
 
     val database by lazy { GpsPosRoomDatabase.getDatabase(this, applicationScope) }
@@ -18,9 +16,14 @@ class JourneyGpsTrackerApplication : Application() {
 
     override fun onCreate() {
         super.onCreate()
+
         Log.i("JourneyGpsTrackerApplication","onCreate()")
-        //var d = GpsData(0,0,0.0,0.0,2.0)
-        //repository.insert(d)
+        initStuff()
     }
 
+    fun initStuff() = runBlocking { // this: CoroutineScope
+        launch {
+            repository.deleteAll()
+        }
+    }
 }

+ 16 - 5
app/src/main/java/com/flacksta/chef/journeygpstracker/TrackingService.kt

@@ -7,13 +7,18 @@ import android.content.Intent
 import android.content.pm.PackageManager
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
+import android.location.Location
 import android.os.IBinder
 import android.os.Looper
 import android.util.Log
 import androidx.core.app.ActivityCompat
 import androidx.core.app.NotificationCompat
 import androidx.core.content.ContextCompat
+import com.flacksta.chef.journeygpstracker.database.GpsData
+import com.flacksta.chef.journeygpstracker.database.GpsPosRepository
 import com.google.android.gms.location.*
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.runBlocking
 
 class TrackingService : Service() {
 
@@ -24,18 +29,25 @@ class TrackingService : Service() {
     private lateinit var mLocationRequest: LocationRequest
     private lateinit var mLocationCallback: LocationCallback
 
-    //val database by lazy { GpsPosRoomDatabase.getDatabase(this) }
-    //val database by lazy { GpsPosRoomDatabase.getDatabase(this) }
-    //val repository by lazy { GpsPosRepository(database.gpsDataDao()) }
+    private lateinit var trackApp : JourneyGpsTrackerApplication
 
     override fun onCreate() {
         super.onCreate()
         Log.i(mTag,"onCreate()")
         generateForegroundNotification()
+        trackApp = JourneyGpsTrackerApplication()
         startForeground(mNotificationId, notification)
         Log.i(mTag,"Requested foreground service...")
     }
 
+    private fun insertPos(pos: Location) = runBlocking {
+        launch {
+            var d = GpsData(0, System.currentTimeMillis(), pos.latitude, pos.longitude, pos.accuracy)
+            val repo: GpsPosRepository = trackApp.repository
+            repo.insert(d)
+        }
+    }
+
     private fun requestLocationUpdates() {
 
         mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
@@ -53,8 +65,7 @@ class TrackingService : Service() {
                     counter += 1
                     val acc = location.accuracy
                     Log.i(mTag, "Got location: $counter  $acc")
-
-
+                    insertPos(location)
                 }
             }
         }

+ 1 - 1
app/src/main/java/com/flacksta/chef/journeygpstracker/database/GpsData.kt

@@ -10,5 +10,5 @@ data class GpsData(
         @ColumnInfo(name = "ts") val ts: Long,
         @ColumnInfo(name = "latitude") val latitude: Double,
         @ColumnInfo(name = "longitude") val longitude: Double,
-        @ColumnInfo(name = "accuracy") val accuracy: Double,
+        @ColumnInfo(name = "accuracy") val accuracy: Float,
 )

+ 6 - 3
app/src/main/java/com/flacksta/chef/journeygpstracker/database/GpsPosRepository.kt

@@ -8,12 +8,15 @@ class GpsPosRepository(private val gpsDataDao: GpsDataDao) {
 
     val allGpsData: Flow<List<GpsData>> = gpsDataDao.getGpsPositions()
 
-    // By default Room runs suspend queries off the main thread, therefore, we don't need to
-    // implement anything else to ensure we're not doing long running database work
-    // off the main thread.
     @Suppress("RedundantSuspendModifier")
     @WorkerThread
     suspend fun insert(word: GpsData) {
         gpsDataDao.insert(word)
     }
+
+    @Suppress("RedundantSuspendModifier")
+    @WorkerThread
+    suspend fun deleteAll() {
+        gpsDataDao.deleteAll()
+    }
 }

+ 2 - 3
app/src/main/java/com/flacksta/chef/journeygpstracker/database/GpsPosRoomDatabase.kt

@@ -29,6 +29,7 @@ public abstract class GpsPosRoomDatabase : RoomDatabase() {
             // if the INSTANCE is not null, then return it,
             // if it is, then create the database
             return INSTANCE ?: synchronized(this) {
+                Log.i(mTag,"getDatabase() B")
                 val instance = Room.databaseBuilder(
                         context.applicationContext,
                         GpsPosRoomDatabase::class.java,
@@ -64,10 +65,8 @@ public abstract class GpsPosRoomDatabase : RoomDatabase() {
         }
 
         suspend fun populateDatabase(gpsDataDao: GpsDataDao) {
-            Log.i(mTag,"populateDatabase()")
+            Log.i(mTag,"populateDatabase() Delete ALL")
             gpsDataDao.deleteAll()
-            var d = GpsData(0,0,0.0,0.0,2.0)
-            gpsDataDao.insert(d)
         }
     }
 }

+ 1 - 1
build.gradle

@@ -28,7 +28,7 @@ ext {
     appCompatVersion = '1.4.1'
     constraintLayoutVersion = '2.1.3'
     coreTestingVersion = '2.1.0'
-    coroutines = '1.6.0'
+    coroutines = '1.6.3'
     lifecycleVersion = '2.4.0'
     materialVersion = '1.5.0'
     roomVersion = '2.4.1'