diff --git a/README.md b/README.md index 2a1dbca528cd5f722559997c86fa6fdd5a63401c..e6e7cb9ebb7eebf587533685fd22fcce5534e165 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/lib/build.gradle b/lib/build.gradle index 1d2f2e01d2c57063eaaca0b8a999a6bb8de9081c..bc680ce84bd4a4983297bdcfefc595dc439757f8 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -6,7 +6,7 @@ plugins { def versionMajor = 0 def versionMinor = 0 -def versionPatch = 5 +def versionPatch = 6 def releasePatch = "alpha" android { diff --git a/lib/src/main/java/foundation/e/lib/telemetry/Constants.kt b/lib/src/main/java/foundation/e/lib/telemetry/Constants.kt index 54a85cebe5297eea67deda2f744e4b2bd38caacf..747ca3c2a26da1dd9ad252377f5f104f9499bf6e 100644 --- a/lib/src/main/java/foundation/e/lib/telemetry/Constants.kt +++ b/lib/src/main/java/foundation/e/lib/telemetry/Constants.kt @@ -2,4 +2,6 @@ package foundation.e.lib.telemetry internal object Constants { const val SETTINGS_TELEMETRY_FIELD = "e_telemetry" -} \ No newline at end of file + const val TAG_E_VERSION = "e_version" + const val PROPERTY_OS_VERSION = "ro.lineage.version" +} diff --git a/lib/src/main/java/foundation/e/lib/telemetry/Telemetry.kt b/lib/src/main/java/foundation/e/lib/telemetry/Telemetry.kt index 0ec02a41a5dde8361a8e0556fbf0bef2261d65fb..5f5765f1eaea44130cd9ffd7cced16669e360688 100644 --- a/lib/src/main/java/foundation/e/lib/telemetry/Telemetry.kt +++ b/lib/src/main/java/foundation/e/lib/telemetry/Telemetry.kt @@ -1,8 +1,10 @@ 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,17 +25,32 @@ 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)) { - SentryAndroid.init(application) { options -> - options.dsn = identifier - options.addIntegration( - SentryTimberIntegration( - minEventLevel = SentryLevel.ERROR, - minBreadcrumbLevel = SentryLevel.WARNING - ) + this.application = application + this.enableOsTag = enableOsTag + this.isTelemetryEnabled = checkTelemetryDeveloperOption() + + val sentryDsn = + if (isTelemetryEnabled) identifier + else String() + + SentryAndroid.init(application) { options -> + options.dsn = sentryDsn + options.addIntegration( + SentryTimberIntegration( + minEventLevel = SentryLevel.ERROR, + minBreadcrumbLevel = SentryLevel.WARNING ) + ) + } + + 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 } } -} \ No newline at end of file + + @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 + } +}