diff --git a/app/src/main/java/eu/faircode/email/DaoFolder.java b/app/src/main/java/eu/faircode/email/DaoFolder.java index a554fbc800..f9125e1b19 100644 --- a/app/src/main/java/eu/faircode/email/DaoFolder.java +++ b/app/src/main/java/eu/faircode/email/DaoFolder.java @@ -130,10 +130,10 @@ public interface DaoFolder { " GROUP BY folder.id") LiveData> liveNavigation(); - @Query("SELECT COUNT(id) FROM folder" + - " WHERE sync_state = 'syncing'" + + @Query("SELECT account, id AS folder, unified, sync_state FROM folder" + + " WHERE sync_state IS NOT NULL" + " AND folder.type <> '" + EntityFolder.OUTBOX + "'") - LiveData liveSynchronizing(); + LiveData> liveSynchronizing(); @Query("SELECT folder.*" + ", account.id AS accountId, account.pop AS accountProtocol, account.`order` AS accountOrder" + diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 0692fd0293..417deb4712 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -24,7 +24,9 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND; import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -604,14 +606,60 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences final TwoStateOwner cowner = new TwoStateOwner(this, "liveUnseenNotify"); - db.folder().liveSynchronizing().observe(this, new Observer() { + db.folder().liveSynchronizing().observe(this, new Observer>() { @Override - public void onChanged(Integer count) { - Log.i("Synchronizing folders=" + count); - if (count == null || count == 0) + public void onChanged(List syncs) { + int syncing = 0; + List accounts = new ArrayList<>(); + List folders = new ArrayList<>(); + if (syncs != null) + for (TupleFolderSync sync : syncs) { + if ("syncing".equals(sync.sync_state)) + syncing++; + if (sync.unified && !accounts.contains(sync.account)) + accounts.add(sync.account); + folders.add(sync.folder); + } + + Log.i("Syncing=" + syncing + + " folders=" + folders.size() + + " accounts=" + accounts.size()); + + if (syncing == 0) cowner.start(); else cowner.stop(); + + for (String _key : prefs.getAll().keySet()) + if (_key.startsWith("widget.") && _key.endsWith(".refresh") && + prefs.getBoolean(_key, false)) { + int appWidgetId = Integer.parseInt(_key.split("\\.")[1]); + + String key = "widget." + appWidgetId + ".sync"; + boolean sync = prefs.contains(key); + if (!sync) + continue; + + long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); + long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L); + + if (folder > 0) { + if (!folders.contains(folder)) { + prefs.edit().remove(key).apply(); + WidgetUnified.init(ServiceSynchronize.this, appWidgetId); + } + } else if (account > 0) { + if (!accounts.contains(account)) { + prefs.edit().remove(key).apply(); + WidgetUnified.init(ServiceSynchronize.this, appWidgetId); + } + } else { + if (accounts.size() == 0) { + prefs.edit().remove(key).apply(); + WidgetUnified.init(ServiceSynchronize.this, appWidgetId); + } + } + } } }); diff --git a/app/src/main/java/eu/faircode/email/ServiceUI.java b/app/src/main/java/eu/faircode/email/ServiceUI.java index 24aed4ca21..68f115f5e0 100644 --- a/app/src/main/java/eu/faircode/email/ServiceUI.java +++ b/app/src/main/java/eu/faircode/email/ServiceUI.java @@ -58,7 +58,7 @@ public class ServiceUI extends IntentService { static final int PI_SNOOZE = 10; static final int PI_IGNORED = 11; - private static final long WIDGET_SYNC_DURATION = 500; + private static final long WIDGET_SYNC_DURATION = 90 * 1000L; public ServiceUI() { this(ServiceUI.class.getName()); diff --git a/app/src/main/java/eu/faircode/email/TupleFolderSync.java b/app/src/main/java/eu/faircode/email/TupleFolderSync.java new file mode 100644 index 0000000000..dde9bb8533 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/TupleFolderSync.java @@ -0,0 +1,27 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +public class TupleFolderSync { + public long account; + public long folder; + public boolean unified; + public String sync_state; +} diff --git a/app/src/main/java/eu/faircode/email/WidgetUnified.java b/app/src/main/java/eu/faircode/email/WidgetUnified.java index 87bba6f228..1016ab0ca9 100644 --- a/app/src/main/java/eu/faircode/email/WidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/WidgetUnified.java @@ -101,16 +101,15 @@ public class WidgetUnified extends AppWidgetProvider { views.setOnClickPendingIntent(R.id.title, pi); - if (refresh) { - long now = new Date().getTime(); - long refreshing = prefs.getLong("widget." + appWidgetId + ".sync", 0L); - views.setViewVisibility(R.id.refresh, refreshing < now ? View.VISIBLE : View.INVISIBLE); - } else - views.setViewVisibility(R.id.refresh, View.GONE); - + views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.GONE); views.setViewPadding(R.id.refresh, px, px, px, px); views.setOnClickPendingIntent(R.id.refresh, piSync); + long now = new Date().getTime(); + long refreshing = prefs.getLong("widget." + appWidgetId + ".sync", 0L); + views.setImageViewResource(R.id.refresh, refreshing < now ? R.drawable.twotone_sync_24 : R.drawable.twotone_compare_arrows_24); + views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.INVISIBLE); + views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE); views.setViewPadding(R.id.compose, px, px, px, px); views.setOnClickPendingIntent(R.id.compose, piCompose);