forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1813085 - If a message notification is being displayed, do not cr…
…eate another notification with the same message.
- Loading branch information
1 parent
8e98aff
commit 84c406b
Showing
8 changed files
with
200 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* 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.utils | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import android.provider.Settings | ||
import androidx.annotation.RequiresApi | ||
import java.io.File | ||
|
||
/** | ||
* Provides access to system properties. | ||
*/ | ||
interface BootUtils { | ||
|
||
/** | ||
* Gets the device boot count. | ||
* | ||
* **Only for Android versions N(24) and above.** | ||
*/ | ||
@RequiresApi(Build.VERSION_CODES.N) | ||
fun getDeviceBootCount(context: Context): String | ||
|
||
val deviceBootId: String? | ||
|
||
val bootIdFileExists: Boolean | ||
|
||
companion object { | ||
/** | ||
* @return either the boot count or a boot id depending on the device Android version. | ||
*/ | ||
fun getBootIdentifier(context: Context, bootUtils: BootUtils = BootUtilsImpl()): String { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
bootUtils.getDeviceBootCount(context) | ||
} else { | ||
return if (bootUtils.bootIdFileExists) { | ||
bootUtils.deviceBootId ?: NO_BOOT_IDENTIFIER | ||
} else { | ||
NO_BOOT_IDENTIFIER | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Implementation of [BootUtils]. | ||
*/ | ||
class BootUtilsImpl : BootUtils { | ||
private val bootIdFile by lazy { File("/proc/sys/kernel/random/boot_id") } | ||
|
||
@RequiresApi(Build.VERSION_CODES.N) | ||
override fun getDeviceBootCount(context: Context): String = | ||
Settings.Global.getString(context.contentResolver, Settings.Global.BOOT_COUNT) | ||
|
||
override val deviceBootId: String? by lazy { bootIdFile.readLines().firstOrNull()?.trim() } | ||
|
||
override val bootIdFileExists: Boolean by lazy { bootIdFile.exists() } | ||
} | ||
|
||
private const val NO_BOOT_IDENTIFIER = "no boot identifier available" |
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
79 changes: 79 additions & 0 deletions
79
app/src/test/java/org/mozilla/fenix/utils/BootUtilsTest.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,79 @@ | ||
package org.mozilla.fenix.utils | ||
|
||
import android.os.Build | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner | ||
import org.mozilla.fenix.utils.BootUtils.Companion.getBootIdentifier | ||
import org.robolectric.annotation.Config | ||
|
||
private const val NO_BOOT_IDENTIFIER = "no boot identifier available" | ||
|
||
@RunWith(FenixRobolectricTestRunner::class) | ||
class BootUtilsTest { | ||
|
||
private lateinit var bootUtils: BootUtils | ||
|
||
@Before | ||
fun setUp() { | ||
bootUtils = mockk(relaxed = true) | ||
} | ||
|
||
@Test | ||
@Config(sdk = [Build.VERSION_CODES.M]) | ||
fun `WHEN no boot id file & Android version is less than N(24) THEN getBootIdentifier returns NO_BOOT_IDENTIFIER`() { | ||
every { bootUtils.bootIdFileExists }.returns(false) | ||
|
||
assertEquals(NO_BOOT_IDENTIFIER, getBootIdentifier(testContext, bootUtils)) | ||
} | ||
|
||
@Test | ||
@Config(sdk = [Build.VERSION_CODES.M]) | ||
fun `WHEN boot id file returns null & Android version is less than N(24) THEN getBootIdentifier returns NO_BOOT_IDENTIFIER`() { | ||
every { bootUtils.bootIdFileExists }.returns(true) | ||
every { bootUtils.deviceBootId }.returns(null) | ||
|
||
assertEquals(NO_BOOT_IDENTIFIER, getBootIdentifier(testContext, bootUtils)) | ||
} | ||
|
||
@Test | ||
@Config(sdk = [Build.VERSION_CODES.M]) | ||
fun `WHEN boot id file has text & Android version is less than N(24) THEN getBootIdentifier returns the boot id`() { | ||
every { bootUtils.bootIdFileExists }.returns(true) | ||
val bootId = "test" | ||
every { bootUtils.deviceBootId }.returns(bootId) | ||
|
||
assertEquals(bootId, getBootIdentifier(testContext, bootUtils)) | ||
} | ||
|
||
@Test | ||
@Config(sdk = [Build.VERSION_CODES.M]) | ||
fun `WHEN boot id file has text with whitespace & Android version is less than N(24) THEN getBootIdentifier returns the trimmed boot id`() { | ||
every { bootUtils.bootIdFileExists }.returns(true) | ||
val bootId = " test " | ||
every { bootUtils.deviceBootId }.returns(bootId) | ||
|
||
assertEquals(bootId, getBootIdentifier(testContext, bootUtils)) | ||
} | ||
|
||
@Test | ||
@Config(sdk = [Build.VERSION_CODES.N]) | ||
fun `WHEN Android version is N(24) THEN getBootIdentifier returns the boot count`() { | ||
val bootCount = "9" | ||
every { bootUtils.getDeviceBootCount(any()) }.returns(bootCount) | ||
assertEquals(bootCount, getBootIdentifier(testContext, bootUtils)) | ||
} | ||
|
||
@Test | ||
@Config(sdk = [Build.VERSION_CODES.O]) | ||
fun `WHEN Android version is more than N(24) THEN getBootIdentifier returns the boot count`() { | ||
val bootCount = "9" | ||
every { bootUtils.getDeviceBootCount(any()) }.returns(bootCount) | ||
assertEquals(bootCount, getBootIdentifier(testContext, bootUtils)) | ||
} | ||
} |