2018-03-01 17:51:37 +00:00
|
|
|
package org.thoughtcrime.securesms.lock;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
|
2018-03-13 17:45:55 +00:00
|
|
|
import java.util.NavigableSet;
|
|
|
|
import java.util.TreeSet;
|
2018-03-01 17:51:37 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
public class RegistrationLockReminders {
|
|
|
|
|
2018-03-13 17:45:55 +00:00
|
|
|
private static final NavigableSet<Long> INTERVALS = new TreeSet<Long>() {{
|
|
|
|
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();
|
|
|
|
|
2018-03-01 17:51:37 +00:00
|
|
|
public static boolean needsReminder(@NonNull Context context) {
|
|
|
|
if (!TextSecurePreferences.isRegistrationtLockEnabled(context)) return false;
|
|
|
|
|
|
|
|
long lastReminderTime = TextSecurePreferences.getRegistrationLockLastReminderTime(context);
|
|
|
|
long nextIntervalTime = TextSecurePreferences.getRegistrationLockNextReminderInterval(context);
|
|
|
|
|
|
|
|
return System.currentTimeMillis() > lastReminderTime + nextIntervalTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void scheduleReminder(@NonNull Context context, boolean success) {
|
2018-03-13 17:45:55 +00:00
|
|
|
Long nextReminderInterval;
|
2018-03-01 17:51:37 +00:00
|
|
|
|
2018-03-12 00:30:31 +00:00
|
|
|
if (success) {
|
2018-03-13 17:45:55 +00:00
|
|
|
long timeSinceLastReminder = System.currentTimeMillis() - TextSecurePreferences.getRegistrationLockLastReminderTime(context);
|
|
|
|
nextReminderInterval = INTERVALS.higher(timeSinceLastReminder);
|
|
|
|
if (nextReminderInterval == null) nextReminderInterval = INTERVALS.last();
|
2018-03-12 00:30:31 +00:00
|
|
|
} else {
|
2018-03-13 17:45:55 +00:00
|
|
|
long lastReminderInterval = TextSecurePreferences.getRegistrationLockNextReminderInterval(context);
|
|
|
|
nextReminderInterval = INTERVALS.lower(lastReminderInterval);
|
|
|
|
if (nextReminderInterval == null) nextReminderInterval = INTERVALS.first();
|
2018-03-12 00:30:31 +00:00
|
|
|
}
|
2018-03-01 17:51:37 +00:00
|
|
|
|
|
|
|
TextSecurePreferences.setRegistrationLockLastReminderTime(context, System.currentTimeMillis());
|
|
|
|
TextSecurePreferences.setRegistrationLockNextReminderInterval(context, nextReminderInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|