Simplified reload

This commit is contained in:
M66B 2018-08-22 12:30:27 +00:00
parent 3b52e9ddda
commit 4a5407e48f
4 changed files with 175 additions and 231 deletions

View File

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

View File

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

View File

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

View File

@ -23,11 +23,9 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.Network;
@ -35,10 +33,8 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.TextUtils;
@ -186,39 +182,43 @@ public class ServiceSynchronize extends LifecycleService {
@Override
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);
if (intent != null && "unseen".equals(intent.getAction())) {
Bundle args = new Bundle();
args.putLong("time", new Date().getTime());
if (intent != null)
if ("reload".equals(intent.getAction()))
serviceManager.restart();
else if ("unseen".equals(intent.getAction())) {
Bundle args = new Bundle();
args.putLong("time", new Date().getTime());
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
long time = args.getLong("time");
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
long time = args.getLong("time");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (EntityAccount account : db.account().getAccounts(true))
db.account().setAccountSeenUntil(account.id, time);
for (EntityAccount account : db.account().getAccounts(true))
db.account().setAccountSeenUntil(account.id, time);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
db.setTransactionSuccessful();
} finally {
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;
}
@ -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() {
@Override
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) {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class));
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class));
}
public static void stopSynchronous(Context context, String reason) {
Log.i(Helper.TAG, "Stop because of '" + reason + "'");
final Semaphore semaphore = new Semaphore(0, true);
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");
public static void reload(Context context, String reason) {
Log.i(Helper.TAG, "Reload because of '" + reason + "'");
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class).setAction("reload"));
}
private class ServiceState {