-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notifications: simplify the flow by removing persistence (#2924)
* Notifications: simplify the flow by removing persistence. * Bump of minSdk to `24` (Android 7). * Add migration to remove `notification.bin` file
- Loading branch information
1 parent
17678ad
commit 04e5031
Showing
62 changed files
with
2,017 additions
and
2,607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...l/src/main/kotlin/io/element/android/features/migration/impl/migrations/AppMigration04.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...c/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration04Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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() | ||
assertThat(file.exists()).isTrue() | ||
|
||
val migration = AppMigration04(context) | ||
|
||
migration.migrate() | ||
|
||
// Check that the file has been deleted | ||
assertThat(file.exists()).isFalse() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...otlin/io/element/android/libraries/push/impl/notifications/ActiveNotificationsProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(sessionId: SessionId): 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 } | ||
} | ||
|
||
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 getNotificationsForSession(sessionId).filter { it.id == notificationId && it.tag == roomId.value } | ||
} | ||
|
||
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 getNotificationsForSession(sessionId).find { it.id == summaryId } | ||
} | ||
|
||
override fun count(sessionId: SessionId): Int { | ||
return getNotificationsForSession(sessionId).size | ||
} | ||
} |
Oops, something went wrong.