package com.flacksta.chef.journeygpstracker import android.Manifest import android.app.* import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.graphics.Bitmap import android.graphics.BitmapFactory 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.google.android.gms.location.* class TrackingService : Service() { private val mTag: String = "TrackingService" var counter = 0 private lateinit var mFusedLocationClient: FusedLocationProviderClient private lateinit var mLocationRequest: LocationRequest private lateinit var mLocationCallback: LocationCallback override fun onCreate() { super.onCreate() Log.i(mTag,"onCreate()") requestLocationUpdates() } private fun requestLocationUpdates() { mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this) mLocationRequest = LocationRequest.create().apply { interval = 2000 fastestInterval = 2000 priority = LocationRequest.PRIORITY_HIGH_ACCURACY } mLocationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult?) { locationResult ?: return for (location in locationResult.locations){ counter += 1 Log.i(mTag, "Got location: $counter") } } } val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) if (permission == PackageManager.PERMISSION_GRANTED ) { mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper()) } } override fun onBind(intent: Intent): IBinder? { Log.i(mTag,"onBind()") return null } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @Suppress("UNUSED_VARIABLE") val retVal: Int = super.onStartCommand(intent, flags, startId) Log.i(mTag,"onStartCommand()") generateForegroundNotification() return START_STICKY } private var iconNotification: Bitmap? = null private var notification: Notification? = null private var mNotificationManager: NotificationManager? = null private val mNotificationId = 123 private fun generateForegroundNotification() { val intentMainLanding = Intent(this, MainScreenActivity::class.java) val pendingIntent = PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE) iconNotification = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher) if (mNotificationManager == null) { mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } assert(mNotificationManager != null) mNotificationManager?.createNotificationChannelGroup( NotificationChannelGroup("chats_group", "Chats") ) val notificationChannel = NotificationChannel("service_channel", "Service Notifications", NotificationManager.IMPORTANCE_MIN) notificationChannel.enableLights(false) notificationChannel.lockscreenVisibility = Notification.VISIBILITY_SECRET mNotificationManager?.createNotificationChannel(notificationChannel) val builder = NotificationCompat.Builder(this, "service_channel") builder.setContentTitle(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. .setSmallIcon(R.drawable.ic_baseline_map_24) .setPriority(NotificationCompat.PRIORITY_LOW) .setWhen(0) .setOnlyAlertOnce(true) .setContentIntent(pendingIntent) .setOngoing(true) if (iconNotification != null) { builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false)) } builder.color = ContextCompat.getColor(this, R.color.purple_200) builder.build().also { notification = it } startForeground(mNotificationId, notification) } override fun onDestroy() { super.onDestroy() Log.i(mTag,"onDestroy()") } }