Use WorkManager for background poll task.

This commit is contained in:
Anton Chekulaev 2020-09-24 14:48:51 +10:00
parent 41041071f4
commit ab6ab5c625
13 changed files with 134 additions and 153 deletions

View File

@ -682,7 +682,7 @@
</receiver>
<!-- Session -->
<receiver
android:name="org.thoughtcrime.securesms.loki.api.BackgroundPollListener"
android:name="org.thoughtcrime.securesms.loki.api.BackgroundPollWorker$BootBroadcastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
@ -700,13 +700,6 @@
<receiver
android:name="org.thoughtcrime.securesms.jobmanager.AlarmManagerScheduler$RetryReceiver"
android:enabled="@bool/enable_alarm_manager" /> <!-- Probably don't need this one -->
<receiver
android:name="org.thoughtcrime.securesms.jobmanager.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-library
android:name="com.sec.android.app.multiwindow"

View File

@ -83,6 +83,7 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
implementation "androidx.work:work-runtime-ktx:2.4.0"
implementation ("com.google.firebase:firebase-messaging:18.0.0") {
exclude group: 'com.google.firebase', module: 'firebase-core'
@ -247,6 +248,10 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'LICENSE'

View File

@ -61,7 +61,7 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.logging.PersistentLogger;
import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger;
import org.thoughtcrime.securesms.loki.activities.HomeActivity;
import org.thoughtcrime.securesms.loki.api.BackgroundPollListener;
import org.thoughtcrime.securesms.loki.api.BackgroundPollWorker;
import org.thoughtcrime.securesms.loki.api.ClosedGroupPoller;
import org.thoughtcrime.securesms.loki.api.LokiPushNotificationManager;
import org.thoughtcrime.securesms.loki.api.PublicChatManager;
@ -385,7 +385,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
RotateSignedPreKeyListener.schedule(this);
LocalBackupListener.schedule(this);
RotateSenderCertificateListener.schedule(this);
BackgroundPollListener.schedule(this); // Loki
BackgroundPollWorker.schedulePeriodic(this); // Loki
if (BuildConfig.PLAY_STORE_DISABLED) {
UpdateApkRefreshListener.schedule(this);

View File

@ -1,17 +0,0 @@
package org.thoughtcrime.securesms.jobmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.thoughtcrime.securesms.logging.Log;
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = BootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Boot received. Application is created, kickstarting JobManager.");
}
}

View File

@ -23,6 +23,10 @@ import java.util.concurrent.TimeUnit;
* {@link #serialize()}. Your job is then recreated using a {@link Factory} that you register in
* {@link JobManager.Configuration.Builder#setJobFactories(Map)}, which is given the saved
* {@link Data} bundle.
*
* @deprecated
* use <a href="https://developer.android.com/reference/androidx/work/WorkManager">WorkManager</a>
* API instead.
*/
public abstract class Job {

View File

@ -44,7 +44,6 @@ import org.thoughtcrime.securesms.jobs.SmsSentJob;
import org.thoughtcrime.securesms.jobs.TrimThreadJob;
import org.thoughtcrime.securesms.jobs.TypingSendJob;
import org.thoughtcrime.securesms.jobs.UpdateApkJob;
import org.thoughtcrime.securesms.loki.api.BackgroundPollJob;
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob;
import org.thoughtcrime.securesms.loki.protocol.NullMessageSendJob;
import org.thoughtcrime.securesms.loki.protocol.SessionRequestMessageSendJob;
@ -59,7 +58,6 @@ public class WorkManagerFactoryMappings {
put(AttachmentDownloadJob.class.getName(), AttachmentDownloadJob.KEY);
put(AttachmentUploadJob.class.getName(), AttachmentUploadJob.KEY);
put(AvatarDownloadJob.class.getName(), AvatarDownloadJob.KEY);
put(BackgroundPollJob.class.getName(), BackgroundPollJob.KEY);
put(CleanPreKeysJob.class.getName(), CleanPreKeysJob.KEY);
put(ClosedGroupUpdateMessageSendJob.class.getName(), ClosedGroupUpdateMessageSendJob.KEY);
put(CreateSignedPreKeyJob.class.getName(), CreateSignedPreKeyJob.KEY);

View File

@ -6,6 +6,11 @@ import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.JobLogger;
import org.thoughtcrime.securesms.logging.Log;
/**
* @deprecated
* use <a href="https://developer.android.com/reference/androidx/work/WorkManager">WorkManager</a>
* API instead.
*/
public abstract class BaseJob extends Job {
private static final String TAG = BaseJob.class.getSimpleName();

View File

@ -14,7 +14,6 @@ import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraintObserver;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkOrCellServiceConstraint;
import org.thoughtcrime.securesms.jobmanager.impl.SqlCipherMigrationConstraint;
import org.thoughtcrime.securesms.jobmanager.impl.SqlCipherMigrationConstraintObserver;
import org.thoughtcrime.securesms.loki.api.BackgroundPollJob;
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob;
import org.thoughtcrime.securesms.loki.protocol.NullMessageSendJob;
import org.thoughtcrime.securesms.loki.protocol.SessionRequestMessageSendJob;
@ -32,7 +31,6 @@ public final class JobManagerFactories {
put(AttachmentDownloadJob.KEY, new AttachmentDownloadJob.Factory());
put(AttachmentUploadJob.KEY, new AttachmentUploadJob.Factory());
put(AvatarDownloadJob.KEY, new AvatarDownloadJob.Factory());
put(BackgroundPollJob.KEY, new BackgroundPollJob.Factory());
put(CleanPreKeysJob.KEY, new CleanPreKeysJob.Factory());
put(ClosedGroupUpdateMessageSendJob.KEY, new ClosedGroupUpdateMessageSendJob.Factory());
put(CreateSignedPreKeyJob.KEY, new CreateSignedPreKeyJob.Factory());

View File

@ -1,84 +0,0 @@
package org.thoughtcrime.securesms.loki.api
import android.content.Context
import kotlinx.coroutines.awaitAll
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.all
import nl.komponents.kovenant.functional.map
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.dependencies.InjectableType
import org.thoughtcrime.securesms.jobmanager.Data
import org.thoughtcrime.securesms.jobmanager.Job
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
import org.thoughtcrime.securesms.jobs.BaseJob
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
import org.thoughtcrime.securesms.jobs.RotateCertificateJob
import org.thoughtcrime.securesms.logging.Log
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.SignalServiceAccountManager
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException
import org.whispersystems.signalservice.loki.api.SnodeAPI
import java.io.IOException
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class BackgroundPollJob private constructor(parameters: Parameters) : BaseJob(parameters) {
companion object {
const val KEY = "BackgroundPollJob"
}
constructor(context: Context) : this(Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setQueue(KEY)
.setLifespan(TimeUnit.DAYS.toMillis(1))
.setMaxAttempts(Parameters.UNLIMITED)
.build()) {
setContext(context)
}
override fun serialize(): Data {
return Data.EMPTY
}
override fun getFactoryKey(): String { return KEY }
public override fun onRun() {
try {
Log.d("Loki", "Performing background poll.")
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
val promises = mutableListOf<Promise<Unit, Exception>>()
val promise = SnodeAPI.shared.getMessages(userPublicKey).map { envelopes ->
envelopes.forEach {
PushContentReceiveJob(context).processEnvelope(SignalServiceEnvelope(it), false)
}
}
promises.add(promise)
promises.addAll(ClosedGroupPoller.shared.pollOnce())
val openGroups = DatabaseFactory.getLokiThreadDatabase(context).getAllPublicChats().map { it.value }
for (openGroup in openGroups) {
val poller = PublicChatPoller(context, openGroup)
poller.stop()
promises.add(poller.pollForNewMessages())
}
all(promises).get()
} catch (exception: Exception) {
Log.d("Loki", "Background poll failed due to error: $exception.")
}
}
public override fun onShouldRetry(e: Exception): Boolean {
return false
}
override fun onCanceled() { }
class Factory : Job.Factory<BackgroundPollJob> {
override fun create(parameters: Parameters, data: Data): BackgroundPollJob {
return BackgroundPollJob(parameters)
}
}
}

View File

@ -1,36 +0,0 @@
package org.thoughtcrime.securesms.loki.api
import android.content.Context
import android.content.Intent
import nl.komponents.kovenant.functional.map
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
import org.whispersystems.signalservice.loki.api.SnodeAPI
import java.util.concurrent.TimeUnit
class BackgroundPollListener : PersistentAlarmManagerListener() {
companion object {
private val pollInterval = TimeUnit.MINUTES.toMillis(15)
@JvmStatic
fun schedule(context: Context) {
BackgroundPollListener().onReceive(context, Intent())
}
}
override fun getNextScheduledExecutionTime(context: Context): Long {
return TextSecurePreferences.getBackgroundPollTime(context)
}
override fun onAlarm(context: Context, scheduledTime: Long): Long {
ApplicationContext.getInstance(context).jobManager.add(BackgroundPollJob(context))
val nextTime = System.currentTimeMillis() + pollInterval
TextSecurePreferences.setBackgroundPollTime(context, nextTime)
return nextTime
}
}

View File

@ -0,0 +1,113 @@
package org.thoughtcrime.securesms.loki.api
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.work.*
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.all
import nl.komponents.kovenant.functional.map
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
import org.thoughtcrime.securesms.logging.Log
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
import org.whispersystems.signalservice.loki.api.SnodeAPI
import java.util.concurrent.TimeUnit
class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
companion object {
const val TAG = "BackgroundPollWorker"
private const val RETRY_ATTEMPTS = 3
@JvmStatic
fun scheduleInstant(context: Context) {
val workRequest = OneTimeWorkRequestBuilder<BackgroundPollWorker>()
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
WorkManager
.getInstance(context)
.enqueue(workRequest)
}
@JvmStatic
fun schedulePeriodic(context: Context) {
Log.v(TAG, "Scheduling periodic work.")
val workRequest = PeriodicWorkRequestBuilder<BackgroundPollWorker>(15, TimeUnit.MINUTES)
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
WorkManager
.getInstance(context)
.enqueueUniquePeriodicWork(
TAG,
ExistingPeriodicWorkPolicy.KEEP,
workRequest
)
}
}
override fun doWork(): Result {
if (TextSecurePreferences.getLocalNumber(context) == null) {
Log.v(TAG, "Background poll is canceled due to the Session user is not set up yet.")
return Result.failure()
}
try {
Log.v(TAG, "Performing background poll.")
val promises = mutableListOf<Promise<Unit, Exception>>()
// Private chats
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
val privateChatsPromise = SnodeAPI.shared.getMessages(userPublicKey).map { envelopes ->
envelopes.forEach {
PushContentReceiveJob(context).processEnvelope(SignalServiceEnvelope(it), false)
}
}
promises.add(privateChatsPromise)
// Closed groups
val sskDatabase = DatabaseFactory.getSSKDatabase(context)
ClosedGroupPoller.configureIfNeeded(context, sskDatabase)
promises.addAll(ClosedGroupPoller.shared.pollOnce())
// Open Groups
val openGroups = DatabaseFactory.getLokiThreadDatabase(context).getAllPublicChats().map { it.value }
for (openGroup in openGroups) {
val poller = PublicChatPoller(context, openGroup)
promises.add(poller.pollForNewMessages())
}
// Wait till all the promises get resolved
all(promises).get()
return Result.success()
} catch (exception: Exception) {
Log.v(TAG, "Background poll failed due to error: ${exception.message}.", exception)
return if (runAttemptCount < RETRY_ATTEMPTS) Result.retry() else Result.failure()
}
}
class BootBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
Log.v(TAG, "Boot broadcast caught.")
BackgroundPollWorker.scheduleInstant(context)
BackgroundPollWorker.schedulePeriodic(context)
}
}
}
}

View File

@ -17,7 +17,7 @@ import org.whispersystems.signalservice.loki.utilities.getRandomElementOrNull
class ClosedGroupPoller private constructor(private val context: Context, private val database: SharedSenderKeysDatabase) {
private var isPolling = false
private val handler = Handler()
private val handler: Handler by lazy { Handler() }
private val task = object : Runnable {

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.loki.api
import android.content.Context
import android.os.Handler
import android.util.Log
import androidx.annotation.WorkerThread
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.functional.bind
import nl.komponents.kovenant.functional.map
@ -30,9 +31,10 @@ import org.whispersystems.signalservice.loki.api.opengroups.PublicChatMessage
import org.whispersystems.signalservice.loki.protocol.shelved.multidevice.MultiDeviceProtocol
import java.security.MessageDigest
import java.util.*
import java.util.concurrent.CompletableFuture
class PublicChatPoller(private val context: Context, private val group: PublicChat) {
private val handler = Handler()
private val handler by lazy { Handler() }
private var hasStarted = false
private var isPollOngoing = false
public var isCaughtUp = false