From ca721a5e99775db70f0939f88a5428b8f771ead6 Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Thu, 21 Jun 2018 15:51:31 -0700 Subject: [PATCH] For #2741 - Disable telemetry for a distribution --- .../org/mozilla/focus/FocusApplication.kt | 3 ++ .../org/mozilla/focus/distribution/Device.kt | 37 +++++++++++++++ .../focus/distribution/DeviceIdentifier.kt | 46 +++++++++++++++++++ .../mozilla/focus/distribution/DeviceTest.kt | 41 +++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 app/src/main/java/org/mozilla/focus/distribution/Device.kt create mode 100644 app/src/main/java/org/mozilla/focus/distribution/DeviceIdentifier.kt create mode 100644 app/src/test/java/org/mozilla/focus/distribution/DeviceTest.kt diff --git a/app/src/main/java/org/mozilla/focus/FocusApplication.kt b/app/src/main/java/org/mozilla/focus/FocusApplication.kt index 1c43b64b507..2f8add50d07 100644 --- a/app/src/main/java/org/mozilla/focus/FocusApplication.kt +++ b/app/src/main/java/org/mozilla/focus/FocusApplication.kt @@ -9,6 +9,7 @@ import android.os.StrictMode import android.preference.PreferenceManager import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.launch +import org.mozilla.focus.distribution.BrandAndDeviceDeviceIdentifier import org.mozilla.focus.locale.LocaleAwareApplication import org.mozilla.focus.session.NotificationSessionObserver import org.mozilla.focus.session.SessionManager @@ -28,6 +29,8 @@ class FocusApplication : LocaleAwareApplication() { PreferenceManager.setDefaultValues(this, R.xml.settings, false) + BrandAndDeviceDeviceIdentifier().identify()?.onApplicationLaunch(this) + enableStrictMode() Components.searchEngineManager.apply { diff --git a/app/src/main/java/org/mozilla/focus/distribution/Device.kt b/app/src/main/java/org/mozilla/focus/distribution/Device.kt new file mode 100644 index 00000000000..180620d9b1e --- /dev/null +++ b/app/src/main/java/org/mozilla/focus/distribution/Device.kt @@ -0,0 +1,37 @@ +/* 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.focus.distribution + +import android.content.Context +import android.preference.PreferenceManager +import org.mozilla.focus.R + +sealed class Device { + class KEY2 : Device() { + override fun onApplicationLaunch(context: Context) { + // Disable Telemetry on Launch + val resources = context.resources + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + + preferences.edit() + .putBoolean(resources.getString(R.string.pref_key_telemetry), false) + .apply() + } + + companion object { + val BRAND = "blackberry" + val DEVICE = "bbf100" + } + } + + abstract fun onApplicationLaunch(context: Context) + + companion object { + fun create(brand: String, device: String): Device? = when { + (brand == KEY2.BRAND && device == KEY2.DEVICE) -> KEY2() + else -> null + } + } +} diff --git a/app/src/main/java/org/mozilla/focus/distribution/DeviceIdentifier.kt b/app/src/main/java/org/mozilla/focus/distribution/DeviceIdentifier.kt new file mode 100644 index 00000000000..535c4c63113 --- /dev/null +++ b/app/src/main/java/org/mozilla/focus/distribution/DeviceIdentifier.kt @@ -0,0 +1,46 @@ +/* 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.focus.distribution + +import java.io.BufferedReader +import java.io.InputStreamReader + +interface DeviceIdentifier { + fun identify(): Device? +} + +@SuppressWarnings("TooGenericExceptionCaught") +class BrandAndDeviceDeviceIdentifier : DeviceIdentifier { + companion object { + private val BRAND_PROP = "ro.vendor.product.brand" + private val DEVICE_PROP = "ro.vendor.product.device" + } + + override fun identify(): Device? { + val brand: String + val device: String + + try { + brand = get(BRAND_PROP) + device = get(DEVICE_PROP) + } catch (e: Exception) { + return null + } + + return Device.create(brand, device) + } + + private fun get(property: String): String { + val value: String? + val getPropProcess = Runtime.getRuntime().exec("getprop $property") + val osRes = BufferedReader(InputStreamReader(getPropProcess.inputStream)) + + value = osRes.readLine() + + osRes.close() + + return value + } +} diff --git a/app/src/test/java/org/mozilla/focus/distribution/DeviceTest.kt b/app/src/test/java/org/mozilla/focus/distribution/DeviceTest.kt new file mode 100644 index 00000000000..4584e6c39db --- /dev/null +++ b/app/src/test/java/org/mozilla/focus/distribution/DeviceTest.kt @@ -0,0 +1,41 @@ +/* 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.focus.distribution + +import android.preference.PreferenceManager +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.junit.Before +import org.mozilla.focus.R +import org.robolectric.RuntimeEnvironment + +@RunWith(RobolectricTestRunner::class) +class DeviceTest { + @Before + fun setUp() { + // Reset all saved and cached values before running a test + PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application) + .edit() + .clear() + .apply() + } + + @Test + fun testKEY2WillDisableTelemetry() { + val brand = "blackberry" + val device = "bbf100" + Device.create(brand, device) + ?.onApplicationLaunch(RuntimeEnvironment.application) + + val resources = RuntimeEnvironment.application.resources + val context = RuntimeEnvironment.application + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + + val isTelemetryEnabled = preferences.getBoolean(resources.getString(R.string.pref_key_telemetry), true) + Assert.assertFalse(isTelemetryEnabled) + } +} \ No newline at end of file