Move unsent messages to send service, refactoring

pull/152/head
M66B 6 years ago
parent 39b4715915
commit 889450d5af

@ -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<TupleAccountStats> liveStats();
@Query("SELECT COUNT(operation.id) FROM operation" +
" WHERE operation.name = '" + EntityOperation.SEND + "'")
LiveData<Integer> 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" +

@ -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<Integer>() {
@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());
super.onStartCommand(intent, flags, startId);
if (lastUnsent > 0)
builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(
getResources().getQuantityString(
R.plurals.title_notification_unsent, lastUnsent, lastUnsent)));
return START_STICKY;
return builder;
}
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {

@ -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<TupleAccountStats>() {
@ -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;
}

@ -22,5 +22,4 @@ package eu.faircode.email;
public class TupleAccountStats {
public Integer accounts = 0;
public Integer operations = 0;
public Integer unsent = 0;
}

Loading…
Cancel
Save