From af28f983cd5bb4cb8114379916e01079da0daeb3 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 11 Aug 2018 08:37:56 +0000 Subject: [PATCH] Refactoring --- .../eu/faircode/email/EntityOperation.java | 8 ++---- .../eu/faircode/email/ServiceSynchronize.java | 25 +++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 4c5ec85c..40f81aeb 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -99,12 +99,8 @@ public class EntityOperation { operation.id = db.operation().insertOperation(operation); Intent intent = new Intent(); - if (SEND.equals(name)) - intent.setAction(ServiceSynchronize.ACTION_PROCESS_OUTBOX); - else { - intent.setType("account/" + message.account); - intent.setAction(ServiceSynchronize.ACTION_PROCESS_FOLDER); - } + intent.setType("account/" + (message.account == null ? "outbox" : message.account)); + intent.setAction(ServiceSynchronize.ACTION_PROCESS_OPERATIONS); intent.putExtra("folder", message.folder); synchronized (queue) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 130786ad..db600782 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -105,8 +105,7 @@ public class ServiceSynchronize extends LifecycleService { private static final int FETCH_BATCH_SIZE = 10; private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes - static final String ACTION_PROCESS_FOLDER = BuildConfig.APPLICATION_ID + ".PROCESS_FOLDER"; - static final String ACTION_PROCESS_OUTBOX = BuildConfig.APPLICATION_ID + ".PROCESS_OUTBOX"; + static final String ACTION_PROCESS_OPERATIONS = BuildConfig.APPLICATION_ID + ".PROCESS_OPERATIONS"; public ServiceSynchronize() { // https://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html @@ -407,7 +406,7 @@ public class ServiceSynchronize extends LifecycleService { }, "sync.folder." + folder.id).start(); } - IntentFilter f = new IntentFilter(ACTION_PROCESS_FOLDER); + IntentFilter f = new IntentFilter(ACTION_PROCESS_OPERATIONS); f.addDataType("account/" + account.id); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this); lbm.registerReceiver(processReceiver, f); @@ -415,7 +414,7 @@ public class ServiceSynchronize extends LifecycleService { Log.i(Helper.TAG, "listen process folder"); for (final EntityFolder folder : db.folder().getFolders(account.id)) if (!EntityFolder.OUTBOX.equals(folder.type)) - lbm.sendBroadcast(new Intent(ACTION_PROCESS_FOLDER) + lbm.sendBroadcast(new Intent(ACTION_PROCESS_OPERATIONS) .setType("account/" + account.id) .putExtra("folder", folder.id)); @@ -1314,11 +1313,17 @@ public class ServiceSynchronize extends LifecycleService { } outbox = db.folder().getOutbox(); - if (outbox != null) { + if (outbox != null) try { + IntentFilter f = new IntentFilter(ACTION_PROCESS_OPERATIONS); + f.addDataType("account/outbox"); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this); - lbm.registerReceiver(receiverOutbox, new IntentFilter(ACTION_PROCESS_OUTBOX)); - Log.i(Helper.TAG, outbox.name + " listen operations"); - lbm.sendBroadcast(new Intent(ACTION_PROCESS_OUTBOX)); + lbm.registerReceiver(outboxReceiver, f); + + lbm.sendBroadcast(new Intent(ACTION_PROCESS_OPERATIONS) + .setType("account/outbox") + .putExtra("folder", outbox.id)); + } catch (Throwable ex) { + Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); } } @@ -1336,12 +1341,12 @@ public class ServiceSynchronize extends LifecycleService { if (outbox != null) { LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this); - lbm.unregisterReceiver(receiverOutbox); + lbm.unregisterReceiver(outboxReceiver); Log.i(Helper.TAG, outbox.name + " unlisten operations"); } } - BroadcastReceiver receiverOutbox = new BroadcastReceiver() { + BroadcastReceiver outboxReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.i(Helper.TAG, outbox.name + " run operations");