mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-21 15:05:19 +00:00
Merge pull request #354 from metaphore/background-polling
Use WorkManager API for Background Polling
This commit is contained in:
commit
9d8514d7c9
@ -683,7 +683,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" />
|
||||
@ -701,13 +701,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"
|
||||
|
@ -89,6 +89,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'
|
||||
@ -256,6 +257,10 @@ android {
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
|
||||
packagingOptions {
|
||||
exclude 'LICENSE.txt'
|
||||
exclude 'LICENSE'
|
||||
|
@ -62,7 +62,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;
|
||||
@ -389,7 +389,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);
|
||||
|
@ -94,8 +94,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
private static final int lokiV15 = 36;
|
||||
private static final int lokiV16 = 37;
|
||||
private static final int lokiV17 = 38;
|
||||
private static final int lokiV18_CLEAR_BG_POLL_JOBS = 39;
|
||||
|
||||
private static final int DATABASE_VERSION = lokiV17;
|
||||
private static final int DATABASE_VERSION = lokiV18_CLEAR_BG_POLL_JOBS; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes
|
||||
private static final String DATABASE_NAME = "signal.db";
|
||||
|
||||
private final Context context;
|
||||
@ -634,7 +635,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
if (oldVersion < lokiV15) {
|
||||
db.execSQL(SharedSenderKeysDatabase.getCreateOldClosedGroupRatchetTableCommand());
|
||||
}
|
||||
|
||||
|
||||
if (oldVersion < lokiV16) {
|
||||
db.execSQL(LokiAPIDatabase.getCreateOpenGroupProfilePictureTableCommand());
|
||||
}
|
||||
@ -644,6 +645,12 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
db.execSQL("ALTER TABLE part ADD COLUMN audio_duration INTEGER");
|
||||
}
|
||||
|
||||
if (oldVersion < lokiV18_CLEAR_BG_POLL_JOBS) {
|
||||
// BackgroundPollJob was replaced with BackgroundPollWorker. Clear all the scheduled job records.
|
||||
db.execSQL("DELETE FROM job_spec WHERE factory_key = 'BackgroundPollJob'");
|
||||
db.execSQL("DELETE FROM constraint_spec WHERE factory_key = 'BackgroundPollJob'");
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
|
@ -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.");
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
||||
|
@ -7,7 +7,6 @@ import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.DefaultExecutorFactory;
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer;
|
||||
import org.thoughtcrime.securesms.jobmanager.migration.WorkManagerMigrator;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobStorage;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.util.Debouncer;
|
||||
@ -52,11 +51,6 @@ public class JobManager implements ConstraintObserver.Notifier {
|
||||
this::onEmptyQueue);
|
||||
|
||||
executor.execute(() -> {
|
||||
if (WorkManagerMigrator.needsMigration(application)) {
|
||||
Log.i(TAG, "Detected an old WorkManager database. Migrating.");
|
||||
WorkManagerMigrator.migrate(application, configuration.getJobStorage(), configuration.getDataSerializer());
|
||||
}
|
||||
|
||||
jobController.init();
|
||||
|
||||
for (int i = 0; i < jobRunners.length; i++) {
|
||||
|
@ -1,101 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.migration;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.ConstraintSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.FullSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
final class WorkManagerDatabase extends SQLiteOpenHelper {
|
||||
|
||||
private static final String TAG = WorkManagerDatabase.class.getSimpleName();
|
||||
|
||||
static final String DB_NAME = "androidx.work.workdb";
|
||||
|
||||
WorkManagerDatabase(@NonNull Context context) {
|
||||
super(context, DB_NAME, null, 5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
throw new UnsupportedOperationException("We should never be creating this database, only migrating an existing one!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// There's a chance that a user who hasn't upgraded in > 6 months could hit this onUpgrade path,
|
||||
// but we don't use any of the columns that were added in any migrations they could hit, so we
|
||||
// can ignore this.
|
||||
Log.w(TAG, "Hit onUpgrade path from " + oldVersion + " to " + newVersion);
|
||||
}
|
||||
|
||||
@NonNull List<FullSpec> getAllJobs(@NonNull Data.Serializer dataSerializer) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String[] columns = new String[] { "id", "worker_class_name", "input", "required_network_type"};
|
||||
List<FullSpec> fullSpecs = new LinkedList<>();
|
||||
|
||||
try (Cursor cursor = db.query("WorkSpec", columns, null, null, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String factoryName = WorkManagerFactoryMappings.getFactoryKey(cursor.getString(cursor.getColumnIndexOrThrow("worker_class_name")));
|
||||
|
||||
if (factoryName != null) {
|
||||
String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
|
||||
byte[] data = cursor.getBlob(cursor.getColumnIndexOrThrow("input"));
|
||||
|
||||
List<ConstraintSpec> constraints = new LinkedList<>();
|
||||
JobSpec jobSpec = new JobSpec(id,
|
||||
factoryName,
|
||||
getQueueKey(id),
|
||||
System.currentTimeMillis(),
|
||||
0,
|
||||
0,
|
||||
Job.Parameters.UNLIMITED,
|
||||
TimeUnit.SECONDS.toMillis(30),
|
||||
TimeUnit.DAYS.toMillis(1),
|
||||
Job.Parameters.UNLIMITED,
|
||||
dataSerializer.serialize(DataMigrator.convert(data)),
|
||||
false);
|
||||
|
||||
|
||||
|
||||
if (cursor.getInt(cursor.getColumnIndexOrThrow("required_network_type")) != 0) {
|
||||
constraints.add(new ConstraintSpec(id, NetworkConstraint.KEY));
|
||||
}
|
||||
|
||||
fullSpecs.add(new FullSpec(jobSpec, constraints, Collections.emptyList()));
|
||||
} else {
|
||||
Log.w(TAG, "Failed to find a matching factory for worker class: " + factoryName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fullSpecs;
|
||||
}
|
||||
|
||||
private @Nullable String getQueueKey(@NonNull String jobId) {
|
||||
String query = "work_spec_id = ?";
|
||||
String[] args = new String[] { jobId };
|
||||
|
||||
try (Cursor cursor = getReadableDatabase().query("WorkName", null, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow("name"));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -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.api.PrepareAttachmentAudioExtrasJob;
|
||||
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob;
|
||||
import org.thoughtcrime.securesms.loki.protocol.NullMessageSendJob;
|
||||
@ -60,7 +59,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);
|
||||
|
@ -1,45 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.migration;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.FullSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobStorage;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class WorkManagerMigrator {
|
||||
|
||||
private static final String TAG = Log.tag(WorkManagerMigrator.class);
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
@WorkerThread
|
||||
public static synchronized void migrate(@NonNull Context context,
|
||||
@NonNull JobStorage jobStorage,
|
||||
@NonNull Data.Serializer dataSerializer)
|
||||
{
|
||||
long startTime = System.currentTimeMillis();
|
||||
Log.i(TAG, "Beginning WorkManager migration.");
|
||||
|
||||
WorkManagerDatabase database = new WorkManagerDatabase(context);
|
||||
List<FullSpec> fullSpecs = database.getAllJobs(dataSerializer);
|
||||
|
||||
for (FullSpec fullSpec : fullSpecs) {
|
||||
Log.i(TAG, String.format("Migrating job with key '%s' and %d constraint(s).", fullSpec.getJobSpec().getFactoryKey(), fullSpec.getConstraintSpecs().size()));
|
||||
}
|
||||
|
||||
jobStorage.insertJobs(fullSpecs);
|
||||
|
||||
context.deleteDatabase(WorkManagerDatabase.DB_NAME);
|
||||
Log.i(TAG, String.format("WorkManager migration finished. Migrated %d job(s) in %d ms.", fullSpecs.size(), System.currentTimeMillis() - startTime));
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
public static synchronized boolean needsMigration(@NonNull Context context) {
|
||||
return context.getDatabasePath(WorkManagerDatabase.DB_NAME).exists();
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
@ -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.api.PrepareAttachmentAudioExtrasJob;
|
||||
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob;
|
||||
import org.thoughtcrime.securesms.loki.protocol.NullMessageSendJob;
|
||||
@ -33,7 +32,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());
|
||||
|
@ -1,87 +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>>()
|
||||
if (!TextSecurePreferences.isUsingFCM(context)) {
|
||||
Log.d("Loki", "Not using FCM; polling for contacts and closed groups.")
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
113
src/org/thoughtcrime/securesms/loki/api/BackgroundPollWorker.kt
Normal file
113
src/org/thoughtcrime/securesms/loki/api/BackgroundPollWorker.kt
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user