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

Commit 2a3bc40e authored by Sayantan Roychowdhury's avatar Sayantan Roychowdhury
Browse files

trackTelemetryOptionChange()

parent 3a9e3903
Loading
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
package foundation.e.lib.telemetry

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import foundation.e.lib.telemetry.Constants.SETTINGS_TELEMETRY_FIELD
import io.sentry.Sentry
@@ -10,6 +12,7 @@ object Telemetry {

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

    private var isTelemetryEnabled: Boolean = false

@@ -24,12 +27,15 @@ object Telemetry {
    ) {
        this.identifier = identifier
        this.application = application
        this.enableOsTag = enableOsTag

        this.isTelemetryEnabled = checkTelemetryDeveloperOption()
        if (isTelemetryEnabled) {
            SentryAndroid.init(application) { options ->
                options.dsn = identifier
            }
        }

        Sentry.configureScope {
            if (enableOsTag) {
                val eVersion = Utils.getSystemProperty(Constants.PROPERTY_OS_VERSION) ?: ""
@@ -40,6 +46,34 @@ object Telemetry {
        }
    }

    /**
     * Check if telemetry option is changed every 2 seconds.
     * Or specify a different [interval].
     */
    fun trackTelemetryOptionChange(interval: Long = 2000) {
        val handle = Handler(Looper.getMainLooper())
        val runnable = Runnable {
            if (isTelemetryEnabled && !checkTelemetryDeveloperOption()) {
                /* Indicates that telemetry reporting is going on but
                 * telemetry from developer options is turned off.
                 * In this case, disable telemetry.
                 */
                SentryAndroid.init(application) { options ->
                    options.dsn = null
                }
                isTelemetryEnabled = false

            } else if (!isTelemetryEnabled && checkTelemetryDeveloperOption()) {
                /*
                 * Just the opposite of the above case.
                 * Re-initialise to auto enable.
                 */
                init(identifier, application, enableOsTag)
            }
        }
        handle.postDelayed(runnable, interval)
    }

    /**
     * Send a simple string message.
     */