mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-03 23:15:31 +00:00
Bug 1838613 - Migrate NotificationManagerCompat ext to AC
This commit is contained in:
parent
b0ec138066
commit
0582611fde
@ -48,6 +48,8 @@ import mozilla.components.service.fxa.manager.SyncEnginesStorage
|
||||
import mozilla.components.service.glean.Glean
|
||||
import mozilla.components.service.glean.config.Configuration
|
||||
import mozilla.components.service.glean.net.ConceptFetchHttpUploader
|
||||
import mozilla.components.support.base.ext.areNotificationsEnabledSafe
|
||||
import mozilla.components.support.base.ext.isNotificationChannelEnabled
|
||||
import mozilla.components.support.base.facts.register
|
||||
import mozilla.components.support.base.log.Log
|
||||
import mozilla.components.support.base.log.logger.Logger
|
||||
@ -78,12 +80,10 @@ import org.mozilla.fenix.components.appstate.AppAction
|
||||
import org.mozilla.fenix.components.metrics.MetricServiceType
|
||||
import org.mozilla.fenix.components.metrics.MozillaProductDetector
|
||||
import org.mozilla.fenix.experiments.maybeFetchExperiments
|
||||
import org.mozilla.fenix.ext.areNotificationsEnabledSafe
|
||||
import org.mozilla.fenix.ext.containsQueryParameters
|
||||
import org.mozilla.fenix.ext.getCustomGleanServerUrlIfAvailable
|
||||
import org.mozilla.fenix.ext.isCustomEngine
|
||||
import org.mozilla.fenix.ext.isKnownSearchDomain
|
||||
import org.mozilla.fenix.ext.isNotificationChannelEnabled
|
||||
import org.mozilla.fenix.ext.setCustomEndpointIfAvailable
|
||||
import org.mozilla.fenix.ext.settings
|
||||
import org.mozilla.fenix.lifecycle.StoreLifecycleObserver
|
||||
|
@ -61,6 +61,7 @@ import mozilla.components.feature.media.ext.findActiveMediaTab
|
||||
import mozilla.components.feature.privatemode.notification.PrivateNotificationFeature
|
||||
import mozilla.components.feature.search.BrowserStoreSearchAdapter
|
||||
import mozilla.components.service.fxa.sync.SyncReason
|
||||
import mozilla.components.support.base.ext.areNotificationsEnabledSafe
|
||||
import mozilla.components.support.base.feature.ActivityResultHandler
|
||||
import mozilla.components.support.base.feature.UserInteractionHandler
|
||||
import mozilla.components.support.base.log.logger.Logger
|
||||
@ -94,7 +95,6 @@ import org.mozilla.fenix.databinding.ActivityHomeBinding
|
||||
import org.mozilla.fenix.exceptions.trackingprotection.TrackingProtectionExceptionsFragmentDirections
|
||||
import org.mozilla.fenix.experiments.ResearchSurfaceDialogFragment
|
||||
import org.mozilla.fenix.ext.alreadyOnDestination
|
||||
import org.mozilla.fenix.ext.areNotificationsEnabledSafe
|
||||
import org.mozilla.fenix.ext.breadcrumb
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.ext.hasTopDestination
|
||||
|
@ -1,60 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.ext
|
||||
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationChannelCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
|
||||
/**
|
||||
* Returns whether notifications are enabled, catches any exception that was thrown from
|
||||
* [NotificationManagerCompat.areNotificationsEnabled] and returns false.
|
||||
*/
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
fun NotificationManagerCompat.areNotificationsEnabledSafe(): Boolean {
|
||||
return try {
|
||||
areNotificationsEnabled()
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the channel does not exist or is null, this returns false.
|
||||
* If the channel exists with importance more than [NotificationManagerCompat.IMPORTANCE_NONE] and
|
||||
* notifications are enabled for the app, this returns true.
|
||||
* On <= SDK 26, this checks if notifications are enabled for the app.
|
||||
*
|
||||
* @param channelId the id of the notification channel to check.
|
||||
* @return true if the channel is enabled, false otherwise.
|
||||
*/
|
||||
fun NotificationManagerCompat.isNotificationChannelEnabled(channelId: String): Boolean {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = getNotificationChannelSafe(channelId)
|
||||
if (channel == null) {
|
||||
false
|
||||
} else {
|
||||
areNotificationsEnabledSafe() && channel.importance != NotificationManagerCompat.IMPORTANCE_NONE
|
||||
}
|
||||
} else {
|
||||
areNotificationsEnabledSafe()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the notification channel with the given [channelId], or null if the channel does not
|
||||
* exist, catches any exception that was thrown by
|
||||
* [NotificationManagerCompat.getNotificationChannelCompat] and returns null.
|
||||
*
|
||||
* @param channelId the id of the notification channel to check.
|
||||
*/
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
private fun NotificationManagerCompat.getNotificationChannelSafe(channelId: String): NotificationChannelCompat? {
|
||||
return try {
|
||||
getNotificationChannelCompat(channelId)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ package org.mozilla.fenix.messaging
|
||||
import android.content.Context
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import mozilla.components.service.nimbus.messaging.JexlAttributeProvider
|
||||
import mozilla.components.support.base.ext.areNotificationsEnabledSafe
|
||||
import mozilla.components.support.utils.BrowsersCache
|
||||
import org.json.JSONObject
|
||||
import org.mozilla.fenix.components.metrics.UTMParams.Companion.UTM_CAMPAIGN
|
||||
@ -14,7 +15,6 @@ import org.mozilla.fenix.components.metrics.UTMParams.Companion.UTM_CONTENT
|
||||
import org.mozilla.fenix.components.metrics.UTMParams.Companion.UTM_MEDIUM
|
||||
import org.mozilla.fenix.components.metrics.UTMParams.Companion.UTM_SOURCE
|
||||
import org.mozilla.fenix.components.metrics.UTMParams.Companion.UTM_TERM
|
||||
import org.mozilla.fenix.ext.areNotificationsEnabledSafe
|
||||
import org.mozilla.fenix.ext.settings
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
|
@ -19,9 +19,9 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import mozilla.components.support.base.ext.areNotificationsEnabledSafe
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.accounts.FenixFxAEntryPoint
|
||||
import org.mozilla.fenix.ext.areNotificationsEnabledSafe
|
||||
import org.mozilla.fenix.ext.hideToolbar
|
||||
import org.mozilla.fenix.ext.nav
|
||||
import org.mozilla.fenix.ext.openSetDefaultBrowserOption
|
||||
|
@ -1,119 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.ext
|
||||
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationChannelCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class NotificationManagerCompatTest {
|
||||
|
||||
private lateinit var tested: NotificationManagerCompat
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
tested = mockk(relaxed = true)
|
||||
|
||||
mockkStatic("org.mozilla.fenix.ext.NotificationManagerCompatKt")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN areNotificationsEnabled throws an exception THEN areNotificationsEnabledSafe returns false`() {
|
||||
every { tested.areNotificationsEnabled() } throws RuntimeException()
|
||||
|
||||
assertFalse(tested.areNotificationsEnabledSafe())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN areNotificationsEnabled returns false THEN areNotificationsEnabledSafe returns false`() {
|
||||
every { tested.areNotificationsEnabled() } returns false
|
||||
|
||||
assertFalse(tested.areNotificationsEnabledSafe())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN areNotificationsEnabled returns true THEN areNotificationsEnabledSafe returns true`() {
|
||||
every { tested.areNotificationsEnabled() } returns true
|
||||
|
||||
assertTrue(tested.areNotificationsEnabledSafe())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN getNotificationChannelCompat returns a channel with IMPORTANCE_DEFAULT and areNotificationsEnabled returns true THEN isNotificationChannelEnabled returns true`() {
|
||||
val testChannel = "test-channel"
|
||||
val notificationChannelCompat =
|
||||
NotificationChannelCompat.Builder(
|
||||
testChannel,
|
||||
NotificationManagerCompat.IMPORTANCE_DEFAULT,
|
||||
).build()
|
||||
|
||||
every { tested.getNotificationChannelCompat(testChannel) } returns notificationChannelCompat
|
||||
every { tested.areNotificationsEnabled() } returns true
|
||||
|
||||
assertTrue(tested.isNotificationChannelEnabled(testChannel))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN getNotificationChannelCompat returns a channel with IMPORTANCE_NONE and areNotificationsEnabled returns true THEN isNotificationChannelEnabled returns false`() {
|
||||
val testChannel = "test-channel"
|
||||
val notificationChannelCompat =
|
||||
NotificationChannelCompat.Builder(
|
||||
testChannel,
|
||||
NotificationManagerCompat.IMPORTANCE_NONE,
|
||||
).build()
|
||||
|
||||
every { tested.getNotificationChannelCompat(testChannel) } returns notificationChannelCompat
|
||||
every { tested.areNotificationsEnabled() } returns true
|
||||
|
||||
assertFalse(tested.isNotificationChannelEnabled(testChannel))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN getNotificationChannelCompat returns a channel and areNotificationsEnabled returns false THEN isNotificationChannelEnabled returns false`() {
|
||||
val testChannel = "test-channel"
|
||||
val notificationChannelCompat =
|
||||
NotificationChannelCompat.Builder(
|
||||
testChannel,
|
||||
NotificationManagerCompat.IMPORTANCE_DEFAULT,
|
||||
).build()
|
||||
|
||||
every { tested.getNotificationChannelCompat(testChannel) } returns notificationChannelCompat
|
||||
every { tested.areNotificationsEnabled() } returns false
|
||||
|
||||
assertFalse(tested.isNotificationChannelEnabled(testChannel))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN getNotificationChannelCompat returns null THEN isNotificationChannelEnabled returns false`() {
|
||||
val testChannel = "test-channel"
|
||||
|
||||
every { tested.getNotificationChannelCompat(testChannel) } returns null
|
||||
every { tested.areNotificationsEnabled() } returns true
|
||||
|
||||
assertFalse(tested.isNotificationChannelEnabled(testChannel))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN sdk less than 26 and areNotificationsEnabled returns true THEN isNotificationChannelEnabled returns true`() {
|
||||
val testChannel = "test-channel"
|
||||
|
||||
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 25)
|
||||
|
||||
every { tested.areNotificationsEnabled() } returns true
|
||||
|
||||
assertTrue(tested.isNotificationChannelEnabled(testChannel))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user