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

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

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

import android.content.Context
import android.util.Log

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

class Reflection {
    companion object {
        @Throws(Exception::class)
        fun getAdvertisingInfoObject(context: Context?): Any? {
            return invokeStaticMethod(
                "com.google.android.gms.ads.identifier.AdvertisingIdClient",
                "getAdvertisingIdInfo",
                arrayOf(
                    Context::class.java
                ),
                context
            )
        }

        fun getImeiParameters(context: Context?, logger: ILogger?): Map? {
            try {
                val nonPlayParameters = invokeStaticMethod(
                    "com.wisetrack.sdk.imei.javaversion.Util", "getImeiParameters", arrayOf(
                        Context::class.java,
                        ILogger::class.java
                    ), context, logger
                )
                val stringStringMapClass = MutableMap::class.java as Class>
                if (nonPlayParameters != null && stringStringMapClass.isInstance(nonPlayParameters)) {
                    return nonPlayParameters as Map
                }
            } catch (e: Exception) {
            }
            return null
        }

        fun getOaidParameters(context: Context?, logger: ILogger?): Map? {
            try {
                val oaidParameters = invokeStaticMethod(
                    "com.wisetrack.sdk.oaid.javaversion.UtilJavaVersion", "getOaidParameters", arrayOf(
                        Context::class.java,
                        ILogger::class.java
                    ), context, logger
                )as Map?
                val stringStringMapClass = MutableMap::class.java as Class>
                if (oaidParameters != null && stringStringMapClass.isInstance(oaidParameters)) {
                    return oaidParameters as Map
                }

            } catch (e: Exception) {
            }
            return null
        }

        fun getPlayAdId(context: Context, AdvertisingInfoObject: Any): String? {
            return try {
                val playAdid = invokeInstanceMethod(
                    AdvertisingInfoObject,
                    "getId",
                    null
                )
                playAdid as String
            } catch (t: Throwable) {
                null
            }
        }

        fun isPlayTrackingEnabled(context: Context?, AdvertisingInfoObject: Any): Boolean? {
            return try {
                val isLimitedTrackingEnabled =
                    invokeInstanceMethod(
                        AdvertisingInfoObject,
                        "isLimitAdTrackingEnabled",
                        null
                    ) as Boolean
                return if (isLimitedTrackingEnabled == null) null else !isLimitedTrackingEnabled
            } catch (t: Throwable) {
                null
            }
        }

        fun forName(className: String): Class<*>? {
            return try {
                Class.forName(className)
            } catch (t: Throwable) {
                null
            }
        }

        fun createDefaultInstance(className: String): Any? {
            val classObject: Class<*> = forName(className)
                ?: return null
            return createDefaultInstance(classObject)
        }

        fun createDefaultInstance(classObject: Class<*>): Any? {
            return try {
                classObject.newInstance()
            } catch (t: Throwable) {
                null
            }
        }

        fun createInstance(className: String, cArgs: Array?>, vararg args: Any?): Any? {
            return try {
                val classObject = Class.forName(className)
                val constructor =
                    classObject.getConstructor(*cArgs)
                constructor.newInstance(*args)
            } catch (t: Throwable) {
                null
            }
        }


        @Throws(Exception::class)
        fun invokeStaticMethod(
            className: String,
            methodName: String,
            cArgs: Array>,
            vararg args: Any?
        ): Any? {

            val classObject = Class.forName(className)
            return invokeMethod(classObject, methodName, null, cArgs, *args)
        }

        @Throws(Exception::class)
        fun invokeInstanceMethod(
            instance: Any,
            methodName: String,
            cArgs: Array>?,
            vararg args: Any?
        ): Any? {
            val classObject: Class<*> = instance.javaClass
            return invokeMethod(classObject, methodName, instance, cArgs, *args)
        }

        fun isAppRunningInSamsungCloudEnvironment(context: Context?, logger: ILogger): Boolean {
            var isCloudEnvironment = false
            try {
                isCloudEnvironment = invokeStaticMethod(
                    "com.wisetrack.sdk.samsung.clouddev.Util",
                    "isAppRunningInCloudEnvironment", arrayOf(
                        Context::class.java,
                        ILogger::class.java
                    ),
                    context, logger
                ) as Boolean
            } catch (e: java.lang.Exception) {
                logger.info("invoke isAppRunningInCloudEnvironment : " + e.message)
            }
            return isCloudEnvironment
        }


        fun getSamsungCloudDevGoogleAdId(context: Context?, logger: ILogger): String? {
            var googleAdId: String? = null
            try {
                googleAdId = invokeStaticMethod(
                    "com.wisetrack.sdk.samsung.clouddev.Util",
                    "getGoogleAdId", arrayOf(
                        Context::class.java,
                        ILogger::class.java
                    ),
                    context, logger
                ) as String
            } catch (e: java.lang.Exception) {
                logger.info("invoke getGoogleAdId : " + e.message)
            }
            return googleAdId
        }
        @Throws(Exception::class)
        fun invokeMethod(
            classObject: Class<*>,
            methodName: String,
            instance: Any?,
            cArgs: Array>?,
            vararg args: Any?
        ): Any? {

            val methodObject = if (cArgs != null) {
                classObject.getMethod(methodName, *cArgs)
            } else {
                classObject.getMethod(methodName)
            }

            return methodObject.invoke(instance, *args)
        }

        @Throws(Exception::class)
        fun readField(className: String, fieldName: String): Any? {
            return readField(className, fieldName, null)
        }

        @Throws(Exception::class)
        fun readField(className: String, fieldName: String, instance: Any?): Any? {
            val classObject: Class<*> = forName(className) ?: return null
            val fieldObject = classObject.getField(fieldName) ?: return null
            return fieldObject[instance]
        }

        @Throws(Exception::class)
        fun getAppSetId(context: Context?): String? {
            try {
                val appSetIdClientObject = invokeStaticMethod(
                    "com.google.android.gms.appset.AppSet",
                    "getClient", arrayOf>(Context::class.java), context
                )



                val taskWithAppSetInfoObject = invokeInstanceMethod(
                    appSetIdClientObject!!,
                    "getAppSetIdInfo", null
                )
                val appSetInfoObject = invokeStaticMethod(
                    "com.google.android.gms.tasks.Tasks",
                    "await",
                    arrayOf(forName("com.google.android.gms.tasks.Task")!!) ,
                    taskWithAppSetInfoObject
                )

                return invokeInstanceMethod(appSetInfoObject!!,"getId", null) as String
            } catch (t: Throwable) {
                null
            }
            return null
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy