2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-09 19:39:25 +00:00
|
|
|
import android.app.AlarmManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2018-09-14 13:31:00 +00:00
|
|
|
import android.content.SharedPreferences;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.Network;
|
|
|
|
import android.net.NetworkCapabilities;
|
|
|
|
import android.net.NetworkRequest;
|
|
|
|
import android.os.Build;
|
2018-12-02 13:19:54 +00:00
|
|
|
import android.os.Handler;
|
2018-10-08 06:18:44 +00:00
|
|
|
import android.os.PowerManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
|
import com.sun.mail.imap.IMAPStore;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2018-08-03 12:07:51 +00:00
|
|
|
import java.io.IOException;
|
2018-08-24 18:06:44 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2018-08-04 22:35:47 +00:00
|
|
|
import java.util.HashMap;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.List;
|
2018-08-04 22:35:47 +00:00
|
|
|
import java.util.Map;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2019-03-25 14:05:52 +00:00
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
import javax.mail.FetchProfile;
|
|
|
|
import javax.mail.Folder;
|
2018-08-02 15:25:01 +00:00
|
|
|
import javax.mail.FolderClosedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.Message;
|
2018-08-02 14:40:01 +00:00
|
|
|
import javax.mail.MessageRemovedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.MessagingException;
|
2018-08-15 19:34:22 +00:00
|
|
|
import javax.mail.NoSuchProviderException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.Session;
|
2018-09-03 16:34:38 +00:00
|
|
|
import javax.mail.StoreClosedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.UIDFolder;
|
|
|
|
import javax.mail.event.ConnectionAdapter;
|
|
|
|
import javax.mail.event.ConnectionEvent;
|
|
|
|
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
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-02-22 16:04:53 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.lifecycle.LifecycleService;
|
|
|
|
import androidx.lifecycle.Observer;
|
2019-03-15 13:54:25 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-03-27 09:48:20 +00:00
|
|
|
import me.leolin.shortcutbadger.ShortcutBadger;
|
2018-08-08 06:55:47 +00:00
|
|
|
|
2018-08-19 07:21:39 +00:00
|
|
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
public class ServiceSynchronize extends LifecycleService {
|
2019-03-16 13:12:31 +00:00
|
|
|
private Helper.NetworkState networkState = new Helper.NetworkState();
|
2019-03-06 10:18:57 +00:00
|
|
|
private Core.State state;
|
|
|
|
private boolean started = false;
|
|
|
|
private int queued = 0;
|
|
|
|
private long lastLost = 0;
|
2019-02-27 20:49:49 +00:00
|
|
|
private TupleAccountStats lastStats = new TupleAccountStats();
|
2019-03-06 10:18:57 +00:00
|
|
|
private ExecutorService queue = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-28 07:55:08 +00:00
|
|
|
private static boolean booted = false;
|
|
|
|
|
2018-09-07 13:54:32 +00:00
|
|
|
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
2018-11-08 12:47:34 +00:00
|
|
|
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
|
2018-11-08 17:16:32 +00:00
|
|
|
private static final int CONNECT_BACKOFF_AlARM = 15; // minutes
|
2018-11-27 15:36:42 +00:00
|
|
|
private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds
|
2019-02-13 07:11:20 +00:00
|
|
|
private static final int ACCOUNT_ERROR_AFTER = 90; // minutes
|
2019-02-12 17:12:25 +00:00
|
|
|
private static final int BACKOFF_ERROR_AFTER = 16; // seconds
|
2019-03-05 10:34:00 +00:00
|
|
|
private static final long ONESHOT_DURATION = 60 * 1000L; // milliseconds
|
2019-03-02 07:15:49 +00:00
|
|
|
private static final long STOP_DELAY = 5000L; // milliseconds
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-02-28 07:55:08 +00:00
|
|
|
static final int PI_ALARM = 1;
|
2019-03-05 10:34:00 +00:00
|
|
|
static final int PI_ONESHOT = 2;
|
2018-09-20 09:10:19 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Service create version=" + BuildConfig.VERSION_NAME);
|
2018-08-02 13:33:06 +00:00
|
|
|
super.onCreate();
|
|
|
|
|
|
|
|
// 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-03-06 10:18:57 +00:00
|
|
|
cm.registerNetworkCallback(builder.build(), networkCallback);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
DB db = DB.getInstance(this);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
db.account().liveStats().observe(this, new Observer<TupleAccountStats>() {
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable TupleAccountStats stats) {
|
2018-12-09 17:49:52 +00:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2019-02-15 07:51:14 +00:00
|
|
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(stats).build());
|
2018-08-24 18:06:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-27 13:24:58 +00:00
|
|
|
final TwoStateOwner cowner = new TwoStateOwner(this, "liveUnseenNotify");
|
2019-03-20 14:52:51 +00:00
|
|
|
|
|
|
|
db.folder().liveSynchronizing().observe(this, new Observer<Integer>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(Integer count) {
|
|
|
|
Log.i("Synchronizing folders=" + count);
|
|
|
|
if (count == 0)
|
|
|
|
cowner.start();
|
|
|
|
else
|
|
|
|
cowner.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.message().liveUnseenNotify().observe(cowner, new Observer<List<TupleMessageEx>>() {
|
2018-08-24 18:06:44 +00:00
|
|
|
@Override
|
2019-02-27 18:52:38 +00:00
|
|
|
public void onChanged(List<TupleMessageEx> messages) {
|
2019-02-27 19:04:26 +00:00
|
|
|
Core.notifyMessages(ServiceSynchronize.this, messages);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
});
|
2019-02-27 20:49:49 +00:00
|
|
|
|
2019-03-03 09:48:41 +00:00
|
|
|
WorkerCleanup.queue();
|
2019-03-04 06:56:56 +00:00
|
|
|
JobDaily.cancel(this);
|
2018-10-16 10:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Service destroy");
|
2019-03-25 14:05:52 +00:00
|
|
|
EntityLog.log(this, "Service destroy");
|
2018-10-16 10:26:35 +00:00
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
2019-03-06 10:18:57 +00:00
|
|
|
cm.unregisterNetworkCallback(networkCallback);
|
2018-10-16 10:26:35 +00:00
|
|
|
|
2018-10-21 18:29:28 +00:00
|
|
|
Widget.update(this, -1);
|
2019-04-05 12:38:54 +00:00
|
|
|
try {
|
|
|
|
ShortcutBadger.applyCount(this, 0);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2018-10-21 18:29:28 +00:00
|
|
|
|
2019-03-03 09:48:41 +00:00
|
|
|
WorkerCleanup.cancel();
|
2019-02-27 11:16:38 +00:00
|
|
|
|
2018-10-16 10:26:35 +00:00
|
|
|
stopForeground(true);
|
|
|
|
|
2018-12-09 17:49:52 +00:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2019-02-15 07:51:14 +00:00
|
|
|
nm.cancel(Helper.NOTIFICATION_SYNCHRONIZE);
|
2018-10-16 10:26:35 +00:00
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
String action = (intent == null ? null : intent.getAction());
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Service command intent=" + intent + " action=" + action);
|
2019-02-14 12:28:03 +00:00
|
|
|
Log.logExtras(intent);
|
2018-10-16 10:26:35 +00:00
|
|
|
|
2019-02-15 07:51:14 +00:00
|
|
|
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null).build());
|
2018-10-16 10:26:35 +00:00
|
|
|
|
|
|
|
super.onStartCommand(intent, flags, startId);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-28 17:21:31 +00:00
|
|
|
if (action != null)
|
|
|
|
try {
|
2019-02-28 07:55:08 +00:00
|
|
|
switch (action) {
|
2019-03-02 07:15:49 +00:00
|
|
|
case "init":
|
2019-03-06 10:18:57 +00:00
|
|
|
onInit();
|
2018-12-28 17:21:31 +00:00
|
|
|
break;
|
|
|
|
|
2019-02-28 07:55:08 +00:00
|
|
|
case "alarm":
|
2019-03-06 10:18:57 +00:00
|
|
|
onAlarm();
|
2019-02-14 12:28:03 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-28 17:21:31 +00:00
|
|
|
case "reload":
|
2019-03-06 10:18:57 +00:00
|
|
|
onReload(intent.getStringExtra("reason"));
|
2018-12-28 17:21:31 +00:00
|
|
|
break;
|
|
|
|
|
2019-03-05 10:34:00 +00:00
|
|
|
case "oneshot_start":
|
2019-03-06 10:18:57 +00:00
|
|
|
onOneshot(true);
|
2019-03-05 10:34:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "oneshot_end":
|
2019-03-06 10:18:57 +00:00
|
|
|
onOneshot(false);
|
2019-03-05 10:34:00 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-28 17:21:31 +00:00
|
|
|
default:
|
|
|
|
Log.w("Unknown action: " + action);
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2018-08-22 12:30:27 +00:00
|
|
|
}
|
2018-08-07 20:03:45 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:04:53 +00:00
|
|
|
private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) {
|
2019-02-27 20:49:49 +00:00
|
|
|
if (stats != null)
|
|
|
|
lastStats = stats;
|
2018-10-17 06:11:20 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Build pending intent
|
2019-03-14 14:57:24 +00:00
|
|
|
Intent why = new Intent(this, ActivityView.class);
|
|
|
|
why.setAction("why");
|
|
|
|
PendingIntent piWhy = PendingIntent.getActivity(this, ActivityView.REQUEST_WHY, why, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
// Build notification
|
2019-02-22 16:04:53 +00:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
builder
|
2018-10-06 20:45:07 +00:00
|
|
|
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
2018-10-17 06:11:20 +00:00
|
|
|
.setContentTitle(getResources().getQuantityString(
|
2019-02-27 20:49:49 +00:00
|
|
|
R.plurals.title_notification_synchronizing, lastStats.accounts, lastStats.accounts))
|
2019-03-14 14:57:24 +00:00
|
|
|
.setContentIntent(piWhy)
|
2018-08-02 13:33:06 +00:00
|
|
|
.setAutoCancel(false)
|
2018-08-05 10:56:24 +00:00
|
|
|
.setShowWhen(false)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_MIN)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
2019-02-22 16:04:53 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-27 20:49:49 +00:00
|
|
|
if (lastStats.operations > 0)
|
2019-03-03 09:17:42 +00:00
|
|
|
builder.setContentText(getResources().getQuantityString(
|
|
|
|
R.plurals.title_notification_operations, lastStats.operations, lastStats.operations));
|
2018-08-08 11:41:49 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
private void onInit() {
|
|
|
|
EntityLog.log(this, "Service init");
|
|
|
|
// Network events will manage the service
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onAlarm() {
|
|
|
|
schedule(this);
|
|
|
|
onReload("alarm");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onReload(String reason) {
|
|
|
|
synchronized (this) {
|
|
|
|
try {
|
|
|
|
queue_reload(true, reason);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onOneshot(boolean start) {
|
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
|
|
|
Intent alarm = new Intent(this, ServiceSynchronize.class);
|
|
|
|
alarm.setAction("oneshot_end");
|
|
|
|
PendingIntent piOneshot;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
piOneshot = PendingIntent.getService(this, PI_ONESHOT, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
else
|
|
|
|
piOneshot = PendingIntent.getForegroundService(this, PI_ONESHOT, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
am.cancel(piOneshot);
|
|
|
|
|
|
|
|
if (start) {
|
|
|
|
// Network events will manage the service
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
|
|
|
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot);
|
|
|
|
else
|
|
|
|
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot);
|
|
|
|
} else {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("oneshot", false).apply();
|
|
|
|
queue_reload(true, "oneshot");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void queue_reload(final boolean start, final String reason) {
|
|
|
|
final boolean doStop = started;
|
2019-03-16 13:12:31 +00:00
|
|
|
final boolean doStart = (start && isEnabled() && networkState.isSuitable());
|
2019-03-06 10:18:57 +00:00
|
|
|
|
|
|
|
EntityLog.log(this, "Queue reload" +
|
|
|
|
" doStop=" + doStop + " doStart=" + doStart + " queued=" + queued + " " + reason);
|
|
|
|
|
|
|
|
started = doStart;
|
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
try {
|
|
|
|
queued++;
|
|
|
|
queue.submit(new Runnable() {
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":manage");
|
2019-03-06 10:18:57 +00:00
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
2019-03-06 10:18:57 +00:00
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
try {
|
|
|
|
wl.acquire();
|
2019-03-06 10:18:57 +00:00
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Reload" +
|
|
|
|
" stop=" + doStop + " start=" + doStart + " queued=" + queued + " " + reason);
|
2019-03-06 10:18:57 +00:00
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
if (doStop)
|
|
|
|
stop();
|
2019-03-06 10:18:57 +00:00
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
for (EntityAccount account : db.account().getAccountsTbd())
|
|
|
|
nm.deleteNotificationChannel(EntityAccount.getNotificationChannelId(account.id));
|
|
|
|
}
|
2019-03-06 10:18:57 +00:00
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
int accounts = db.account().deleteAccountsTbd();
|
|
|
|
int identities = db.identity().deleteIdentitiesTbd();
|
|
|
|
if (accounts > 0 || identities > 0)
|
|
|
|
Log.i("Deleted accounts=" + accounts + " identities=" + identities);
|
|
|
|
|
|
|
|
if (doStart)
|
|
|
|
start();
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
} finally {
|
|
|
|
queued--;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Reload done queued=" + queued);
|
|
|
|
|
|
|
|
if (!doStart && queued == 0 && !isEnabled()) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(STOP_DELAY);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
if (!doStart && queued == 0 && !isEnabled()) {
|
|
|
|
queue.shutdownNow();
|
|
|
|
stopService();
|
|
|
|
}
|
2019-03-06 10:18:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 14:05:52 +00:00
|
|
|
wl.release();
|
|
|
|
}
|
2019-03-06 10:18:57 +00:00
|
|
|
}
|
2019-03-25 14:05:52 +00:00
|
|
|
});
|
|
|
|
} catch (RejectedExecutionException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-03-06 10:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isEnabled() {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
|
|
|
boolean oneshot = prefs.getBoolean("oneshot", false);
|
|
|
|
return (enabled || oneshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void start() {
|
|
|
|
EntityLog.log(this, "Main start");
|
|
|
|
|
2019-03-16 13:12:31 +00:00
|
|
|
state = new Core.State(networkState);
|
2019-03-06 10:18:57 +00:00
|
|
|
state.runnable(new Runnable() {
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":main");
|
|
|
|
private List<Core.State> threadState = new ArrayList<>();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
|
|
|
|
final DB db = DB.getInstance(ServiceSynchronize.this);
|
|
|
|
|
|
|
|
long ago = new Date().getTime() - lastLost;
|
|
|
|
if (ago < RECONNECT_BACKOFF)
|
|
|
|
try {
|
|
|
|
long backoff = RECONNECT_BACKOFF - ago;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main backoff=" + (backoff / 1000));
|
|
|
|
if (state.acquire(backoff))
|
|
|
|
return;
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w("main backoff " + ex.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start monitoring accounts
|
|
|
|
List<EntityAccount> accounts = db.account().getSynchronizingAccounts();
|
|
|
|
for (final EntityAccount account : accounts) {
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
|
|
|
|
if (account.notify)
|
|
|
|
account.createNotificationChannel(ServiceSynchronize.this);
|
|
|
|
else
|
|
|
|
account.deleteNotificationChannel(ServiceSynchronize.this);
|
|
|
|
|
|
|
|
Log.i(account.host + "/" + account.user + " run");
|
2019-03-16 13:12:31 +00:00
|
|
|
final Core.State astate = new Core.State(state);
|
2019-03-06 10:18:57 +00:00
|
|
|
astate.runnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
monitorAccount(account, astate);
|
|
|
|
} catch (Throwable ex) {
|
2019-03-19 18:47:45 +00:00
|
|
|
Log.e(account.name, ex);
|
2019-03-06 10:18:57 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " " + Helper.formatThrowable(ex));
|
|
|
|
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "sync.account." + account.id);
|
|
|
|
astate.start();
|
|
|
|
threadState.add(astate);
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main started");
|
|
|
|
|
|
|
|
try {
|
|
|
|
wl.release();
|
|
|
|
state.acquire();
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w("main wait " + ex.toString());
|
|
|
|
} finally {
|
|
|
|
wl.acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop monitoring accounts
|
|
|
|
for (Core.State astate : threadState)
|
|
|
|
astate.stop();
|
|
|
|
for (Core.State astate : threadState)
|
|
|
|
astate.join();
|
|
|
|
threadState.clear();
|
|
|
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main exited");
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
// Fail-safe
|
|
|
|
Log.e(ex);
|
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Start wake lock=" + wl.isHeld());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "sync.main");
|
|
|
|
state.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stop() {
|
|
|
|
EntityLog.log(this, "Main stop");
|
|
|
|
|
|
|
|
state.stop();
|
|
|
|
state.join();
|
|
|
|
|
|
|
|
EntityLog.log(this, "Main stopped");
|
|
|
|
|
|
|
|
state = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopService() {
|
|
|
|
EntityLog.log(this, "Service stop");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(this);
|
|
|
|
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
|
|
|
|
for (EntityOperation op : ops)
|
|
|
|
db.folder().setFolderSyncState(op.folder, null);
|
|
|
|
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
private void monitorAccount(final EntityAccount account, final Core.State state) 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);
|
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
|
|
|
|
|
|
|
final DB db = DB.getInstance(this);
|
|
|
|
|
|
|
|
int backoff = CONNECT_BACKOFF_START;
|
2018-11-28 18:47:16 +00:00
|
|
|
while (state.running()) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " run");
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2019-03-30 19:26:09 +00:00
|
|
|
Handler handler = new Handler(getMainLooper());
|
|
|
|
final List<TwoStateOwner> cowners = new ArrayList<>();
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Debug
|
2019-03-06 10:18:57 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2019-02-28 18:13:28 +00:00
|
|
|
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
|
|
|
|
//System.setProperty("mail.socket.debug", Boolean.toString(debug));
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2019-03-02 12:55:13 +00:00
|
|
|
// Get properties
|
2019-01-10 18:24:20 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
|
2019-03-02 12:55:13 +00:00
|
|
|
|
|
|
|
// Create session
|
2018-10-10 07:26:04 +00:00
|
|
|
final Session isession = Session.getInstance(props, null);
|
|
|
|
isession.setDebug(debug);
|
|
|
|
// adb -t 1 logcat | grep "fairemail\|System.out"
|
|
|
|
|
2019-03-09 16:54:43 +00:00
|
|
|
final IMAPStore istore = (IMAPStore) isession.getStore(account.getProtocol());
|
2019-03-02 12:55:13 +00:00
|
|
|
|
2019-03-25 17:34:48 +00:00
|
|
|
final Map<EntityFolder, IMAPFolder> folders = new HashMap<>();
|
2018-10-10 07:26:04 +00:00
|
|
|
List<Thread> idlers = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
// Listen for store events
|
|
|
|
istore.addStoreListener(new StoreListener() {
|
|
|
|
@Override
|
|
|
|
public void notification(StoreEvent e) {
|
2018-10-10 08:00:42 +00:00
|
|
|
try {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.acquire();
|
2019-03-31 08:12:11 +00:00
|
|
|
String message = e.getMessage();
|
2018-11-29 09:31:54 +00:00
|
|
|
if (e.getMessageType() == StoreEvent.ALERT) {
|
2019-03-31 08:12:11 +00:00
|
|
|
Log.w(account.name + " alert: " + message);
|
|
|
|
db.account().setAccountError(account.id, message);
|
|
|
|
if (BuildConfig.DEBUG ||
|
|
|
|
(message != null && !message.startsWith("Too many simultaneous connections")))
|
|
|
|
Core.reportError(ServiceSynchronize.this, account, null,
|
|
|
|
new Core.AlertException(message));
|
2018-11-29 09:31:54 +00:00
|
|
|
state.error();
|
2018-12-11 18:02:41 +00:00
|
|
|
} else
|
2019-03-31 08:12:11 +00:00
|
|
|
Log.i(account.name + " notice: " + message);
|
2018-10-10 08:00:42 +00:00
|
|
|
} finally {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.release();
|
2018-10-10 08:00:42 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-15 19:34:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Listen for folder events
|
|
|
|
istore.addFolderListener(new FolderAdapter() {
|
|
|
|
@Override
|
|
|
|
public void folderCreated(FolderEvent e) {
|
|
|
|
try {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Folder created=" + e.getFolder().getFullName());
|
2018-11-19 14:35:04 +00:00
|
|
|
reload(ServiceSynchronize.this, "folder created");
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.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 {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Folder renamed=" + e.getFolder().getFullName());
|
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();
|
|
|
|
int count = db.folder().renameFolder(account.id, old, name);
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Renamed to " + name + " count=" + count);
|
2018-09-14 06:28:23 +00:00
|
|
|
|
2018-11-19 14:35:04 +00:00
|
|
|
reload(ServiceSynchronize.this, "folder renamed");
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.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 {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Folder deleted=" + e.getFolder().getFullName());
|
2018-11-19 14:35:04 +00:00
|
|
|
reload(ServiceSynchronize.this, "folder deleted");
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.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
|
|
|
// Listen for connection events
|
|
|
|
istore.addConnectionListener(new ConnectionAdapter() {
|
|
|
|
@Override
|
|
|
|
public void opened(ConnectionEvent e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " opened event");
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-06 16:22:01 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void disconnected(ConnectionEvent e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(account.name + " disconnected event");
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-11 08:28:54 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void closed(ConnectionEvent e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(account.name + " closed event");
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-15 19:34:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Initiate connection
|
2018-11-27 10:43:37 +00:00
|
|
|
EntityLog.log(this, account.name + " connecting");
|
2018-10-10 07:26:04 +00:00
|
|
|
for (EntityFolder folder : db.folder().getFolders(account.id))
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
|
|
|
db.account().setAccountState(account.id, "connecting");
|
2018-11-19 13:14:02 +00:00
|
|
|
|
2018-11-22 07:57:14 +00:00
|
|
|
try {
|
|
|
|
Helper.connect(this, istore, account);
|
|
|
|
} catch (Throwable ex) {
|
2018-11-28 18:47:16 +00:00
|
|
|
// Report account connection error
|
2018-11-22 07:57:14 +00:00
|
|
|
if (account.last_connected != null) {
|
|
|
|
EntityLog.log(this, account.name + " last connected: " + new Date(account.last_connected));
|
|
|
|
long now = new Date().getTime();
|
2019-01-24 18:50:37 +00:00
|
|
|
long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L;
|
2019-02-12 17:12:25 +00:00
|
|
|
if (delayed > ACCOUNT_ERROR_AFTER * 60 * 1000L && backoff > BACKOFF_ERROR_AFTER) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Reporting sync error after=" + delayed);
|
2019-01-23 08:44:25 +00:00
|
|
|
Throwable warning = new Throwable(
|
|
|
|
getString(R.string.title_no_sync,
|
|
|
|
SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
|
2019-03-05 19:06:57 +00:00
|
|
|
.format(account.last_connected)), ex);
|
2018-12-09 17:49:52 +00:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-11-28 17:29:46 +00:00
|
|
|
nm.notify("receive", account.id.intValue(),
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.getNotificationError(this, "warning", account.name, warning, false)
|
2019-02-27 13:03:17 +00:00
|
|
|
.build());
|
2018-11-22 07:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
}
|
2018-11-19 13:14:02 +00:00
|
|
|
|
2019-03-31 08:12:11 +00:00
|
|
|
final boolean capIdle = istore.hasCapability("IDLE");
|
2019-03-08 17:22:13 +00:00
|
|
|
Log.i(account.name + " idle=" + capIdle);
|
2018-11-19 13:14:02 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.account().setAccountState(account.id, "connected");
|
2019-03-30 17:32:25 +00:00
|
|
|
db.account().setAccountError(account.id, null);
|
|
|
|
db.account().setAccountWarning(account.id, null);
|
2018-10-10 07:26:04 +00:00
|
|
|
EntityLog.log(this, account.name + " connected");
|
2018-09-04 05:49:58 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Update folder list
|
2019-02-27 17:02:30 +00:00
|
|
|
Core.onSynchronizeFolders(this, account, istore, state);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-12-05 09:25:35 +00:00
|
|
|
// Open synchronizing folders
|
2018-12-05 15:53:12 +00:00
|
|
|
final ExecutorService pollExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-12-05 09:25:35 +00:00
|
|
|
for (final EntityFolder folder : db.folder().getFolders(account.id)) {
|
|
|
|
if (folder.synchronize && !folder.poll && capIdle) {
|
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-03-25 17:34:48 +00:00
|
|
|
final IMAPFolder ifolder = (IMAPFolder) istore.getFolder(folder.name);
|
2018-12-05 09:25:35 +00:00
|
|
|
try {
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
2018-12-25 14:52:53 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
// Including ReadOnlyFolderException
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-25 14:52:53 +00:00
|
|
|
continue;
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (Throwable ex) {
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 09:25:35 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
folders.put(folder, ifolder);
|
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
|
|
|
|
2019-03-09 08:20:46 +00:00
|
|
|
int count = ifolder.getMessageCount();
|
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
|
|
|
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " folder " + folder.name + " flags=" + ifolder.getPermanentFlags());
|
2018-11-25 12:34:08 +00:00
|
|
|
|
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 {
|
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " messages added");
|
2018-12-05 09:25:35 +00:00
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
fp.add(FetchProfile.Item.FLAGS);
|
|
|
|
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
fp.add(FetchProfile.Item.SIZE);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
|
|
|
|
ifolder.fetch(e.getMessages(), fp);
|
|
|
|
|
|
|
|
for (Message imessage : e.getMessages())
|
|
|
|
try {
|
2019-03-30 13:49:51 +00:00
|
|
|
EntityMessage message = Core.synchronizeMessage(
|
|
|
|
ServiceSynchronize.this,
|
|
|
|
account, folder,
|
|
|
|
ifolder, (IMAPMessage) imessage,
|
|
|
|
false,
|
|
|
|
db.rule().getEnabledRules(folder.id));
|
2018-12-05 09:25:35 +00:00
|
|
|
|
2019-01-19 13:36:23 +00:00
|
|
|
if (db.folder().getFolderDownload(folder.id))
|
2019-03-02 10:49:58 +00:00
|
|
|
Core.downloadMessage(ServiceSynchronize.this,
|
2019-03-25 17:34:48 +00:00
|
|
|
folder, ifolder,
|
2019-03-16 13:12:31 +00:00
|
|
|
(IMAPMessage) imessage, message.id, state);
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (MessageRemovedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-21 10:17:18 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (IOException ex) {
|
2018-12-21 15:50:02 +00:00
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-21 15:50:02 +00:00
|
|
|
} else
|
|
|
|
throw ex;
|
2018-12-21 10:17:18 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 09:25:35 +00:00
|
|
|
}
|
2019-03-09 08:20:46 +00:00
|
|
|
|
|
|
|
int count = ifolder.getMessageCount();
|
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2018-12-05 09:25:35 +00:00
|
|
|
state.error();
|
|
|
|
} finally {
|
|
|
|
wlAccount.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void messagesRemoved(MessageCountEvent e) {
|
|
|
|
try {
|
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " messages removed");
|
2018-12-05 09:25:35 +00:00
|
|
|
for (Message imessage : e.getMessages())
|
|
|
|
try {
|
2019-03-25 17:34:48 +00:00
|
|
|
long uid = ifolder.getUID(imessage);
|
2018-12-05 09:25:35 +00:00
|
|
|
|
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
|
|
|
|
2019-01-28 16:54:14 +00:00
|
|
|
Log.i(folder.name + " deleted uid=" + uid + " count=" + count);
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (MessageRemovedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-05 09:25:35 +00:00
|
|
|
}
|
2019-03-09 08:20:46 +00:00
|
|
|
|
|
|
|
int count = ifolder.getMessageCount();
|
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2018-12-05 09:25:35 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 09:25:35 +00:00
|
|
|
state.error();
|
|
|
|
} finally {
|
|
|
|
wlAccount.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Flags (like "seen") at the remote could be changed while synchronizing
|
|
|
|
|
|
|
|
// Listen for changed messages
|
|
|
|
ifolder.addMessageChangedListener(new MessageChangedListener() {
|
|
|
|
@Override
|
|
|
|
public void messageChanged(MessageChangedEvent e) {
|
|
|
|
try {
|
|
|
|
wlAccount.acquire();
|
2018-12-02 13:19:54 +00:00
|
|
|
try {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " message changed");
|
2018-12-05 09:25:35 +00:00
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.FLAGS);
|
|
|
|
ifolder.fetch(new Message[]{e.getMessage()}, fp);
|
|
|
|
|
2019-03-30 13:49:51 +00:00
|
|
|
EntityMessage message = Core.synchronizeMessage(
|
|
|
|
ServiceSynchronize.this,
|
|
|
|
account, folder,
|
|
|
|
ifolder, (IMAPMessage) e.getMessage(),
|
|
|
|
false,
|
|
|
|
db.rule().getEnabledRules(folder.id));
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2019-01-19 13:36:23 +00:00
|
|
|
if (db.folder().getFolderDownload(folder.id))
|
2019-03-02 10:49:58 +00:00
|
|
|
Core.downloadMessage(ServiceSynchronize.this,
|
2019-03-25 17:34:48 +00:00
|
|
|
folder, ifolder,
|
2019-03-16 13:12:31 +00:00
|
|
|
(IMAPMessage) e.getMessage(), message.id, state);
|
2018-12-02 13:19:54 +00:00
|
|
|
} catch (MessageRemovedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-21 10:17:18 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
2018-12-02 13:19:54 +00:00
|
|
|
} catch (IOException ex) {
|
2018-12-21 15:50:02 +00:00
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-21 15:50:02 +00:00
|
|
|
} else
|
|
|
|
throw ex;
|
2018-12-21 10:17:18 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-08-15 19:34:22 +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-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2018-12-05 09:25:35 +00:00
|
|
|
state.error();
|
|
|
|
} finally {
|
|
|
|
wlAccount.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");
|
2018-11-29 11:06:09 +00:00
|
|
|
while (state.running()) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " do idle");
|
2019-03-25 17:34:48 +00:00
|
|
|
ifolder.idle(false);
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
2019-02-21 09:17:16 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-11-28 18:47:16 +00:00
|
|
|
state.error();
|
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-03-02 07:43:12 +00:00
|
|
|
EntityOperation.sync(this, folder.id, false);
|
2018-12-05 09:25:35 +00:00
|
|
|
} else
|
|
|
|
folders.put(folder, null);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2019-03-30 19:26:09 +00:00
|
|
|
handler.post(new Runnable() {
|
2018-12-02 13:19:54 +00:00
|
|
|
@Override
|
2019-03-06 16:36:20 +00:00
|
|
|
public void run() {
|
2019-03-30 19:26:09 +00:00
|
|
|
TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name);
|
|
|
|
cowners.add(cowner);
|
|
|
|
cowner.start();
|
|
|
|
|
2019-03-30 06:39:26 +00:00
|
|
|
db.operation().liveOperations(folder.id).observe(cowner, new Observer<List<EntityOperation>>() {
|
2019-03-06 16:36:20 +00:00
|
|
|
private List<Long> handling = new ArrayList<>();
|
|
|
|
private final ExecutorService folderExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
|
|
|
private final PowerManager.WakeLock wlFolder = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":folder." + folder.id);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(final List<EntityOperation> operations) {
|
|
|
|
boolean process = false;
|
|
|
|
List<Long> ops = new ArrayList<>();
|
|
|
|
for (EntityOperation op : operations) {
|
|
|
|
if (!handling.contains(op.id))
|
|
|
|
process = true;
|
|
|
|
ops.add(op.id);
|
2019-03-05 12:46:25 +00:00
|
|
|
}
|
2019-03-06 16:36:20 +00:00
|
|
|
handling = ops;
|
|
|
|
|
|
|
|
if (handling.size() > 0 && process) {
|
|
|
|
Log.i(folder.name + " operations=" + operations.size());
|
|
|
|
(folder.poll ? pollExecutor : folderExecutor).submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-12-05 10:50:07 +00:00
|
|
|
try {
|
2019-03-06 16:36:20 +00:00
|
|
|
wlFolder.acquire();
|
|
|
|
Log.i(folder.name + " process");
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
// Get folder
|
|
|
|
Folder ifolder = folders.get(folder); // null when polling
|
|
|
|
final boolean shouldClose = (ifolder == null);
|
2018-08-26 08:33:22 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " run " + (shouldClose ? "offline" : "online"));
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
if (ifolder == null) {
|
|
|
|
// Prevent unnecessary folder connections
|
|
|
|
if (db.operation().getOperationCount(folder.id, null) == 0)
|
|
|
|
return;
|
2019-02-10 19:22:35 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connecting");
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
ifolder = istore.getFolder(folder.name);
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
2018-12-05 10:50:07 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
2018-12-05 10:50:07 +00:00
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
db.folder().setFolderError(folder.id, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Core.processOperations(ServiceSynchronize.this,
|
|
|
|
account, folder,
|
|
|
|
isession, istore, ifolder,
|
|
|
|
state);
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
|
|
|
state.error();
|
|
|
|
} finally {
|
|
|
|
if (shouldClose) {
|
|
|
|
if (ifolder != null && ifolder.isOpen()) {
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
|
|
|
try {
|
|
|
|
ifolder.close(false);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
}
|
2018-12-02 13:19:54 +00:00
|
|
|
}
|
2019-03-06 16:36:20 +00:00
|
|
|
if (folder.synchronize && (folder.poll || !capIdle))
|
|
|
|
db.folder().setFolderState(folder.id, "waiting");
|
|
|
|
else
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-12-02 13:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 16:36:20 +00:00
|
|
|
} finally {
|
|
|
|
wlFolder.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-12-05 10:50:07 +00:00
|
|
|
}
|
2019-03-06 16:36:20 +00:00
|
|
|
});
|
|
|
|
}
|
2018-12-05 10:50:07 +00:00
|
|
|
}
|
2019-03-06 16:36:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-02 13:19:54 +00:00
|
|
|
}
|
2018-10-17 10:11:24 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Keep alive alarm receiver
|
|
|
|
BroadcastReceiver alarm = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
// Receiver runs on main thread
|
|
|
|
// Receiver has a wake lock for ~10 seconds
|
2018-12-02 13:19:54 +00:00
|
|
|
EntityLog.log(context, account.name + " keep alive wake lock=" + wlAccount.isHeld());
|
2018-11-28 18:47:16 +00:00
|
|
|
state.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-07 17:24:21 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
String id = BuildConfig.APPLICATION_ID + ".POLL." + account.id;
|
2019-01-05 11:17:33 +00:00
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
|
2018-10-10 07:26:04 +00:00
|
|
|
registerReceiver(alarm, new IntentFilter(id));
|
2018-10-07 10:13:32 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Keep alive
|
2018-12-09 17:49:52 +00:00
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-11-29 09:56:12 +00:00
|
|
|
while (state.running()) {
|
2019-03-25 17:34:48 +00:00
|
|
|
if (!istore.isConnected()) // Sends store NOOP
|
2018-11-28 17:41:02 +00:00
|
|
|
throw new StoreClosedException(istore);
|
|
|
|
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-12-05 09:25:35 +00:00
|
|
|
if (folder.synchronize)
|
|
|
|
if (!folder.poll && capIdle) {
|
2019-03-25 17:34:48 +00:00
|
|
|
if (!folders.get(folder).isOpen()) // Sends folder NOOP
|
2018-12-05 09:25:35 +00:00
|
|
|
throw new FolderClosedException(folders.get(folder));
|
2019-03-25 20:05:12 +00:00
|
|
|
} else
|
2019-03-02 07:43:12 +00:00
|
|
|
EntityOperation.sync(this, folder.id, false);
|
2018-11-28 17:41:02 +00:00
|
|
|
|
|
|
|
// Successfully connected: reset back off time
|
|
|
|
backoff = CONNECT_BACKOFF_START;
|
|
|
|
|
|
|
|
// Record successful connection
|
2019-03-05 19:06:57 +00:00
|
|
|
account.last_connected = new Date().getTime();
|
|
|
|
EntityLog.log(this, account.name + " set last_connected=" + new Date(account.last_connected));
|
|
|
|
db.account().setAccountConnected(account.id, account.last_connected);
|
2019-03-27 08:19:11 +00:00
|
|
|
db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle));
|
2018-11-28 17:41:02 +00:00
|
|
|
|
2019-02-17 18:06:46 +00:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.cancel("receive", account.id.intValue());
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Schedule keep alive alarm
|
|
|
|
EntityLog.log(this, account.name + " wait=" + account.poll_interval);
|
2018-12-09 17:49:52 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
|
|
|
am.set(
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
System.currentTimeMillis() + account.poll_interval * 60 * 1000L,
|
|
|
|
pi);
|
|
|
|
else
|
|
|
|
am.setAndAllowWhileIdle(
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
System.currentTimeMillis() + account.poll_interval * 60 * 1000L,
|
|
|
|
pi);
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.release();
|
2018-11-28 18:47:16 +00:00
|
|
|
state.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2018-11-28 18:47:16 +00:00
|
|
|
EntityLog.log(this, account.name + " waited state=" + state);
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-10-07 18:43:44 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
// Cleanup
|
|
|
|
am.cancel(pi);
|
|
|
|
unregisterReceiver(alarm);
|
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);
|
2019-03-28 07:22:30 +00:00
|
|
|
} catch (StoreClosedException ex) {
|
|
|
|
Log.w(ex);
|
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-03-06 10:18:57 +00:00
|
|
|
Core.reportError(this, account, null, ex);
|
2018-10-10 07:26:04 +00:00
|
|
|
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
|
2018-08-15 19:34:22 +00:00
|
|
|
} finally {
|
2018-12-05 09:25:35 +00:00
|
|
|
// Stop watching for operations
|
2019-03-30 19:26:09 +00:00
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TwoStateOwner owner : cowners)
|
|
|
|
owner.destroy();
|
|
|
|
}
|
|
|
|
});
|
2018-12-05 09:25:35 +00:00
|
|
|
|
2019-03-30 06:39:26 +00:00
|
|
|
// Update state
|
2018-10-10 07:26:04 +00:00
|
|
|
EntityLog.log(this, account.name + " closing");
|
|
|
|
db.account().setAccountState(account.id, "closing");
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-12-05 09:25:35 +00:00
|
|
|
if (folder.synchronize && !folder.poll)
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
2018-09-08 05:42:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Close store
|
|
|
|
try {
|
2019-01-05 11:17:33 +00:00
|
|
|
EntityLog.log(this, account.name + " store closing");
|
2018-11-27 15:36:42 +00:00
|
|
|
istore.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-11-28 18:47:16 +00:00
|
|
|
|
2019-03-30 06:39:26 +00:00
|
|
|
// Update state
|
2018-11-28 18:47:16 +00:00
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-12-05 09:25:35 +00:00
|
|
|
if (folder.synchronize && !folder.poll)
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-11-28 18:47:16 +00:00
|
|
|
if (state.running())
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-11-08 17:16:32 +00:00
|
|
|
if (backoff <= CONNECT_BACKOFF_MAX) {
|
|
|
|
// Short back-off period, keep device awake
|
|
|
|
EntityLog.log(this, account.name + " backoff=" + backoff);
|
2018-11-28 18:47:16 +00:00
|
|
|
state.acquire(backoff * 1000L);
|
2018-11-08 17:16:32 +00:00
|
|
|
} else {
|
|
|
|
// Long back-off period, let device sleep
|
|
|
|
EntityLog.log(this, account.name + " backoff alarm=" + CONNECT_BACKOFF_AlARM);
|
|
|
|
|
|
|
|
BroadcastReceiver alarm = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2018-11-28 18:47:16 +00:00
|
|
|
state.release();
|
2018-11-08 17:16:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
String id = BuildConfig.APPLICATION_ID + ".BACKOFF." + account.id;
|
2019-01-05 11:17:33 +00:00
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
|
2018-11-08 17:16:32 +00:00
|
|
|
registerReceiver(alarm, new IntentFilter(id));
|
|
|
|
|
2018-12-09 17:49:52 +00:00
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
2018-11-08 17:16:32 +00:00
|
|
|
try {
|
2018-12-09 17:49:52 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
|
|
|
am.set(
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
|
|
|
|
pi);
|
|
|
|
else
|
|
|
|
am.setAndAllowWhileIdle(
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
|
|
|
|
pi);
|
2018-11-09 08:54:24 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.release();
|
2018-11-28 18:47:16 +00:00
|
|
|
state.acquire(2 * CONNECT_BACKOFF_AlARM * 60 * 1000L);
|
2018-11-09 08:54:24 +00:00
|
|
|
} finally {
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.acquire();
|
2018-11-09 08:54:24 +00:00
|
|
|
}
|
2018-11-08 17:16:32 +00:00
|
|
|
} finally {
|
2018-11-09 08:54:24 +00:00
|
|
|
// Cleanup
|
|
|
|
am.cancel(pi);
|
|
|
|
unregisterReceiver(alarm);
|
2018-11-08 17:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 18:22:18 +00:00
|
|
|
|
2018-11-19 14:35:04 +00:00
|
|
|
if (backoff <= CONNECT_BACKOFF_MAX)
|
2018-10-10 07:26:04 +00:00
|
|
|
backoff *= 2;
|
|
|
|
} catch (InterruptedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(account.name + " backoff " + ex.toString());
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
EntityLog.log(this, account.name + " stopped");
|
2018-12-02 13:19:54 +00:00
|
|
|
wlAccount.release();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
2018-11-27 15:36:42 +00:00
|
|
|
@Override
|
2018-12-25 11:37:35 +00:00
|
|
|
public void onAvailable(Network network) {
|
2019-03-16 13:12:31 +00:00
|
|
|
networkState.update(Helper.getNetworkState(ServiceSynchronize.this));
|
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
synchronized (ServiceSynchronize.this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
2019-03-16 18:35:14 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Available " + network + " capabilities " + cm.getNetworkCapabilities(network));
|
2018-12-25 11:37:35 +00:00
|
|
|
|
2019-03-16 13:12:31 +00:00
|
|
|
if (!started && networkState.isSuitable())
|
2018-12-28 17:16:49 +00:00
|
|
|
queue_reload(true, "connect " + network);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2018-12-02 13:27:47 +00:00
|
|
|
}
|
2018-11-27 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
2018-12-25 11:37:35 +00:00
|
|
|
public void onCapabilitiesChanged(Network network, NetworkCapabilities capabilities) {
|
2019-03-16 13:12:31 +00:00
|
|
|
networkState.update(Helper.getNetworkState(ServiceSynchronize.this));
|
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
synchronized (ServiceSynchronize.this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
if (!started) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Network " + network + " capabilities " + capabilities);
|
2019-03-16 13:12:31 +00:00
|
|
|
if (networkState.isSuitable())
|
2018-12-28 17:42:28 +00:00
|
|
|
queue_reload(true, "capabilities " + network);
|
2018-12-28 17:16:49 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2018-12-25 11:37:35 +00:00
|
|
|
}
|
2018-12-02 13:27:47 +00:00
|
|
|
}
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-15 07:40:18 +00:00
|
|
|
@Override
|
|
|
|
public void onLost(Network network) {
|
2019-03-16 13:12:31 +00:00
|
|
|
networkState.update(Helper.getNetworkState(ServiceSynchronize.this));
|
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
synchronized (ServiceSynchronize.this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Lost " + network);
|
2018-11-27 15:36:42 +00:00
|
|
|
|
2019-03-16 13:12:31 +00:00
|
|
|
if (started && !networkState.isSuitable()) {
|
2018-12-28 17:16:49 +00:00
|
|
|
lastLost = new Date().getTime();
|
|
|
|
queue_reload(false, "disconnect " + network);
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2018-12-02 13:27:47 +00:00
|
|
|
}
|
2018-09-08 07:01:02 +00:00
|
|
|
}
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
2019-03-06 10:18:57 +00:00
|
|
|
};
|
2018-08-15 07:40:18 +00:00
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
static void boot(final Context context) {
|
|
|
|
if (!booted) {
|
|
|
|
booted = true;
|
2018-12-06 12:43:00 +00:00
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
Thread thread = new Thread(new Runnable() {
|
2018-11-29 07:35:34 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2019-03-06 10:18:57 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2018-11-28 17:06:58 +00:00
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
// Reset state
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
prefs.edit().remove("oneshot").apply();
|
2018-12-06 11:38:09 +00:00
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
// Restore snooze timers
|
|
|
|
for (EntityMessage message : db.message().getSnoozed())
|
|
|
|
EntityMessage.snooze(context, message.id, message.ui_snoozed);
|
2018-12-06 12:43:00 +00:00
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
// Restore schedule
|
|
|
|
schedule(context);
|
2018-11-28 19:13:13 +00:00
|
|
|
|
2019-03-06 10:18:57 +00:00
|
|
|
// Conditionally init service
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
|
|
|
int accounts = db.account().getSynchronizingAccounts().size();
|
|
|
|
if (enabled && accounts > 0)
|
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
.setAction("init"));
|
2019-03-17 18:12:53 +00:00
|
|
|
else {
|
|
|
|
for (EntityAccount account : db.account().getAccounts())
|
|
|
|
db.account().setAccountState(account.id, null);
|
|
|
|
|
|
|
|
for (EntityFolder folder : db.folder().getFolders()) {
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 07:35:34 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-10-13 13:30:00 +00:00
|
|
|
}
|
2018-11-29 07:35:34 +00:00
|
|
|
}
|
|
|
|
});
|
2019-03-06 10:18:57 +00:00
|
|
|
thread.start();
|
2019-03-05 10:34:00 +00:00
|
|
|
}
|
2018-08-14 18:46:14 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-28 07:55:08 +00:00
|
|
|
private static void schedule(Context context) {
|
|
|
|
Intent alarm = new Intent(context, ServiceSynchronize.class);
|
|
|
|
alarm.setAction("alarm");
|
2019-03-05 10:34:00 +00:00
|
|
|
PendingIntent piAlarm;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
piAlarm = PendingIntent.getService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
else
|
|
|
|
piAlarm = PendingIntent.getForegroundService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-28 07:55:08 +00:00
|
|
|
|
|
|
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
am.cancel(piAlarm);
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
if (!prefs.getBoolean("schedule", false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
int minuteStart = prefs.getInt("schedule_start", 0);
|
|
|
|
int minuteEnd = prefs.getInt("schedule_end", 0);
|
|
|
|
|
|
|
|
if (minuteEnd <= minuteStart)
|
|
|
|
minuteEnd += 24 * 60;
|
|
|
|
|
|
|
|
Calendar calStart = Calendar.getInstance();
|
|
|
|
calStart.set(Calendar.HOUR_OF_DAY, minuteStart / 60);
|
|
|
|
calStart.set(Calendar.MINUTE, minuteStart % 60);
|
|
|
|
calStart.set(Calendar.SECOND, 0);
|
|
|
|
calStart.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
Calendar calEnd = Calendar.getInstance();
|
|
|
|
calEnd.set(Calendar.HOUR_OF_DAY, minuteEnd / 60);
|
|
|
|
calEnd.set(Calendar.MINUTE, minuteEnd % 60);
|
|
|
|
calEnd.set(Calendar.SECOND, 0);
|
|
|
|
calEnd.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
long now = new Date().getTime();
|
|
|
|
if (now > calEnd.getTimeInMillis()) {
|
|
|
|
calStart.set(Calendar.DAY_OF_MONTH, calStart.get(Calendar.DAY_OF_MONTH) + 1);
|
|
|
|
calEnd.set(Calendar.DAY_OF_MONTH, calEnd.get(Calendar.DAY_OF_MONTH) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
long start = calStart.getTimeInMillis();
|
|
|
|
long end = calEnd.getTimeInMillis();
|
|
|
|
long next = (now < start ? start : end);
|
|
|
|
|
|
|
|
Log.i("Schedule now=" + new Date(now));
|
|
|
|
Log.i("Schedule start=" + new Date(start));
|
|
|
|
Log.i("Schedule end=" + new Date(end));
|
|
|
|
Log.i("Schedule next=" + new Date(next));
|
|
|
|
|
|
|
|
boolean enabled = (now >= start && now < end);
|
|
|
|
prefs.edit().putBoolean("enabled", enabled).apply();
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, next, piAlarm);
|
|
|
|
else
|
|
|
|
am.set(AlarmManager.RTC_WAKEUP, next, piAlarm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reschedule(Context context) {
|
2019-02-14 12:28:03 +00:00
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
2019-02-28 07:55:08 +00:00
|
|
|
.setAction("alarm"));
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 07:55:08 +00:00
|
|
|
static void reload(Context context, String reason) {
|
2018-11-27 15:36:42 +00:00
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
.setAction("reload")
|
|
|
|
.putExtra("reason", reason));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2019-03-05 10:34:00 +00:00
|
|
|
|
|
|
|
static void process(Context context) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", true);
|
|
|
|
boolean oneshot = prefs.getBoolean("oneshot", false);
|
|
|
|
if (!enabled && !oneshot) {
|
|
|
|
prefs.edit().putBoolean("oneshot", true).apply();
|
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
.setAction("oneshot_start"));
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|