Skip to content

Commit 40e6bf5

Browse files
committed
Inject NotificationManagerCompat where possible
1 parent d098c50 commit 40e6bf5

File tree

5 files changed

+7
-10
lines changed

5 files changed

+7
-10
lines changed

features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/SystemNotificationsEnabledProvider.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
package io.element.android.features.preferences.impl.notifications
1818

19-
import android.content.Context
2019
import androidx.core.app.NotificationManagerCompat
2120
import com.squareup.anvil.annotations.ContributesBinding
2221
import io.element.android.libraries.di.AppScope
23-
import io.element.android.libraries.di.ApplicationContext
2422
import io.element.android.libraries.di.SingleIn
2523
import javax.inject.Inject
2624

@@ -31,9 +29,9 @@ interface SystemNotificationsEnabledProvider {
3129
@SingleIn(AppScope::class)
3230
@ContributesBinding(AppScope::class, boundType = SystemNotificationsEnabledProvider::class)
3331
class DefaultSystemNotificationsEnabledProvider @Inject constructor(
34-
@ApplicationContext private val context: Context,
32+
private val notificationManager: NotificationManagerCompat,
3533
) : SystemNotificationsEnabledProvider {
3634
override fun notificationsEnabled(): Boolean {
37-
return NotificationManagerCompat.from(context).areNotificationsEnabled()
35+
return notificationManager.areNotificationsEnabled()
3836
}
3937
}

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationDisplayer.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ interface NotificationDisplayer {
3838
@ContributesBinding(AppScope::class)
3939
class DefaultNotificationDisplayer @Inject constructor(
4040
@ApplicationContext private val context: Context,
41+
private val notificationManager: NotificationManagerCompat
4142
) : NotificationDisplayer {
42-
private val notificationManager = NotificationManagerCompat.from(context)
43-
4443
override fun showNotificationMessage(tag: String?, id: Int, notification: Notification): Boolean {
4544
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
4645
Timber.w("Not allowed to notify.")

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/channels/NotificationChannels.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ import javax.inject.Inject
3636
@SingleIn(AppScope::class)
3737
class NotificationChannels @Inject constructor(
3838
@ApplicationContext private val context: Context,
39+
private val notificationManager: NotificationManagerCompat,
3940
private val stringProvider: StringProvider,
4041
) {
41-
private val notificationManager = NotificationManagerCompat.from(context)
42-
4342
init {
4443
createNotificationChannels()
4544
}

libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/DefaultNotificationDrawerManagerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class DefaultNotificationDrawerManagerTest {
199199
notificationManager = notificationManager,
200200
notificationRenderer = NotificationRenderer(
201201
notificationIdProvider = NotificationIdProvider(),
202-
notificationDisplayer = DefaultNotificationDisplayer(context),
202+
notificationDisplayer = DefaultNotificationDisplayer(context, NotificationManagerCompat.from(context)),
203203
notificationDataFactory = DefaultNotificationDataFactory(
204204
notificationCreator = FakeNotificationCreator(),
205205
roomGroupMessageCreator = roomGroupMessageCreator,

libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/factories/DefaultNotificationCreatorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.app.Notification
2020
import android.content.Context
2121
import android.os.Build
2222
import androidx.core.app.NotificationCompat
23+
import androidx.core.app.NotificationManagerCompat
2324
import com.google.common.truth.Truth.assertThat
2425
import io.element.android.libraries.core.meta.BuildMeta
2526
import io.element.android.libraries.matrix.test.AN_EVENT_ID
@@ -328,5 +329,5 @@ fun createNotificationCreator(
328329

329330
fun createNotificationChannels(): NotificationChannels {
330331
val context = RuntimeEnvironment.getApplication()
331-
return NotificationChannels(context, FakeStringProvider(""))
332+
return NotificationChannels(context, NotificationManagerCompat.from(context), FakeStringProvider(""))
332333
}

0 commit comments

Comments
 (0)