Switch to AC implementation for checking device manufacturer.

This commit is contained in:
mcarare 2023-01-04 18:48:06 +02:00 committed by mergify[bot]
parent af51a7e34c
commit b1c40a784b
6 changed files with 6 additions and 99 deletions

View File

@ -67,6 +67,7 @@ import mozilla.components.support.ktx.android.content.share
import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
import mozilla.components.support.utils.ManufacturerCodes
import mozilla.components.support.utils.SafeIntent
import mozilla.components.support.utils.toSafeIntent
import mozilla.components.support.webextensions.WebExtensionPopupFeature
@ -134,7 +135,6 @@ import org.mozilla.fenix.theme.DefaultThemeManager
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.trackingprotection.TrackingProtectionPanelDialogFragmentDirections
import org.mozilla.fenix.utils.BrowsersCache
import org.mozilla.fenix.utils.ManufacturerCodes
import org.mozilla.fenix.utils.Settings
import java.lang.ref.WeakReference

View File

@ -18,9 +18,9 @@ import androidx.annotation.VisibleForTesting.Companion.PRIVATE
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import mozilla.components.support.ktx.android.os.resetAfter
import mozilla.components.support.utils.ManufacturerCodes
import org.mozilla.fenix.Config
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.utils.ManufacturerCodes
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicLong
@ -168,13 +168,13 @@ open class StrictModeManager(
* To add a new manufacturer to the list, log "Build.MANUFACTURER" from the device to get the
* exact name of the manufacturer.
*/
private val strictModeExceptionList = setOf(ManufacturerCodes.HUAWEI, ManufacturerCodes.ONE_PLUS)
private fun isInStrictModeExceptionList() = ManufacturerCodes.isHuawei || ManufacturerCodes.isOnePlus
private fun StrictMode.ThreadPolicy.Builder.penaltyDeathWithIgnores(): StrictMode.ThreadPolicy.Builder {
// This workaround was added before we realized we can ignored based on violation contents
// (see code below). This solution - blanket disabling StrictMode on some manufacturers - isn't
// great so, if we have time, we should consider reimplementing these fixes using the methods below.
if (Build.MANUFACTURER in strictModeExceptionList) {
if (isInStrictModeExceptionList()) {
return this
}

View File

@ -9,7 +9,7 @@ import android.os.StrictMode
import android.os.strictmode.Violation
import androidx.annotation.RequiresApi
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.utils.ManufacturerCodes
import mozilla.components.support.utils.ManufacturerCodes
private const val FCQN_EDM_STORAGE_PROVIDER_BASE = "com.android.server.enterprise.storage.EdmStorageProviderBase"
private const val INSTRUMENTED_HOOKS_CLASS = "com.android.tools.deploy.instrument.InstrumentationHooks"

View File

@ -1,23 +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.utils
import android.os.Build
/**
* A listing of codes returned by [android.os.Build.MANUFACTURER] for different manufacturers.
* While we try to get the casing accurate, it may be good to use .equals(str, ignoreCase = true)
* to do the comparison.
*/
object ManufacturerCodes {
const val HUAWEI: String = "HUAWEI"
private const val LG = "LGE"
const val ONE_PLUS: String = "OnePlus"
private const val SAMSUNG = "samsung"
val isLG get() = Build.MANUFACTURER.equals(LG, ignoreCase = true)
val isSamsung get() = Build.MANUFACTURER.equals(SAMSUNG, ignoreCase = true)
val isHuawei get() = Build.MANUFACTURER.equals(HUAWEI, ignoreCase = true)
}

View File

@ -13,11 +13,11 @@ import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.utils.ManufacturerCodes
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.mozilla.fenix.helpers.StackTraces
import org.mozilla.fenix.utils.ManufacturerCodes
class ThreadPenaltyDeathWithIgnoresListenerTest {

View File

@ -1,70 +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.utils
import android.os.Build
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.lang.reflect.Modifier
class ManufacturerCodesTest {
private val manufacturerField = Build::class.java.getDeclaredField("MANUFACTURER")
private var manufacturer: String?
get() = Build.MANUFACTURER
set(value) { manufacturerField.set(null, value) }
@Before
fun setUp() {
enableManufacturerModifications()
manufacturer = null // reset to default state before test.
assertEquals(null, Build.MANUFACTURER) // sanity check.
}
private fun enableManufacturerModifications() {
// Mocking, which might be simpler, doesn't seem to work so we use reflection.
// Methodology via https://stackoverflow.com/a/3301720/2219998
manufacturerField.isAccessible = true
val modifiers = manufacturerField.javaClass.getDeclaredField("modifiers")
modifiers.isAccessible = true
modifiers.setInt(manufacturerField, manufacturerField.modifiers and Modifier.FINAL.inv())
}
@After
fun tearDown() {
// After this method, Build.MANUFACTURER appears to return to
// static final so we don't need to undo that.
manufacturer = null
assertEquals(null, Build.MANUFACTURER) // sanity check.
}
@Test // To reduce boilerplate, we avoid best practice and put several tests in one.
fun testIsLG() {
manufacturer = "LGE" // expected value for lg devices
assertTrue(ManufacturerCodes.isLG)
manufacturer = "lge" // unexpected value but is still an lg device
assertTrue(ManufacturerCodes.isLG)
manufacturer = "samsung"
assertFalse(ManufacturerCodes.isLG)
}
@Test // To reduce boilerplate, we avoid best practice and put several tests in one.
fun testIsSamsung() {
manufacturer = "samsung" // expected value for samsung devices
assertTrue(ManufacturerCodes.isSamsung)
manufacturer = "SaMsUnG" // unexpected value but is still a samsung device
assertTrue(ManufacturerCodes.isSamsung)
manufacturer = "LGE"
assertFalse(ManufacturerCodes.isSamsung)
}
}