diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f88da22725da85f2ae58b6ec4dcbbb8f4bc3eea9..d6bb04abe2e1b0e25533254e03f1954f38ddf6f1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,6 +15,12 @@ cache: lib: stage: lib + rules: + - if: '$CI_COMMIT_TAG !~ "/^$/"' + when: always + - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' + when: always + - when: manual script: - ./gradlew :lib:assembleRelease artifacts: @@ -23,5 +29,6 @@ lib: publish: stage: publish + needs: ["lib"] script: - ./gradlew :lib:publish 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..02268fef129fabf3243559cc5167c8f54d75f64f 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" + const val TAG_E_VERSION = "e_version" + const val PROPERTY_OS_VERSION = "ro.lineage.version" } \ No newline at end of file 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..70bb00d963155254c609869a75e34a321e55083c 100644 --- a/lib/src/main/java/foundation/e/lib/telemetry/Telemetry.kt +++ b/lib/src/main/java/foundation/e/lib/telemetry/Telemetry.kt @@ -1,5 +1,6 @@ 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 @@ -11,6 +12,10 @@ 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,40 @@ object Telemetry { * @param application: the application context */ @JvmStatic - fun init(identifier: String, application: Application) { + fun init( + identifier: String, + application: Application, + enableOsTag: Boolean = true, + ) { 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 +85,38 @@ object Telemetry { Sentry.captureMessage(message, level) } + @Deprecated( + "Use isReportingTelemetry()", + ReplaceWith("isReportingTelemetry()"), + ) + 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. + */ + fun isReportingTelemetry(): Boolean { + return 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 +125,16 @@ object Telemetry { false } } + + @SuppressLint("PrivateApi") + private 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 + } } \ No newline at end of file