This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #2741 - Disable telemetry for a distribution
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
app/src/main/java/org/mozilla/focus/distribution/Device.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,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 | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/org/mozilla/focus/distribution/DeviceIdentifier.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,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
41
app/src/test/java/org/mozilla/focus/distribution/DeviceTest.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,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) | ||
} | ||
} |