Fixed outbox sync

pull/152/head
M66B 6 years ago
parent fe5d1b5d0b
commit a736035bc9

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

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

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

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

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

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

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

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

Loading…
Cancel
Save