123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package io.nexilis.service
- import android.app.Activity
- import android.content.Context
- import android.content.Intent
- import android.os.Build
- import android.util.Log
- import dagger.hilt.android.EntryPointAccessors
- import io.newuniverse.SDK.nuSDKService
- import io.nexilis.service.core.ApiEntry
- import io.nexilis.service.core.DaggerIncomingComponent
- import io.nexilis.service.core.Data
- import io.nexilis.service.core.getSharedPreferences
- import io.nexilis.service.core.optString
- import io.nexilis.service.core.put
- import kotlinx.coroutines.launch
- import java.security.SecureRandom
- import java.util.concurrent.TimeUnit
- import java.util.concurrent.locks.Condition
- import java.util.concurrent.locks.ReentrantLock
- import kotlin.concurrent.withLock
- class Api {
- companion object {
- internal val lock = ReentrantLock()
- internal var condition: Condition? = null
- fun connect(account: String, activity: Activity) {
- Log.d(tag, "connect:$account")
- val sdkCallback = SdkCallback()
- DaggerIncomingComponent.builder()
- .context(activity)
- .appDependencies(
- EntryPointAccessors.fromApplication(
- activity.applicationContext,
- ApiEntry.IncomingInterface::class.java
- )
- )
- .build()
- .inject(sdkCallback)
- Thread {
- try {
- activity.startService(Intent(activity, nuSDKService::class.java))
- while (!nuSDKService.bServiceStarted()) {
- Thread.sleep(100)
- }
- val preferences = activity.getSharedPreferences()
- preferences.put(key = "api", value = account)
- val random = String.format("%010d", SecureRandom().nextInt(999999))
- val session = preferences.optString(
- key = "session", default = random
- )
- Log.d(tag, "initConnection:$pass,$account,$activity,$session")
- nuSDKService.getInstance(pass).initConnection(
- pass,
- account,
- activity,
- SdkCallback::class.java,
- sdkCallback,
- "202.158.33.26",
- 62823,
- session,
- "09:00",
- false,
- "202.158.33.26:62823"
- )
- val signature: String =
- nuSDKService.getInstance(pass).sGetSignature()
- if ("1,1,1,1,1,1,1,1,1,1,1,1" != signature) nuSDKService.getInstance(pass)
- .UpdateSignature()
- val pin = preferences.optString(key = "pin", default = "")
- if (pin.isEmpty()) {
- condition = lock.newCondition()
- lock.withLock {
- Log.d(tag, "wait connection")
- condition?.let {
- val await = it.await(15, TimeUnit.SECONDS)
- preferences.put(key = "session", value = session)
- Log.d(tag, "signup:start:$await")
- val response = Service.sendSync(
- Data(
- code = "SUA01",
- status = System.nanoTime().toString(),
- bodies = mutableMapOf("Api" to account)
- )
- )
- Log.d(tag, "signup:response:$response")
- response?.let { r ->
- Log.d(tag, "signup:isOk:${r.isOk()}")
- if (r.isOk()) {
- val p = r.bodies["A00"] ?: ""
- Log.d(tag, "signup:put:pin:$p")
- preferences.put(key = "pin", value = p)
- Log.d(tag, "signup:retrieve")
- Service.sendAsync(
- Data(
- code = "A050",
- status = System.nanoTime().toString(),
- )
- )
- }
- }
- }
- }
- }
- Log.d(tag, "user pin: ${preferences.optString(key = "pin", default = "")}")
- } catch (e: Exception) {
- Log.e(tag, e.message, e)
- }
- }.start()
- }
- internal fun sendToken(context: Context, token: String) {
- if (context.getSharedPreferences().getBoolean("is_login", false)) {
- context.getSharedPreferences().getString("pin", null)?.let {
- apiScope.launch {
- Service.sendAsync(
- Data(
- code = "ATO",
- status = System.nanoTime().toString(),
- f_pin = it,
- bodies = mutableMapOf(
- "TKN" to token,
- "Bo" to Build.BRAND
- )
- )
- )
- }
- }
- }
- }
- fun version(): String {
- return nuSDKService.getInstance(pass).sGetVersion()
- }
- fun isConnected(): Boolean {
- return nuSDKService.getInstance(pass).nGetCLXConnState() == 1.toByte()
- }
- }
- }
|