Moved polling to sync service

This commit is contained in:
M66B 2021-03-29 15:22:49 +02:00
parent 9800d018b9
commit ba8022f869
2 changed files with 74 additions and 41 deletions

View File

@ -153,6 +153,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
static final int PI_BACKOFF = 2;
static final int PI_KEEPALIVE = 3;
static final int PI_ENABLE = 4;
static final int PI_POLL = 5;
@Override
public void onCreate() {
@ -814,6 +815,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
onState(intent);
break;
case "poll":
onPoll(intent);
break;
case "alarm":
onAlarm(intent);
break;
@ -878,6 +883,43 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
foreground = intent.getBooleanExtra("foreground", false);
}
private void onPoll(Intent intent) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
DB db = DB.getInstance(ServiceSynchronize.this);
try {
db.beginTransaction();
List<EntityAccount> accounts = db.account().getPollAccounts(null);
for (EntityAccount account : accounts) {
List<EntityFolder> folders = db.folder().getSynchronizingFolders(account.id);
if (folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(ServiceSynchronize.this));
for (EntityFolder folder : folders)
EntityOperation.sync(ServiceSynchronize.this, folder.id, false);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
long now = new Date().getTime();
long[] schedule = ServiceSynchronize.getSchedule(ServiceSynchronize.this);
boolean poll = (schedule == null || (now >= schedule[0] && now < schedule[1]));
schedule(ServiceSynchronize.this, poll, null);
// Prevent service stop
eval(ServiceSynchronize.this, "poll");
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
private void onAlarm(Intent intent) {
schedule(this, true);
@ -2308,7 +2350,35 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
}
ServiceUI.schedule(context, poll, at);
schedule(context, poll, at);
}
private static void schedule(Context context, boolean poll, Long at) {
Intent intent = new Intent(context, ServiceSynchronize.class);
intent.setAction("poll");
PendingIntent piSync = PendingIntentCompat.getForegroundService(
context, PI_POLL, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(piSync);
if (at == null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", ServiceSynchronize.DEFAULT_POLL_INTERVAL);
if (poll && enabled && pollInterval > 0) {
long now = new Date().getTime();
long interval = pollInterval * 60 * 1000L;
long next = now + interval - now % interval + 30 * 1000L;
if (next < now + interval / 5)
next += interval;
EntityLog.log(context, "Poll next=" + new Date(next));
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piSync);
}
} else
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, at, piSync);
}
static long[] getSchedule(Context context) {

View File

@ -61,7 +61,7 @@ public class ServiceUI extends IntentService {
static final int PI_IGNORED = 10;
static final int PI_THREAD = 11;
static final int PI_WAKEUP = 12;
static final int PI_SYNC = 13;
static final int PI_BANNER = 14;
static final int PI_EXISTS = 15;
static final int PI_PROTOCOL = 16;
@ -169,8 +169,7 @@ public class ServiceUI extends IntentService {
break;
case "sync":
boolean reschedule = intent.getBooleanExtra("reschedule", false);
onSync(id, reschedule);
onSync(id);
break;
case "exists":
@ -543,7 +542,7 @@ public class ServiceUI extends IntentService {
ServiceSend.start(this);
}
private void onSync(long aid, boolean reschedule) {
private void onSync(long aid) {
DB db = DB.getInstance(this);
try {
db.beginTransaction();
@ -561,13 +560,6 @@ public class ServiceUI extends IntentService {
} finally {
db.endTransaction();
}
if (reschedule) {
long now = new Date().getTime();
long[] schedule = ServiceSynchronize.getSchedule(this);
boolean poll = (schedule == null || (now >= schedule[0] && now < schedule[1]));
schedule(this, poll, null);
}
}
private void onExists(long id) {
@ -604,35 +596,6 @@ public class ServiceUI extends IntentService {
.setAction(account == null ? "sync" : "sync:" + account));
}
static void schedule(Context context, boolean poll, Long at) {
Intent intent = new Intent(context, ServiceUI.class);
intent.setAction("sync");
intent.putExtra("reschedule", true);
PendingIntent piSync = PendingIntentCompat.getService(
context, ServiceUI.PI_SYNC, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(piSync);
if (at == null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", ServiceSynchronize.DEFAULT_POLL_INTERVAL);
if (poll && enabled && pollInterval > 0) {
long now = new Date().getTime();
long interval = pollInterval * 60 * 1000L;
long next = now + interval - now % interval + 30 * 1000L;
if (next < now + interval / 5)
next += interval;
EntityLog.log(context, "Poll next=" + new Date(next));
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piSync);
}
} else
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, at, piSync);
}
private static PendingIntent getBannerIntent(Context context) {
Intent intent = new Intent(context, ServiceUI.class);
intent.setAction("banner");