kopia lustrzana https://github.com/ryukoposting/Signal-Android
Make FcmFetchForegroundService stop itself.
rodzic
eeb4cdf064
commit
c0db88960c
|
@ -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() {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue