Simplify reminder interval lookup

Fixes #7513
This commit is contained in:
Moxie Marlinspike 2018-03-13 09:39:56 -07:00
parent aff65abe91
commit 41d06fc8c2

View File

@ -14,23 +14,6 @@ public class RegistrationLockReminders {
public static final long INITIAL_INTERVAL = TimeUnit.HOURS.toMillis(6); public static final long INITIAL_INTERVAL = TimeUnit.HOURS.toMillis(6);
private static Map<Long, Long> INTERVAL_PROGRESSION = new HashMap<Long, Long>() {{
put(TimeUnit.HOURS.toMillis(6), TimeUnit.HOURS.toMillis(12));
put(TimeUnit.HOURS.toMillis(12), TimeUnit.DAYS.toMillis(1));
put(TimeUnit.DAYS.toMillis(1), TimeUnit.DAYS.toMillis(3));
put(TimeUnit.DAYS.toMillis(3), TimeUnit.DAYS.toMillis(7));
put(TimeUnit.DAYS.toMillis(7), TimeUnit.DAYS.toMillis(7));
}};
private static Map<Long, Long> INTERVAL_REGRESSION = new HashMap<Long, Long>() {{
put(TimeUnit.HOURS.toMillis(12), TimeUnit.HOURS.toMillis(6));
put(TimeUnit.DAYS.toMillis(1), TimeUnit.HOURS.toMillis(12));
put(TimeUnit.DAYS.toMillis(3), TimeUnit.DAYS.toMillis(1));
put(TimeUnit.DAYS.toMillis(7), TimeUnit.DAYS.toMillis(3));
}};
public static boolean needsReminder(@NonNull Context context) { public static boolean needsReminder(@NonNull Context context) {
if (!TextSecurePreferences.isRegistrationtLockEnabled(context)) return false; if (!TextSecurePreferences.isRegistrationtLockEnabled(context)) return false;
@ -45,11 +28,16 @@ public class RegistrationLockReminders {
long nextReminderInterval; long nextReminderInterval;
if (success) { if (success) {
if (INTERVAL_PROGRESSION.containsKey(lastReminderInterval)) nextReminderInterval = INTERVAL_PROGRESSION.get(lastReminderInterval); if (lastReminderInterval <= TimeUnit.HOURS.toMillis(6)) nextReminderInterval = TimeUnit.HOURS.toMillis(12);
else nextReminderInterval = INTERVAL_PROGRESSION.get(TimeUnit.HOURS.toMillis(6)); 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);
} else { } else {
if (INTERVAL_REGRESSION.containsKey(lastReminderInterval)) nextReminderInterval = INTERVAL_REGRESSION.get(lastReminderInterval); if (lastReminderInterval >= TimeUnit.DAYS.toMillis(7)) nextReminderInterval = TimeUnit.DAYS.toMillis(3);
else nextReminderInterval = INTERVAL_REGRESSION.get(TimeUnit.HOURS.toMillis(12)); 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);
} }
TextSecurePreferences.setRegistrationLockLastReminderTime(context, System.currentTimeMillis()); TextSecurePreferences.setRegistrationLockLastReminderTime(context, System.currentTimeMillis());