From 9c0129c4b0818bfe3e18a8901a27efe4a3b76776 Mon Sep 17 00:00:00 2001 From: FeuRenard Date: Tue, 13 Mar 2018 18:45:55 +0100 Subject: [PATCH] Allow skipping registration PIN intervals on success Closes #7517 Closes #7518 --- .../lock/RegistrationLockReminders.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/org/thoughtcrime/securesms/lock/RegistrationLockReminders.java b/src/org/thoughtcrime/securesms/lock/RegistrationLockReminders.java index 36f55f744..8df9fa0d6 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());