package dev.onejs.onenative import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.Service import android.content.Intent import android.content.pm.ServiceInfo import android.os.Build import android.os.IBinder import androidx.core.app.NotificationCompat // foreground holder for background location watches: while a background // listener is registered the service runs as a location-type foreground // service with a persistent notification, and stops when the last one is // removed. it carries no location logic itself. class OneLocationService : Service() { override fun onBind(intent: Intent?): IBinder? = null override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val manager = getSystemService(NotificationManager::class.java) if (Build.VERSION.SDK_INT >= 26) { manager?.createNotificationChannel( NotificationChannel(CHANNEL_ID, "Location", NotificationManager.IMPORTANCE_LOW) ) } val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Location updates active") .setContentText("${applicationInfo.loadLabel(packageManager)} is receiving location updates in the background.") .setSmallIcon(android.R.drawable.ic_menu_mylocation) .setOngoing(true) .build() if (Build.VERSION.SDK_INT >= 29) { startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION) } else { startForeground(NOTIFICATION_ID, notification) } return START_NOT_STICKY } companion object { private const val CHANNEL_ID = "one-location" private const val NOTIFICATION_ID = 4301 } }