forked from signalapp/Signal-Android
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor FCM processing to improve use of foreground services.
- Loading branch information
1 parent
06a49b5
commit 9afeb20
Showing
7 changed files
with
177 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchBackgroundService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.thoughtcrime.securesms.gcm; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.IBinder; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import org.signal.core.util.logging.Log; | ||
|
||
/** | ||
* Works with {@link FcmFetchManager} to exists as a service that will keep the app process running in the background while we fetch messages. | ||
*/ | ||
public class FcmFetchBackgroundService extends Service { | ||
|
||
private static final String TAG = Log.tag(FcmFetchBackgroundService.class); | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
return START_STICKY; | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
Log.i(TAG, "onDestroy()"); | ||
} | ||
|
||
@Override | ||
public @Nullable IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
} | ||
|
44 changes: 44 additions & 0 deletions
44
app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchForegroundService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.thoughtcrime.securesms.gcm | ||
|
||
import android.app.PendingIntent | ||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.IBinder | ||
import androidx.core.app.NotificationCompat | ||
import org.signal.core.util.logging.Log | ||
import org.thoughtcrime.securesms.MainActivity | ||
import org.thoughtcrime.securesms.R | ||
import org.thoughtcrime.securesms.notifications.NotificationChannels | ||
import org.thoughtcrime.securesms.notifications.NotificationIds | ||
|
||
/** | ||
* Works with {@link FcmFetchManager} to exists as a service that will keep the app process running in the foreground while we fetch messages. | ||
*/ | ||
class FcmFetchForegroundService : Service() { | ||
|
||
private val TAG = Log.tag(FcmFetchForegroundService::class.java) | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
startForeground( | ||
NotificationIds.FCM_FETCH, | ||
NotificationCompat.Builder(this, NotificationChannels.OTHER) | ||
.setSmallIcon(R.drawable.ic_notification) | ||
.setContentTitle(getString(R.string.BackgroundMessageRetriever_checking_for_messages)) | ||
.setCategory(NotificationCompat.CATEGORY_SERVICE) | ||
.setProgress(0, 0, true) | ||
.setContentIntent(PendingIntent.getActivity(this, 0, MainActivity.clearTop(this), 0)) | ||
.setVibrate(longArrayOf(0)) | ||
.build() | ||
) | ||
|
||
return START_STICKY | ||
} | ||
|
||
override fun onDestroy() { | ||
Log.i(TAG, "onDestroy()") | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder? { | ||
return null | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.thoughtcrime.securesms.gcm | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import androidx.core.content.ContextCompat | ||
import org.signal.core.util.concurrent.SignalExecutors | ||
import org.signal.core.util.logging.Log | ||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies | ||
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob | ||
import org.thoughtcrime.securesms.messages.RestStrategy | ||
import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor | ||
|
||
/** | ||
* Our goals with FCM processing are as follows: | ||
* (1) Ensure some service is active for the duration of the fetch and processing stages. | ||
* (2) Do not make unnecessary network requests. | ||
* | ||
* To fulfill goal 1, this class will not stop the services until there is no more running | ||
* requests. | ||
* | ||
* To fulfill goal 2, this class will not enqueue a fetch if there are already 2 active fetches | ||
* (or rather, 1 active and 1 waiting, since we use a single thread executor). | ||
* | ||
* Unfortunately we can't do this all in [FcmReceiveService] because it won't let us process | ||
* the next FCM message until [FcmReceiveService.onMessageReceived] returns, | ||
* but as soon as that method returns, it could also destroy the service. By not letting us control | ||
* when the service is destroyed, we can't accomplish both goals within that service. | ||
*/ | ||
object FcmFetchManager { | ||
|
||
private val TAG = Log.tag(FcmFetchManager::class.java) | ||
|
||
private val EXECUTOR = SerialMonoLifoExecutor(SignalExecutors.UNBOUNDED) | ||
|
||
@Volatile | ||
private var activeCount = 0 | ||
|
||
@JvmStatic | ||
fun enqueue(context: Context, foreground: Boolean) { | ||
synchronized(this) { | ||
if (foreground) { | ||
Log.i(TAG, "Starting in the foreground.") | ||
ContextCompat.startForegroundService(context, Intent(context, FcmFetchForegroundService::class.java)) | ||
} else { | ||
Log.i(TAG, "Starting in the background.") | ||
context.startService(Intent(context, FcmFetchBackgroundService::class.java)) | ||
} | ||
|
||
val performedReplace = EXECUTOR.enqueue { fetch(context) } | ||
|
||
if (performedReplace) { | ||
Log.i(TAG, "Already have one running and one enqueued. Ignoring.") | ||
} else { | ||
activeCount++ | ||
Log.i(TAG, "Incrementing active count to $activeCount") | ||
} | ||
} | ||
} | ||
|
||
private fun fetch(context: Context) { | ||
retrieveMessages(context) | ||
|
||
synchronized(this) { | ||
activeCount-- | ||
|
||
if (activeCount <= 0) { | ||
Log.i(TAG, "No more active. Stopping.") | ||
context.stopService(Intent(context, FcmFetchForegroundService::class.java)) | ||
context.stopService(Intent(context, FcmFetchBackgroundService::class.java)) | ||
} | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun retrieveMessages(context: Context) { | ||
val success = ApplicationDependencies.getBackgroundMessageRetriever().retrieveMessages(context, RestStrategy(), RestStrategy()) | ||
|
||
if (success) { | ||
Log.i(TAG, "Successfully retrieved messages.") | ||
} else { | ||
if (Build.VERSION.SDK_INT >= 26) { | ||
Log.w(TAG, "[API ${Build.VERSION.SDK_INT}] Failed to retrieve messages. Scheduling on the system JobScheduler (API " + Build.VERSION.SDK_INT + ").") | ||
FcmJobService.schedule(context) | ||
} else { | ||
Log.w(TAG, "[API ${Build.VERSION.SDK_INT}] Failed to retrieve messages. Scheduling on JobManager (API " + Build.VERSION.SDK_INT + ").") | ||
ApplicationDependencies.getJobManager().add(PushNotificationReceiveJob()) | ||
} | ||
} | ||
} | ||
} |
142 changes: 0 additions & 142 deletions
142
app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters