1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-02-22 06:01:12 +00:00

Ensure running

This commit is contained in:
M66B 2021-07-27 18:32:10 +02:00
parent f77a099624
commit 0e82be12a1
2 changed files with 36 additions and 13 deletions

View file

@ -2269,7 +2269,7 @@ class Core {
private static void onSynchronizeMessages( private static void onSynchronizeMessages(
Context context, JSONArray jargs, Context context, JSONArray jargs,
EntityAccount account, final EntityFolder folder, EntityAccount account, final EntityFolder folder,
POP3Folder ifolder, POP3Store istore, State state) throws MessagingException, IOException { POP3Folder ifolder, POP3Store istore, State state) throws Throwable {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean notify_known = prefs.getBoolean("notify_known", false); boolean notify_known = prefs.getBoolean("notify_known", false);
@ -2382,11 +2382,10 @@ class Core {
boolean _new = true; boolean _new = true;
for (int i = imessages.length - 1; i >= imessages.length - max; i--) { for (int i = imessages.length - 1; i >= imessages.length - max; i--) {
state.ensureRunning("Sync/POP3");
Message imessage = imessages[i]; Message imessage = imessages[i];
try { try {
if (!state.isRunning())
return;
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context); MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
String uidl; String uidl;
@ -2583,7 +2582,7 @@ class Core {
private static void onSynchronizeMessages( private static void onSynchronizeMessages(
Context context, JSONArray jargs, Context context, JSONArray jargs,
EntityAccount account, final EntityFolder folder, EntityAccount account, final EntityFolder folder,
IMAPStore istore, final IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException { IMAPStore istore, final IMAPFolder ifolder, State state) throws Throwable {
final DB db = DB.getInstance(context); final DB db = DB.getInstance(context);
try { try {
SyncStats stats = new SyncStats(); SyncStats stats = new SyncStats();
@ -2741,7 +2740,9 @@ class Core {
Log.i(folder.name + " remote fetched=" + stats.flags_ms + " ms"); Log.i(folder.name + " remote fetched=" + stats.flags_ms + " ms");
try { try {
for (int i = 0; i < imessages.length && state.isRunning() && state.isRecoverable(); i++) { for (int i = 0; i < imessages.length; i++) {
state.ensureRunning("Sync/IMAP");
long uid = ifolder.getUID(imessages[i]); long uid = ifolder.getUID(imessages[i]);
EntityMessage message = db.message().getMessageByUid(folder.id, uid); EntityMessage message = db.message().getMessageByUid(folder.id, uid);
ids[i] = (message == null ? null : message.id); ids[i] = (message == null ? null : message.id);
@ -2794,7 +2795,9 @@ class Core {
}); });
int expunge = 0; int expunge = 0;
for (int i = 0; i < imessages.length && state.isRunning() && state.isRecoverable(); i++) for (int i = 0; i < imessages.length; i++) {
state.ensureRunning("Sync/IMAP");
try { try {
if (perform_expunge && imessages[i].isSet(Flags.Flag.DELETED)) if (perform_expunge && imessages[i].isSet(Flags.Flag.DELETED))
expunge++; expunge++;
@ -2809,6 +2812,7 @@ class Core {
EntityLog.log(context, folder.name + " expunge " + Log.formatThrowable(ex, false)); EntityLog.log(context, folder.name + " expunge " + Log.formatThrowable(ex, false));
db.folder().setFolderError(folder.id, Log.formatThrowable(ex)); db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
} }
}
if (expunge > 0) if (expunge > 0)
try { try {
@ -2965,7 +2969,9 @@ class Core {
// Add/update local messages // Add/update local messages
int synced = 0; int synced = 0;
Log.i(folder.name + " add=" + imessages.length); Log.i(folder.name + " add=" + imessages.length);
for (int i = imessages.length - 1; i >= 0 && state.isRunning() && state.isRecoverable(); i -= SYNC_BATCH_SIZE) { for (int i = imessages.length - 1; i >= 0; i -= SYNC_BATCH_SIZE) {
state.ensureRunning("Sync/IMAP");
int from = Math.max(0, i - SYNC_BATCH_SIZE + 1); int from = Math.max(0, i - SYNC_BATCH_SIZE + 1);
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1); Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
@ -2996,7 +3002,9 @@ class Core {
Log.breadcrumb("sync", crumb); Log.breadcrumb("sync", crumb);
Log.i("Sync " + from + ".." + i + " free=" + free); Log.i("Sync " + from + ".." + i + " free=" + free);
for (int j = isub.length - 1; j >= 0 && state.isRunning() && state.isRecoverable(); j--) for (int j = isub.length - 1; j >= 0; j--) {
state.ensureRunning("Sync/IMAP");
try { try {
// Some providers erroneously return old messages // Some providers erroneously return old messages
if (full.contains(isub[j])) if (full.contains(isub[j]))
@ -3047,6 +3055,7 @@ class Core {
// Free memory // Free memory
isub[j] = null; isub[j] = null;
} }
}
} }
} }
@ -3072,9 +3081,10 @@ class Core {
// Download messages/attachments // Download messages/attachments
int downloaded = 0; int downloaded = 0;
Log.i(folder.name + " download=" + imessages.length); Log.i(folder.name + " download=" + imessages.length);
for (int i = imessages.length - 1; i >= 0 && state.isRunning() && state.isRecoverable(); i -= DOWNLOAD_BATCH_SIZE) { for (int i = imessages.length - 1; i >= 0; i -= DOWNLOAD_BATCH_SIZE) {
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1); state.ensureRunning("Sync/IMAP");
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1);
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1); Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
Arrays.fill(imessages, from, i + 1, null); Arrays.fill(imessages, from, i + 1, null);
// Fetch on demand // Fetch on demand
@ -3090,7 +3100,9 @@ class Core {
Log.breadcrumb("download", crumb); Log.breadcrumb("download", crumb);
Log.i("Download " + from + ".." + i + " free=" + free); Log.i("Download " + from + ".." + i + " free=" + free);
for (int j = isub.length - 1; j >= 0 && state.isRunning() && state.isRecoverable(); j--) for (int j = isub.length - 1; j >= 0; j--) {
state.ensureRunning("Sync/IMAP");
try { try {
if (ids[from + j] != null) { if (ids[from + j] != null) {
boolean fetched = downloadMessage( boolean fetched = downloadMessage(
@ -3116,6 +3128,7 @@ class Core {
// Free memory // Free memory
isub[j] = null; isub[j] = null;
} }
}
} }
} }
@ -4976,6 +4989,13 @@ class Core {
join(thread); join(thread);
} }
void ensureRunning(String reason) throws Throwable {
if (!recoverable && unrecoverable != null)
throw unrecoverable;
if (!running)
throw new OperationCanceledException(reason);
}
boolean isRunning() { boolean isRunning() {
return running; return running;
} }

View file

@ -47,6 +47,7 @@ import android.os.Bundle;
import android.os.DeadObjectException; import android.os.DeadObjectException;
import android.os.DeadSystemException; import android.os.DeadSystemException;
import android.os.Debug; import android.os.Debug;
import android.os.OperationCanceledException;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.TransactionTooLargeException; import android.os.TransactionTooLargeException;
@ -1464,7 +1465,9 @@ public class Log {
return null; return null;
if (ex instanceof StoreClosedException || if (ex instanceof StoreClosedException ||
ex instanceof FolderClosedException || ex instanceof FolderClosedIOException) ex instanceof FolderClosedException ||
ex instanceof FolderClosedIOException ||
ex instanceof OperationCanceledException)
return null; return null;
if (ex instanceof IllegalStateException && if (ex instanceof IllegalStateException &&