mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-30 13:41:47 +01:00
Make FcmFetchForegroundService stop itself.
This commit is contained in:
committed by
Alex Hart
parent
eeb4cdf064
commit
c0db88960c
@@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.gcm
|
|||||||
|
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
@@ -16,7 +17,22 @@ import org.thoughtcrime.securesms.notifications.NotificationIds
|
|||||||
*/
|
*/
|
||||||
class FcmFetchForegroundService : Service() {
|
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 {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
startForeground(
|
startForeground(
|
||||||
@@ -31,7 +47,13 @@ class FcmFetchForegroundService : Service() {
|
|||||||
.build()
|
.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() {
|
override fun onDestroy() {
|
||||||
|
|||||||
@@ -66,8 +66,8 @@ object FcmFetchManager {
|
|||||||
|
|
||||||
if (activeCount <= 0) {
|
if (activeCount <= 0) {
|
||||||
Log.i(TAG, "No more active. Stopping.")
|
Log.i(TAG, "No more active. Stopping.")
|
||||||
context.stopService(Intent(context, FcmFetchForegroundService::class.java))
|
|
||||||
context.stopService(Intent(context, FcmFetchBackgroundService::class.java))
|
context.stopService(Intent(context, FcmFetchBackgroundService::class.java))
|
||||||
|
context.startService(FcmFetchForegroundService.buildStopIntent(context))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user