Make the foreground delay configurable. Remove short initial delay.

fork-5.53.8
Alan Evans 2021-03-11 14:54:39 -04:00 zatwierdzone przez Greyson Parrelli
rodzic 75aab4c031
commit a64430c65f
2 zmienionych plików z 29 dodań i 13 usunięć

Wyświetl plik

@ -20,7 +20,7 @@ import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.whispersystems.libsignal.util.guava.Optional;
import java.util.concurrent.TimeUnit;
import java.util.Locale;
/**
* On received message, runs a job to poll for messages.
@ -29,10 +29,6 @@ public final class MessageProcessReceiver extends BroadcastReceiver {
private static final String TAG = Log.tag(MessageProcessReceiver.class);
private static final long FIRST_RUN_DELAY = TimeUnit.MINUTES.toMillis(3);
private static final long FOREGROUND_DELAY = 300;
private static final long JOB_TIMEOUT = FOREGROUND_DELAY + 200;
public static final String BROADCAST_ACTION = "org.thoughtcrime.securesms.action.PROCESS_MESSAGES";
@Override
@ -44,15 +40,26 @@ public final class MessageProcessReceiver extends BroadcastReceiver {
Log.i(TAG, "Starting Alarm because of boot receiver");
startOrUpdateAlarm(context);
} else if (BROADCAST_ACTION.equals(intent.getAction())) {
if (ApplicationDependencies.getAppForegroundObserver().isForegrounded()) {
Log.i(TAG, "App is foregrounded");
return;
}
long foregroundDelayMs = FeatureFlags.getBackgroundMessageProcessForegroundDelay();
long jobTimeout = foregroundDelayMs + 200;
Log.i(TAG, String.format(Locale.US, "Starting PushNotificationReceiveJob asynchronously with %d delay before foreground shown", foregroundDelayMs));
PendingResult pendingResult = goAsync();
new Handler(Looper.getMainLooper()).postDelayed(pendingResult::finish, JOB_TIMEOUT);
new Handler(Looper.getMainLooper()).postDelayed(pendingResult::finish, jobTimeout);
SignalExecutors.BOUNDED.submit(() -> {
Log.i(TAG, "Running PushNotificationReceiveJob");
Optional<JobTracker.JobState> jobState = ApplicationDependencies.getJobManager()
.runSynchronously(PushNotificationReceiveJob.withDelayedForegroundService(FOREGROUND_DELAY), JOB_TIMEOUT);
.runSynchronously(PushNotificationReceiveJob.withDelayedForegroundService(foregroundDelayMs), jobTimeout);
Log.i(TAG, "PushNotificationReceiveJob ended: " + (jobState.isPresent() ? jobState.get().toString() : "Job did not complete"));
});
@ -67,14 +74,14 @@ public final class MessageProcessReceiver extends BroadcastReceiver {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long interval = FeatureFlags.getBackgroundMessageProcessDelay();
long interval = FeatureFlags.getBackgroundMessageProcessInterval();
if (interval < 0) {
alarmManager.cancel(pendingIntent);
Log.i(TAG, "Alarm cancelled");
} else {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + FIRST_RUN_DELAY,
SystemClock.elapsedRealtime() + interval,
interval,
pendingIntent);
Log.i(TAG, "Alarm scheduled to repeat at interval " + interval);

Wyświetl plik

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.util;
import android.os.Build;
import android.text.TextUtils;
import android.util.TimeUtils;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
@ -75,6 +74,7 @@ public final class FeatureFlags {
private static final String ANIMATED_STICKER_MIN_MEMORY = "android.animatedStickerMinMemory";
private static final String ANIMATED_STICKER_MIN_TOTAL_MEMORY = "android.animatedStickerMinTotalMemory";
private static final String MESSAGE_PROCESSOR_ALARM_INTERVAL = "android.messageProcessor.alarmIntervalMins";
private static final String MESSAGE_PROCESSOR_DELAY = "android.messageProcessor.foregroundDelayMs";
/**
* We will only store remote values for flags in this set. If you want a flag to be controllable
@ -104,7 +104,8 @@ public final class FeatureFlags {
SHARE_SELECTION_LIMIT,
ANIMATED_STICKER_MIN_MEMORY,
ANIMATED_STICKER_MIN_TOTAL_MEMORY,
MESSAGE_PROCESSOR_ALARM_INTERVAL
MESSAGE_PROCESSOR_ALARM_INTERVAL,
MESSAGE_PROCESSOR_DELAY
);
@VisibleForTesting
@ -146,7 +147,8 @@ public final class FeatureFlags {
SHARE_SELECTION_LIMIT,
ANIMATED_STICKER_MIN_MEMORY,
ANIMATED_STICKER_MIN_TOTAL_MEMORY,
MESSAGE_PROCESSOR_ALARM_INTERVAL
MESSAGE_PROCESSOR_ALARM_INTERVAL,
MESSAGE_PROCESSOR_DELAY
);
/**
@ -463,11 +465,18 @@ public final class FeatureFlags {
}
}
public static long getBackgroundMessageProcessDelay() {
public static long getBackgroundMessageProcessInterval() {
int delayMinutes = getInteger(MESSAGE_PROCESSOR_ALARM_INTERVAL, (int) TimeUnit.HOURS.toMinutes(6));
return TimeUnit.MINUTES.toMillis(delayMinutes);
}
/**
* How long before a "Checking messages" foreground notification is shown to the user.
*/
public static long getBackgroundMessageProcessForegroundDelay() {
return getInteger(MESSAGE_PROCESSOR_DELAY, 300);
}
private enum VersionFlag {
/** The flag is no set */
OFF,