1
0

feat: add stop tracking button

This commit is contained in:
Sweetbread 2025-05-22 22:54:51 +03:00
parent 7bee8910ec
commit 761bd921ab
Signed by: Sweetbread
GPG Key ID: 17A5CB9A7DD85319
3 changed files with 53 additions and 7 deletions

View File

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

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M336,680L480,536L624,680L680,624L536,480L680,336L624,280L480,424L336,280L280,336L424,480L280,624L336,680ZM480,880Q397,880 324,848.5Q251,817 197,763Q143,709 111.5,636Q80,563 80,480Q80,397 111.5,324Q143,251 197,197Q251,143 324,111.5Q397,80 480,80Q563,80 636,111.5Q709,143 763,197Q817,251 848.5,324Q880,397 880,480Q880,563 848.5,636Q817,709 763,763Q709,817 636,848.5Q563,880 480,880ZM480,800Q614,800 707,707Q800,614 800,480Q800,346 707,253Q614,160 480,160Q346,160 253,253Q160,346 160,480Q160,614 253,707Q346,800 480,800ZM480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M519,878L519,798Q561,792 600.5,775Q640,758 675,732L733,790Q686,827 632,849.5Q578,872 519,878ZM789,732L733,676Q759,643 775,603.5Q791,564 797,520L879,520Q871,582 848.5,635.5Q826,689 789,732ZM797,440Q791,395 775,355.5Q759,316 733,284L789,228Q827,272 850.5,326Q874,380 879,440L797,440ZM439,878Q286,860 183.5,747Q81,634 81,480Q81,325 183.5,212Q286,99 439,82L439,162Q319,179 240,269Q161,359 161,480Q161,601 240,690.5Q319,780 439,798L439,878ZM677,228Q641,201 601,184Q561,167 519,162L519,82Q578,87 632,109.5Q686,132 733,170L677,228ZM480,680Q422,631 371,575Q320,519 320,444Q320,376 366.5,328Q413,280 480,280Q547,280 593.5,328Q640,376 640,444Q640,519 589,575Q538,631 480,680ZM480,480Q498,480 510.5,467.5Q523,455 523,437Q523,420 510.5,407Q498,394 480,394Q462,394 449.5,407Q437,420 437,437Q437,455 449.5,467.5Q462,480 480,480Z"/>
</vector>