| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package com.wenting.youyiplugin;
- import android.app.Activity;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.content.Intent;
- import androidx.annotation.NonNull;
- import com.unity3d.player.UnityPlayer;
- import com.unity3d.player.UnityPlayerActivity;
- import com.youyi.yysdk.YouYi;
- import com.youyi.yysdk.callback.ExitCallBack;
- import com.youyi.yysdk.callback.LoginCallBack;
- import com.youyi.yysdk.callback.PayStatusCallBack;
- import java.util.HashMap;
- public class MainActivity extends UnityPlayerActivity implements Handler.Callback
- {
- private static final int MSG_INIT = 101;
- private static final int MSG_LOGIN = 102;
- private static final int MSG_SWITCH_ACCOUNT = 103;
- private static final int MSG_PAY = 104;
- private static final int MSG_CREATE_ROLE_LOG = 105;
- private static final int MSG_LOGIN_ROLE_LOG = 106;
- private static final int MSG_LEVEL_LOG = 107;
- private static final int MSG_REPORT_ACTION = 108;
- private static final int MSG_EXIT = 110;
- private final String LOG = "YOUYI_PLUGIN";
- private String gameObjectName;
- private boolean mInited = false;
- private Handler mHandler = new Handler(this);
- private LoginCallBack mLoginCallBack = new LoginCallBack()
- {
- @Override
- public void login(String s) {
- Log.d(LOG, "login");
- callUnityFunc("OnLoginSuccess", s);
- }
- @Override
- public void switchAccount() {
- Log.d(LOG, "switchAccount");
- callUnityFunc("OnLogoutSuccess", "");
- }
- };
- private ExitCallBack mExitCallBack = new ExitCallBack()
- {
- @Override
- public void exit() {
- Log.d(LOG, "exit");
- callUnityFunc("OnExitSuccess", "");
- }
- };
- private PayStatusCallBack mPayStatusCallBack = new PayStatusCallBack()
- {
- @Override
- public void paySuccessful() {
- //支付成功
- Log.d(LOG, "支付成功");
- callUnityFunc("OnPaySuccess", "");
- }
- @Override
- public void closePay() {
- //取消支付
- Log.d(LOG, "取消支付");
- callUnityFunc("OnPayFailed", "");
- }
- @Override
- public void payFailed() {
- //支付失败
- Log.d(LOG, "支付失败");
- callUnityFunc("OnPayFailed", "");
- }
- };
- private void callUnityFunc(String funcName, String paramStr)
- {
- if (TextUtils.isEmpty(this.gameObjectName)) {
- Log.e(LOG, "gameObject is null, please set gameObject first");
- return;
- }
- UnityPlayer.UnitySendMessage(this.gameObjectName, funcName, paramStr);
- }
- public boolean handleMessage(Message msg)
- {
- switch (msg.what)
- {
- case MSG_INIT:
- HashMap<String, String> initObj = (HashMap<String, String>)msg.obj;
- YouYi.getInstance().init(this, initObj.get("gameId"), initObj.get("appKey"), initObj.get("gameVersion"));
- mInited = true;
- callUnityFunc("OnInitSuccess", "");
- break;
- case MSG_LOGIN:
- YouYi.getInstance().login(this, mLoginCallBack);
- break;
- case MSG_SWITCH_ACCOUNT:
- YouYi.getInstance().switchAccountLogin();
- break;
- case MSG_PAY: {
- HashMap<String, Object> payObj = (HashMap<String, Object>) msg.obj;
- String cpOrderId = (String) payObj.get("cpOrderId");
- String amount = (String) payObj.get("amount");
- String goodsId = (String) payObj.get("goodsId");
- String goodsName = (String) payObj.get("goodsName");
- String roleId = (String) payObj.get("roleId");
- String roleName = (String) payObj.get("roleName");
- String serverId = (String) payObj.get("serverId");
- String serverName = (String) payObj.get("serverName");
- int level = (int) payObj.get("level");
- String extendParams = (String) payObj.get("extendParams");
- YouYi.getInstance().pay(cpOrderId, amount, goodsId, goodsName, roleId, roleName, serverId, serverName, level, extendParams, mPayStatusCallBack);
- break;
- }
- case MSG_CREATE_ROLE_LOG: {
- HashMap<String, Object> createRoleLogObj = (HashMap<String, Object>) msg.obj;
- String roleId = (String) createRoleLogObj.get("roleId");
- String roleName = (String) createRoleLogObj.get("roleName");
- String serverId = (String) createRoleLogObj.get("serverId");
- String serverName = (String) createRoleLogObj.get("serverName");
- int level = (int) createRoleLogObj.get("level");
- String spare = (String) createRoleLogObj.get("spare");
- YouYi.getInstance().creatingRole(roleId, roleName, serverId, serverName, level, spare);
- break;
- }
- case MSG_LOGIN_ROLE_LOG: {
- HashMap<String, Object> createRoleLogObj = (HashMap<String, Object>) msg.obj;
- String roleId = (String) createRoleLogObj.get("roleId");
- String roleName = (String) createRoleLogObj.get("roleName");
- String serverId = (String) createRoleLogObj.get("serverId");
- String serverName = (String) createRoleLogObj.get("serverName");
- int level = (int) createRoleLogObj.get("level");
- String spare = (String) createRoleLogObj.get("spare");
- YouYi.getInstance().loginRole(roleId, roleName, serverId, serverName, level, spare);
- break;
- }
- case MSG_LEVEL_LOG: {
- HashMap<String, Object> levelLogObj = (HashMap<String, Object>) msg.obj;
- String roleId = (String) levelLogObj.get("roleId");
- String roleName = (String) levelLogObj.get("roleName");
- String serverId = (String) levelLogObj.get("serverId");
- String serverName = (String) levelLogObj.get("serverName");
- int level = (int) levelLogObj.get("level");
- String spare = (String) levelLogObj.get("spare");
- YouYi.getInstance().levelLog(roleId, roleName, serverId, serverName, level, spare);
- break;
- }
- case MSG_REPORT_ACTION: {
- HashMap<String, Object> reportActionObj = (HashMap<String, Object>) msg.obj;
- if (reportActionObj.containsKey("event"))
- {
- YouYi.getInstance().actionReport((String)reportActionObj.get("event"), reportActionObj);
- }
- break;
- }
- case MSG_EXIT:
- YouYi.getInstance().exit(mExitCallBack);
- break;
- }
- return false;
- }
- public void init(String gameObjectName)
- {
- this.gameObjectName = gameObjectName;
- if (mInited)
- {
- callUnityFunc("OnInitSuccess", "");
- return;
- }
- String gameId = "";
- String appKey = "";
- String gameVersion = "";
- try {
- PackageManager packageManager = this.getPackageManager();
- ApplicationInfo appInfo = packageManager.getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
- Bundle metaData = appInfo.metaData;
- gameId = String.valueOf(metaData.getInt("YouYi_GameId"));
- appKey = metaData.getString("YouYi_AppKey");
- PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
- if (metaData.containsKey("LEBIAN_VERCODE"))
- {
- int lebianVersion = metaData.getInt("LEBIAN_VERCODE");
- int version = packageInfo.versionCode;
- if (lebianVersion > version)
- {
- int major = lebianVersion / 1000000;
- lebianVersion = lebianVersion - major * 1000000;
- int minor = lebianVersion / 10000;
- lebianVersion = lebianVersion - minor * 10000;
- int release = lebianVersion / 100;
- lebianVersion = lebianVersion - release * 100;
- int patch = lebianVersion;
- gameVersion = major + "." + minor + "." + release + "." + patch;
- }
- else
- {
- gameVersion = packageInfo.versionName;
- }
- }
- else
- {
- gameVersion = packageInfo.versionName;
- }
- } catch (Exception e) {
- e.printStackTrace();
- callUnityFunc("OnInitFailed", "");
- return;
- }
- Message msg = mHandler.obtainMessage(MSG_INIT);
- HashMap<String, String> mapObj = new HashMap<>();
- mapObj.put("gameId", gameId);
- mapObj.put("appKey", appKey);
- mapObj.put("gameVersion", gameVersion);
- msg.obj = mapObj;
- msg.sendToTarget();
- }
- public void login()
- {
- mHandler.sendEmptyMessage(MSG_LOGIN);
- }
- public void switchAccountLogin()
- {
- mHandler.sendEmptyMessage(MSG_SWITCH_ACCOUNT);
- }
- public void pay(String cpOrderId, String amount,
- String goodsId, String goodsName,
- String roleId, String roleName,
- String serverId, String serverName,
- int level, String extendParams)
- {
- Message msg = mHandler.obtainMessage(MSG_PAY);
- HashMap<String, Object> mapObj = new HashMap<>();
- mapObj.put("cpOrderId", cpOrderId);
- mapObj.put("amount", amount);
- mapObj.put("goodsId", goodsId);
- mapObj.put("goodsName", goodsName);
- mapObj.put("roleId", roleId);
- mapObj.put("roleName", roleName);
- mapObj.put("serverId", serverId);
- mapObj.put("serverName", serverName);
- mapObj.put("level", level);
- mapObj.put("extendParams", extendParams);
- msg.obj = mapObj;
- msg.sendToTarget();
- }
- public void createRoleLog(String roleId, String roleName,
- String serverId, String serverName,
- int level, String spare)
- {
- Message msg = mHandler.obtainMessage(MSG_CREATE_ROLE_LOG);
- HashMap<String, Object> mapObj = new HashMap<>();
- mapObj.put("roleId", roleId);
- mapObj.put("roleName", roleName);
- mapObj.put("serverId", serverId);
- mapObj.put("serverName", serverName);
- mapObj.put("level", level);
- mapObj.put("spare", spare);
- msg.obj = mapObj;
- msg.sendToTarget();
- }
- public void loginRoleLog(String roleId, String roleName,
- String serverId, String serverName,
- int level, String spare)
- {
- Message msg = mHandler.obtainMessage(MSG_LOGIN_ROLE_LOG);
- HashMap<String, Object> mapObj = new HashMap<>();
- mapObj.put("roleId", roleId);
- mapObj.put("roleName", roleName);
- mapObj.put("serverId", serverId);
- mapObj.put("serverName", serverName);
- mapObj.put("level", level);
- mapObj.put("spare", spare);
- msg.obj = mapObj;
- msg.sendToTarget();
- }
- public void levelLog(String roleId, String roleName,
- String serverId, String serverName,
- int level, String spare)
- {
- Message msg = mHandler.obtainMessage(MSG_LEVEL_LOG);
- HashMap<String, Object> mapObj = new HashMap<>();
- mapObj.put("roleId", roleId);
- mapObj.put("roleName", roleName);
- mapObj.put("serverId", serverId);
- mapObj.put("serverName", serverName);
- mapObj.put("level", level);
- mapObj.put("spare", spare);
- msg.obj = mapObj;
- msg.sendToTarget();
- }
- public void reportAction(HashMap<String, Object> params)
- {
- Message msg = mHandler.obtainMessage(MSG_REPORT_ACTION);
- msg.obj = params;
- msg.sendToTarget();
- }
- public void exit()
- {
- mHandler.sendEmptyMessage(MSG_EXIT);
- }
- // Setup activity layout
- @Override protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- YouYi.getInstance().onCreate();
- }
- // Quit Unity
- @Override protected void onDestroy ()
- {
- super.onDestroy();
- YouYi.getInstance().onDestroy();
- }
- // Pause Unity
- @Override protected void onPause()
- {
- super.onPause();
- YouYi.getInstance().onPause();
- }
- // Resume Unity
- @Override protected void onResume()
- {
- super.onResume();
- YouYi.getInstance().onResume();
- }
- @Override protected void onStart()
- {
- super.onStart();
- YouYi.getInstance().onStart();
- }
- @Override protected void onStop()
- {
- super.onStop();
- YouYi.getInstance().onStop();
- }
- @Override protected void onRestart()
- {
- super.onRestart();
- YouYi.getInstance().onRestart();
- }
- @Override protected void onNewIntent(Intent intent)
- {
- super.onNewIntent(intent);
- YouYi.getInstance().onNewIntent(intent);
- }
- @Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- YouYi.getInstance().onActivityResult(requestCode, resultCode, data);
- }
- @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
- {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- YouYi.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
- }
- }
|