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

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

There is a newer version: 1.5.8-alpha
Show newest version
package com.wisetrack.sdk

import android.content.Context
import android.os.AsyncTask

/**
 * AdsIdentifier class for managing Advertising ID retrieval.
 * @param config Configuration object containing context and logger.
 * @author hamed (@hamed-hsb)
 * @since 30th October 2024
 */

class AdsIdentifier(private val config: WiseTrackConfig) {

    fun start() {
        Thread {
            try {

                val adInfo: com.google.android.gms.ads.identifier.AdvertisingIdClient.Info =
                    com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(
                        config.context!!
                    )

                println("thread adid: ${adInfo.id}")
                println("limit tracking adid: ${adInfo.isLimitAdTrackingEnabled}")

                val adId: String = adInfo.getId().toString()
                validdateLimited(adInfo.isLimitAdTrackingEnabled)
                validateAdId(adId)
            } catch (e: Exception) {
                println("thread gms ads exception: ${e.message}")
                e.message?.let { config.logger.report(it) }
            }
        }.start()

        getAdvertisingId()
    }


   private fun getAdvertisingId() {
       println("load function getAdvertisingId")

       FetchAdIdTask(config.context!!,config).execute()
    }

    private class FetchAdIdTask(val context: Context,val wiseTrackConfig: WiseTrackConfig) : AsyncTask() {
        override fun doInBackground(vararg params: Void?): String? {
            return try {
                println(" start doInBackground")

                val adInfo: com.google.android.gms.ads.identifier.AdvertisingIdClient.Info =
                    com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(
                        context
                    )
                println("task adid: ${adInfo.id}")
                println("task tracking adid: ${adInfo.isLimitAdTrackingEnabled}")

                validdateLimited(adInfo.isLimitAdTrackingEnabled)
                validateAdId(adInfo.id)
                adInfo.id // Return the Advertising ID
            } catch (e: Exception) {
                println("async task exception: ${e.message}")
                e.message?.let { wiseTrackConfig.logger.report(it) }
                null
            }
        }

        override fun onPostExecute(adId: String?) {
            println("call onPostExecute")
            validateAdId(adId)
        }
    }


    companion object {
        var adId: String? = null
        var isLimited: Boolean? = null
        private fun validateAdId(adid: String?) {
            println("load function validateAdId")

            if (adid != null) {
                this.adId = adid
            }
        }

        private fun validdateLimited(limited: Boolean?) {
            println("load function validdateLimited")

            if (limited != null) {
                this.isLimited = limited
            }
        }
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy