mirror of
https://github.com/M66B/FairEmail.git
synced 2025-01-01 12:44:42 +00:00
Move unsent messages to send service, refactoring
This commit is contained in:
parent
39b4715915
commit
889450d5af
4 changed files with 42 additions and 25 deletions
|
@ -82,11 +82,13 @@ public interface DaoAccount {
|
||||||
", (SELECT COUNT(operation.id) FROM operation" +
|
", (SELECT COUNT(operation.id) FROM operation" +
|
||||||
" JOIN folder ON folder.id = operation.folder" +
|
" JOIN folder ON folder.id = operation.folder" +
|
||||||
" JOIN account ON account.id = folder.account" + // not outbox
|
" JOIN account ON account.id = folder.account" + // not outbox
|
||||||
" WHERE account.synchronize) AS operations" +
|
" WHERE account.synchronize) AS operations")
|
||||||
", (SELECT COUNT(operation.id) FROM operation" +
|
|
||||||
" WHERE operation.name = '" + EntityOperation.SEND + "') AS unsent")
|
|
||||||
LiveData<TupleAccountStats> liveStats();
|
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" +
|
@Query("SELECT account.id, swipe_left, l.type AS left_type, swipe_right, r.type AS right_type" +
|
||||||
" FROM account" +
|
" FROM account" +
|
||||||
" LEFT JOIN folder l ON l.id = account.swipe_left" +
|
" 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.core.app.NotificationCompat;
|
||||||
import androidx.lifecycle.LifecycleService;
|
import androidx.lifecycle.LifecycleService;
|
||||||
|
import androidx.lifecycle.Observer;
|
||||||
|
|
||||||
public class ServiceSend extends LifecycleService {
|
public class ServiceSend extends LifecycleService {
|
||||||
|
private int lastUnsent = 0;
|
||||||
|
|
||||||
private static final int IDENTITY_ERROR_AFTER = 30; // minutes
|
private static final int IDENTITY_ERROR_AFTER = 30; // minutes
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -68,6 +71,16 @@ public class ServiceSend extends LifecycleService {
|
||||||
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
||||||
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
||||||
cm.registerNetworkCallback(builder.build(), networkCallback);
|
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
|
@Override
|
||||||
|
@ -87,7 +100,17 @@ public class ServiceSend extends LifecycleService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
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");
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");
|
||||||
|
|
||||||
builder
|
builder
|
||||||
|
@ -99,11 +122,12 @@ public class ServiceSend extends LifecycleService {
|
||||||
.setCategory(Notification.CATEGORY_STATUS)
|
.setCategory(Notification.CATEGORY_STATUS)
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
.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 builder;
|
||||||
|
|
||||||
return START_STICKY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
||||||
|
|
|
@ -86,7 +86,7 @@ import androidx.lifecycle.Observer;
|
||||||
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
||||||
|
|
||||||
public class ServiceSynchronize extends LifecycleService {
|
public class ServiceSynchronize extends LifecycleService {
|
||||||
private TupleAccountStats lastStats = null;
|
private TupleAccountStats lastStats = new TupleAccountStats();
|
||||||
private ServiceManager serviceManager = new ServiceManager();
|
private ServiceManager serviceManager = new ServiceManager();
|
||||||
|
|
||||||
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
||||||
|
@ -112,8 +112,6 @@ public class ServiceSynchronize extends LifecycleService {
|
||||||
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
||||||
cm.registerNetworkCallback(builder.build(), serviceManager);
|
cm.registerNetworkCallback(builder.build(), serviceManager);
|
||||||
|
|
||||||
JobDaily.schedule(this);
|
|
||||||
|
|
||||||
DB db = DB.getInstance(this);
|
DB db = DB.getInstance(this);
|
||||||
|
|
||||||
db.account().liveStats().observe(this, new Observer<TupleAccountStats>() {
|
db.account().liveStats().observe(this, new Observer<TupleAccountStats>() {
|
||||||
|
@ -130,6 +128,8 @@ public class ServiceSynchronize extends LifecycleService {
|
||||||
Core.notifyMessages(ServiceSynchronize.this, messages);
|
Core.notifyMessages(ServiceSynchronize.this, messages);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
JobDaily.schedule(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -191,10 +191,8 @@ public class ServiceSynchronize extends LifecycleService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) {
|
private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) {
|
||||||
if (stats == null)
|
if (stats != null)
|
||||||
stats = lastStats;
|
lastStats = stats;
|
||||||
if (stats == null)
|
|
||||||
stats = new TupleAccountStats();
|
|
||||||
|
|
||||||
// Build pending intent
|
// Build pending intent
|
||||||
Intent intent = new Intent(this, ServiceUI.class);
|
Intent intent = new Intent(this, ServiceUI.class);
|
||||||
|
@ -207,7 +205,7 @@ public class ServiceSynchronize extends LifecycleService {
|
||||||
builder
|
builder
|
||||||
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
||||||
.setContentTitle(getResources().getQuantityString(
|
.setContentTitle(getResources().getQuantityString(
|
||||||
R.plurals.title_notification_synchronizing, stats.accounts, stats.accounts))
|
R.plurals.title_notification_synchronizing, lastStats.accounts, lastStats.accounts))
|
||||||
.setContentIntent(pi)
|
.setContentIntent(pi)
|
||||||
.setAutoCancel(false)
|
.setAutoCancel(false)
|
||||||
.setShowWhen(false)
|
.setShowWhen(false)
|
||||||
|
@ -215,16 +213,10 @@ public class ServiceSynchronize extends LifecycleService {
|
||||||
.setCategory(Notification.CATEGORY_STATUS)
|
.setCategory(Notification.CATEGORY_STATUS)
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
||||||
|
|
||||||
if (stats.operations > 0)
|
if (lastStats.operations > 0)
|
||||||
builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(
|
builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(
|
||||||
getResources().getQuantityString(
|
getResources().getQuantityString(
|
||||||
R.plurals.title_notification_operations, stats.operations, stats.operations)));
|
R.plurals.title_notification_operations, lastStats.operations, lastStats.operations)));
|
||||||
|
|
||||||
if (stats.unsent > 0)
|
|
||||||
builder.setContentText(getResources().getQuantityString(
|
|
||||||
R.plurals.title_notification_unsent, stats.unsent, stats.unsent));
|
|
||||||
|
|
||||||
lastStats = stats;
|
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,5 +22,4 @@ package eu.faircode.email;
|
||||||
public class TupleAccountStats {
|
public class TupleAccountStats {
|
||||||
public Integer accounts = 0;
|
public Integer accounts = 0;
|
||||||
public Integer operations = 0;
|
public Integer operations = 0;
|
||||||
public Integer unsent = 0;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue