Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc02b526 authored by Yash Garg's avatar Yash Garg 💬
Browse files

Merge branch 'feat/os_tag' into 'main'

feat: use OS tag also for reporting

See merge request !5
parents a88b2670 13839620
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ maven("https://gitlab.e.foundation/api/v4/groups/1391/-/packages/maven")
- Then, we need to add dependency to `build.gradle`:

```groovy
implementation 'foundation.e.lib:telemetry:0.0.5-alpha'
implementation 'foundation.e.lib:telemetry:0.0.6-alpha'
```

- Get the Sentry DSN from the sentry dashboard and add the following to `build.gradle`:
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ plugins {

def versionMajor = 0
def versionMinor = 0
def versionPatch = 5
def versionPatch = 6
def releasePatch = "alpha"

android {
+3 −1
Original line number Diff line number Diff line
@@ -2,4 +2,6 @@ package foundation.e.lib.telemetry

internal object Constants {
    const val SETTINGS_TELEMETRY_FIELD = "e_telemetry"
    const val TAG_E_VERSION = "e_version"
    const val PROPERTY_OS_VERSION = "ro.lineage.version"
}
+70 −13
Original line number Diff line number Diff line
package foundation.e.lib.telemetry

import android.annotation.SuppressLint
import android.app.Application
import android.provider.Settings
import foundation.e.lib.telemetry.Constants.SETTINGS_TELEMETRY_FIELD
import foundation.e.lib.telemetry.Telemetry.isReportingTelemetry
import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.android.core.SentryAndroid
@@ -11,6 +13,9 @@ import io.sentry.android.timber.SentryTimberIntegration
object Telemetry {

    private var identifier: String = ""
    private lateinit var application: Application
    private var enableOsTag: Boolean = true
    private var isTelemetryEnabled: Boolean = false

    /**
     * Call this function in `onCreate()` of custom Application class.
@@ -20,11 +25,18 @@ object Telemetry {
     * @param application: the application context
     */
    @JvmStatic
    fun init(identifier: String, application: Application) {
    fun init(identifier: String, application: Application, enableOsTag: Boolean) {
        this.identifier = identifier
        if (isTelemetryEnabled(application)) {
        this.application = application
        this.enableOsTag = enableOsTag
        this.isTelemetryEnabled = checkTelemetryDeveloperOption()

        val sentryDsn =
            if (isTelemetryEnabled) identifier
            else String()

        SentryAndroid.init(application) { options ->
                options.dsn = identifier
            options.dsn = sentryDsn
            options.addIntegration(
                SentryTimberIntegration(
                    minEventLevel = SentryLevel.ERROR,
@@ -32,6 +44,14 @@ object Telemetry {
                )
            )
        }

        Sentry.configureScope {
            if (enableOsTag) {
                val eVersion = getSystemProperty(Constants.PROPERTY_OS_VERSION) ?: ""
                it.setTag(Constants.TAG_E_VERSION, eVersion)
            } else {
                it.removeTag(Constants.TAG_E_VERSION)
            }
        }
    }

@@ -57,13 +77,38 @@ object Telemetry {
        Sentry.captureMessage(message, level)
    }

    @Deprecated(
        "Use isReportingTelemetry()",
        ReplaceWith("isReportingTelemetry()"),
    )
    @JvmStatic
    fun isTelemetryEnabled(application: Application): Boolean {
        return try {
            Settings.System.getInt(application.contentResolver, SETTINGS_TELEMETRY_FIELD) == 1
        } catch (e: Settings.SettingNotFoundException) {
            false
        } catch (_: Exception) {
            false
        }
    }

    /**
     * It is possible that the telemetry option in developer options is different from
     * the current state of the library. It is possible if the app is open and
     * e_telemetry system setting is (say) disabled via ADB command.
     *
     * This method reports the actual state of telemetry reporting, immaterial of the
     * developer options. This state is set when [init] has been called, usually
     * during app start up.
     */
    @JvmStatic
    fun isReportingTelemetry() = isTelemetryEnabled

    /**
     * Read from OS developer options.
     * Pass false by default.
     *
     * @param application: the application context
     */
    fun isTelemetryEnabled(application: Application): Boolean {
    private fun checkTelemetryDeveloperOption(): Boolean {
        return try {
            Settings.System.getInt(application.contentResolver, SETTINGS_TELEMETRY_FIELD) == 1
        } catch (e: Settings.SettingNotFoundException) {
@@ -72,4 +117,16 @@ object Telemetry {
            false
        }
    }

    @SuppressLint("PrivateApi")
    fun getSystemProperty(key: String): String? {
        var value: String? = null
        try {
            value = Class.forName("android.os.SystemProperties")
                .getMethod("get", String::class.java).invoke(null, key) as String
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return value
    }
}