2024-02-20 14:02:09 +08:00

255 lines
7.7 KiB
Java

package com.jc.jcfw;
import static com.cege.games.release.Constants.FUNID_PREFIX;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.cege.games.release.MainActivity;
import com.cege.games.release.MainApplication;
import com.cege.games.release.ui.UIManager;
import com.cege.games.release.webpage.events.CallJSEvent;
import com.cege.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 com.unity3d.player.UnityPlayer;
import org.cocos2dx.lib.CocosJSHelper;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class JcSDK {
private static final String TAG = JcSDK.class.getSimpleName();
private static UnityCallback commonCB;
@SuppressLint("StaticFieldLeak")
private static PayClient payClient;
private static native int runJS(final String funId, final String methodName, final String[] params);
public static native String decryptPass(final String account, final String pass);
public static native void tick(float dt);
public static void initCommonCB(UnityCallback callBack) {
Log.i(TAG, "call init common callback from unity");
commonCB = callBack;
}
/**
* @Deprecated
* 不使用该方法, 直接由unity调用cpp方法
* @param password
*/
public static void initWallet(String password) {
Log.i(TAG, "call init wallet from unity with password: " + password);
CocosJSHelper.initWallet(password);
commonCB.nativeCallback("", "wallet init success", 0);
}
/**
* 回调至c#
*/
public static void csCallback(String funId, String msg) {
if (!Objects.equals(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();
*/
public static void toWallet(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Log.i(TAG, url);
try {
intent.setData(Uri.parse(url));
MainActivity.app.startActivity(intent);
} catch (ActivityNotFoundException e) {
Intent i = new Intent(Intent.ACTION_VIEW);
String 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.setData(Uri.parse(downloadUrl));
MainActivity.app.startActivity(i);
}
}
public static void showQRCode(String funid, String content) {
UIUtils.showQRCode(MainActivity.app, content, "");
}
public static void showWebPage(String funid, String url) {
MainActivity.app.showPage(funid, url);
}
public static void scanQRCode(String funid, String title) {
// MainActivity.app.showQRScan(funid, title);
UIManager.getSingleton().showQRScan(funid, title);
}
public static void signWithTiktok(String funid) {
MainActivity.app.signWithTiktok(funid);
}
public static void signWithFacebook(String funid) {
MainActivity.app.signWithFacebook(funid);
}
public static void shareWithFacebook(String content) {
MainActivity.app.shareWithFacebook(content);
}
public static void signWithTwitter(String funid) {
MainActivity.app.signWithTwitter(funid);
}
public static void signWithGoogle(String funid) {
MainActivity.app.signWithGoogle(funid);
}
public static void signWithApple(String funid) {
// MainActivity.app.signWithApple(funid);
}
public static void signWithOAuth(String funid, String jsonData) {
MainActivity.app.oauthLogin(funid, jsonData);
}
public static void signOutGoogle(String funid) {
MainActivity.app.signOutGoogle(funid);
}
public static void logEvent(String content) {
MainActivity.app.logEvent(content);
}
public static void queryProducts(String funid, String skuListStr) {
Log.i(TAG, "queryProducts with: " + skuListStr);
if (payClient == null) {
payClient = PayClient.getInstance();
}
List<String> skuList = new ArrayList<>();
if (skuListStr.contains(",")) {
String[] skuArr = skuListStr.split(",");
skuList.addAll(Arrays.asList(skuArr));
} else {
skuList.add(skuListStr);
}
payClient.queryProductList(funid, skuList);
}
public static void buyProduct(String funid, String productId, String orderId) {
Log.i(TAG, "buyProduct with: " + productId);
if (payClient == null) {
payClient = PayClient.getInstance();
}
payClient.buyOne(funid, productId, orderId);
}
public static void queryPurchase(String funid) {
Log.i(TAG, "queryPurchase");
if (payClient == null) {
payClient = PayClient.getInstance();
}
payClient.queryPurchase(funid);
}
public static void passStorageState(String funid, String account) {
Log.i(TAG, "passStorageState with: " + account);
MainActivity.app.passStorageState(funid, account);
}
public static void storagePass(String funid, String account, String password) {
MainActivity.app.storagePass(funid, account, password);
}
public static void authGetStoragePass(String funid, String account) {
MainActivity.app.authGetStoragePass(funid, account);
}
public static void storageGameData(String data) {
MainApplication.application.setGameData(data);
}
public static void getClientId(String funid) {
Log.i(TAG, "getClientId ");
MainActivity.app.getClientId(funid);
}
public static void onProxyCB(String funId, String data) {
EventBus.getDefault().post(new ProxyCBEvent(funId, data));
}
public static void nativeCb(String funId, String error, String dataStr) {
JSONObject result = new JSONObject();
try {
if (Strings.isNullOrEmpty(error)) {
result.put("errcode", 0);
result.put("data", dataStr);
} else {
result.put("errcode", 1);
result.put("errmessage", error);
}
} catch (JSONException e) {
Log.e(TAG, "JSONException: " + e.getMessage());
}
if (funId == null || funId.isEmpty()) {
funId = MainActivity.app.getFunId();
}
Log.i(TAG, String.format("%s native cb, error: %s, data: %s", funId, error, dataStr));
if (funId.startsWith(FUNID_PREFIX)) {
EventBus.getDefault().post(new CallJSEvent(funId, result.toString()));
} else {
String finalFunId = funId;
ThreadUtils.runInMain(() -> JcSDK.runJS(finalFunId, "jniCallback", new String[]{result.toString()}));
}
}
public static void callNativeJS(String funId, String methodName, String[] params) {
ThreadUtils.runInMain(() -> JcSDK.runJS(funId, methodName, params));
}
public static void nativeCb(NativeResult result) {
nativeCb(result.getFunid(), result.getError(), result.getDataStr());
}
public static void toUnity(String funId, String error, String dataStr) {
JSONObject result = new JSONObject();
try {
result.put("funid", funId);
if (Strings.isNullOrEmpty(error)) {
result.put("errcode", 0);
result.put("data", dataStr);
} else {
result.put("errcode", 1);
result.put("errmessage", error);
}
} catch (JSONException e) {
Log.e(TAG, "JSONException: " + e.getMessage());
}
UnityPlayer.UnitySendMessage("WalletPanel1", "onNativeCallback", result.toString());
}
}