Back to simplicity

This commit is contained in:
M66B 2020-09-21 21:46:39 +02:00
parent f11867bedb
commit e2f58970e7
4 changed files with 41 additions and 82 deletions

View File

@ -3,18 +3,9 @@
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
<package name="java.util" withSubpackages="false" static="false" />
<package name="kotlinx.android.synthetic" withSubpackages="true" static="false" />
<package name="io.ktor" withSubpackages="true" static="false" />
</value>
</option>
</JetCodeStyleSettings>

View File

@ -496,8 +496,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
Log.i("Boundary server sync uid=" + uid);
EntityMessage message = db.message().getMessageByUid(browsable.id, uid);
if (message == null) {
message = Core.synchronizeMessage(
context, -1,
message = Core.synchronizeMessage(context,
account, browsable,
(IMAPStore) state.iservice.getStore(), state.ifolder, (MimeMessage) isub[j],
true, true,

View File

@ -147,7 +147,7 @@ class Core {
private static final int TOTAL_RETRY_MAX = LOCAL_RETRY_MAX * 10;
static void processOperations(
Context context, int session,
Context context,
EntityAccount account, EntityFolder folder, List<TupleOperationEx> ops,
Store istore, Folder ifolder,
State state, int priority, long sequence)
@ -162,7 +162,7 @@ class Core {
Log.i(folder.name + " executing operations=" + ops.size());
while (retry < LOCAL_RETRY_MAX && ops.size() > 0 &&
state.isRunning() &&
state.batchCanRun(session, folder.id, priority, sequence)) {
state.batchCanRun(folder.id, priority, sequence)) {
TupleOperationEx op = ops.get(0);
try {
@ -302,7 +302,7 @@ class Core {
break;
case EntityOperation.DELETE:
onDelete(context, session, jargs, account, folder, message, (POP3Folder) ifolder, (POP3Store) istore, state);
onDelete(context, jargs, account, folder, message, (POP3Folder) ifolder, (POP3Store) istore, state);
break;
case EntityOperation.SYNC:
@ -341,22 +341,22 @@ class Core {
break;
case EntityOperation.ADD:
onAdd(context, session, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
onAdd(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
break;
case EntityOperation.MOVE:
List<EntityMessage> messages = new ArrayList<>();
messages.add(message);
messages.addAll(similar.values());
onMove(context, session, jargs, false, folder, messages, (IMAPStore) istore, (IMAPFolder) ifolder, state);
onMove(context, jargs, false, folder, messages, (IMAPStore) istore, (IMAPFolder) ifolder, state);
break;
case EntityOperation.COPY:
onMove(context, session, jargs, true, folder, Arrays.asList(message), (IMAPStore) istore, (IMAPFolder) ifolder, state);
onMove(context, jargs, true, folder, Arrays.asList(message), (IMAPStore) istore, (IMAPFolder) ifolder, state);
break;
case EntityOperation.FETCH:
onFetch(context, session, jargs, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
onFetch(context, jargs, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
break;
case EntityOperation.DELETE:
@ -384,7 +384,7 @@ class Core {
break;
case EntityOperation.SYNC:
onSynchronizeMessages(context, session, jargs, account, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
onSynchronizeMessages(context, jargs, account, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
break;
case EntityOperation.SUBSCRIBE:
@ -502,7 +502,7 @@ class Core {
retry++;
if (retry < LOCAL_RETRY_MAX &&
state.isRunning() &&
state.batchCanRun(session, folder.id, priority, sequence))
state.batchCanRun(folder.id, priority, sequence))
try {
Thread.sleep(LOCAL_RETRY_DELAY);
} catch (InterruptedException ex1) {
@ -530,8 +530,6 @@ class Core {
if (ops.size() == 0)
state.batchCompleted(folder.id, priority, sequence);
else
state.error(new OperationCanceledException("Processing"), session);
} finally {
Log.i(folder.name + " end process state=" + state + " pending=" + ops.size());
}
@ -801,7 +799,7 @@ class Core {
}
}
private static void onAdd(Context context, int session, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException {
private static void onAdd(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException {
// Add message
DB db = DB.getInstance(context);
@ -915,7 +913,7 @@ class Core {
try {
JSONArray fargs = new JSONArray();
fargs.put(newuid);
onFetch(context, session, fargs, folder, istore, ifolder, state);
onFetch(context, fargs, folder, istore, ifolder, state);
} catch (JSONException ex) {
Log.e(ex);
}
@ -929,7 +927,7 @@ class Core {
}
}
private static void onMove(Context context, int session, JSONArray jargs, boolean copy, EntityFolder folder, List<EntityMessage> messages, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
private static void onMove(Context context, JSONArray jargs, boolean copy, EntityFolder folder, List<EntityMessage> messages, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
// Move message
DB db = DB.getInstance(context);
@ -1062,7 +1060,7 @@ class Core {
if (fetch) {
JSONArray fargs = new JSONArray();
fargs.put(uid);
onFetch(context, session, fargs, target, istore, itarget, state);
onFetch(context, fargs, target, istore, itarget, state);
}
}
} catch (Throwable ex) {
@ -1118,7 +1116,7 @@ class Core {
db.message().updateMessage(message);
}
private static void onFetch(Context context, int session, JSONArray jargs, EntityFolder folder, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
private static void onFetch(Context context, JSONArray jargs, EntityFolder folder, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
long uid = jargs.getLong(0);
boolean removed = jargs.optBoolean(1);
@ -1162,7 +1160,7 @@ class Core {
}
ifolder.fetch(new Message[]{imessage}, fp);
EntityMessage message = synchronizeMessage(context, session, account, folder, istore, ifolder, imessage, false, download, rules, state, stats);
EntityMessage message = synchronizeMessage(context, account, folder, istore, ifolder, imessage, false, download, rules, state, stats);
if (message != null) {
if (account.isGmail() && EntityFolder.USER.equals(folder.type))
try {
@ -1175,7 +1173,7 @@ class Core {
}
if (download)
downloadMessage(context, session, account, folder, istore, ifolder, imessage, message.id, state, stats);
downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state, stats);
}
if (!stats.isEmpty())
@ -1251,7 +1249,7 @@ class Core {
}
}
private static void onDelete(Context context, int session, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, POP3Folder ifolder, POP3Store istore, State state) throws MessagingException, IOException {
private static void onDelete(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, POP3Folder ifolder, POP3Store istore, State state) throws MessagingException, IOException {
// Delete message
DB db = DB.getInstance(context);
@ -1294,7 +1292,7 @@ class Core {
ifolder.open(Folder.READ_WRITE);
} catch (Throwable ex) {
Log.e(ex);
state.error(new FolderClosedException(ifolder, "POP"), session);
state.error(new FolderClosedException(ifolder, "POP"));
}
}
}
@ -2091,7 +2089,7 @@ class Core {
}
private static void onSynchronizeMessages(
Context context, int session, JSONArray jargs,
Context context, JSONArray jargs,
EntityAccount account, final EntityFolder folder,
IMAPStore istore, final IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
final DB db = DB.getInstance(context);
@ -2401,7 +2399,7 @@ class Core {
}
EntityMessage message = synchronizeMessage(
context, session,
context,
account, folder,
istore, ifolder, (MimeMessage) isub[j],
false, download && initialize == 0,
@ -2480,7 +2478,7 @@ class Core {
try {
if (ids[from + j] != null) {
boolean fetched = downloadMessage(
context, session,
context,
account, folder,
istore, ifolder,
(MimeMessage) isub[j], ids[from + j],
@ -2536,7 +2534,7 @@ class Core {
}
static EntityMessage synchronizeMessage(
Context context, int session,
Context context,
EntityAccount account, EntityFolder folder,
IMAPStore istore, IMAPFolder ifolder, MimeMessage imessage,
boolean browsed, boolean download,
@ -2871,7 +2869,7 @@ class Core {
" size=" + message.size + "/" + (body == null ? null : body.length()));
if (TextUtils.isEmpty(body) && parts.hasBody())
reportEmptyMessage(context, session, state, account, istore);
reportEmptyMessage(context, state, account, istore);
}
}
} else {
@ -3192,7 +3190,7 @@ class Core {
}
private static boolean downloadMessage(
Context context, int session,
Context context,
EntityAccount account, EntityFolder folder,
IMAPStore istore, IMAPFolder ifolder,
MimeMessage imessage, long id, State state, SyncStats stats) throws MessagingException, IOException {
@ -3265,7 +3263,7 @@ class Core {
" size=" + message.size + "/" + (body == null ? null : body.length()));
if (TextUtils.isEmpty(body) && parts.hasBody())
reportEmptyMessage(context, session, state, account, istore);
reportEmptyMessage(context, state, account, istore);
}
}
@ -3286,7 +3284,7 @@ class Core {
return fetch;
}
private static void reportEmptyMessage(Context context, int session, State state, EntityAccount account, IMAPStore istore) {
private static void reportEmptyMessage(Context context, State state, EntityAccount account, IMAPStore istore) {
try {
if (istore.hasCapability("ID")) {
Map<String, String> id = new LinkedHashMap<>();
@ -3313,7 +3311,7 @@ class Core {
account.partial_fetch = false;
DB db = DB.getInstance(context);
db.account().setAccountPartialFetch(account.id, account.partial_fetch);
state.error(new StoreClosedException(istore), session);
state.error(new StoreClosedException(istore));
}
}
@ -4056,8 +4054,6 @@ class Core {
}
static class State {
private int session = -1;
private boolean active = false;
private int backoff;
private ConnectionHelper.NetworkState networkState;
private Thread thread = new Thread();
@ -4107,17 +4103,7 @@ class Core {
return semaphore.tryAcquire(milliseconds, TimeUnit.MILLISECONDS);
}
void error(Throwable ex, int session) {
if (!this.active || session != this.session) {
Log.i("Ignoring" +
" active=" + active +
" session=" + session + "/" + this.session +
" ex=" + ex);
return;
}
active = false;
void error(Throwable ex) {
if (ex instanceof MessagingException &&
("connection failure".equals(ex.getMessage()) ||
"Not connected".equals(ex.getMessage()) || // POP3
@ -4148,18 +4134,12 @@ class Core {
yield();
}
void reset(int run) {
session = run;
active = true;
void reset() {
recoverable = true;
lastActivity = null;
resetBatches();
}
void setActive(boolean whether) {
this.active = whether;
}
void resetBatches() {
synchronized (this) {
for (FolderPriority key : sequence.keySet()) {
@ -4255,10 +4235,7 @@ class Core {
}
}
boolean batchCanRun(int session, long folder, int priority, long current) {
if (!this.active || session != this.session)
return false;
boolean batchCanRun(long folder, int priority, long current) {
synchronized (this) {
FolderPriority key = new FolderPriority(folder, priority);
boolean can = batch.get(key).equals(current);

View File

@ -35,7 +35,6 @@ import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Bundle;
import android.os.OperationCanceledException;
import android.os.PowerManager;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
@ -905,13 +904,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.w(account.name + " backoff " + ex.toString());
}
int run = 0;
int errors = 0;
state.setBackoff(CONNECT_BACKOFF_START);
while (state.isRunning() &&
currentThread != null && currentThread.equals(thread)) {
final int session = ++run;
state.reset(session);
state.reset();
Log.i(account.name + " run thread=" + currentThread);
final List<TwoStateOwner> cowners = new ArrayList<>();
@ -1134,7 +1131,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityLog.log(
ServiceSynchronize.this,
folder.name + " " + Log.formatThrowable(ex, false));
state.error(ex, session);
state.error(ex);
} finally {
wlMessage.release();
}
@ -1163,7 +1160,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityLog.log(
ServiceSynchronize.this,
folder.name + " " + Log.formatThrowable(ex, false));
state.error(ex, session);
state.error(ex);
} finally {
wlMessage.release();
}
@ -1187,7 +1184,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityLog.log(
ServiceSynchronize.this,
folder.name + " " + Log.formatThrowable(ex, false));
state.error(ex, session);
state.error(ex);
} finally {
wlMessage.release();
}
@ -1210,7 +1207,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityLog.log(
ServiceSynchronize.this,
folder.name + " " + Log.formatThrowable(ex, false));
state.error(new FolderClosedException(ifolder, "IDLE"), session);
state.error(new FolderClosedException(ifolder, "IDLE"));
} finally {
Log.i(folder.name + " end idle");
}
@ -1346,8 +1343,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i(account.name + " folder " + folder.name + " flags=" + ifolder.getPermanentFlags());
}
Core.processOperations(
ServiceSynchronize.this, session,
Core.processOperations(ServiceSynchronize.this,
account, folder,
partition,
iservice.getStore(), ifolder,
@ -1359,7 +1355,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
ServiceSynchronize.this,
folder.name + " " + Log.formatThrowable(ex, false));
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
state.error(new OperationCanceledException("Process"), session);
state.error(ex);
} finally {
if (shouldClose) {
if (ifolder != null && ifolder.isOpen()) {
@ -1544,8 +1540,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i(account.name + " done state=" + state);
} catch (Throwable ex) {
state.setActive(false);
Log.e(account.name, ex);
EntityLog.log(
ServiceSynchronize.this,
@ -1590,8 +1584,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
}
} finally {
state.setActive(false);
// Update state
EntityLog.log(this, account.name + " closing");
db.account().setAccountState(account.id, "closing");