TrackingService.kt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. requestLocationUpdates()
  26. }
  27. private fun requestLocationUpdates() {
  28. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
  29. mLocationRequest = LocationRequest.create().apply {
  30. interval = 2000
  31. fastestInterval = 2000
  32. priority = LocationRequest.PRIORITY_HIGH_ACCURACY
  33. }
  34. mLocationCallback = object : LocationCallback() {
  35. override fun onLocationResult(locationResult: LocationResult?) {
  36. locationResult ?: return
  37. for (location in locationResult.locations){
  38. counter += 1
  39. Log.i(mTag, "Got location: $counter")
  40. }
  41. }
  42. }
  43. val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  44. if (permission == PackageManager.PERMISSION_GRANTED ) {
  45. mFusedLocationClient.requestLocationUpdates(mLocationRequest,
  46. mLocationCallback,
  47. Looper.getMainLooper())
  48. }
  49. }
  50. override fun onBind(intent: Intent): IBinder? {
  51. Log.i(mTag,"onBind()")
  52. return null
  53. }
  54. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  55. @Suppress("UNUSED_VARIABLE") val retVal: Int = super.onStartCommand(intent, flags, startId)
  56. Log.i(mTag,"onStartCommand()")
  57. generateForegroundNotification()
  58. return START_STICKY
  59. }
  60. private var iconNotification: Bitmap? = null
  61. private var notification: Notification? = null
  62. private var mNotificationManager: NotificationManager? = null
  63. private val mNotificationId = 123
  64. private fun generateForegroundNotification() {
  65. val intentMainLanding = Intent(this, MainScreenActivity::class.java)
  66. val pendingIntent =
  67. PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
  68. iconNotification = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
  69. if (mNotificationManager == null) {
  70. mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  71. }
  72. assert(mNotificationManager != null)
  73. mNotificationManager?.createNotificationChannelGroup(
  74. NotificationChannelGroup("chats_group", "Chats")
  75. )
  76. val notificationChannel =
  77. NotificationChannel("service_channel", "Service Notifications",
  78. NotificationManager.IMPORTANCE_MIN)
  79. notificationChannel.enableLights(false)
  80. notificationChannel.lockscreenVisibility = Notification.VISIBILITY_SECRET
  81. mNotificationManager?.createNotificationChannel(notificationChannel)
  82. val builder = NotificationCompat.Builder(this, "service_channel")
  83. builder.setContentTitle(StringBuilder(resources.getString(R.string.app_name)).append(" service is running").toString())
  84. .setTicker(StringBuilder(resources.getString(R.string.app_name)).append("service is running").toString())
  85. .setContentText("Touch to open") // , swipe down for more options.
  86. .setSmallIcon(R.drawable.ic_baseline_map_24)
  87. .setPriority(NotificationCompat.PRIORITY_LOW)
  88. .setWhen(0)
  89. .setOnlyAlertOnce(true)
  90. .setContentIntent(pendingIntent)
  91. .setOngoing(true)
  92. if (iconNotification != null) {
  93. builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
  94. }
  95. builder.color = ContextCompat.getColor(this, R.color.purple_200)
  96. builder.build().also { notification = it }
  97. startForeground(mNotificationId, notification)
  98. }
  99. override fun onDestroy() {
  100. super.onDestroy()
  101. Log.i(mTag,"onDestroy()")
  102. }
  103. }