Revert "Switch to alarm manager for more control over polling"

This reverts commit 7658047300.
This commit is contained in:
M66B 2020-01-10 12:08:14 +01:00
parent 7658047300
commit 9306d4e1e9
6 changed files with 150 additions and 75 deletions

View File

@ -819,7 +819,19 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (intent.getBooleanExtra("refresh", false)) {
intent.removeExtra("refresh");
setIntent(intent);
ServiceSynchronize.poll(this, null);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
WorkerPoll.sync(context, null);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(this, new Bundle(), "view:refresh");
}
String action = intent.getAction();

View File

@ -148,7 +148,8 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("schedule", checked).apply();
ServiceSynchronize.reschedule(getContext());
if (checked)
ServiceSynchronize.reschedule(getContext());
}
});

View File

@ -90,7 +90,7 @@ public class ServiceExternal extends Service {
public void run() {
if (accountName == null) {
if (enabled == null)
ServiceSynchronize.poll(context, null);
WorkerPoll.sync(context, null);
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("enabled", enabled).apply();
@ -106,7 +106,7 @@ public class ServiceExternal extends Service {
}
if (enabled == null)
ServiceSynchronize.poll(context, account.id);
WorkerPoll.sync(context, account.id);
else {
db.account().setAccountSynchronize(account.id, enabled);
ServiceSynchronize.eval(context, "external account=" + accountName + " enabled=" + enabled);

View File

@ -121,9 +121,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
));
static final int PI_ALARM = 1;
static final int PI_POLL = 2;
static final int PI_BACKOFF = 3;
static final int PI_KEEPALIVE = 4;
static final int PI_BACKOFF = 2;
static final int PI_KEEPALIVE = 3;
@Override
public void onCreate() {
@ -225,12 +224,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
// Some networks disallow email server connections:
// - reload on network type change when disconnected
if (reload || sync ||
if (reload ||
prev.canRun() != current.canRun() ||
!prev.accountState.equals(current.accountState) ||
(!"connected".equals(current.accountState.state) &&
!Objects.equals(prev.networkState.getType(), current.networkState.getType()))) {
if (prev.canRun() || current.canRun())
EntityLog.log(ServiceSynchronize.this, "### changed " + current +
" reload=" + reload +
@ -241,14 +239,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" enabled=" + current.accountState.synchronize +
" state=" + current.accountState.state +
" type=" + prev.networkState.getType() + "/" + current.networkState.getType());
if (prev.canRun())
if (sync && current.canRun()) {
sync(current);
continue;
} else
stop(prev);
stop(prev);
if (current.canRun())
start(current, current.accountState.isEnabled(current.enabled) || sync);
}
@ -333,22 +325,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
});
}
private void sync(final TupleAccountNetworkState accountNetworkState) {
EntityLog.log(ServiceSynchronize.this, "Service sync=" + accountNetworkState);
queue.submit(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(ServiceSynchronize.this);
List<EntityFolder> folders = db.folder().getSynchronizingFolders(accountNetworkState.accountState.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);
}
});
}
private void delete(final TupleAccountNetworkState accountNetworkState) {
EntityLog.log(ServiceSynchronize.this, "Service delete=" + accountNetworkState);
@ -637,7 +613,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private void onEval(Intent intent) {
Bundle command = new Bundle();
command.putString("name", "eval");
command.putBoolean("sync", intent.getBooleanExtra("sync", false));
command.putLong("account", intent.getLongExtra("account", -1));
liveAccountNetworkState.post(command);
}
@ -1565,46 +1540,31 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
private static void schedule(Context context) {
Intent alarm = new Intent(context, ServiceSynchronize.class);
alarm.setAction("alarm");
PendingIntent piAlarm = PendingIntentCompat.getForegroundService(
context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
Intent poll = new Intent(context, ServiceSynchronize.class);
poll.setAction("poll");
PendingIntent piPoll = PendingIntentCompat.getForegroundService(
context, PI_POLL, poll, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ServiceSynchronize.class);
intent.setAction("alarm");
PendingIntent pi = PendingIntentCompat.getForegroundService(
context, PI_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(piAlarm);
am.cancel(piPoll);
am.cancel(pi);
boolean enabled;
long now = new Date().getTime();
long[] schedule = getSchedule(context);
if (schedule == null)
enabled = true;
else {
long next = (now < schedule[0] ? schedule[0] : schedule[1]);
enabled = (now >= schedule[0] && now < schedule[1]);
return;
Log.i("Schedule now=" + new Date(now));
Log.i("Schedule start=" + new Date(schedule[0]));
Log.i("Schedule end=" + new Date(schedule[1]));
Log.i("Schedule next=" + new Date(next));
Log.i("Schedule enabled=" + enabled);
long now = new Date().getTime();
long next = (now < schedule[0] ? schedule[0] : schedule[1]);
boolean enabled = (now >= schedule[0] && now < schedule[1]);
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piAlarm);
}
Log.i("Schedule now=" + new Date(now));
Log.i("Schedule start=" + new Date(schedule[0]));
Log.i("Schedule end=" + new Date(schedule[1]));
Log.i("Schedule next=" + new Date(next));
Log.i("Schedule enabled=" + enabled);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int pollInterval = prefs.getInt("poll_interval", 0);
if (enabled && pollInterval > 0) {
long interval = pollInterval * 60 * 1000L;
long trigger = now / interval * interval + interval;
Log.i("Poll interval=" + pollInterval + " trigger=" + new Date(trigger));
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, trigger, interval, piPoll);
}
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi);
WorkerPoll.init(context, enabled);
}
private static long[] getSchedule(Context context) {
@ -1700,15 +1660,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
.putExtra("reason", reason));
}
static void poll(Context context, Long account) {
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class)
.setAction("eval")
.putExtra("account", account == null ? -1 : account)
.putExtra("sync", true)
.putExtra("reason", "poll"));
}
static void reload(Context context, Long account, String reason) {
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class)

View File

@ -20,6 +20,7 @@ package eu.faircode.email;
*/
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Build;
@ -96,6 +97,12 @@ public class ServiceTileUnseen extends TileService {
public void onClick() {
Log.i("Click tile unseen");
ServiceSynchronize.poll(this, null);
final Context context = getApplicationContext();
new Thread(new Runnable() {
@Override
public void run() {
WorkerPoll.sync(context, null);
}
}).start();
}
}

View File

@ -0,0 +1,104 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class WorkerPoll extends Worker {
public WorkerPoll(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
Log.i("Instance " + getName());
}
@NonNull
@Override
public Result doWork() {
Log.i("Running " + getName());
sync(getApplicationContext(), null);
return Result.success();
}
static void init(Context context, boolean enabled) {
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int pollInterval = prefs.getInt("poll_interval", 0);
if (enabled && pollInterval > 0) {
Log.i("Queuing " + getName() + " every " + pollInterval + " minutes");
PeriodicWorkRequest workRequest =
new PeriodicWorkRequest.Builder(WorkerPoll.class, pollInterval, TimeUnit.MINUTES)
.build();
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.REPLACE, workRequest);
Log.i("Queued " + getName());
} else {
Log.i("Cancelling " + getName());
WorkManager.getInstance(context).cancelUniqueWork(getName());
Log.i("Cancelled " + getName());
}
} catch (IllegalStateException ex) {
// https://issuetracker.google.com/issues/138465476
Log.w(ex);
}
}
static void sync(Context context, Long aid) {
DB db = DB.getInstance(context);
try {
db.beginTransaction();
List<EntityAccount> accounts = db.account().getSynchronizingAccounts();
for (EntityAccount account : accounts)
if (aid == null || account.id.equals(aid)) {
List<EntityFolder> folders = db.folder().getSynchronizingFolders(account.id);
if (folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(context));
for (EntityFolder folder : folders)
EntityOperation.sync(context, folder.id, false);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "refresh/poll");
}
private static String getName() {
return WorkerPoll.class.getSimpleName();
}
}