Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
For #2741 - Disable telemetry for a distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
boek committed Jun 22, 2018
1 parent 01ebe6a commit ca721a5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/org/mozilla/focus/FocusApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,8 @@ class FocusApplication : LocaleAwareApplication() {

PreferenceManager.setDefaultValues(this, R.xml.settings, false)

BrandAndDeviceDeviceIdentifier().identify()?.onApplicationLaunch(this)

enableStrictMode()

Components.searchEngineManager.apply {
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/org/mozilla/focus/distribution/Device.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
41 changes: 41 additions & 0 deletions app/src/test/java/org/mozilla/focus/distribution/DeviceTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ca721a5

Please sign in to comment.