From 86ea7e878e25295d1e8bf388e1b7a31d57cd170e Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 10 Sep 2020 15:46:56 +0300 Subject: [PATCH] Sync: fix the sync notification to work with etesync 2.0 --- .../syncadapter/CalendarSyncManager.kt | 2 +- .../syncadapter/syncadapter/SyncManager.kt | 59 ++++++++++++------- .../syncadapter/TasksSyncManager.kt | 2 +- app/src/main/res/values-nb-rNO/strings.xml | 2 +- app/src/main/res/values-pl/strings.xml | 2 +- app/src/main/res/values/plurals.xml | 4 +- app/src/main/res/values/strings.xml | 2 +- 7 files changed, 44 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt index 7592b8b2..377593aa 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt @@ -44,7 +44,7 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra get() = context.getString(R.string.sync_error_calendar, account.name) override val syncSuccessfullyTitle: String - get() = context.getString(R.string.sync_successfully_calendar, info.displayName, + get() = context.getString(R.string.sync_successfully_calendar, localCalendar().displayName, account.name) init { diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt index 1208b817..0b15fd2d 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt @@ -36,6 +36,7 @@ import com.etesync.syncadapter.resource.* import com.etesync.syncadapter.ui.AccountsActivity import com.etesync.syncadapter.ui.DebugInfoActivity import com.etesync.syncadapter.ui.ViewCollectionActivity +import com.etesync.syncadapter.ui.etebase.CollectionActivity import org.jetbrains.anko.defaultSharedPreferences import java.io.Closeable import java.io.FileNotFoundException @@ -64,6 +65,11 @@ constructor(protected val context: Context, protected val account: Account, prot protected lateinit var itemMgr: ItemManager protected lateinit var cachedCollection: CachedCollection + // Sync counters + private var syncItemsTotal = 0 + private var syncItemsDeleted = 0 + private var syncItemsChanged = 0 + protected var journal: JournalEntryManager? = null private var _journalEntity: JournalEntity? = null @@ -140,6 +146,10 @@ constructor(protected val context: Context, protected val account: Account, prot @TargetApi(21) fun performSync() { + syncItemsTotal = 0 + syncItemsDeleted = 0 + syncItemsChanged = 0 + var syncPhase = R.string.sync_phase_prepare try { Logger.log.info("Sync phase: " + context.getString(syncPhase)) @@ -331,39 +341,28 @@ constructor(protected val context: Context, protected val account: Account, prot private fun notifyUserOnSync() { val changeNotification = context.defaultSharedPreferences.getBoolean(App.CHANGE_NOTIFICATION, true) - val remoteEntries = remoteEntries - if ((remoteEntries == null) || remoteEntries.isEmpty() || !changeNotification) { + + if (!changeNotification || (syncItemsTotal == 0)) { return } + val notificationHelper = SyncNotification(context, System.currentTimeMillis().toString(), notificationId()) - - var deleted = 0 - var added = 0 - var changed = 0 - for (entry in remoteEntries) { - val cEntry = SyncEntry.fromJournalEntry(crypto, entry) - val action = cEntry.action - when (action) { - ADD -> added++ - SyncEntry.Actions.DELETE -> deleted++ - SyncEntry.Actions.CHANGE -> changed++ - } - } - val resources = context.resources - val intent = ViewCollectionActivity.newIntent(context, account, info) + val intent = if (isLegacy) { + ViewCollectionActivity.newIntent(context, account, info) + } else { + CollectionActivity.newIntent(context, account, cachedCollection.col.uid) + } notificationHelper.notify(syncSuccessfullyTitle, String.format(context.getString(R.string.sync_successfully_modified), resources.getQuantityString(R.plurals.sync_successfully, - remoteEntries!!.size, remoteEntries!!.size)), + syncItemsTotal, syncItemsTotal)), String.format(context.getString(R.string.sync_successfully_modified_full), resources.getQuantityString(R.plurals.sync_successfully, - added, added), + syncItemsChanged, syncItemsChanged), resources.getQuantityString(R.plurals.sync_successfully, - changed, changed), - resources.getQuantityString(R.plurals.sync_successfully, - deleted, deleted)), + syncItemsDeleted, syncItemsDeleted)), intent) } @@ -443,6 +442,7 @@ constructor(protected val context: Context, protected val account: Account, prot // Process new vcards from server val size = items.size var i = 0 + syncItemsTotal += size for (item in items) { if (Thread.interrupted()) { @@ -451,6 +451,12 @@ constructor(protected val context: Context, protected val account: Account, prot i++ Logger.log.info("Processing (${i}/${size}) UID=${item.uid} Etag=${item.etag}") + if (item.isDeleted) { + syncItemsDeleted++ + } else { + syncItemsChanged++ + } + processItem(item) persistItem(item) } @@ -491,6 +497,8 @@ constructor(protected val context: Context, protected val account: Account, prot val strTotal = remoteEntries!!.size.toString() var i = 0 + syncItemsTotal += remoteEntries!!.size + for (entry in remoteEntries!!) { if (Thread.interrupted()) { throw InterruptedException() @@ -508,6 +516,13 @@ constructor(protected val context: Context, protected val account: Account, prot error = e.toString() } + val action = cEntry.action + when (action) { + ADD -> syncItemsChanged++ + SyncEntry.Actions.DELETE -> syncItemsDeleted++ + SyncEntry.Actions.CHANGE -> syncItemsChanged++ + } + persistSyncEntry(entry.uid, cEntry, error) remoteCTag = entry.uid diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt index 51643537..ccebb0a7 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt @@ -44,7 +44,7 @@ class TasksSyncManager( get() = context.getString(R.string.sync_error_tasks, account.name) override val syncSuccessfullyTitle: String - get() = context.getString(R.string.sync_successfully_tasks, info.displayName, + get() = context.getString(R.string.sync_successfully_tasks, localTaskList().name!!, account.name) init { diff --git a/app/src/main/res/values-nb-rNO/strings.xml b/app/src/main/res/values-nb-rNO/strings.xml index 905d54f3..47252564 100644 --- a/app/src/main/res/values-nb-rNO/strings.xml +++ b/app/src/main/res/values-nb-rNO/strings.xml @@ -317,7 +317,7 @@ Kontakter endret (%s) Gjøremålene «%s» er endret (%s) %s endret. - %s lagt til.\n%s oppdatert.\n%s slettet. + %s oppdatert.\n%s slettet. Journalen \"%s\" er skrivebeskyttet Journalen er skrivebeskyttet, så alle endringene dine (%d) har blitt omgjort. diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 5dfc831c..a0104319 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -287,7 +287,7 @@ Kalendarz \"%s\" został zmodyfikowany (%s) Kontakty zostały zmodyfikowane (%s) %s został zmodyfikowany. - %s został dodany. \n%s został zaktualizowany.\n%s został usunięty. + %s został zaktualizowany.\n%s został usunięty. EteSync: Bezpieczeństwo połączenia diff --git a/app/src/main/res/values/plurals.xml b/app/src/main/res/values/plurals.xml index 8549eacc..2182ca03 100644 --- a/app/src/main/res/values/plurals.xml +++ b/app/src/main/res/values/plurals.xml @@ -1,7 +1,7 @@ - %d entry - %d entries + %d item + %d items \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 857bf9ad..55a29486 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -390,7 +390,7 @@ Contacts modified (%s) Tasks \"%s\" modified (%s) %s modified. - %s added.\n%s updated.\n%s deleted. + %s updated.\n%s deleted. Journal \"%s\" is read only The journal is read only so all of your changes (%d) have been reverted.