|
@@ -2,15 +2,18 @@ package com.flacksta.chef.journeygpstracker
|
|
|
|
|
|
import android.Manifest
|
|
import android.Manifest
|
|
import android.app.*
|
|
import android.app.*
|
|
|
|
+import android.app.Notification.FOREGROUND_SERVICE_IMMEDIATE
|
|
import android.content.Context
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.content.pm.PackageManager
|
|
import android.graphics.Bitmap
|
|
import android.graphics.Bitmap
|
|
import android.graphics.BitmapFactory
|
|
import android.graphics.BitmapFactory
|
|
import android.location.Location
|
|
import android.location.Location
|
|
|
|
+import android.os.Build
|
|
import android.os.IBinder
|
|
import android.os.IBinder
|
|
import android.os.Looper
|
|
import android.os.Looper
|
|
import android.util.Log
|
|
import android.util.Log
|
|
|
|
+import androidx.annotation.RequiresApi
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.app.NotificationCompat
|
|
import androidx.core.app.NotificationCompat
|
|
import androidx.core.content.ContextCompat
|
|
import androidx.core.content.ContextCompat
|
|
@@ -18,10 +21,7 @@ import com.flacksta.chef.journeygpstracker.backend.BackendClient
|
|
import com.flacksta.chef.journeygpstracker.database.GpsData
|
|
import com.flacksta.chef.journeygpstracker.database.GpsData
|
|
import com.flacksta.chef.journeygpstracker.database.GpsPosRepository
|
|
import com.flacksta.chef.journeygpstracker.database.GpsPosRepository
|
|
import com.google.android.gms.location.*
|
|
import com.google.android.gms.location.*
|
|
-import kotlinx.coroutines.GlobalScope
|
|
|
|
-import kotlinx.coroutines.delay
|
|
|
|
-import kotlinx.coroutines.launch
|
|
|
|
-import kotlinx.coroutines.runBlocking
|
|
|
|
|
|
+import kotlinx.coroutines.*
|
|
|
|
|
|
class TrackingService : Service() {
|
|
class TrackingService : Service() {
|
|
|
|
|
|
@@ -39,20 +39,14 @@ class TrackingService : Service() {
|
|
Log.i(mTag,"onCreate()")
|
|
Log.i(mTag,"onCreate()")
|
|
generateForegroundNotification()
|
|
generateForegroundNotification()
|
|
trackApp = JourneyGpsTrackerApplication()
|
|
trackApp = JourneyGpsTrackerApplication()
|
|
- startForeground(mNotificationId, notification)
|
|
|
|
|
|
+ startForeground(mNotificationId, generateForegroundNotification() )
|
|
|
|
|
|
// Start background upload functions...
|
|
// Start background upload functions...
|
|
- val bec: BackendClient = BackendClient()
|
|
|
|
|
|
+ BackendClient()
|
|
|
|
|
|
Log.i(mTag,"Requested foreground service...")
|
|
Log.i(mTag,"Requested foreground service...")
|
|
}
|
|
}
|
|
|
|
|
|
- private fun insertPos(pos: Location) = GlobalScope.launch {
|
|
|
|
- var d = GpsData(0, pos.time, pos.latitude, pos.longitude, pos.accuracy, false)
|
|
|
|
- val repo: GpsPosRepository = trackApp.repository
|
|
|
|
- repo.insert(d)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private fun requestLocationUpdates() {
|
|
private fun requestLocationUpdates() {
|
|
|
|
|
|
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
@@ -70,7 +64,9 @@ class TrackingService : Service() {
|
|
counter += 1
|
|
counter += 1
|
|
val acc = location.accuracy
|
|
val acc = location.accuracy
|
|
Log.i(mTag, "Got location: $counter $acc")
|
|
Log.i(mTag, "Got location: $counter $acc")
|
|
- insertPos(location)
|
|
|
|
|
|
+ CoroutineScope(Dispatchers.Default).launch {
|
|
|
|
+ insertPos(location)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -83,6 +79,12 @@ class TrackingService : Service() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private suspend fun insertPos(pos: Location) {
|
|
|
|
+ val d = GpsData(0, pos.time, pos.latitude, pos.longitude, pos.accuracy, false)
|
|
|
|
+ val repo: GpsPosRepository = trackApp.repository
|
|
|
|
+ repo.insert(d)
|
|
|
|
+ }
|
|
|
|
+
|
|
override fun onBind(intent: Intent): IBinder? {
|
|
override fun onBind(intent: Intent): IBinder? {
|
|
Log.i(mTag,"onBind()")
|
|
Log.i(mTag,"onBind()")
|
|
return null
|
|
return null
|
|
@@ -96,11 +98,10 @@ class TrackingService : Service() {
|
|
}
|
|
}
|
|
|
|
|
|
private var iconNotification: Bitmap? = null
|
|
private var iconNotification: Bitmap? = null
|
|
- private var notification: Notification? = null
|
|
|
|
private var mNotificationManager: NotificationManager? = null
|
|
private var mNotificationManager: NotificationManager? = null
|
|
private val mNotificationId = 123
|
|
private val mNotificationId = 123
|
|
|
|
|
|
- private fun generateForegroundNotification() {
|
|
|
|
|
|
+ private fun generateForegroundNotification() : Notification {
|
|
val intentMainLanding = Intent(this, MainActivity::class.java)
|
|
val intentMainLanding = Intent(this, MainActivity::class.java)
|
|
val pendingIntent =
|
|
val pendingIntent =
|
|
PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
|
|
PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
|
|
@@ -122,20 +123,21 @@ class TrackingService : Service() {
|
|
|
|
|
|
val builder = NotificationCompat.Builder(this, "service_channel")
|
|
val builder = NotificationCompat.Builder(this, "service_channel")
|
|
|
|
|
|
- builder.setContentTitle(StringBuilder(resources.getString(R.string.app_name)).append(" service is running").toString())
|
|
|
|
|
|
+ builder.setContentTitle(StringBuilder(resources.getString(R.string.app_name)).append(" is tracking your journey.").toString())
|
|
.setTicker(StringBuilder(resources.getString(R.string.app_name)).append("service is running").toString())
|
|
.setTicker(StringBuilder(resources.getString(R.string.app_name)).append("service is running").toString())
|
|
- .setContentText("Touch to open") // , swipe down for more options.
|
|
|
|
|
|
+ .setContentText("Touch to open the App") // , swipe down for more options.
|
|
.setSmallIcon(R.drawable.ic_baseline_map_24)
|
|
.setSmallIcon(R.drawable.ic_baseline_map_24)
|
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
.setWhen(0)
|
|
.setWhen(0)
|
|
.setOnlyAlertOnce(true)
|
|
.setOnlyAlertOnce(true)
|
|
.setContentIntent(pendingIntent)
|
|
.setContentIntent(pendingIntent)
|
|
.setOngoing(true)
|
|
.setOngoing(true)
|
|
|
|
+ .setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE)
|
|
if (iconNotification != null) {
|
|
if (iconNotification != null) {
|
|
builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
|
|
builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
|
|
}
|
|
}
|
|
builder.color = ContextCompat.getColor(this, R.color.purple_200)
|
|
builder.color = ContextCompat.getColor(this, R.color.purple_200)
|
|
- builder.build().also { notification = it }
|
|
|
|
|
|
+ return builder.build()
|
|
}
|
|
}
|
|
|
|
|
|
override fun onDestroy() {
|
|
override fun onDestroy() {
|