NotificationEventPersistence is now an interface, to allow in-memory implementation.
This commit is contained in:
committed by
Benoit Marty
parent
c3eb653261
commit
0930cd0dc6
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 io.element.android.libraries.push.impl.notifications
|
||||
|
||||
import android.content.Context
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.androidutils.file.EncryptedFileFactory
|
||||
import io.element.android.libraries.androidutils.file.safeDelete
|
||||
import io.element.android.libraries.core.data.tryOrNull
|
||||
import io.element.android.libraries.core.log.logger.LoggerTag
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val ROOMS_NOTIFICATIONS_FILE_NAME_LEGACY = "im.vector.notifications.cache"
|
||||
private const val FILE_NAME = "notifications.bin"
|
||||
|
||||
private val loggerTag = LoggerTag("NotificationEventPersistence", LoggerTag.NotificationLoggerTag)
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultNotificationEventPersistence @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
) : NotificationEventPersistence {
|
||||
private val file by lazy {
|
||||
deleteLegacyFileIfAny()
|
||||
context.getDatabasePath(FILE_NAME)
|
||||
}
|
||||
|
||||
private val encryptedFile by lazy {
|
||||
EncryptedFileFactory(context).create(file)
|
||||
}
|
||||
|
||||
override fun loadEvents(factory: (List<NotifiableEvent>) -> NotificationEventQueue): NotificationEventQueue {
|
||||
val rawEvents: ArrayList<NotifiableEvent>? = file
|
||||
.takeIf { it.exists() }
|
||||
?.let {
|
||||
try {
|
||||
encryptedFile.openFileInput().use { fis ->
|
||||
ObjectInputStream(fis).use { ois ->
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
ois.readObject() as? ArrayList<NotifiableEvent>
|
||||
}
|
||||
}.also {
|
||||
Timber.tag(loggerTag.value).d("Deserializing ${it?.size} NotifiableEvent(s)")
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Timber.tag(loggerTag.value).e(e, "## Failed to load cached notification info")
|
||||
null
|
||||
}
|
||||
}
|
||||
return factory(rawEvents.orEmpty())
|
||||
}
|
||||
|
||||
override fun persistEvents(queuedEvents: NotificationEventQueue) {
|
||||
Timber.tag(loggerTag.value).d("Serializing ${queuedEvents.rawEvents().size} NotifiableEvent(s)")
|
||||
// Always delete file before writing, or encryptedFile.openFileOutput() will throw
|
||||
file.safeDelete()
|
||||
if (queuedEvents.isEmpty()) return
|
||||
try {
|
||||
encryptedFile.openFileOutput().use { fos ->
|
||||
ObjectOutputStream(fos).use { oos ->
|
||||
oos.writeObject(queuedEvents.rawEvents())
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Timber.tag(loggerTag.value).e(e, "## Failed to save cached notification info")
|
||||
}
|
||||
}
|
||||
|
||||
private fun deleteLegacyFileIfAny() {
|
||||
tryOrNull {
|
||||
File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME_LEGACY).delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 New Vector Ltd
|
||||
* Copyright (c) 2023 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.
|
||||
@@ -16,76 +16,9 @@
|
||||
|
||||
package io.element.android.libraries.push.impl.notifications
|
||||
|
||||
import android.content.Context
|
||||
import io.element.android.libraries.androidutils.file.EncryptedFileFactory
|
||||
import io.element.android.libraries.androidutils.file.safeDelete
|
||||
import io.element.android.libraries.core.data.tryOrNull
|
||||
import io.element.android.libraries.core.log.logger.LoggerTag
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val ROOMS_NOTIFICATIONS_FILE_NAME_LEGACY = "im.vector.notifications.cache"
|
||||
private const val FILE_NAME = "notifications.bin"
|
||||
|
||||
private val loggerTag = LoggerTag("NotificationEventPersistence", LoggerTag.NotificationLoggerTag)
|
||||
|
||||
class NotificationEventPersistence @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
) {
|
||||
private val file by lazy {
|
||||
deleteLegacyFileIfAny()
|
||||
context.getDatabasePath(FILE_NAME)
|
||||
}
|
||||
|
||||
private val encryptedFile by lazy {
|
||||
EncryptedFileFactory(context).create(file)
|
||||
}
|
||||
|
||||
fun loadEvents(factory: (List<NotifiableEvent>) -> NotificationEventQueue): NotificationEventQueue {
|
||||
val rawEvents: ArrayList<NotifiableEvent>? = file
|
||||
.takeIf { it.exists() }
|
||||
?.let {
|
||||
try {
|
||||
encryptedFile.openFileInput().use { fis ->
|
||||
ObjectInputStream(fis).use { ois ->
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
ois.readObject() as? ArrayList<NotifiableEvent>
|
||||
}
|
||||
}.also {
|
||||
Timber.tag(loggerTag.value).d("Deserializing ${it?.size} NotifiableEvent(s)")
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Timber.tag(loggerTag.value).e(e, "## Failed to load cached notification info")
|
||||
null
|
||||
}
|
||||
}
|
||||
return factory(rawEvents.orEmpty())
|
||||
}
|
||||
|
||||
fun persistEvents(queuedEvents: NotificationEventQueue) {
|
||||
Timber.tag(loggerTag.value).d("Serializing ${queuedEvents.rawEvents().size} NotifiableEvent(s)")
|
||||
// Always delete file before writing, or encryptedFile.openFileOutput() will throw
|
||||
file.safeDelete()
|
||||
if (queuedEvents.isEmpty()) return
|
||||
try {
|
||||
encryptedFile.openFileOutput().use { fos ->
|
||||
ObjectOutputStream(fos).use { oos ->
|
||||
oos.writeObject(queuedEvents.rawEvents())
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Timber.tag(loggerTag.value).e(e, "## Failed to save cached notification info")
|
||||
}
|
||||
}
|
||||
|
||||
private fun deleteLegacyFileIfAny() {
|
||||
tryOrNull {
|
||||
File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME_LEGACY).delete()
|
||||
}
|
||||
}
|
||||
interface NotificationEventPersistence {
|
||||
fun loadEvents(factory: (List<NotifiableEvent>) -> NotificationEventQueue): NotificationEventQueue
|
||||
fun persistEvents(queuedEvents: NotificationEventQueue)
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class NotificationEventPersistenceTest {
|
||||
class DefaultNotificationEventPersistenceTest {
|
||||
@Test
|
||||
fun `loadEvents should return empty NotificationEventQueue`() {
|
||||
val sut = createNotificationEventPersistence()
|
||||
val sut = createDefaultNotificationEventPersistence()
|
||||
val result = sut.loadEvents(
|
||||
factory = { rawEvents ->
|
||||
NotificationEventQueue(rawEvents.toMutableList(), seenEventIds = CircularCache.create(cacheSize = 25))
|
||||
@@ -39,7 +39,7 @@ class NotificationEventPersistenceTest {
|
||||
|
||||
@Test
|
||||
fun `after persisting NotificationEventQueue, loadEvents should return non-empty NotificationEventQueue`() {
|
||||
val sut = createNotificationEventPersistence()
|
||||
val sut = createDefaultNotificationEventPersistence()
|
||||
val notificationEventQueue = NotificationEventQueue(mutableListOf(), seenEventIds = CircularCache.create(cacheSize = 25))
|
||||
// First persist an empty queue
|
||||
sut.persistEvents(notificationEventQueue)
|
||||
@@ -57,8 +57,8 @@ class NotificationEventPersistenceTest {
|
||||
// assertThat(result.isEmpty()).isFalse()
|
||||
}
|
||||
|
||||
private fun createNotificationEventPersistence(): NotificationEventPersistence {
|
||||
private fun createDefaultNotificationEventPersistence(): DefaultNotificationEventPersistence {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
return NotificationEventPersistence(context)
|
||||
return DefaultNotificationEventPersistence(context)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user