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

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

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

import android.content.Context

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

class WiseTrackSigner {
    private constructor()

    companion object {
        // https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
        private var signerInstance: Any? = null

        fun enableSigning(logger: ILogger) {
            getSignerInstance()
            if (signerInstance == null) {
                return
            }
            try {
                Reflection.invokeInstanceMethod(signerInstance!!, "enableSigning", null)
            } catch (e: Exception) {
                logger.warn("Invoking Signer enableSigning() received an error [${e.message!!}]")
                logger.report("Invoking Signer enableSigning() received an error [${e.message!!}]")
            }
        }

        fun disableSigning(logger: ILogger) {
            getSignerInstance()
            if (signerInstance == null) {
                return
            }
            try {
                Reflection.invokeInstanceMethod(signerInstance!!, "disableSigning", null)
            } catch (e: Exception) {
                logger.warn("Invoking Signer disableSigning() received an error [${e.message!!}]")
                logger.report("Invoking Signer disableSigning() received an error [${e.message!!}]")
            }
        }

        fun onResume(logger: ILogger) {
            getSignerInstance()
            if (signerInstance == null) {
                return
            }
            try {
                Reflection.invokeInstanceMethod(signerInstance!!, "onResume", null)
            } catch (e: Exception) {
                logger.warn("Invoking Signer onResume() received an error [${e.message!!}]")
                logger.report("Invoking Signer onResume() received an error [${e.message!!}]")
            }
        }

        fun sign(
            parameters: HashMap?, activityKind: String?, clientSdk: String?,
            context: Context?, logger: ILogger
        ) {
            getSignerInstance()
            if (signerInstance == null) {
                return
            }
            try {
                Reflection.invokeInstanceMethod(
                    signerInstance!!, "sign", arrayOf(
                        Context::class.java,
                        MutableMap::class.java,
                        String::class.java,
                        String::class.java
                    ),
                    context, parameters, activityKind, clientSdk
                )
            } catch (e: Exception) {
                logger.warn("Invoking Signer sign() for ${activityKind!!}received an error [${e.message!!}]")
                logger.report("Invoking Signer sign() for ${activityKind!!}received an error [${e.message!!}]")
            }
        }

        private fun getSignerInstance() {
            if (signerInstance == null) {
                synchronized(WiseTrackSigner::class.java) {
                    if (signerInstance == null) {
                        signerInstance =
                            Reflection.createDefaultInstance("com.wisetrack.sdk.sig.Signer")
                    }
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy