-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notifications: simplify the flow by removing persistence #2924
Changes from 10 commits
d9b4335
279ceb3
d6e0d1e
dd5dcbe
b552e53
bd7a5c1
b7e96d3
b539dd0
f5761c8
42a4297
d2d597e
d098c50
40e6bf5
f925b45
ca7d98b
d7e38cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Simplify notifications by removing the custom persistence layer. | ||
|
||
Bump minSdk to 24 (Android 7). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2024 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.features.migration.impl.migrations | ||
|
||
import android.content.Context | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import io.element.android.libraries.di.AppScope | ||
import io.element.android.libraries.di.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Remove notifications.bin file, used to store notification data locally. | ||
*/ | ||
@ContributesMultibinding(AppScope::class) | ||
class AppMigration04 @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
) : AppMigration { | ||
companion object { | ||
internal const val NOTIFICATION_FILE_NAME = "notifications.bin" | ||
} | ||
override val order: Int = 4 | ||
|
||
override suspend fun migrate() { | ||
runCatching { context.getDatabasePath(NOTIFICATION_FILE_NAME).delete() } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2024 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.features.migration.impl.migrations | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class AppMigration04Test { | ||
@Test | ||
fun `test migration`() = runTest { | ||
val context = InstrumentationRegistry.getInstrumentation().context | ||
|
||
// Create fake temporary file at the path to be deleted | ||
val file = context.getDatabasePath(AppMigration04.NOTIFICATION_FILE_NAME) | ||
file.parentFile?.mkdirs() | ||
file.createNewFile() | ||
|
||
val migration = AppMigration04(context) | ||
|
||
migration.migrate() | ||
|
||
// Check that the file has been deleted | ||
assertThat(file.exists()).isFalse() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to ensure that the test behaves as expected, I would add
before invoking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,16 +59,16 @@ class AndroidMediaPreProcessorTest { | |
assertThat(data.file.path).endsWith("image.png") | ||
val info = data as MediaUploadInfo.Image | ||
// Computing thumbnailFile is failing with Robolectric | ||
assertThat(info.thumbnailFile).isNull() | ||
assertThat(info.thumbnailFile).isNotNull() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe remove the comment above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
assertThat(info.imageInfo).isEqualTo( | ||
ImageInfo( | ||
height = 1_178, | ||
width = 1_818, | ||
mimetype = MimeTypes.Png, | ||
size = 114_867, | ||
thumbnailInfo = null, | ||
ThumbnailInfo(height = 294, width = 454, mimetype = "image/jpeg", size = 4567), | ||
thumbnailSource = null, | ||
blurhash = null, | ||
blurhash = "K13]7q%zWC00R4of%\$baad" | ||
) | ||
) | ||
assertThat(file.exists()).isTrue() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2024 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.di | ||
|
||
import android.content.Context | ||
import androidx.core.app.NotificationManagerCompat | ||
import com.squareup.anvil.annotations.ContributesTo | ||
import dagger.Module | ||
import dagger.Provides | ||
import io.element.android.libraries.di.AppScope | ||
import io.element.android.libraries.di.ApplicationContext | ||
|
||
@Module | ||
@ContributesTo(AppScope::class) | ||
object PushModule { | ||
@Provides | ||
fun provideNotificationCompatManager(@ApplicationContext context: Context): NotificationManagerCompat { | ||
return NotificationManagerCompat.from(context) | ||
Check warning on line 32 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushModule.kt Codecov / codecov/patchlibraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushModule.kt#L32
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe Inject There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||
/* | ||||||
* Copyright (c) 2024 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.service.notification.StatusBarNotification | ||||||
import androidx.core.app.NotificationManagerCompat | ||||||
import com.squareup.anvil.annotations.ContributesBinding | ||||||
import io.element.android.libraries.di.AppScope | ||||||
import io.element.android.libraries.matrix.api.core.RoomId | ||||||
import io.element.android.libraries.matrix.api.core.SessionId | ||||||
import javax.inject.Inject | ||||||
|
||||||
interface ActiveNotificationsProvider { | ||||||
fun getAllNotifications(): List<StatusBarNotification> | ||||||
fun getMessageNotificationsForRoom(sessionId: SessionId, roomId: RoomId): List<StatusBarNotification> | ||||||
fun getNotificationsForSession(sessionId: SessionId): List<StatusBarNotification> | ||||||
fun getMembershipNotificationForSession(sessionId: SessionId): List<StatusBarNotification> | ||||||
fun getMembershipNotificationForRoom(sessionId: SessionId, roomId: RoomId): List<StatusBarNotification> | ||||||
fun getSummaryNotification(sessionId: SessionId): StatusBarNotification? | ||||||
fun count(): Int | ||||||
} | ||||||
|
||||||
@ContributesBinding(AppScope::class) | ||||||
class DefaultActiveNotificationsProvider @Inject constructor( | ||||||
private val notificationManager: NotificationManagerCompat, | ||||||
private val notificationIdProvider: NotificationIdProvider, | ||||||
) : ActiveNotificationsProvider { | ||||||
override fun getAllNotifications(): List<StatusBarNotification> { | ||||||
return notificationManager.activeNotifications | ||||||
} | ||||||
|
||||||
override fun getNotificationsForSession(sessionId: SessionId): List<StatusBarNotification> { | ||||||
return notificationManager.activeNotifications.filter { it.groupKey == sessionId.value } | ||||||
jmartinesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
override fun getMembershipNotificationForSession(sessionId: SessionId): List<StatusBarNotification> { | ||||||
val notificationId = notificationIdProvider.getRoomInvitationNotificationId(sessionId) | ||||||
return getNotificationsForSession(sessionId).filter { it.id == notificationId } | ||||||
} | ||||||
|
||||||
override fun getMessageNotificationsForRoom(sessionId: SessionId, roomId: RoomId): List<StatusBarNotification> { | ||||||
val notificationId = notificationIdProvider.getRoomMessagesNotificationId(sessionId) | ||||||
return notificationManager.activeNotifications.filter { it.id == notificationId && it.tag == roomId.value } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, I would use
Suggested change
But the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
|
||||||
override fun getMembershipNotificationForRoom(sessionId: SessionId, roomId: RoomId): List<StatusBarNotification> { | ||||||
val notificationId = notificationIdProvider.getRoomInvitationNotificationId(sessionId) | ||||||
return getNotificationsForSession(sessionId).filter { it.id == notificationId && it.tag == roomId.value } | ||||||
} | ||||||
|
||||||
override fun getSummaryNotification(sessionId: SessionId): StatusBarNotification? { | ||||||
val summaryId = notificationIdProvider.getSummaryNotificationId(sessionId) | ||||||
return notificationManager.activeNotifications.find { it.id == summaryId } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
|
||||||
override fun count(): Int { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
return getAllNotifications().size | ||||||
Check warning on line 71 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/ActiveNotificationsProvider.kt Codecov / codecov/patchlibraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/ActiveNotificationsProvider.kt#L71
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, thanks!