|
@@ -12,6 +12,7 @@ import android.location.Location
|
|
|
import android.os.IBinder
|
|
|
import android.os.Looper
|
|
|
import android.util.Log
|
|
|
+import android.widget.Toast
|
|
|
import androidx.core.app.ActivityCompat
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
import androidx.core.content.ContextCompat
|
|
@@ -31,10 +32,9 @@ class TrackingService : Service() {
|
|
|
private lateinit var mFusedLocationClient: FusedLocationProviderClient
|
|
|
private lateinit var mLocationRequest: LocationRequest
|
|
|
private lateinit var mLocationCallback: LocationCallback
|
|
|
-
|
|
|
private lateinit var trackApp : JourneyGpsTrackerApplication
|
|
|
-
|
|
|
private val mNotificationId = 123
|
|
|
+ private var isServiceStarted = false
|
|
|
|
|
|
override fun onCreate() {
|
|
|
super.onCreate()
|
|
@@ -43,6 +43,39 @@ class TrackingService : Service() {
|
|
|
startForeground(mNotificationId, generateForegroundNotification() )
|
|
|
}
|
|
|
|
|
|
+ override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
|
+ super.onStartCommand(intent, flags, startId)
|
|
|
+ Log.i(mTag,"onStartCommand()")
|
|
|
+ if (intent != null) {
|
|
|
+ val action = intent.action
|
|
|
+ Log.i(mTag, "onStartCommand() action $action")
|
|
|
+ when (action) {
|
|
|
+ Actions.START.name -> startService()
|
|
|
+ Actions.STOP.name -> stopService()
|
|
|
+ else -> Log.e(mTag, "onStartCommand() This should never happen. No action in the received intent")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Log.e(mTag,"onStartCommand() with a null intent. It has been probably restarted by the system."
|
|
|
+ )
|
|
|
+ }
|
|
|
+ return START_STICKY
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDestroy() {
|
|
|
+ super.onDestroy()
|
|
|
+ Log.i(mTag,"onDestroy()")
|
|
|
+ Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onBind(intent: Intent): IBinder? {
|
|
|
+ Log.i(mTag,"onBind()")
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onTaskRemoved(rootIntent: Intent) {
|
|
|
+ Log.e(mTag,"onTaskRemoved()")
|
|
|
+ }
|
|
|
+
|
|
|
private fun requestLocationUpdates() {
|
|
|
|
|
|
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
@@ -81,18 +114,27 @@ class TrackingService : Service() {
|
|
|
repo.insert(d)
|
|
|
}
|
|
|
|
|
|
- override fun onBind(intent: Intent): IBinder? {
|
|
|
- Log.i(mTag,"onBind()")
|
|
|
- return null
|
|
|
- }
|
|
|
-
|
|
|
- override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
|
- super.onStartCommand(intent, flags, startId)
|
|
|
- Log.i(mTag,"onStartCommand()")
|
|
|
- startForeground(mNotificationId, generateForegroundNotification() )
|
|
|
+ private fun startService() {
|
|
|
+ if(isServiceStarted) return
|
|
|
+ Log.i(mTag,"startService()")
|
|
|
+ Toast.makeText(this, "Starting JourneyGPSTracker service", Toast.LENGTH_SHORT).show()
|
|
|
+ isServiceStarted = true
|
|
|
+ setServiceState(this, ServiceState.STARTED)
|
|
|
requestLocationUpdates()
|
|
|
BackendClient()
|
|
|
- return START_STICKY
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun stopService() {
|
|
|
+ Log.i(mTag,"stopService()")
|
|
|
+ try {
|
|
|
+ stopForeground(true)
|
|
|
+ stopSelf()
|
|
|
+ mFusedLocationClient.removeLocationUpdates(mLocationCallback)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e(mTag, "Service stopped without being started: ${e.message}")
|
|
|
+ }
|
|
|
+ isServiceStarted = false
|
|
|
+ setServiceState(this, ServiceState.STOPPED)
|
|
|
}
|
|
|
|
|
|
private fun generateForegroundNotification() : Notification {
|
|
@@ -131,8 +173,5 @@ class TrackingService : Service() {
|
|
|
return builder.build()
|
|
|
}
|
|
|
|
|
|
- override fun onDestroy() {
|
|
|
- super.onDestroy()
|
|
|
- Log.i(mTag,"onDestroy()")
|
|
|
- }
|
|
|
+
|
|
|
}
|