allow user stop service

This commit is contained in:
2026-06-13 09:04:58 +07:00
parent ba92fbd737
commit 1cd4eb330b
2 changed files with 24 additions and 7 deletions

View File

@@ -8,7 +8,7 @@
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<queries> <queries>
@@ -59,7 +59,7 @@
android:name=".NostrForegroundService" android:name=".NostrForegroundService"
android:enabled="true" android:enabled="true"
android:exported="false" android:exported="false"
android:foregroundServiceType="dataSync" /> android:foregroundServiceType="remoteMessaging" />
</application> </application>
</manifest> </manifest>

View File

@@ -22,6 +22,8 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.File import java.io.File
private const val GROUP_KEY_MESSAGES = "su.reya.coop.MESSAGES"
class NostrForegroundService : Service() { class NostrForegroundService : Service() {
private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val nostr by lazy { NostrManager.instance } private val nostr by lazy { NostrManager.instance }
@@ -29,10 +31,6 @@ class NostrForegroundService : Service() {
override fun onBind(intent: Intent?): IBinder? = null override fun onBind(intent: Intent?): IBinder? = null
private fun isUserInApp(): Boolean {
return ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
}
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
createNotificationChannel() createNotificationChannel()
@@ -40,6 +38,13 @@ class NostrForegroundService : Service() {
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.action == "STOP_SERVICE") {
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
return START_NOT_STICKY
}
// Start the nostr service in the foreground
startAsForeground() startAsForeground()
// Check if the service is already running // Check if the service is already running
@@ -90,15 +95,25 @@ class NostrForegroundService : Service() {
return START_STICKY return START_STICKY
} }
private fun isUserInApp(): Boolean {
return ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
}
private fun startAsForeground() { private fun startAsForeground() {
val notification = createNotification() val notification = createNotification()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC) startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING)
} else { } else {
startForeground(1, notification) startForeground(1, notification)
} }
} }
private fun getStopServicePendingIntent(): PendingIntent {
val intent = Intent(this, NostrForegroundService::class.java).apply {
action = "STOP_SERVICE"
}
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}
private fun createNotificationChannel() { private fun createNotificationChannel() {
val manager = getSystemService(NotificationManager::class.java) val manager = getSystemService(NotificationManager::class.java)
@@ -122,10 +137,12 @@ class NostrForegroundService : Service() {
private fun createNotification(content: String? = null): Notification { private fun createNotification(content: String? = null): Notification {
val builder = NotificationCompat.Builder(this, "nostr_service") val builder = NotificationCompat.Builder(this, "nostr_service")
.setGroup(GROUP_KEY_MESSAGES)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setOngoing(true) .setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MIN) .setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE) .setCategory(Notification.CATEGORY_SERVICE)
.addAction(R.drawable.ic_notification, "Stop", getStopServicePendingIntent())
if (content != null) { if (content != null) {
builder.setContentTitle("Coop") builder.setContentTitle("Coop")