Prolonged oneshots

pull/156/head
M66B 6 years ago
parent 65dcbc7464
commit 56b1361992

@ -89,6 +89,7 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
public class ServiceSynchronize extends LifecycleService { public class ServiceSynchronize extends LifecycleService {
private Helper.NetworkState networkState = new Helper.NetworkState(); private Helper.NetworkState networkState = new Helper.NetworkState();
private Core.State state; private Core.State state;
private boolean oneshot = false;
private boolean started = false; private boolean started = false;
private int queued = 0; private int queued = 0;
private long lastLost = 0; private long lastLost = 0;
@ -285,11 +286,13 @@ public class ServiceSynchronize extends LifecycleService {
} }
private void onOneshot(boolean start) { private void onOneshot(boolean start) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Log.i("Oneshot start=" + start);
Intent alarm = new Intent(this, ServiceSynchronize.class); Intent alarm = new Intent(this, ServiceSynchronize.class);
alarm.setAction("oneshot_end"); alarm.setAction("oneshot_end");
PendingIntent piOneshot; PendingIntent piOneshot;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
piOneshot = PendingIntent.getService(this, PI_ONESHOT, alarm, PendingIntent.FLAG_UPDATE_CURRENT); piOneshot = PendingIntent.getService(this, PI_ONESHOT, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
else else
@ -297,17 +300,16 @@ public class ServiceSynchronize extends LifecycleService {
am.cancel(piOneshot); am.cancel(piOneshot);
oneshot = start;
if (start) { if (start) {
// Network events will manage the service // Network events will manage the service
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot);
else else
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot); am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot);
} else { } else
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); onReload(true, "oneshot");
prefs.edit().putBoolean("oneshot", false).apply();
queue_reload(true, true, "oneshot");
}
} }
private void queue_reload(final boolean start, final boolean clear, final String reason) { private void queue_reload(final boolean start, final boolean clear, final String reason) {
@ -315,7 +317,7 @@ public class ServiceSynchronize extends LifecycleService {
final boolean doStart = (start && isEnabled() && networkState.isSuitable()); final boolean doStart = (start && isEnabled() && networkState.isSuitable());
EntityLog.log(this, "Queue reload" + EntityLog.log(this, "Queue reload" +
" doStop=" + doStop + " doStart=" + doStart + " queued=" + queued + " " + reason); " doStop=" + doStop + " doStart=" + doStart + " queued=" + queued + " reason=" + reason);
started = doStart; started = doStart;
@ -386,7 +388,6 @@ public class ServiceSynchronize extends LifecycleService {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean enabled = prefs.getBoolean("enabled", true); boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", 0); int pollInterval = prefs.getInt("poll_interval", 0);
boolean oneshot = prefs.getBoolean("oneshot", false);
return ((enabled && pollInterval == 0) || oneshot); return ((enabled && pollInterval == 0) || oneshot);
} }
@ -1203,10 +1204,6 @@ public class ServiceSynchronize extends LifecycleService {
try { try {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
// Reset state
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().remove("oneshot").apply();
// Restore snooze timers // Restore snooze timers
for (EntityMessage message : db.message().getSnoozed()) for (EntityMessage message : db.message().getSnoozed())
EntityMessage.snooze(context, message.id, message.ui_snoozed); EntityMessage.snooze(context, message.id, message.ui_snoozed);
@ -1215,6 +1212,7 @@ public class ServiceSynchronize extends LifecycleService {
schedule(context); schedule(context);
// Conditionally init service // Conditionally init service
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true); boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", 0); int pollInterval = prefs.getInt("poll_interval", 0);
int accounts = db.account().getSynchronizingAccounts().size(); int accounts = db.account().getSynchronizingAccounts().size();
@ -1329,17 +1327,8 @@ public class ServiceSynchronize extends LifecycleService {
boolean enabled = prefs.getBoolean("enabled", true); boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", 0); int pollInterval = prefs.getInt("poll_interval", 0);
if (!enabled || pollInterval > 0) if (!enabled || pollInterval > 0)
onshot(context);
}
static void onshot(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean oneshot = prefs.getBoolean("oneshot", false);
if (!oneshot) {
prefs.edit().putBoolean("oneshot", true).apply();
ContextCompat.startForegroundService(context, ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class) new Intent(context, ServiceSynchronize.class)
.setAction("oneshot_start")); .setAction("oneshot_start"));
}
} }
} }

@ -42,7 +42,7 @@ public class WorkerPoll extends Worker {
@Override @Override
public Result doWork() { public Result doWork() {
Log.i("Running " + getName()); Log.i("Running " + getName());
ServiceSynchronize.onshot(getApplicationContext()); ServiceSynchronize.process(getApplicationContext());
return Result.success(); return Result.success();
} }

Loading…
Cancel
Save