From 889450d5aff2b25217e4772fbea8c01779d91f1f Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 27 Feb 2019 20:49:49 +0000 Subject: [PATCH] Move unsent messages to send service, refactoring --- .../java/eu/faircode/email/DaoAccount.java | 8 +++-- .../java/eu/faircode/email/ServiceSend.java | 34 ++++++++++++++++--- .../eu/faircode/email/ServiceSynchronize.java | 24 +++++-------- .../eu/faircode/email/TupleAccountStats.java | 1 - 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoAccount.java b/app/src/main/java/eu/faircode/email/DaoAccount.java index d521b3f19d..ac763c2848 100644 --- a/app/src/main/java/eu/faircode/email/DaoAccount.java +++ b/app/src/main/java/eu/faircode/email/DaoAccount.java @@ -82,11 +82,13 @@ public interface DaoAccount { ", (SELECT COUNT(operation.id) FROM operation" + " JOIN folder ON folder.id = operation.folder" + " JOIN account ON account.id = folder.account" + // not outbox - " WHERE account.synchronize) AS operations" + - ", (SELECT COUNT(operation.id) FROM operation" + - " WHERE operation.name = '" + EntityOperation.SEND + "') AS unsent") + " WHERE account.synchronize) AS operations") LiveData liveStats(); + @Query("SELECT COUNT(operation.id) FROM operation" + + " WHERE operation.name = '" + EntityOperation.SEND + "'") + LiveData liveUnsent(); + @Query("SELECT account.id, swipe_left, l.type AS left_type, swipe_right, r.type AS right_type" + " FROM account" + " LEFT JOIN folder l ON l.id = account.swipe_left" + diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index dcff00a5f2..9a5a929a7d 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -55,8 +55,11 @@ import javax.mail.internet.MimeMessage; import androidx.core.app.NotificationCompat; import androidx.lifecycle.LifecycleService; +import androidx.lifecycle.Observer; public class ServiceSend extends LifecycleService { + private int lastUnsent = 0; + private static final int IDENTITY_ERROR_AFTER = 30; // minutes @Override @@ -68,6 +71,16 @@ public class ServiceSend extends LifecycleService { NetworkRequest.Builder builder = new NetworkRequest.Builder(); builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); cm.registerNetworkCallback(builder.build(), networkCallback); + + DB db = DB.getInstance(this); + + db.account().liveUnsent().observe(this, new Observer() { + @Override + public void onChanged(Integer unsent) { + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + nm.notify(Helper.NOTIFICATION_SEND, getNotificationService(unsent).build()); + } + }); } @Override @@ -87,7 +100,17 @@ public class ServiceSend extends LifecycleService { @Override public int onStartCommand(Intent intent, int flags, int startId) { - // Build notification + startForeground(Helper.NOTIFICATION_SEND, getNotificationService(null).build()); + + super.onStartCommand(intent, flags, startId); + + return START_STICKY; + } + + NotificationCompat.Builder getNotificationService(Integer unsent) { + if (unsent != null) + lastUnsent = unsent; + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service"); builder @@ -99,11 +122,12 @@ public class ServiceSend extends LifecycleService { .setCategory(Notification.CATEGORY_STATUS) .setVisibility(NotificationCompat.VISIBILITY_SECRET); - startForeground(Helper.NOTIFICATION_SEND, builder.build()); + if (lastUnsent > 0) + builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText( + getResources().getQuantityString( + R.plurals.title_notification_unsent, lastUnsent, lastUnsent))); - super.onStartCommand(intent, flags, startId); - - return START_STICKY; + return builder; } ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 51bafa86b9..5644bac08f 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -86,7 +86,7 @@ import androidx.lifecycle.Observer; import static android.os.Process.THREAD_PRIORITY_BACKGROUND; public class ServiceSynchronize extends LifecycleService { - private TupleAccountStats lastStats = null; + private TupleAccountStats lastStats = new TupleAccountStats(); private ServiceManager serviceManager = new ServiceManager(); private static final int CONNECT_BACKOFF_START = 8; // seconds @@ -112,8 +112,6 @@ public class ServiceSynchronize extends LifecycleService { // builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); cm.registerNetworkCallback(builder.build(), serviceManager); - JobDaily.schedule(this); - DB db = DB.getInstance(this); db.account().liveStats().observe(this, new Observer() { @@ -130,6 +128,8 @@ public class ServiceSynchronize extends LifecycleService { Core.notifyMessages(ServiceSynchronize.this, messages); } }); + + JobDaily.schedule(this); } @Override @@ -191,10 +191,8 @@ public class ServiceSynchronize extends LifecycleService { } private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) { - if (stats == null) - stats = lastStats; - if (stats == null) - stats = new TupleAccountStats(); + if (stats != null) + lastStats = stats; // Build pending intent Intent intent = new Intent(this, ServiceUI.class); @@ -207,7 +205,7 @@ public class ServiceSynchronize extends LifecycleService { builder .setSmallIcon(R.drawable.baseline_compare_arrows_white_24) .setContentTitle(getResources().getQuantityString( - R.plurals.title_notification_synchronizing, stats.accounts, stats.accounts)) + R.plurals.title_notification_synchronizing, lastStats.accounts, lastStats.accounts)) .setContentIntent(pi) .setAutoCancel(false) .setShowWhen(false) @@ -215,16 +213,10 @@ public class ServiceSynchronize extends LifecycleService { .setCategory(Notification.CATEGORY_STATUS) .setVisibility(NotificationCompat.VISIBILITY_SECRET); - if (stats.operations > 0) + if (lastStats.operations > 0) builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText( getResources().getQuantityString( - R.plurals.title_notification_operations, stats.operations, stats.operations))); - - if (stats.unsent > 0) - builder.setContentText(getResources().getQuantityString( - R.plurals.title_notification_unsent, stats.unsent, stats.unsent)); - - lastStats = stats; + R.plurals.title_notification_operations, lastStats.operations, lastStats.operations))); return builder; } diff --git a/app/src/main/java/eu/faircode/email/TupleAccountStats.java b/app/src/main/java/eu/faircode/email/TupleAccountStats.java index 372e7d218c..a59d72574e 100644 --- a/app/src/main/java/eu/faircode/email/TupleAccountStats.java +++ b/app/src/main/java/eu/faircode/email/TupleAccountStats.java @@ -22,5 +22,4 @@ package eu.faircode.email; public class TupleAccountStats { public Integer accounts = 0; public Integer operations = 0; - public Integer unsent = 0; }