All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.wisetrack.sdk.PackageBuilder.kt Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
package com.wisetrack.sdk

import android.content.ContentResolver
import com.wisetrack.sdk.pm.AppsInfo
import org.json.JSONArray
import org.json.JSONObject
import java.util.Date
import java.util.Locale

/**
@author hamed (@hamed-hsb)
 * @since 11th October 2021
 */

class PackageBuilder(
    private val wiseTrackConfig: WiseTrackConfig,
    private val deviceInfo: DeviceInfo,
    activityState: ActivityState?,
    private val sessionParameters: SessionParameters?,
    private val createdAt: Long
) {

    private val logger: ILogger = WiseTrackFactory.getLogger()
    private var activityStateCopy: ActivityStateCopy? = null

    var clickTimeInSeconds: Long = -1
    var clickTimeInMilliseconds: Long = -1
    var installBeginTimeInSeconds: Long = -1
    var clickTimeServerInSeconds: Long = -1
    var installBeginTimeServerInSeconds: Long = -1
    var reftag: String? = null
    var deeplink: String? = null
    var referrer: String? = null
    var installVersion: String? = null
    var rawReferrer: String? = null
    var referrerApi: String? = null
    var preinstallPayload: String? = null
    var preinstallLocation: String? = null
    var googlePlayInstant: Boolean? = null
    var attribution: WiseTrackAttribution? = null
    var extraParameters: Map? = null

    init {
        activityStateCopy = ActivityStateCopy(activityState)
    }

    fun buildSessionPackage(
        isInDelay: Boolean,
        referrerDetails: String,
        installreferrerApi: String
    ): ActivityPackage {
        referrer = referrerDetails
        referrerApi = installreferrerApi
        val parameters: HashMap = getSessionParameters(isInDelay)
        val sessionPackage: ActivityPackage = getDefaultActivityPackage(ActivityKind.SESSION)
        sessionPackage.path = Constants.END_POINT_SESSION
        sessionPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.SESSION.toString(),
            sessionPackage.clientSdk, wiseTrackConfig.context, logger
        )
        sessionPackage.parameters = parameters
        return sessionPackage
    }

    fun buildEventPackage(
        event: WiseTrackEvent,
        isInDelay: Boolean,
        referrerDetails: String,
        installreferrerApi: String
    ): ActivityPackage {
        referrer = referrerDetails
        referrerApi = installreferrerApi
        val parameters: HashMap = getEventParameters(event, isInDelay)
        val eventPackage = getDefaultActivityPackage(ActivityKind.EVENT)
        eventPackage.path = Constants.END_POINT_EVENT
        eventPackage.suffix = getEventSuffix(event)
        WiseTrackSigner.sign(
            parameters, ActivityKind.EVENT.toString(),
            eventPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        eventPackage.parameters = parameters
        if (isInDelay) {
            eventPackage.callbackParameters = event.callbackParameters
            eventPackage.partnerParameters = event.partnerParameters
        }
        return eventPackage
    }

    fun buildInfoPackage(
        source: String?,
        referrerDetails: String,
        installreferrerApi: String
    ): ActivityPackage {
        referrer = referrerDetails
        referrerApi = installreferrerApi

        val parameters: HashMap = getInfoParameters(source!!)
        val infoPackage = getDefaultActivityPackage(ActivityKind.INFO)
        infoPackage.path = Constants.END_POINT_SDK_INFO
        infoPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.INFO.toString(),
            infoPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        infoPackage.parameters = parameters
        return infoPackage
    }

    fun buildClickPackage(source: String?): ActivityPackage {
        val parameters: HashMap = getClickParameters(source!!)
        val clickPackage = getDefaultActivityPackage(ActivityKind.CLICK)
        clickPackage.path = Constants.END_POINT_SDK_CLICK
        clickPackage.suffix = ""
        clickPackage.clickTimeInMilliseconds = clickTimeInMilliseconds
        clickPackage.clickTimeInSeconds = clickTimeInSeconds
        clickPackage.installBeginTimeInSeconds = installBeginTimeInSeconds
        clickPackage.clickTimeServerInSeconds = clickTimeServerInSeconds
        clickPackage.installBeginTimeServerInSeconds = installBeginTimeServerInSeconds
        clickPackage.installVersion = installVersion
        clickPackage.googlePlayInstant = googlePlayInstant
        WiseTrackSigner.sign(
            parameters, ActivityKind.CLICK.toString(),
            clickPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        clickPackage.parameters = parameters
        return clickPackage
    }

    fun buildAttributionPackage(
        initiatedByDescription: String?,
        referrerDetails: String,
        installreferrerApi: String
    ): ActivityPackage {
        referrer = referrerDetails
        referrerApi = installreferrerApi

        val parameters: HashMap = getAttributionParameters(
            initiatedByDescription!!
        )
        val attributionPackage = getDefaultActivityPackage(ActivityKind.ATTRIBUTION)
        attributionPackage.path =Constants.END_POINT_ATTRIBUTION // does not contain '/' because of Uri.Builder.appendPath
        attributionPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.ATTRIBUTION.toString(),
            attributionPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        attributionPackage.parameters = parameters
        return attributionPackage
    }

    fun buildPkgPackage(initiatedByDescription: String?, pkgList: List): ActivityPackage {
        val parameters: HashMap =
            getPackageInfoParameters(initiatedByDescription!!, pkgList)
        val pkgPackage = getDefaultActivityPackage(ActivityKind.PKG_INFO)

        pkgPackage.path = Constants.END_POINT_PACKAGE_INFO
        pkgPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.PKG_INFO.toString(),
            pkgPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        pkgPackage.parameters = parameters
        return pkgPackage
    }

    fun buildPagePackage(initiatedByDescription: String?, pages: MutableList>): ActivityPackage {
        val parameters: HashMap =
            getPageParameters(initiatedByDescription!!, pages)
        val pkgPackage = getDefaultActivityPackage(ActivityKind.PKG_INFO)

        pkgPackage.path = Constants.END_POINT_PAGE
        pkgPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.PKG_INFO.toString(),
            pkgPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        pkgPackage.parameters = parameters
        return pkgPackage
    }


    fun buildGdprPackage(): ActivityPackage {
        val parameters: HashMap = getGdprParameters()
        val gdprPackage = getDefaultActivityPackage(ActivityKind.GDPR)
        gdprPackage.path = "/gdpr_forget_device"
        gdprPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.GDPR.toString(),
            gdprPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        gdprPackage.parameters = parameters
        return gdprPackage
    }

    fun buildConfigPackage(): ActivityPackage {
        val parameters: HashMap =
            getConfigParameters()
        val pkgPackage = getDefaultActivityPackage(ActivityKind.CONFIG)

        pkgPackage.path = "/config"
        pkgPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.PKG_INFO.toString(),
            pkgPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        pkgPackage.parameters = parameters
        return pkgPackage
    }




    fun buildDisableThirdPartySharingPackage(): ActivityPackage {
        val parameters: HashMap = getDisableThirdPartySharingParameters()
        val activityPackage = getDefaultActivityPackage(ActivityKind.DISABLE_THIRD_PARTY_SHARING)
        activityPackage.path = "/disable_third_party_sharing"
        activityPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.DISABLE_THIRD_PARTY_SHARING.toString(),
            activityPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        activityPackage.parameters = parameters
        return activityPackage
    }

    fun buildThirdPartySharingPackage(
        wiseTrackThirdPartySharing: WiseTrackThirdPartySharing
    ): ActivityPackage {
        val parameters: HashMap =
            getThirdPartySharingParameters(wiseTrackThirdPartySharing)
        val activityPackage = getDefaultActivityPackage(ActivityKind.THIRD_PARTY_SHARING)
        activityPackage.path = "/third_party_sharing"
        activityPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.THIRD_PARTY_SHARING.toString(),
            activityPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        activityPackage.parameters = parameters
        return activityPackage
    }

    fun buildMeasurementConsentPackage(consentMeasurement: Boolean): ActivityPackage {
        val parameters: HashMap =
            getMeasurementConsentParameters(consentMeasurement)
        val activityPackage = getDefaultActivityPackage(ActivityKind.MEASUREMENT_CONSENT)
        activityPackage.path = "/measurement_consent"
        activityPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.MEASUREMENT_CONSENT.toString(),
            activityPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        activityPackage.parameters = parameters
        return activityPackage
    }

    fun buildAdRevenuePackage(source: String?, adRevenueJson: JSONObject?): ActivityPackage {
        val parameters: HashMap = getAdRevenueParameters(
            source!!, adRevenueJson!!
        )
        val adRevenuePackage = getDefaultActivityPackage(ActivityKind.AD_REVENUE)
        adRevenuePackage.path = "/ad_revenue"
        adRevenuePackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.AD_REVENUE.toString(),
            adRevenuePackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        adRevenuePackage.parameters = parameters
        return adRevenuePackage
    }

    fun buildAdRevenuePackage(
        wiseTrackAdRevenue: WiseTrackAdRevenue,
        isInDelay: Boolean
    ): ActivityPackage {
        val parameters: HashMap =
            getAdRevenueParameters(wiseTrackAdRevenue, isInDelay)
        val adRevenuePackage = getDefaultActivityPackage(ActivityKind.AD_REVENUE)
        adRevenuePackage.path = "/ad_revenue"
        adRevenuePackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.AD_REVENUE.toString(),
            adRevenuePackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        adRevenuePackage.parameters = parameters
        if (isInDelay) {
            adRevenuePackage.callbackParameters = wiseTrackAdRevenue.callbackParameters
            adRevenuePackage.partnerParameters = wiseTrackAdRevenue.partnerParameters
        }
        return adRevenuePackage
    }

    fun buildSubscriptionPackage(
        subscription: WiseTrackPlayStoreSubscription,
        isInDelay: Boolean
    ): ActivityPackage {
        val parameters: HashMap = getSubscriptionParameters(subscription, isInDelay)
        val subscriptionPackage = getDefaultActivityPackage(ActivityKind.SUBSCRIPTION)
        subscriptionPackage.path = "/v2/purchase"
        subscriptionPackage.suffix = ""
        WiseTrackSigner.sign(
            parameters, ActivityKind.SUBSCRIPTION.toString(),
            subscriptionPackage.clientSdk, wiseTrackConfig.context!!, logger
        )
        subscriptionPackage.parameters = parameters
        return subscriptionPackage
    }

    private class ActivityStateCopy {
        var eventCount = -1
        var sessionCount = -1
        var subsessionCount = -1
        var timeSpent: Long = -1
        var lastInterval: Long = -1
        var sessionLength: Long = -1
        var uuid: String? = null
        var pushToken: String? = null

        constructor(activityState: ActivityState?) {
            if (activityState == null) {
                return
            }
            eventCount = activityState.eventCount
            sessionCount = activityState.sessionCount
            subsessionCount = activityState.subsessionCount
            timeSpent = activityState.timeSpent
            lastInterval = activityState.lastInterval
            sessionLength = activityState.sessionLength
            uuid = activityState.uuid
            pushToken = activityState.pushToken
        }
    }

    private fun getSessionParameters(isInDelay: Boolean): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val session: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)


        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)

        // Callback and partner parameters.
        if (!isInDelay) {
            addMapJson(
                parameters,
                "callback_params",
                sessionParameters!!.callbackParameters
            )
            addMapJson(
                parameters,
                "partner_params",
                sessionParameters.partnerParameters
            )
        }


        addBoolean(session, "needs_response_details", true)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)


        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(session) && !containsFireIds(session)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addLong(
            parameters,
            "connectivity_type",
            AndroidUtil.getConnectivityType(wiseTrackConfig.context!!).toLong()
        )
        addString(parameters, "country", deviceInfo.country)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "installed_at", deviceInfo.appInstallTime)
        addString(parameters, "language", deviceInfo.language)
        addDuration(parameters, "last_interval", activityStateCopy!!.lastInterval)
        addString(parameters, "mcc", AndroidUtil.getMcc(wiseTrackConfig.context!!))
        addString(parameters, "mnc", AndroidUtil.getMnc(wiseTrackConfig.context!!))


        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)

        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addLong(
            parameters,
            "session_count",
            activityStateCopy!!.sessionCount.toLong()
        )
        addDuration(parameters, "session_length", activityStateCopy!!.sessionLength)
        addLong(
            parameters,
            "subsession_count",
            activityStateCopy!!.subsessionCount.toLong()
        )
        addDuration(parameters, "time_spent", activityStateCopy!!.timeSpent)
        addString(parameters, "updated_at", deviceInfo.appUpdateTime)

        addString(parameters, "app_set_id", deviceInfo.appSetId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)



        addMapJson(session, "parameters", parameters)
        checkDeviceIds(session)
        return session
    }

    fun getEventParameters(event: WiseTrackEvent, isInDelay: Boolean): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val eventParameters: HashMap = HashMap()


        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Callback and partner parameters.
        if (!isInDelay) {
            addMapJson(
                parameters, "callback_params", Util.mergeParameters(
                    sessionParameters!!.callbackParameters, event.callbackParameters, "Callback"
                )
            )
            addMapJson(
                parameters, "partner_params", Util.mergeParameters(
                    sessionParameters.partnerParameters, event.partnerParameters, "Partner"
                )
            )
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)

        addBoolean(eventParameters, "needs_response_details", true)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)

        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(eventParameters) && !containsFireIds(eventParameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)

        addString(parameters, "android_id", deviceInfo.androidId)
        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        addLong(
            parameters,
            "connectivity_type",
            AndroidUtil.getConnectivityType(wiseTrackConfig.context!!).toLong()
        )
        addString(parameters, "country", deviceInfo.country)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addString(parameters, "currency", event.currency)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addString(parameters, "event_callback_id", event.callbackId)
        addLong(parameters, "event_count", activityStateCopy!!.eventCount.toLong())
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "event_token", event.eventToken)
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "language", deviceInfo.language)
        addString(parameters, "mcc", AndroidUtil.getMcc(wiseTrackConfig.context!!))
        addString(parameters, "mnc", AndroidUtil.getMnc(wiseTrackConfig.context!!))


        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addDouble(parameters, "revenue", event.revenue)
        addString(parameters, "deduplication_id", event.orderId);
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addLong(
            parameters,
            "session_count",
            activityStateCopy!!.sessionCount.toLong()
        )
        addDuration(parameters, "session_length", activityStateCopy!!.sessionLength)
        addLong(
            parameters,
            "subsession_count",
            activityStateCopy!!.subsessionCount.toLong()
        )
        addDuration(parameters, "time_spent", activityStateCopy!!.timeSpent)

        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "app_set_id", deviceInfo.appSetId)


        addMapJson(eventParameters, "parameters", parameters)
        checkDeviceIds(eventParameters)
        return eventParameters
    }

    private fun getInfoParameters(source: String): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val sdkInfoParameters: HashMap = HashMap()

        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)

        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(sdkInfoParameters) && !containsFireIds(sdkInfoParameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }


        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_build", deviceInfo.buildName)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "android_id", deviceInfo.androidId)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)

        // Rest of the parameters.
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addBoolean(parameters, "needs_response_details", true)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addString(parameters, "source", source)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "app_set_id", deviceInfo.appSetId)

        addMapJson(sdkInfoParameters, "parameters", parameters)
        checkDeviceIds(parameters)
        return sdkInfoParameters
    }

    private fun getClickParameters(source: String): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val sdkClick: HashMap = HashMap()


        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)

        addBoolean(sdkClick, "needs_response_details", true)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(sdkClick) && !containsFireIds(sdkClick)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )

            deviceInfo.reloadNonPlayIds(wiseTrackConfig);
        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Attribution parameters.
        if (attribution != null) {
            addString(parameters, "tracker", attribution!!.trackerName)
            addString(parameters, "campaign", attribution!!.campaign)
            addString(parameters, "adgroup", attribution!!.adgroup)
            addString(parameters, "creative", attribution!!.creative)
        }

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)

        addMapJson(
            parameters,
            "callback_params",
            sessionParameters!!.callbackParameters
        )
        addDateInMilliseconds(parameters, "click_time", clickTimeInMilliseconds)
        addDateInSeconds(parameters, "click_time", clickTimeInSeconds)
        addDateInSeconds(parameters, "click_time_server", clickTimeServerInSeconds)
        addLong(
            parameters,
            "connectivity_type",
            AndroidUtil.getConnectivityType(wiseTrackConfig.context!!).toLong()
        )
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        addString(parameters, "country", deviceInfo.country)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addString(parameters, "deeplink", deeplink)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addBoolean(parameters, "google_play_instant", googlePlayInstant)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addDateInSeconds(parameters, "install_begin_time", installBeginTimeInSeconds)
        addDateInSeconds(
            parameters,
            "install_begin_time_server",
            installBeginTimeServerInSeconds
        )
        addString(parameters, "install_version", installVersion)
        addString(parameters, "installed_at", deviceInfo.appInstallTime)
        addString(parameters, "language", deviceInfo.language)
        addDuration(parameters, "last_interval", activityStateCopy!!.lastInterval)
        addString(parameters, "mcc", AndroidUtil.getMcc(wiseTrackConfig.context!!))
        addString(parameters, "mnc", AndroidUtil.getMnc(wiseTrackConfig.context!!))


        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addMapJson(parameters, "params", extraParameters)
        addMapJson(
            parameters,
            "partner_params",
            sessionParameters.partnerParameters
        )
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "raw_referrer", rawReferrer)
        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)
        addString(parameters, "reftag", reftag)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addLong(
            parameters,
            "session_count",
            activityStateCopy!!.sessionCount.toLong()
        )
        addDuration(parameters, "session_length", activityStateCopy!!.sessionLength)
        addString(parameters, "source", source)
        addLong(
            parameters,
            "subsession_count",
            activityStateCopy!!.subsessionCount.toLong()
        )
        addDuration(parameters, "time_spent", activityStateCopy!!.timeSpent)
        addString(parameters, "updated_at", deviceInfo.appUpdateTime)
        addString(parameters, "payload", preinstallPayload)
        addString(parameters, "found_location", preinstallLocation)

        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "app_set_id", deviceInfo.appSetId)



        addMapJson(sdkClick, "parameters", parameters)

        checkDeviceIds(sdkClick)
        return sdkClick
    }

    private fun getAttributionParameters(initiatedBy: String): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val attribution: HashMap = HashMap()


        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)



        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(attribution) && !containsFireIds(attribution)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addString(parameters, "android_id", deviceInfo.androidId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)

        // Rest of the parameters.
        addBoolean(attribution, "needs_response_details", true)

        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(parameters, "event_buffering_enabled", wiseTrackConfig.eventBufferingEnabled)
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addString(parameters, "app_set_id", deviceInfo.appSetId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)


        addMapJson(attribution, "parameters", parameters)
        checkDeviceIds(attribution)
        return attribution
    }


    private fun getPageParameters(
        initiatedBy: String,
        pages: MutableList>
    ): HashMap {

        val parameters: HashMap = HashMap()
        val page: HashMap = HashMap()
        var pageParameter: HashMap
        var jsonObjectList = mutableListOf?>()

        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)

        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(page) && !containsFireIds(page)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_id", deviceInfo.androidId)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        // Rest of the parameters.
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addBoolean(parameters, "needs_response_details", true)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "app_set_id", deviceInfo.appSetId)

        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_build", deviceInfo.buildName)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addString(parameters, "referrer", referrer)
        addString(parameters, "referrer_api", referrerApi)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "android_id", deviceInfo.androidId)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)


        pages.forEach {
            pageParameter = HashMap()
            it.forEach {(key, value) ->
                addString(pageParameter,key,value)

            }
            jsonObjectList.add(pageParameter)
        }


        addMapJsonArray(page, "pages", jsonObjectList)
        addMapJson(page, "parameters", parameters)
        checkDeviceIds(parameters)
        return page
    }

    private fun getPackageInfoParameters(
        initiatedBy: String,
        pkgList: List
    ): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        var parametersPkgInfo: HashMap

        val pkgInfo: HashMap = HashMap()

        var jsonObjectList = mutableListOf?>()




        deviceInfo.reloadOtherDeviceInfoParams(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (deviceInfo.imeiParameters != null) {
            parameters.putAll(deviceInfo.imeiParameters!!)
        }


        // Check if oaid plugin is used and if yes, add the parameter
        if (deviceInfo.oaidParameters != null) {
            parameters.putAll(deviceInfo.oaidParameters!!)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)



        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(pkgInfo) && !containsFireIds(pkgInfo)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }


        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addString(parameters, "android_id", deviceInfo.androidId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)

        // Rest of the parameters.
        addBoolean(pkgInfo, "needs_response_details", true)

        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addString(parameters, "app_set_id", deviceInfo.appSetId)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(parameters, "event_buffering_enabled", wiseTrackConfig.eventBufferingEnabled)
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "initiated_by", initiatedBy)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)


        // TODO API LEVEL 24
        pkgList.forEach {

            parametersPkgInfo = HashMap()

            var permission = ""

            addString(parametersPkgInfo, "label", it.label)
            addString(parametersPkgInfo, "package_name", it.packageName)
            addString(parametersPkgInfo, "category", it.category)
            addLong(parametersPkgInfo, "version_code", it.versionCode)
            addString(parametersPkgInfo, "version_name", it.versionName)
            addInteger(parametersPkgInfo, "min_sdk_version", it.minSdkVersion)
            addInteger(parametersPkgInfo, "compile_sdk_version", it.compileSdkVersion)
            addInteger(parametersPkgInfo, "target_sdk_version", it.targetSdkVersion)
            addString(parametersPkgInfo, "install_location", it.installLocation)
            addLong(parametersPkgInfo, "first_install_time", it.firstInstallTime)
            addLong(parametersPkgInfo, "last_update_time", it.lastUpdateTime)
            addString(parametersPkgInfo, "shared_user_id", it.sharedUserId)
            addBoolean(parametersPkgInfo, "enabled", it.enabled)
            addString(parametersPkgInfo, "process_name", it.processName)


            it.requestedPermissions!!.forEach { itp ->
                permission += itp + ","
            }

            permission = permission.substring(0, permission.length - 1)

            addString(parametersPkgInfo, "requested_permissions", permission)

            jsonObjectList.add(parametersPkgInfo)
        }

        addMapJsonArray(pkgInfo, "package_info", jsonObjectList)

        addMapJson(pkgInfo, "parameters", parameters)

        checkDeviceIds(pkgInfo)
        return pkgInfo
    }


    private fun getConfigParameters(
    ): HashMap {
        val config: HashMap = HashMap()
        val KEY = "caede917a8676edcac7e9d6481043710792411c9"

        addString(config, "env", BuildConfig.ENVIRMENT_PRODOCUTION)
        addString(config, "sdk_hash", KEY)
        addString(config, "sdk_platform", "android")
        addString(config, "sdk_version", "1.5.4")


        checkDeviceIds(config)
        return config
    }

    private fun getGdprParameters(): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addBoolean(parameters, "needs_response_details", true)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun getDisableThirdPartySharingParameters(): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addBoolean(parameters, "needs_response_details", true)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun getThirdPartySharingParameters(wiseTrackThirdPartySharing: WiseTrackThirdPartySharing): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Third Party Sharing
        if (wiseTrackThirdPartySharing.isEnabled != null) {
            addString(
                parameters, "sharing",
                if (wiseTrackThirdPartySharing.isEnabled) "enable" else "disable"
            )
        }
        addMapJson(
            parameters, "granular_third_party_sharing_options",
            wiseTrackThirdPartySharing.granularOptions
        )

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addBoolean(parameters, "needs_response_details", true)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun getMeasurementConsentParameters(
        consentMeasurement: Boolean
    ): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Measurement Consent
        addString(
            parameters, "measurement",
            if (consentMeasurement) "enable" else "disable"
        )

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addBoolean(parameters, "needs_response_details", true)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun getAdRevenueParameters(
        source: String,
        adRevenueJson: JSONObject
    ): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addLong(
            parameters,
            "connectivity_type",
            AndroidUtil.getConnectivityType(wiseTrackConfig.context!!).toLong()
        )
        addString(parameters, "country", deviceInfo.country)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "installed_at", deviceInfo.appInstallTime)
        addString(parameters, "language", deviceInfo.language)
        addDuration(parameters, "last_interval", activityStateCopy!!.lastInterval)
        addString(parameters, "mcc", AndroidUtil.getMcc(wiseTrackConfig.context!!))
        addString(parameters, "mnc", AndroidUtil.getMnc(wiseTrackConfig.context!!))
        addBoolean(parameters, "needs_response_details", true)

        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addString(parameters, "source", source)
        addJsonObject(parameters, "payload", adRevenueJson)
        addLong(
            parameters,
            "session_count",
            activityStateCopy!!.sessionCount.toLong()
        )
        addDuration(parameters, "session_length", activityStateCopy!!.sessionLength)
        addLong(
            parameters,
            "subsession_count",
            activityStateCopy!!.subsessionCount.toLong()
        )
        addDuration(parameters, "time_spent", activityStateCopy!!.timeSpent)
        addString(parameters, "updated_at", deviceInfo.appUpdateTime)
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun getAdRevenueParameters(
        wiseTrackAdRevenue: WiseTrackAdRevenue,
        isInDelay: Boolean
    ): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Callback and partner parameters.
        if (!isInDelay) {
            addMapJson(
                parameters, "callback_params", Util.mergeParameters(
                    sessionParameters!!.callbackParameters,
                    wiseTrackAdRevenue.callbackParameters,
                    "Callback"
                )
            )
            addMapJson(
                parameters, "partner_params", Util.mergeParameters(
                    sessionParameters.partnerParameters,
                    wiseTrackAdRevenue.partnerParameters,
                    "Partner"
                )
            )
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addLong(
            parameters,
            "connectivity_type",
            AndroidUtil.getConnectivityType(wiseTrackConfig.context!!).toLong()
        )
        addString(parameters, "country", deviceInfo.country)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "installed_at", deviceInfo.appInstallTime)
        addString(parameters, "language", deviceInfo.language)
        addDuration(parameters, "last_interval", activityStateCopy!!.lastInterval)
        addString(parameters, "mcc", AndroidUtil.getMcc(wiseTrackConfig.context!!))
        addString(parameters, "mnc", AndroidUtil.getMnc(wiseTrackConfig.context!!))
        addBoolean(parameters, "needs_response_details", true)

        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addString(parameters, "source", wiseTrackAdRevenue.source)
        addDoubleWithoutRounding(parameters, "revenue", wiseTrackAdRevenue.revenue)
        addString(parameters, "currency", wiseTrackAdRevenue.currency)
        addInteger(
            parameters,
            "ad_impressions_count",
            wiseTrackAdRevenue.adImpressionsCount
        )
        addString(parameters, "ad_revenue_network", wiseTrackAdRevenue.adRevenueNetwork)
        addString(parameters, "ad_revenue_unit", wiseTrackAdRevenue.adRevenueUnit)
        addString(
            parameters,
            "ad_revenue_placement",
            wiseTrackAdRevenue.adRevenuePlacement
        )
        addLong(
            parameters,
            "session_count",
            activityStateCopy!!.sessionCount.toLong()
        )
        addDuration(parameters, "session_length", activityStateCopy!!.sessionLength)
        addLong(
            parameters,
            "subsession_count",
            activityStateCopy!!.subsessionCount.toLong()
        )
        addDuration(parameters, "time_spent", activityStateCopy!!.timeSpent)
        addString(parameters, "updated_at", deviceInfo.appUpdateTime)
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun getSubscriptionParameters(
        subscription: WiseTrackPlayStoreSubscription,
        isInDelay: Boolean
    ): HashMap {
        val contentResolver: ContentResolver = wiseTrackConfig.context!!.contentResolver
        val parameters: HashMap = HashMap()
        val imeiParameters =
            Util.getImeiParameters(wiseTrackConfig, logger)

        // Check if plugin is used and if yes, add read parameters.
        if (imeiParameters != null) {
            parameters.putAll(imeiParameters)
        }

        // Check if oaid plugin is used and if yes, add the parameter
        val oaidParameters =
            Util.getOaidParameters(wiseTrackConfig, logger)
        if (oaidParameters != null) {
            parameters.putAll(oaidParameters)
        }

        // Device identifiers.
        deviceInfo.reloadPlayIds(wiseTrackConfig)
        addString(parameters, "initiated_by", Constants.INITIATED_BY)
        addString(parameters, "android_uuid", activityStateCopy!!.uuid)
        addString(parameters, "gps_adid", deviceInfo.playAdId)
        addLong(parameters, "gps_adid_attempt", deviceInfo.playAdIdAttempt.toLong())
        addString(parameters, "gps_adid_src", deviceInfo.playAdIdSource)
        addBoolean(parameters, "tracking_enabled", deviceInfo.isTrackingEnabled)
        addString(
            parameters,
            "fire_adid",
            Util.getFireAdvertisingId(wiseTrackConfig.context!!.contentResolver)
        )
        addBoolean(
            parameters,
            "fire_tracking_enabled",
            Util.getFireTrackingEnabled(wiseTrackConfig.context!!.contentResolver)
        )
        if (!containsPlayIds(parameters) && !containsFireIds(parameters)) {
            logger.warn(
                "Google Advertising ID or Fire Advertising ID not detected, " +
                        "fallback to non Google Play and Fire identifiers will take place"
            )
            deviceInfo.reloadNonPlayIds(wiseTrackConfig);

        }

        addString(parameters, "android_id", deviceInfo.androidId)

        // Callback and partner parameters.
        if (!isInDelay) {
            addMapJson(
                parameters, "callback_params", Util.mergeParameters(
                    sessionParameters?.callbackParameters,
                    subscription.getCallbackParameters(),
                    "Callback"
                )
            )
            addMapJson(
                parameters, "partner_params", Util.mergeParameters(
                    sessionParameters?.partnerParameters,
                    subscription.getPartnerParameters(),
                    "Partner"
                )
            )
        }

        // Rest of the parameters.
        addString(parameters, "api_level", deviceInfo.apiLevel)
        addString(parameters, "app_secret", wiseTrackConfig.appSecret)
        addString(parameters, "app_token", wiseTrackConfig.appToken)
        addString(parameters, "app_version", deviceInfo.appVersion)
        addBoolean(parameters, "attribution_deeplink", true)
        addLong(
            parameters,
            "connectivity_type",
            AndroidUtil.getConnectivityType(wiseTrackConfig.context!!).toLong()
        )
        addString(parameters, "country", deviceInfo.country)
        addString(parameters, "cpu_type", deviceInfo.abi)
        addDateInMilliseconds(parameters, "created_at", createdAt)
        addString(parameters, "default_tracker", wiseTrackConfig.defaultTracker)
        addBoolean(parameters, "device_known", wiseTrackConfig.deviceKnown)
        addBoolean(parameters, "needs_cost", wiseTrackConfig.needsCost)
        addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer)
        addString(parameters, "device_name", deviceInfo.deviceName)
        addString(parameters, "device_type", deviceInfo.deviceType)
        addLong(parameters, "ui_mode", deviceInfo.uiMode?.toLong())
        addString(parameters, "display_height", deviceInfo.displayHeight)
        addString(parameters, "display_width", deviceInfo.displayWidth)
        addString(parameters, "environment", wiseTrackConfig.environment)
        addBoolean(
            parameters,
            "event_buffering_enabled",
            wiseTrackConfig.eventBufferingEnabled
        )
        addString(parameters, "external_device_id", wiseTrackConfig.externalDeviceId)
        addString(parameters, "fb_id", deviceInfo.fbAttributionId)
        addString(parameters, "hardware_name", deviceInfo.hardwareName)
        addString(parameters, "installed_at", deviceInfo.appInstallTime)
        addString(parameters, "language", deviceInfo.language)
        addDuration(parameters, "last_interval", activityStateCopy!!.lastInterval)
        addString(parameters, "mcc", AndroidUtil.getMcc(wiseTrackConfig.context!!))
        addString(parameters, "mnc", AndroidUtil.getMnc(wiseTrackConfig.context!!))
        addBoolean(parameters, "needs_response_details", true)

        addString(parameters, "os_build", deviceInfo.buildName)
        addString(parameters, "os_name", deviceInfo.osName)
        addString(parameters, "os_version", deviceInfo.osVersion)
        addString(parameters, "package_name", deviceInfo.packageName)
        addString(parameters, "push_token", activityStateCopy!!.pushToken)
        addString(parameters, "screen_density", deviceInfo.screenDensity)
        addString(parameters, "screen_format", deviceInfo.screenFormat)
        addString(parameters, "screen_size", deviceInfo.screenSize)
        addString(parameters, "secret_id", wiseTrackConfig.secretId)
        addLong(
            parameters,
            "session_count",
            activityStateCopy!!.sessionCount.toLong()
        )
        addDuration(parameters, "session_length", activityStateCopy!!.sessionLength)
        addLong(
            parameters,
            "subsession_count",
            activityStateCopy!!.subsessionCount.toLong()
        )
        addDuration(parameters, "time_spent", activityStateCopy!!.timeSpent)
        addString(parameters, "updated_at", deviceInfo.appUpdateTime)

        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)

        // subscription specific parameters
        addString(parameters, "billing_store", subscription.getBillingStore())
        addString(parameters, "currency", subscription.getCurrency())
        addString(parameters, "product_id", subscription.getSku())
        addString(parameters, "purchase_token", subscription.getPurchaseToken())
        addString(parameters, "receipt", subscription.getSignature())
        addLong(parameters, "revenue", subscription.getPrice())
        addDateInMilliseconds(
            parameters,
            "transaction_date",
            subscription.getPurchaseTime()
        )
        addString(parameters, "transaction_id", subscription.getOrderId())
        addBoolean(parameters, "ff_play_store_kids_app", wiseTrackConfig.playStoreKidsAppEnabled)
        addBoolean(parameters, "ff_coppa", wiseTrackConfig.coppaCompliantEnabled)
        checkDeviceIds(parameters)
        return parameters
    }

    private fun checkDeviceIds(parameters: Map?) {
        if (parameters != null && !parameters.containsKey("android_id")
            && !parameters.containsKey("gps_adid")
            && !parameters.containsKey("fire_adid")
            && !parameters.containsKey("oaid")
            && !parameters.containsKey("imei")
            && !parameters.containsKey("meid")
            && !parameters.containsKey("device_id")
            && !parameters.containsKey("imeis")
            && !parameters.containsKey("meids")
            && !parameters.containsKey("device_ids")
        ) {
            // TODO
            // logger.error("Missing device id's. Please check if Proguard is correctly set with WiseTrack SDK")
        }
    }

    private fun containsPlayIds(parameters: Map?): Boolean {
        return parameters?.containsKey("gps_adid") ?: false
    }

    private fun containsFireIds(parameters: Map?): Boolean {
        return parameters?.containsKey("fire_adid") ?: false
    }

    private fun getDefaultActivityPackage(activityKind: ActivityKind): ActivityPackage {
        val activityPackage = ActivityPackage(activityKind)
        activityPackage.clientSdk = deviceInfo.clientSdk
        return activityPackage
    }

    companion object {
        fun addString(parameters: HashMap, key: String, value: String?) {
            if (value.isNullOrEmpty()) {
                return
            }

            parameters[key] = value

        }

        fun addBoolean(parameters: HashMap, key: String, value: Boolean?) {
            if (value == null) {
                return
            }
            val intValue = if (value) 1 else 0
            addLong(parameters, key, intValue.toLong())
        }

        fun addJsonObject(
            parameters: HashMap,
            key: String,
            jsonObject: JSONObject?
        ) {
            if (jsonObject == null) {
                return
            }
            addString(parameters, key, jsonObject.toString())
        }

        fun addMapJson(parameters: HashMap, key: String, map: Map<*, *>?) {
            if (map == null) {
                return
            }
            if (map.isEmpty()) {
                return
            }
            val jsonObject = JSONObject(map)
            val jsonString = jsonObject.toString()
            addString(parameters, key, jsonString)
        }

        fun addMapJsonArray(
            parameters: HashMap,
            key: String,
            map: MutableList?>
        ) {
            if (map == null) {
                return
            }
            if (map.isEmpty()) {
                return
            }

            var jsonObject: JSONObject
            val jsonArray = JSONArray()

            map.forEach {

                jsonObject = JSONObject(it)
                jsonArray.put(jsonObject)
            }


            val jsonArrayString = jsonArray.toString()

            addString(parameters, key, jsonArrayString)
        }

        fun addLong(parameters: HashMap, key: String, value: Long?) {
            if (value == null) {
                return
            }

            if (value < 0) {
                return
            }
            val valueString = value.toString()
            addString(parameters, key, valueString)
        }

        private fun addDateInMilliseconds(
            parameters: HashMap,
            key: String,
            value: Long
        ) {
            if (value <= 0) {
                return
            }
            val date = Date(value)
            addDate(parameters, key, date)
        }

        private fun addDateInSeconds(
            parameters: HashMap,
            key: String,
            value: Long
        ) {
            if (value <= 0) {
                return
            }
            val date = Date(value * 1000)
            addDate(parameters, key, date)
        }

        private fun addDate(parameters: HashMap, key: String, value: Date?) {
            if (value == null) {
                return
            }
            val dateString = Util.dateFormatter.format(value)
            addString(parameters, key, dateString)
        }

        private fun addDuration(
            parameters: HashMap,
            key: String,
            durationInMilliSeconds: Long
        ) {
            if (durationInMilliSeconds < 0) {
                return
            }
            val durationInSeconds = (durationInMilliSeconds + 500) / 1000
            addLong(parameters, key, durationInSeconds)
        }

        private fun addDouble(parameters: HashMap, key: String, value: Double?) {
            if (value == null) {
                return
            }

            val doubleString: String = String.format(Locale.UK, "%.5f", value)

            addString(parameters, key, doubleString)
        }

        private fun addDoubleWithoutRounding(
            parameters: HashMap,
            key: String,
            value: Double?
        ) {
            if (value == null) {
                return
            }
            val doubleString = value.toString()
            addString(parameters, key, doubleString)
        }

        private fun addInteger(parameters: HashMap, key: String, value: Int?) {
            if (value == null) {
                return
            }
            val intString = value.toString()
            addString(parameters, key, intString)
        }
    }

    private fun getEventSuffix(event: WiseTrackEvent): String {
        return if (event.revenue == null) {
            "'${event.eventToken}'"
        } else {
            String.format("(%.5f %s, '%s')", event.revenue, event.currency, event.eventToken)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy