mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-29 06:19:20 +00:00
Throttle background contact syncs to once every 6 hours.
Unfortunately, there's apps out there that trigger contact changes very frequently. Because we listen to the system for contact changes to tell us when to sync, that could result in us sending an abundance of contact syncs to linked desktop instances. This throttles contact sync requests using the following methodology: - By default, throttle contact syncs to 6 hrs while the app is backgrounded. - If a sync is throttled in the background, we set a dirty flag and will execute the sync the next time the app is foregrounded. - Syncs explicitly requested by desktop are never throttled.
This commit is contained in:
@@ -17,9 +17,13 @@
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.arch.lifecycle.DefaultLifecycleObserver;
|
||||
import android.arch.lifecycle.LifecycleOwner;
|
||||
import android.arch.lifecycle.ProcessLifecycleOwner;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.multidex.MultiDexApplication;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -35,6 +39,7 @@ import org.thoughtcrime.securesms.jobmanager.persistence.JavaJobSerializer;
|
||||
import org.thoughtcrime.securesms.jobmanager.requirements.NetworkRequirementProvider;
|
||||
import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob;
|
||||
import org.thoughtcrime.securesms.jobs.GcmRefreshJob;
|
||||
import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob;
|
||||
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirementProvider;
|
||||
import org.thoughtcrime.securesms.jobs.requirements.ServiceRequirementProvider;
|
||||
import org.thoughtcrime.securesms.jobs.requirements.SqlCipherMigrationRequirementProvider;
|
||||
@@ -66,7 +71,7 @@ import dagger.ObjectGraph;
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*/
|
||||
public class ApplicationContext extends MultiDexApplication implements DependencyInjector {
|
||||
public class ApplicationContext extends MultiDexApplication implements DependencyInjector, DefaultLifecycleObserver {
|
||||
|
||||
private static final String TAG = ApplicationContext.class.getName();
|
||||
|
||||
@@ -74,6 +79,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
private JobManager jobManager;
|
||||
private ObjectGraph objectGraph;
|
||||
|
||||
private volatile boolean isAppVisible;
|
||||
|
||||
public static ApplicationContext getInstance(Context context) {
|
||||
return (ApplicationContext)context.getApplicationContext();
|
||||
}
|
||||
@@ -91,6 +98,21 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
initializePeriodicTasks();
|
||||
initializeCircumvention();
|
||||
initializeWebRtc();
|
||||
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(@NonNull LifecycleOwner owner) {
|
||||
isAppVisible = true;
|
||||
Log.i(TAG, "App is now visible.");
|
||||
|
||||
executePendingContactSync();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(@NonNull LifecycleOwner owner) {
|
||||
isAppVisible = false;
|
||||
Log.i(TAG, "App is no longer visible.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -108,6 +130,10 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
return expiringMessageManager;
|
||||
}
|
||||
|
||||
public boolean isAppVisible() {
|
||||
return isAppVisible;
|
||||
}
|
||||
|
||||
private void initializeRandomNumberFix() {
|
||||
PRNGFixes.apply();
|
||||
}
|
||||
@@ -216,4 +242,10 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
|
||||
private void executePendingContactSync() {
|
||||
if (TextSecurePreferences.needsFullContactSync(this)) {
|
||||
ApplicationContext.getInstance(this).getJobManager().add(new MultiDeviceContactUpdateJob(this, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user