231 lines
6.9 KiB
Kotlin
231 lines
6.9 KiB
Kotlin
package com.jc.jcfw
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.content.ActivityNotFoundException
|
|
import android.content.Intent
|
|
import android.net.Uri
|
|
import android.util.Log
|
|
import com.ctf.games.release.MainActivity
|
|
import com.ctf.games.release.MainApplication
|
|
import com.ctf.games.release.ui.UIManager.Companion.singleton
|
|
import com.ctf.games.release.webpage.events.CallJSEvent
|
|
import com.ctf.games.release.webpage.events.ProxyCBEvent
|
|
import com.google.common.base.Strings
|
|
import com.jc.jcfw.google.PayClient
|
|
import com.jc.jcfw.util.ThreadUtils
|
|
import com.jc.jcfw.util.UIUtils
|
|
import org.cocos2dx.lib.CocosJSHelper
|
|
import org.greenrobot.eventbus.EventBus
|
|
import org.json.JSONException
|
|
import org.json.JSONObject
|
|
import java.util.Arrays
|
|
|
|
const val FUNID_PREFIX = "webpage_"
|
|
class JcSDK {
|
|
companion object {
|
|
external fun runJS(funId: String?, methodName: String, params: Array<String?>): Int
|
|
external fun decryptPass(account: String?, pass: String?): String?
|
|
external fun tick(dt: Float)
|
|
|
|
private val TAG = JcSDK::class.java.simpleName
|
|
private var commonCB: UnityCallback? = null
|
|
@SuppressLint("StaticFieldLeak")
|
|
private var payClient: PayClient? = null
|
|
|
|
@JvmStatic
|
|
fun initCommonCB(callBack: UnityCallback?) {
|
|
Log.i(TAG, "call init common callback from unity")
|
|
commonCB = callBack
|
|
}
|
|
|
|
/**
|
|
* @Deprecated
|
|
* @param password
|
|
*/
|
|
@JvmStatic
|
|
fun initWallet(password: String) {
|
|
Log.i(TAG, "call init wallet from unity with password: $password")
|
|
CocosJSHelper.initWallet(password)
|
|
commonCB!!.nativeCallback("", "wallet init success", 0)
|
|
}
|
|
|
|
/**
|
|
* 回调至c#
|
|
*/
|
|
@JvmStatic
|
|
fun csCallback(funId: String, msg: String?) {
|
|
if (funId != "" && funId.indexOf("js_") == 0) {
|
|
commonCB!!.nativeCallback(funId, msg, 1)
|
|
} else {
|
|
commonCB!!.nativeCallback(funId, msg, 0)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* check if metamask installed and jump to metamask
|
|
*
|
|
* @param url
|
|
* sample:
|
|
* "https://metamask.app.link/wc?uri="+ExampleApplication.config.toWCUri();
|
|
*/
|
|
@JvmStatic
|
|
fun toWallet(url: String) {
|
|
val intent = Intent(Intent.ACTION_VIEW)
|
|
Log.i(TAG, url)
|
|
try {
|
|
intent.data = Uri.parse(url)
|
|
MainActivity.app!!.startActivity(intent)
|
|
} catch (e: ActivityNotFoundException) {
|
|
val i = Intent(Intent.ACTION_VIEW)
|
|
var downloadUrl = "https://metamask.io/download/"
|
|
if (url.startsWith("imtokenv2")) {
|
|
downloadUrl = "https://token.im/download"
|
|
} else if (url.startsWith("okx://")) {
|
|
downloadUrl = "https://www.okx.com/download"
|
|
}
|
|
i.data = Uri.parse(downloadUrl)
|
|
MainActivity.app!!.startActivity(i)
|
|
}
|
|
}
|
|
|
|
@JvmStatic
|
|
fun showQRCode(funid: String?, content: String?) {
|
|
UIUtils.showQRCode(MainActivity.app, content, "")
|
|
}
|
|
|
|
@JvmStatic
|
|
fun showWebPage(funid: String?, url: String?) {
|
|
MainActivity.app!!.showPage(funid, url)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun scanQRCode(funid: String?, title: String?) {
|
|
// MainActivity.app.showQRScan(funid, title);
|
|
singleton!!.showQRScan(funid, title)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun signWithTiktok(funid: String?) {
|
|
MainActivity.app!!.signWithTiktok(funid!!)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun signWithFacebook(funid: String?) {
|
|
MainActivity.app!!.signWithFacebook(funid!!)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun shareWithFacebook(content: String?) {
|
|
MainActivity.app!!.shareWithFacebook(content)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun signWithTwitter(funid: String?) {
|
|
MainActivity.app!!.signWithTwitter(funid!!)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun signWithGoogle(funid: String?) {
|
|
MainActivity.app!!.signWithGoogle(funid)
|
|
}
|
|
@JvmStatic
|
|
fun signWithApple(funid: String?) {
|
|
MainActivity.app!!.signWithApple(funid!!)
|
|
}
|
|
@JvmStatic
|
|
fun signOutGoogle(funid: String?) {
|
|
MainActivity.app!!.signOutGoogle(funid)
|
|
}
|
|
@JvmStatic
|
|
fun logEvent(content: String?) {
|
|
MainActivity.app!!.logEvent(content)
|
|
}
|
|
@JvmStatic
|
|
fun queryProducts(funid: String?, skuListStr: String) {
|
|
Log.i(TAG, "queryProducts with: $skuListStr")
|
|
if (payClient == null) {
|
|
payClient = PayClient.instance
|
|
}
|
|
val skuList: MutableList<String> = ArrayList()
|
|
if (skuListStr.contains(",")) {
|
|
val skuArr = skuListStr.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
|
skuList.addAll(listOf(*skuArr))
|
|
} else {
|
|
skuList.add(skuListStr)
|
|
}
|
|
payClient!!.queryProductList(funid, skuList)
|
|
}
|
|
@JvmStatic
|
|
fun buyProduct(funid: String?, productId: String, orderId: String?) {
|
|
Log.i(TAG, "buyProduct with: $productId")
|
|
if (payClient == null) {
|
|
payClient = PayClient.instance
|
|
}
|
|
payClient!!.buyOne(funid, productId, orderId)
|
|
}
|
|
@JvmStatic
|
|
fun queryPurchase(funid: String?) {
|
|
Log.i(TAG, "queryPurchase")
|
|
if (payClient == null) {
|
|
payClient = PayClient.instance
|
|
}
|
|
payClient!!.queryPurchase(funid)
|
|
}
|
|
@JvmStatic
|
|
fun passStorageState(funid: String?, account: String) {
|
|
Log.i(TAG, "passStorageState with: $account")
|
|
MainActivity.app!!.passStorageState(funid, account)
|
|
}
|
|
@JvmStatic
|
|
fun storagePass(funid: String?, account: String?, password: String?) {
|
|
MainActivity.app!!.storagePass(funid, account, password)
|
|
}
|
|
@JvmStatic
|
|
fun authGetStoragePass(funid: String?, account: String?) {
|
|
MainActivity.app!!.authGetStoragePass(funid, account!!)
|
|
}
|
|
@JvmStatic
|
|
fun storageGameData(data: String?) {
|
|
MainApplication.application!!.gameData = data
|
|
}
|
|
@JvmStatic
|
|
fun getClientId(funid: String?) {
|
|
Log.i(TAG, "getClientId ")
|
|
MainActivity.app!!.getClientId(funid)
|
|
}
|
|
@JvmStatic
|
|
fun onProxyCB(funId: String?, data: String?) {
|
|
EventBus.getDefault().post(ProxyCBEvent(funId!!, data!!))
|
|
}
|
|
|
|
fun nativeCb(funId: String?, error: String?, dataStr: String?) {
|
|
val result = JSONObject()
|
|
try {
|
|
if (Strings.isNullOrEmpty(error)) {
|
|
result.put("errcode", 0)
|
|
result.put("data", dataStr)
|
|
} else {
|
|
result.put("errcode", 1)
|
|
result.put("errmessage", error)
|
|
}
|
|
} catch (e: JSONException) {
|
|
Log.e(TAG, "JSONException: " + e.message)
|
|
}
|
|
Log.i(TAG, String.format("%s native cb, error: %s, data: %s", funId, error, dataStr))
|
|
if (funId != null && funId.startsWith(FUNID_PREFIX)) {
|
|
EventBus.getDefault().post(CallJSEvent(funId, result.toString()))
|
|
} else {
|
|
ThreadUtils.runInMain { runJS(funId, "jniCallback", arrayOf(result.toString())) }
|
|
}
|
|
}
|
|
|
|
fun callNativeJS(funId: String?, methodName: String, params: Array<String?>) {
|
|
ThreadUtils.runInMain { runJS(funId, methodName, params) }
|
|
}
|
|
|
|
fun nativeCb(result: NativeResult) {
|
|
nativeCb(result.funid, result.error, result.dataStr)
|
|
}
|
|
}
|
|
|
|
} |