Merge pull request #5574 from k9mail/no_notification_on_first_sync

Don't create notifications when first syncing a folder
This commit is contained in:
cketti 2021-08-21 21:22:57 +02:00 committed by GitHub
commit e229254e53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 51 additions and 28 deletions

View file

@ -65,6 +65,8 @@ import com.fsck.k9.mail.MessageRetrievalListener;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mailstore.FolderDetailsAccessor;
import com.fsck.k9.mailstore.ListenableMessageStore;
import com.fsck.k9.mailstore.LocalFolder;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mailstore.LocalStore;
@ -679,9 +681,13 @@ public class MessagingController {
return;
}
MessageStore messageStore = messageStoreManager.getMessageStore(account);
Long lastChecked = messageStore.getFolder(folderId, FolderDetailsAccessor::getLastChecked);
boolean suppressNotifications = lastChecked == null;
String folderServerId = localFolder.getServerId();
SyncConfig syncConfig = createSyncConfig(account);
ControllerSyncListener syncListener = new ControllerSyncListener(account, listener);
ControllerSyncListener syncListener = new ControllerSyncListener(account, listener, suppressNotifications);
backend.sync(folderServerId, syncConfig, syncListener);
@ -2477,9 +2483,6 @@ public class MessagingController {
showFetchingMailNotificationIfNecessary(account, folder);
try {
synchronizeMailboxSynchronous(account, folder.getDatabaseId(), listener);
long now = System.currentTimeMillis();
folder.setLastChecked(now);
} finally {
clearFetchingMailNotificationIfNecessary(account);
}
@ -2716,12 +2719,14 @@ public class MessagingController {
private final MessagingListener listener;
private final LocalStore localStore;
private final int previousUnreadMessageCount;
private final boolean suppressNotifications;
boolean syncFailed = false;
ControllerSyncListener(Account account, MessagingListener listener) {
ControllerSyncListener(Account account, MessagingListener listener, boolean suppressNotifications) {
this.account = account;
this.listener = listener;
this.suppressNotifications = suppressNotifications;
this.localStore = getLocalStoreOrThrow(account);
previousUnreadMessageCount = getUnreadMessageCount(account);
@ -2778,7 +2783,8 @@ public class MessagingController {
// Send a notification of this message
LocalMessage message = loadMessage(folderServerId, messageServerId);
LocalFolder localFolder = message.getFolder();
if (notificationStrategy.shouldNotifyForMessage(account, localFolder, message, isOldMessage)) {
if (!suppressNotifications &&
notificationStrategy.shouldNotifyForMessage(account, localFolder, message, isOldMessage)) {
Timber.v("Creating notification for message %s:%s", localFolder.getName(), message.getUid());
// Notify with the localMessage so that we don't have to recalculate the content preview.
notificationController.addNewMailNotification(account, message, previousUnreadMessageCount);

View file

@ -21,6 +21,7 @@ interface FolderDetailsAccessor {
val pushClass: FolderClass
val visibleLimit: Int
val moreMessages: MoreMessages
val lastChecked: Long?
val unreadMessageCount: Int
val starredMessageCount: Int

View file

@ -67,7 +67,7 @@ class K9BackendFolder(
}
override fun setLastChecked(timestamp: Long) {
messageStore.setLastUpdated(folderId, timestamp)
messageStore.setLastChecked(folderId, timestamp)
}
override fun setStatus(status: String?) {

View file

@ -351,16 +351,6 @@ public class LocalFolder {
}
}
public void setLastChecked(final long lastChecked) throws MessagingException {
try {
open();
this.lastChecked = lastChecked;
} catch (MessagingException e) {
throw new WrappedException(e);
}
updateFolderColumn("last_updated", lastChecked);
}
public int getVisibleLimit() throws MessagingException {
open();
return visibleLimit;
@ -1124,6 +1114,7 @@ public class LocalFolder {
db.execSQL("DELETE FROM messages WHERE folder_id = ?", folderIdArg);
setMoreMessages(MoreMessages.UNKNOWN);
resetLastChecked(db);
return null;
} catch (MessagingException e) {
@ -1137,10 +1128,17 @@ public class LocalFolder {
this.localStore.notifyChange();
setLastChecked(0);
setVisibleLimit(getAccount().getDisplayCount());
}
private void resetLastChecked(SQLiteDatabase db) {
lastChecked = 0;
ContentValues values = new ContentValues();
values.putNull("last_updated");
db.update("folders", values, "id = ?", new String[] { Long.toString(databaseId) });
}
public void destroyLocalOnlyMessages() throws MessagingException {
destroyMessages("uid LIKE '" + K9.LOCAL_UID_PREFIX + "%'");
}

View file

@ -212,9 +212,9 @@ interface MessageStore {
fun setMoreMessages(folderId: Long, moreMessages: MoreMessages)
/**
* Update the 'last updated' state of a folder.
* Update the time when the folder was last checked for new messages.
*/
fun setLastUpdated(folderId: Long, timestamp: Long)
fun setLastChecked(folderId: Long, timestamp: Long)
/**
* Update folder status message.

View file

@ -12,7 +12,7 @@ import timber.log.Timber;
class StoreSchemaDefinition implements SchemaDefinition {
static final int DB_VERSION = 79;
static final int DB_VERSION = 80;
private final MigrationsHelper migrationsHelper;

View file

@ -165,8 +165,8 @@ class K9MessageStore(
updateFolderOperations.setMoreMessages(folderId, moreMessages)
}
override fun setLastUpdated(folderId: Long, timestamp: Long) {
updateFolderOperations.setLastUpdated(folderId, timestamp)
override fun setLastChecked(folderId: Long, timestamp: Long) {
updateFolderOperations.setLastChecked(folderId, timestamp)
}
override fun setStatus(folderId: Long, status: String?) {

View file

@ -1,6 +1,7 @@
package com.fsck.k9.storage.messages
import android.database.Cursor
import androidx.core.database.getLongOrNull
import com.fsck.k9.Account.FolderMode
import com.fsck.k9.helper.map
import com.fsck.k9.mail.FolderClass
@ -171,11 +172,14 @@ private class CursorFolderAccessor(val cursor: Cursor) : FolderDetailsAccessor {
override val moreMessages: MoreMessages
get() = MoreMessages.fromDatabaseName(cursor.getString(12))
override val lastChecked: Long?
get() = cursor.getLongOrNull(13)
override val unreadMessageCount: Int
get() = cursor.getInt(13)
get() = cursor.getInt(14)
override val starredMessageCount: Int
get() = cursor.getInt(14)
get() = cursor.getInt(15)
override fun serverIdOrThrow(): String {
return serverId ?: error("No server ID found for folder '$name' ($id)")
@ -199,5 +203,6 @@ private val FOLDER_COLUMNS = arrayOf(
"notify_class",
"push_class",
"visible_limit",
"more_messages"
"more_messages",
"last_updated"
)

View file

@ -65,7 +65,7 @@ internal class UpdateFolderOperations(private val lockableDatabase: LockableData
setString(folderId = folderId, columnName = "more_messages", value = moreMessages.databaseName)
}
fun setLastUpdated(folderId: Long, timestamp: Long) {
fun setLastChecked(folderId: Long, timestamp: Long) {
lockableDatabase.execute(false) { db ->
val contentValues = ContentValues().apply {
put("last_updated", timestamp)

View file

@ -0,0 +1,12 @@
package com.fsck.k9.storage.migrations
import android.database.sqlite.SQLiteDatabase
/**
* Rewrite 'last_update' column to NULL when the value is 0
*/
internal class MigrationTo80(private val db: SQLiteDatabase) {
fun rewriteLastUpdatedColumn() {
db.execSQL("UPDATE folders SET last_updated = NULL WHERE last_updated = 0")
}
}

View file

@ -25,5 +25,6 @@ object Migrations {
// 77: No longer necessary
if (oldVersion < 78) MigrationTo78(db).removeServerIdFromLocalFolders()
if (oldVersion < 79) MigrationTo79(db).updateDeleteMessageTrigger()
if (oldVersion < 80) MigrationTo80(db).rewriteLastUpdatedColumn()
}
}

View file

@ -135,7 +135,7 @@ class UpdateFolderOperationsTest : RobolectricTest() {
fun `update late updated state`() {
val folderId = sqliteDatabase.createFolder(lastUpdated = 23)
updateFolderOperations.setLastUpdated(folderId = folderId, timestamp = 42)
updateFolderOperations.setLastChecked(folderId = folderId, timestamp = 42)
val folder = sqliteDatabase.readFolders().first()
assertThat(folder.id).isEqualTo(folderId)