Fixed outbox sync

This commit is contained in:
M66B
2019-02-27 14:06:10 +00:00
parent fe5d1b5d0b
commit a736035bc9
8 changed files with 46 additions and 22 deletions

View File

@@ -26,6 +26,8 @@ import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
@@ -294,16 +296,19 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
if (!prefs.getBoolean("enabled", true))
throw new IllegalStateException(context.getString(R.string.title_sync_disabled));
boolean internet = (Helper.isMetered(context, true) != null);
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
boolean internet = (ni != null && ni.isConnected());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
boolean now;
if (aid < 0) { // outbox
now = "connected".equals(folder.state);
EntityOperation.sync(db, fid);
if (aid < 0) {
// Outbox
now = internet;
EntityOperation.sync(context, db, fid);
} else {
EntityAccount account = db.account().getAccount(aid);
if (account.ondemand) {
@@ -314,7 +319,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
} else {
now = "connected".equals(account.state);
EntityOperation.sync(db, fid);
EntityOperation.sync(context, db, fid);
}
}

View File

@@ -99,7 +99,7 @@ public class EntityOperation {
queue(context, db, message, name, jargs);
}
static void sync(DB db, long fid) {
static void sync(Context context, DB db, long fid) {
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
EntityFolder folder = db.folder().getFolder(fid);
@@ -114,6 +114,9 @@ public class EntityOperation {
db.folder().setFolderSyncState(fid, "requested");
if (folder.account == null) // Outbox
context.startService(new Intent(context, ServiceSend.class));
Log.i("Queued sync folder=" + folder);
}
}

View File

@@ -250,7 +250,7 @@ public class FragmentFolder extends FragmentBase {
db.message().deleteMessagesBefore(id, keep_time, true);
EntityOperation.sync(db, folder.id);
EntityOperation.sync(context, db, folder.id);
}
db.setTransactionSuccessful();

View File

@@ -31,6 +31,7 @@ import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Bundle;
import android.os.Handler;
@@ -495,7 +496,9 @@ public class FragmentMessages extends FragmentBase {
if (!prefs.getBoolean("enabled", true))
throw new IllegalStateException(context.getString(R.string.title_sync_disabled));
boolean internet = (Helper.isMetered(context, true) != null);
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
boolean internet = (ni != null && ni.isConnected());
DB db = DB.getInstance(context);
try {
@@ -515,9 +518,10 @@ public class FragmentMessages extends FragmentBase {
boolean now = false;
boolean nointernet = false;
for (EntityFolder folder : folders)
if (folder.account == null) { // outbox
now = "connected".equals(folder.state);
EntityOperation.sync(db, folder.id);
if (folder.account == null) {
// Outbox
now = internet;
EntityOperation.sync(context, db, folder.id);
} else {
EntityAccount account = db.account().getAccount(folder.account);
if (account.ondemand) {
@@ -528,7 +532,7 @@ public class FragmentMessages extends FragmentBase {
nointernet = true;
} else {
now = "connected".equals(account.state);
EntityOperation.sync(db, folder.id);
EntityOperation.sync(context, db, folder.id);
}
}

View File

@@ -269,7 +269,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
DB db = DB.getInstance(context);
List<EntityFolder> folders = db.folder().getFoldersAutoSync();
for (EntityFolder folder : folders)
EntityOperation.sync(db, folder.id);
EntityOperation.sync(context, db, folder.id);
return null;
}

View File

@@ -406,7 +406,7 @@ public class FragmentSetup extends FragmentBase {
protected Void onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
for (EntityFolder folder : db.folder().getFoldersAutoSync())
EntityOperation.sync(db, folder.id);
EntityOperation.sync(context, db, folder.id);
return null;
}

View File

@@ -134,17 +134,27 @@ public class ServiceSend extends LifecycleService {
List<EntityOperation> ops = db.operation().getOperations(outbox.id);
Log.i(outbox.name + " pending operations=" + ops.size());
for (EntityOperation op : ops) {
EntityMessage message = db.message().getMessage(op.message);
EntityMessage message = null;
try {
Log.i(outbox.name +
" start op=" + op.id + "/" + op.name +
" msg=" + op.message +
" args=" + op.args);
if (message == null)
throw new MessageRemovedException();
switch (op.name) {
case EntityOperation.SYNC:
break;
send(message);
case EntityOperation.SEND:
message = db.message().getMessage(op.message);
if (message == null)
throw new MessageRemovedException();
send(message);
break;
default:
throw new IllegalArgumentException("Unknown operation=" + op.name);
}
db.operation().deleteOperation(op.id);
} catch (Throwable ex) {
@@ -153,7 +163,9 @@ public class ServiceSend extends LifecycleService {
if (message != null)
db.message().setMessageError(message.id, Helper.formatThrowable(ex));
if (ex instanceof SendFailedException)
if (ex instanceof MessageRemovedException ||
ex instanceof SendFailedException ||
ex instanceof IllegalArgumentException)
db.operation().deleteOperation(op.id);
else
throw ex;

View File

@@ -1122,7 +1122,7 @@ public class ServiceSynchronize extends LifecycleService {
idler.start();
idlers.add(idler);
EntityOperation.sync(db, folder.id);
EntityOperation.sync(this, db, folder.id);
} else
folders.put(folder, null);
@@ -1260,7 +1260,7 @@ public class ServiceSynchronize extends LifecycleService {
if (!folders.get(folder).isOpen())
throw new FolderClosedException(folders.get(folder));
} else
EntityOperation.sync(db, folder.id);
EntityOperation.sync(this, db, folder.id);
// Successfully connected: reset back off time
backoff = CONNECT_BACKOFF_START;
@@ -1498,7 +1498,7 @@ public class ServiceSynchronize extends LifecycleService {
break;
default:
throw new MessagingException("Unknown operation name=" + op.name);
throw new IllegalArgumentException("Unknown operation=" + op.name);
}
// Operation succeeded