TrackingService.kt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.flacksta.chef.journeygpstracker
  2. import android.Manifest
  3. import android.app.*
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.content.pm.PackageManager
  7. import android.graphics.Bitmap
  8. import android.graphics.BitmapFactory
  9. import android.os.IBinder
  10. import android.os.Looper
  11. import android.util.Log
  12. import androidx.core.app.ActivityCompat
  13. import androidx.core.app.NotificationCompat
  14. import androidx.core.content.ContextCompat
  15. import com.google.android.gms.location.*
  16. class TrackingService : Service() {
  17. private val mTag: String = "TrackingService"
  18. var counter = 0
  19. private lateinit var mFusedLocationClient: FusedLocationProviderClient
  20. private lateinit var mLocationRequest: LocationRequest
  21. private lateinit var mLocationCallback: LocationCallback
  22. override fun onCreate() {
  23. super.onCreate()
  24. Log.i(mTag,"onCreate()")
  25. generateForegroundNotification()
  26. startForeground(mNotificationId, notification)
  27. Log.i(mTag,"Requested foreground service...")
  28. }
  29. private fun requestLocationUpdates() {
  30. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
  31. mLocationRequest = LocationRequest.create().apply {
  32. interval = 2000
  33. fastestInterval = 2000
  34. //smallestDisplacement = 2.0f
  35. priority = LocationRequest.PRIORITY_HIGH_ACCURACY
  36. }
  37. mLocationCallback = object : LocationCallback() {
  38. override fun onLocationResult(locationResult: LocationResult?) {
  39. locationResult ?: return
  40. for (location in locationResult.locations){
  41. counter += 1
  42. val acc = location.accuracy
  43. Log.i(mTag, "Got location: $counter $acc")
  44. }
  45. }
  46. }
  47. val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  48. if (permission == PackageManager.PERMISSION_GRANTED ) {
  49. mFusedLocationClient.requestLocationUpdates(mLocationRequest,
  50. mLocationCallback,
  51. Looper.getMainLooper())
  52. }
  53. }
  54. override fun onBind(intent: Intent): IBinder? {
  55. Log.i(mTag,"onBind()")
  56. return null
  57. }
  58. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  59. @Suppress("UNUSED_VARIABLE") val retVal: Int = super.onStartCommand(intent, flags, startId)
  60. Log.i(mTag,"onStartCommand()")
  61. requestLocationUpdates()
  62. return START_STICKY
  63. }
  64. private var iconNotification: Bitmap? = null
  65. private var notification: Notification? = null
  66. private var mNotificationManager: NotificationManager? = null
  67. private val mNotificationId = 123
  68. private fun generateForegroundNotification() {
  69. val intentMainLanding = Intent(this, MainScreenActivity::class.java)
  70. val pendingIntent =
  71. PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
  72. iconNotification = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
  73. if (mNotificationManager == null) {
  74. mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  75. }
  76. assert(mNotificationManager != null)
  77. mNotificationManager?.createNotificationChannelGroup(
  78. NotificationChannelGroup("chats_group", "Chats")
  79. )
  80. val notificationChannel =
  81. NotificationChannel("service_channel", "Service Notifications",
  82. NotificationManager.IMPORTANCE_MIN)
  83. notificationChannel.enableLights(false)
  84. notificationChannel.lockscreenVisibility = Notification.VISIBILITY_SECRET
  85. mNotificationManager?.createNotificationChannel(notificationChannel)
  86. val builder = NotificationCompat.Builder(this, "service_channel")
  87. builder.setContentTitle(StringBuilder(resources.getString(R.string.app_name)).append(" service is running").toString())
  88. .setTicker(StringBuilder(resources.getString(R.string.app_name)).append("service is running").toString())
  89. .setContentText("Touch to open") // , swipe down for more options.
  90. .setSmallIcon(R.drawable.ic_baseline_map_24)
  91. .setPriority(NotificationCompat.PRIORITY_LOW)
  92. .setWhen(0)
  93. .setOnlyAlertOnce(true)
  94. .setContentIntent(pendingIntent)
  95. .setOngoing(true)
  96. if (iconNotification != null) {
  97. builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
  98. }
  99. builder.color = ContextCompat.getColor(this, R.color.purple_200)
  100. builder.build().also { notification = it }
  101. }
  102. override fun onDestroy() {
  103. super.onDestroy()
  104. Log.i(mTag,"onDestroy()")
  105. }
  106. }