FairEmail/app/src/main/java/eu/faircode/email/ServiceSynchronize.java

1513 lines
73 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2018-10-09 19:39:25 +00:00
import android.app.AlarmManager;
2018-08-02 13:33:06 +00:00
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
2018-09-14 13:31:00 +00:00
import android.content.SharedPreferences;
2018-08-02 13:33:06 +00:00
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
2018-08-02 13:33:06 +00:00
import android.net.NetworkRequest;
import android.os.Build;
2018-12-02 13:19:54 +00:00
import android.os.Handler;
2019-12-09 11:00:41 +00:00
import android.os.Looper;
2018-10-08 06:18:44 +00:00
import android.os.PowerManager;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
2018-08-02 13:33:06 +00:00
import androidx.annotation.NonNull;
2019-07-08 17:23:07 +00:00
import androidx.core.app.AlarmManagerCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
2019-12-06 15:00:11 +00:00
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager;
2018-08-02 13:33:06 +00:00
import com.sun.mail.imap.IMAPFolder;
2019-02-27 15:05:15 +00:00
import java.text.DateFormat;
2018-08-02 13:33:06 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2018-08-02 13:33:06 +00:00
import java.util.Calendar;
2019-05-21 13:21:03 +00:00
import java.util.Collections;
import java.util.Comparator;
2018-08-02 13:33:06 +00:00
import java.util.Date;
2018-08-04 22:35:47 +00:00
import java.util.HashMap;
import java.util.Hashtable;
2018-08-02 13:33:06 +00:00
import java.util.List;
2018-08-04 22:35:47 +00:00
import java.util.Map;
import java.util.Objects;
2019-06-20 16:07:56 +00:00
import java.util.Random;
2018-08-02 13:33:06 +00:00
import java.util.concurrent.ExecutorService;
2019-06-23 18:56:07 +00:00
import javax.mail.AuthenticationFailedException;
2018-08-02 13:33:06 +00:00
import javax.mail.Folder;
2018-08-02 15:25:01 +00:00
import javax.mail.FolderClosedException;
2019-05-25 14:45:46 +00:00
import javax.mail.FolderNotFoundException;
2018-08-02 13:33:06 +00:00
import javax.mail.Message;
import javax.mail.MessagingException;
2018-08-15 19:34:22 +00:00
import javax.mail.NoSuchProviderException;
2019-04-29 19:54:49 +00:00
import javax.mail.ReadOnlyFolderException;
2018-09-03 16:34:38 +00:00
import javax.mail.StoreClosedException;
2018-08-02 13:33:06 +00:00
import javax.mail.event.FolderAdapter;
import javax.mail.event.FolderEvent;
import javax.mail.event.MessageChangedEvent;
import javax.mail.event.MessageChangedListener;
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;
import javax.mail.event.StoreEvent;
import javax.mail.event.StoreListener;
2019-02-27 15:05:15 +00:00
import me.leolin.shortcutbadger.ShortcutBadger;
2018-08-19 07:21:39 +00:00
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener {
2019-12-07 21:06:40 +00:00
private Boolean lastSuitable = null;
2019-03-06 10:18:57 +00:00
private long lastLost = 0;
2019-12-07 21:06:40 +00:00
private int lastAccounts = 0;
private int lastOperations = 0;
2019-02-28 07:55:08 +00:00
private static final int CONNECT_BACKOFF_START = 8; // seconds
2018-11-08 12:47:34 +00:00
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
2018-11-08 17:16:32 +00:00
private static final int CONNECT_BACKOFF_AlARM = 15; // minutes
private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds
2019-04-25 13:21:32 +00:00
private static final int ACCOUNT_ERROR_AFTER = 60; // minutes
private static final int BACKOFF_ERROR_AFTER = 16; // seconds
private static final List<String> PREF_EVAL = Collections.unmodifiableList(Arrays.asList(
"enabled", "poll_interval"
));
private static final List<String> PREF_RELOAD = Collections.unmodifiableList(Arrays.asList(
2019-12-07 19:32:58 +00:00
"metered", "roaming", "rlah", // force reconnect
"socks_enabled", "socks_proxy", // force reconnect
"subscribed_only", // force folder sync
"badge", "unseen_ignored", // force update badge/widget
"debug" // force reconnect
));
2019-02-27 15:05:15 +00:00
2019-02-28 07:55:08 +00:00
static final int PI_ALARM = 1;
2019-12-06 15:00:11 +00:00
private MutableLiveData<ConnectionHelper.NetworkState> liveNetworkState = new MutableLiveData<>();
private MutableLiveData<List<TupleAccountState>> liveAccountState = new MutableLiveData<>();
private MediatorState liveAccountNetworkState = new MediatorState();
private class MediatorState extends MediatorLiveData<List<TupleAccountNetworkState>> {
private ConnectionHelper.NetworkState lastNetworkState = null;
private List<TupleAccountState> lastAccountStates = null;
private void post(boolean reload) {
post(reload, lastNetworkState, lastAccountStates);
}
2019-12-06 15:00:11 +00:00
private void post(ConnectionHelper.NetworkState networkState) {
lastNetworkState = networkState;
post(false, lastNetworkState, lastAccountStates);
2019-12-06 15:00:11 +00:00
}
private void post(List<TupleAccountState> accountStates) {
lastAccountStates = accountStates;
post(false, lastNetworkState, lastAccountStates);
2019-12-06 15:00:11 +00:00
}
2019-12-09 11:00:41 +00:00
private void postDestroy() {
postValue(null);
}
private void post(boolean reload, ConnectionHelper.NetworkState networkState, List<TupleAccountState> accountStates) {
2019-12-09 11:00:41 +00:00
if (Looper.myLooper() == Looper.getMainLooper())
_post(reload, networkState, accountStates);
else {
// Some Android versions call onDestroy not on the main thread
Log.e("### not main thread state=" + (accountStates == null ? null : accountStates.size()));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
_post(reload, networkState, accountStates);
}
});
}
}
private void _post(boolean reload, ConnectionHelper.NetworkState networkState, List<TupleAccountState> accountStates) {
2019-12-06 15:00:11 +00:00
if (networkState != null && accountStates != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", 0);
long[] schedule = getSchedule(ServiceSynchronize.this);
long now = new Date().getTime();
boolean scheduled = (schedule == null || now >= schedule[0] && now < schedule[1]);
2019-12-06 15:00:11 +00:00
List<TupleAccountNetworkState> result = new ArrayList<>();
for (TupleAccountState accountState : accountStates)
result.add(new TupleAccountNetworkState(
enabled && pollInterval == 0 && scheduled,
reload,
networkState,
accountState));
2019-12-06 15:00:11 +00:00
postValue(result);
}
}
}
2018-08-02 13:33:06 +00:00
@Override
public void onCreate() {
2019-08-17 11:06:57 +00:00
EntityLog.log(this, "Service create version=" + BuildConfig.VERSION_NAME);
2018-08-02 13:33:06 +00:00
super.onCreate();
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build());
2018-08-02 13:33:06 +00:00
// Listen for network changes
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
// Removed because of Android VPN service
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
2019-10-04 15:17:42 +00:00
cm.registerNetworkCallback(builder.build(), networkCallback);
2019-07-11 06:42:20 +00:00
2019-10-04 15:17:42 +00:00
IntentFilter iif = new IntentFilter();
iif.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
iif.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(connectionChangedReceiver, iif);
2019-07-11 06:42:20 +00:00
registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));
2018-08-02 13:33:06 +00:00
DB db = DB.getInstance(this);
db.account().liveAccountState().observe(this, new Observer<List<TupleAccountState>>() {
@Override
public void onChanged(List<TupleAccountState> accountStates) {
liveAccountState.postValue(accountStates);
}
});
2019-12-06 15:00:11 +00:00
liveAccountNetworkState.addSource(liveNetworkState, new Observer<ConnectionHelper.NetworkState>() {
@Override
public void onChanged(ConnectionHelper.NetworkState networkState) {
liveAccountNetworkState.post(networkState);
}
});
liveAccountNetworkState.addSource(liveAccountState, new Observer<List<TupleAccountState>>() {
@Override
public void onChanged(List<TupleAccountState> accountStates) {
liveAccountNetworkState.post(accountStates);
}
});
2019-12-07 19:32:58 +00:00
liveAccountNetworkState.observeForever(new Observer<List<TupleAccountNetworkState>>() {
boolean running = true;
private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
private Map<TupleAccountNetworkState, Core.State> serviceStates = new Hashtable<>();
private ExecutorService queue = Helper.getBackgroundExecutor(1, "service");
2019-12-06 15:00:11 +00:00
@Override
public void onChanged(List<TupleAccountNetworkState> accountNetworkStates) {
2019-12-07 19:32:58 +00:00
if (accountNetworkStates == null) {
for (TupleAccountNetworkState prev : serviceStates.keySet())
stop(prev);
quit();
accountStates.clear();
serviceStates.clear();
liveAccountNetworkState.removeObserver(this);
} else {
if (!running) {
Log.i("### not running");
return;
}
2019-12-07 21:06:40 +00:00
int accounts = 0;
int operations = 0;
2019-12-07 19:32:58 +00:00
boolean runService = false;
for (TupleAccountNetworkState current : accountNetworkStates) {
if (current.accountState.shouldRun(current.enabled))
runService = true;
if ("connected".equals(current.accountState.state))
2019-12-07 21:06:40 +00:00
accounts++;
2019-12-09 07:33:25 +00:00
if (current.accountState.synchronize)
operations += current.accountState.operations;
2019-12-07 19:32:58 +00:00
int index = accountStates.indexOf(current);
if (index < 0) {
if (current.canRun()) {
EntityLog.log(ServiceSynchronize.this, "### new " + current);
start(current, current.accountState.isEnabled(current.enabled));
2019-12-07 19:32:58 +00:00
}
} else {
TupleAccountNetworkState prev = accountStates.get(index);
accountStates.remove(index);
Core.State state = serviceStates.get(current);
if (state != null)
state.setNetworkState(current.networkState);
// Some networks disallow email server connections:
// - reload on network type change when disconnected
2019-12-07 19:32:58 +00:00
if (current.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=" + current.reload +
" stop=" + prev.canRun() +
" start=" + current.canRun() +
" changed=" + !prev.accountState.equals(current.accountState) +
2019-12-09 08:31:09 +00:00
" enabled=" + current.accountState.synchronize +
" state=" + current.accountState.state +
" type=" + prev.networkState.getType() + "/" + current.networkState.getType());
2019-12-07 19:32:58 +00:00
if (prev.canRun())
stop(prev);
if (current.canRun())
start(current, current.accountState.isEnabled(current.enabled));
if (current.accountState.tbd != null)
delete(current);
}
}
2019-12-07 19:32:58 +00:00
if (current.accountState.tbd == null)
accountStates.add(current);
}
2019-12-07 19:32:58 +00:00
if (runService) {
2019-12-07 21:06:40 +00:00
if (lastAccounts != accounts || lastOperations != operations) {
lastAccounts = accounts;
lastOperations = operations;
2019-12-07 19:32:58 +00:00
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2019-12-07 21:06:40 +00:00
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
2019-12-07 19:32:58 +00:00
}
} else {
running = false;
2019-12-07 21:06:40 +00:00
stopSelf(); // will result in quit
2019-12-07 19:32:58 +00:00
}
}
}
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync) {
EntityLog.log(ServiceSynchronize.this, "Service start=" + accountNetworkState);
2019-12-07 21:06:40 +00:00
final Core.State astate = new Core.State(accountNetworkState.networkState);
astate.runnable(new Runnable() {
@Override
public void run() {
try {
monitorAccount(accountNetworkState.accountState, astate, sync);
} catch (Throwable ex) {
Log.e(accountNetworkState.accountState.name, ex);
}
}
}, "sync.account." + accountNetworkState.accountState.id);
serviceStates.put(accountNetworkState, astate);
queue.submit(new Runnable() {
@Override
public void run() {
Map<String, String> crumb = new HashMap<>();
crumb.put("account", accountNetworkState.toString());
2019-12-07 21:06:40 +00:00
crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected()));
crumb.put("suitable", Boolean.toString(accountNetworkState.networkState.isSuitable()));
crumb.put("unmetered", Boolean.toString(accountNetworkState.networkState.isUnmetered()));
crumb.put("roaming", Boolean.toString(accountNetworkState.networkState.isRoaming()));
crumb.put("lastLost", new Date(lastLost).toString());
Log.breadcrumb("start", crumb);
2019-12-07 16:02:42 +00:00
Log.i("### start=" + accountNetworkState);
astate.start();
EntityLog.log(ServiceSynchronize.this, "### started=" + accountNetworkState);
}
});
}
private void stop(final TupleAccountNetworkState accountNetworkState) {
EntityLog.log(ServiceSynchronize.this, "Service stop=" + accountNetworkState);
final Core.State state = serviceStates.get(accountNetworkState);
serviceStates.remove(accountNetworkState);
queue.submit(new Runnable() {
@Override
public void run() {
Map<String, String> crumb = new HashMap<>();
crumb.put("account", accountNetworkState.toString());
2019-12-07 21:06:40 +00:00
crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected()));
crumb.put("suitable", Boolean.toString(accountNetworkState.networkState.isSuitable()));
crumb.put("unmetered", Boolean.toString(accountNetworkState.networkState.isUnmetered()));
crumb.put("roaming", Boolean.toString(accountNetworkState.networkState.isRoaming()));
crumb.put("lastLost", new Date(lastLost).toString());
Log.breadcrumb("stop", crumb);
2019-12-07 16:02:42 +00:00
Log.i("### stop=" + accountNetworkState);
state.stop();
state.join();
EntityLog.log(ServiceSynchronize.this, "### stopped=" + accountNetworkState);
}
});
2019-12-06 15:00:11 +00:00
}
private void delete(final TupleAccountNetworkState accountNetworkState) {
EntityLog.log(ServiceSynchronize.this, "Service delete=" + accountNetworkState);
2019-06-01 06:44:46 +00:00
queue.submit(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(ServiceSynchronize.this);
db.account().deleteAccount(accountNetworkState.accountState.id);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.deleteNotificationChannel(EntityAccount.getNotificationChannelId(accountNetworkState.accountState.id));
}
2019-06-01 13:58:09 +00:00
}
});
}
2019-06-01 06:44:46 +00:00
2019-12-07 19:32:58 +00:00
private void quit() {
EntityLog.log(ServiceSynchronize.this, "Service quit");
2019-06-01 06:44:46 +00:00
queue.submit(new Runnable() {
@Override
public void run() {
2019-12-07 19:32:58 +00:00
Log.i("### quit");
DB db = DB.getInstance(ServiceSynchronize.this);
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
for (EntityOperation op : ops)
db.folder().setFolderSyncState(op.folder, null);
stopSelf();
EntityLog.log(ServiceSynchronize.this, "### quited");
}
});
}
});
2019-09-23 08:57:02 +00:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-09-23 08:57:02 +00:00
db.message().liveUnseenWidget().observe(this, new Observer<TupleMessageStats>() {
2019-09-22 12:42:43 +00:00
private Integer lastUnseen = null;
@Override
public void onChanged(TupleMessageStats stats) {
if (stats == null)
stats = new TupleMessageStats();
boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false);
2019-09-22 12:42:43 +00:00
Integer unseen = (unseen_ignored ? stats.notifying : stats.unseen);
if (unseen == null)
unseen = 0;
2019-09-22 12:42:43 +00:00
if (lastUnseen == null || !lastUnseen.equals(unseen)) {
Log.i("Stats " + stats);
2019-09-22 12:42:43 +00:00
lastUnseen = unseen;
setUnseen(unseen);
}
}
});
2019-03-27 13:24:58 +00:00
final TwoStateOwner cowner = new TwoStateOwner(this, "liveUnseenNotify");
2019-03-20 14:52:51 +00:00
db.folder().liveSynchronizing().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer count) {
Log.i("Synchronizing folders=" + count);
2019-06-01 08:02:12 +00:00
if (count == null || count == 0)
2019-03-20 14:52:51 +00:00
cowner.start();
else
cowner.stop();
}
});
2019-09-01 09:32:18 +00:00
Map<Long, List<Long>> groupNotifying = new HashMap<>();
// Get existing notifications
2019-09-13 09:50:33 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
for (StatusBarNotification sbn : nm.getActiveNotifications()) {
String tag = sbn.getTag();
if (tag != null && tag.startsWith("unseen.")) {
String[] p = tag.split(("\\."));
long group = Long.parseLong(p[1]);
2019-09-18 19:36:10 +00:00
long id = sbn.getNotification().extras.getLong("id", 0);
2019-09-13 09:50:33 +00:00
if (!groupNotifying.containsKey(group))
groupNotifying.put(group, new ArrayList<>());
if (id > 0) {
2019-09-18 19:36:10 +00:00
Log.i("Notify restore " + tag + " id=" + id);
2019-09-13 09:50:33 +00:00
groupNotifying.get(group).add(id);
}
}
}
2019-09-13 09:50:33 +00:00
} catch (Throwable ex) {
Log.w(ex);
/*
java.lang.RuntimeException: Unable to create service eu.faircode.email.ServiceSynchronize: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.content.pm.ParceledListSlice.getList()' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
at android.app.ActivityThread.access$1900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
*/
}
2019-03-20 14:52:51 +00:00
db.message().liveUnseenNotify().observe(cowner, new Observer<List<TupleMessageEx>>() {
2019-08-01 07:19:22 +00:00
private ExecutorService executor =
2019-10-10 11:26:44 +00:00
Helper.getBackgroundExecutor(1, "notify");
2019-08-01 07:19:22 +00:00
@Override
2019-08-01 07:19:22 +00:00
public void onChanged(final List<TupleMessageEx> messages) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
2019-08-04 13:21:21 +00:00
Core.notifyMessages(ServiceSynchronize.this, messages, groupNotifying);
2019-08-01 07:19:22 +00:00
} catch (SecurityException ex) {
Log.w(ex);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
prefs.edit().remove("sound").apply();
} catch (Throwable ex) {
Log.e(ex);
}
}
});
2018-08-02 13:33:06 +00:00
}
});
2019-12-01 10:42:54 +00:00
db.message().liveWidgetUnified().observe(cowner, new Observer<List<TupleMessageWidgetCount>>() {
private List<TupleMessageWidgetCount> last = null;
2019-07-25 15:22:00 +00:00
@Override
public void onChanged(List<TupleMessageWidgetCount> current) {
if (current == null)
current = new ArrayList<>();
boolean changed = false;
if (last == null || last.size() != current.size())
changed = true;
else
for (int i = 0; i < current.size(); i++)
if (!current.get(i).equals(last.get(i))) {
changed = true;
break;
}
if (changed)
WidgetUnified.update(ServiceSynchronize.this);
2019-11-18 09:14:37 +00:00
last = current;
2019-07-25 15:22:00 +00:00
}
});
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (PREF_EVAL.contains(key))
2019-12-06 20:42:56 +00:00
liveAccountNetworkState.post(false);
else if (PREF_RELOAD.contains(key))
liveAccountNetworkState.post(true);
2018-10-16 10:26:35 +00:00
}
@Override
public void onDestroy() {
2019-03-25 14:05:52 +00:00
EntityLog.log(this, "Service destroy");
2018-10-16 10:26:35 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
2019-07-11 06:42:20 +00:00
unregisterReceiver(onScreenOff);
2019-10-04 15:17:42 +00:00
unregisterReceiver(connectionChangedReceiver);
2019-07-11 06:42:20 +00:00
2018-10-16 10:26:35 +00:00
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
2019-10-04 15:17:42 +00:00
cm.unregisterNetworkCallback(networkCallback);
2018-10-16 10:26:35 +00:00
2019-12-07 19:32:58 +00:00
liveAccountNetworkState.postDestroy();
2018-10-21 18:29:28 +00:00
setUnseen(null);
2019-09-23 12:42:13 +00:00
2019-08-12 06:18:28 +00:00
try {
stopForeground(true);
} catch (Throwable ex) {
Log.e(ex);
/*
OnePlus A6013 - Android 9
java.lang.RuntimeException: Unable to stop service eu.faircode.email.ServiceSynchronize@3995fc9: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
at android.app.ActivityThread.handleStopService(ActivityThread.java:3908)
at android.app.ActivityThread.access$1900(ActivityThread.java:209)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1826)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6954)
at java.lang.reflect.Method.invoke(Method.java:-2)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
at android.os.Parcel.createException(Parcel.java:1956)
at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5111)
at android.app.Service.stopForeground(Service.java:724)
at android.app.Service.stopForeground(Service.java:710)
*/
}
2018-10-16 10:26:35 +00:00
2018-12-09 17:49:52 +00:00
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2019-02-15 07:51:14 +00:00
nm.cancel(Helper.NOTIFICATION_SYNCHRONIZE);
2018-10-16 10:26:35 +00:00
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = (intent == null ? null : intent.getAction());
2019-12-07 16:02:42 +00:00
String reason = (intent == null ? null : intent.getStringExtra("reason"));
EntityLog.log(ServiceSynchronize.this, "### Service command " + intent +
" action=" + action + " reason=" + reason);
2019-02-14 12:28:03 +00:00
Log.logExtras(intent);
2018-10-16 10:26:35 +00:00
super.onStartCommand(intent, flags, startId);
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build());
2018-08-02 13:33:06 +00:00
2018-12-28 17:21:31 +00:00
if (action != null)
try {
2019-02-28 07:55:08 +00:00
switch (action) {
case "eval":
boolean reload = intent.getBooleanExtra("reload", false);
liveAccountNetworkState.post(reload);
2019-02-14 12:28:03 +00:00
break;
case "alarm":
schedule(this);
eval(this, false, "alarm");
2018-12-28 17:21:31 +00:00
break;
2019-04-06 12:52:35 +00:00
case "reset":
lastLost = 0;
eval(this, true, "reset");
2019-05-12 18:15:16 +00:00
break;
2018-12-28 17:21:31 +00:00
default:
Log.w("Unknown action: " + action);
}
} catch (Throwable ex) {
Log.e(ex);
2018-08-22 12:30:27 +00:00
}
2018-08-02 13:33:06 +00:00
return START_STICKY;
}
2019-12-07 21:06:40 +00:00
private NotificationCompat.Builder getNotificationService(Integer accounts, Integer operations) {
if (accounts != null)
this.lastAccounts = accounts;
if (operations != null)
this.lastOperations = operations;
2018-10-17 06:11:20 +00:00
2018-08-02 13:33:06 +00:00
// Build pending intent
2019-03-14 14:57:24 +00:00
Intent why = new Intent(this, ActivityView.class);
why.setAction("why");
PendingIntent piWhy = PendingIntent.getActivity(this, ActivityView.REQUEST_WHY, why, PendingIntent.FLAG_UPDATE_CURRENT);
2018-08-02 13:33:06 +00:00
// Build notification
2019-06-23 08:23:49 +00:00
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "service")
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
.setContentTitle(getResources().getQuantityString(
2019-12-07 21:06:40 +00:00
R.plurals.title_notification_synchronizing, lastAccounts, lastAccounts))
2019-06-23 08:23:49 +00:00
.setContentIntent(piWhy)
.setAutoCancel(false)
.setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_MIN)
2019-07-23 09:38:36 +00:00
.setCategory(NotificationCompat.CATEGORY_SERVICE)
2019-10-16 13:18:59 +00:00
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setLocalOnly(true);
2018-08-02 13:33:06 +00:00
2019-12-07 21:06:40 +00:00
if (lastOperations > 0)
2019-03-03 09:17:42 +00:00
builder.setContentText(getResources().getQuantityString(
2019-12-07 21:06:40 +00:00
R.plurals.title_notification_operations, lastOperations, lastOperations));
2019-12-07 21:06:40 +00:00
if (lastSuitable == null || !lastSuitable)
builder.setSubText(getString(R.string.title_notification_waiting));
2018-08-02 13:33:06 +00:00
return builder;
}
private void setUnseen(Integer unseen) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean badge = prefs.getBoolean("badge", true);
Widget.update(this, unseen);
try {
if (unseen == null || !badge)
ShortcutBadger.removeCount(this);
else
ShortcutBadger.applyCount(this, unseen);
} catch (Throwable ex) {
Log.e(ex);
}
}
2019-07-22 11:18:52 +00:00
private void monitorAccount(final EntityAccount account, final Core.State state, final boolean sync) throws NoSuchProviderException {
2018-12-09 17:49:52 +00:00
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
2018-12-02 13:19:54 +00:00
final PowerManager.WakeLock wlAccount = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":account." + account.id);
2019-05-18 06:02:45 +00:00
final PowerManager.WakeLock wlFolder = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":account." + account.id + ".folder");
final PowerManager.WakeLock wlMessage = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":account." + account.id + ".message");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (account.notify)
account.createNotificationChannel(ServiceSynchronize.this);
else
account.deleteNotificationChannel(ServiceSynchronize.this);
}
2018-10-10 07:26:04 +00:00
try {
2018-12-02 13:19:54 +00:00
wlAccount.acquire();
2018-10-10 07:26:04 +00:00
2019-12-08 08:14:29 +00:00
long ago = new Date().getTime() - lastLost;
if (ago < RECONNECT_BACKOFF)
try {
long backoff = RECONNECT_BACKOFF - ago;
EntityLog.log(ServiceSynchronize.this, account.name + " backoff=" + (backoff / 1000));
state.acquire(backoff);
} catch (InterruptedException ex) {
Log.w(account.name + " backoff " + ex.toString());
}
2018-10-10 07:26:04 +00:00
final DB db = DB.getInstance(this);
int backoff = CONNECT_BACKOFF_START;
2019-08-12 13:50:46 +00:00
while (state.isRunning()) {
2019-04-10 18:17:58 +00:00
state.reset();
2018-12-24 12:27:45 +00:00
Log.i(account.name + " run");
2018-10-10 07:26:04 +00:00
2019-03-30 19:26:09 +00:00
Handler handler = new Handler(getMainLooper());
final List<TwoStateOwner> cowners = new ArrayList<>();
2018-10-10 07:26:04 +00:00
// Debug
2019-03-06 10:18:57 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-04-06 07:56:37 +00:00
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.DEBUG);
2018-10-10 07:26:04 +00:00
2019-07-29 09:17:12 +00:00
final MailService iservice = new MailService(
2019-10-19 08:05:43 +00:00
this, account.getProtocol(), account.realm, account.insecure, false, debug);
2019-07-29 09:17:12 +00:00
iservice.setPartialFetch(account.partial_fetch);
iservice.setIgnoreBodyStructureSize(account.ignore_size);
2019-11-23 12:48:59 +00:00
if (account.protocol != EntityAccount.TYPE_IMAP)
iservice.setLeaveOnServer(account.browse);
2019-05-21 13:21:03 +00:00
final Map<EntityFolder, IMAPFolder> mapFolders = new HashMap<>();
2018-10-10 07:26:04 +00:00
List<Thread> idlers = new ArrayList<>();
try {
2019-07-28 19:45:29 +00:00
// Initiate connection
EntityLog.log(this, account.name + " connecting");
db.folder().setFolderStates(account.id, null);
db.account().setAccountState(account.id, "connecting");
try {
2019-07-29 09:17:12 +00:00
iservice.connect(account);
2019-07-28 19:45:29 +00:00
} catch (Throwable ex) {
if (ex instanceof AuthenticationFailedException) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("receive:" + account.id, 1,
Core.getNotificationError(this, "error", account.name, ex)
.build());
throw ex;
}
// Report account connection error
if (account.last_connected != null && !ConnectionHelper.airplaneMode(this)) {
EntityLog.log(this, account.name + " last connected: " + new Date(account.last_connected));
long now = new Date().getTime();
long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L;
if (delayed > ACCOUNT_ERROR_AFTER * 60 * 1000L && backoff > BACKOFF_ERROR_AFTER) {
Log.i("Reporting sync error after=" + delayed);
Throwable warning = new Throwable(
getString(R.string.title_no_sync,
Helper.getDateTimeInstance(this, DateFormat.SHORT, DateFormat.SHORT)
.format(account.last_connected)), ex);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("receive:" + account.id, 1,
Core.getNotificationError(this, "warning", account.name, warning)
.build());
}
}
throw ex;
}
2019-09-19 15:41:26 +00:00
final boolean capIdle = iservice.hasCapability("IDLE");
2019-07-28 19:45:29 +00:00
Log.i(account.name + " idle=" + capIdle);
db.account().setAccountState(account.id, "connected");
db.account().setAccountError(account.id, null);
db.account().setAccountWarning(account.id, null);
EntityLog.log(this, account.name + " connected");
2018-10-10 07:26:04 +00:00
// Listen for store events
2019-07-28 19:45:29 +00:00
iservice.getStore().addStoreListener(new StoreListener() {
2018-10-10 07:26:04 +00:00
@Override
public void notification(StoreEvent e) {
2019-05-21 16:26:56 +00:00
if (e.getMessageType() == StoreEvent.NOTICE)
2019-07-09 11:22:51 +00:00
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + e.getMessage());
2019-05-21 16:26:56 +00:00
else
try {
wlFolder.acquire();
2019-06-25 06:05:28 +00:00
2019-05-21 16:26:56 +00:00
String message = e.getMessage();
Log.w(account.name + " alert: " + message);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this, account.name + " " +
2019-12-06 07:50:46 +00:00
Log.formatThrowable(new Core.AlertException(message), false));
db.account().setAccountError(account.id, message);
2019-06-23 18:56:07 +00:00
if (message != null && !message.startsWith("Too many simultaneous connections")) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2019-06-24 06:45:55 +00:00
nm.notify("alert:" + account.id, 1,
2019-06-23 18:56:07 +00:00
Core.getNotificationError(
ServiceSynchronize.this, "warning", account.name,
new Core.AlertException(message))
.build());
}
2019-04-10 18:17:58 +00:00
state.error(null);
2019-05-21 16:26:56 +00:00
} finally {
wlFolder.release();
}
2018-10-10 07:26:04 +00:00
}
});
2018-08-15 19:34:22 +00:00
2018-10-10 07:26:04 +00:00
// Listen for folder events
2019-07-28 19:45:29 +00:00
iservice.getStore().addFolderListener(new FolderAdapter() {
2018-10-10 07:26:04 +00:00
@Override
public void folderCreated(FolderEvent e) {
try {
2019-05-18 06:02:45 +00:00
wlFolder.acquire();
2019-05-25 14:32:08 +00:00
String name = e.getFolder().getFullName();
Log.i("Folder created=" + name);
if (db.folder().getFolderByName(account.id, name) == null)
eval(ServiceSynchronize.this, true, "folder created");
2018-10-10 07:26:04 +00:00
} finally {
2019-05-18 06:02:45 +00:00
wlFolder.release();
2018-10-10 07:26:04 +00:00
}
}
2018-08-02 13:33:06 +00:00
2018-10-10 07:26:04 +00:00
@Override
public void folderRenamed(FolderEvent e) {
try {
2019-05-18 06:02:45 +00:00
wlFolder.acquire();
2018-09-14 06:28:23 +00:00
2018-10-10 07:26:04 +00:00
String old = e.getFolder().getFullName();
String name = e.getNewFolder().getFullName();
2019-05-25 14:32:08 +00:00
Log.i("Folder renamed from=" + old + " to=" + name);
2018-10-10 07:26:04 +00:00
int count = db.folder().renameFolder(account.id, old, name);
2018-12-24 12:27:45 +00:00
Log.i("Renamed to " + name + " count=" + count);
2019-05-25 14:32:08 +00:00
if (count == 0)
eval(ServiceSynchronize.this, true, "folder renamed");
2018-10-10 07:26:04 +00:00
} finally {
2019-05-18 06:02:45 +00:00
wlFolder.release();
2018-10-10 07:26:04 +00:00
}
}
2018-08-02 13:33:06 +00:00
2018-10-10 07:26:04 +00:00
@Override
public void folderDeleted(FolderEvent e) {
try {
2019-05-18 06:02:45 +00:00
wlFolder.acquire();
2019-05-25 14:32:08 +00:00
String name = e.getFolder().getFullName();
Log.i("Folder deleted=" + name);
if (db.folder().getFolderByName(account.id, name) != null)
eval(ServiceSynchronize.this, true, "folder deleted");
2018-10-10 07:26:04 +00:00
} finally {
2019-05-18 06:02:45 +00:00
wlFolder.release();
2018-10-10 07:26:04 +00:00
}
}
});
2018-08-02 13:33:06 +00:00
2018-10-10 07:26:04 +00:00
// Update folder list
2019-11-23 12:48:59 +00:00
if (account.protocol == EntityAccount.TYPE_IMAP)
2019-09-19 15:41:26 +00:00
Core.onSynchronizeFolders(this, account, iservice.getStore(), state);
2018-08-04 22:35:47 +00:00
// Open synchronizing folders
2019-10-10 11:26:44 +00:00
final ExecutorService executor =
Helper.getBackgroundExecutor(1, "account_" + account.id);
2019-06-24 06:34:17 +00:00
2019-07-07 07:25:52 +00:00
List<EntityFolder> folders = db.folder().getFolders(account.id, false, true);
2019-06-24 06:34:17 +00:00
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
int s1 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type);
int s2 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type);
int s = Integer.compare(s1, s2);
if (s != 0)
return s;
return f1.name.compareTo(f2.name);
}
});
2019-05-21 13:21:03 +00:00
for (final EntityFolder folder : folders) {
2019-08-24 13:39:59 +00:00
if (folder.synchronize && !folder.poll && capIdle && sync) {
2018-12-24 12:27:45 +00:00
Log.i(account.name + " sync folder " + folder.name);
2018-08-22 05:31:00 +00:00
db.folder().setFolderState(folder.id, "connecting");
2018-08-22 05:31:00 +00:00
2019-07-28 19:45:29 +00:00
final IMAPFolder ifolder = (IMAPFolder) iservice.getStore().getFolder(folder.name);
try {
2019-07-07 07:25:52 +00:00
if (BuildConfig.DEBUG && "Postausgang".equals(folder.name))
throw new ReadOnlyFolderException(ifolder);
ifolder.open(Folder.READ_WRITE);
db.folder().setFolderReadOnly(folder.id, ifolder.getUIDNotSticky());
2019-04-29 19:54:49 +00:00
} catch (ReadOnlyFolderException ex) {
2019-04-30 07:06:32 +00:00
Log.w(folder.name + " read only");
2019-04-29 19:54:49 +00:00
try {
ifolder.open(Folder.READ_ONLY);
2019-04-30 07:06:32 +00:00
db.folder().setFolderReadOnly(folder.id, true);
2019-12-08 08:31:09 +00:00
} catch (Throwable ex1) {
2019-12-06 07:50:46 +00:00
db.folder().setFolderError(folder.id, Log.formatThrowable(ex1));
2019-12-08 08:31:09 +00:00
throw ex1;
2019-04-29 19:54:49 +00:00
}
2019-05-25 14:45:46 +00:00
} catch (FolderNotFoundException ex) {
2019-09-17 19:25:41 +00:00
Log.w(folder.name, ex);
2019-05-25 14:45:46 +00:00
db.folder().deleteFolder(folder.id);
continue;
} catch (Throwable ex) {
2019-12-06 07:50:46 +00:00
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
throw ex;
}
2019-05-21 13:21:03 +00:00
mapFolders.put(folder, ifolder);
2018-08-22 05:31:00 +00:00
db.folder().setFolderState(folder.id, "connected");
db.folder().setFolderError(folder.id, null);
2018-11-29 13:00:46 +00:00
2019-03-09 08:20:46 +00:00
int count = ifolder.getMessageCount();
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
2018-12-24 12:27:45 +00:00
Log.i(account.name + " folder " + folder.name + " flags=" + ifolder.getPermanentFlags());
2018-11-25 12:34:08 +00:00
// Listen for new and deleted messages
ifolder.addMessageCountListener(new MessageCountAdapter() {
@Override
public void messagesAdded(MessageCountEvent e) {
try {
2019-05-18 06:02:45 +00:00
wlMessage.acquire();
2018-12-24 12:27:45 +00:00
Log.i(folder.name + " messages added");
2019-09-17 11:38:23 +00:00
for (Message imessage : e.getMessages()) {
long uid = ifolder.getUID(imessage);
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid);
}
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(folder.name, ex);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this,
2019-12-06 07:50:46 +00:00
folder.name + " " + Log.formatThrowable(ex, false));
2019-04-10 18:17:58 +00:00
state.error(ex);
} finally {
2019-05-18 06:02:45 +00:00
wlMessage.release();
}
}
@Override
public void messagesRemoved(MessageCountEvent e) {
try {
2019-05-18 06:02:45 +00:00
wlMessage.acquire();
2018-12-24 12:27:45 +00:00
Log.i(folder.name + " messages removed");
2019-09-17 11:38:23 +00:00
for (Message imessage : e.getMessages()) {
long uid = ifolder.getUID(imessage);
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid);
2019-09-17 11:38:23 +00:00
}
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(folder.name, ex);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this,
2019-12-06 07:50:46 +00:00
folder.name + " " + Log.formatThrowable(ex, false));
2019-04-10 18:17:58 +00:00
state.error(ex);
} finally {
2019-05-18 06:02:45 +00:00
wlMessage.release();
}
}
});
// Flags (like "seen") at the remote could be changed while synchronizing
// Listen for changed messages
ifolder.addMessageChangedListener(new MessageChangedListener() {
@Override
public void messageChanged(MessageChangedEvent e) {
try {
2019-05-18 06:02:45 +00:00
wlMessage.acquire();
2019-09-17 11:38:23 +00:00
Log.i(folder.name + " message changed");
long uid = ifolder.getUID(e.getMessage());
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid);
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(folder.name, ex);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this,
2019-12-06 07:50:46 +00:00
folder.name + " " + Log.formatThrowable(ex, false));
2019-04-10 18:17:58 +00:00
state.error(ex);
} finally {
2019-05-18 06:02:45 +00:00
wlMessage.release();
2018-12-02 13:19:54 +00:00
}
2018-08-15 19:34:22 +00:00
}
});
2018-10-10 07:26:04 +00:00
// Idle folder
2018-10-10 07:26:04 +00:00
Thread idler = new Thread(new Runnable() {
@Override
public void run() {
try {
2018-12-24 12:27:45 +00:00
Log.i(folder.name + " start idle");
while (ifolder.isOpen() && state.isRunning() && state.isRecoverable()) {
2018-12-24 12:27:45 +00:00
Log.i(folder.name + " do idle");
2019-03-25 17:34:48 +00:00
ifolder.idle(false);
2018-10-10 07:26:04 +00:00
}
} catch (Throwable ex) {
Log.e(folder.name, ex);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this,
2019-12-06 07:50:46 +00:00
folder.name + " " + Log.formatThrowable(ex, false));
state.error(new FolderClosedException(ifolder, "IDLE"));
2018-10-10 07:26:04 +00:00
} finally {
2018-12-24 12:27:45 +00:00
Log.i(folder.name + " end idle");
}
2018-10-10 07:26:04 +00:00
}
}, "idler." + folder.id);
2019-01-07 15:05:24 +00:00
idler.setPriority(THREAD_PRIORITY_BACKGROUND);
2018-10-10 07:26:04 +00:00
idler.start();
idlers.add(idler);
2018-12-02 13:19:54 +00:00
2019-09-13 09:49:16 +00:00
if (sync && folder.selectable)
2019-07-22 11:18:52 +00:00
EntityOperation.sync(this, folder.id, false);
} else
2019-05-21 13:21:03 +00:00
mapFolders.put(folder, null);
2018-08-04 22:35:47 +00:00
2019-12-08 08:31:09 +00:00
Log.d(folder.name + " observing");
2019-03-30 19:26:09 +00:00
handler.post(new Runnable() {
2018-12-02 13:19:54 +00:00
@Override
2019-03-06 16:36:20 +00:00
public void run() {
2019-03-30 19:26:09 +00:00
TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name);
cowners.add(cowner);
cowner.start();
2019-03-30 06:39:26 +00:00
db.operation().liveOperations(folder.id).observe(cowner, new Observer<List<EntityOperation>>() {
2019-03-06 16:36:20 +00:00
private List<Long> handling = new ArrayList<>();
private final PowerManager.WakeLock wlFolder = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":folder." + folder.id);
@Override
public void onChanged(final List<EntityOperation> operations) {
boolean process = false;
List<Long> ops = new ArrayList<>();
for (EntityOperation op : operations) {
if (!handling.contains(op.id))
process = true;
ops.add(op.id);
2019-03-05 12:46:25 +00:00
}
2019-03-06 16:36:20 +00:00
handling = ops;
if (handling.size() > 0 && process) {
2019-05-22 07:33:25 +00:00
Log.i(folder.name + " operations=" + operations.size() +
" init=" + folder.initialize + " poll=" + folder.poll);
executor.submit(new Runnable() {
2019-03-06 16:36:20 +00:00
@Override
public void run() {
2018-12-05 10:50:07 +00:00
try {
2019-03-06 16:36:20 +00:00
wlFolder.acquire();
Log.i(folder.name + " process");
2018-08-04 22:35:47 +00:00
2019-03-06 16:36:20 +00:00
// Get folder
2019-05-21 13:21:03 +00:00
Folder ifolder = mapFolders.get(folder); // null when polling
2019-11-23 12:48:59 +00:00
boolean canOpen = (account.protocol == EntityAccount.TYPE_IMAP || EntityFolder.INBOX.equals(folder.type));
2019-09-19 15:41:26 +00:00
final boolean shouldClose = (ifolder == null && canOpen);
2019-03-06 16:36:20 +00:00
try {
Log.i(folder.name + " run " + (shouldClose ? "offline" : "online"));
2018-09-03 09:34:13 +00:00
2019-09-19 15:41:26 +00:00
if (shouldClose) {
2019-03-06 16:36:20 +00:00
// Prevent unnecessary folder connections
if (db.operation().getOperationCount(folder.id, null) == 0)
return;
2019-02-10 19:22:35 +00:00
2019-03-06 16:36:20 +00:00
db.folder().setFolderState(folder.id, "connecting");
2018-10-10 07:26:04 +00:00
2019-07-28 19:45:29 +00:00
ifolder = iservice.getStore().getFolder(folder.name);
2019-03-06 16:36:20 +00:00
ifolder.open(Folder.READ_WRITE);
2018-12-05 10:50:07 +00:00
2019-03-06 16:36:20 +00:00
db.folder().setFolderState(folder.id, "connected");
2018-12-05 10:50:07 +00:00
2019-03-06 16:36:20 +00:00
db.folder().setFolderError(folder.id, null);
}
Core.processOperations(ServiceSynchronize.this,
account, folder,
2019-07-29 09:17:12 +00:00
iservice.getStore(), ifolder,
2019-03-06 16:36:20 +00:00
state);
} catch (FolderNotFoundException ex) {
2019-09-17 19:25:41 +00:00
Log.w(folder.name, ex);
db.folder().deleteFolder(folder.id);
2019-03-06 16:36:20 +00:00
} catch (Throwable ex) {
Log.e(folder.name, ex);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this,
2019-12-06 07:50:46 +00:00
folder.name + " " + Log.formatThrowable(ex, false));
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
2019-04-10 18:17:58 +00:00
state.error(ex);
2019-03-06 16:36:20 +00:00
} finally {
if (shouldClose) {
if (ifolder != null && ifolder.isOpen()) {
db.folder().setFolderState(folder.id, "closing");
try {
ifolder.close(false);
} catch (MessagingException ex) {
Log.w(folder.name, ex);
}
2018-12-02 13:19:54 +00:00
}
2019-03-06 16:36:20 +00:00
if (folder.synchronize && (folder.poll || !capIdle))
db.folder().setFolderState(folder.id, "waiting");
else
db.folder().setFolderState(folder.id, null);
2018-12-02 13:19:54 +00:00
}
}
2019-03-06 16:36:20 +00:00
} finally {
wlFolder.release();
2018-10-10 07:26:04 +00:00
}
2018-12-05 10:50:07 +00:00
}
2019-03-06 16:36:20 +00:00
});
}
2018-12-05 10:50:07 +00:00
}
2019-03-06 16:36:20 +00:00
});
}
});
2018-12-02 13:19:54 +00:00
}
2018-10-10 07:26:04 +00:00
// Keep alive alarm receiver
BroadcastReceiver alarm = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Receiver runs on main thread
// Receiver has a wake lock for ~10 seconds
2018-12-02 13:19:54 +00:00
EntityLog.log(context, account.name + " keep alive wake lock=" + wlAccount.isHeld());
2018-11-28 18:47:16 +00:00
state.release();
2018-10-10 07:26:04 +00:00
}
};
2018-10-07 17:24:21 +00:00
2019-06-20 16:07:56 +00:00
String id = BuildConfig.APPLICATION_ID + ".POLL." + account.id + "." + new Random().nextInt();
2019-01-05 11:17:33 +00:00
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
2018-10-10 07:26:04 +00:00
registerReceiver(alarm, new IntentFilter(id));
2018-10-07 10:13:32 +00:00
2018-10-10 07:26:04 +00:00
// Keep alive
2018-12-09 17:49:52 +00:00
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
2018-10-10 07:26:04 +00:00
try {
2019-08-12 13:50:46 +00:00
while (state.isRunning()) {
if (!state.isRecoverable())
2019-07-28 19:45:29 +00:00
throw new StoreClosedException(iservice.getStore(), "Unrecoverable");
2019-07-08 17:23:07 +00:00
2019-07-08 14:48:59 +00:00
// Sends store NOOP
2019-07-28 19:45:29 +00:00
if (!iservice.getStore().isConnected())
throw new StoreClosedException(iservice.getStore(), "NOOP");
2018-11-28 17:41:02 +00:00
if (sync)
for (EntityFolder folder : mapFolders.keySet())
if (folder.synchronize)
if (!folder.poll && capIdle) {
// Sends folder NOOP
if (!mapFolders.get(folder).isOpen())
throw new StoreClosedException(iservice.getStore(), folder.name);
} else
EntityOperation.sync(this, folder.id, false);
2018-11-28 17:41:02 +00:00
// Successfully connected: reset back off time
backoff = CONNECT_BACKOFF_START;
// Record successful connection
2019-03-05 19:06:57 +00:00
account.last_connected = new Date().getTime();
EntityLog.log(this, account.name + " set last_connected=" + new Date(account.last_connected));
db.account().setAccountConnected(account.id, account.last_connected);
2019-03-27 08:19:11 +00:00
db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle));
2018-11-28 17:41:02 +00:00
2019-02-17 18:06:46 +00:00
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2019-06-23 18:56:07 +00:00
nm.cancel("receive:" + account.id, 1);
2019-02-17 18:06:46 +00:00
2018-10-10 07:26:04 +00:00
// Schedule keep alive alarm
2019-07-08 17:23:07 +00:00
EntityLog.log(this, account.name + " wait=" + account.poll_interval);
AlarmManagerCompat.setAndAllowWhileIdle(am,
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + account.poll_interval * 60 * 1000L,
pi);
2018-10-08 06:18:44 +00:00
2018-10-10 07:26:04 +00:00
try {
2018-12-02 13:19:54 +00:00
wlAccount.release();
2018-11-28 18:47:16 +00:00
state.acquire();
2018-10-10 07:26:04 +00:00
} catch (InterruptedException ex) {
2018-11-28 18:47:16 +00:00
EntityLog.log(this, account.name + " waited state=" + state);
2018-10-10 07:26:04 +00:00
} finally {
2018-12-02 13:19:54 +00:00
wlAccount.acquire();
2018-10-10 07:26:04 +00:00
}
2018-10-07 18:43:44 +00:00
}
2018-10-10 07:26:04 +00:00
} finally {
// Cleanup
am.cancel(pi);
2019-06-21 15:58:41 +00:00
try {
unregisterReceiver(alarm);
} catch (IllegalArgumentException ex) {
2019-09-23 12:42:13 +00:00
Log.e(ex);
2019-06-21 15:58:41 +00:00
}
2018-08-02 13:33:06 +00:00
}
2018-10-08 06:18:44 +00:00
2018-12-24 12:27:45 +00:00
Log.i(account.name + " done state=" + state);
2018-10-10 07:26:04 +00:00
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(account.name, ex);
2019-06-25 06:05:28 +00:00
EntityLog.log(
ServiceSynchronize.this,
2019-12-06 07:50:46 +00:00
account.name + " " + Log.formatThrowable(ex, false));
db.account().setAccountError(account.id, Log.formatThrowable(ex));
2018-08-15 19:34:22 +00:00
} finally {
2019-12-09 07:39:51 +00:00
// Update state
EntityLog.log(this, account.name + " closing");
db.account().setAccountState(account.id, "closing");
// Stop watching for operations
2019-03-30 19:26:09 +00:00
handler.post(new Runnable() {
@Override
public void run() {
for (TwoStateOwner owner : cowners)
owner.destroy();
}
});
2019-12-09 07:42:52 +00:00
// Close folders
2019-05-21 13:21:03 +00:00
for (EntityFolder folder : mapFolders.keySet())
2019-12-09 07:42:52 +00:00
if (folder.synchronize && !folder.poll && mapFolders.get(folder) != null) {
db.folder().setFolderState(folder.id, "closing");
2019-12-09 07:42:52 +00:00
try {
mapFolders.get(folder).close();
} catch (MessagingException ex) {
Log.w(ex);
} finally {
db.folder().setFolderState(folder.id, null);
}
}
2018-09-08 05:42:44 +00:00
2018-10-10 07:26:04 +00:00
// Close store
try {
2019-01-05 11:17:33 +00:00
EntityLog.log(this, account.name + " store closing");
2019-07-28 19:45:29 +00:00
iservice.close();
2019-01-05 11:17:33 +00:00
EntityLog.log(this, account.name + " store closed");
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.w(account.name, ex);
2018-10-10 07:26:04 +00:00
} finally {
EntityLog.log(this, account.name + " closed");
db.account().setAccountState(account.id, null);
2018-09-08 08:43:13 +00:00
}
2018-10-10 08:07:37 +00:00
// Stop idlers
2018-11-28 18:47:16 +00:00
for (Thread idler : idlers)
state.join(idler);
2018-12-02 13:19:54 +00:00
idlers.clear();
2018-08-15 19:34:22 +00:00
}
2018-08-02 13:33:06 +00:00
2019-12-09 07:51:05 +00:00
if (state.isRunning()) {
if (backoff <= CONNECT_BACKOFF_MAX) {
// Short back-off period, keep device awake
EntityLog.log(this, account.name + " backoff=" + backoff);
try {
2018-11-28 18:47:16 +00:00
state.acquire(backoff * 1000L);
2019-12-09 07:51:05 +00:00
} catch (InterruptedException ex) {
Log.w(account.name + " backoff " + ex.toString());
}
} else {
// Long back-off period, let device sleep
EntityLog.log(this, account.name + " backoff alarm=" + CONNECT_BACKOFF_AlARM);
2018-11-08 17:16:32 +00:00
2019-12-09 07:51:05 +00:00
BroadcastReceiver alarm = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
state.release();
}
};
2018-11-08 17:16:32 +00:00
2019-12-09 07:51:05 +00:00
String id = BuildConfig.APPLICATION_ID + ".BACKOFF." + account.id;
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
registerReceiver(alarm, new IntentFilter(id));
2018-11-08 17:16:32 +00:00
2019-12-09 07:51:05 +00:00
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
am.set(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
pi);
else
am.setAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
pi);
2018-11-09 08:54:24 +00:00
2019-12-09 07:51:05 +00:00
try {
wlAccount.release();
state.acquire(2 * CONNECT_BACKOFF_AlARM * 60 * 1000L);
} catch (InterruptedException ex) {
Log.w(account.name + " backoff " + ex.toString());
2018-11-08 17:16:32 +00:00
} finally {
2019-12-09 07:51:05 +00:00
wlAccount.acquire();
}
} finally {
// Cleanup
am.cancel(pi);
try {
unregisterReceiver(alarm);
} catch (IllegalArgumentException ex) {
// Should not happen, but does happen
Log.e(ex);
2018-11-08 17:16:32 +00:00
}
}
2018-10-10 07:26:04 +00:00
}
2019-12-09 07:51:05 +00:00
if (backoff <= CONNECT_BACKOFF_MAX)
backoff *= 2;
}
2018-08-02 13:33:06 +00:00
}
2018-10-10 07:26:04 +00:00
} finally {
EntityLog.log(this, account.name + " stopped");
2018-12-02 13:19:54 +00:00
wlAccount.release();
2018-08-02 13:33:06 +00:00
}
}
2019-10-04 15:17:42 +00:00
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EntityLog.log(ServiceSynchronize.this, "Available network=" + network +
2019-12-07 21:06:40 +00:00
" capabilities " + cm.getNetworkCapabilities(network));
updateState();
}
2018-08-02 13:33:06 +00:00
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities capabilities) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EntityLog.log(ServiceSynchronize.this, "Changed network=" + network +
2019-12-07 21:06:40 +00:00
" capabilities " + cm.getNetworkCapabilities(network));
updateState();
}
2018-08-02 13:33:06 +00:00
@Override
public void onLost(@NonNull Network network) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo active = cm.getActiveNetworkInfo();
EntityLog.log(ServiceSynchronize.this, "Lost network=" + network + " active=" + active);
if (active == null)
lastLost = new Date().getTime();
updateState();
}
private void updateState() {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
liveNetworkState.postValue(ns);
2019-07-29 14:44:11 +00:00
2019-12-07 21:06:40 +00:00
if (lastSuitable == null || lastSuitable != ns.isSuitable()) {
lastSuitable = ns.isSuitable();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2019-12-07 21:06:40 +00:00
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
}
}
2019-03-06 10:18:57 +00:00
};
2019-10-04 15:17:42 +00:00
private BroadcastReceiver connectionChangedReceiver = new BroadcastReceiver() {
2019-09-30 18:44:22 +00:00
@Override
public void onReceive(Context context, Intent intent) {
2019-10-04 15:17:42 +00:00
EntityLog.log(ServiceSynchronize.this, "Received intent=" + intent +
" " + TextUtils.join(" ", Log.getExtras(intent.getExtras())));
2019-10-04 15:17:42 +00:00
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
boolean on = intent.getBooleanExtra("state", false);
if (!on)
lastLost = 0;
}
networkCallback.onCapabilitiesChanged(null, null);
2019-09-30 18:44:22 +00:00
}
};
2019-07-11 06:42:20 +00:00
private BroadcastReceiver onScreenOff = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Received " + intent);
Log.logExtras(intent);
Helper.clearAuthentication(ServiceSynchronize.this);
}
};
2019-03-06 10:18:57 +00:00
static void boot(final Context context) {
2019-08-01 07:08:28 +00:00
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
DB db = DB.getInstance(context);
// Restore notifications
db.message().clearNotifyingMessages();
// Restore snooze timers
for (EntityMessage message : db.message().getSnoozed())
EntityMessage.snooze(context, message.id, message.ui_snoozed);
// Restore schedule
schedule(context);
// Conditionally init service
int accounts = db.account().getSynchronizingAccounts().size();
2019-08-03 14:05:30 +00:00
if (accounts > 0)
eval(context, false, "boot");
2019-08-01 07:08:28 +00:00
else {
for (EntityAccount account : db.account().getAccounts())
db.account().setAccountState(account.id, null);
for (EntityFolder folder : db.folder().getFolders()) {
db.folder().setFolderState(folder.id, null);
db.folder().setFolderSyncState(folder.id, null);
2019-03-17 18:12:53 +00:00
}
2018-10-13 13:30:00 +00:00
}
2019-08-01 07:08:28 +00:00
} catch (Throwable ex) {
Log.e(ex);
2018-11-29 07:35:34 +00:00
}
2019-08-01 07:08:28 +00:00
}
}, "synchronize:boot");
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
thread.start();
}
2019-02-28 07:55:08 +00:00
private static void schedule(Context context) {
Intent alarm = new Intent(context, ServiceSynchronize.class);
alarm.setAction("alarm");
PendingIntent piAlarm;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
piAlarm = PendingIntent.getService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
else
piAlarm = PendingIntent.getForegroundService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
2019-02-28 07:55:08 +00:00
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(piAlarm);
long[] schedule = getSchedule(context);
if (schedule == null)
return;
long now = new Date().getTime();
long next = (now < schedule[0] ? schedule[0] : schedule[1]);
boolean enabled = (now >= schedule[0] && now < schedule[1]);
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, next, piAlarm);
else
am.set(AlarmManager.RTC_WAKEUP, next, piAlarm);
WorkerPoll.init(context);
}
private static long[] getSchedule(Context context) {
2019-02-28 07:55:08 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2019-08-13 08:27:17 +00:00
if (!prefs.getBoolean("schedule", false) || !ActivityBilling.isPro(context))
return null;
2019-02-28 07:55:08 +00:00
int minuteStart = prefs.getInt("schedule_start", 0);
int minuteEnd = prefs.getInt("schedule_end", 0);
if (minuteEnd <= minuteStart)
minuteEnd += 24 * 60;
Calendar calStart = Calendar.getInstance();
calStart.set(Calendar.HOUR_OF_DAY, minuteStart / 60);
calStart.set(Calendar.MINUTE, minuteStart % 60);
calStart.set(Calendar.SECOND, 0);
calStart.set(Calendar.MILLISECOND, 0);
Calendar calEnd = Calendar.getInstance();
calEnd.set(Calendar.HOUR_OF_DAY, minuteEnd / 60);
calEnd.set(Calendar.MINUTE, minuteEnd % 60);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
long now = new Date().getTime();
if (now > calEnd.getTimeInMillis()) {
calStart.set(Calendar.DAY_OF_MONTH, calStart.get(Calendar.DAY_OF_MONTH) + 1);
calEnd.set(Calendar.DAY_OF_MONTH, calEnd.get(Calendar.DAY_OF_MONTH) + 1);
}
if (now > calEnd.getTimeInMillis())
Log.e("Schedule invalid now=" + new Date(now) + " end=" + new Date(calEnd.getTimeInMillis()));
2019-02-28 07:55:08 +00:00
long start = calStart.getTimeInMillis();
long end = calEnd.getTimeInMillis();
if (start > end)
Log.e("Schedule invalid start=" + new Date(start) + " end=" + new Date(end));
2019-04-27 16:07:06 +00:00
return new long[]{start, end};
2019-02-28 07:55:08 +00:00
}
static void eval(Context context, boolean reload, String reason) {
2019-02-14 12:28:03 +00:00
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class)
.setAction("eval")
.putExtra("reload", reload)
.putExtra("reason", reason));
2019-04-17 08:53:15 +00:00
}
static void reschedule(Context context) {
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class)
.setAction("alarm"));
2018-08-02 13:33:06 +00:00
}
2019-04-06 12:52:35 +00:00
static void reset(Context context) {
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class)
.setAction("reset"));
}
2018-08-02 13:33:06 +00:00
}