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
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 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;
|
2020-07-14 19:12:35 +00:00
|
|
|
import android.net.LinkProperties;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.net.Network;
|
|
|
|
import android.net.NetworkCapabilities;
|
2019-12-07 14:14:35 +00:00
|
|
|
import android.net.NetworkInfo;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.net.NetworkRequest;
|
|
|
|
import android.os.Build;
|
2019-12-09 21:26:29 +00:00
|
|
|
import android.os.Bundle;
|
2020-09-21 20:33:06 +00:00
|
|
|
import android.os.OperationCanceledException;
|
2018-10-08 06:18:44 +00:00
|
|
|
import android.os.PowerManager;
|
2019-08-24 07:06:57 +00:00
|
|
|
import android.service.notification.StatusBarNotification;
|
2019-10-01 07:20:05 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-06-12 12:02:14 +00:00
|
|
|
import androidx.annotation.RequiresApi;
|
2019-07-08 17:23:07 +00:00
|
|
|
import androidx.core.app.AlarmManagerCompat;
|
2019-04-17 18:21:44 +00:00
|
|
|
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;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.lifecycle.Observer;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
2020-01-07 16:43:27 +00:00
|
|
|
import com.sun.mail.imap.IMAPStore;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
import java.text.DateFormat;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.ArrayList;
|
2019-12-06 19:58:56 +00:00
|
|
|
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;
|
2019-12-06 19:58:56 +00:00
|
|
|
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;
|
2019-12-08 08:00:00 +00:00
|
|
|
import java.util.Objects;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2020-09-23 06:19:28 +00:00
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
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;
|
2020-01-07 16:43:27 +00:00
|
|
|
import javax.mail.Quota;
|
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
|
|
|
|
2019-09-22 07:45:37 +00:00
|
|
|
import me.leolin.shortcutbadger.ShortcutBadger;
|
|
|
|
|
2018-08-19 07:21:39 +00:00
|
|
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
|
|
|
|
2019-12-06 19:58:56 +00:00
|
|
|
public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2020-09-24 09:14:27 +00:00
|
|
|
private Network lastActive = null;
|
2019-12-07 21:06:40 +00:00
|
|
|
private Boolean lastSuitable = null;
|
2019-12-12 08:19:30 +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
|
|
|
|
2020-07-25 17:54:28 +00:00
|
|
|
private boolean foreground = false;
|
2019-12-12 07:51:48 +00:00
|
|
|
private Map<Long, Core.State> coreStates = new Hashtable<>();
|
2019-12-12 07:56:57 +00:00
|
|
|
private MutableLiveData<ConnectionHelper.NetworkState> liveNetworkState = new MutableLiveData<>();
|
|
|
|
private MutableLiveData<List<TupleAccountState>> liveAccountState = new MutableLiveData<>();
|
|
|
|
private MediatorState liveAccountNetworkState = new MediatorState();
|
2019-12-12 07:51:48 +00:00
|
|
|
|
2020-01-17 07:54:16 +00:00
|
|
|
private static final long QUIT_DELAY = 5 * 1000L; // milliseconds
|
2020-02-22 17:13:44 +00:00
|
|
|
private static final long STILL_THERE_THRESHOLD = 3 * 60 * 1000L; // milliseconds
|
2020-02-23 17:39:42 +00:00
|
|
|
static final int DEFAULT_POLL_INTERVAL = 0; // minutes
|
2020-07-06 06:47:29 +00:00
|
|
|
private static final int OPTIMIZE_KEEP_ALIVE_INTERVAL = 12; // minutes
|
2020-07-06 05:47:20 +00:00
|
|
|
private static final int OPTIMIZE_POLL_INTERVAL = 15; // minutes
|
2018-09-07 13:54:32 +00:00
|
|
|
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
2020-02-27 11:36:02 +00:00
|
|
|
private static final int CONNECT_BACKOFF_MAX = 32; // seconds (totally ~1 minutes)
|
2020-09-29 19:47:45 +00:00
|
|
|
private static final int CONNECT_BACKOFF_ALARM_START = 15; // minutes
|
|
|
|
private static final int CONNECT_BACKOFF_ALARM_MAX = 60; // minutes
|
2019-12-12 08:19:30 +00:00
|
|
|
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
|
2020-10-01 13:46:20 +00:00
|
|
|
private static final int ACCOUNT_ERROR_AFTER_POLL = 4; // times
|
2019-02-12 17:12:25 +00:00
|
|
|
private static final int BACKOFF_ERROR_AFTER = 16; // seconds
|
2020-07-16 15:03:29 +00:00
|
|
|
private static final int FAST_ERROR_COUNT = 3;
|
2020-09-29 19:47:45 +00:00
|
|
|
private static final int FAST_ERROR_BACKOFF = CONNECT_BACKOFF_ALARM_START;
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2020-06-05 06:36:17 +00:00
|
|
|
private static final String ACTION_NEW_MESSAGE_COUNT = BuildConfig.APPLICATION_ID + ".NEW_MESSAGE_COUNT";
|
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
private static final List<String> PREF_EVAL = Collections.unmodifiableList(Arrays.asList(
|
2019-12-10 10:09:36 +00:00
|
|
|
"enabled", "poll_interval" // restart account(s)
|
2019-12-07 14:14:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
private static final List<String> PREF_RELOAD = Collections.unmodifiableList(Arrays.asList(
|
2020-10-25 17:21:20 +00:00
|
|
|
"sync_nodate",
|
|
|
|
"sync_unseen",
|
|
|
|
"sync_flagged",
|
|
|
|
"delete_unseen",
|
|
|
|
"sync_kept",
|
|
|
|
"sync_folders",
|
|
|
|
"sync_shared_folders",
|
2020-10-27 12:22:23 +00:00
|
|
|
"prefer_ip4", "ssl_harden", // force reconnect
|
2019-12-07 19:32:58 +00:00
|
|
|
"badge", "unseen_ignored", // force update badge/widget
|
2020-10-25 17:21:20 +00:00
|
|
|
"protocol", "debug", // force reconnect
|
|
|
|
"auth_plain",
|
|
|
|
"auth_login",
|
|
|
|
"auth_sasl"
|
2019-12-07 14:14:35 +00:00
|
|
|
));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-02-28 07:55:08 +00:00
|
|
|
static final int PI_ALARM = 1;
|
2020-01-10 11:08:14 +00:00
|
|
|
static final int PI_BACKOFF = 2;
|
|
|
|
static final int PI_KEEPALIVE = 3;
|
2018-09-20 09:10:19 +00:00
|
|
|
|
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();
|
2020-05-27 07:22:30 +00:00
|
|
|
|
2020-10-16 11:02:32 +00:00
|
|
|
if (isBackgroundService(this))
|
2020-05-27 07:22:30 +00:00
|
|
|
stopForeground(true);
|
|
|
|
else
|
|
|
|
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);
|
2018-08-10 13:43:54 +00:00
|
|
|
// 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);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2020-06-12 12:02:14 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
registerReceiver(idleModeChangedReceiver, new IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED));
|
|
|
|
|
2020-10-16 11:02:32 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
DB db = DB.getInstance(this);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
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>>() {
|
2020-01-15 11:07:45 +00:00
|
|
|
private boolean fts = false;
|
2020-08-24 12:33:32 +00:00
|
|
|
private int lastEventId = 0;
|
|
|
|
private int lastQuitId = -1;
|
2019-12-06 19:58:56 +00:00
|
|
|
private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
|
2019-12-07 14:14:35 +00:00
|
|
|
private ExecutorService queue = Helper.getBackgroundExecutor(1, "service");
|
2019-12-06 19:58:56 +00:00
|
|
|
|
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) {
|
2019-12-09 21:26:29 +00:00
|
|
|
// Destroy
|
2019-12-12 07:51:48 +00:00
|
|
|
for (TupleAccountNetworkState prev : accountStates)
|
2019-12-07 19:32:58 +00:00
|
|
|
stop(prev);
|
|
|
|
|
2020-01-12 18:28:19 +00:00
|
|
|
quit(null);
|
2019-12-07 19:32:58 +00:00
|
|
|
|
|
|
|
accountStates.clear();
|
2019-12-12 07:51:48 +00:00
|
|
|
coreStates.clear();
|
2019-12-07 19:32:58 +00:00
|
|
|
liveAccountNetworkState.removeObserver(this);
|
|
|
|
} else {
|
2019-12-07 21:06:40 +00:00
|
|
|
int accounts = 0;
|
|
|
|
int operations = 0;
|
2020-08-24 12:33:32 +00:00
|
|
|
boolean event = false;
|
2019-12-07 19:32:58 +00:00
|
|
|
boolean runService = false;
|
|
|
|
for (TupleAccountNetworkState current : accountNetworkStates) {
|
2019-12-18 16:43:28 +00:00
|
|
|
Log.d("### evaluating " + current);
|
2019-12-07 19:32:58 +00:00
|
|
|
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
|
|
|
|
2019-12-10 10:09:36 +00:00
|
|
|
long account = current.command.getLong("account", -1);
|
|
|
|
if (account > 0 && !current.accountState.id.equals(account))
|
|
|
|
continue;
|
|
|
|
|
2019-12-07 19:32:58 +00:00
|
|
|
int index = accountStates.indexOf(current);
|
|
|
|
if (index < 0) {
|
|
|
|
if (current.canRun()) {
|
2020-07-16 14:03:54 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "### new " + current +
|
|
|
|
" start=" + current.canRun() +
|
|
|
|
" sync=" + current.accountState.isEnabled(current.enabled) +
|
|
|
|
" enabled=" + current.accountState.synchronize +
|
|
|
|
" ondemand=" + current.accountState.ondemand +
|
|
|
|
" folders=" + current.accountState.folders +
|
|
|
|
" ops=" + current.accountState.operations +
|
|
|
|
" tbd=" + current.accountState.tbd +
|
|
|
|
" state=" + current.accountState.state +
|
|
|
|
" type=" + current.networkState.getType());
|
2020-08-24 12:33:32 +00:00
|
|
|
event = true;
|
2020-07-12 06:44:50 +00:00
|
|
|
start(current, current.accountState.isEnabled(current.enabled), false);
|
2019-12-07 19:32:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TupleAccountNetworkState prev = accountStates.get(index);
|
2019-12-12 07:51:48 +00:00
|
|
|
Core.State state = coreStates.get(current.accountState.id);
|
2019-12-07 19:32:58 +00:00
|
|
|
if (state != null)
|
|
|
|
state.setNetworkState(current.networkState);
|
|
|
|
|
2019-12-09 21:26:29 +00:00
|
|
|
boolean reload = false;
|
2019-12-18 08:29:38 +00:00
|
|
|
boolean sync = current.command.getBoolean("sync", false);
|
2020-07-12 06:44:50 +00:00
|
|
|
boolean force = current.command.getBoolean("force", false);
|
2019-12-10 10:09:36 +00:00
|
|
|
switch (current.command.getString("name")) {
|
|
|
|
case "reload":
|
|
|
|
reload = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-12-09 21:26:29 +00:00
|
|
|
|
2019-12-10 09:36:59 +00:00
|
|
|
accountStates.remove(index);
|
|
|
|
|
2019-12-08 08:00:00 +00:00
|
|
|
// Some networks disallow email server connections:
|
|
|
|
// - reload on network type change when disconnected
|
2020-01-10 11:08:14 +00:00
|
|
|
if (reload ||
|
2020-03-03 18:22:45 +00:00
|
|
|
prev.canRun() != current.canRun() ||
|
2019-12-08 08:00:00 +00:00
|
|
|
!prev.accountState.equals(current.accountState) ||
|
|
|
|
(!"connected".equals(current.accountState.state) &&
|
|
|
|
!Objects.equals(prev.networkState.getType(), current.networkState.getType()))) {
|
2020-03-03 18:22:45 +00:00
|
|
|
if (prev.canRun() || current.canRun())
|
2019-12-08 08:00:00 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "### changed " + current +
|
2019-12-09 21:26:29 +00:00
|
|
|
" reload=" + reload +
|
2020-07-12 06:44:50 +00:00
|
|
|
" force=" + force +
|
2020-03-03 18:22:45 +00:00
|
|
|
" stop=" + prev.canRun() +
|
2019-12-08 08:00:00 +00:00
|
|
|
" start=" + current.canRun() +
|
2020-08-24 12:33:32 +00:00
|
|
|
" sync=" + sync +
|
|
|
|
" enabled=" + current.accountState.isEnabled(current.enabled) +
|
|
|
|
" should=" + current.accountState.shouldRun(current.enabled) +
|
2019-12-08 08:00:00 +00:00
|
|
|
" changed=" + !prev.accountState.equals(current.accountState) +
|
2020-08-24 12:33:32 +00:00
|
|
|
" synchronize=" + current.accountState.synchronize +
|
2020-02-02 13:58:54 +00:00
|
|
|
" ondemand=" + current.accountState.ondemand +
|
|
|
|
" folders=" + current.accountState.folders +
|
|
|
|
" ops=" + current.accountState.operations +
|
|
|
|
" tbd=" + current.accountState.tbd +
|
2019-12-08 08:00:00 +00:00
|
|
|
" state=" + current.accountState.state +
|
|
|
|
" type=" + prev.networkState.getType() + "/" + current.networkState.getType());
|
2020-08-24 12:33:32 +00:00
|
|
|
if (prev.canRun()) {
|
|
|
|
event = true;
|
2020-01-10 11:08:14 +00:00
|
|
|
stop(prev);
|
2020-08-24 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (current.canRun()) {
|
|
|
|
event = true;
|
2020-07-12 06:44:50 +00:00
|
|
|
start(current, current.accountState.isEnabled(current.enabled) || sync, force);
|
2020-08-24 12:33:32 +00:00
|
|
|
}
|
2019-12-07 19:32:58 +00:00
|
|
|
}
|
2019-12-06 19:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 19:32:58 +00:00
|
|
|
if (current.accountState.tbd == null)
|
|
|
|
accountStates.add(current);
|
2020-08-24 12:33:32 +00:00
|
|
|
else {
|
|
|
|
event = true;
|
2019-12-18 16:43:28 +00:00
|
|
|
delete(current);
|
2020-08-24 12:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
lastEventId++;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "### eventId=" + lastEventId);
|
2019-12-07 19:32:58 +00:00
|
|
|
}
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2020-01-17 07:54:16 +00:00
|
|
|
if (lastAccounts != accounts || lastOperations != operations) {
|
|
|
|
lastAccounts = accounts;
|
|
|
|
lastOperations = operations;
|
|
|
|
if (operations == 0) {
|
|
|
|
fts = true;
|
|
|
|
WorkerFts.init(ServiceSynchronize.this, false);
|
|
|
|
} else if (fts) {
|
|
|
|
fts = false;
|
|
|
|
WorkerFts.cancel(ServiceSynchronize.this);
|
2019-12-07 19:32:58 +00:00
|
|
|
}
|
2020-02-22 21:09:46 +00:00
|
|
|
|
2020-05-27 07:22:30 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
|
|
|
boolean background_service = prefs.getBoolean("background_service", false);
|
|
|
|
if (!background_service)
|
|
|
|
try {
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
|
|
|
|
} catch (Throwable ex) {
|
2020-02-22 21:09:46 +00:00
|
|
|
/*
|
|
|
|
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference
|
|
|
|
at android.app.ApplicationPackageManager.getUserIfProfile(ApplicationPackageManager.java:2167)
|
|
|
|
at android.app.ApplicationPackageManager.getUserBadgeForDensity(ApplicationPackageManager.java:1002)
|
|
|
|
at android.app.Notification$Builder.getProfileBadgeDrawable(Notification.java:2890)
|
|
|
|
at android.app.Notification$Builder.hasThreeLines(Notification.java:3105)
|
|
|
|
at android.app.Notification$Builder.build(Notification.java:3659)
|
|
|
|
at androidx.core.app.NotificationCompatBuilder.buildInternal(SourceFile:355)
|
|
|
|
at androidx.core.app.NotificationCompatBuilder.build(SourceFile:247)
|
|
|
|
at androidx.core.app.NotificationCompat$Builder.build(SourceFile:1677)
|
|
|
|
*/
|
2020-05-27 07:22:30 +00:00
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-01-17 07:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 12:33:32 +00:00
|
|
|
if (!runService && lastQuitId != lastEventId) {
|
|
|
|
lastQuitId = lastEventId;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "### quitting startId=" + lastEventId);
|
|
|
|
quit(lastEventId);
|
|
|
|
}
|
2019-12-06 19:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 06:44:50 +00:00
|
|
|
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync, boolean force) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this,
|
|
|
|
"Service start=" + accountNetworkState + " sync=" + sync + " force=" + force);
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2019-12-07 21:06:40 +00:00
|
|
|
final Core.State astate = new Core.State(accountNetworkState.networkState);
|
2019-12-07 14:14:35 +00:00
|
|
|
astate.runnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2020-07-12 06:44:50 +00:00
|
|
|
monitorAccount(accountNetworkState.accountState, astate, sync, force);
|
2019-12-07 14:14:35 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(accountNetworkState.accountState.name, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "sync.account." + accountNetworkState.accountState.id);
|
2019-12-12 07:51:48 +00:00
|
|
|
coreStates.put(accountNetworkState.accountState.id, astate);
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2019-12-06 19:58:56 +00:00
|
|
|
queue.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-12-07 14:14:35 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
2019-12-09 11:41:00 +00:00
|
|
|
crumb.put("account", accountNetworkState.accountState.id.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()));
|
2019-12-12 08:19:30 +00:00
|
|
|
crumb.put("lastLost", new Date(lastLost).toString());
|
2019-12-07 14:14:35 +00:00
|
|
|
Log.breadcrumb("start", crumb);
|
2019-12-06 19:58:56 +00:00
|
|
|
|
2020-01-10 12:02:46 +00:00
|
|
|
Log.i("### start=" + accountNetworkState + " sync=" + sync);
|
2019-12-06 19:58:56 +00:00
|
|
|
astate.start();
|
2019-12-08 08:00:00 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "### started=" + accountNetworkState);
|
2019-12-06 19:58:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stop(final TupleAccountNetworkState accountNetworkState) {
|
2019-12-07 14:14:35 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service stop=" + accountNetworkState);
|
|
|
|
|
2019-12-12 07:51:48 +00:00
|
|
|
final Core.State state = coreStates.get(accountNetworkState.accountState.id);
|
|
|
|
coreStates.remove(accountNetworkState.accountState.id);
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2019-12-06 19:58:56 +00:00
|
|
|
queue.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-12-07 14:14:35 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
2019-12-09 11:41:00 +00:00
|
|
|
crumb.put("account", accountNetworkState.accountState.id.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()));
|
2019-12-12 08:19:30 +00:00
|
|
|
crumb.put("lastLost", new Date(lastLost).toString());
|
2019-12-07 14:14:35 +00:00
|
|
|
Log.breadcrumb("stop", crumb);
|
|
|
|
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.i("### stop=" + accountNetworkState);
|
2019-12-06 19:58:56 +00:00
|
|
|
state.stop();
|
|
|
|
state.join();
|
2019-12-08 08:00:00 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "### stopped=" + accountNetworkState);
|
2019-12-06 19:58:56 +00:00
|
|
|
}
|
|
|
|
});
|
2019-12-06 15:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
private void delete(final TupleAccountNetworkState accountNetworkState) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service delete=" + accountNetworkState);
|
2019-06-01 06:44:46 +00:00
|
|
|
|
2019-12-07 14:14:35 +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-12-07 14:14:35 +00:00
|
|
|
});
|
|
|
|
}
|
2019-06-01 06:44:46 +00:00
|
|
|
|
2020-08-24 12:33:32 +00:00
|
|
|
private void quit(final Integer eventId) {
|
2019-12-07 14:14:35 +00:00
|
|
|
queue.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2020-08-24 12:33:32 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "### quit eventId=" + eventId);
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2020-08-24 12:33:32 +00:00
|
|
|
if (eventId == null) {
|
2020-01-17 07:54:16 +00:00
|
|
|
// Service destroy
|
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
|
|
|
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
|
|
|
|
for (EntityOperation op : ops)
|
|
|
|
db.folder().setFolderSyncState(op.folder, null);
|
|
|
|
} else {
|
2020-01-19 14:28:04 +00:00
|
|
|
// Yield update notifications/widgets
|
2020-01-17 07:54:16 +00:00
|
|
|
try {
|
|
|
|
Thread.sleep(QUIT_DELAY);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(ex);
|
2020-01-12 18:28:19 +00:00
|
|
|
}
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2020-08-24 12:33:32 +00:00
|
|
|
if (!eventId.equals(lastEventId)) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "### quit cancelled eventId=" + eventId + "/" + lastEventId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:54:16 +00:00
|
|
|
// Stop service
|
2020-08-24 12:33:32 +00:00
|
|
|
stopSelf();
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "### stop self eventId=" + eventId);
|
2020-10-17 13:57:01 +00:00
|
|
|
|
|
|
|
WorkerCleanup.cleanupConditionally(ServiceSynchronize.this);
|
2020-01-12 18:28:19 +00:00
|
|
|
}
|
2019-12-07 14:14:35 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-24 18:06:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-12 10:09:11 +00:00
|
|
|
final TwoStateOwner cowner = new TwoStateOwner(this, "liveUnseenNotify");
|
|
|
|
|
|
|
|
db.folder().liveSynchronizing().observe(this, new Observer<Integer>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(Integer count) {
|
|
|
|
Log.i("Synchronizing folders=" + count);
|
|
|
|
if (count == null || count == 0)
|
|
|
|
cowner.start();
|
|
|
|
else
|
|
|
|
cowner.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-01 09:32:18 +00:00
|
|
|
Map<Long, List<Long>> groupNotifying = new HashMap<>();
|
2019-08-24 07:06:57 +00:00
|
|
|
|
|
|
|
// 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-08-24 07:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
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-08-24 07:06:57 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-08-24 18:06:44 +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 {
|
2020-07-25 17:54:28 +00:00
|
|
|
Core.notifyMessages(ServiceSynchronize.this, messages, groupNotifying, foreground);
|
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
|
|
|
}
|
2020-03-28 09:02:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
db.message().liveWidgetUnseen(null).observe(cowner, new Observer<List<TupleMessageStats>>() {
|
2020-04-25 06:51:50 +00:00
|
|
|
private Integer lastCount = null;
|
2020-03-28 09:02:00 +00:00
|
|
|
private List<TupleMessageStats> last = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(List<TupleMessageStats> stats) {
|
|
|
|
if (stats == null)
|
|
|
|
stats = new ArrayList<>();
|
|
|
|
|
|
|
|
boolean changed = false;
|
|
|
|
if (last == null || last.size() != stats.size())
|
|
|
|
changed = true;
|
|
|
|
else
|
|
|
|
for (int i = 0; i < stats.size(); i++)
|
|
|
|
if (!last.get(i).equals(stats.get(i))) {
|
|
|
|
changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed)
|
|
|
|
return;
|
|
|
|
|
2020-04-20 08:38:45 +00:00
|
|
|
last = stats;
|
|
|
|
|
2020-06-16 09:33:28 +00:00
|
|
|
Widget.update(ServiceSynchronize.this);
|
2020-03-28 09:02:00 +00:00
|
|
|
|
|
|
|
boolean badge = prefs.getBoolean("badge", true);
|
|
|
|
boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false);
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (TupleMessageStats stat : stats) {
|
|
|
|
Integer unseen = (unseen_ignored ? stat.notifying : stat.unseen);
|
|
|
|
if (unseen != null)
|
|
|
|
count += unseen;
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:51:50 +00:00
|
|
|
if (lastCount == null || !lastCount.equals(count)) {
|
|
|
|
lastCount = count;
|
2020-06-05 06:36:17 +00:00
|
|
|
// Broadcast new message count
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(ACTION_NEW_MESSAGE_COUNT);
|
|
|
|
intent.putExtra("count", count);
|
|
|
|
sendBroadcast(intent);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update badge
|
2020-04-25 06:51:50 +00:00
|
|
|
try {
|
|
|
|
if (count == 0 || !badge)
|
|
|
|
ShortcutBadger.removeCount(ServiceSynchronize.this);
|
|
|
|
else
|
|
|
|
ShortcutBadger.applyCount(ServiceSynchronize.this, count);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2020-03-28 09:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
});
|
2019-02-27 20:49:49 +00:00
|
|
|
|
2019-12-01 10:42:54 +00:00
|
|
|
db.message().liveWidgetUnified().observe(cowner, new Observer<List<TupleMessageWidgetCount>>() {
|
2019-11-24 11:33:50 +00:00
|
|
|
private List<TupleMessageWidgetCount> last = null;
|
2019-07-25 15:22:00 +00:00
|
|
|
|
|
|
|
@Override
|
2019-11-24 11:33:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-16 09:33:28 +00:00
|
|
|
if (changed)
|
|
|
|
WidgetUnified.updateData(ServiceSynchronize.this);
|
2019-11-18 09:14:37 +00:00
|
|
|
|
|
|
|
last = current;
|
2019-07-25 15:22:00 +00:00
|
|
|
}
|
|
|
|
});
|
2019-12-06 19:58:56 +00:00
|
|
|
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
2020-07-27 14:33:24 +00:00
|
|
|
if (PREF_EVAL.contains(key) || ConnectionHelper.PREF_NETWORK.contains(key)) {
|
|
|
|
if (ConnectionHelper.PREF_NETWORK.contains(key))
|
2020-07-27 06:02:39 +00:00
|
|
|
updateNetworkState(null, null);
|
|
|
|
|
2019-12-09 21:26:29 +00:00
|
|
|
Bundle command = new Bundle();
|
2020-04-07 09:20:27 +00:00
|
|
|
command.putString("pref", key);
|
2019-12-09 21:26:29 +00:00
|
|
|
command.putString("name", "eval");
|
2019-12-12 10:57:35 +00:00
|
|
|
liveAccountNetworkState.post(command);
|
2019-12-09 21:26:29 +00:00
|
|
|
} else if (PREF_RELOAD.contains(key)) {
|
|
|
|
Bundle command = new Bundle();
|
2020-04-07 09:20:27 +00:00
|
|
|
command.putString("pref", key);
|
2019-12-09 21:26:29 +00:00
|
|
|
command.putString("name", "reload");
|
|
|
|
liveAccountNetworkState.post(command);
|
|
|
|
}
|
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
|
|
|
|
2019-12-06 19:58:56 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.unregisterOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2020-06-12 12:02:14 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
unregisterReceiver(idleModeChangedReceiver);
|
|
|
|
|
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
|
|
|
|
2020-07-03 19:00:15 +00:00
|
|
|
TTSHelper.shutdown();
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:16:02 +00:00
|
|
|
@Override
|
|
|
|
public void onTaskRemoved(Intent rootIntent) {
|
|
|
|
super.onTaskRemoved(rootIntent);
|
|
|
|
Log.i("Task removed=" + rootIntent);
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:26:35 +00:00
|
|
|
@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"));
|
2019-12-08 08:00:00 +00:00
|
|
|
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);
|
2020-05-27 07:22:30 +00:00
|
|
|
|
2020-10-16 11:02:32 +00:00
|
|
|
if (isBackgroundService(this))
|
2020-05-27 07:22:30 +00:00
|
|
|
stopForeground(true);
|
|
|
|
else
|
|
|
|
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-12-12 07:51:48 +00:00
|
|
|
switch (action.split(":")[0]) {
|
2019-12-07 14:14:35 +00:00
|
|
|
case "eval":
|
2019-12-12 07:51:48 +00:00
|
|
|
onEval(intent);
|
2019-02-14 12:28:03 +00:00
|
|
|
break;
|
|
|
|
|
2019-12-09 21:26:29 +00:00
|
|
|
case "reload":
|
2019-12-12 07:51:48 +00:00
|
|
|
onReload(intent);
|
2019-12-09 21:26:29 +00:00
|
|
|
break;
|
|
|
|
|
2019-12-12 10:10:57 +00:00
|
|
|
case "backoff":
|
|
|
|
case "keepalive":
|
2019-12-12 07:51:48 +00:00
|
|
|
onWakeup(intent);
|
2018-12-28 17:21:31 +00:00
|
|
|
break;
|
|
|
|
|
2020-07-25 17:54:28 +00:00
|
|
|
case "state":
|
|
|
|
onState(intent);
|
|
|
|
break;
|
|
|
|
|
2019-12-09 21:26:29 +00:00
|
|
|
case "alarm":
|
2019-12-12 07:51:48 +00:00
|
|
|
onAlarm(intent);
|
2019-05-12 18:15:16 +00:00
|
|
|
break;
|
|
|
|
|
2020-03-25 09:44:50 +00:00
|
|
|
case "watchdog":
|
|
|
|
onWatchdog(intent);
|
|
|
|
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-07 20:03:45 +00:00
|
|
|
|
2019-12-12 07:51:48 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2019-12-12 07:51:48 +00:00
|
|
|
private void onEval(Intent intent) {
|
|
|
|
Bundle command = new Bundle();
|
|
|
|
command.putString("name", "eval");
|
|
|
|
command.putLong("account", intent.getLongExtra("account", -1));
|
|
|
|
liveAccountNetworkState.post(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onReload(Intent intent) {
|
2020-07-12 06:44:50 +00:00
|
|
|
boolean force = intent.getBooleanExtra("force", false);
|
|
|
|
if (force)
|
2020-03-30 11:07:37 +00:00
|
|
|
lastLost = 0;
|
2020-07-12 06:44:50 +00:00
|
|
|
|
2019-12-12 07:51:48 +00:00
|
|
|
Bundle command = new Bundle();
|
|
|
|
command.putString("name", "reload");
|
|
|
|
command.putLong("account", intent.getLongExtra("account", -1));
|
2020-07-12 06:44:50 +00:00
|
|
|
command.putBoolean("force", force);
|
2019-12-12 07:51:48 +00:00
|
|
|
liveAccountNetworkState.post(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onWakeup(Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
long account = Long.parseLong(action.split(":")[1]);
|
|
|
|
Core.State state = coreStates.get(account);
|
2020-08-10 14:45:36 +00:00
|
|
|
|
2019-12-12 07:51:48 +00:00
|
|
|
if (state == null)
|
|
|
|
EntityLog.log(this, "### wakeup missing account=" + account);
|
|
|
|
else {
|
|
|
|
EntityLog.log(this, "### waking up account=" + account);
|
2020-08-11 05:47:08 +00:00
|
|
|
if (!state.release())
|
2020-08-14 07:44:56 +00:00
|
|
|
EntityLog.log(this, "### waking up failed account=" + account);
|
2019-12-12 07:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:54:28 +00:00
|
|
|
private void onState(Intent intent) {
|
|
|
|
foreground = intent.getBooleanExtra("foreground", false);
|
|
|
|
}
|
|
|
|
|
2019-12-12 07:51:48 +00:00
|
|
|
private void onAlarm(Intent intent) {
|
|
|
|
Bundle command = new Bundle();
|
2020-08-29 09:26:47 +00:00
|
|
|
schedule(this, true);
|
2019-12-12 07:51:48 +00:00
|
|
|
command.putString("name", "eval");
|
2019-12-18 08:29:38 +00:00
|
|
|
command.putBoolean("sync", true);
|
2019-12-12 07:51:48 +00:00
|
|
|
liveAccountNetworkState.post(command);
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:44:50 +00:00
|
|
|
private void onWatchdog(Intent intent) {
|
2020-10-25 14:14:38 +00:00
|
|
|
EntityLog.log(this, "Watchdog");
|
2020-08-29 09:26:47 +00:00
|
|
|
schedule(this, false);
|
2020-04-30 09:25:27 +00:00
|
|
|
networkCallback.onCapabilitiesChanged(null, null);
|
2020-03-25 09:44:50 +00:00
|
|
|
}
|
|
|
|
|
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");
|
2020-01-13 10:33:34 +00:00
|
|
|
why.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2019-03-14 14:57:24 +00:00
|
|
|
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)
|
2020-02-10 19:06:55 +00:00
|
|
|
.setDefaults(0) // disable sound on pre Android 8
|
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));
|
2018-08-08 11:41:49 +00:00
|
|
|
|
2019-12-07 21:06:40 +00:00
|
|
|
if (lastSuitable == null || !lastSuitable)
|
2019-07-29 10:01:13 +00:00
|
|
|
builder.setSubText(getString(R.string.title_notification_waiting));
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2019-12-10 10:51:03 +00:00
|
|
|
private NotificationCompat.Builder getNotificationAlert(String account, String message) {
|
|
|
|
// Build pending intent
|
|
|
|
Intent alert = new Intent(this, ActivityView.class);
|
|
|
|
alert.setAction("alert");
|
2020-01-13 10:33:34 +00:00
|
|
|
alert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2019-12-10 10:51:03 +00:00
|
|
|
PendingIntent piAlert = PendingIntent.getActivity(this, ActivityView.REQUEST_ALERT, alert, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
// Build notification
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(this, "alerts")
|
|
|
|
.setSmallIcon(R.drawable.baseline_warning_white_24)
|
|
|
|
.setContentTitle(getString(R.string.title_notification_alert, account))
|
|
|
|
.setContentText(message)
|
|
|
|
.setContentIntent(piAlert)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MAX)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_ERROR)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
|
|
|
.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(message));
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2020-07-12 06:44:50 +00:00
|
|
|
private void monitorAccount(
|
|
|
|
final EntityAccount account, final Core.State state,
|
|
|
|
final boolean sync, final boolean force) 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");
|
2019-12-07 14:14:35 +00:00
|
|
|
|
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
|
|
|
|
2020-03-08 10:08:28 +00:00
|
|
|
final DB db = DB.getInstance(this);
|
|
|
|
|
|
|
|
long thread = Thread.currentThread().getId();
|
|
|
|
Long currentThread = thread;
|
|
|
|
db.account().setAccountThread(account.id, thread);
|
|
|
|
|
2019-12-09 11:43:43 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
if (account.notify)
|
|
|
|
account.createNotificationChannel(ServiceSynchronize.this);
|
|
|
|
else
|
|
|
|
account.deleteNotificationChannel(ServiceSynchronize.this);
|
|
|
|
}
|
|
|
|
|
2019-12-12 08:19:30 +00:00
|
|
|
long ago = new Date().getTime() - lastLost;
|
|
|
|
if (ago < RECONNECT_BACKOFF)
|
|
|
|
try {
|
|
|
|
long backoff = RECONNECT_BACKOFF - ago;
|
2020-04-24 07:49:17 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " reconnect backoff=" + (backoff / 1000));
|
2020-09-23 18:31:37 +00:00
|
|
|
state.acquire(backoff, true);
|
2019-12-12 08:19:30 +00:00
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(account.name + " backoff " + ex.toString());
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:03:29 +00:00
|
|
|
int errors = 0;
|
2019-12-11 10:43:32 +00:00
|
|
|
state.setBackoff(CONNECT_BACKOFF_START);
|
2020-03-08 10:08:28 +00:00
|
|
|
while (state.isRunning() &&
|
|
|
|
currentThread != null && currentThread.equals(thread)) {
|
2020-09-21 19:46:39 +00:00
|
|
|
state.reset();
|
2020-03-08 10:08:28 +00:00
|
|
|
Log.i(account.name + " run thread=" + currentThread);
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
final ObjectHolder<TwoStateOwner> cowner = new ObjectHolder<>();
|
2020-09-23 06:19:28 +00:00
|
|
|
final ExecutorService executor =
|
|
|
|
Helper.getBackgroundExecutor(1, "account_" + account.id);
|
2020-09-22 10:47:49 +00:00
|
|
|
|
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
|
|
|
|
2020-01-29 20:06:45 +00:00
|
|
|
final EmailService iservice = new EmailService(
|
2020-08-23 07:28:10 +00:00
|
|
|
this, account.getProtocol(), account.realm, account.encryption, account.insecure, debug);
|
2019-07-29 09:17:12 +00:00
|
|
|
iservice.setPartialFetch(account.partial_fetch);
|
2019-10-09 12:01:26 +00:00
|
|
|
iservice.setIgnoreBodyStructureSize(account.ignore_size);
|
2019-11-23 12:48:59 +00:00
|
|
|
if (account.protocol != EntityAccount.TYPE_IMAP)
|
2020-01-21 14:15:48 +00:00
|
|
|
iservice.setLeaveOnServer(account.leave_on_server);
|
2020-02-22 14:44:58 +00:00
|
|
|
|
|
|
|
final long start = new Date().getTime();
|
|
|
|
|
2019-12-09 15:32:37 +00:00
|
|
|
iservice.setListener(new StoreListener() {
|
|
|
|
@Override
|
|
|
|
public void notification(StoreEvent e) {
|
2019-12-11 10:43:32 +00:00
|
|
|
String message = e.getMessage();
|
|
|
|
if (TextUtils.isEmpty(message))
|
|
|
|
message = "?";
|
2020-02-22 13:52:26 +00:00
|
|
|
if (e.getMessageType() == StoreEvent.NOTICE) {
|
2019-12-11 10:43:32 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + message);
|
2020-02-22 13:52:26 +00:00
|
|
|
|
2020-10-10 18:31:40 +00:00
|
|
|
// Store NOOP
|
|
|
|
//iservice.getStore().isConnected();
|
|
|
|
|
2020-10-26 10:47:42 +00:00
|
|
|
if ("Still here".equals(message) && !isTransient(account)) {
|
2020-02-22 14:44:58 +00:00
|
|
|
long now = new Date().getTime();
|
2020-03-10 07:44:47 +00:00
|
|
|
if (now - start < STILL_THERE_THRESHOLD)
|
2020-10-26 17:56:34 +00:00
|
|
|
optimizeAccount(account, message);
|
2020-02-22 13:52:26 +00:00
|
|
|
}
|
|
|
|
} else
|
2019-12-09 15:32:37 +00:00
|
|
|
try {
|
|
|
|
wlFolder.acquire();
|
|
|
|
|
2019-12-12 08:33:29 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " alert: " + message);
|
2019-12-09 15:32:37 +00:00
|
|
|
|
2020-03-24 10:29:01 +00:00
|
|
|
if (!ConnectionHelper.isMaxConnections(message))
|
2020-02-22 21:09:46 +00:00
|
|
|
try {
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify("alert:" + account.id, 1,
|
|
|
|
getNotificationAlert(account.name, message).build());
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-12-09 15:32:37 +00:00
|
|
|
} finally {
|
|
|
|
wlFolder.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-03-02 12:55:13 +00:00
|
|
|
|
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) {
|
2019-12-09 11:24:42 +00:00
|
|
|
// Immediately report auth errors
|
2020-01-13 12:18:49 +00:00
|
|
|
if (ex instanceof AuthenticationFailedException) {
|
2020-07-05 16:43:54 +00:00
|
|
|
if (ConnectionHelper.isIoError(ex)) {
|
|
|
|
if (!BuildConfig.PLAY_STORE_RELEASE)
|
|
|
|
Log.e(ex);
|
|
|
|
} else {
|
2020-02-24 09:35:34 +00:00
|
|
|
Log.e(ex);
|
2020-02-22 21:09:46 +00:00
|
|
|
try {
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify("receive:" + account.id, 1,
|
|
|
|
Core.getNotificationError(this, "error", account.name, ex)
|
|
|
|
.build());
|
|
|
|
} catch (Throwable ex1) {
|
|
|
|
Log.w(ex1);
|
|
|
|
}
|
2020-01-13 12:18:49 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
2019-07-28 19:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2020-08-29 10:38:06 +00:00
|
|
|
// https://tools.ietf.org/html/rfc2177
|
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);
|
2020-07-06 06:47:29 +00:00
|
|
|
if (!capIdle || account.poll_interval < OPTIMIZE_KEEP_ALIVE_INTERVAL)
|
2020-10-26 17:56:34 +00:00
|
|
|
optimizeAccount(account, "IDLE");
|
2019-07-28 19:45:29 +00:00
|
|
|
|
|
|
|
db.account().setAccountState(account.id, "connected");
|
|
|
|
db.account().setAccountError(account.id, null);
|
|
|
|
db.account().setAccountWarning(account.id, null);
|
|
|
|
EntityLog.log(this, account.name + " connected");
|
|
|
|
|
2020-07-03 07:57:43 +00:00
|
|
|
db.account().setAccountMaxSize(account.id, iservice.getMaxSize());
|
|
|
|
|
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)
|
2020-03-30 11:07:37 +00:00
|
|
|
reload(ServiceSynchronize.this, account.id, false, "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)
|
2020-03-30 11:07:37 +00:00
|
|
|
reload(ServiceSynchronize.this, account.id, false, "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)
|
2020-03-30 11:07:37 +00:00
|
|
|
reload(ServiceSynchronize.this, account.id, false, "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)
|
2020-07-12 06:44:50 +00:00
|
|
|
Core.onSynchronizeFolders(this, account, iservice.getStore(), state, force);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-12-05 09:25:35 +00:00
|
|
|
// Open synchronizing folders
|
2019-07-07 07:25:52 +00:00
|
|
|
List<EntityFolder> folders = db.folder().getFolders(account.id, false, true);
|
2020-02-01 07:19:41 +00:00
|
|
|
if (folders.size() > 0)
|
|
|
|
Collections.sort(folders, folders.get(0).getComparator(ServiceSynchronize.this));
|
2019-06-24 06:34:17 +00:00
|
|
|
|
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
|
|
|
|
2018-12-05 09:25:35 +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);
|
2019-12-11 10:48:57 +00:00
|
|
|
mapFolders.put(folder, ifolder);
|
2018-12-05 09:25:35 +00:00
|
|
|
try {
|
2019-07-07 07:25:52 +00:00
|
|
|
if (BuildConfig.DEBUG && "Postausgang".equals(folder.name))
|
|
|
|
throw new ReadOnlyFolderException(ifolder);
|
2018-12-05 09:25:35 +00:00
|
|
|
ifolder.open(Folder.READ_WRITE);
|
2019-07-25 05:17:27 +00:00
|
|
|
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);
|
2020-10-20 14:18:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
db.folder().setFolderSynchronize(folder.id, false);
|
2019-05-25 14:45:46 +00:00
|
|
|
continue;
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
2020-10-23 05:39:37 +00:00
|
|
|
if (EntityFolder.INBOX.equals(folder.type))
|
|
|
|
throw ex;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
javax.mail.MessagingException: D2 NO Mailbox does not exist, or must be subscribed to.;
|
|
|
|
nested exception is:
|
|
|
|
com.sun.mail.iap.CommandFailedException: D2 NO Mailbox does not exist, or must be subscribed to.
|
|
|
|
javax.mail.MessagingException: D2 NO Mailbox does not exist, or must be subscribed to.;
|
|
|
|
nested exception is:
|
|
|
|
com.sun.mail.iap.CommandFailedException: D2 NO Mailbox does not exist, or must be subscribed to.
|
|
|
|
at com.sun.mail.imap.IMAPFolder.open(SourceFile:61)
|
|
|
|
at com.sun.mail.imap.IMAPFolder.open(SourceFile:1)
|
|
|
|
at eu.faircode.email.ServiceSynchronize.monitorAccount(SourceFile:63)
|
|
|
|
at eu.faircode.email.ServiceSynchronize.access$900(SourceFile:1)
|
|
|
|
at eu.faircode.email.ServiceSynchronize$4$1.run(SourceFile:1)
|
|
|
|
at java.lang.Thread.run(Thread.java:919)
|
|
|
|
Caused by: com.sun.mail.iap.CommandFailedException: D2 NO Mailbox does not exist, or must be subscribed to.
|
|
|
|
at com.sun.mail.iap.Protocol.handleResult(SourceFile:8)
|
|
|
|
at com.sun.mail.imap.protocol.IMAPProtocol.select(SourceFile:19)
|
|
|
|
at com.sun.mail.imap.IMAPFolder.open(SourceFile:16)
|
|
|
|
*/
|
2018-12-05 09:25:35 +00:00
|
|
|
}
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-12-05 09:25:35 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
|
|
|
db.folder().setFolderError(folder.id, null);
|
2018-11-29 13:00:46 +00:00
|
|
|
|
2020-08-28 10:02:08 +00:00
|
|
|
if (capIdle != MessageHelper.hasCapability(ifolder, "IDLE"))
|
|
|
|
Log.e("Conflicting IDLE=" + capIdle + " host=" + account.host);
|
|
|
|
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2019-03-09 08:20:46 +00:00
|
|
|
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
|
|
|
|
2018-12-05 09:25:35 +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");
|
2018-12-05 09:25:35 +00:00
|
|
|
|
2020-01-25 17:48:06 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
for (Message imessage : e.getMessages()) {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
|
|
|
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2018-12-05 09:25:35 +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));
|
2020-09-21 19:46:39 +00:00
|
|
|
state.error(ex);
|
2018-12-05 09:25:35 +00:00
|
|
|
} finally {
|
2019-05-18 06:02:45 +00:00
|
|
|
wlMessage.release();
|
2018-12-05 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
2020-01-25 17:48:06 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
for (Message imessage : e.getMessages()) {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
2020-08-09 05:35:53 +00:00
|
|
|
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid, true);
|
2020-01-25 17:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2018-12-05 09:25:35 +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));
|
2020-09-21 19:46:39 +00:00
|
|
|
state.error(ex);
|
2018-12-05 09:25:35 +00:00
|
|
|
} finally {
|
2019-05-18 06:02:45 +00:00
|
|
|
wlMessage.release();
|
2018-12-05 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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);
|
2018-12-05 09:25:35 +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));
|
2020-09-21 19:46:39 +00:00
|
|
|
state.error(ex);
|
2018-12-05 09:25:35 +00:00
|
|
|
} 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-12-05 09:25:35 +00:00
|
|
|
});
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2018-12-05 09:25:35 +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");
|
2019-10-21 16:19:11 +00:00
|
|
|
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);
|
2019-12-28 19:35:41 +00:00
|
|
|
state.activity();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
2019-02-21 09:17:16 +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));
|
2020-09-21 19:46:39 +00:00
|
|
|
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-08-21 19:08:51 +00:00
|
|
|
}
|
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);
|
2020-10-26 10:47:42 +00:00
|
|
|
} else {
|
2019-05-21 13:21:03 +00:00
|
|
|
mapFolders.put(folder, null);
|
2020-10-26 10:47:42 +00:00
|
|
|
db.folder().setFolderState(folder.id, null);
|
2020-10-26 12:21:43 +00:00
|
|
|
if (!capIdle && !folder.poll) {
|
|
|
|
folder.poll = true;
|
|
|
|
db.folder().setFolderPoll(folder.id, folder.poll);
|
|
|
|
}
|
2020-10-26 10:47:42 +00:00
|
|
|
}
|
2020-10-04 14:57:19 +00:00
|
|
|
}
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
Log.i(account.name + " observing operations");
|
|
|
|
getMainHandler().post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
cowner.value = new TwoStateOwner(ServiceSynchronize.this, account.name);
|
|
|
|
cowner.value.start();
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
db.operation().liveOperations(account.id).observe(cowner.value, new Observer<List<TupleOperationEx>>() {
|
|
|
|
private List<Long> handling = new ArrayList<>();
|
|
|
|
private final Map<TupleOperationEx.PartitionKey, List<TupleOperationEx>> partitions = new HashMap<>();
|
2020-01-27 09:24:47 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
private final PowerManager.WakeLock wlOperations = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":operations." + account.id);
|
2020-01-27 09:24:47 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(final List<TupleOperationEx> _operations) {
|
|
|
|
// Get new operations
|
|
|
|
List<Long> ops = new ArrayList<>();
|
|
|
|
Map<EntityFolder, List<TupleOperationEx>> added = new HashMap<>();
|
|
|
|
for (TupleOperationEx op : _operations) {
|
|
|
|
if (!handling.contains(op.id)) {
|
|
|
|
boolean found = false;
|
|
|
|
for (EntityFolder folder : mapFolders.keySet())
|
|
|
|
if (Objects.equals(folder.id, op.folder)) {
|
|
|
|
found = true;
|
|
|
|
if (!added.containsKey(folder))
|
|
|
|
added.put(folder, new ArrayList<>());
|
|
|
|
added.get(folder).add(op);
|
|
|
|
break;
|
2020-01-27 09:24:47 +00:00
|
|
|
}
|
2020-10-04 14:57:19 +00:00
|
|
|
if (!found)
|
2020-10-06 13:43:49 +00:00
|
|
|
Log.w(account.name + " folder not found operation=" + op.name);
|
2020-10-04 14:57:19 +00:00
|
|
|
}
|
|
|
|
ops.add(op.id);
|
|
|
|
}
|
|
|
|
handling = ops;
|
|
|
|
|
|
|
|
for (EntityFolder folder : added.keySet()) {
|
|
|
|
Log.i(folder.name + " queuing operations=" + added.size() +
|
|
|
|
" init=" + folder.initialize + " poll=" + folder.poll);
|
|
|
|
|
|
|
|
// Partition operations by priority
|
|
|
|
boolean offline = (mapFolders.get(folder) == null);
|
|
|
|
List<TupleOperationEx.PartitionKey> keys = new ArrayList<>();
|
|
|
|
synchronized (partitions) {
|
|
|
|
for (TupleOperationEx op : added.get(folder)) {
|
|
|
|
TupleOperationEx.PartitionKey key = op.getPartitionKey(offline);
|
|
|
|
|
|
|
|
if (!partitions.containsKey(key)) {
|
|
|
|
partitions.put(key, new ArrayList<>());
|
|
|
|
keys.add(key);
|
2020-01-27 12:12:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
partitions.get(key).add(op);
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 11:58:21 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
Collections.sort(keys, new Comparator<TupleOperationEx.PartitionKey>() {
|
|
|
|
@Override
|
|
|
|
public int compare(TupleOperationEx.PartitionKey k1, TupleOperationEx.PartitionKey k2) {
|
|
|
|
Integer p1 = k1.getPriority();
|
|
|
|
Integer p2 = k2.getPriority();
|
|
|
|
int priority = p1.compareTo(p2);
|
|
|
|
if (priority == 0) {
|
|
|
|
Long o1 = k1.getOrder();
|
|
|
|
Long o2 = k2.getOrder();
|
|
|
|
return o1.compareTo(o2);
|
|
|
|
} else
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
});
|
2020-09-22 12:04:26 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
for (TupleOperationEx.PartitionKey key : keys) {
|
|
|
|
synchronized (partitions) {
|
|
|
|
Log.i(folder.name +
|
|
|
|
" queuing partition=" + key +
|
|
|
|
" operations=" + partitions.get(key).size());
|
|
|
|
}
|
2020-01-27 09:24:47 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
final long sequence = state.getSequence(folder.id, key.getPriority());
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
executor.submit(new Helper.PriorityRunnable(key.getPriority(), key.getOrder()) {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
super.run();
|
|
|
|
try {
|
|
|
|
wlOperations.acquire();
|
|
|
|
|
|
|
|
List<TupleOperationEx> partition;
|
|
|
|
synchronized (partitions) {
|
|
|
|
partition = partitions.get(key);
|
|
|
|
partitions.remove(key);
|
|
|
|
}
|
2018-08-26 08:33:22 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
Log.i(folder.name +
|
|
|
|
" executing partition=" + key +
|
|
|
|
" operations=" + partition.size());
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
// Get folder
|
|
|
|
Folder ifolder = mapFolders.get(folder); // null when polling
|
|
|
|
boolean canOpen = (account.protocol == EntityAccount.TYPE_IMAP || EntityFolder.INBOX.equals(folder.type));
|
|
|
|
final boolean shouldClose = (ifolder == null && canOpen);
|
2019-02-10 19:22:35 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " run " + (shouldClose ? "offline" : "online"));
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
if (shouldClose) {
|
|
|
|
// Prevent unnecessary folder connections
|
|
|
|
if (db.operation().getOperationCount(folder.id, null) == 0)
|
|
|
|
return;
|
2018-12-05 10:50:07 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connecting");
|
2020-09-22 06:37:25 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
try {
|
|
|
|
ifolder = iservice.getStore().getFolder(folder.name);
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
if ("Not connected".equals(ex.getMessage()))
|
|
|
|
return; // Store closed
|
|
|
|
else
|
|
|
|
throw ex;
|
|
|
|
}
|
2018-12-05 10:50:07 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
try {
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
|
|
|
if (ifolder instanceof IMAPFolder)
|
|
|
|
db.folder().setFolderReadOnly(folder.id, ((IMAPFolder) ifolder).getUIDNotSticky());
|
|
|
|
} catch (ReadOnlyFolderException ex) {
|
|
|
|
Log.w(folder.name + " read only");
|
|
|
|
ifolder.open(Folder.READ_ONLY);
|
|
|
|
db.folder().setFolderReadOnly(folder.id, true);
|
|
|
|
}
|
2020-02-11 08:09:31 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
|
|
|
db.folder().setFolderError(folder.id, null);
|
2020-02-11 08:09:31 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2020-01-31 07:45:23 +00:00
|
|
|
|
2020-10-04 14:57:19 +00:00
|
|
|
Log.i(account.name + " folder " + folder.name + " flags=" + ifolder.getPermanentFlags());
|
|
|
|
}
|
|
|
|
|
|
|
|
Core.processOperations(ServiceSynchronize.this,
|
|
|
|
account, folder,
|
|
|
|
partition,
|
|
|
|
iservice.getStore(), ifolder,
|
|
|
|
state, key.getPriority(), sequence);
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
EntityLog.log(
|
|
|
|
ServiceSynchronize.this,
|
|
|
|
folder.name + " " + Log.formatThrowable(ex, false));
|
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
2020-10-20 12:33:29 +00:00
|
|
|
if (!(ex instanceof FolderNotFoundException))
|
|
|
|
state.error(new OperationCanceledException("Process"));
|
2020-10-04 14:57:19 +00:00
|
|
|
} finally {
|
|
|
|
if (shouldClose) {
|
|
|
|
if (ifolder != null && ifolder.isOpen()) {
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
|
|
|
try {
|
|
|
|
ifolder.close(false);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(folder.name, ex);
|
2019-03-06 16:36:20 +00:00
|
|
|
}
|
2018-12-02 13:19:54 +00:00
|
|
|
}
|
2020-10-26 12:21:43 +00:00
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-12-02 13:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-04 14:57:19 +00:00
|
|
|
} finally {
|
|
|
|
wlOperations.release();
|
2020-02-11 15:11:03 +00:00
|
|
|
}
|
2020-10-04 14:57:19 +00:00
|
|
|
}
|
|
|
|
});
|
2019-03-06 16:36:20 +00:00
|
|
|
}
|
2018-12-05 10:50:07 +00:00
|
|
|
}
|
2020-10-04 14:57:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-10-17 10:11:24 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Keep alive
|
2019-12-28 19:35:41 +00:00
|
|
|
boolean first = true;
|
2019-12-09 21:26:29 +00:00
|
|
|
while (state.isRunning()) {
|
2020-03-31 09:33:13 +00:00
|
|
|
long idleTime = state.getIdleTime();
|
2020-06-12 06:36:27 +00:00
|
|
|
boolean tune_keep_alive = prefs.getBoolean("tune_keep_alive", true);
|
|
|
|
boolean tune = (tune_keep_alive && !first &&
|
2020-03-31 14:41:29 +00:00
|
|
|
!account.keep_alive_ok && account.poll_interval > 9 &&
|
2020-03-31 10:33:33 +00:00
|
|
|
Math.abs(idleTime - account.poll_interval * 60 * 1000L) < 60 * 1000L);
|
2020-06-12 06:36:27 +00:00
|
|
|
if (tune_keep_alive && !first && !account.keep_alive_ok)
|
2020-03-31 10:33:33 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name +
|
2020-06-12 06:36:27 +00:00
|
|
|
" Tune interval=" + account.poll_interval +
|
|
|
|
" idle=" + idleTime + "/" + tune);
|
2019-12-28 19:35:41 +00:00
|
|
|
try {
|
|
|
|
if (!state.isRecoverable())
|
|
|
|
throw new StoreClosedException(iservice.getStore(), "Unrecoverable");
|
|
|
|
|
|
|
|
// Sends store NOOP
|
2020-08-14 07:44:56 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " checking store");
|
2019-12-28 19:35:41 +00:00
|
|
|
if (!iservice.getStore().isConnected())
|
|
|
|
throw new StoreClosedException(iservice.getStore(), "NOOP");
|
|
|
|
|
2020-01-19 08:09:50 +00:00
|
|
|
if (!ServiceSynchronize.this.getMainLooper().getThread().isAlive()) {
|
|
|
|
Log.e("App died");
|
2020-01-19 14:28:04 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " app died");
|
2020-01-19 08:09:50 +00:00
|
|
|
state.stop();
|
|
|
|
throw new StoreClosedException(iservice.getStore(), "App died");
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:44:56 +00:00
|
|
|
if (sync) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " checking folders");
|
2019-12-28 19:35:41 +00:00
|
|
|
for (EntityFolder folder : mapFolders.keySet())
|
|
|
|
if (folder.synchronize)
|
|
|
|
if (!folder.poll && capIdle) {
|
|
|
|
// Sends folder NOOP
|
2020-04-22 06:46:18 +00:00
|
|
|
if (!mapFolders.get(folder).isOpen())
|
2019-12-28 19:35:41 +00:00
|
|
|
throw new StoreClosedException(iservice.getStore(), folder.name);
|
2020-02-25 20:06:17 +00:00
|
|
|
} else {
|
|
|
|
if (folder.poll_count == 0)
|
|
|
|
EntityOperation.sync(this, folder.id, false);
|
|
|
|
folder.poll_count = (folder.poll_count + 1) % folder.poll_factor;
|
|
|
|
db.folder().setFolderPollCount(folder.id, folder.poll_count);
|
|
|
|
Log.i(folder.name + " poll count=" + folder.poll_count);
|
|
|
|
}
|
2020-08-14 07:44:56 +00:00
|
|
|
}
|
2020-09-25 09:41:10 +00:00
|
|
|
|
|
|
|
if (!first)
|
|
|
|
errors = 0;
|
2019-12-28 19:35:41 +00:00
|
|
|
} catch (Throwable ex) {
|
2020-06-12 06:36:27 +00:00
|
|
|
if (tune) {
|
2019-12-28 19:35:41 +00:00
|
|
|
account.keep_alive_failed++;
|
2020-04-01 06:57:27 +00:00
|
|
|
account.keep_alive_succeeded = 0;
|
2019-12-29 16:00:38 +00:00
|
|
|
if (account.keep_alive_failed >= 3) {
|
2019-12-28 19:35:41 +00:00
|
|
|
account.keep_alive_failed = 0;
|
2020-10-21 06:33:28 +00:00
|
|
|
account.poll_interval = account.poll_interval - 2;
|
2019-12-28 19:35:41 +00:00
|
|
|
db.account().setAccountKeepAliveInterval(account.id, account.poll_interval);
|
|
|
|
}
|
2020-04-01 06:57:27 +00:00
|
|
|
db.account().setAccountKeepAliveValues(account.id,
|
|
|
|
account.keep_alive_failed, account.keep_alive_succeeded);
|
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " keep alive" +
|
|
|
|
" failed=" + account.keep_alive_failed +
|
|
|
|
" succeeded=" + account.keep_alive_succeeded +
|
|
|
|
" interval=" + account.poll_interval +
|
|
|
|
" idle=" + idleTime);
|
2019-12-28 19:35:41 +00:00
|
|
|
}
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2020-06-12 06:36:27 +00:00
|
|
|
if (tune) {
|
2020-03-31 09:33:13 +00:00
|
|
|
account.keep_alive_failed = 0;
|
2020-04-01 06:57:27 +00:00
|
|
|
account.keep_alive_succeeded++;
|
|
|
|
db.account().setAccountKeepAliveValues(account.id,
|
|
|
|
account.keep_alive_failed, account.keep_alive_succeeded);
|
|
|
|
if (account.keep_alive_succeeded >= 3) {
|
|
|
|
account.keep_alive_ok = true;
|
|
|
|
db.account().setAccountKeepAliveOk(account.id, true);
|
|
|
|
if (!BuildConfig.PLAY_STORE_RELEASE)
|
2020-07-06 10:29:20 +00:00
|
|
|
Log.w(account.host + " set keep-alive=" + account.poll_interval);
|
2020-04-01 06:57:27 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " keep alive ok");
|
|
|
|
} else
|
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " keep alive" +
|
|
|
|
" failed=" + account.keep_alive_failed +
|
|
|
|
" succeeded=" + account.keep_alive_succeeded +
|
|
|
|
" interval=" + account.poll_interval +
|
|
|
|
" idle=" + idleTime);
|
2019-12-28 19:35:41 +00:00
|
|
|
}
|
2019-12-09 21:26:29 +00:00
|
|
|
|
|
|
|
// Successfully connected: reset back off time
|
2019-12-11 10:43:32 +00:00
|
|
|
state.setBackoff(CONNECT_BACKOFF_START);
|
2019-12-09 21:26:29 +00:00
|
|
|
|
|
|
|
// Record successful connection
|
|
|
|
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);
|
|
|
|
db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle));
|
|
|
|
|
2020-01-07 16:43:27 +00:00
|
|
|
// Get quota
|
|
|
|
if (iservice.hasCapability("QUOTA"))
|
|
|
|
try {
|
|
|
|
// https://tools.ietf.org/id/draft-melnikov-extra-quota-00.html
|
|
|
|
Quota[] quotas = ((IMAPStore) iservice.getStore()).getQuota("INBOX");
|
|
|
|
if (quotas != null) {
|
|
|
|
long usage = 0;
|
|
|
|
long limit = 0;
|
|
|
|
for (Quota quota : quotas)
|
|
|
|
if (quota.resources != null)
|
|
|
|
for (Quota.Resource resource : quota.resources) {
|
|
|
|
Log.i("Quota " + resource.name + " " + resource.usage + "/" + resource.limit);
|
|
|
|
if ("STORAGE".equalsIgnoreCase(resource.name)) {
|
|
|
|
usage += resource.usage * 1024;
|
|
|
|
limit = Math.max(limit, resource.limit * 1024);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.account().setAccountQuota(account.id, usage, limit);
|
|
|
|
}
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
db.account().setAccountQuota(account.id, null, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
db.account().setAccountQuota(account.id, null, null);
|
|
|
|
|
2019-12-09 21:26:29 +00:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.cancel("receive:" + account.id, 1);
|
2019-12-10 15:11:49 +00:00
|
|
|
nm.cancel("alert:" + account.id, 1);
|
2019-12-09 21:26:29 +00:00
|
|
|
|
|
|
|
// Schedule keep alive alarm
|
2019-12-12 10:10:57 +00:00
|
|
|
Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
|
|
|
intent.setAction("keepalive:" + account.id);
|
|
|
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
|
|
|
this, PI_KEEPALIVE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2019-12-09 21:26:29 +00:00
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
|
|
|
try {
|
|
|
|
long duration = account.poll_interval * 60 * 1000L;
|
|
|
|
long trigger = System.currentTimeMillis() + duration;
|
|
|
|
EntityLog.log(this, "### " + account.name + " keep alive" +
|
|
|
|
" wait=" + account.poll_interval + " until=" + new Date(trigger));
|
2019-12-12 10:10:57 +00:00
|
|
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, 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();
|
2020-09-23 18:31:37 +00:00
|
|
|
state.acquire(2 * duration, false);
|
2019-12-12 06:34:29 +00:00
|
|
|
Log.i("### " + account.name + " keeping alive");
|
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
|
|
|
}
|
2019-12-09 21:26:29 +00:00
|
|
|
} finally {
|
2019-12-12 10:10:57 +00:00
|
|
|
am.cancel(pi);
|
2019-06-21 15:58:41 +00:00
|
|
|
}
|
2019-12-28 19:35:41 +00:00
|
|
|
|
|
|
|
first = false;
|
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));
|
2020-07-05 16:46:15 +00:00
|
|
|
|
2020-07-16 15:03:29 +00:00
|
|
|
long now = new Date().getTime();
|
|
|
|
|
|
|
|
// Check for fast account errors
|
|
|
|
if (account.last_connected != null &&
|
2020-10-20 05:56:46 +00:00
|
|
|
now - account.last_connected < account.poll_interval * 60 * 1000L / 2) {
|
2020-07-16 15:03:29 +00:00
|
|
|
errors++;
|
|
|
|
EntityLog.log(ServiceSynchronize.this,
|
|
|
|
account.name + " fast errors=" + errors +
|
|
|
|
" last connected: " + new Date(account.last_connected));
|
2020-09-15 14:39:18 +00:00
|
|
|
if (errors >= FAST_ERROR_COUNT)
|
2020-09-25 10:47:31 +00:00
|
|
|
state.setBackoff(FAST_ERROR_BACKOFF * 60);
|
2020-10-06 09:35:35 +00:00
|
|
|
|
|
|
|
boolean auto_optimize = prefs.getBoolean("auto_optimize", false);
|
|
|
|
if (auto_optimize) {
|
|
|
|
Throwable e = ex;
|
|
|
|
while (e != null) {
|
|
|
|
if (ConnectionHelper.isMaxConnections(e.getMessage())) {
|
2020-10-22 11:02:02 +00:00
|
|
|
for (int i = 0; i < EntityFolder.SYSTEM_FOLDER_SYNC.size(); i++)
|
|
|
|
if (EntityFolder.SYSTEM_FOLDER_POLL.get(i)) {
|
|
|
|
String ft = EntityFolder.SYSTEM_FOLDER_SYNC.get(i);
|
|
|
|
EntityFolder f = db.folder().getFolderByType(account.id, ft);
|
|
|
|
if (f != null && f.synchronize) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + "/" + f.name + "=poll");
|
|
|
|
db.folder().setFolderPoll(f.id, true);
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 09:35:35 +00:00
|
|
|
}
|
|
|
|
e = e.getCause();
|
|
|
|
}
|
|
|
|
}
|
2020-07-16 15:03:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:46:15 +00:00
|
|
|
// Report account connection error
|
|
|
|
if (account.last_connected != null && !ConnectionHelper.airplaneMode(this)) {
|
|
|
|
EntityLog.log(this, account.name + " last connected: " + new Date(account.last_connected));
|
|
|
|
|
|
|
|
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
|
|
|
|
long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L;
|
|
|
|
long maxDelayed = (pollInterval > 0 && !account.poll_exempted
|
|
|
|
? pollInterval * ACCOUNT_ERROR_AFTER_POLL : ACCOUNT_ERROR_AFTER) * 60 * 1000L;
|
|
|
|
if (delayed > maxDelayed && state.getBackoff() > 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);
|
|
|
|
try {
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify("receive:" + account.id, 1,
|
|
|
|
Core.getNotificationError(this, "warning", account.name, warning)
|
|
|
|
.build());
|
|
|
|
} catch (Throwable ex1) {
|
|
|
|
Log.w(ex1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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");
|
|
|
|
|
2020-09-22 11:10:46 +00:00
|
|
|
// Stop watching operations
|
|
|
|
Log.i(account.name + " stop watching operations");
|
2020-08-23 15:34:14 +00:00
|
|
|
getMainHandler().post(new Runnable() {
|
2019-03-30 19:26:09 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2020-10-04 14:57:19 +00:00
|
|
|
try {
|
2020-10-05 06:22:16 +00:00
|
|
|
if (cowner.value != null)
|
|
|
|
cowner.value.destroy();
|
2020-10-04 14:57:19 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-03-30 19:26:09 +00:00
|
|
|
}
|
|
|
|
});
|
2018-12-05 09:25:35 +00:00
|
|
|
|
2020-09-22 11:10:46 +00:00
|
|
|
// Stop executing operations
|
|
|
|
Log.i(account.name + " stop executing operations");
|
2020-09-22 12:04:26 +00:00
|
|
|
state.resetBatches();
|
2020-09-23 06:19:28 +00:00
|
|
|
((ThreadPoolExecutor) executor).getQueue().clear();
|
2020-03-04 08:24:24 +00:00
|
|
|
|
2020-09-24 06:12:25 +00:00
|
|
|
// Close folders
|
2020-10-26 10:47:42 +00:00
|
|
|
for (EntityFolder folder : mapFolders.keySet()) {
|
2020-09-24 06:12:25 +00:00
|
|
|
if (folder.synchronize && !folder.poll && mapFolders.get(folder) != null) {
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
|
|
|
try {
|
|
|
|
if (iservice.getStore().isConnected())
|
|
|
|
mapFolders.get(folder).close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 10:47:42 +00:00
|
|
|
db.folder().setFolderState(folder.id, null);
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:12:25 +00:00
|
|
|
// Close store
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2020-09-24 06:20:19 +00:00
|
|
|
db.account().setAccountState(account.id, "closing");
|
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");
|
2018-11-27 15:36:42 +00:00
|
|
|
} 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()) {
|
2019-12-11 10:43:32 +00:00
|
|
|
int backoff = state.getBackoff();
|
2020-05-08 18:10:38 +00:00
|
|
|
long cbackoff = (RECONNECT_BACKOFF - (new Date().getTime() - lastLost)) / 1000L;
|
2020-04-24 07:49:17 +00:00
|
|
|
if (cbackoff > backoff) {
|
2019-12-09 07:51:05 +00:00
|
|
|
try {
|
2020-04-24 07:49:17 +00:00
|
|
|
EntityLog.log(this, account.name + " reconnect backoff=" + cbackoff);
|
2020-09-23 18:31:37 +00:00
|
|
|
state.acquire(cbackoff * 1000L, true);
|
2019-12-09 07:51:05 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2020-04-24 07:49:17 +00:00
|
|
|
Log.w(account.name + " cbackoff " + ex.toString());
|
2019-12-09 07:51:05 +00:00
|
|
|
}
|
2020-04-24 07:49:17 +00:00
|
|
|
state.setBackoff(CONNECT_BACKOFF_START);
|
2019-12-09 07:51:05 +00:00
|
|
|
} else {
|
2020-04-24 07:49:17 +00:00
|
|
|
EntityLog.log(this, account.name + " backoff=" + backoff);
|
|
|
|
if (backoff <= CONNECT_BACKOFF_MAX) {
|
|
|
|
// Short back-off period, keep device awake
|
|
|
|
try {
|
2020-09-23 18:31:37 +00:00
|
|
|
state.acquire(backoff * 1000L, true);
|
2020-04-24 07:49:17 +00:00
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(account.name + " backoff " + ex.toString());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Cancel transient sync operations
|
2020-10-26 10:47:42 +00:00
|
|
|
if (isTransient(account)) {
|
2020-04-24 07:49:17 +00:00
|
|
|
List<EntityOperation> syncs = db.operation().getOperations(account.id, EntityOperation.SYNC);
|
|
|
|
if (syncs != null) {
|
|
|
|
for (EntityOperation op : syncs) {
|
|
|
|
db.folder().setFolderSyncState(op.folder, null);
|
|
|
|
db.operation().deleteOperation(op.id);
|
|
|
|
}
|
|
|
|
Log.i(account.name + " cancelled syncs=" + syncs.size());
|
2020-03-11 08:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 10:10:57 +00:00
|
|
|
|
2020-04-24 07:49:17 +00:00
|
|
|
// Long back-off period, let device sleep
|
|
|
|
Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
|
|
|
intent.setAction("backoff:" + account.id);
|
|
|
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
|
|
|
this, PI_BACKOFF, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-11-09 08:54:24 +00:00
|
|
|
|
2020-04-24 07:49:17 +00:00
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
2019-12-09 07:51:05 +00:00
|
|
|
try {
|
2020-04-24 07:49:17 +00:00
|
|
|
long trigger = System.currentTimeMillis() + backoff * 1000L;
|
|
|
|
EntityLog.log(this, "### " + account.name + " backoff until=" + new Date(trigger));
|
|
|
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi);
|
|
|
|
|
|
|
|
try {
|
|
|
|
wlAccount.release();
|
2020-09-23 18:31:37 +00:00
|
|
|
state.acquire(2 * backoff * 1000L, true);
|
2020-04-24 07:49:17 +00:00
|
|
|
Log.i("### " + account.name + " backoff done");
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(account.name + " backoff " + ex.toString());
|
|
|
|
} finally {
|
|
|
|
wlAccount.acquire();
|
|
|
|
}
|
2018-11-08 17:16:32 +00:00
|
|
|
} finally {
|
2020-04-24 07:49:17 +00:00
|
|
|
am.cancel(pi);
|
2019-12-09 07:51:05 +00:00
|
|
|
}
|
2018-11-08 17:16:32 +00:00
|
|
|
}
|
2019-12-09 07:51:05 +00:00
|
|
|
|
2020-04-24 07:49:17 +00:00
|
|
|
if (backoff < CONNECT_BACKOFF_MAX)
|
|
|
|
state.setBackoff(backoff * 2);
|
|
|
|
else if (backoff == CONNECT_BACKOFF_MAX)
|
2020-09-29 19:47:45 +00:00
|
|
|
state.setBackoff(CONNECT_BACKOFF_ALARM_START * 60);
|
|
|
|
else if (backoff < CONNECT_BACKOFF_ALARM_MAX * 60)
|
2020-04-24 07:49:17 +00:00
|
|
|
state.setBackoff(backoff * 2);
|
|
|
|
}
|
2019-12-09 07:51:05 +00:00
|
|
|
}
|
2020-03-08 10:08:28 +00:00
|
|
|
|
|
|
|
currentThread = Thread.currentThread().getId();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2020-03-08 10:08:28 +00:00
|
|
|
|
|
|
|
if (currentThread == null || !currentThread.equals(thread))
|
|
|
|
Log.e(account.name + " orphan thread id=" + currentThread + "/" + thread);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 10:47:42 +00:00
|
|
|
private boolean isTransient(EntityAccount account) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
|
|
|
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
|
|
|
|
return (!enabled || account.ondemand || (pollInterval > 0 && !account.poll_exempted));
|
|
|
|
}
|
|
|
|
|
2020-10-26 17:56:34 +00:00
|
|
|
private void optimizeAccount(EntityAccount account, String reason) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2020-07-09 06:53:16 +00:00
|
|
|
boolean auto_optimize = prefs.getBoolean("auto_optimize", false);
|
2020-03-10 07:44:47 +00:00
|
|
|
if (!auto_optimize)
|
|
|
|
return;
|
|
|
|
|
2020-10-26 17:56:34 +00:00
|
|
|
DB db = DB.getInstance(this);
|
2020-03-10 07:44:47 +00:00
|
|
|
|
|
|
|
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
|
2020-10-26 17:56:34 +00:00
|
|
|
EntityLog.log(this, "Auto optimize account=" + account.name + " poll interval=" + pollInterval);
|
2020-03-10 07:44:47 +00:00
|
|
|
if (pollInterval == 0) {
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
for (EntityAccount a : db.account().getAccounts())
|
|
|
|
db.account().setAccountPollExempted(a.id, !a.id.equals(account.id));
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-10-25 17:21:20 +00:00
|
|
|
prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply();
|
2020-03-30 16:14:02 +00:00
|
|
|
} else if (pollInterval <= 60 && account.poll_exempted) {
|
2020-03-10 07:44:47 +00:00
|
|
|
db.account().setAccountPollExempted(account.id, false);
|
|
|
|
ServiceSynchronize.eval(ServiceSynchronize.this, "Optimize=" + reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:17:42 +00:00
|
|
|
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
2018-11-27 15:36:42 +00:00
|
|
|
@Override
|
2019-12-07 14:14:35 +00:00
|
|
|
public void onAvailable(@NonNull Network network) {
|
2020-03-28 07:05:01 +00:00
|
|
|
try {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo ni = cm.getNetworkInfo(network);
|
2020-07-15 18:41:58 +00:00
|
|
|
NetworkInfo ani = cm.getActiveNetworkInfo();
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Available network=" + network + " info=" + ni + " active=" + ani);
|
2020-03-28 07:05:01 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-07-15 18:41:58 +00:00
|
|
|
|
2020-07-17 06:06:25 +00:00
|
|
|
updateNetworkState(network, null);
|
2018-11-27 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
2020-07-15 18:41:58 +00:00
|
|
|
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities caps) {
|
2020-07-17 06:06:25 +00:00
|
|
|
updateNetworkState(network, caps);
|
2020-09-24 09:14:27 +00:00
|
|
|
/*
|
2020-07-15 18:41:58 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
try {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
Network active = cm.getActiveNetwork();
|
|
|
|
if (active != null && active.equals(network)) {
|
2020-07-17 06:03:09 +00:00
|
|
|
boolean reload = (!active.equals(reloaded) && lastActiveCaps != null &&
|
2020-07-15 18:41:58 +00:00
|
|
|
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) &&
|
|
|
|
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) &&
|
|
|
|
lastActiveCaps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) &&
|
|
|
|
!lastActiveCaps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED));
|
|
|
|
|
2020-07-17 06:03:09 +00:00
|
|
|
if (reload) {
|
|
|
|
reloaded = active;
|
2020-07-25 05:40:13 +00:00
|
|
|
reload(ServiceSynchronize.this, -1L, false,
|
|
|
|
"Connectivity changed " + network + " caps=" + caps);
|
2020-07-17 06:03:09 +00:00
|
|
|
}
|
2020-07-15 18:41:58 +00:00
|
|
|
|
|
|
|
lastActiveCaps = caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2020-09-24 09:14:27 +00:00
|
|
|
*/
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2020-07-14 19:12:35 +00:00
|
|
|
@Override
|
2020-07-15 18:41:58 +00:00
|
|
|
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties props) {
|
2020-09-24 09:14:27 +00:00
|
|
|
/*
|
2020-07-15 18:41:58 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
try {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
Network active = cm.getActiveNetwork();
|
|
|
|
if (active != null && active.equals(network)) {
|
|
|
|
boolean ahas4 = false;
|
|
|
|
boolean ahas6 = false;
|
|
|
|
boolean lhas4 = false;
|
|
|
|
boolean lhas6 = false;
|
|
|
|
if (lastActiveProps != null) {
|
|
|
|
String aname = props.getInterfaceName();
|
|
|
|
String lname = lastActiveProps.getInterfaceName();
|
|
|
|
if (!TextUtils.isEmpty(aname) && !TextUtils.isEmpty(lname)) {
|
|
|
|
NetworkInterface aintf = NetworkInterface.getByName(aname);
|
|
|
|
NetworkInterface lintf = NetworkInterface.getByName(lname);
|
|
|
|
if (aintf != null && lintf != null) {
|
|
|
|
for (InterfaceAddress iaddr : aintf.getInterfaceAddresses()) {
|
|
|
|
InetAddress addr = iaddr.getAddress();
|
|
|
|
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
|
|
|
|
if (addr instanceof Inet4Address)
|
|
|
|
ahas4 = true;
|
|
|
|
else if (addr instanceof Inet6Address)
|
|
|
|
ahas6 = true;
|
|
|
|
}
|
2020-07-14 19:12:35 +00:00
|
|
|
|
2020-07-15 18:41:58 +00:00
|
|
|
for (InterfaceAddress iaddr : lintf.getInterfaceAddresses()) {
|
|
|
|
InetAddress addr = iaddr.getAddress();
|
|
|
|
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
|
|
|
|
if (addr instanceof Inet4Address)
|
|
|
|
lhas4 = true;
|
|
|
|
else if (addr instanceof Inet6Address)
|
|
|
|
lhas6 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 19:12:35 +00:00
|
|
|
|
2020-07-17 06:03:09 +00:00
|
|
|
boolean reload = (!active.equals(reloaded) &&
|
|
|
|
(ahas4 && !lhas4) || (ahas6 && !lhas6));
|
2020-07-15 18:41:58 +00:00
|
|
|
|
2020-07-17 06:03:09 +00:00
|
|
|
if (reload) {
|
|
|
|
reloaded = active;
|
2020-07-25 05:40:13 +00:00
|
|
|
reload(ServiceSynchronize.this, -1L, false,
|
|
|
|
"Connectivity changed " + network + " props=" + props);
|
2020-07-17 06:03:09 +00:00
|
|
|
}
|
2020-07-15 18:41:58 +00:00
|
|
|
|
|
|
|
lastActiveProps = props;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2020-09-24 09:14:27 +00:00
|
|
|
*/
|
2020-07-14 19:12:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 07:40:18 +00:00
|
|
|
@Override
|
2019-12-07 14:14:35 +00:00
|
|
|
public void onLost(@NonNull Network network) {
|
2020-09-24 09:14:27 +00:00
|
|
|
/*
|
2019-12-13 11:06:28 +00:00
|
|
|
try {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
2020-03-28 07:05:01 +00:00
|
|
|
NetworkInfo ani = cm.getActiveNetworkInfo();
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Lost network=" + network + " active=" + ani);
|
|
|
|
if (ani == null)
|
2019-12-13 11:06:28 +00:00
|
|
|
lastLost = new Date().getTime();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-09-24 09:14:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (Objects.equals(lastActive, network)) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Lost active network=" + network);
|
|
|
|
lastLost = new Date().getTime();
|
|
|
|
}
|
2020-07-15 18:41:58 +00:00
|
|
|
|
2020-07-17 06:06:25 +00:00
|
|
|
updateNetworkState(network, null);
|
2019-07-29 10:01:13 +00:00
|
|
|
}
|
2019-03-06 10:18:57 +00:00
|
|
|
};
|
2018-08-15 07:40:18 +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) {
|
2020-01-15 09:59:48 +00:00
|
|
|
Log.i("Received intent=" + intent +
|
2019-10-01 07:20:05 +00:00
|
|
|
" " + 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)
|
2019-12-12 08:19:30 +00:00
|
|
|
lastLost = 0;
|
2019-10-04 15:17:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 06:06:25 +00:00
|
|
|
updateNetworkState(null, null);
|
2019-09-30 18:44:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-17 06:06:25 +00:00
|
|
|
private void updateNetworkState(Network network, NetworkCapabilities capabilities) {
|
|
|
|
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
|
|
|
|
liveNetworkState.postValue(ns);
|
|
|
|
|
|
|
|
if (lastSuitable == null || lastSuitable != ns.isSuitable()) {
|
|
|
|
lastSuitable = ns.isSuitable();
|
|
|
|
EntityLog.log(ServiceSynchronize.this,
|
|
|
|
"Updated network=" + network +
|
|
|
|
" capabilities " + capabilities +
|
|
|
|
" suitable=" + lastSuitable);
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
|
|
|
boolean background_service = prefs.getBoolean("background_service", false);
|
|
|
|
if (!background_service)
|
|
|
|
try {
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 09:14:27 +00:00
|
|
|
|
|
|
|
Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this);
|
|
|
|
if (!Objects.equals(lastActive, active)) {
|
|
|
|
lastActive = active;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "New active network=" + active);
|
|
|
|
reload(ServiceSynchronize.this, -1L, false, "Network changed active=" + active);
|
|
|
|
}
|
2020-07-17 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 12:02:14 +00:00
|
|
|
private BroadcastReceiver idleModeChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
EntityLog.log(context, "Doze mode=" + pm.isDeviceIdleMode() +
|
|
|
|
" ignoring=" + pm.isIgnoringBatteryOptimizations(context.getPackageName()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-12 07:56:57 +00:00
|
|
|
private class MediatorState extends MediatorLiveData<List<TupleAccountNetworkState>> {
|
|
|
|
boolean running = true;
|
|
|
|
private ConnectionHelper.NetworkState lastNetworkState = null;
|
|
|
|
private List<TupleAccountState> lastAccountStates = null;
|
|
|
|
|
|
|
|
private void post(Bundle command) {
|
|
|
|
Log.i("### command posted");
|
|
|
|
for (String extra : Log.getExtras(command))
|
|
|
|
Log.i("### " + extra);
|
|
|
|
post(command, lastNetworkState, lastAccountStates);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void post(ConnectionHelper.NetworkState networkState) {
|
|
|
|
lastNetworkState = networkState;
|
|
|
|
post(null, lastNetworkState, lastAccountStates);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void post(List<TupleAccountState> accountStates) {
|
|
|
|
lastAccountStates = accountStates;
|
|
|
|
post(null, lastNetworkState, lastAccountStates);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void postDestroy() {
|
|
|
|
if (running) {
|
|
|
|
running = false;
|
|
|
|
postValue(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void post(Bundle command, ConnectionHelper.NetworkState networkState, List<TupleAccountState> accountStates) {
|
|
|
|
if (!running) {
|
|
|
|
Log.i("### not running");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-16 08:51:47 +00:00
|
|
|
if (networkState == null)
|
|
|
|
networkState = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
|
|
|
|
|
|
|
|
if (accountStates == null)
|
2019-12-12 07:56:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
2020-02-23 17:39:42 +00:00
|
|
|
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
|
2019-12-12 07:56:57 +00:00
|
|
|
|
|
|
|
long[] schedule = getSchedule(ServiceSynchronize.this);
|
|
|
|
long now = new Date().getTime();
|
2020-03-19 20:36:56 +00:00
|
|
|
boolean scheduled = (schedule == null || (now >= schedule[0] && now < schedule[1]));
|
2019-12-12 07:56:57 +00:00
|
|
|
|
|
|
|
if (command == null) {
|
|
|
|
command = new Bundle();
|
|
|
|
command.putString("name", "eval");
|
|
|
|
}
|
|
|
|
|
|
|
|
List<TupleAccountNetworkState> result = new ArrayList<>();
|
|
|
|
for (TupleAccountState accountState : accountStates)
|
|
|
|
result.add(new TupleAccountNetworkState(
|
2020-01-12 17:48:26 +00:00
|
|
|
enabled && (pollInterval == 0 || accountState.poll_exempted) && scheduled,
|
2019-12-12 07:56:57 +00:00
|
|
|
command,
|
|
|
|
networkState,
|
|
|
|
accountState));
|
|
|
|
|
|
|
|
postValue(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2020-04-05 11:54:38 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
// Reset accounts
|
|
|
|
for (EntityAccount account : db.account().getAccounts())
|
|
|
|
db.account().setAccountState(account.id, null);
|
|
|
|
|
|
|
|
// reset folders
|
|
|
|
for (EntityFolder folder : db.folder().getFolders()) {
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
2020-10-26 12:34:46 +00:00
|
|
|
db.folder().setFolderPollCount(folder.id, 0);
|
2020-04-05 11:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset operations
|
|
|
|
db.operation().resetOperationStates();
|
|
|
|
|
|
|
|
// Restore notifications
|
|
|
|
db.message().clearNotifyingMessages();
|
|
|
|
|
|
|
|
// Restore snooze timers
|
|
|
|
for (EntityMessage message : db.message().getSnoozed(null))
|
|
|
|
EntityMessage.snooze(context, message.id, message.ui_snoozed);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore schedule
|
2020-08-30 07:34:51 +00:00
|
|
|
schedule(context, false);
|
2020-04-05 11:54:38 +00:00
|
|
|
|
|
|
|
// Init service
|
2019-12-13 07:08:18 +00:00
|
|
|
int accounts = db.account().getSynchronizingAccounts().size();
|
2020-04-05 11:57:58 +00:00
|
|
|
if (accounts > 0) {
|
|
|
|
// Reload: watchdog or user might have started service already
|
|
|
|
reload(context, null, false, "boot");
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-29 09:26:47 +00:00
|
|
|
private static void schedule(Context context, boolean sync) {
|
2020-01-10 11:08:14 +00:00
|
|
|
Intent intent = new Intent(context, ServiceSynchronize.class);
|
|
|
|
intent.setAction("alarm");
|
|
|
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
|
|
|
context, PI_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-28 07:55:08 +00:00
|
|
|
|
|
|
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
2020-01-10 11:08:14 +00:00
|
|
|
am.cancel(pi);
|
2019-02-28 07:55:08 +00:00
|
|
|
|
2020-03-21 08:39:46 +00:00
|
|
|
boolean poll;
|
2020-08-30 07:34:51 +00:00
|
|
|
Long at = null;
|
2019-12-07 14:14:35 +00:00
|
|
|
long[] schedule = getSchedule(context);
|
|
|
|
if (schedule == null)
|
2020-03-21 08:39:46 +00:00
|
|
|
poll = true;
|
2020-01-10 11:12:14 +00:00
|
|
|
else {
|
|
|
|
long now = new Date().getTime();
|
|
|
|
long next = (now < schedule[0] ? schedule[0] : schedule[1]);
|
2020-03-21 08:39:46 +00:00
|
|
|
poll = (now >= schedule[0] && now < schedule[1]);
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2020-01-10 11:12:14 +00:00
|
|
|
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));
|
2020-03-21 08:39:46 +00:00
|
|
|
Log.i("Schedule poll=" + poll);
|
2019-12-07 14:14:35 +00:00
|
|
|
|
2020-01-10 11:12:14 +00:00
|
|
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi);
|
2020-08-29 09:26:47 +00:00
|
|
|
|
2020-08-30 07:34:51 +00:00
|
|
|
if (sync & poll) {
|
|
|
|
at = now + 30 * 1000L;
|
|
|
|
Log.i("Sync at schedule start=" + new Date(at));
|
2020-08-29 09:26:47 +00:00
|
|
|
}
|
2020-01-10 11:12:14 +00:00
|
|
|
}
|
2020-01-10 11:08:14 +00:00
|
|
|
|
2020-08-30 07:34:51 +00:00
|
|
|
ServiceUI.schedule(context, poll, at);
|
2019-12-07 14:14:35 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 08:02:37 +00:00
|
|
|
static long[] getSchedule(Context context) {
|
2019-02-28 07:55:08 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2020-03-19 20:36:56 +00:00
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
|
|
|
boolean schedule = prefs.getBoolean("schedule", false);
|
|
|
|
|
|
|
|
if (!enabled || !schedule)
|
2019-12-23 13:08:04 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
if (!ActivityBilling.isPro(context))
|
2019-12-07 14:14:35 +00:00
|
|
|
return null;
|
2019-02-28 07:55:08 +00:00
|
|
|
|
|
|
|
int minuteStart = prefs.getInt("schedule_start", 0);
|
|
|
|
int minuteEnd = prefs.getInt("schedule_end", 0);
|
|
|
|
|
|
|
|
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);
|
2019-12-23 13:08:04 +00:00
|
|
|
calStart.add(Calendar.DATE, -1);
|
2019-02-28 07:55:08 +00:00
|
|
|
|
|
|
|
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);
|
2019-12-23 13:08:04 +00:00
|
|
|
if (minuteEnd > minuteStart)
|
|
|
|
calEnd.add(Calendar.DATE, -1);
|
2019-02-28 07:55:08 +00:00
|
|
|
|
|
|
|
long now = new Date().getTime();
|
2019-12-23 13:08:04 +00:00
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
int sdow = calStart.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
|
int edow = calEnd.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
|
boolean son = prefs.getBoolean("schedule_day" + sdow, true);
|
|
|
|
boolean eon = prefs.getBoolean("schedule_day" + edow, true);
|
|
|
|
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("@@@ eval dow=" + sdow + "/" + edow +
|
|
|
|
" on=" + son + "/" + eon +
|
|
|
|
" start=" + new Date(calStart.getTimeInMillis()) +
|
|
|
|
" end=" + new Date(calEnd.getTimeInMillis()));
|
|
|
|
|
|
|
|
if ((son || eon) &&
|
|
|
|
now < calEnd.getTimeInMillis() &&
|
|
|
|
(i > 0 || (now >= calStart.getTimeInMillis() && eon))) {
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
if (!son) {
|
|
|
|
calStart.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
calStart.set(Calendar.MINUTE, 0);
|
|
|
|
calStart.add(Calendar.DATE, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eon) {
|
|
|
|
calEnd.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
calEnd.set(Calendar.MINUTE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
calStart.add(Calendar.DATE, 1);
|
|
|
|
calEnd.add(Calendar.DATE, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("@@@ not found");
|
|
|
|
return null;
|
2019-02-28 07:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long start = calStart.getTimeInMillis();
|
|
|
|
long end = calEnd.getTimeInMillis();
|
2019-12-23 13:08:04 +00:00
|
|
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
Log.i("@@@ start=" + new Date(start));
|
|
|
|
Log.i("@@@ end=" + new Date(end));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now > end)
|
|
|
|
Log.e("Schedule invalid now=" + new Date(now) + " end=" + new Date(end));
|
2019-12-07 14:14:35 +00:00
|
|
|
if (start > end)
|
|
|
|
Log.e("Schedule invalid start=" + new Date(start) + " end=" + new Date(end));
|
2019-04-27 16:07:06 +00:00
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
return new long[]{start, end};
|
2019-02-28 07:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:44:27 +00:00
|
|
|
static void eval(Context context, String reason) {
|
2020-05-27 07:22:30 +00:00
|
|
|
start(context,
|
2019-02-14 12:28:03 +00:00
|
|
|
new Intent(context, ServiceSynchronize.class)
|
2019-12-07 14:14:35 +00:00
|
|
|
.setAction("eval")
|
2019-12-09 18:44:27 +00:00
|
|
|
.putExtra("reason", reason));
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:07:37 +00:00
|
|
|
static void reload(Context context, Long account, boolean force, String reason) {
|
2020-05-27 07:22:30 +00:00
|
|
|
start(context,
|
2019-12-09 18:44:27 +00:00
|
|
|
new Intent(context, ServiceSynchronize.class)
|
2019-12-09 21:26:29 +00:00
|
|
|
.setAction("reload")
|
|
|
|
.putExtra("account", account == null ? -1 : account)
|
2020-03-30 11:07:37 +00:00
|
|
|
.putExtra("force", force)
|
2019-12-07 14:14:35 +00:00
|
|
|
.putExtra("reason", reason));
|
2019-04-17 08:53:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
static void reschedule(Context context) {
|
2020-05-27 07:22:30 +00:00
|
|
|
start(context,
|
2018-11-27 15:36:42 +00:00
|
|
|
new Intent(context, ServiceSynchronize.class)
|
2019-12-07 14:14:35 +00:00
|
|
|
.setAction("alarm"));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2020-03-25 09:44:50 +00:00
|
|
|
|
2020-07-25 17:54:28 +00:00
|
|
|
static void state(Context context, boolean foreground) {
|
|
|
|
start(context,
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
.setAction("state")
|
|
|
|
.putExtra("foreground", foreground));
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:44:50 +00:00
|
|
|
static void watchdog(Context context) {
|
2020-06-22 13:17:02 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
|
|
|
if (enabled)
|
|
|
|
start(context,
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
.setAction("watchdog"));
|
2020-03-25 09:44:50 +00:00
|
|
|
}
|
2020-05-27 07:22:30 +00:00
|
|
|
|
|
|
|
private static void start(Context context, Intent intent) {
|
2020-10-16 11:02:32 +00:00
|
|
|
if (isBackgroundService(context))
|
2020-05-27 07:22:30 +00:00
|
|
|
context.startService(intent);
|
|
|
|
else
|
|
|
|
ContextCompat.startForegroundService(context, intent);
|
|
|
|
}
|
2020-10-16 11:02:32 +00:00
|
|
|
|
|
|
|
private static boolean isBackgroundService(Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
return prefs.getBoolean("background_service", false);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|