diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt index 2d65aa0c24..696e965963 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt @@ -161,7 +161,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context notificationState.clearAndAddRenderedEvents(eventsToRender) val session = currentSession ?: return renderEvents(session, eventsToRender) - notificationState.persistState(context, session) + notificationState.persist(context, session) } } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationEventPersistence.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationEventPersistence.kt new file mode 100644 index 0000000000..65a3203d50 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationEventPersistence.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.notifications + +import android.content.Context +import org.matrix.android.sdk.api.session.Session +import timber.log.Timber +import java.io.File +import java.io.FileOutputStream + +// TODO Multi-account +private const val ROOMS_NOTIFICATIONS_FILE_NAME = "im.vector.notifications.cache" +private const val KEY_ALIAS_SECRET_STORAGE = "notificationMgr" + +object NotificationEventPersistence { + + fun loadEvents(context: Context, currentSession: Session?): NotificationEventQueue { + try { + val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) + if (file.exists()) { + file.inputStream().use { + val events: ArrayList? = currentSession?.loadSecureSecret(it, KEY_ALIAS_SECRET_STORAGE) + if (events != null) { + return NotificationEventQueue(events.toMutableList()) + } + } + } + } catch (e: Throwable) { + Timber.e(e, "## Failed to load cached notification info") + } + return NotificationEventQueue() + } + + fun persistEvents(queuedEvents: NotificationEventQueue, context: Context, currentSession: Session) { + synchronized(queuedEvents) { + if (queuedEvents.isEmpty()) { + deleteCachedRoomNotifications(context) + return + } + try { + val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) + if (!file.exists()) file.createNewFile() + FileOutputStream(file).use { + currentSession.securelyStoreObject(queuedEvents.rawEvents(), KEY_ALIAS_SECRET_STORAGE, it) + } + } catch (e: Throwable) { + Timber.e(e, "## Failed to save cached notification info") + } + } + } + + private fun deleteCachedRoomNotifications(context: Context) { + val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) + if (file.exists()) { + file.delete() + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationState.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationState.kt index 62a64b1d96..4cb44609c0 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationState.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationState.kt @@ -18,13 +18,6 @@ package im.vector.app.features.notifications import android.content.Context import org.matrix.android.sdk.api.session.Session -import timber.log.Timber -import java.io.File -import java.io.FileOutputStream - -// TODO Mutliaccount -private const val ROOMS_NOTIFICATIONS_FILE_NAME = "im.vector.notifications.cache" -private const val KEY_ALIAS_SECRET_STORAGE = "notificationMgr" class NotificationState( /** @@ -65,57 +58,20 @@ class NotificationState( fun hasAlreadyRendered(eventsToRender: List>) = renderedEvents == eventsToRender - fun persistState(context: Context, currentSession: Session) { - synchronized(queuedEvents) { - if (queuedEvents.isEmpty()) { - deleteCachedRoomNotifications(context) - return - } - try { - val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) - if (!file.exists()) file.createNewFile() - FileOutputStream(file).use { - currentSession.securelyStoreObject(queuedEvents.rawEvents(), KEY_ALIAS_SECRET_STORAGE, it) - } - } catch (e: Throwable) { - Timber.e(e, "## Failed to save cached notification info") - } - } - } - - private fun deleteCachedRoomNotifications(context: Context) { - val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) - if (file.exists()) { - file.delete() - } + fun persist(context: Context, session: Session) { + NotificationEventPersistence.persistEvents(queuedEvents, context, session) } companion object { fun createInitialNotificationState(context: Context, currentSession: Session?): NotificationState { - val queuedEvents = loadEventInfo(context, currentSession) + val queuedEvents = NotificationEventPersistence.loadEvents(context, currentSession) return NotificationState( queuedEvents = queuedEvents, renderedEvents = queuedEvents.rawEvents().map { ProcessedEvent(ProcessedEvent.Type.KEEP, it) }.toMutableList(), seenEventIds = CircularCache.create(cacheSize = 25) ) } - - private fun loadEventInfo(context: Context, currentSession: Session?): NotificationEventQueue { - try { - val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) - if (file.exists()) { - file.inputStream().use { - val events: ArrayList? = currentSession?.loadSecureSecret(it, KEY_ALIAS_SECRET_STORAGE) - if (events != null) { - return NotificationEventQueue(events.toMutableList()) - } - } - } - } catch (e: Throwable) { - Timber.e(e, "## Failed to load cached notification info") - } - return NotificationEventQueue() - } } } +