Added conditional intermediate back-off

pull/202/head
M66B 3 years ago
parent 8f46bdebd1
commit e76055dca1

@ -33,11 +33,7 @@ public class AlarmManagerCompatEx {
static void setAndAllowWhileIdle(
@NonNull Context context, @NonNull AlarmManager am,
int type, long trigger, @NonNull PendingIntent pi) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean exact_alarms = prefs.getBoolean("exact_alarms", true);
// https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission
if (exact_alarms && canScheduleExactAlarms(context))
if (hasExactAlarms(context))
try {
AlarmManagerCompat.setExactAndAllowWhileIdle(am, type, trigger, pi);
} catch (SecurityException ex) {
@ -48,10 +44,17 @@ public class AlarmManagerCompatEx {
AlarmManagerCompat.setAndAllowWhileIdle(am, type, trigger, pi);
}
static boolean hasExactAlarms(Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean exact_alarms = prefs.getBoolean("exact_alarms", true);
return (exact_alarms && canScheduleExactAlarms(context));
}
static boolean canScheduleExactAlarms(Context context) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R)
return true;
else {
// https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
return am.canScheduleExactAlarms();
}

@ -1729,8 +1729,11 @@ public class Log {
.append(" night=").append(Helper.isNight(context))
.append("\r\n");
sb.append("canScheduleExactAlarms=")
sb.append("ExactAlarms")
.append(" can=")
.append(AlarmManagerCompatEx.canScheduleExactAlarms(context))
.append(" has=")
.append(AlarmManagerCompatEx.hasExactAlarms(context))
.append("\r\n");
sb.append("Transliterate: ")

@ -121,6 +121,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private static final int OPTIMIZE_POLL_INTERVAL = 15; // minutes
private static final int CONNECT_BACKOFF_START = 8; // seconds
private static final int CONNECT_BACKOFF_MAX = 8; // seconds (totally 8+2x20=48 seconds)
private static final int CONNECT_BACKOFF_INTERMEDIATE = 5; // minutes
private static final int CONNECT_BACKOFF_ALARM_START = 15; // minutes
private static final int CONNECT_BACKOFF_ALARM_MAX = 60; // minutes
private static final long CONNECT_BACKOFF_GRACE = 2 * 60 * 1000L; // milliseconds
@ -2153,6 +2154,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (backoff < CONNECT_BACKOFF_MAX)
state.setBackoff(backoff * 2);
else if (backoff == CONNECT_BACKOFF_MAX)
if (AlarmManagerCompatEx.hasExactAlarms(this))
state.setBackoff(CONNECT_BACKOFF_INTERMEDIATE * 60);
else
state.setBackoff(CONNECT_BACKOFF_ALARM_START * 60);
else if (backoff == CONNECT_BACKOFF_INTERMEDIATE * 60)
state.setBackoff(CONNECT_BACKOFF_ALARM_START * 60);
else if (backoff < CONNECT_BACKOFF_ALARM_MAX * 60) {
int b = backoff * 2;

Loading…
Cancel
Save