Simplified reload

pull/50/head
M66B 6 years ago
parent 3b52e9ddda
commit 4a5407e48f

@ -421,89 +421,85 @@ public class FragmentAccount extends FragmentEx {
if (TextUtils.isEmpty(name)) if (TextUtils.isEmpty(name))
name = host + "/" + user; name = host + "/" + user;
DB db = DB.getInstance(getContext());
try { try {
ServiceSynchronize.stopSynchronous(getContext(), "save account"); db.beginTransaction();
DB db = DB.getInstance(getContext()); EntityAccount account = db.account().getAccount(args.getLong("id"));
try { boolean update = (account != null);
db.beginTransaction(); if (account == null)
account = new EntityAccount();
EntityAccount account = db.account().getAccount(args.getLong("id")); account.name = name;
boolean update = (account != null); account.host = host;
if (account == null) account.port = Integer.parseInt(port);
account = new EntityAccount(); account.user = user;
account.name = name; account.password = password;
account.host = host; account.synchronize = synchronize;
account.port = Integer.parseInt(port); account.primary = (account.synchronize && primary);
account.user = user; account.store_sent = store_sent;
account.password = password;
account.synchronize = synchronize; if (!synchronize)
account.primary = (account.synchronize && primary); account.error = null;
account.store_sent = store_sent;
if (account.primary)
if (!synchronize) db.account().resetPrimary();
account.error = null;
if (update)
if (account.primary) db.account().updateAccount(account);
db.account().resetPrimary(); else
account.id = db.account().insertAccount(account);
if (update)
db.account().updateAccount(account); List<EntityFolder> folders = new ArrayList<>();
else
account.id = db.account().insertAccount(account); EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
List<EntityFolder> folders = new ArrayList<>(); inbox.type = EntityFolder.INBOX;
inbox.synchronize = true;
EntityFolder inbox = new EntityFolder(); inbox.after = EntityFolder.DEFAULT_INBOX_SYNC;
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX; folders.add(inbox);
inbox.synchronize = true;
inbox.after = EntityFolder.DEFAULT_INBOX_SYNC; if (drafts != null) {
drafts.type = EntityFolder.DRAFTS;
folders.add(inbox); folders.add(drafts);
}
if (drafts != null) {
drafts.type = EntityFolder.DRAFTS;
folders.add(drafts);
}
if (sent != null) {
sent.type = EntityFolder.SENT;
folders.add(sent);
}
if (all != null) {
all.type = EntityFolder.ARCHIVE;
folders.add(all);
}
if (trash != null) {
trash.type = EntityFolder.TRASH;
folders.add(trash);
}
if (junk != null) {
junk.type = EntityFolder.JUNK;
folders.add(junk);
}
for (EntityFolder folder : folders) { if (sent != null) {
db.folder().setFolderUser(account.id, folder.type); sent.type = EntityFolder.SENT;
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name); folders.add(sent);
if (existing == null) { }
folder.account = account.id; if (all != null) {
Log.i(Helper.TAG, "Creating folder=" + folder.name + " (" + folder.type + ")"); all.type = EntityFolder.ARCHIVE;
folder.id = db.folder().insertFolder(folder); folders.add(all);
} else }
db.folder().setFolderType(existing.id, folder.type); if (trash != null) {
} trash.type = EntityFolder.TRASH;
folders.add(trash);
}
if (junk != null) {
junk.type = EntityFolder.JUNK;
folders.add(junk);
}
db.setTransactionSuccessful(); for (EntityFolder folder : folders) {
} finally { db.folder().setFolderUser(account.id, folder.type);
db.endTransaction(); EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
Log.i(Helper.TAG, "Creating folder=" + folder.name + " (" + folder.type + ")");
folder.id = db.folder().insertFolder(folder);
} else
db.folder().setFolderType(existing.id, folder.type);
} }
return null; db.setTransactionSuccessful();
} finally { } finally {
ServiceSynchronize.start(getContext()); db.endTransaction();
} }
ServiceSynchronize.reload(getContext(), "save account");
return null;
} }
@Override @Override
@ -544,15 +540,10 @@ public class FragmentAccount extends FragmentEx {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
try { long id = args.getLong("id");
ServiceSynchronize.stopSynchronous(getContext(), "delete account"); DB.getInstance(context).account().deleteAccount(id);
ServiceSynchronize.reload(getContext(), "delete account");
long id = args.getLong("id"); return null;
DB.getInstance(context).account().deleteAccount(id);
return null;
} finally {
ServiceSynchronize.start(getContext());
}
} }
@Override @Override

@ -76,35 +76,31 @@ public class FragmentFolder extends FragmentEx {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
try { long id = args.getLong("id");
ServiceSynchronize.stopSynchronous(getContext(), "save folder"); boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after");
long id = args.getLong("id"); int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after");
int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
db.folder().setFolderProperties(id, synchronize, days); DB db = DB.getInstance(getContext());
if (!synchronize) try {
db.folder().setFolderError(id, null); db.beginTransaction();
EntityFolder folder = db.folder().getFolder(id); db.folder().setFolderProperties(id, synchronize, days);
if (!folder.synchronize) if (!synchronize)
db.message().deleteMessages(folder.id); db.folder().setFolderError(id, null);
db.setTransactionSuccessful(); EntityFolder folder = db.folder().getFolder(id);
} finally { if (!folder.synchronize)
db.endTransaction(); db.message().deleteMessages(folder.id);
}
return null; db.setTransactionSuccessful();
} finally { } finally {
ServiceSynchronize.start(getContext()); db.endTransaction();
} }
ServiceSynchronize.reload(getContext(), "save folder");
return null;
} }
@Override @Override

@ -253,49 +253,45 @@ public class FragmentIdentity extends FragmentEx {
} }
} }
DB db = DB.getInstance(getContext());
try { try {
ServiceSynchronize.stopSynchronous(getContext(), "save identity"); db.beginTransaction();
DB db = DB.getInstance(getContext()); EntityIdentity identity = db.identity().getIdentity(id);
try { boolean update = (identity != null);
db.beginTransaction(); if (identity == null)
identity = new EntityIdentity();
EntityIdentity identity = db.identity().getIdentity(id); identity.name = name;
boolean update = (identity != null); identity.email = email;
if (identity == null) identity.replyto = replyto;
identity = new EntityIdentity(); identity.account = account;
identity.name = name; identity.host = host;
identity.email = email; identity.port = Integer.parseInt(port);
identity.replyto = replyto; identity.starttls = starttls;
identity.account = account; identity.user = user;
identity.host = host; identity.password = password;
identity.port = Integer.parseInt(port); identity.synchronize = synchronize;
identity.starttls = starttls; identity.primary = (identity.synchronize && args.getBoolean("primary"));
identity.user = user;
identity.password = password; if (!identity.synchronize)
identity.synchronize = synchronize; identity.error = null;
identity.primary = (identity.synchronize && args.getBoolean("primary"));
if (identity.primary)
if (!identity.synchronize) db.identity().resetPrimary();
identity.error = null;
if (update)
if (identity.primary) db.identity().updateIdentity(identity);
db.identity().resetPrimary(); else
identity.id = db.identity().insertIdentity(identity);
if (update)
db.identity().updateIdentity(identity); db.setTransactionSuccessful();
else
identity.id = db.identity().insertIdentity(identity);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
} finally { } finally {
ServiceSynchronize.start(getContext()); db.endTransaction();
} }
ServiceSynchronize.reload(getContext(), "save identity");
return null;
} }
@Override @Override
@ -335,15 +331,10 @@ public class FragmentIdentity extends FragmentEx {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
try { long id = args.getLong("id");
ServiceSynchronize.stopSynchronous(getContext(), "delete identity"); DB.getInstance(context).identity().deleteIdentity(id);
ServiceSynchronize.reload(getContext(), "delete identity");
long id = args.getLong("id"); return null;
DB.getInstance(context).identity().deleteIdentity(id);
return null;
} finally {
ServiceSynchronize.start(getContext());
}
} }
@Override @Override

@ -23,11 +23,9 @@ import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Network; import android.net.Network;
@ -35,10 +33,8 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.net.Uri; import android.net.Uri;
import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -186,39 +182,43 @@ public class ServiceSynchronize extends LifecycleService {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(Helper.TAG, "Service start intent=" + intent); Log.i(Helper.TAG, "Service command intent=" + intent);
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
if (intent != null && "unseen".equals(intent.getAction())) { if (intent != null)
Bundle args = new Bundle(); if ("reload".equals(intent.getAction()))
args.putLong("time", new Date().getTime()); serviceManager.restart();
else if ("unseen".equals(intent.getAction())) {
Bundle args = new Bundle();
args.putLong("time", new Date().getTime());
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
long time = args.getLong("time"); long time = args.getLong("time");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
for (EntityAccount account : db.account().getAccounts(true)) for (EntityAccount account : db.account().getAccounts(true))
db.account().setAccountSeenUntil(account.id, time); db.account().setAccountSeenUntil(account.id, time);
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
db.endTransaction(); db.endTransaction();
}
return null;
} }
return null; @Override
} protected void onLoaded(Bundle args, Void data) {
Log.i(Helper.TAG, "Updated seen until");
}
}.load(ServiceSynchronize.this, args);
@Override }
protected void onLoaded(Bundle args, Void data) {
Log.i(Helper.TAG, "Updated seen until");
}
}.load(ServiceSynchronize.this, args);
}
return START_STICKY; return START_STICKY;
} }
@ -1490,6 +1490,23 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
private void restart() {
lifecycle.submit(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, "Stopping service");
stop(true);
}
});
lifecycle.submit(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, "Starting service");
start();
}
});
}
private BroadcastReceiver outboxReceiver = new BroadcastReceiver() { private BroadcastReceiver outboxReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -1541,66 +1558,15 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
private IBinder binder = new LocalBinder();
private class LocalBinder extends Binder {
ServiceSynchronize getService() {
return ServiceSynchronize.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void quit() {
Log.i(Helper.TAG, "Service quit");
serviceManager.stop(false);
Log.i(Helper.TAG, "Service quited");
stopSelf();
}
public static void start(Context context) { public static void start(Context context) {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class)); ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class));
} }
public static void stopSynchronous(Context context, String reason) { public static void reload(Context context, String reason) {
Log.i(Helper.TAG, "Stop because of '" + reason + "'"); Log.i(Helper.TAG, "Reload because of '" + reason + "'");
ContextCompat.startForegroundService(context,
final Semaphore semaphore = new Semaphore(0, true); new Intent(context, ServiceSynchronize.class).setAction("reload"));
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(Helper.TAG, "Service connected");
((LocalBinder) binder).getService().quit();
semaphore.release();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(Helper.TAG, "Service disconnected");
semaphore.release();
}
@Override
public void onBindingDied(ComponentName name) {
Log.i(Helper.TAG, "Service died");
semaphore.release();
}
};
Intent intent = new Intent(context, ServiceSynchronize.class);
boolean exists = context.getApplicationContext().bindService(intent, connection, Context.BIND_AUTO_CREATE);
Log.i(Helper.TAG, "Service exists=" + exists);
if (exists) {
Log.i(Helper.TAG, "Service stopping");
acquire(semaphore, "service");
context.getApplicationContext().unbindService(connection);
}
Log.i(Helper.TAG, "Service stopped");
} }
private class ServiceState { private class ServiceState {

Loading…
Cancel
Save