1
0

feat: add stop tracking button

This commit is contained in:
Sweetbread 2025-05-22 22:54:51 +03:00
parent 7bee8910ec
commit b55ee94d8a
Signed by: Sweetbread
GPG Key ID: 17A5CB9A7DD85319

View File

@ -5,6 +5,7 @@ import android.Manifest.permission.POST_NOTIFICATIONS
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Build
@ -41,12 +42,14 @@ class LocationForegroundService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("Service", "onStartCommand")
if (!(hasPermission(ACCESS_FINE_LOCATION)
if (
!(hasPermission(ACCESS_FINE_LOCATION)
&& (
(Build.VERSION.SDK_INT < 33)
|| hasPermission(POST_NOTIFICATIONS)
)
)) {
))
|| intent?.action == ACTION_STOP_SERVICE
) {
stopSelf()
return START_NOT_STICKY
}
@ -60,6 +63,7 @@ class LocationForegroundService : Service() {
}
override fun onDestroy() {
Log.d("Service", "Destroyed")
locationTracker.stopTracking()
super.onDestroy()
}
@ -69,8 +73,8 @@ class LocationForegroundService : Service() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"location_channel",
"Location Tracking",
NotificationManager.IMPORTANCE_LOW
"Отправка Местоположения",
NotificationManager.IMPORTANCE_HIGH
)
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
@ -78,12 +82,36 @@ class LocationForegroundService : Service() {
}
private fun createNotification(): Notification {
val stopIntent = Intent(this, LocationForegroundService::class.java).apply {
action = ACTION_STOP_SERVICE
}
val stopPendingIntent = PendingIntent.getService(
this,
0,
stopIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
else
PendingIntent.FLAG_UPDATE_CURRENT
)
return NotificationCompat.Builder(this, "location_channel")
.setContentTitle("Отслеживание местоположения")
.setContentText("Обновление каждые ${SettingsPreferences.interval} секунд")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setSmallIcon(R.drawable.share_location)
.addAction(
R.drawable.cancel,
"Остановить",
stopPendingIntent
)
.setOngoing(true)
.build()
}
override fun onBind(intent: Intent?): IBinder? = null
}
companion object {
const val ACTION_STOP_SERVICE = "ru.risdeveau.geotracker.STOP_SERVICE"
}
}