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.
|
|
|
|
|
|
|
|
NetGuard is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2018-09-20 11:16:23 +00:00
|
|
|
import android.Manifest;
|
2018-10-09 19:39:25 +00:00
|
|
|
import android.app.AlarmManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
2018-09-20 11:16:23 +00:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentUris;
|
2018-08-02 13:33:06 +00:00
|
|
|
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-09-20 11:16:23 +00:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.database.Cursor;
|
2018-09-19 09:43:57 +00:00
|
|
|
import android.graphics.drawable.Icon;
|
2018-08-05 10:56:24 +00:00
|
|
|
import android.media.RingtoneManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.Network;
|
|
|
|
import android.net.NetworkCapabilities;
|
2018-08-15 07:40:18 +00:00
|
|
|
import android.net.NetworkInfo;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.net.NetworkRequest;
|
2018-08-05 10:56:24 +00:00
|
|
|
import android.net.Uri;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.os.Build;
|
2018-08-12 12:47:52 +00:00
|
|
|
import android.os.Bundle;
|
2018-10-08 06:18:44 +00:00
|
|
|
import android.os.PowerManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.os.SystemClock;
|
2018-08-13 14:37:15 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2018-09-20 11:16:23 +00:00
|
|
|
import android.provider.ContactsContract;
|
2018-08-25 13:58:20 +00:00
|
|
|
import android.text.Html;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2018-08-24 17:28:36 +00:00
|
|
|
import com.sun.mail.iap.ConnectionException;
|
2018-08-13 14:37:15 +00:00
|
|
|
import com.sun.mail.imap.AppendUID;
|
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;
|
2018-09-16 15:18:57 +00:00
|
|
|
import com.sun.mail.util.FolderClosedIOException;
|
2018-08-22 16:05:22 +00:00
|
|
|
import com.sun.mail.util.MailConnectException;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
2018-08-03 12:07:51 +00:00
|
|
|
import java.io.IOException;
|
2018-09-02 08:34:06 +00:00
|
|
|
import java.net.SocketException;
|
2018-08-08 08:09:53 +00:00
|
|
|
import java.net.SocketTimeoutException;
|
2018-09-05 09:47:22 +00:00
|
|
|
import java.net.UnknownHostException;
|
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;
|
2018-09-16 18:07:26 +00:00
|
|
|
import java.util.Arrays;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2018-09-05 07:23:51 +00:00
|
|
|
import java.util.Enumeration;
|
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;
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
2018-08-27 14:31:45 +00:00
|
|
|
import javax.mail.AuthenticationFailedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.FetchProfile;
|
|
|
|
import javax.mail.Flags;
|
|
|
|
import javax.mail.Folder;
|
2018-08-02 15:25:01 +00:00
|
|
|
import javax.mail.FolderClosedException;
|
2018-08-03 14:41:31 +00:00
|
|
|
import javax.mail.FolderNotFoundException;
|
2018-09-05 07:23:51 +00:00
|
|
|
import javax.mail.Header;
|
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-14 14:50:41 +00:00
|
|
|
import javax.mail.SendFailedException;
|
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.Transport;
|
|
|
|
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;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.search.ComparisonTerm;
|
|
|
|
import javax.mail.search.ReceivedDateTerm;
|
2018-08-28 16:15:15 +00:00
|
|
|
import javax.net.ssl.SSLException;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.lifecycle.LifecycleService;
|
|
|
|
import androidx.lifecycle.Observer;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
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 {
|
2018-08-12 13:04:24 +00:00
|
|
|
private final Object lock = new Object();
|
2018-10-17 06:11:20 +00:00
|
|
|
private TupleAccountStats lastStats = null;
|
2018-08-14 18:46:14 +00:00
|
|
|
private ServiceManager serviceManager = new ServiceManager();
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
private static final int NOTIFICATION_SYNCHRONIZE = 1;
|
|
|
|
|
2018-09-07 13:54:32 +00:00
|
|
|
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
2018-08-27 16:27:54 +00:00
|
|
|
private static final int CONNECT_BACKOFF_MAX = 1024; // seconds (1024 sec ~ 17 min)
|
2018-09-16 18:07:26 +00:00
|
|
|
private static final int SYNC_BATCH_SIZE = 20;
|
|
|
|
private static final int DOWNLOAD_BATCH_SIZE = 20;
|
2018-09-16 10:44:13 +00:00
|
|
|
private static final int MESSAGE_AUTO_DOWNLOAD_SIZE = 32 * 1024; // bytes
|
2018-09-04 08:28:29 +00:00
|
|
|
private static final int ATTACHMENT_AUTO_DOWNLOAD_SIZE = 32 * 1024; // bytes
|
2018-09-30 10:13:24 +00:00
|
|
|
private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-17 06:20:24 +00:00
|
|
|
static final int PI_CLEAR = 1;
|
|
|
|
static final int PI_SEEN = 2;
|
|
|
|
static final int PI_TRASH = 3;
|
|
|
|
static final int PI_IGNORED = 4;
|
2018-09-20 09:10:19 +00:00
|
|
|
|
2018-08-26 08:33:22 +00:00
|
|
|
static final String ACTION_SYNCHRONIZE_FOLDER = BuildConfig.APPLICATION_ID + ".SYNCHRONIZE_FOLDER";
|
2018-08-11 08:37:56 +00:00
|
|
|
static final String ACTION_PROCESS_OPERATIONS = BuildConfig.APPLICATION_ID + ".PROCESS_OPERATIONS";
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2018-08-23 11:21:40 +00:00
|
|
|
Log.i(Helper.TAG, "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);
|
2018-08-14 18:46:14 +00:00
|
|
|
cm.registerNetworkCallback(builder.build(), serviceManager);
|
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-08-12 15:31:43 +00:00
|
|
|
NotificationManager nm = getSystemService(NotificationManager.class);
|
2018-10-17 06:11:20 +00:00
|
|
|
nm.notify(NOTIFICATION_SYNCHRONIZE, getNotificationService(stats).build());
|
2018-08-24 18:06:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.message().liveUnseenUnified().observe(this, new Observer<List<EntityMessage>>() {
|
2018-09-20 09:10:19 +00:00
|
|
|
private List<Integer> notifying = new ArrayList<>();
|
2018-08-12 15:31:43 +00:00
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(List<EntityMessage> messages) {
|
|
|
|
NotificationManager nm = getSystemService(NotificationManager.class);
|
2018-09-20 09:10:19 +00:00
|
|
|
List<Notification> notifications = getNotificationUnseen(messages);
|
|
|
|
|
|
|
|
List<Integer> all = new ArrayList<>();
|
|
|
|
List<Integer> added = new ArrayList<>();
|
|
|
|
List<Integer> removed = new ArrayList<>(notifying);
|
|
|
|
for (Notification notification : notifications) {
|
|
|
|
Integer id = (int) notification.extras.getLong("id", 0);
|
|
|
|
if (id > 0) {
|
|
|
|
all.add(id);
|
|
|
|
if (removed.contains(id))
|
|
|
|
removed.remove(id);
|
|
|
|
else
|
|
|
|
added.add(id);
|
2018-08-12 15:31:43 +00:00
|
|
|
}
|
2018-09-20 09:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (notifications.size() == 0)
|
|
|
|
nm.cancel("unseen", 0);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
for (Integer id : removed)
|
|
|
|
nm.cancel("unseen", id);
|
|
|
|
|
|
|
|
for (Notification notification : notifications) {
|
|
|
|
Integer id = (int) notification.extras.getLong("id", 0);
|
|
|
|
if ((id == 0 && added.size() + removed.size() > 0) || added.contains(id))
|
|
|
|
nm.notify("unseen", id, notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
notifying = all;
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
});
|
2018-10-16 10:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i(Helper.TAG, "Service destroy");
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
cm.unregisterNetworkCallback(serviceManager);
|
|
|
|
|
|
|
|
serviceManager.onLost(null);
|
|
|
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
|
|
|
NotificationManager nm = getSystemService(NotificationManager.class);
|
|
|
|
nm.cancel(NOTIFICATION_SYNCHRONIZE);
|
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
String action = (intent == null ? null : intent.getAction());
|
|
|
|
Log.i(Helper.TAG, "Service command intent=" + intent + " action=" + action);
|
|
|
|
|
2018-10-17 06:11:20 +00:00
|
|
|
startForeground(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-10-13 13:30:00 +00:00
|
|
|
if (action != null) {
|
|
|
|
if ("start".equals(action))
|
|
|
|
serviceManager.queue_start();
|
|
|
|
else if ("stop".equals(action))
|
|
|
|
serviceManager.queue_stop();
|
|
|
|
else if ("reload".equals(action))
|
|
|
|
serviceManager.queue_reload();
|
2018-10-17 06:20:24 +00:00
|
|
|
else if ("clear".equals(action)) {
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onLoad(Context context, Bundle args) throws Throwable {
|
|
|
|
DB.getInstance(context).message().ignoreAll();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.load(this, new Bundle());
|
|
|
|
} else if (action.startsWith("seen:") || action.startsWith("trash:") || action.startsWith("ignored:")) {
|
2018-09-19 09:43:57 +00:00
|
|
|
Bundle args = new Bundle();
|
2018-09-20 09:10:19 +00:00
|
|
|
args.putLong("id", Long.parseLong(action.split(":")[1]));
|
|
|
|
args.putString("action", action.split(":")[0]);
|
2018-09-19 09:43:57 +00:00
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onLoad(Context context, Bundle args) {
|
2018-09-20 09:10:19 +00:00
|
|
|
long id = args.getLong("id");
|
|
|
|
String action = args.getString("action");
|
|
|
|
|
2018-09-19 09:43:57 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
EntityMessage message = db.message().getMessage(id);
|
|
|
|
if ("seen".equals(action)) {
|
2018-09-19 09:43:57 +00:00
|
|
|
db.message().setMessageUiSeen(message.id, true);
|
2018-10-19 06:40:32 +00:00
|
|
|
db.message().setMessageUiIgnored(message.id, true);
|
2018-09-19 09:43:57 +00:00
|
|
|
EntityOperation.queue(db, message, EntityOperation.SEEN, true);
|
2018-09-20 09:10:19 +00:00
|
|
|
} else if ("trash".equals(action)) {
|
|
|
|
db.message().setMessageUiHide(message.id, true);
|
|
|
|
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
|
|
|
|
if (trash != null)
|
|
|
|
EntityOperation.queue(db, message, EntityOperation.MOVE, trash.id);
|
2018-10-16 11:29:12 +00:00
|
|
|
} else if ("ignored".equals(action))
|
|
|
|
db.message().setMessageUiIgnored(message.id, true);
|
2018-09-19 09:43:57 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityOperation.process(context);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.load(this, args);
|
2018-08-22 12:30:27 +00:00
|
|
|
}
|
2018-09-20 09:10:19 +00:00
|
|
|
}
|
2018-08-07 20:03:45 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:20 +00:00
|
|
|
private Notification.Builder getNotificationService(TupleAccountStats stats) {
|
|
|
|
if (stats == null)
|
|
|
|
stats = lastStats;
|
|
|
|
if (stats == null)
|
|
|
|
stats = new TupleAccountStats();
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Build pending intent
|
|
|
|
Intent intent = new Intent(this, ActivityView.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(
|
2018-10-15 10:05:42 +00:00
|
|
|
this, ActivityView.REQUEST_UNIFIED, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
// Build notification
|
|
|
|
Notification.Builder builder;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
2018-08-05 10:56:24 +00:00
|
|
|
builder = new Notification.Builder(this, "service");
|
2018-08-02 13:33:06 +00:00
|
|
|
else
|
|
|
|
builder = new Notification.Builder(this);
|
|
|
|
|
|
|
|
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(
|
|
|
|
R.plurals.title_notification_synchronizing, stats.accounts, stats.accounts))
|
2018-08-02 13:33:06 +00:00
|
|
|
.setContentIntent(pi)
|
|
|
|
.setAutoCancel(false)
|
2018-08-05 10:56:24 +00:00
|
|
|
.setShowWhen(false)
|
|
|
|
.setPriority(Notification.PRIORITY_MIN)
|
2018-08-02 13:33:06 +00:00
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
|
|
|
.setVisibility(Notification.VISIBILITY_SECRET);
|
|
|
|
|
2018-10-17 06:11:20 +00:00
|
|
|
if (stats.operations > 0)
|
2018-08-12 11:17:50 +00:00
|
|
|
builder.setStyle(new Notification.BigTextStyle().setSummaryText(
|
2018-10-17 06:11:20 +00:00
|
|
|
getResources().getQuantityString(
|
|
|
|
R.plurals.title_notification_operations, stats.operations, stats.operations)));
|
|
|
|
|
|
|
|
if (stats.unsent > 0)
|
|
|
|
builder.setContentText(getResources().getQuantityString(
|
|
|
|
R.plurals.title_notification_unsent, stats.unsent, stats.unsent));
|
2018-08-12 11:17:50 +00:00
|
|
|
|
2018-10-17 06:11:20 +00:00
|
|
|
lastStats = stats;
|
2018-08-08 11:41:49 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
private List<Notification> getNotificationUnseen(List<EntityMessage> messages) {
|
|
|
|
// https://developer.android.com/training/notify-user/group
|
|
|
|
List<Notification> notifications = new ArrayList<>();
|
2018-09-14 13:31:00 +00:00
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
if (messages.size() == 0)
|
|
|
|
return notifications;
|
2018-08-07 18:31:14 +00:00
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
boolean pro = Helper.isPro(this);
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2018-09-19 09:43:57 +00:00
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
// Build pending intent
|
|
|
|
Intent view = new Intent(this, ActivityView.class);
|
|
|
|
view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
PendingIntent piView = PendingIntent.getActivity(
|
2018-10-15 10:05:42 +00:00
|
|
|
this, ActivityView.REQUEST_UNIFIED, view, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2018-10-17 06:20:24 +00:00
|
|
|
Intent clear = new Intent(this, ServiceSynchronize.class);
|
|
|
|
clear.setAction("clear");
|
|
|
|
PendingIntent piClear = PendingIntent.getService(this, PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2018-08-05 10:56:24 +00:00
|
|
|
// Build notification
|
|
|
|
Notification.Builder builder;
|
2018-09-20 09:10:19 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
2018-08-05 10:56:24 +00:00
|
|
|
builder = new Notification.Builder(this);
|
2018-09-20 09:10:19 +00:00
|
|
|
else
|
|
|
|
builder = new Notification.Builder(this, "notification");
|
2018-08-05 10:56:24 +00:00
|
|
|
|
|
|
|
builder
|
2018-10-06 20:45:07 +00:00
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
2018-08-24 18:06:44 +00:00
|
|
|
.setContentTitle(getResources().getQuantityString(R.plurals.title_notification_unseen, messages.size(), messages.size()))
|
2018-09-20 09:10:19 +00:00
|
|
|
.setContentText("")
|
|
|
|
.setContentIntent(piView)
|
|
|
|
.setNumber(messages.size())
|
2018-08-07 18:31:14 +00:00
|
|
|
.setShowWhen(false)
|
2018-10-17 06:20:24 +00:00
|
|
|
.setDeleteIntent(piClear)
|
2018-08-05 10:56:24 +00:00
|
|
|
.setPriority(Notification.PRIORITY_DEFAULT)
|
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setVisibility(Notification.VISIBILITY_PRIVATE)
|
|
|
|
.setGroup(BuildConfig.APPLICATION_ID)
|
|
|
|
.setGroupSummary(true);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
builder.setSound(null);
|
|
|
|
else
|
|
|
|
builder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2018-09-14 13:31:00 +00:00
|
|
|
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O &&
|
2018-09-14 14:33:00 +00:00
|
|
|
prefs.getBoolean("light", false)) {
|
2018-09-14 13:31:00 +00:00
|
|
|
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS);
|
|
|
|
builder.setLights(0xff00ff00, 1000, 1000);
|
|
|
|
}
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
if (pro) {
|
2018-08-25 13:36:01 +00:00
|
|
|
DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (EntityMessage message : messages) {
|
2018-08-25 13:58:20 +00:00
|
|
|
sb.append("<strong>").append(MessageHelper.getFormattedAddresses(message.from, false)).append("</strong>");
|
2018-08-25 13:36:01 +00:00
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
sb.append(": ").append(message.subject);
|
2018-09-13 12:43:59 +00:00
|
|
|
sb.append(" ").append(df.format(new Date(message.sent == null ? message.received : message.sent)));
|
2018-09-02 08:36:02 +00:00
|
|
|
sb.append("<br>");
|
2018-08-25 13:36:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 13:58:20 +00:00
|
|
|
builder.setStyle(new Notification.BigTextStyle().bigText(Html.fromHtml(sb.toString())));
|
2018-08-25 13:36:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
notifications.add(builder.build());
|
|
|
|
|
|
|
|
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
|
|
|
|
|
for (EntityMessage message : messages) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", message.id);
|
|
|
|
|
2018-10-15 10:05:42 +00:00
|
|
|
Intent thread = new Intent(this, ActivityView.class);
|
2018-10-17 09:15:44 +00:00
|
|
|
thread.setAction("thread:" + message.thread);
|
2018-10-15 10:05:42 +00:00
|
|
|
thread.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2018-10-17 09:15:44 +00:00
|
|
|
thread.putExtra("account", message.account);
|
2018-10-16 11:29:12 +00:00
|
|
|
PendingIntent piContent = PendingIntent.getActivity(
|
2018-10-15 10:05:42 +00:00
|
|
|
this, ActivityView.REQUEST_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2018-10-16 11:29:12 +00:00
|
|
|
Intent ignored = new Intent(this, ServiceSynchronize.class);
|
|
|
|
ignored.setAction("ignored:" + message.id);
|
|
|
|
PendingIntent piDelete = PendingIntent.getService(this, PI_IGNORED, ignored, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
Intent seen = new Intent(this, ServiceSynchronize.class);
|
|
|
|
seen.setAction("seen:" + message.id);
|
|
|
|
PendingIntent piSeen = PendingIntent.getService(this, PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
Intent trash = new Intent(this, ServiceSynchronize.class);
|
|
|
|
trash.setAction("trash:" + message.id);
|
|
|
|
PendingIntent piTrash = PendingIntent.getService(this, PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
Notification.Action.Builder actionSeen = new Notification.Action.Builder(
|
|
|
|
Icon.createWithResource(this, R.drawable.baseline_visibility_24),
|
|
|
|
getString(R.string.title_seen),
|
|
|
|
piSeen);
|
|
|
|
|
|
|
|
Notification.Action.Builder actionTrash = new Notification.Action.Builder(
|
|
|
|
Icon.createWithResource(this, R.drawable.baseline_delete_24),
|
|
|
|
getString(R.string.title_trash),
|
|
|
|
piTrash);
|
|
|
|
|
|
|
|
Notification.Builder mbuilder;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
mbuilder = new Notification.Builder(this);
|
|
|
|
else
|
|
|
|
mbuilder = new Notification.Builder(this, "notification");
|
|
|
|
|
|
|
|
mbuilder
|
|
|
|
.addExtras(args)
|
|
|
|
.setSmallIcon(R.drawable.baseline_mail_24)
|
|
|
|
.setContentTitle(MessageHelper.getFormattedAddresses(message.from, true))
|
2018-10-16 11:29:12 +00:00
|
|
|
.setContentIntent(piContent)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setSound(uri)
|
|
|
|
.setWhen(message.sent == null ? message.received : message.sent)
|
2018-10-16 11:29:12 +00:00
|
|
|
.setDeleteIntent(piDelete)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setPriority(Notification.PRIORITY_DEFAULT)
|
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
|
|
|
.setVisibility(Notification.VISIBILITY_PRIVATE)
|
|
|
|
.setGroup(BuildConfig.APPLICATION_ID)
|
|
|
|
.setGroupSummary(false)
|
|
|
|
.addAction(actionSeen.build())
|
|
|
|
.addAction(actionTrash.build());
|
|
|
|
|
|
|
|
if (pro)
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
mbuilder.setContentText(message.subject);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
|
|
|
mbuilder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
|
|
|
|
|
|
|
|
notifications.add(mbuilder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
2018-08-05 10:56:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 18:31:14 +00:00
|
|
|
private Notification.Builder getNotificationError(String action, Throwable ex) {
|
2018-08-02 13:33:06 +00:00
|
|
|
// Build pending intent
|
|
|
|
Intent intent = new Intent(this, ActivityView.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(
|
2018-09-20 19:31:43 +00:00
|
|
|
this, ActivityView.REQUEST_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
// Build notification
|
|
|
|
Notification.Builder builder;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
|
|
|
builder = new Notification.Builder(this, "error");
|
|
|
|
else
|
|
|
|
builder = new Notification.Builder(this);
|
|
|
|
|
|
|
|
builder
|
|
|
|
.setSmallIcon(android.R.drawable.stat_notify_error)
|
2018-08-05 08:53:43 +00:00
|
|
|
.setContentTitle(getString(R.string.title_notification_failed, action))
|
2018-08-02 13:33:06 +00:00
|
|
|
.setContentText(Helper.formatThrowable(ex))
|
|
|
|
.setContentIntent(pi)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setPriority(Notification.PRIORITY_MAX)
|
|
|
|
.setCategory(Notification.CATEGORY_ERROR)
|
|
|
|
.setVisibility(Notification.VISIBILITY_SECRET);
|
|
|
|
|
2018-09-03 04:46:44 +00:00
|
|
|
builder.setStyle(new Notification.BigTextStyle().bigText(ex.toString()));
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2018-08-02 15:25:01 +00:00
|
|
|
private void reportError(String account, String folder, Throwable ex) {
|
2018-08-15 17:25:12 +00:00
|
|
|
// FolderClosedException: can happen when no connectivity
|
|
|
|
|
|
|
|
// IllegalStateException:
|
|
|
|
// - "This operation is not allowed on a closed folder"
|
|
|
|
// - can happen when syncing message
|
|
|
|
|
2018-08-24 17:28:36 +00:00
|
|
|
// ConnectionException
|
|
|
|
// - failed to create new store connection (connectivity)
|
|
|
|
|
2018-08-21 16:32:10 +00:00
|
|
|
// MailConnectException
|
2018-08-24 17:28:36 +00:00
|
|
|
// - on connectity problems when connecting to store
|
2018-08-22 16:05:22 +00:00
|
|
|
|
2018-09-04 05:49:58 +00:00
|
|
|
String action;
|
|
|
|
if (TextUtils.isEmpty(account))
|
|
|
|
action = folder;
|
|
|
|
else if (TextUtils.isEmpty(folder))
|
|
|
|
action = account;
|
|
|
|
else
|
|
|
|
action = account + "/" + folder;
|
|
|
|
|
2018-09-08 11:44:42 +00:00
|
|
|
EntityLog.log(this, action + " " + Helper.formatThrowable(ex));
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2018-09-08 05:42:44 +00:00
|
|
|
if (ex instanceof SendFailedException) {
|
|
|
|
NotificationManager nm = getSystemService(NotificationManager.class);
|
|
|
|
nm.notify(action, 1, getNotificationError(action, ex).build());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BuildConfig.DEBUG &&
|
|
|
|
!(ex instanceof SendFailedException) &&
|
|
|
|
!(ex instanceof MailConnectException) &&
|
2018-08-22 16:05:22 +00:00
|
|
|
!(ex instanceof FolderClosedException) &&
|
2018-08-23 13:00:01 +00:00
|
|
|
!(ex instanceof IllegalStateException) &&
|
2018-09-04 12:40:23 +00:00
|
|
|
!(ex instanceof AuthenticationFailedException) && // Also: Too many simultaneous connections
|
2018-09-03 16:34:38 +00:00
|
|
|
!(ex instanceof StoreClosedException) &&
|
2018-09-05 09:47:22 +00:00
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof UnknownHostException) &&
|
2018-08-24 17:28:36 +00:00
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof ConnectionException) &&
|
2018-09-02 08:34:06 +00:00
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof SocketException) &&
|
2018-08-28 06:37:09 +00:00
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof SocketTimeoutException) &&
|
2018-09-04 12:40:23 +00:00
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof SSLException) &&
|
|
|
|
!(ex instanceof MessagingException && "connection failure".equals(ex.getMessage()))) {
|
2018-08-15 07:40:18 +00:00
|
|
|
NotificationManager nm = getSystemService(NotificationManager.class);
|
|
|
|
nm.notify(action, 1, getNotificationError(action, ex).build());
|
|
|
|
}
|
2018-08-02 15:25:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 19:34:22 +00:00
|
|
|
private void monitorAccount(final EntityAccount account, final ServiceState state) throws NoSuchProviderException {
|
2018-10-10 07:26:04 +00:00
|
|
|
final PowerManager pm = getSystemService(PowerManager.class);
|
2018-10-16 12:06:42 +00:00
|
|
|
final PowerManager.WakeLock wl0 = pm.newWakeLock(
|
2018-10-10 07:26:04 +00:00
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
2018-10-16 12:06:42 +00:00
|
|
|
BuildConfig.APPLICATION_ID + ":account." + account.id + ".monitor");
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-10-16 12:06:42 +00:00
|
|
|
wl0.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
|
|
|
|
final DB db = DB.getInstance(this);
|
|
|
|
final ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
|
|
|
|
|
|
|
int backoff = CONNECT_BACKOFF_START;
|
|
|
|
while (state.running) {
|
|
|
|
EntityLog.log(this, account.name + " run");
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
boolean debug = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("debug", false);
|
|
|
|
debug = debug || BuildConfig.DEBUG;
|
|
|
|
System.setProperty("mail.socket.debug", Boolean.toString(debug));
|
|
|
|
|
|
|
|
// Create session
|
|
|
|
Properties props = MessageHelper.getSessionProperties(account.auth_type);
|
|
|
|
final Session isession = Session.getInstance(props, null);
|
|
|
|
isession.setDebug(debug);
|
|
|
|
// adb -t 1 logcat | grep "fairemail\|System.out"
|
|
|
|
|
|
|
|
final IMAPStore istore = (IMAPStore) isession.getStore("imaps");
|
|
|
|
final Map<EntityFolder, IMAPFolder> folders = new HashMap<>();
|
|
|
|
List<Thread> syncs = new ArrayList<>();
|
|
|
|
List<Thread> idlers = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
// Listen for store events
|
|
|
|
istore.addStoreListener(new StoreListener() {
|
2018-10-16 12:06:42 +00:00
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":account." + account.id + ".store");
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void notification(StoreEvent e) {
|
2018-10-10 08:00:42 +00:00
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, account.name + " event: " + e.getMessage());
|
|
|
|
db.account().setAccountError(account.id, e.getMessage());
|
|
|
|
state.thread.interrupt();
|
|
|
|
yieldWakelock();
|
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-15 19:34:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Listen for folder events
|
|
|
|
istore.addFolderListener(new FolderAdapter() {
|
2018-10-16 12:06:42 +00:00
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":account." + account.id + ".folder");
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void folderCreated(FolderEvent e) {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, "Folder created=" + e.getFolder().getFullName());
|
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void folderRenamed(FolderEvent e) {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, "Folder renamed=" + e.getFolder());
|
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);
|
|
|
|
Log.i(Helper.TAG, "Renamed to " + name + " count=" + count);
|
2018-09-14 06:28:23 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void folderDeleted(FolderEvent e) {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, "Folder deleted=" + e.getFolder().getFullName());
|
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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) {
|
|
|
|
Log.i(Helper.TAG, account.name + " opened");
|
|
|
|
}
|
2018-08-06 16:22:01 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void disconnected(ConnectionEvent e) {
|
|
|
|
Log.e(Helper.TAG, account.name + " disconnected event");
|
|
|
|
}
|
2018-08-11 08:28:54 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void closed(ConnectionEvent e) {
|
|
|
|
Log.e(Helper.TAG, account.name + " closed event");
|
|
|
|
}
|
|
|
|
});
|
2018-08-15 19:34:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Initiate connection
|
|
|
|
Log.i(Helper.TAG, account.name + " connect");
|
|
|
|
for (EntityFolder folder : db.folder().getFolders(account.id))
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
|
|
|
db.account().setAccountState(account.id, "connecting");
|
|
|
|
Helper.connect(this, istore, account);
|
|
|
|
final boolean capIdle = istore.hasCapability("IDLE");
|
|
|
|
Log.i(Helper.TAG, account.name + " idle=" + capIdle);
|
|
|
|
db.account().setAccountState(account.id, "connected");
|
|
|
|
db.account().setAccountError(account.id, null);
|
2018-08-12 12:47:52 +00:00
|
|
|
|
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
|
|
|
|
synchronizeFolders(account, istore, state);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Open folders
|
|
|
|
for (final EntityFolder folder : db.folder().getFolders(account.id, true)) {
|
|
|
|
Log.i(Helper.TAG, account.name + " sync folder " + folder.name);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connecting");
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
final IMAPFolder ifolder = (IMAPFolder) istore.getFolder(folder.name);
|
|
|
|
try {
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
folders.put(folder, ifolder);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
|
|
|
db.folder().setFolderError(folder.id, null);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Synchronize folder
|
|
|
|
Thread sync = new Thread(new Runnable() {
|
2018-10-16 12:06:42 +00:00
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":account." + account.id + ".sync");
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
|
|
|
|
// Process pending operations
|
|
|
|
processOperations(folder, isession, istore, ifolder);
|
|
|
|
|
|
|
|
// Listen for new and deleted messages
|
|
|
|
ifolder.addMessageCountListener(new MessageCountAdapter() {
|
|
|
|
@Override
|
|
|
|
public void messagesAdded(MessageCountEvent e) {
|
|
|
|
synchronized (lock) {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, folder.name + " messages added");
|
|
|
|
|
|
|
|
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())
|
2018-09-21 11:53:54 +00:00
|
|
|
try {
|
2018-10-10 07:26:04 +00:00
|
|
|
long id;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
id = synchronizeMessage(ServiceSynchronize.this, folder, ifolder, (IMAPMessage) imessage, false);
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
downloadMessage(ServiceSynchronize.this, folder, ifolder, (IMAPMessage) imessage, id);
|
|
|
|
} catch (MessageRemovedException ex) {
|
2018-09-06 12:23:54 +00:00
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
if (ex.getCause() instanceof MessageRemovedException)
|
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
else
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
EntityOperation.process(ServiceSynchronize.this); // download small attachments
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, folder.name, ex);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
2018-08-04 22:35:47 +00:00
|
|
|
}
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-08-14 12:07:57 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void messagesRemoved(MessageCountEvent e) {
|
|
|
|
synchronized (lock) {
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, folder.name + " messages removed");
|
|
|
|
for (Message imessage : e.getMessages())
|
|
|
|
try {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
Log.i(Helper.TAG, "Deleted uid=" + uid + " count=" + count);
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, folder.name, ex);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state.thread.interrupt();
|
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
2018-08-04 22:35:47 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
});
|
2018-08-11 08:28:54 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Fetch e-mail
|
|
|
|
synchronizeMessages(account, folder, ifolder, state);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Flags (like "seen") at the remote could be changed while synchronizing
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Listen for changed messages
|
|
|
|
ifolder.addMessageChangedListener(new MessageChangedListener() {
|
|
|
|
@Override
|
|
|
|
public void messageChanged(MessageChangedEvent e) {
|
|
|
|
synchronized (lock) {
|
2018-08-22 05:31:00 +00:00
|
|
|
try {
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.acquire();
|
|
|
|
try {
|
|
|
|
Log.i(Helper.TAG, folder.name + " message changed");
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.FLAGS);
|
|
|
|
ifolder.fetch(new Message[]{e.getMessage()}, fp);
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
long id;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
id = synchronizeMessage(ServiceSynchronize.this, folder, ifolder, (IMAPMessage) e.getMessage(), false);
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
downloadMessage(ServiceSynchronize.this, folder, ifolder, (IMAPMessage) e.getMessage(), id);
|
|
|
|
} catch (MessageRemovedException ex) {
|
2018-09-06 12:23:54 +00:00
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
if (ex.getCause() instanceof MessageRemovedException)
|
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
else
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, folder.name, ex);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
|
|
|
}
|
2018-08-22 05:31:00 +00:00
|
|
|
}
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
});
|
2018-09-21 17:19:02 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, folder.name, ex);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-09-21 17:19:02 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-07 18:43:44 +00:00
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-09-21 17:19:02 +00:00
|
|
|
} finally {
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.release();
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}, "sync." + folder.id);
|
|
|
|
sync.start();
|
|
|
|
syncs.add(sync);
|
|
|
|
|
|
|
|
// Idle folder
|
|
|
|
if (capIdle) {
|
|
|
|
Thread idler = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Log.i(Helper.TAG, folder.name + " start idle");
|
|
|
|
while (state.running) {
|
|
|
|
Log.i(Helper.TAG, folder.name + " do idle");
|
|
|
|
ifolder.idle(false);
|
|
|
|
//Log.i(Helper.TAG, folder.name + " done idle");
|
|
|
|
}
|
2018-10-10 08:07:37 +00:00
|
|
|
} catch (FolderClosedException ignored) {
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, folder.name, ex);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2018-09-09 07:23:51 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
Log.i(Helper.TAG, folder.name + " end idle");
|
2018-08-21 19:08:51 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
}, "idler." + folder.id);
|
|
|
|
idler.start();
|
|
|
|
idlers.add(idler);
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Successfully connected: reset back off time
|
|
|
|
backoff = CONNECT_BACKOFF_START;
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Process folder actions
|
|
|
|
BroadcastReceiver processFolder = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, final Intent intent) {
|
|
|
|
executor.submit(new Runnable() {
|
2018-10-16 12:06:42 +00:00
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":account." + account.id + ".process");
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
long fid = intent.getLongExtra("folder", -1);
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
Log.i(Helper.TAG, "Process folder=" + fid + " intent=" + intent);
|
|
|
|
|
|
|
|
// Get folder
|
|
|
|
EntityFolder folder = null;
|
|
|
|
IMAPFolder ifolder = null;
|
|
|
|
for (EntityFolder f : folders.keySet())
|
|
|
|
if (f.id == fid) {
|
|
|
|
folder = f;
|
|
|
|
ifolder = folders.get(f);
|
|
|
|
break;
|
|
|
|
}
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
final boolean shouldClose = (folder == null);
|
2018-08-05 04:54:17 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
|
|
|
if (folder == null)
|
|
|
|
folder = db.folder().getFolder(fid);
|
2018-08-05 04:54:17 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " run " + (shouldClose ? "offline" : "online"));
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
if (ifolder == null) {
|
|
|
|
// Prevent unnecessary folder connections
|
|
|
|
if (ACTION_PROCESS_OPERATIONS.equals(intent.getAction()))
|
|
|
|
if (db.operation().getOperationCount(fid) == 0)
|
|
|
|
return;
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connecting");
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
ifolder = (IMAPFolder) istore.getFolder(folder.name);
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
2018-08-26 08:33:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
|
|
|
db.folder().setFolderError(folder.id, null);
|
|
|
|
}
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
if (ACTION_PROCESS_OPERATIONS.equals(intent.getAction()))
|
|
|
|
processOperations(folder, isession, istore, ifolder);
|
|
|
|
|
|
|
|
else if (ACTION_SYNCHRONIZE_FOLDER.equals(intent.getAction())) {
|
|
|
|
processOperations(folder, isession, istore, ifolder);
|
|
|
|
synchronizeMessages(account, folder, ifolder, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, folder.name, ex);
|
|
|
|
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
|
|
|
} finally {
|
|
|
|
if (shouldClose) {
|
|
|
|
if (ifolder != null && ifolder.isOpen()) {
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
|
|
|
try {
|
|
|
|
ifolder.close(false);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-08-21 19:08:51 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
2018-09-03 09:34:13 +00:00
|
|
|
}
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-08-26 08:33:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Listen for folder operations
|
|
|
|
IntentFilter f = new IntentFilter();
|
|
|
|
f.addAction(ACTION_SYNCHRONIZE_FOLDER);
|
|
|
|
f.addAction(ACTION_PROCESS_OPERATIONS);
|
|
|
|
f.addDataType("account/" + account.id);
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this);
|
|
|
|
lbm.registerReceiver(processFolder, f);
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2018-10-17 10:11:24 +00:00
|
|
|
for (EntityFolder folder : folders.keySet())
|
|
|
|
if (db.operation().getOperationCount(folder.id) > 0) {
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.setType("account/" + account.id);
|
|
|
|
intent.setAction(ServiceSynchronize.ACTION_PROCESS_OPERATIONS);
|
|
|
|
intent.putExtra("folder", folder.id);
|
|
|
|
lbm.sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
|
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-10-16 12:06:42 +00:00
|
|
|
EntityLog.log(context, account.name + " keep alive wake lock=" + wl0.isHeld());
|
2018-10-10 07:26:04 +00:00
|
|
|
state.thread.interrupt();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
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;
|
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(ServiceSynchronize.this, 0, new Intent(id), 0);
|
|
|
|
registerReceiver(alarm, new IntentFilter(id));
|
2018-10-07 10:13:32 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Keep alive
|
|
|
|
AlarmManager am = getSystemService(AlarmManager.class);
|
|
|
|
try {
|
|
|
|
while (state.running) {
|
|
|
|
// Schedule keep alive alarm
|
|
|
|
EntityLog.log(this, account.name + " wait=" + account.poll_interval);
|
|
|
|
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-10-16 12:06:42 +00:00
|
|
|
wl0.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
Thread.sleep(Long.MAX_VALUE);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
EntityLog.log(this, account.name + " waited running=" + state.running);
|
|
|
|
} finally {
|
2018-10-16 12:06:42 +00:00
|
|
|
wl0.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-10-07 15:44:05 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
if (state.running) {
|
|
|
|
if (!istore.isConnected())
|
|
|
|
throw new StoreClosedException(istore);
|
2018-10-09 19:39:25 +00:00
|
|
|
|
2018-10-15 05:28:27 +00:00
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-10-10 07:26:04 +00:00
|
|
|
if (capIdle) {
|
2018-10-15 05:28:27 +00:00
|
|
|
if (!folders.get(folder).isOpen())
|
|
|
|
throw new FolderClosedException(folders.get(folder));
|
2018-10-10 07:26:04 +00:00
|
|
|
} else
|
2018-10-15 05:28:27 +00:00
|
|
|
synchronizeMessages(account, folder, folders.get(folder), state);
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-10-09 19:39:25 +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);
|
|
|
|
lbm.unregisterReceiver(processFolder);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
Log.i(Helper.TAG, account.name + " done running=" + state.running);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, account.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(account.name, null, ex);
|
2018-10-08 06:18:44 +00:00
|
|
|
|
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-10-10 07:26:04 +00:00
|
|
|
EntityLog.log(this, account.name + " closing");
|
|
|
|
db.account().setAccountState(account.id, "closing");
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
2018-09-08 05:42:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Stop syncs
|
|
|
|
for (Thread sync : syncs) {
|
|
|
|
sync.interrupt();
|
|
|
|
join(sync);
|
|
|
|
}
|
2018-10-09 19:39:25 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Close store
|
|
|
|
try {
|
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " store closing");
|
|
|
|
istore.close();
|
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " store closed");
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(Helper.TAG, account.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
2018-09-08 08:43:13 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
});
|
|
|
|
t.start();
|
|
|
|
try {
|
|
|
|
t.join(MessageHelper.NETWORK_TIMEOUT);
|
|
|
|
if (t.isAlive())
|
|
|
|
Log.w(Helper.TAG, account.name + " Close timeout");
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(Helper.TAG, account.name + " close wait " + ex.toString());
|
|
|
|
t.interrupt();
|
2018-09-08 08:43:13 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
EntityLog.log(this, account.name + " closed");
|
|
|
|
db.account().setAccountState(account.id, null);
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-09-08 08:43:13 +00:00
|
|
|
}
|
2018-10-10 08:07:37 +00:00
|
|
|
|
|
|
|
// Stop idlers
|
|
|
|
for (Thread idler : idlers) {
|
|
|
|
idler.interrupt();
|
|
|
|
join(idler);
|
|
|
|
}
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
if (state.running) {
|
|
|
|
try {
|
|
|
|
EntityLog.log(this, account.name + " backoff=" + backoff);
|
|
|
|
Thread.sleep(backoff * 1000L);
|
2018-08-13 18:22:18 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
if (backoff < CONNECT_BACKOFF_MAX)
|
|
|
|
backoff *= 2;
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(Helper.TAG, account.name + " backoff " + ex.toString());
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
EntityLog.log(this, account.name + " stopped");
|
2018-10-16 12:06:42 +00:00
|
|
|
wl0.release();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 17:31:33 +00:00
|
|
|
private void processOperations(EntityFolder folder, Session isession, IMAPStore istore, IMAPFolder ifolder) throws MessagingException, JSONException, IOException {
|
2018-08-12 13:04:24 +00:00
|
|
|
synchronized (lock) {
|
|
|
|
try {
|
|
|
|
Log.i(Helper.TAG, folder.name + " start process");
|
2018-08-12 10:51:09 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
DB db = DB.getInstance(this);
|
|
|
|
List<EntityOperation> ops = db.operation().getOperationsByFolder(folder.id);
|
|
|
|
Log.i(Helper.TAG, folder.name + " pending operations=" + ops.size());
|
|
|
|
for (EntityOperation op : ops)
|
2018-08-03 19:12:19 +00:00
|
|
|
try {
|
2018-08-12 13:04:24 +00:00
|
|
|
Log.i(Helper.TAG, folder.name +
|
|
|
|
" start op=" + op.id + "/" + op.name +
|
|
|
|
" msg=" + op.message +
|
|
|
|
" args=" + op.args);
|
2018-08-11 08:28:54 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
EntityMessage message = db.message().getMessage(op.message);
|
|
|
|
try {
|
2018-09-13 15:58:10 +00:00
|
|
|
if (message == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2018-08-17 05:49:38 +00:00
|
|
|
db.message().setMessageError(message.id, null);
|
|
|
|
|
2018-08-21 16:39:26 +00:00
|
|
|
if (message.uid == null &&
|
|
|
|
(EntityOperation.SEEN.equals(op.name) ||
|
|
|
|
EntityOperation.DELETE.equals(op.name) ||
|
2018-09-05 07:23:51 +00:00
|
|
|
EntityOperation.MOVE.equals(op.name) ||
|
|
|
|
EntityOperation.HEADERS.equals(op.name)))
|
2018-10-18 07:55:32 +00:00
|
|
|
throw new IllegalArgumentException(op.name + " without uid " + op.args);
|
2018-08-21 16:39:26 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
JSONArray jargs = new JSONArray(op.args);
|
2018-08-11 17:48:35 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
if (EntityOperation.SEEN.equals(op.name))
|
2018-08-12 15:31:43 +00:00
|
|
|
doSeen(folder, ifolder, message, jargs, db);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-09-07 15:12:43 +00:00
|
|
|
else if (EntityOperation.FLAG.equals(op.name))
|
|
|
|
doFlag(folder, ifolder, message, jargs, db);
|
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
else if (EntityOperation.ADD.equals(op.name))
|
2018-08-13 14:37:15 +00:00
|
|
|
doAdd(folder, isession, ifolder, message, jargs, db);
|
2018-08-08 11:58:50 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
else if (EntityOperation.MOVE.equals(op.name))
|
|
|
|
doMove(folder, isession, istore, ifolder, message, jargs, db);
|
2018-08-08 11:58:50 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
else if (EntityOperation.DELETE.equals(op.name))
|
|
|
|
doDelete(folder, ifolder, message, jargs, db);
|
2018-08-03 19:12:19 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
else if (EntityOperation.SEND.equals(op.name))
|
2018-08-27 14:31:45 +00:00
|
|
|
doSend(message, db);
|
2018-08-03 19:12:19 +00:00
|
|
|
|
2018-09-05 07:23:51 +00:00
|
|
|
else if (EntityOperation.HEADERS.equals(op.name))
|
|
|
|
doHeaders(folder, ifolder, message, db);
|
|
|
|
|
2018-09-15 07:22:42 +00:00
|
|
|
else if (EntityOperation.BODY.equals(op.name))
|
|
|
|
doBody(folder, ifolder, message, db);
|
|
|
|
|
2018-09-16 10:44:13 +00:00
|
|
|
else if (EntityOperation.ATTACHMENT.equals(op.name))
|
|
|
|
doAttachment(folder, op, ifolder, message, jargs, db);
|
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
else
|
|
|
|
throw new MessagingException("Unknown operation name=" + op.name);
|
2018-08-11 08:28:54 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
// Operation succeeded
|
2018-08-11 14:13:29 +00:00
|
|
|
db.operation().deleteOperation(op.id);
|
2018-08-12 13:04:24 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-08-14 12:56:26 +00:00
|
|
|
// TODO: SMTP response codes: https://www.ietf.org/rfc/rfc821.txt
|
2018-08-14 14:50:41 +00:00
|
|
|
if (ex instanceof SendFailedException)
|
2018-08-14 12:56:26 +00:00
|
|
|
reportError(null, folder.name, ex);
|
|
|
|
|
2018-09-14 17:01:12 +00:00
|
|
|
if (message != null)
|
|
|
|
db.message().setMessageError(message.id, Helper.formatThrowable(ex));
|
2018-08-11 14:13:29 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
if (ex instanceof MessageRemovedException ||
|
|
|
|
ex instanceof FolderNotFoundException ||
|
2018-08-14 14:50:41 +00:00
|
|
|
ex instanceof SendFailedException) {
|
|
|
|
Log.w(Helper.TAG, "Unrecoverable " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-08-12 13:04:24 +00:00
|
|
|
|
|
|
|
// There is no use in repeating
|
|
|
|
db.operation().deleteOperation(op.id);
|
|
|
|
continue;
|
|
|
|
} else if (ex instanceof MessagingException) {
|
|
|
|
// Socket timeout is a recoverable condition (send message)
|
|
|
|
if (ex.getCause() instanceof SocketTimeoutException) {
|
2018-08-14 14:50:41 +00:00
|
|
|
Log.w(Helper.TAG, "Recoverable " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-08-12 13:04:24 +00:00
|
|
|
// No need to inform user
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 08:28:54 +00:00
|
|
|
}
|
2018-08-03 19:12:19 +00:00
|
|
|
|
2018-08-12 13:04:24 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(Helper.TAG, folder.name + " end op=" + op.id + "/" + op.name);
|
2018-08-03 19:12:19 +00:00
|
|
|
}
|
2018-08-12 13:04:24 +00:00
|
|
|
} finally {
|
|
|
|
Log.i(Helper.TAG, folder.name + " end process");
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 15:31:43 +00:00
|
|
|
private void doSeen(EntityFolder folder, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws MessagingException, JSONException {
|
2018-08-11 17:31:33 +00:00
|
|
|
// Mark message (un)seen
|
2018-08-12 15:31:43 +00:00
|
|
|
boolean seen = jargs.getBoolean(0);
|
2018-09-17 05:22:17 +00:00
|
|
|
if (message.seen == seen)
|
|
|
|
return;
|
|
|
|
|
2018-08-09 20:45:42 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2018-08-12 15:31:43 +00:00
|
|
|
imessage.setFlag(Flags.Flag.SEEN, seen);
|
|
|
|
|
2018-08-13 14:44:47 +00:00
|
|
|
db.message().setMessageSeen(message.id, seen);
|
2018-08-09 20:45:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:12:43 +00:00
|
|
|
private void doFlag(EntityFolder folder, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws MessagingException, JSONException {
|
|
|
|
// Star/unstar message
|
|
|
|
boolean flagged = jargs.getBoolean(0);
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
imessage.setFlag(Flags.Flag.FLAGGED, flagged);
|
|
|
|
|
|
|
|
db.message().setMessageFlagged(message.id, flagged);
|
|
|
|
}
|
|
|
|
|
2018-08-19 06:53:56 +00:00
|
|
|
private void doAdd(EntityFolder folder, Session isession, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws MessagingException, JSONException, IOException {
|
2018-08-09 20:45:42 +00:00
|
|
|
// Append message
|
2018-08-11 17:31:33 +00:00
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
2018-09-13 06:05:47 +00:00
|
|
|
MimeMessage imessage = MessageHelper.from(this, message, null, attachments, isession);
|
2018-08-13 14:37:15 +00:00
|
|
|
AppendUID[] uid = ifolder.appendUIDMessages(new Message[]{imessage});
|
2018-09-03 17:48:48 +00:00
|
|
|
db.message().setMessageUid(message.id, uid[0].uid);
|
2018-09-04 08:08:57 +00:00
|
|
|
Log.i(Helper.TAG, "Appended uid=" + uid[0].uid);
|
2018-09-03 17:48:48 +00:00
|
|
|
|
2018-09-04 08:08:57 +00:00
|
|
|
if (message.uid != null) {
|
|
|
|
Message iprev = ifolder.getMessageByUID(message.uid);
|
2018-08-13 11:22:57 +00:00
|
|
|
if (iprev != null) {
|
2018-09-04 08:08:57 +00:00
|
|
|
Log.i(Helper.TAG, "Deleting existing uid=" + message.uid);
|
2018-08-13 11:22:57 +00:00
|
|
|
iprev.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
ifolder.expunge();
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 20:45:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 06:53:56 +00:00
|
|
|
private void doMove(EntityFolder folder, Session isession, IMAPStore istore, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws JSONException, MessagingException, IOException {
|
2018-08-11 17:31:33 +00:00
|
|
|
// Move message
|
2018-08-09 20:45:42 +00:00
|
|
|
long id = jargs.getLong(0);
|
|
|
|
EntityFolder target = db.folder().getFolder(id);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
|
|
|
|
|
|
|
// Get message
|
2018-08-11 14:13:29 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
2018-08-09 20:45:42 +00:00
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2018-08-11 17:31:33 +00:00
|
|
|
if (istore.hasCapability("MOVE")) {
|
|
|
|
Folder itarget = istore.getFolder(target.name);
|
|
|
|
ifolder.moveMessages(new Message[]{imessage}, itarget);
|
|
|
|
} else {
|
|
|
|
Log.w(Helper.TAG, "MOVE by DELETE/APPEND");
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
|
2018-08-12 15:31:43 +00:00
|
|
|
if (!EntityFolder.ARCHIVE.equals(folder.type)) {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
ifolder.expunge();
|
|
|
|
}
|
2018-08-11 17:31:33 +00:00
|
|
|
|
2018-09-13 06:05:47 +00:00
|
|
|
MimeMessageEx icopy = MessageHelper.from(this, message, null, attachments, isession);
|
2018-08-11 17:31:33 +00:00
|
|
|
Folder itarget = istore.getFolder(target.name);
|
2018-08-09 20:45:42 +00:00
|
|
|
itarget.appendMessages(new Message[]{icopy});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 10:51:09 +00:00
|
|
|
private void doDelete(EntityFolder folder, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws MessagingException, JSONException {
|
2018-08-09 20:45:42 +00:00
|
|
|
// Delete message
|
2018-08-12 10:51:09 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
ifolder.expunge();
|
2018-08-09 20:45:42 +00:00
|
|
|
|
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
}
|
|
|
|
|
2018-08-27 14:31:45 +00:00
|
|
|
private void doSend(EntityMessage message, DB db) throws MessagingException, IOException {
|
2018-08-09 20:45:42 +00:00
|
|
|
// Send message
|
|
|
|
EntityIdentity ident = db.identity().getIdentity(message.identity);
|
|
|
|
if (!ident.synchronize) {
|
|
|
|
// Message will remain in outbox
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-27 14:31:45 +00:00
|
|
|
// Create session
|
2018-09-21 13:18:26 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties(ident.auth_type);
|
2018-08-27 14:31:45 +00:00
|
|
|
final Session isession = Session.getInstance(props, null);
|
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
// Create message
|
|
|
|
MimeMessage imessage;
|
2018-08-27 14:31:45 +00:00
|
|
|
EntityMessage reply = (message.replying == null ? null : db.message().getMessage(message.replying));
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
2018-09-13 06:05:47 +00:00
|
|
|
imessage = MessageHelper.from(this, message, reply, attachments, isession);
|
2018-08-27 14:31:45 +00:00
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
if (ident.replyto != null)
|
|
|
|
imessage.setReplyTo(new Address[]{new InternetAddress(ident.replyto)});
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
// Create transport
|
|
|
|
// TODO: cache transport?
|
|
|
|
Transport itransport = isession.getTransport(ident.starttls ? "smtp" : "smtps");
|
|
|
|
try {
|
|
|
|
// Connect transport
|
2018-08-15 11:26:59 +00:00
|
|
|
db.identity().setIdentityState(ident.id, "connecting");
|
2018-09-21 13:09:22 +00:00
|
|
|
try {
|
|
|
|
itransport.connect(ident.host, ident.port, ident.user, ident.password);
|
|
|
|
} catch (AuthenticationFailedException ex) {
|
|
|
|
if (ident.auth_type == Helper.AUTH_TYPE_GMAIL) {
|
2018-09-29 12:45:56 +00:00
|
|
|
EntityAccount account = db.account().getAccount(ident.account);
|
|
|
|
ident.password = Helper.refreshToken(this, "com.google", ident.user, account.password);
|
2018-09-21 13:09:22 +00:00
|
|
|
DB.getInstance(this).identity().setIdentityPassword(ident.id, ident.password);
|
|
|
|
itransport.connect(ident.host, ident.port, ident.user, ident.password);
|
|
|
|
} else
|
|
|
|
throw ex;
|
|
|
|
}
|
2018-08-15 11:26:59 +00:00
|
|
|
db.identity().setIdentityState(ident.id, "connected");
|
|
|
|
db.identity().setIdentityError(ident.id, null);
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
// Send message
|
2018-08-14 12:56:26 +00:00
|
|
|
Address[] to = imessage.getAllRecipients();
|
|
|
|
itransport.sendMessage(imessage, to);
|
|
|
|
Log.i(Helper.TAG, "Sent via " + ident.host + "/" + ident.user +
|
|
|
|
" to " + TextUtils.join(", ", to));
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2018-08-17 07:14:12 +00:00
|
|
|
// Mark message as sent
|
|
|
|
// - will be moved to sent folder by synchronize message later
|
2018-08-12 11:17:50 +00:00
|
|
|
message.sent = imessage.getSentDate().getTime();
|
2018-08-09 20:45:42 +00:00
|
|
|
message.seen = true;
|
|
|
|
message.ui_seen = true;
|
|
|
|
db.message().updateMessage(message);
|
|
|
|
|
2018-08-25 14:24:26 +00:00
|
|
|
if (ident.store_sent) {
|
2018-08-17 07:14:12 +00:00
|
|
|
EntityFolder sent = db.folder().getFolderByType(ident.account, EntityFolder.SENT);
|
2018-08-16 18:19:09 +00:00
|
|
|
if (sent != null) {
|
2018-08-21 16:32:10 +00:00
|
|
|
message.folder = sent.id;
|
|
|
|
message.uid = null;
|
|
|
|
db.message().updateMessage(message);
|
2018-08-16 18:19:09 +00:00
|
|
|
Log.i(Helper.TAG, "Appending sent msgid=" + message.msgid);
|
|
|
|
EntityOperation.queue(db, message, EntityOperation.ADD); // Could already exist
|
|
|
|
}
|
2018-08-17 07:14:12 +00:00
|
|
|
}
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-08-11 06:50:21 +00:00
|
|
|
db.setTransactionSuccessful();
|
2018-08-09 20:45:42 +00:00
|
|
|
} finally {
|
2018-08-11 06:50:21 +00:00
|
|
|
db.endTransaction();
|
2018-08-09 20:45:42 +00:00
|
|
|
}
|
2018-08-16 14:27:55 +00:00
|
|
|
|
|
|
|
EntityOperation.process(this);
|
2018-08-15 11:26:59 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
db.identity().setIdentityError(ident.id, Helper.formatThrowable(ex));
|
|
|
|
throw ex;
|
2018-08-09 20:45:42 +00:00
|
|
|
} finally {
|
2018-08-15 11:26:59 +00:00
|
|
|
try {
|
|
|
|
itransport.close();
|
|
|
|
} finally {
|
|
|
|
db.identity().setIdentityState(ident.id, null);
|
|
|
|
}
|
2018-08-09 20:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 07:23:51 +00:00
|
|
|
private void doHeaders(EntityFolder folder, IMAPFolder ifolder, EntityMessage message, DB db) throws MessagingException {
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
2018-10-06 20:30:13 +00:00
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2018-09-05 07:23:51 +00:00
|
|
|
Enumeration<Header> headers = imessage.getAllHeaders();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
while (headers.hasMoreElements()) {
|
|
|
|
Header header = headers.nextElement();
|
|
|
|
sb.append(header.getName()).append(": ").append(header.getValue()).append("\n");
|
|
|
|
}
|
|
|
|
db.message().setMessageHeaders(message.id, sb.toString());
|
|
|
|
}
|
|
|
|
|
2018-09-15 07:22:42 +00:00
|
|
|
private void doBody(EntityFolder folder, IMAPFolder ifolder, EntityMessage message, DB db) throws MessagingException, IOException {
|
2018-09-16 10:44:13 +00:00
|
|
|
// Download message body
|
|
|
|
if (message.content)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get message
|
2018-09-15 07:22:42 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
2018-09-16 10:44:13 +00:00
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2018-09-15 07:22:42 +00:00
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
message.write(this, helper.getHtml());
|
2018-09-16 10:44:13 +00:00
|
|
|
db.message().setMessageContent(message.id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doAttachment(EntityFolder folder, EntityOperation op, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws JSONException, MessagingException, IOException {
|
|
|
|
// Download attachment
|
|
|
|
int sequence = jargs.getInt(0);
|
|
|
|
|
|
|
|
// Get attachment
|
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(op.message, sequence);
|
|
|
|
if (attachment.available)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
// Download attachment
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
EntityAttachment a = helper.getAttachments().get(sequence - 1);
|
|
|
|
attachment.part = a.part;
|
|
|
|
attachment.download(this, db);
|
2018-09-15 07:22:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:34:53 +00:00
|
|
|
private void synchronizeFolders(EntityAccount account, IMAPStore istore, ServiceState state) throws MessagingException {
|
2018-09-05 09:37:56 +00:00
|
|
|
DB db = DB.getInstance(this);
|
2018-08-02 13:33:06 +00:00
|
|
|
try {
|
2018-09-05 09:37:56 +00:00
|
|
|
db.beginTransaction();
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-16 07:56:49 +00:00
|
|
|
Log.v(Helper.TAG, "Start sync folders account=" + account.name);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
List<String> names = new ArrayList<>();
|
2018-08-12 12:47:52 +00:00
|
|
|
for (EntityFolder folder : db.folder().getUserFolders(account.id))
|
2018-08-02 13:33:06 +00:00
|
|
|
names.add(folder.name);
|
|
|
|
Log.i(Helper.TAG, "Local folder count=" + names.size());
|
|
|
|
|
2018-10-16 07:56:49 +00:00
|
|
|
Folder[] ifolders = istore.getDefaultFolder().list("*");
|
2018-08-02 13:33:06 +00:00
|
|
|
Log.i(Helper.TAG, "Remote folder count=" + ifolders.length);
|
|
|
|
|
|
|
|
for (Folder ifolder : ifolders) {
|
2018-10-16 12:25:21 +00:00
|
|
|
boolean selectable = true;
|
|
|
|
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
|
|
|
|
for (String attr : attrs) {
|
|
|
|
if ("\\Noselect".equals(attr))
|
|
|
|
selectable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectable) {
|
|
|
|
EntityFolder folder = db.folder().getFolderByName(account.id, ifolder.getFullName());
|
|
|
|
if (folder == null) {
|
|
|
|
folder = new EntityFolder();
|
|
|
|
folder.account = account.id;
|
|
|
|
folder.name = ifolder.getFullName();
|
|
|
|
folder.type = EntityFolder.USER;
|
|
|
|
folder.synchronize = false;
|
|
|
|
folder.after = EntityFolder.DEFAULT_USER_SYNC;
|
|
|
|
db.folder().insertFolder(folder);
|
|
|
|
Log.i(Helper.TAG, folder.name + " added");
|
|
|
|
} else {
|
|
|
|
names.remove(folder.name);
|
|
|
|
Log.i(Helper.TAG, folder.name + " exists");
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Delete local folder=" + names.size());
|
2018-10-16 12:25:21 +00:00
|
|
|
for (String name : names) {
|
2018-08-12 12:47:52 +00:00
|
|
|
db.folder().deleteFolder(account.id, name);
|
2018-10-16 12:25:21 +00:00
|
|
|
Log.i(Helper.TAG, name + " deleted");
|
|
|
|
}
|
2018-09-05 09:37:56 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
2018-08-02 13:33:06 +00:00
|
|
|
} finally {
|
2018-09-05 09:37:56 +00:00
|
|
|
db.endTransaction();
|
2018-08-15 07:40:18 +00:00
|
|
|
Log.v(Helper.TAG, "End sync folder");
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:34:53 +00:00
|
|
|
private void synchronizeMessages(EntityAccount account, EntityFolder folder, IMAPFolder ifolder, ServiceState state) throws MessagingException, IOException {
|
2018-08-28 06:55:57 +00:00
|
|
|
DB db = DB.getInstance(this);
|
2018-08-02 13:33:06 +00:00
|
|
|
try {
|
2018-08-15 07:40:18 +00:00
|
|
|
Log.v(Helper.TAG, folder.name + " start sync after=" + folder.after);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-28 06:55:57 +00:00
|
|
|
db.folder().setFolderState(folder.id, "syncing");
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
// Get reference times
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.add(Calendar.DAY_OF_MONTH, -folder.after);
|
|
|
|
cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal.set(Calendar.MINUTE, 0);
|
|
|
|
cal.set(Calendar.SECOND, 0);
|
|
|
|
cal.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
long ago = cal.getTimeInMillis();
|
2018-08-23 19:53:04 +00:00
|
|
|
if (ago < 0)
|
|
|
|
ago = 0;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " ago=" + new Date(ago));
|
|
|
|
|
|
|
|
// Delete old local messages
|
2018-08-12 12:47:52 +00:00
|
|
|
int old = db.message().deleteMessagesBefore(folder.id, ago);
|
2018-08-02 13:33:06 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " local old=" + old);
|
|
|
|
|
|
|
|
// Get list of local uids
|
2018-08-12 12:47:52 +00:00
|
|
|
List<Long> uids = db.message().getUids(folder.id, ago);
|
2018-08-02 13:33:06 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " local count=" + uids.size());
|
|
|
|
|
|
|
|
// Reduce list of local uids
|
|
|
|
long search = SystemClock.elapsedRealtime();
|
|
|
|
Message[] imessages = ifolder.search(new ReceivedDateTerm(ComparisonTerm.GE, new Date(ago)));
|
|
|
|
Log.i(Helper.TAG, folder.name + " remote count=" + imessages.length +
|
|
|
|
" search=" + (SystemClock.elapsedRealtime() - search) + " ms");
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
2018-09-16 20:15:43 +00:00
|
|
|
fp.add(FetchProfile.Item.FLAGS);
|
2018-08-02 13:33:06 +00:00
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
long fetch = SystemClock.elapsedRealtime();
|
|
|
|
Log.i(Helper.TAG, folder.name + " remote fetched=" + (SystemClock.elapsedRealtime() - fetch) + " ms");
|
|
|
|
|
2018-08-21 19:34:53 +00:00
|
|
|
for (Message imessage : imessages) {
|
|
|
|
if (!state.running)
|
|
|
|
return;
|
|
|
|
|
2018-08-02 16:24:23 +00:00
|
|
|
try {
|
2018-08-06 11:51:14 +00:00
|
|
|
uids.remove(ifolder.getUID(imessage));
|
2018-08-02 16:24:23 +00:00
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-08-18 20:57:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-09-08 05:38:46 +00:00
|
|
|
reportError(account.name, folder.name, ex);
|
2018-08-18 20:57:29 +00:00
|
|
|
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-08-21 19:34:53 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
// Delete local messages not at remote
|
|
|
|
Log.i(Helper.TAG, folder.name + " delete=" + uids.size());
|
|
|
|
for (Long uid : uids) {
|
2018-08-12 12:47:52 +00:00
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
2018-08-06 11:05:14 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " delete local uid=" + uid + " count=" + count);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 18:07:26 +00:00
|
|
|
fp.add(FetchProfile.Item.ENVELOPE);
|
2018-09-16 20:15:43 +00:00
|
|
|
// fp.add(FetchProfile.Item.FLAGS);
|
2018-09-16 18:07:26 +00:00
|
|
|
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);
|
|
|
|
|
2018-08-06 11:51:14 +00:00
|
|
|
// Add/update local messages
|
2018-09-16 14:00:36 +00:00
|
|
|
Long[] ids = new Long[imessages.length];
|
2018-08-06 14:04:35 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " add=" + imessages.length);
|
2018-09-16 18:07:26 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0; i -= SYNC_BATCH_SIZE) {
|
2018-09-16 18:43:05 +00:00
|
|
|
int from = Math.max(0, i - SYNC_BATCH_SIZE + 1);
|
2018-09-20 09:10:19 +00:00
|
|
|
//Log.i(Helper.TAG, folder.name + " update " + from + " .. " + i);
|
2018-09-16 18:07:26 +00:00
|
|
|
|
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
2018-09-16 20:15:43 +00:00
|
|
|
|
|
|
|
// Full fetch new/changed messages only
|
|
|
|
List<Message> full = new ArrayList<>();
|
|
|
|
for (Message imessage : isub) {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
2018-10-20 17:56:09 +00:00
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid, false);
|
2018-09-16 20:15:43 +00:00
|
|
|
if (message == null)
|
|
|
|
full.add(imessage);
|
|
|
|
}
|
2018-10-07 10:13:32 +00:00
|
|
|
if (full.size() > 0) {
|
|
|
|
long headers = SystemClock.elapsedRealtime();
|
|
|
|
ifolder.fetch(full.toArray(new Message[0]), fp);
|
|
|
|
Log.i(Helper.TAG, folder.name + " fetched headers=" + full.size() +
|
|
|
|
" " + (SystemClock.elapsedRealtime() - headers) + " ms");
|
|
|
|
}
|
2018-09-16 18:07:26 +00:00
|
|
|
|
|
|
|
for (int j = isub.length - 1; j >= 0; j--)
|
|
|
|
try {
|
2018-09-21 11:53:54 +00:00
|
|
|
db.beginTransaction();
|
2018-09-16 18:07:26 +00:00
|
|
|
ids[from + j] = synchronizeMessage(this, folder, ifolder, (IMAPMessage) isub[j], false);
|
2018-09-21 11:53:54 +00:00
|
|
|
db.setTransactionSuccessful();
|
2018-09-22 13:37:25 +00:00
|
|
|
Thread.sleep(20);
|
2018-09-16 18:07:26 +00:00
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (FolderClosedIOException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
} finally {
|
2018-09-21 11:53:54 +00:00
|
|
|
db.endTransaction();
|
2018-09-16 20:15:43 +00:00
|
|
|
// Reduce memory usage
|
2018-09-17 08:16:49 +00:00
|
|
|
((IMAPMessage) isub[j]).invalidateHeaders();
|
2018-09-16 18:07:26 +00:00
|
|
|
}
|
2018-09-22 13:37:25 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(100);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
2018-09-16 18:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.folder().setFolderState(folder.id, "downloading");
|
|
|
|
|
|
|
|
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
2018-08-12 12:47:52 +00:00
|
|
|
|
2018-09-16 14:00:36 +00:00
|
|
|
// Download messages/attachments
|
|
|
|
Log.i(Helper.TAG, folder.name + " download=" + imessages.length);
|
2018-09-16 18:07:26 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0; i -= DOWNLOAD_BATCH_SIZE) {
|
2018-09-16 18:43:05 +00:00
|
|
|
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1);
|
2018-09-20 09:10:19 +00:00
|
|
|
//Log.i(Helper.TAG, folder.name + " download " + from + " .. " + i);
|
2018-09-16 18:07:26 +00:00
|
|
|
|
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
2018-09-16 20:15:43 +00:00
|
|
|
// Fetch on demand
|
2018-09-16 18:07:26 +00:00
|
|
|
|
|
|
|
for (int j = isub.length - 1; j >= 0; j--)
|
2018-09-16 14:00:36 +00:00
|
|
|
try {
|
2018-09-20 09:10:19 +00:00
|
|
|
//Log.i(Helper.TAG, folder.name + " download index=" + (from + j) + " id=" + ids[from + j]);
|
2018-09-22 13:37:25 +00:00
|
|
|
if (ids[from + j] != null) {
|
2018-09-17 08:16:49 +00:00
|
|
|
downloadMessage(this, folder, ifolder, (IMAPMessage) isub[j], ids[from + j]);
|
2018-09-22 13:37:25 +00:00
|
|
|
Thread.sleep(20);
|
|
|
|
}
|
2018-09-16 14:00:36 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
2018-09-16 15:18:57 +00:00
|
|
|
} catch (FolderClosedIOException ex) {
|
|
|
|
throw ex;
|
2018-09-16 14:00:36 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
2018-09-16 18:07:26 +00:00
|
|
|
} finally {
|
|
|
|
// Free memory
|
|
|
|
((IMAPMessage) isub[j]).invalidateHeaders();
|
2018-09-16 14:00:36 +00:00
|
|
|
}
|
2018-09-22 13:37:25 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(100);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
2018-09-16 18:07:26 +00:00
|
|
|
}
|
2018-09-16 14:00:36 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
} finally {
|
2018-08-15 07:40:18 +00:00
|
|
|
Log.v(Helper.TAG, folder.name + " end sync");
|
2018-08-28 06:55:57 +00:00
|
|
|
db.folder().setFolderState(folder.id, ifolder.isOpen() ? "connected" : "disconnected");
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:00:36 +00:00
|
|
|
static Long synchronizeMessage(Context context, EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage, boolean found) throws MessagingException, IOException {
|
2018-09-16 18:07:26 +00:00
|
|
|
long uid = ifolder.getUID(imessage);
|
2018-08-02 20:52:06 +00:00
|
|
|
|
2018-09-16 18:07:26 +00:00
|
|
|
if (imessage.isExpunged()) {
|
|
|
|
Log.i(Helper.TAG, folder.name + " expunged uid=" + uid);
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
}
|
|
|
|
if (imessage.isSet(Flags.Flag.DELETED)) {
|
|
|
|
Log.i(Helper.TAG, folder.name + " deleted uid=" + uid);
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
}
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-09-16 18:07:26 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage);
|
|
|
|
boolean seen = helper.getSeen();
|
|
|
|
boolean flagged = helper.getFlagged();
|
2018-08-09 20:45:42 +00:00
|
|
|
|
2018-09-16 18:07:26 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2018-08-12 11:46:34 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
// Find message by uid (fast, no headers required)
|
2018-10-20 17:56:09 +00:00
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid, found);
|
2018-09-21 11:53:54 +00:00
|
|
|
|
|
|
|
// Find message by Message-ID (slow, headers required)
|
|
|
|
// - messages in inbox have same id as message sent to self
|
|
|
|
// - messages in archive have same id as original
|
|
|
|
if (message == null) {
|
|
|
|
// Will fetch headers within database transaction
|
|
|
|
String msgid = helper.getMessageID();
|
|
|
|
String[] refs = helper.getReferences();
|
|
|
|
String reference = (refs.length == 1 && refs[0].indexOf(BuildConfig.APPLICATION_ID) > 0 ? refs[0] : msgid);
|
|
|
|
Log.i(Helper.TAG, "Searching for " + msgid + " / " + reference);
|
2018-10-20 17:56:09 +00:00
|
|
|
for (EntityMessage dup : db.message().getMessageByMsgId(folder.account, msgid, reference, found)) {
|
2018-09-21 11:53:54 +00:00
|
|
|
EntityFolder dfolder = db.folder().getFolder(dup.folder);
|
|
|
|
boolean outbox = EntityFolder.OUTBOX.equals(dfolder.type);
|
2018-10-18 08:21:42 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " found as id=" + dup.id + "/" + dup.uid +
|
|
|
|
" folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id +
|
|
|
|
" msgid=" + dup.msgid + " thread=" + dup.thread);
|
2018-09-21 11:53:54 +00:00
|
|
|
|
|
|
|
if (dup.folder.equals(folder.id) || outbox) {
|
2018-10-18 08:21:42 +00:00
|
|
|
String thread = helper.getThreadId(uid);
|
|
|
|
Log.i(Helper.TAG, folder.name + " found as id=" + dup.id + "/" + uid +
|
|
|
|
" msgid=" + msgid + " thread=" + thread);
|
2018-09-21 11:53:54 +00:00
|
|
|
dup.folder = folder.id;
|
|
|
|
dup.uid = uid;
|
2018-10-18 08:21:42 +00:00
|
|
|
dup.msgid = msgid;
|
|
|
|
dup.thread = thread;
|
|
|
|
dup.error = null;
|
2018-09-21 11:53:54 +00:00
|
|
|
db.message().updateMessage(dup);
|
|
|
|
message = dup;
|
2018-08-12 11:46:34 +00:00
|
|
|
}
|
2018-09-16 18:07:26 +00:00
|
|
|
}
|
2018-09-21 11:53:54 +00:00
|
|
|
}
|
2018-08-11 14:13:29 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
if (message == null) {
|
|
|
|
message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.uid = uid;
|
2018-09-04 08:28:29 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
if (!EntityFolder.ARCHIVE.equals(folder.type)) {
|
|
|
|
message.msgid = helper.getMessageID();
|
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
|
|
|
Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid);
|
|
|
|
}
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
message.references = TextUtils.join(" ", helper.getReferences());
|
|
|
|
message.inreplyto = helper.getInReplyTo();
|
|
|
|
message.deliveredto = helper.getDeliveredTo();
|
|
|
|
message.thread = helper.getThreadId(uid);
|
|
|
|
message.from = helper.getFrom();
|
|
|
|
message.to = helper.getTo();
|
|
|
|
message.cc = helper.getCc();
|
|
|
|
message.bcc = helper.getBcc();
|
|
|
|
message.reply = helper.getReply();
|
|
|
|
message.subject = imessage.getSubject();
|
|
|
|
message.size = helper.getSize();
|
|
|
|
message.content = false;
|
|
|
|
message.received = imessage.getReceivedDate().getTime();
|
|
|
|
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime());
|
|
|
|
message.seen = seen;
|
|
|
|
message.ui_seen = seen;
|
|
|
|
message.flagged = false;
|
|
|
|
message.ui_flagged = false;
|
|
|
|
message.ui_hide = false;
|
|
|
|
message.ui_found = found;
|
2018-10-16 11:29:12 +00:00
|
|
|
message.ui_ignored = false;
|
2018-09-21 11:53:54 +00:00
|
|
|
|
|
|
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
|
|
|
|
== PackageManager.PERMISSION_GRANTED) {
|
|
|
|
try {
|
|
|
|
if (message.from != null)
|
|
|
|
for (int i = 0; i < message.from.length; i++) {
|
|
|
|
String email = ((InternetAddress) message.from[i]).getAddress();
|
|
|
|
Cursor cursor = null;
|
|
|
|
try {
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
|
|
|
new String[]{
|
|
|
|
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
|
|
|
ContactsContract.Contacts.DISPLAY_NAME
|
|
|
|
},
|
|
|
|
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
|
|
|
|
new String[]{email}, null);
|
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
|
|
|
|
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
|
|
|
|
long contactId = cursor.getLong(colContactId);
|
|
|
|
String displayName = cursor.getString(colDisplayName);
|
|
|
|
|
|
|
|
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
|
|
|
|
message.avatar = uri.toString();
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(displayName))
|
|
|
|
((InternetAddress) message.from[i]).setPersonal(displayName);
|
2018-09-20 11:16:23 +00:00
|
|
|
}
|
2018-09-21 11:53:54 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
2018-09-20 11:16:23 +00:00
|
|
|
}
|
2018-09-21 11:53:54 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
2018-09-20 11:16:23 +00:00
|
|
|
}
|
2018-09-21 11:53:54 +00:00
|
|
|
}
|
2018-09-20 11:16:23 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
int sequence = 1;
|
|
|
|
for (EntityAttachment attachment : helper.getAttachments()) {
|
2018-09-21 13:27:11 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " attachment seq=" + sequence +
|
|
|
|
" name=" + attachment.name + " type=" + attachment.type + " cid=" + attachment.cid);
|
|
|
|
if (!TextUtils.isEmpty(attachment.cid) &&
|
|
|
|
db.attachment().getAttachment(message.id, attachment.cid) != null) {
|
|
|
|
Log.i(Helper.TAG, "Skipping duplicated CID");
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-21 11:53:54 +00:00
|
|
|
attachment.message = message.id;
|
|
|
|
attachment.sequence = sequence++;
|
|
|
|
attachment.id = db.attachment().insertAttachment(attachment);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (message.seen != seen || message.seen != message.ui_seen) {
|
|
|
|
message.seen = seen;
|
|
|
|
message.ui_seen = seen;
|
2018-09-16 18:07:26 +00:00
|
|
|
db.message().updateMessage(message);
|
2018-09-21 11:53:54 +00:00
|
|
|
Log.i(Helper.TAG, folder.name + " updated id=" + message.id + " uid=" + message.uid + " seen=" + seen);
|
2018-09-16 18:07:26 +00:00
|
|
|
}
|
2018-08-17 05:26:50 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
if (message.flagged != flagged || message.flagged != message.ui_flagged) {
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.ui_flagged = flagged;
|
|
|
|
db.message().updateMessage(message);
|
|
|
|
Log.i(Helper.TAG, folder.name + " updated id=" + message.id + " uid=" + message.uid + " flagged=" + flagged);
|
|
|
|
}
|
2018-09-16 14:00:36 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
if (message.ui_hide) {
|
|
|
|
message.ui_hide = false;
|
|
|
|
db.message().updateMessage(message);
|
|
|
|
Log.i(Helper.TAG, folder.name + " unhidden id=" + message.id + " uid=" + message.uid);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-09-21 11:53:54 +00:00
|
|
|
|
|
|
|
return message.id;
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 08:16:49 +00:00
|
|
|
private static void downloadMessage(Context context, EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage, long id) throws MessagingException, IOException {
|
2018-09-16 14:00:36 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
2018-10-13 13:30:00 +00:00
|
|
|
if (message == null)
|
|
|
|
return;
|
2018-09-17 08:16:49 +00:00
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
2018-09-16 14:00:36 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage);
|
|
|
|
|
|
|
|
ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
|
2018-09-16 15:18:57 +00:00
|
|
|
boolean metered = (cm == null || cm.isActiveNetworkMetered());
|
2018-09-16 14:00:36 +00:00
|
|
|
|
2018-09-17 08:16:49 +00:00
|
|
|
boolean fetch = false;
|
|
|
|
if (!message.content)
|
|
|
|
if (!metered || (message.size != null && message.size < MESSAGE_AUTO_DOWNLOAD_SIZE))
|
|
|
|
fetch = true;
|
|
|
|
|
|
|
|
if (!fetch)
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
if (!attachment.available)
|
2018-10-01 13:05:14 +00:00
|
|
|
if (!metered || (attachment.size != null && attachment.size < ATTACHMENT_AUTO_DOWNLOAD_SIZE)) {
|
2018-09-17 08:16:49 +00:00
|
|
|
fetch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetch) {
|
|
|
|
Log.i(Helper.TAG, folder.name + " fetching message id=" + message.id);
|
|
|
|
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(new Message[]{imessage}, fp);
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:00:36 +00:00
|
|
|
if (!message.content)
|
|
|
|
if (!metered || (message.size != null && message.size < MESSAGE_AUTO_DOWNLOAD_SIZE)) {
|
|
|
|
message.write(context, helper.getHtml());
|
|
|
|
db.message().setMessageContent(message.id, true);
|
|
|
|
Log.i(Helper.TAG, folder.name + " downloaded message id=" + message.id + " size=" + message.size);
|
|
|
|
}
|
|
|
|
|
2018-09-17 08:16:49 +00:00
|
|
|
List<EntityAttachment> iattachments = null;
|
|
|
|
for (int i = 0; i < attachments.size(); i++) {
|
|
|
|
EntityAttachment attachment = attachments.get(i);
|
|
|
|
if (!attachment.available)
|
2018-10-01 13:05:14 +00:00
|
|
|
if (!metered || (attachment.size != null && attachment.size < ATTACHMENT_AUTO_DOWNLOAD_SIZE)) {
|
2018-09-17 08:16:49 +00:00
|
|
|
if (iattachments == null)
|
|
|
|
iattachments = helper.getAttachments();
|
|
|
|
attachment.part = iattachments.get(i).part;
|
|
|
|
attachment.download(context, db);
|
|
|
|
Log.i(Helper.TAG, folder.name + " downloaded message id=" + message.id + " attachment=" + attachment.name + " size=" + message.size);
|
|
|
|
}
|
2018-09-16 14:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 18:46:14 +00:00
|
|
|
private class ServiceManager extends ConnectivityManager.NetworkCallback {
|
2018-08-28 12:12:07 +00:00
|
|
|
private ServiceState state;
|
2018-08-15 19:34:22 +00:00
|
|
|
private boolean running = false;
|
2018-09-30 10:13:24 +00:00
|
|
|
private long lastLost = 0;
|
2018-08-02 13:33:06 +00:00
|
|
|
private EntityFolder outbox = null;
|
2018-09-04 07:02:54 +00:00
|
|
|
private ExecutorService lifecycle = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
|
|
|
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAvailable(Network network) {
|
2018-09-03 19:11:16 +00:00
|
|
|
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
|
|
|
|
NetworkInfo ni = cm.getNetworkInfo(network);
|
2018-09-08 07:01:02 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Network available " + network + " running=" + running + " " + ni);
|
2018-08-15 07:40:18 +00:00
|
|
|
|
2018-09-08 07:01:02 +00:00
|
|
|
if (!running) {
|
2018-08-15 19:34:22 +00:00
|
|
|
running = true;
|
2018-08-15 07:40:18 +00:00
|
|
|
lifecycle.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-08-15 19:34:22 +00:00
|
|
|
Log.i(Helper.TAG, "Starting service");
|
2018-08-15 07:40:18 +00:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-15 07:40:18 +00:00
|
|
|
@Override
|
|
|
|
public void onLost(Network network) {
|
2018-09-08 07:01:02 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Network lost " + network + " running=" + running);
|
2018-08-15 07:40:18 +00:00
|
|
|
|
2018-08-15 19:34:22 +00:00
|
|
|
if (running) {
|
2018-08-15 07:40:18 +00:00
|
|
|
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
|
2018-09-30 13:49:01 +00:00
|
|
|
NetworkInfo ani = (network == null ? null : cm.getActiveNetworkInfo());
|
2018-09-08 07:01:02 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Network active=" + (ani == null ? null : ani.toString()));
|
2018-09-03 19:11:16 +00:00
|
|
|
if (ani == null || !ani.isConnected()) {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Network disconnected=" + ani);
|
2018-08-15 19:34:22 +00:00
|
|
|
running = false;
|
2018-09-30 10:13:24 +00:00
|
|
|
lastLost = new Date().getTime();
|
2018-08-15 07:40:18 +00:00
|
|
|
lifecycle.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-09-08 07:01:02 +00:00
|
|
|
stop();
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-08 07:01:02 +00:00
|
|
|
}
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 19:34:22 +00:00
|
|
|
private void start() {
|
2018-09-04 05:49:58 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main start");
|
2018-09-12 08:16:38 +00:00
|
|
|
|
2018-08-28 12:12:07 +00:00
|
|
|
state = new ServiceState();
|
2018-10-07 18:43:44 +00:00
|
|
|
state.thread = new Thread(new Runnable() {
|
|
|
|
private List<ServiceState> threadState = new ArrayList<>();
|
2018-08-13 19:56:53 +00:00
|
|
|
|
2018-08-10 14:56:00 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-10-10 07:26:04 +00:00
|
|
|
PowerManager pm = getSystemService(PowerManager.class);
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":start");
|
2018-08-10 14:56:00 +00:00
|
|
|
try {
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.acquire();
|
|
|
|
|
2018-09-12 08:16:38 +00:00
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
|
|
|
|
2018-08-14 11:45:12 +00:00
|
|
|
outbox = db.folder().getOutbox();
|
|
|
|
if (outbox == null) {
|
2018-09-08 07:01:02 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "No outbox, halt");
|
2018-08-14 11:45:12 +00:00
|
|
|
stopSelf();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-10 14:56:00 +00:00
|
|
|
List<EntityAccount> accounts = db.account().getAccounts(true);
|
|
|
|
if (accounts.size() == 0) {
|
2018-09-08 07:01:02 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "No accounts, halt");
|
2018-08-10 14:56:00 +00:00
|
|
|
stopSelf();
|
2018-08-14 11:45:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-09-30 10:13:24 +00:00
|
|
|
long ago = new Date().getTime() - lastLost;
|
|
|
|
if (ago < RECONNECT_BACKOFF)
|
|
|
|
try {
|
|
|
|
long backoff = RECONNECT_BACKOFF - ago;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main backoff=" + (backoff / 1000));
|
|
|
|
Thread.sleep(backoff);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(Helper.TAG, "main backoff " + ex.toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:45:12 +00:00
|
|
|
// Start monitoring outbox
|
2018-08-26 08:33:22 +00:00
|
|
|
IntentFilter f = new IntentFilter();
|
|
|
|
f.addAction(ACTION_SYNCHRONIZE_FOLDER);
|
|
|
|
f.addAction(ACTION_PROCESS_OPERATIONS);
|
2018-08-11 08:37:56 +00:00
|
|
|
f.addDataType("account/outbox");
|
2018-08-10 14:56:00 +00:00
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this);
|
2018-08-11 08:37:56 +00:00
|
|
|
lbm.registerReceiver(outboxReceiver, f);
|
2018-10-17 10:11:24 +00:00
|
|
|
|
2018-08-14 11:45:12 +00:00
|
|
|
db.folder().setFolderState(outbox.id, "connected");
|
2018-09-16 11:32:03 +00:00
|
|
|
db.folder().setFolderError(outbox.id, null);
|
2018-08-11 08:37:56 +00:00
|
|
|
|
|
|
|
lbm.sendBroadcast(new Intent(ACTION_PROCESS_OPERATIONS)
|
|
|
|
.setType("account/outbox")
|
|
|
|
.putExtra("folder", outbox.id));
|
2018-08-14 11:45:12 +00:00
|
|
|
|
|
|
|
// Start monitoring accounts
|
|
|
|
for (final EntityAccount account : accounts) {
|
|
|
|
Log.i(Helper.TAG, account.host + "/" + account.user + " run");
|
2018-09-14 17:12:54 +00:00
|
|
|
final ServiceState astate = new ServiceState();
|
2018-10-07 18:43:44 +00:00
|
|
|
astate.thread = new Thread(new Runnable() {
|
2018-08-14 11:45:12 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2018-09-14 17:12:54 +00:00
|
|
|
monitorAccount(account, astate);
|
2018-08-14 11:45:12 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
// Fall-safe
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "sync.account." + account.id);
|
2018-10-07 18:43:44 +00:00
|
|
|
astate.thread.start();
|
|
|
|
threadState.add(astate);
|
2018-08-14 11:45:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 08:16:38 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main started");
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2018-10-07 18:43:44 +00:00
|
|
|
try {
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.release();
|
2018-10-07 18:43:44 +00:00
|
|
|
Thread.sleep(Long.MAX_VALUE);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(Helper.TAG, "main wait " + ex.toString());
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.acquire();
|
2018-09-05 19:18:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:45:12 +00:00
|
|
|
// Stop monitoring accounts
|
2018-10-07 18:43:44 +00:00
|
|
|
for (ServiceState astate : threadState) {
|
|
|
|
astate.running = false;
|
|
|
|
astate.thread.interrupt();
|
|
|
|
join(astate.thread);
|
2018-09-05 19:18:33 +00:00
|
|
|
}
|
2018-09-14 17:12:54 +00:00
|
|
|
threadState.clear();
|
2018-08-14 11:45:12 +00:00
|
|
|
|
|
|
|
// Stop monitoring outbox
|
|
|
|
lbm.unregisterReceiver(outboxReceiver);
|
|
|
|
Log.i(Helper.TAG, outbox.name + " unlisten operations");
|
|
|
|
db.folder().setFolderState(outbox.id, null);
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2018-09-04 05:49:58 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main exited");
|
2018-08-11 08:37:56 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-08-14 11:45:12 +00:00
|
|
|
// Fail-safe
|
2018-08-11 08:37:56 +00:00
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
wl.release();
|
2018-10-11 15:43:09 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Start wake lock=" + wl.isHeld());
|
2018-08-10 14:56:00 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-08-13 19:56:53 +00:00
|
|
|
}, "sync.main");
|
2018-10-07 18:43:44 +00:00
|
|
|
state.thread.setPriority(THREAD_PRIORITY_BACKGROUND); // will be inherited
|
|
|
|
state.thread.start();
|
2018-10-10 08:00:42 +00:00
|
|
|
yieldWakelock();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 07:01:02 +00:00
|
|
|
private void stop() {
|
2018-10-10 07:26:04 +00:00
|
|
|
PowerManager pm = getSystemService(PowerManager.class);
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":stop");
|
|
|
|
try {
|
|
|
|
wl.acquire();
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main stop");
|
2018-09-12 08:16:38 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state.running = false;
|
|
|
|
state.thread.interrupt();
|
|
|
|
join(state.thread);
|
2018-08-14 11:45:12 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main stopped");
|
2018-09-12 08:16:38 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
state = null;
|
|
|
|
} finally {
|
|
|
|
wl.release();
|
2018-10-11 15:43:09 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Stop wake lock=" + wl.isHeld());
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 13:30:00 +00:00
|
|
|
private void queue_reload() {
|
2018-09-11 16:29:43 +00:00
|
|
|
if (running)
|
|
|
|
lifecycle.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
stop();
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
});
|
2018-08-22 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 13:30:00 +00:00
|
|
|
private void queue_start() {
|
|
|
|
if (!running) {
|
|
|
|
running = true;
|
|
|
|
lifecycle.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void queue_stop() {
|
|
|
|
if (running) {
|
|
|
|
running = false;
|
|
|
|
lifecycle.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
stop();
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:45:12 +00:00
|
|
|
private BroadcastReceiver outboxReceiver = new BroadcastReceiver() {
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
2018-08-28 06:55:57 +00:00
|
|
|
public void onReceive(final Context context, Intent intent) {
|
2018-08-15 07:40:18 +00:00
|
|
|
Log.v(Helper.TAG, outbox.name + " run operations");
|
2018-08-14 12:56:26 +00:00
|
|
|
|
2018-08-21 19:08:51 +00:00
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-10-10 07:26:04 +00:00
|
|
|
PowerManager pm = getSystemService(PowerManager.class);
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
BuildConfig.APPLICATION_ID + ":outbox");
|
|
|
|
|
2018-08-21 19:08:51 +00:00
|
|
|
try {
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.acquire();
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
Log.i(Helper.TAG, outbox.name + " start operations");
|
|
|
|
db.folder().setFolderState(outbox.id, "syncing");
|
|
|
|
processOperations(outbox, null, null, null);
|
2018-10-18 07:55:32 +00:00
|
|
|
db.folder().setFolderError(outbox.id, null);
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, outbox.name + " " + ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
reportError(null, outbox.name, ex);
|
2018-09-08 05:42:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.folder().setFolderError(outbox.id, Helper.formatThrowable(ex));
|
|
|
|
} finally {
|
|
|
|
Log.i(Helper.TAG, outbox.name + " end operations");
|
|
|
|
db.folder().setFolderState(outbox.id, null);
|
|
|
|
}
|
2018-08-21 19:08:51 +00:00
|
|
|
} finally {
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.release();
|
2018-10-11 15:43:09 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Outbox wake lock=" + wl.isHeld());
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-08-21 19:08:51 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
};
|
2018-08-14 18:46:14 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-09-08 07:01:02 +00:00
|
|
|
private void join(Thread thread) {
|
2018-08-15 07:40:18 +00:00
|
|
|
boolean joined = false;
|
|
|
|
while (!joined)
|
|
|
|
try {
|
2018-09-08 11:44:42 +00:00
|
|
|
Log.i(Helper.TAG, "Joining " + thread.getName());
|
2018-08-15 07:40:18 +00:00
|
|
|
thread.join();
|
|
|
|
joined = true;
|
2018-09-08 11:44:42 +00:00
|
|
|
Log.i(Helper.TAG, "Joined " + thread.getName());
|
2018-08-15 07:40:18 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2018-09-08 07:01:02 +00:00
|
|
|
Log.w(Helper.TAG, thread.getName() + " join " + ex.toString());
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 08:00:42 +00:00
|
|
|
private void yieldWakelock() {
|
|
|
|
try {
|
|
|
|
// Give interrupted thread some time to acquire wake lock
|
|
|
|
Thread.sleep(500L);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 13:30:00 +00:00
|
|
|
public static void init(Context context) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
if (prefs.getBoolean("enabled", true))
|
2018-10-17 06:00:48 +00:00
|
|
|
start(context);
|
2018-10-13 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 07:40:18 +00:00
|
|
|
public static void start(Context context) {
|
2018-10-17 06:00:48 +00:00
|
|
|
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class).setAction("start"));
|
2018-10-13 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void stop(Context context) {
|
2018-10-17 06:00:48 +00:00
|
|
|
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class).setAction("stop"));
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 12:30:27 +00:00
|
|
|
public static void reload(Context context, String reason) {
|
2018-10-13 13:30:00 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
if (prefs.getBoolean("enabled", true)) {
|
|
|
|
Log.i(Helper.TAG, "Reload because of '" + reason + "'");
|
2018-10-17 06:00:48 +00:00
|
|
|
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class).setAction("reload"));
|
2018-10-13 13:30:00 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-08-10 14:56:00 +00:00
|
|
|
|
|
|
|
private class ServiceState {
|
2018-08-28 12:12:07 +00:00
|
|
|
boolean running = true;
|
2018-10-07 18:43:44 +00:00
|
|
|
Thread thread;
|
2018-08-10 14:56:00 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|