Make FcmFetchForegroundService stop itself.

fork-5.53.8
Greyson Parrelli 2022-05-11 13:42:40 -04:00 zatwierdzone przez Alex Hart
rodzic eeb4cdf064
commit c0db88960c
2 zmienionych plików z 25 dodań i 3 usunięć

Wyświetl plik

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.gcm
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import androidx.core.app.NotificationCompat
@ -16,7 +17,22 @@ import org.thoughtcrime.securesms.notifications.NotificationIds
*/
class FcmFetchForegroundService : Service() {
private val TAG = Log.tag(FcmFetchForegroundService::class.java)
companion object {
private val TAG = Log.tag(FcmFetchForegroundService::class.java)
private const val KEY_STOP_SELF = "stop_self"
/**
* Android's requirement for calling [startForeground] is enforced _even if your service was stopped before it started_.
* That means we can't just stop it normally, since we don't know if it got to start yet.
* The safest thing to do is to just tell it to start so it can call [startForeground] and then stop itself.
* Fun.
*/
fun buildStopIntent(context: Context): Intent {
return Intent(context, FcmFetchForegroundService::class.java).apply {
putExtra(KEY_STOP_SELF, true)
}
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startForeground(
@ -31,7 +47,13 @@ class FcmFetchForegroundService : Service() {
.build()
)
return START_STICKY
return if (intent != null && intent.getBooleanExtra(KEY_STOP_SELF, false)) {
stopForeground(true)
stopSelf()
START_NOT_STICKY
} else {
START_STICKY
}
}
override fun onDestroy() {

Wyświetl plik

@ -66,8 +66,8 @@ object FcmFetchManager {
if (activeCount <= 0) {
Log.i(TAG, "No more active. Stopping.")
context.stopService(Intent(context, FcmFetchForegroundService::class.java))
context.stopService(Intent(context, FcmFetchBackgroundService::class.java))
context.startService(FcmFetchForegroundService.buildStopIntent(context))
}
}
}