diff --git a/src/org/thoughtcrime/securesms/lock/RegistrationLockReminders.java b/src/org/thoughtcrime/securesms/lock/RegistrationLockReminders.java index 36f55f7443..8df9fa0d6f 100644 --- a/src/org/thoughtcrime/securesms/lock/RegistrationLockReminders.java +++ b/src/org/thoughtcrime/securesms/lock/RegistrationLockReminders.java @@ -6,14 +6,22 @@ import android.support.annotation.NonNull; import org.thoughtcrime.securesms.util.TextSecurePreferences; -import java.util.HashMap; -import java.util.Map; +import java.util.NavigableSet; +import java.util.TreeSet; import java.util.concurrent.TimeUnit; public class RegistrationLockReminders { - public static final long INITIAL_INTERVAL = TimeUnit.HOURS.toMillis(6); - + private static final NavigableSet INTERVALS = new TreeSet() {{ + add(TimeUnit.HOURS.toMillis(6)); + add(TimeUnit.HOURS.toMillis(12)); + add(TimeUnit.DAYS.toMillis(1)); + add(TimeUnit.DAYS.toMillis(3)); + add(TimeUnit.DAYS.toMillis(7)); + }}; + + public static final long INITIAL_INTERVAL = INTERVALS.first(); + public static boolean needsReminder(@NonNull Context context) { if (!TextSecurePreferences.isRegistrationtLockEnabled(context)) return false; @@ -24,20 +32,16 @@ public class RegistrationLockReminders { } public static void scheduleReminder(@NonNull Context context, boolean success) { - long lastReminderInterval = TextSecurePreferences.getRegistrationLockNextReminderInterval(context); - long nextReminderInterval; + Long nextReminderInterval; if (success) { - if (lastReminderInterval <= TimeUnit.HOURS.toMillis(6)) nextReminderInterval = TimeUnit.HOURS.toMillis(12); - else if (lastReminderInterval <= TimeUnit.HOURS.toMillis(12)) nextReminderInterval = TimeUnit.DAYS.toMillis(1); - else if (lastReminderInterval <= TimeUnit.DAYS.toMillis(1)) nextReminderInterval = TimeUnit.DAYS.toMillis(3); - else if (lastReminderInterval <= TimeUnit.DAYS.toMillis(3)) nextReminderInterval = TimeUnit.DAYS.toMillis(7); - else nextReminderInterval = TimeUnit.DAYS.toMillis(7); + long timeSinceLastReminder = System.currentTimeMillis() - TextSecurePreferences.getRegistrationLockLastReminderTime(context); + nextReminderInterval = INTERVALS.higher(timeSinceLastReminder); + if (nextReminderInterval == null) nextReminderInterval = INTERVALS.last(); } else { - if (lastReminderInterval >= TimeUnit.DAYS.toMillis(7)) nextReminderInterval = TimeUnit.DAYS.toMillis(3); - else if (lastReminderInterval >= TimeUnit.DAYS.toMillis(3)) nextReminderInterval = TimeUnit.DAYS.toMillis(1); - else if (lastReminderInterval >= TimeUnit.DAYS.toMillis(1)) nextReminderInterval = TimeUnit.HOURS.toMillis(12); - else nextReminderInterval = TimeUnit.HOURS.toMillis(6); + long lastReminderInterval = TextSecurePreferences.getRegistrationLockNextReminderInterval(context); + nextReminderInterval = INTERVALS.lower(lastReminderInterval); + if (nextReminderInterval == null) nextReminderInterval = INTERVALS.first(); } TextSecurePreferences.setRegistrationLockLastReminderTime(context, System.currentTimeMillis());