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.cege.games.release.MainActivity; import com.jc.jcfw.google.PayClient; import org.cocos2dx.lib.CocosJSHelper; 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 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"; } i.setData(Uri.parse(downloadUrl)); MainActivity.app.startActivity(i); } } public static void showQRCode(String funid, String content) { MainActivity.app.showQRCode(funid, 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); } 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 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 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); } /** * 回调至js */ public static void nativeCb(String funId, String error, String dataStr) { JSONObject result = new JSONObject(); try { if (error != null && !error.isEmpty()) { result.put("errcode", 1); result.put("errmessage", error); } else { result.put("errcode", 0); result.put("data", dataStr); } } catch (JSONException e) { Log.e(TAG, "JSONException: " + e.getMessage()); } if (funId == null || funId.isEmpty()) { funId = MainActivity.app.getFunId(); } JcSDK.runJS(funId, "jniCallback", result.toString()); } }