2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2018-10-09 19:39:25 +00:00
|
|
|
import android.app.AlarmManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
2018-09-14 13:31:00 +00:00
|
|
|
import android.content.SharedPreferences;
|
2018-12-09 13:41:26 +01:00
|
|
|
import android.graphics.Color;
|
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;
|
|
|
|
|
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-12-02 14:19:54 +01:00
|
|
|
import android.os.Handler;
|
2018-10-08 06:18:44 +00:00
|
|
|
import android.os.PowerManager;
|
2018-08-13 14:37:15 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.text.TextUtils;
|
2018-11-24 09:26:34 +01:00
|
|
|
import android.util.LongSparseArray;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
|
|
import com.sun.mail.imap.IMAPStore;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-02-19 16:21:25 +00:00
|
|
|
import org.jsoup.Jsoup;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-03 12:07:51 +00:00
|
|
|
import java.io.IOException;
|
2018-08-24 18:06:44 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
2018-08-04 22:35:47 +00:00
|
|
|
import java.util.HashMap;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.List;
|
2018-08-04 22:35:47 +00:00
|
|
|
import java.util.Map;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
import javax.mail.FetchProfile;
|
|
|
|
|
import javax.mail.Folder;
|
2018-08-02 15:25:01 +00:00
|
|
|
import javax.mail.FolderClosedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.Message;
|
2018-08-02 14:40:01 +00:00
|
|
|
import javax.mail.MessageRemovedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.MessagingException;
|
2018-08-15 19:34:22 +00:00
|
|
|
import javax.mail.NoSuchProviderException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.Session;
|
2019-02-09 21:03:53 +00:00
|
|
|
import javax.mail.Store;
|
2018-09-03 16:34:38 +00:00
|
|
|
import javax.mail.StoreClosedException;
|
2018-08-02 13:33:06 +00:00
|
|
|
import javax.mail.UIDFolder;
|
|
|
|
|
import javax.mail.event.ConnectionAdapter;
|
|
|
|
|
import javax.mail.event.ConnectionEvent;
|
|
|
|
|
import javax.mail.event.FolderAdapter;
|
|
|
|
|
import javax.mail.event.FolderEvent;
|
|
|
|
|
import javax.mail.event.MessageChangedEvent;
|
|
|
|
|
import javax.mail.event.MessageChangedListener;
|
|
|
|
|
import javax.mail.event.MessageCountAdapter;
|
|
|
|
|
import javax.mail.event.MessageCountEvent;
|
|
|
|
|
import javax.mail.event.StoreEvent;
|
|
|
|
|
import javax.mail.event.StoreListener;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-02-22 17:04:53 +01:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
import androidx.lifecycle.LifecycleService;
|
2018-12-05 11:50:07 +01:00
|
|
|
import androidx.lifecycle.LiveData;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.lifecycle.Observer;
|
|
|
|
|
|
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-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
|
|
|
|
2018-09-07 13:54:32 +00:00
|
|
|
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
2018-11-08 12:47:34 +00:00
|
|
|
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
|
2018-11-08 17:16:32 +00:00
|
|
|
private static final int CONNECT_BACKOFF_AlARM = 15; // minutes
|
2018-11-27 16:36:42 +01:00
|
|
|
private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds
|
2019-02-13 07:11:20 +00:00
|
|
|
private static final int ACCOUNT_ERROR_AFTER = 90; // minutes
|
2019-02-12 17:12:25 +00:00
|
|
|
private static final int BACKOFF_ERROR_AFTER = 16; // seconds
|
2018-11-27 16:36:42 +01:00
|
|
|
private static final long STOP_DELAY = 5000L; // milliseconds
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
|
static final int PI_SCHEDULE = 1;
|
2018-09-20 09:10:19 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Service create version=" + BuildConfig.VERSION_NAME);
|
2018-08-02 13:33:06 +00:00
|
|
|
super.onCreate();
|
|
|
|
|
|
|
|
|
|
// Listen for network changes
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
|
|
|
|
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
2018-08-10 13:43:54 +00:00
|
|
|
// Removed because of Android VPN service
|
|
|
|
|
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
2018-08-14 18:46:14 +00:00
|
|
|
cm.registerNetworkCallback(builder.build(), serviceManager);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-27 11:16:38 +00:00
|
|
|
JobDaily.schedule(this);
|
|
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
DB db = DB.getInstance(this);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
db.account().liveStats().observe(this, new Observer<TupleAccountStats>() {
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
|
public void onChanged(@Nullable TupleAccountStats stats) {
|
2018-12-09 18:49:52 +01:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2019-02-15 07:51:14 +00:00
|
|
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(stats).build());
|
2018-08-24 18:06:44 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-12-09 09:38:23 +01:00
|
|
|
db.message().liveUnseenNotify().observe(this, new Observer<List<TupleMessageEx>>() {
|
2018-11-24 09:26:34 +01:00
|
|
|
private LongSparseArray<List<Integer>> notifying = new LongSparseArray<>();
|
2018-08-12 15:31:43 +00:00
|
|
|
|
2018-08-24 18:06:44 +00:00
|
|
|
@Override
|
2018-12-01 19:45:07 +01:00
|
|
|
public void onChanged(final List<TupleMessageEx> messages) {
|
2019-02-27 16:00:16 +00:00
|
|
|
Log.i("Notification messages=" + messages.size());
|
|
|
|
|
|
|
|
|
|
Widget.update(ServiceSynchronize.this, messages.size());
|
|
|
|
|
|
|
|
|
|
LongSparseArray<String> accountName = new LongSparseArray<>();
|
|
|
|
|
LongSparseArray<List<TupleMessageEx>> accountMessages = new LongSparseArray<>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < notifying.size(); i++)
|
|
|
|
|
accountMessages.put(notifying.keyAt(i), new ArrayList<TupleMessageEx>());
|
|
|
|
|
|
|
|
|
|
for (TupleMessageEx message : messages) {
|
|
|
|
|
long account = (message.accountNotify ? message.account : 0);
|
|
|
|
|
accountName.put(account, account > 0 ? message.accountName : null);
|
|
|
|
|
if (accountMessages.indexOfKey(account) < 0)
|
|
|
|
|
accountMessages.put(account, new ArrayList<TupleMessageEx>());
|
|
|
|
|
accountMessages.get(account).add(message);
|
|
|
|
|
if (notifying.indexOfKey(account) < 0)
|
|
|
|
|
notifying.put(account, new ArrayList<Integer>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < accountMessages.size(); i++) {
|
|
|
|
|
long account = accountMessages.keyAt(i);
|
|
|
|
|
List<Notification> notifications = getNotificationUnseen(
|
|
|
|
|
account, accountName.get(account), accountMessages.get(account));
|
|
|
|
|
|
|
|
|
|
List<Integer> all = new ArrayList<>();
|
|
|
|
|
List<Integer> added = new ArrayList<>();
|
|
|
|
|
List<Integer> removed = notifying.get(account);
|
|
|
|
|
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);
|
|
|
|
|
Log.i("Notification removing=" + id);
|
|
|
|
|
} else {
|
|
|
|
|
removed.remove(Integer.valueOf(-id));
|
|
|
|
|
added.add(id);
|
|
|
|
|
Log.i("Notification adding=" + id);
|
2018-12-01 19:45:07 +01:00
|
|
|
}
|
2018-11-24 09:26:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 16:00:16 +00:00
|
|
|
|
|
|
|
|
int headers = 0;
|
|
|
|
|
for (Integer id : added)
|
|
|
|
|
if (id < 0)
|
|
|
|
|
headers++;
|
|
|
|
|
|
|
|
|
|
Log.i("Notification account=" + account +
|
|
|
|
|
" notifications=" + notifications.size() + " all=" + all.size() +
|
|
|
|
|
" added=" + added.size() + " removed=" + removed.size() + " headers=" + headers);
|
|
|
|
|
|
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
|
|
if (notifications.size() == 0 ||
|
|
|
|
|
(Build.VERSION.SDK_INT < Build.VERSION_CODES.O && headers > 0))
|
|
|
|
|
nm.cancel("unseen:" + account, 0);
|
|
|
|
|
|
|
|
|
|
for (Integer id : removed)
|
|
|
|
|
nm.cancel("unseen:" + account, Math.abs(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:" + account, Math.abs(id), notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notifying.put(account, all);
|
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
});
|
2018-10-16 10:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Service destroy");
|
2018-10-16 10:26:35 +00:00
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
cm.unregisterNetworkCallback(serviceManager);
|
|
|
|
|
|
2018-11-27 16:36:42 +01:00
|
|
|
serviceManager.service_destroy();
|
2018-10-16 10:26:35 +00:00
|
|
|
|
2018-10-21 18:29:28 +00:00
|
|
|
Widget.update(this, -1);
|
|
|
|
|
|
2019-02-27 11:16:38 +00:00
|
|
|
JobDaily.cancel(this);
|
|
|
|
|
|
2018-10-16 10:26:35 +00:00
|
|
|
stopForeground(true);
|
|
|
|
|
|
2018-12-09 18:49:52 +01:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2019-02-15 07:51:14 +00:00
|
|
|
nm.cancel(Helper.NOTIFICATION_SYNCHRONIZE);
|
2018-10-16 10:26:35 +00:00
|
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
String action = (intent == null ? null : intent.getAction());
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Service command intent=" + intent + " action=" + action);
|
2019-02-14 12:28:03 +00:00
|
|
|
Log.logExtras(intent);
|
2018-10-16 10:26:35 +00:00
|
|
|
|
2019-02-15 07:51:14 +00:00
|
|
|
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null).build());
|
2018-10-16 10:26:35 +00:00
|
|
|
|
|
|
|
|
super.onStartCommand(intent, flags, startId);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-28 17:21:31 +00:00
|
|
|
if (action != null)
|
|
|
|
|
try {
|
|
|
|
|
final String[] parts = action.split(":");
|
|
|
|
|
switch (parts[0]) {
|
|
|
|
|
case "init":
|
|
|
|
|
// Network events will manage the service
|
2019-02-27 11:16:38 +00:00
|
|
|
serviceManager.service_init(intent.getBooleanExtra("boot", false));
|
2018-12-28 17:21:31 +00:00
|
|
|
break;
|
|
|
|
|
|
2019-02-14 12:28:03 +00:00
|
|
|
case "schedule":
|
|
|
|
|
serviceManager.service_schedule();
|
|
|
|
|
break;
|
|
|
|
|
|
2018-12-28 17:21:31 +00:00
|
|
|
case "reload":
|
|
|
|
|
serviceManager.service_reload(intent.getStringExtra("reason"));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Log.w("Unknown action: " + action);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
2018-08-22 12:30:27 +00:00
|
|
|
}
|
2018-08-07 20:03:45 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return START_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) {
|
2018-10-17 06:11:20 +00:00
|
|
|
if (stats == null)
|
|
|
|
|
stats = lastStats;
|
|
|
|
|
if (stats == null)
|
|
|
|
|
stats = new TupleAccountStats();
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Build pending intent
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent intent = new Intent(this, ServiceUI.class);
|
2018-11-10 12:17:22 +00:00
|
|
|
intent.setAction("why");
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent pi = PendingIntent.getService(this, ServiceUI.PI_WHY, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
|
// Build notification
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
|
builder
|
2018-10-06 20:45:07 +00:00
|
|
|
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
2018-10-17 06:11:20 +00:00
|
|
|
.setContentTitle(getResources().getQuantityString(
|
|
|
|
|
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)
|
2019-02-22 17:04:53 +01:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-17 06:11:20 +00:00
|
|
|
if (stats.operations > 0)
|
2019-02-22 17:04:53 +01:00
|
|
|
builder.setStyle(new NotificationCompat.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-11-27 12:35:10 +01:00
|
|
|
private List<Notification> getNotificationUnseen(long account, String accountName, List<TupleMessageEx> messages) {
|
2018-09-20 09:10:19 +00:00
|
|
|
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-11-24 09:26:34 +01:00
|
|
|
// https://developer.android.com/training/notify-user/group
|
|
|
|
|
String group = Long.toString(account);
|
|
|
|
|
|
2019-02-09 14:01:46 +00:00
|
|
|
String title = getResources().getQuantityString(
|
2018-12-02 09:15:06 +01:00
|
|
|
R.plurals.title_notification_unseen, messages.size(), messages.size());
|
|
|
|
|
|
2019-01-19 13:21:21 +00:00
|
|
|
// Get contact info
|
|
|
|
|
Map<TupleMessageEx, ContactInfo> messageContact = new HashMap<>();
|
2019-01-26 09:58:37 +00:00
|
|
|
for (TupleMessageEx message : messages)
|
2019-02-04 11:45:38 +00:00
|
|
|
messageContact.put(message, ContactInfo.get(this, message.from, false));
|
2019-01-19 10:39:37 +00:00
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
// Build pending intent
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent summary = new Intent(this, ServiceUI.class);
|
2019-02-09 14:01:46 +00:00
|
|
|
summary.setAction("summary");
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent piSummary = PendingIntent.getService(this, ServiceUI.PI_SUMMARY, summary, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-08-05 10:56:24 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent clear = new Intent(this, ServiceUI.class);
|
2018-10-17 06:20:24 +00:00
|
|
|
clear.setAction("clear");
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent piClear = PendingIntent.getService(this, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-10-17 06:20:24 +00:00
|
|
|
|
2018-12-10 12:26:50 +01:00
|
|
|
String channelName = (account == 0 ? "notification" : EntityAccount.getNotificationChannelName(account));
|
|
|
|
|
|
2018-11-06 10:18:46 +00:00
|
|
|
// Build public notification
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Builder pbuilder = new NotificationCompat.Builder(this, channelName);
|
2018-11-06 10:18:46 +00:00
|
|
|
|
|
|
|
|
pbuilder
|
|
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
2019-02-09 14:01:46 +00:00
|
|
|
.setContentTitle(title)
|
|
|
|
|
.setContentIntent(piSummary)
|
2018-11-06 10:18:46 +00:00
|
|
|
.setNumber(messages.size())
|
|
|
|
|
.setShowWhen(false)
|
|
|
|
|
.setDeleteIntent(piClear)
|
|
|
|
|
.setPriority(Notification.PRIORITY_DEFAULT)
|
|
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
2019-02-22 17:04:53 +01:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
|
2018-11-06 10:18:46 +00:00
|
|
|
|
2018-11-27 12:35:10 +01:00
|
|
|
if (!TextUtils.isEmpty(accountName))
|
|
|
|
|
pbuilder.setSubText(accountName);
|
|
|
|
|
|
2018-08-05 10:56:24 +00:00
|
|
|
// Build notification
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelName);
|
2018-12-10 12:26:50 +01:00
|
|
|
|
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()))
|
2019-02-09 14:01:46 +00:00
|
|
|
.setContentIntent(piSummary)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setNumber(messages.size())
|
2018-08-07 18:31:14 +00:00
|
|
|
.setShowWhen(false)
|
2018-10-17 06:20:24 +00:00
|
|
|
.setDeleteIntent(piClear)
|
2018-11-06 09:09:43 +00:00
|
|
|
.setPriority(Notification.PRIORITY_DEFAULT)
|
2018-08-05 10:56:24 +00:00
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setVisibility(Notification.VISIBILITY_PRIVATE)
|
2018-11-06 10:18:46 +00:00
|
|
|
.setPublicVersion(pbuilder.build())
|
2018-11-24 09:26:34 +01:00
|
|
|
.setGroup(group)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setGroupSummary(true);
|
|
|
|
|
|
2018-11-27 12:35:10 +01:00
|
|
|
if (!TextUtils.isEmpty(accountName))
|
|
|
|
|
builder.setSubText(accountName);
|
|
|
|
|
|
2018-12-09 10:47:20 +01:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
2019-02-09 14:31:48 +00:00
|
|
|
boolean light = prefs.getBoolean("light", false);
|
|
|
|
|
String sound = prefs.getString("sound", null);
|
|
|
|
|
|
|
|
|
|
if (light)
|
2018-12-09 13:41:26 +01:00
|
|
|
builder.setLights(Color.GREEN, 1000, 1000);
|
2018-12-09 10:47:20 +01:00
|
|
|
|
2018-12-09 13:41:26 +01:00
|
|
|
if (sound == null) {
|
|
|
|
|
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
|
|
builder.setSound(uri);
|
|
|
|
|
} else
|
2018-12-09 10:47:20 +01:00
|
|
|
builder.setSound(Uri.parse(sound));
|
|
|
|
|
|
2018-12-09 13:41:26 +01:00
|
|
|
builder.setOnlyAlertOnce(true);
|
|
|
|
|
} else
|
2019-02-22 17:04:53 +01:00
|
|
|
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
2018-09-14 13:31:00 +00:00
|
|
|
|
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) {
|
2019-01-26 09:58:37 +00:00
|
|
|
sb.append("<strong>").append(messageContact.get(message).getDisplayName(true)).append("</strong>");
|
2018-08-25 13:36:01 +00:00
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
|
sb.append(": ").append(message.subject);
|
2019-01-07 18:26:45 +00:00
|
|
|
sb.append(" ").append(df.format(message.received));
|
2018-09-02 08:36:02 +00:00
|
|
|
sb.append("<br>");
|
2018-08-25 13:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
2019-02-10 12:01:21 +00:00
|
|
|
.bigText(HtmlHelper.fromHtml(sb.toString()))
|
2019-02-09 14:01:46 +00:00
|
|
|
.setSummaryText(title));
|
2018-08-25 13:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
notifications.add(builder.build());
|
|
|
|
|
|
2019-02-09 14:31:48 +00:00
|
|
|
boolean preview = prefs.getBoolean("notify_preview", true);
|
2018-11-06 17:00:07 +00:00
|
|
|
for (TupleMessageEx message : messages) {
|
2019-01-19 13:21:21 +00:00
|
|
|
ContactInfo info = messageContact.get(message);
|
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
Bundle args = new Bundle();
|
2018-12-01 19:45:07 +01:00
|
|
|
args.putLong("id", message.content ? message.id : -message.id);
|
2018-09-20 09:10:19 +00:00
|
|
|
|
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-11-30 10:53:25 +01:00
|
|
|
thread.putExtra("id", message.id);
|
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);
|
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent ignored = new Intent(this, ServiceUI.class);
|
2018-12-02 15:23:31 +01:00
|
|
|
ignored.setAction("ignore:" + message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent piDelete = PendingIntent.getService(this, ServiceUI.PI_IGNORED, ignored, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-10-16 11:29:12 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent seen = new Intent(this, ServiceUI.class);
|
2018-09-20 09:10:19 +00:00
|
|
|
seen.setAction("seen:" + message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent piSeen = PendingIntent.getService(this, ServiceUI.PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-09-20 09:10:19 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent archive = new Intent(this, ServiceUI.class);
|
2018-11-06 09:33:04 +00:00
|
|
|
archive.setAction("archive:" + message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent piArchive = PendingIntent.getService(this, ServiceUI.PI_ARCHIVE, archive, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-11-06 09:33:04 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Intent trash = new Intent(this, ServiceUI.class);
|
2018-09-20 09:10:19 +00:00
|
|
|
trash.setAction("trash:" + message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
PendingIntent piTrash = PendingIntent.getService(this, ServiceUI.PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-12-09 18:49:52 +01:00
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder(
|
2018-12-09 18:49:52 +01:00
|
|
|
R.drawable.baseline_visibility_24,
|
2018-11-06 12:27:49 +00:00
|
|
|
getString(R.string.title_action_seen),
|
2018-09-20 09:10:19 +00:00
|
|
|
piSeen);
|
|
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder(
|
2018-12-09 18:49:52 +01:00
|
|
|
R.drawable.baseline_archive_24,
|
2018-11-06 12:27:49 +00:00
|
|
|
getString(R.string.title_action_archive),
|
2018-11-06 09:33:04 +00:00
|
|
|
piArchive);
|
|
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder(
|
2018-12-09 18:49:52 +01:00
|
|
|
R.drawable.baseline_delete_24,
|
2018-11-06 12:27:49 +00:00
|
|
|
getString(R.string.title_action_trash),
|
2018-09-20 09:10:19 +00:00
|
|
|
piTrash);
|
|
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
NotificationCompat.Builder mbuilder;
|
|
|
|
|
mbuilder = new NotificationCompat.Builder(this, channelName);
|
2018-09-20 09:10:19 +00:00
|
|
|
|
2018-12-09 14:47:55 +01:00
|
|
|
String folderName = message.folderDisplay == null
|
|
|
|
|
? Helper.localizeFolderName(this, message.folderName)
|
|
|
|
|
: message.folderDisplay;
|
|
|
|
|
|
2018-09-20 09:10:19 +00:00
|
|
|
mbuilder
|
|
|
|
|
.addExtras(args)
|
2018-12-30 08:28:36 +00:00
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
2019-01-26 09:58:37 +00:00
|
|
|
.setContentTitle(info.getDisplayName(true))
|
2018-12-09 14:47:55 +01:00
|
|
|
.setSubText(message.accountName + " · " + folderName)
|
2018-10-16 11:29:12 +00:00
|
|
|
.setContentIntent(piContent)
|
2018-11-24 17:22:47 +01:00
|
|
|
.setWhen(message.received)
|
2018-10-16 11:29:12 +00:00
|
|
|
.setDeleteIntent(piDelete)
|
2018-11-06 09:09:43 +00:00
|
|
|
.setPriority(Notification.PRIORITY_DEFAULT)
|
2018-12-01 19:45:07 +01:00
|
|
|
.setOnlyAlertOnce(true)
|
2018-11-06 09:45:10 +00:00
|
|
|
.setCategory(Notification.CATEGORY_MESSAGE)
|
2018-11-06 17:06:42 +00:00
|
|
|
.setVisibility(Notification.VISIBILITY_PRIVATE)
|
2018-11-24 09:26:34 +01:00
|
|
|
.setGroup(group)
|
2018-09-20 09:10:19 +00:00
|
|
|
.setGroupSummary(false)
|
|
|
|
|
.addAction(actionSeen.build())
|
2018-11-06 09:33:04 +00:00
|
|
|
.addAction(actionArchive.build())
|
2018-09-20 09:10:19 +00:00
|
|
|
.addAction(actionTrash.build());
|
|
|
|
|
|
2018-12-09 13:41:26 +01:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
|
mbuilder.setSound(null);
|
|
|
|
|
|
2018-11-07 12:51:43 +00:00
|
|
|
if (pro) {
|
2018-09-20 09:10:19 +00:00
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
|
mbuilder.setContentText(message.subject);
|
|
|
|
|
|
2019-02-09 14:31:48 +00:00
|
|
|
if (message.content && preview)
|
2018-12-01 19:45:07 +01:00
|
|
|
try {
|
2019-01-21 16:45:05 +00:00
|
|
|
String body = Helper.readText(EntityMessage.getFile(this, message.id));
|
2018-12-02 09:15:06 +01:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
|
sb.append(message.subject).append("<br>");
|
2019-02-19 16:21:25 +00:00
|
|
|
String text = Jsoup.parse(body).text();
|
2019-02-19 16:30:03 +00:00
|
|
|
if (!TextUtils.isEmpty(text)) {
|
|
|
|
|
sb.append("<em>");
|
|
|
|
|
if (text.length() > HtmlHelper.PREVIEW_SIZE) {
|
|
|
|
|
sb.append(text.substring(0, HtmlHelper.PREVIEW_SIZE));
|
|
|
|
|
sb.append("…");
|
|
|
|
|
} else
|
|
|
|
|
sb.append(text);
|
|
|
|
|
sb.append("</em>");
|
|
|
|
|
}
|
2019-02-22 17:04:53 +01:00
|
|
|
mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString())));
|
2018-12-01 19:45:07 +01:00
|
|
|
} catch (IOException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2019-02-22 17:04:53 +01:00
|
|
|
mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString()));
|
2018-12-01 19:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-19 13:21:21 +00:00
|
|
|
if (info.hasPhoto())
|
|
|
|
|
mbuilder.setLargeIcon(info.getPhotoBitmap());
|
2018-11-07 12:51:43 +00:00
|
|
|
|
2019-01-19 13:21:21 +00:00
|
|
|
if (info.hasLookupUri())
|
2019-02-22 17:04:53 +01:00
|
|
|
mbuilder.addPerson(info.getLookupUri().toString());
|
2018-11-06 15:40:00 +00:00
|
|
|
|
2018-11-10 08:08:31 +00:00
|
|
|
if (message.accountColor != null) {
|
2018-11-10 08:05:58 +00:00
|
|
|
mbuilder.setColor(message.accountColor);
|
2019-02-22 17:04:53 +01:00
|
|
|
mbuilder.setColorized(true);
|
2018-11-10 08:08:31 +00:00
|
|
|
}
|
2018-11-06 15:40:00 +00:00
|
|
|
}
|
2018-11-06 09:45:10 +00:00
|
|
|
|
2019-02-22 17:04:53 +01:00
|
|
|
mbuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
2018-09-20 09:10:19 +00:00
|
|
|
|
|
|
|
|
notifications.add(mbuilder.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return notifications;
|
2018-08-05 10:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
private void monitorAccount(final EntityAccount account, final Core.State state) throws NoSuchProviderException {
|
2018-12-09 18:49:52 +01:00
|
|
|
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
2018-12-02 14:19:54 +01:00
|
|
|
final PowerManager.WakeLock wlAccount = pm.newWakeLock(
|
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":account." + account.id);
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
|
|
|
|
|
final DB db = DB.getInstance(this);
|
|
|
|
|
|
|
|
|
|
int backoff = CONNECT_BACKOFF_START;
|
2018-11-28 19:47:16 +01:00
|
|
|
while (state.running()) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " run");
|
2018-10-10 07:26:04 +00:00
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
|
boolean debug = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("debug", false);
|
|
|
|
|
debug = debug || BuildConfig.DEBUG;
|
|
|
|
|
System.setProperty("mail.socket.debug", Boolean.toString(debug));
|
|
|
|
|
|
|
|
|
|
// Create session
|
2019-01-10 18:24:20 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
|
2018-10-10 07:26:04 +00:00
|
|
|
final Session isession = Session.getInstance(props, null);
|
|
|
|
|
isession.setDebug(debug);
|
|
|
|
|
// adb -t 1 logcat | grep "fairemail\|System.out"
|
|
|
|
|
|
2019-02-09 21:03:53 +00:00
|
|
|
final Store istore = isession.getStore(account.getProtocol());
|
|
|
|
|
final Map<EntityFolder, Folder> folders = new HashMap<>();
|
2018-10-10 07:26:04 +00:00
|
|
|
List<Thread> idlers = new ArrayList<>();
|
2018-12-02 14:19:54 +01:00
|
|
|
List<Handler> handlers = new ArrayList<>();
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
|
|
|
|
// Listen for store events
|
|
|
|
|
istore.addStoreListener(new StoreListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void notification(StoreEvent e) {
|
2018-10-10 08:00:42 +00:00
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-11-29 10:31:54 +01:00
|
|
|
String type = (e.getMessageType() == StoreEvent.ALERT ? "alert" : "notice");
|
|
|
|
|
if (e.getMessageType() == StoreEvent.ALERT) {
|
2019-02-17 09:18:42 +00:00
|
|
|
Log.w(account.name + " " + type + ": " + e.getMessage());
|
2018-12-11 19:02:41 +01:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " " + type + ": " + e.getMessage());
|
2018-11-12 18:12:29 +01:00
|
|
|
db.account().setAccountError(account.id, e.getMessage());
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(
|
|
|
|
|
ServiceSynchronize.this, account, null,
|
|
|
|
|
new Core.AlertException(e.getMessage()));
|
2018-11-29 10:31:54 +01:00
|
|
|
state.error();
|
2018-12-11 19:02:41 +01:00
|
|
|
} else
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " " + type + ": " + e.getMessage());
|
2018-10-10 08:00:42 +00:00
|
|
|
} finally {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-10-10 08:00:42 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
});
|
2018-08-15 19:34:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Listen for folder events
|
|
|
|
|
istore.addFolderListener(new FolderAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void folderCreated(FolderEvent e) {
|
|
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Folder created=" + e.getFolder().getFullName());
|
2018-11-19 15:35:04 +01:00
|
|
|
reload(ServiceSynchronize.this, "folder created");
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
|
public void folderRenamed(FolderEvent e) {
|
|
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Folder renamed=" + e.getFolder().getFullName());
|
2018-09-14 06:28:23 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
String old = e.getFolder().getFullName();
|
|
|
|
|
String name = e.getNewFolder().getFullName();
|
|
|
|
|
int count = db.folder().renameFolder(account.id, old, name);
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Renamed to " + name + " count=" + count);
|
2018-09-14 06:28:23 +00:00
|
|
|
|
2018-11-19 15:35:04 +01:00
|
|
|
reload(ServiceSynchronize.this, "folder renamed");
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
|
public void folderDeleted(FolderEvent e) {
|
|
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Folder deleted=" + e.getFolder().getFullName());
|
2018-11-19 15:35:04 +01:00
|
|
|
reload(ServiceSynchronize.this, "folder deleted");
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Listen for connection events
|
|
|
|
|
istore.addConnectionListener(new ConnectionAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void opened(ConnectionEvent e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " opened event");
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-06 16:22:01 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
|
public void disconnected(ConnectionEvent e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(account.name + " disconnected event");
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-11 08:28:54 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
@Override
|
|
|
|
|
public void closed(ConnectionEvent e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(account.name + " closed event");
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
});
|
2018-08-15 19:34:22 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Initiate connection
|
2018-11-27 11:43:37 +01:00
|
|
|
EntityLog.log(this, account.name + " connecting");
|
2018-10-10 07:26:04 +00:00
|
|
|
for (EntityFolder folder : db.folder().getFolders(account.id))
|
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
|
|
|
|
db.account().setAccountState(account.id, "connecting");
|
2018-11-19 14:14:02 +01:00
|
|
|
|
2018-11-22 08:57:14 +01:00
|
|
|
try {
|
|
|
|
|
Helper.connect(this, istore, account);
|
|
|
|
|
} catch (Throwable ex) {
|
2018-11-28 19:47:16 +01:00
|
|
|
// Report account connection error
|
2018-11-22 08:57:14 +01:00
|
|
|
if (account.last_connected != null) {
|
|
|
|
|
EntityLog.log(this, account.name + " last connected: " + new Date(account.last_connected));
|
|
|
|
|
long now = new Date().getTime();
|
2019-01-24 18:50:37 +00:00
|
|
|
long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L;
|
2019-02-12 17:12:25 +00:00
|
|
|
if (delayed > ACCOUNT_ERROR_AFTER * 60 * 1000L && backoff > BACKOFF_ERROR_AFTER) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Reporting sync error after=" + delayed);
|
2019-01-23 08:44:25 +00:00
|
|
|
Throwable warning = new Throwable(
|
|
|
|
|
getString(R.string.title_no_sync,
|
|
|
|
|
SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
|
|
|
|
|
.format((account.last_connected))), ex);
|
2018-12-09 18:49:52 +01:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-11-28 18:29:46 +01:00
|
|
|
nm.notify("receive", account.id.intValue(),
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.getNotificationError(this, "warning", account.name, warning, false)
|
2019-02-27 13:03:17 +00:00
|
|
|
.build());
|
2018-11-22 08:57:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
2018-11-19 14:14:02 +01:00
|
|
|
|
2019-02-11 21:46:12 +00:00
|
|
|
final boolean capIdle = ((IMAPStore) istore).hasCapability("IDLE");
|
|
|
|
|
final boolean capUidPlus = ((IMAPStore) istore).hasCapability("UIDPLUS");
|
2019-02-02 14:20:29 +00:00
|
|
|
Log.i(account.name + " idle=" + capIdle + " uidplus=" + capUidPlus);
|
2018-11-19 14:14:02 +01:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
db.account().setAccountState(account.id, "connected");
|
|
|
|
|
EntityLog.log(this, account.name + " connected");
|
2018-09-04 05:49:58 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Update folder list
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.synchronizeFolders(this, account, istore, state);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-12-05 10:25:35 +01:00
|
|
|
// Open synchronizing folders
|
2018-12-05 16:53:12 +01:00
|
|
|
final ExecutorService pollExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-12-05 10:25:35 +01:00
|
|
|
for (final EntityFolder folder : db.folder().getFolders(account.id)) {
|
|
|
|
|
if (folder.synchronize && !folder.poll && capIdle) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " sync folder " + folder.name);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-12-05 10:25:35 +01:00
|
|
|
db.folder().setFolderState(folder.id, "connecting");
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2019-02-09 21:03:53 +00:00
|
|
|
final Folder ifolder = istore.getFolder(folder.name);
|
2018-12-05 10:25:35 +01:00
|
|
|
try {
|
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
2018-12-25 14:52:53 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
|
// Including ReadOnlyFolderException
|
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-25 14:52:53 +00:00
|
|
|
continue;
|
2018-12-05 10:25:35 +01:00
|
|
|
} catch (Throwable ex) {
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 10:25:35 +01:00
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
folders.put(folder, ifolder);
|
2018-08-22 05:31:00 +00:00
|
|
|
|
2018-12-05 10:25:35 +01:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
|
|
|
|
db.folder().setFolderError(folder.id, null);
|
2018-11-29 14:00:46 +01:00
|
|
|
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " folder " + folder.name + " flags=" + ifolder.getPermanentFlags());
|
2018-11-25 13:34:08 +01:00
|
|
|
|
2018-12-05 10:25:35 +01:00
|
|
|
// Listen for new and deleted messages
|
|
|
|
|
ifolder.addMessageCountListener(new MessageCountAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void messagesAdded(MessageCountEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " messages added");
|
2018-12-05 10:25:35 +01:00
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
|
fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
|
fp.add(FetchProfile.Item.FLAGS);
|
|
|
|
|
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
|
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
|
fp.add(FetchProfile.Item.SIZE);
|
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
|
|
|
|
|
ifolder.fetch(e.getMessages(), fp);
|
|
|
|
|
|
|
|
|
|
for (Message imessage : e.getMessages())
|
|
|
|
|
try {
|
2018-12-06 11:59:57 +01:00
|
|
|
EntityMessage message;
|
2018-12-05 10:25:35 +01:00
|
|
|
try {
|
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
message = Core.synchronizeMessage(
|
2018-12-05 10:25:35 +01:00
|
|
|
ServiceSynchronize.this,
|
2019-02-09 21:03:53 +00:00
|
|
|
folder, (IMAPFolder) ifolder, (IMAPMessage) imessage,
|
2019-01-18 08:23:53 +00:00
|
|
|
false,
|
|
|
|
|
db.rule().getEnabledRules(folder.id));
|
2018-12-05 10:25:35 +01:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 13:36:23 +00:00
|
|
|
if (db.folder().getFolderDownload(folder.id))
|
|
|
|
|
try {
|
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.downloadMessage(ServiceSynchronize.this,
|
2019-02-09 21:03:53 +00:00
|
|
|
folder, (IMAPFolder) ifolder,
|
2019-01-19 13:36:23 +00:00
|
|
|
(IMAPMessage) imessage, message.id);
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
2018-12-05 10:25:35 +01:00
|
|
|
} catch (MessageRemovedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-21 11:17:18 +01:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
|
throw ex;
|
2018-12-05 10:25:35 +01:00
|
|
|
} catch (IOException ex) {
|
2018-12-21 16:50:02 +01:00
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-21 16:50:02 +01:00
|
|
|
} else
|
|
|
|
|
throw ex;
|
2018-12-21 11:17:18 +01:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 10:25:35 +01:00
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2018-12-05 10:25:35 +01:00
|
|
|
state.error();
|
|
|
|
|
} finally {
|
|
|
|
|
wlAccount.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void messagesRemoved(MessageCountEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
wlAccount.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " messages removed");
|
2018-12-05 10:25:35 +01:00
|
|
|
for (Message imessage : e.getMessages())
|
|
|
|
|
try {
|
2019-02-09 21:03:53 +00:00
|
|
|
long uid = ((IMAPFolder) ifolder).getUID(imessage);
|
2018-12-05 10:25:35 +01:00
|
|
|
|
|
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
|
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
|
|
|
|
|
2019-01-28 16:54:14 +00:00
|
|
|
Log.i(folder.name + " deleted uid=" + uid + " count=" + count);
|
2018-12-05 10:25:35 +01:00
|
|
|
} catch (MessageRemovedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-05 10:25:35 +01:00
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 10:25:35 +01:00
|
|
|
state.error();
|
|
|
|
|
} finally {
|
|
|
|
|
wlAccount.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Flags (like "seen") at the remote could be changed while synchronizing
|
|
|
|
|
|
|
|
|
|
// Listen for changed messages
|
|
|
|
|
ifolder.addMessageChangedListener(new MessageChangedListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void messageChanged(MessageChangedEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
wlAccount.acquire();
|
2018-12-02 14:19:54 +01:00
|
|
|
try {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " message changed");
|
2018-12-05 10:25:35 +01:00
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.FLAGS);
|
|
|
|
|
ifolder.fetch(new Message[]{e.getMessage()}, fp);
|
|
|
|
|
|
2018-12-06 11:59:57 +01:00
|
|
|
EntityMessage message;
|
2018-12-02 14:19:54 +01:00
|
|
|
try {
|
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
message = Core.synchronizeMessage(
|
2018-12-02 14:19:54 +01:00
|
|
|
ServiceSynchronize.this,
|
2019-02-09 21:03:53 +00:00
|
|
|
folder, (IMAPFolder) ifolder, (IMAPMessage) e.getMessage(),
|
2019-01-18 08:23:53 +00:00
|
|
|
false,
|
|
|
|
|
db.rule().getEnabledRules(folder.id));
|
2018-12-02 14:19:54 +01:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
2018-09-16 18:07:26 +00:00
|
|
|
|
2019-01-19 13:36:23 +00:00
|
|
|
if (db.folder().getFolderDownload(folder.id))
|
|
|
|
|
try {
|
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.downloadMessage(ServiceSynchronize.this,
|
2019-02-09 21:03:53 +00:00
|
|
|
folder, (IMAPFolder) ifolder,
|
2019-01-19 13:36:23 +00:00
|
|
|
(IMAPMessage) e.getMessage(), message.id);
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
2018-12-02 14:19:54 +01:00
|
|
|
} catch (MessageRemovedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-21 11:17:18 +01:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
|
throw ex;
|
2018-12-02 14:19:54 +01:00
|
|
|
} catch (IOException ex) {
|
2018-12-21 16:50:02 +01:00
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-21 16:50:02 +01:00
|
|
|
} else
|
|
|
|
|
throw ex;
|
2018-12-21 11:17:18 +01:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-12-05 10:25:35 +01:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2018-12-05 10:25:35 +01:00
|
|
|
state.error();
|
|
|
|
|
} finally {
|
|
|
|
|
wlAccount.release();
|
2018-12-02 14:19:54 +01:00
|
|
|
}
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-12-05 10:25:35 +01:00
|
|
|
});
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2018-12-05 10:25:35 +01:00
|
|
|
// Idle folder
|
2018-10-10 07:26:04 +00:00
|
|
|
Thread idler = new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " start idle");
|
2018-11-29 12:06:09 +01:00
|
|
|
while (state.running()) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " do idle");
|
2019-02-09 21:03:53 +00:00
|
|
|
((IMAPFolder) ifolder).idle(false);
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
2019-02-21 09:17:16 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-11-28 19:47:16 +01:00
|
|
|
state.error();
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " end idle");
|
2018-08-21 19:08:51 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
}, "idler." + folder.id);
|
2019-01-07 15:05:24 +00:00
|
|
|
idler.setPriority(THREAD_PRIORITY_BACKGROUND);
|
2018-10-10 07:26:04 +00:00
|
|
|
idler.start();
|
|
|
|
|
idlers.add(idler);
|
2018-12-02 14:19:54 +01:00
|
|
|
|
2019-02-27 14:06:10 +00:00
|
|
|
EntityOperation.sync(this, db, folder.id);
|
2018-12-05 10:25:35 +01:00
|
|
|
} else
|
|
|
|
|
folders.put(folder, null);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-12-05 10:25:35 +01:00
|
|
|
// Observe operations
|
2018-12-02 14:19:54 +01:00
|
|
|
Handler handler = new Handler(getMainLooper()) {
|
2018-12-27 15:08:13 +00:00
|
|
|
private List<Long> handling = new ArrayList<>();
|
2018-12-05 11:50:07 +01:00
|
|
|
private LiveData<List<EntityOperation>> liveOperations;
|
2018-10-16 12:06:42 +00:00
|
|
|
|
2018-12-02 14:19:54 +01:00
|
|
|
@Override
|
|
|
|
|
public void handleMessage(android.os.Message msg) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + "/" + folder.name + " observe=" + msg.what);
|
2018-12-05 11:50:07 +01:00
|
|
|
try {
|
2018-12-27 15:08:13 +00:00
|
|
|
if (msg.what == 0) {
|
2018-12-05 11:50:07 +01:00
|
|
|
liveOperations.removeObserver(observer);
|
2018-12-27 15:08:13 +00:00
|
|
|
handling.clear();
|
|
|
|
|
} else {
|
2018-12-05 11:50:07 +01:00
|
|
|
liveOperations = db.operation().liveOperations(folder.id);
|
|
|
|
|
liveOperations.observe(ServiceSynchronize.this, observer);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-12-05 11:50:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Observer<List<EntityOperation>> observer = new Observer<List<EntityOperation>>() {
|
2018-12-05 16:53:12 +01:00
|
|
|
private final ExecutorService folderExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-12-05 11:50:07 +01:00
|
|
|
private final PowerManager.WakeLock wlFolder = pm.newWakeLock(
|
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":folder." + folder.id);
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-01-29 15:41:16 +00:00
|
|
|
public void onChanged(final List<EntityOperation> operations) {
|
|
|
|
|
boolean process = false;
|
|
|
|
|
List<Long> current = new ArrayList<>();
|
|
|
|
|
for (EntityOperation op : operations) {
|
|
|
|
|
if (!handling.contains(op.id))
|
|
|
|
|
process = true;
|
|
|
|
|
current.add(op.id);
|
2018-12-05 11:50:07 +01:00
|
|
|
}
|
2019-01-29 15:41:16 +00:00
|
|
|
handling = current;
|
2018-12-02 14:19:54 +01:00
|
|
|
|
2019-01-29 15:41:16 +00:00
|
|
|
if (handling.size() > 0 && process) {
|
|
|
|
|
Log.i(folder.name + " operations=" + operations.size());
|
2018-12-05 16:53:12 +01:00
|
|
|
(folder.poll ? pollExecutor : folderExecutor).submit(new Runnable() {
|
2018-12-05 11:50:07 +01:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
wlFolder.acquire();
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " process");
|
2018-12-05 11:50:07 +01:00
|
|
|
|
|
|
|
|
// Get folder
|
2019-02-09 21:03:53 +00:00
|
|
|
Folder ifolder = folders.get(folder); // null when polling
|
2018-12-05 11:50:07 +01:00
|
|
|
final boolean shouldClose = (ifolder == null);
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2018-12-05 11:50:07 +01:00
|
|
|
try {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(folder.name + " run " + (shouldClose ? "offline" : "online"));
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2018-12-05 11:50:07 +01:00
|
|
|
if (ifolder == null) {
|
|
|
|
|
// Prevent unnecessary folder connections
|
|
|
|
|
if (db.operation().getOperationCount(folder.id, null) == 0)
|
|
|
|
|
return;
|
2018-08-26 08:33:22 +00:00
|
|
|
|
2019-02-11 21:46:12 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connecting");
|
2018-09-03 09:34:13 +00:00
|
|
|
|
2019-02-11 21:46:12 +00:00
|
|
|
ifolder = istore.getFolder(folder.name);
|
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
2019-02-10 19:22:35 +00:00
|
|
|
|
2019-02-11 21:46:12 +00:00
|
|
|
db.folder().setFolderState(folder.id, "connected");
|
2018-10-10 07:26:04 +00:00
|
|
|
|
2018-12-05 11:50:07 +01:00
|
|
|
db.folder().setFolderError(folder.id, null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.processOperations(ServiceSynchronize.this,
|
|
|
|
|
account, folder,
|
|
|
|
|
isession, istore, ifolder,
|
|
|
|
|
state);
|
2018-12-05 11:50:07 +01:00
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, folder, ex);
|
2019-02-21 09:34:43 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
2018-12-05 11:50:07 +01:00
|
|
|
state.error();
|
|
|
|
|
} finally {
|
|
|
|
|
if (shouldClose) {
|
|
|
|
|
if (ifolder != null && ifolder.isOpen()) {
|
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
|
|
|
|
try {
|
|
|
|
|
ifolder.close(false);
|
|
|
|
|
} catch (MessagingException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(folder.name, ex);
|
2018-12-02 14:19:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-02 08:13:48 +00:00
|
|
|
if (folder.synchronize && (folder.poll || !capIdle))
|
|
|
|
|
db.folder().setFolderState(folder.id, "waiting");
|
|
|
|
|
else
|
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-12-02 14:19:54 +01:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-12-05 11:50:07 +01:00
|
|
|
} finally {
|
|
|
|
|
wlFolder.release();
|
|
|
|
|
}
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-12-05 11:50:07 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-12-02 14:19:54 +01:00
|
|
|
};
|
2018-12-05 10:25:35 +01:00
|
|
|
|
|
|
|
|
// Start watching for operations
|
2018-12-02 14:19:54 +01:00
|
|
|
handler.sendEmptyMessage(1);
|
|
|
|
|
handlers.add(handler);
|
|
|
|
|
}
|
2018-10-17 10:11:24 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Keep alive alarm receiver
|
|
|
|
|
BroadcastReceiver alarm = new BroadcastReceiver() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
// Receiver runs on main thread
|
|
|
|
|
// Receiver has a wake lock for ~10 seconds
|
2018-12-02 14:19:54 +01:00
|
|
|
EntityLog.log(context, account.name + " keep alive wake lock=" + wlAccount.isHeld());
|
2018-11-28 19:47:16 +01:00
|
|
|
state.release();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
};
|
2018-10-07 17:24:21 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
String id = BuildConfig.APPLICATION_ID + ".POLL." + account.id;
|
2019-01-05 11:17:33 +00:00
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
|
2018-10-10 07:26:04 +00:00
|
|
|
registerReceiver(alarm, new IntentFilter(id));
|
2018-10-07 10:13:32 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Keep alive
|
2018-12-09 18:49:52 +01:00
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-11-29 10:56:12 +01:00
|
|
|
while (state.running()) {
|
2018-11-28 18:41:02 +01:00
|
|
|
if (!istore.isConnected())
|
|
|
|
|
throw new StoreClosedException(istore);
|
|
|
|
|
|
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-12-05 10:25:35 +01:00
|
|
|
if (folder.synchronize)
|
|
|
|
|
if (!folder.poll && capIdle) {
|
|
|
|
|
if (!folders.get(folder).isOpen())
|
|
|
|
|
throw new FolderClosedException(folders.get(folder));
|
|
|
|
|
} else
|
2019-02-27 14:06:10 +00:00
|
|
|
EntityOperation.sync(this, db, folder.id);
|
2018-11-28 18:41:02 +01:00
|
|
|
|
|
|
|
|
// Successfully connected: reset back off time
|
|
|
|
|
backoff = CONNECT_BACKOFF_START;
|
|
|
|
|
|
|
|
|
|
// Record successful connection
|
2019-02-12 17:12:25 +00:00
|
|
|
Date last_connected = new Date();
|
|
|
|
|
EntityLog.log(this, account.name + " set last_connected=" + last_connected);
|
|
|
|
|
db.account().setAccountConnected(account.id, last_connected.getTime());
|
2018-11-29 10:31:54 +01:00
|
|
|
db.account().setAccountError(account.id, capIdle ? null : getString(R.string.title_no_idle));
|
2018-11-28 18:41:02 +01:00
|
|
|
|
2019-02-17 18:06:46 +00:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
nm.cancel("receive", account.id.intValue());
|
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Schedule keep alive alarm
|
|
|
|
|
EntityLog.log(this, account.name + " wait=" + account.poll_interval);
|
2018-12-09 18:49:52 +01:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
|
|
|
|
am.set(
|
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
|
System.currentTimeMillis() + account.poll_interval * 60 * 1000L,
|
|
|
|
|
pi);
|
|
|
|
|
else
|
|
|
|
|
am.setAndAllowWhileIdle(
|
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
|
System.currentTimeMillis() + account.poll_interval * 60 * 1000L,
|
|
|
|
|
pi);
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-11-28 19:47:16 +01:00
|
|
|
state.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2018-11-28 19:47:16 +01:00
|
|
|
EntityLog.log(this, account.name + " waited state=" + state);
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-10-07 18:43:44 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
|
// Cleanup
|
|
|
|
|
am.cancel(pi);
|
|
|
|
|
unregisterReceiver(alarm);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.name + " done state=" + state);
|
2018-10-10 07:26:04 +00:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(account.name, ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
Core.reportError(ServiceSynchronize.this, account, null, ex);
|
2018-10-08 06:18:44 +00:00
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
EntityLog.log(this, account.name + " " + Helper.formatThrowable(ex));
|
2018-10-10 07:26:04 +00:00
|
|
|
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
|
2018-08-15 19:34:22 +00:00
|
|
|
} finally {
|
2018-12-05 10:25:35 +01:00
|
|
|
// Stop watching for operations
|
|
|
|
|
for (Handler handler : handlers)
|
|
|
|
|
handler.sendEmptyMessage(0);
|
|
|
|
|
handlers.clear();
|
|
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
EntityLog.log(this, account.name + " closing");
|
|
|
|
|
db.account().setAccountState(account.id, "closing");
|
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-12-05 10:25:35 +01:00
|
|
|
if (folder.synchronize && !folder.poll)
|
|
|
|
|
db.folder().setFolderState(folder.id, "closing");
|
2018-09-08 05:42:44 +00:00
|
|
|
|
2018-10-10 07:26:04 +00:00
|
|
|
// Close store
|
|
|
|
|
try {
|
2019-01-05 11:17:33 +00:00
|
|
|
EntityLog.log(this, account.name + " store closing");
|
2018-11-27 16:36:42 +01:00
|
|
|
istore.close();
|
2019-01-05 11:17:33 +00:00
|
|
|
EntityLog.log(this, account.name + " store closed");
|
2018-11-27 16:36:42 +01:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(account.name, ex);
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
|
EntityLog.log(this, account.name + " closed");
|
|
|
|
|
db.account().setAccountState(account.id, null);
|
2018-09-08 08:43:13 +00:00
|
|
|
}
|
2018-10-10 08:07:37 +00:00
|
|
|
|
|
|
|
|
// Stop idlers
|
2018-11-28 19:47:16 +01:00
|
|
|
for (Thread idler : idlers)
|
|
|
|
|
state.join(idler);
|
2018-12-02 14:19:54 +01:00
|
|
|
idlers.clear();
|
2018-11-28 19:47:16 +01:00
|
|
|
|
|
|
|
|
for (EntityFolder folder : folders.keySet())
|
2018-12-05 10:25:35 +01:00
|
|
|
if (folder.synchronize && !folder.poll)
|
|
|
|
|
db.folder().setFolderState(folder.id, null);
|
2018-08-15 19:34:22 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-11-28 19:47:16 +01:00
|
|
|
if (state.running())
|
2018-10-10 07:26:04 +00:00
|
|
|
try {
|
2018-11-08 17:16:32 +00:00
|
|
|
if (backoff <= CONNECT_BACKOFF_MAX) {
|
|
|
|
|
// Short back-off period, keep device awake
|
|
|
|
|
EntityLog.log(this, account.name + " backoff=" + backoff);
|
2018-11-28 19:47:16 +01:00
|
|
|
state.acquire(backoff * 1000L);
|
2018-11-08 17:16:32 +00:00
|
|
|
} else {
|
|
|
|
|
// Long back-off period, let device sleep
|
|
|
|
|
EntityLog.log(this, account.name + " backoff alarm=" + CONNECT_BACKOFF_AlARM);
|
|
|
|
|
|
|
|
|
|
BroadcastReceiver alarm = new BroadcastReceiver() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2018-11-28 19:47:16 +01:00
|
|
|
state.release();
|
2018-11-08 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
String id = BuildConfig.APPLICATION_ID + ".BACKOFF." + account.id;
|
2019-01-05 11:17:33 +00:00
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
|
2018-11-08 17:16:32 +00:00
|
|
|
registerReceiver(alarm, new IntentFilter(id));
|
|
|
|
|
|
2018-12-09 18:49:52 +01:00
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
2018-11-08 17:16:32 +00:00
|
|
|
try {
|
2018-12-09 18:49:52 +01:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
|
|
|
|
am.set(
|
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
|
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
|
|
|
|
|
pi);
|
|
|
|
|
else
|
|
|
|
|
am.setAndAllowWhileIdle(
|
|
|
|
|
AlarmManager.RTC_WAKEUP,
|
|
|
|
|
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
|
|
|
|
|
pi);
|
2018-11-09 08:54:24 +00:00
|
|
|
|
|
|
|
|
try {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-11-28 19:47:16 +01:00
|
|
|
state.acquire(2 * CONNECT_BACKOFF_AlARM * 60 * 1000L);
|
2018-11-09 08:54:24 +00:00
|
|
|
} finally {
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.acquire();
|
2018-11-09 08:54:24 +00:00
|
|
|
}
|
2018-11-08 17:16:32 +00:00
|
|
|
} finally {
|
2018-11-09 08:54:24 +00:00
|
|
|
// Cleanup
|
|
|
|
|
am.cancel(pi);
|
|
|
|
|
unregisterReceiver(alarm);
|
2018-11-08 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-13 18:22:18 +00:00
|
|
|
|
2018-11-19 15:35:04 +01:00
|
|
|
if (backoff <= CONNECT_BACKOFF_MAX)
|
2018-10-10 07:26:04 +00:00
|
|
|
backoff *= 2;
|
|
|
|
|
} catch (InterruptedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(account.name + " backoff " + ex.toString());
|
2018-10-10 07:26:04 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-10-10 07:26:04 +00:00
|
|
|
} finally {
|
|
|
|
|
EntityLog.log(this, account.name + " stopped");
|
2018-12-02 14:19:54 +01:00
|
|
|
wlAccount.release();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-14 18:46:14 +00:00
|
|
|
private class ServiceManager extends ConnectivityManager.NetworkCallback {
|
2019-02-27 15:05:15 +00:00
|
|
|
private Core.State state;
|
2018-11-27 16:36:42 +01:00
|
|
|
private boolean started = false;
|
2018-11-07 10:11:54 +00:00
|
|
|
private int queued = 0;
|
2018-09-30 10:13:24 +00:00
|
|
|
private long lastLost = 0;
|
2018-11-28 20:13:13 +01:00
|
|
|
private ExecutorService queue = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-11-27 16:36:42 +01:00
|
|
|
@Override
|
2018-12-25 11:37:35 +00:00
|
|
|
public void onAvailable(Network network) {
|
2018-12-28 17:42:28 +00:00
|
|
|
synchronized (this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Available " + network + " " + cm.getNetworkInfo(network));
|
2018-12-25 11:37:35 +00:00
|
|
|
|
2018-12-28 17:16:49 +00:00
|
|
|
if (!started && suitableNetwork())
|
|
|
|
|
queue_reload(true, "connect " + network);
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
}
|
2018-12-02 14:27:47 +01:00
|
|
|
}
|
2018-11-27 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
2018-12-25 11:37:35 +00:00
|
|
|
public void onCapabilitiesChanged(Network network, NetworkCapabilities capabilities) {
|
2018-12-28 17:42:28 +00:00
|
|
|
synchronized (this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
|
if (!started) {
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Network " + network + " capabilities " + capabilities);
|
|
|
|
|
if (suitableNetwork())
|
2018-12-28 17:42:28 +00:00
|
|
|
queue_reload(true, "capabilities " + network);
|
2018-12-28 17:16:49 +00:00
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
2018-12-25 11:37:35 +00:00
|
|
|
}
|
2018-12-02 14:27:47 +01:00
|
|
|
}
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-15 07:40:18 +00:00
|
|
|
@Override
|
|
|
|
|
public void onLost(Network network) {
|
2018-12-28 17:42:28 +00:00
|
|
|
synchronized (this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Lost " + network);
|
2018-11-27 16:36:42 +01:00
|
|
|
|
2018-12-28 17:16:49 +00:00
|
|
|
if (started && !suitableNetwork()) {
|
|
|
|
|
lastLost = new Date().getTime();
|
|
|
|
|
queue_reload(false, "disconnect " + network);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
2018-12-02 14:27:47 +01:00
|
|
|
}
|
2018-09-08 07:01:02 +00:00
|
|
|
}
|
2018-08-15 07:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-27 16:36:42 +01:00
|
|
|
private boolean suitableNetwork() {
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
|
|
|
|
boolean metered = prefs.getBoolean("metered", true);
|
|
|
|
|
|
2018-12-18 17:35:25 +01:00
|
|
|
Boolean isMetered = Helper.isMetered(ServiceSynchronize.this, true);
|
2018-12-07 09:25:57 +01:00
|
|
|
|
|
|
|
|
boolean suitable = (isMetered != null && (metered || !isMetered));
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this,
|
|
|
|
|
"suitable=" + suitable + " metered=" + metered + " isMetered=" + isMetered);
|
2018-12-01 13:59:55 +01:00
|
|
|
|
2018-11-27 16:36:42 +01:00
|
|
|
// The connected state is deliberately ignored
|
2018-12-01 13:59:55 +01:00
|
|
|
return suitable;
|
2018-11-27 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isEnabled() {
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
|
|
|
|
return prefs.getBoolean("enabled", true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 11:16:38 +00:00
|
|
|
private void service_init(boolean boot) {
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service init boot=" + boot);
|
2019-02-14 12:28:03 +00:00
|
|
|
|
2019-02-27 11:16:38 +00:00
|
|
|
if (boot)
|
2019-02-15 07:51:14 +00:00
|
|
|
next_schedule();
|
2019-02-14 12:28:03 +00:00
|
|
|
|
2019-02-27 11:16:38 +00:00
|
|
|
if (!isEnabled())
|
2019-02-14 12:28:03 +00:00
|
|
|
stopSelf();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void service_schedule() {
|
|
|
|
|
next_schedule();
|
|
|
|
|
service_reload("schedule");
|
2018-11-27 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-28 17:16:49 +00:00
|
|
|
private void service_reload(String reason) {
|
2018-12-28 17:42:28 +00:00
|
|
|
synchronized (this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
try {
|
|
|
|
|
serviceManager.queue_reload(true, reason);
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 16:36:42 +01:00
|
|
|
private void service_destroy() {
|
2018-12-28 17:42:28 +00:00
|
|
|
synchronized (this) {
|
2018-12-28 17:16:49 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service destroy");
|
|
|
|
|
if (started)
|
|
|
|
|
queue_reload(false, "service destroy");
|
|
|
|
|
}
|
2018-11-27 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-28 18:06:58 +01:00
|
|
|
private void start() {
|
2018-11-28 21:00:19 +01:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main start");
|
2018-09-12 08:16:38 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
state = new Core.State();
|
2018-11-28 19:47:16 +01:00
|
|
|
state.runnable(new Runnable() {
|
2018-12-09 18:49:52 +01:00
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
2018-11-28 18:44:47 +01:00
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
2018-12-02 14:19:54 +01:00
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":main");
|
2019-02-27 15:05:15 +00:00
|
|
|
private List<Core.State> threadState = new ArrayList<>();
|
2018-08-13 19:56:53 +00:00
|
|
|
|
2018-08-10 14:56:00 +00:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
2018-10-10 07:26:04 +00:00
|
|
|
wl.acquire();
|
|
|
|
|
|
2018-11-08 12:47:34 +00:00
|
|
|
final DB db = DB.getInstance(ServiceSynchronize.this);
|
2018-09-12 08:16:38 +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));
|
2018-11-28 21:00:19 +01:00
|
|
|
if (state.acquire(backoff))
|
|
|
|
|
return;
|
2018-09-30 10:13:24 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w("main backoff " + ex.toString());
|
2018-09-30 10:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-14 11:45:12 +00:00
|
|
|
// Start monitoring accounts
|
2019-02-17 18:29:00 +00:00
|
|
|
List<EntityAccount> accounts = db.account().getSynchronizingAccounts(false);
|
2018-08-14 11:45:12 +00:00
|
|
|
for (final EntityAccount account : accounts) {
|
2018-12-10 12:26:50 +01:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
|
|
|
|
|
if (account.notify)
|
|
|
|
|
account.createNotificationChannel(ServiceSynchronize.this);
|
|
|
|
|
else
|
|
|
|
|
account.deleteNotificationChannel(ServiceSynchronize.this);
|
|
|
|
|
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i(account.host + "/" + account.user + " run");
|
2019-02-27 15:05:15 +00:00
|
|
|
final Core.State astate = new Core.State();
|
2018-11-28 19:47:16 +01:00
|
|
|
astate.runnable(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) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-11-29 11:09:20 +01:00
|
|
|
EntityLog.log(ServiceSynchronize.this, account.name + " " + Helper.formatThrowable(ex));
|
2018-11-08 12:47:34 +00:00
|
|
|
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
|
2018-08-14 11:45:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, "sync.account." + account.id);
|
2018-11-28 19:47:16 +01:00
|
|
|
astate.start();
|
2018-10-07 18:43:44 +00:00
|
|
|
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 07:26:04 +00:00
|
|
|
wl.release();
|
2018-11-28 19:47:16 +01:00
|
|
|
state.acquire();
|
2018-10-07 18:43:44 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w("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
|
2019-02-27 15:05:15 +00:00
|
|
|
for (Core.State astate : threadState)
|
2018-11-28 19:47:16 +01:00
|
|
|
astate.stop();
|
2019-02-27 15:05:15 +00:00
|
|
|
for (Core.State astate : threadState)
|
2018-11-28 19:47:16 +01:00
|
|
|
astate.join();
|
2018-09-14 17:12:54 +00:00
|
|
|
threadState.clear();
|
2018-08-14 11:45:12 +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-12-24 12:27:45 +00:00
|
|
|
Log.e(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-11-28 19:47:16 +01:00
|
|
|
state.start();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-28 18:06:58 +01:00
|
|
|
private void stop() {
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main stop");
|
2018-09-12 08:16:38 +00:00
|
|
|
|
2018-11-28 19:47:16 +01:00
|
|
|
state.stop();
|
|
|
|
|
state.join();
|
2018-08-14 11:45:12 +00:00
|
|
|
|
2018-11-29 11:05:29 +01:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Main stopped");
|
2018-09-12 08:16:38 +00:00
|
|
|
|
2018-11-28 18:06:58 +01:00
|
|
|
state = null;
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-14 12:28:03 +00:00
|
|
|
private void next_schedule() {
|
|
|
|
|
Intent schedule = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
|
|
|
|
schedule.setAction("schedule");
|
|
|
|
|
PendingIntent piSchedule = PendingIntent.getService(
|
|
|
|
|
ServiceSynchronize.this, PI_SCHEDULE, schedule, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
|
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
am.cancel(piSchedule);
|
|
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
2019-02-14 13:28:14 +00:00
|
|
|
if (!prefs.getBoolean("schedule", false))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-14 12:28:03 +00:00
|
|
|
int minuteStart = prefs.getInt("schedule_start", 0);
|
|
|
|
|
int minuteEnd = prefs.getInt("schedule_end", 0);
|
|
|
|
|
|
|
|
|
|
if (minuteEnd <= minuteStart)
|
|
|
|
|
minuteEnd += 24 * 60;
|
|
|
|
|
|
|
|
|
|
Calendar calStart = Calendar.getInstance();
|
|
|
|
|
calStart.set(Calendar.HOUR_OF_DAY, minuteStart / 60);
|
|
|
|
|
calStart.set(Calendar.MINUTE, minuteStart % 60);
|
|
|
|
|
calStart.set(Calendar.SECOND, 0);
|
|
|
|
|
calStart.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
|
|
Calendar calEnd = Calendar.getInstance();
|
|
|
|
|
calEnd.set(Calendar.HOUR_OF_DAY, minuteEnd / 60);
|
|
|
|
|
calEnd.set(Calendar.MINUTE, minuteEnd % 60);
|
|
|
|
|
calEnd.set(Calendar.SECOND, 0);
|
|
|
|
|
calEnd.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
|
|
long now = new Date().getTime();
|
|
|
|
|
if (now > calEnd.getTimeInMillis()) {
|
|
|
|
|
calStart.set(Calendar.DAY_OF_MONTH, calStart.get(Calendar.DAY_OF_MONTH) + 1);
|
|
|
|
|
calEnd.set(Calendar.DAY_OF_MONTH, calEnd.get(Calendar.DAY_OF_MONTH) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long start = calStart.getTimeInMillis();
|
|
|
|
|
long end = calEnd.getTimeInMillis();
|
|
|
|
|
long next = (now < start ? start : end);
|
|
|
|
|
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Schedule now=" + new Date(now));
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Schedule start=" + new Date(start));
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Schedule end=" + new Date(end));
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Schedule next=" + new Date(next));
|
|
|
|
|
|
|
|
|
|
boolean enabled = (now >= start && now < end);
|
|
|
|
|
prefs.edit().putBoolean("enabled", enabled).apply();
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
|
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, next, piSchedule);
|
|
|
|
|
else
|
|
|
|
|
am.set(AlarmManager.RTC_WAKEUP, next, piSchedule);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 20:13:13 +01:00
|
|
|
private void queue_reload(final boolean start, final String reason) {
|
2018-11-29 08:35:34 +01:00
|
|
|
final boolean doStop = started;
|
|
|
|
|
final boolean doStart = (start && isEnabled() && suitableNetwork());
|
2018-10-13 13:30:00 +00:00
|
|
|
|
2019-02-14 12:28:03 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Queue reload" +
|
2018-11-29 08:35:34 +01:00
|
|
|
" doStop=" + doStop + " doStart=" + doStart + " queued=" + queued + " " + reason);
|
2018-11-28 18:06:58 +01:00
|
|
|
|
2018-12-28 18:31:17 +00:00
|
|
|
started = doStart;
|
|
|
|
|
|
2018-11-29 08:35:34 +01:00
|
|
|
queued++;
|
|
|
|
|
queue.submit(new Runnable() {
|
2018-12-09 18:49:52 +01:00
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
2018-12-06 13:43:00 +01:00
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":manage");
|
|
|
|
|
|
2018-11-29 08:35:34 +01:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
wl.acquire();
|
2018-11-28 18:06:58 +01:00
|
|
|
|
2019-02-17 18:04:22 +00:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Reload" +
|
2018-12-06 12:38:09 +01:00
|
|
|
" stop=" + doStop + " start=" + doStart + " queued=" + queued + " " + reason);
|
|
|
|
|
|
2018-11-29 08:35:34 +01:00
|
|
|
if (doStop)
|
|
|
|
|
stop();
|
2018-11-28 18:06:58 +01:00
|
|
|
|
2018-12-06 13:43:00 +01:00
|
|
|
DB db = DB.getInstance(ServiceSynchronize.this);
|
2018-12-10 12:26:50 +01:00
|
|
|
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
for (EntityAccount account : db.account().getAccountsTbd())
|
|
|
|
|
nm.deleteNotificationChannel(EntityAccount.getNotificationChannelName(account.id));
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 13:43:00 +01:00
|
|
|
int accounts = db.account().deleteAccountsTbd();
|
|
|
|
|
int identities = db.identity().deleteIdentitiesTbd();
|
|
|
|
|
if (accounts > 0 || identities > 0)
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Deleted accounts=" + accounts + " identities=" + identities);
|
2018-12-06 13:43:00 +01:00
|
|
|
|
2018-11-29 08:35:34 +01:00
|
|
|
if (doStart)
|
|
|
|
|
start();
|
2018-11-28 20:13:13 +01:00
|
|
|
|
2018-11-29 08:35:34 +01:00
|
|
|
} catch (Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-11-29 08:35:34 +01:00
|
|
|
} finally {
|
|
|
|
|
queued--;
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Reload done queued=" + queued);
|
2018-11-28 18:06:58 +01:00
|
|
|
|
2018-11-29 08:35:34 +01:00
|
|
|
if (queued == 0 && !isEnabled()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(STOP_DELAY);
|
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
|
}
|
2018-11-27 16:36:42 +01:00
|
|
|
if (queued == 0 && !isEnabled()) {
|
2018-11-29 08:35:34 +01:00
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service stop");
|
|
|
|
|
stopSelf();
|
2018-11-07 10:11:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-06 12:38:09 +01:00
|
|
|
|
|
|
|
|
wl.release();
|
2018-10-13 13:30:00 +00:00
|
|
|
}
|
2018-11-29 08:35:34 +01:00
|
|
|
}
|
|
|
|
|
});
|
2018-10-13 13:30:00 +00:00
|
|
|
}
|
2018-08-14 18:46:14 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-27 11:16:38 +00:00
|
|
|
public static void init(Context context, boolean boot) {
|
2019-02-14 12:28:03 +00:00
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
2019-02-15 07:51:14 +00:00
|
|
|
.setAction("init")
|
2019-02-27 11:16:38 +00:00
|
|
|
.putExtra("boot", boot));
|
2019-02-14 12:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void schedule(Context context) {
|
|
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
|
.setAction("schedule"));
|
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-11-27 16:36:42 +01:00
|
|
|
ContextCompat.startForegroundService(context,
|
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
|
.setAction("reload")
|
|
|
|
|
.putExtra("reason", reason));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
}
|