MainActivity.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package com.wenting.youyiplugin;
  2. import android.app.Activity;
  3. import android.content.pm.ApplicationInfo;
  4. import android.content.pm.PackageInfo;
  5. import android.content.pm.PackageManager;
  6. import android.content.res.Configuration;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.text.TextUtils;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.content.Intent;
  14. import androidx.annotation.NonNull;
  15. import com.unity3d.player.UnityPlayer;
  16. import com.unity3d.player.UnityPlayerActivity;
  17. import com.youyi.yysdk.YouYi;
  18. import com.youyi.yysdk.callback.ExitCallBack;
  19. import com.youyi.yysdk.callback.LoginCallBack;
  20. import com.youyi.yysdk.callback.PayStatusCallBack;
  21. import com.youyi.yysdk.callback.ServerCanEnterCallBack;
  22. import java.util.HashMap;
  23. public class MainActivity extends UnityPlayerActivity implements Handler.Callback
  24. {
  25. private static final int MSG_INIT = 101;
  26. private static final int MSG_LOGIN = 102;
  27. private static final int MSG_SWITCH_ACCOUNT = 103;
  28. private static final int MSG_PAY = 104;
  29. private static final int MSG_CREATE_ROLE_LOG = 105;
  30. private static final int MSG_LOGIN_ROLE_LOG = 106;
  31. private static final int MSG_LEVEL_LOG = 107;
  32. private static final int MSG_REPORT_ACTION = 108;
  33. private static final int MSG_CAN_ENTER_SERVER_JUDGE = 109;
  34. private static final int MSG_EXIT = 110;
  35. private final String LOG = "YOUYI_PLUGIN";
  36. private String gameObjectName;
  37. private boolean mInited = false;
  38. private Handler mHandler = new Handler(this);
  39. private LoginCallBack mLoginCallBack = new LoginCallBack()
  40. {
  41. @Override
  42. public void login(String s) {
  43. Log.d(LOG, "login");
  44. callUnityFunc("OnLoginSuccess", s);
  45. }
  46. @Override
  47. public void switchAccount() {
  48. Log.d(LOG, "switchAccount");
  49. callUnityFunc("OnLogoutSuccess", "");
  50. }
  51. };
  52. private ExitCallBack mExitCallBack = new ExitCallBack()
  53. {
  54. @Override
  55. public void exit() {
  56. Log.d(LOG, "exit");
  57. callUnityFunc("OnExitSuccess", "");
  58. }
  59. @Override
  60. public void cancelExit() {
  61. Log.d(LOG, "cancelExit");
  62. }
  63. };
  64. private PayStatusCallBack mPayStatusCallBack = new PayStatusCallBack()
  65. {
  66. @Override
  67. public void paySuccessful() {
  68. //支付成功
  69. Log.d(LOG, "支付成功");
  70. callUnityFunc("OnPaySuccess", "");
  71. }
  72. @Override
  73. public void closePay() {
  74. //取消支付
  75. Log.d(LOG, "取消支付");
  76. callUnityFunc("OnPayFailed", "");
  77. }
  78. @Override
  79. public void payFailed() {
  80. //支付失败
  81. Log.d(LOG, "支付失败");
  82. callUnityFunc("OnPayFailed", "");
  83. }
  84. };
  85. private ServerCanEnterCallBack mServerCanEnterCallBack = new ServerCanEnterCallBack()
  86. {
  87. @Override
  88. public void canEnterListen(boolean valid)
  89. {
  90. callUnityFunc("OnCanEnterServerJudge", "" + (valid ? 1 : 0));
  91. }
  92. };
  93. private void callUnityFunc(String funcName, String paramStr)
  94. {
  95. if (TextUtils.isEmpty(this.gameObjectName)) {
  96. Log.e(LOG, "gameObject is null, please set gameObject first");
  97. return;
  98. }
  99. UnityPlayer.UnitySendMessage(this.gameObjectName, funcName, paramStr);
  100. }
  101. public boolean handleMessage(Message msg)
  102. {
  103. switch (msg.what)
  104. {
  105. case MSG_INIT:
  106. HashMap<String, String> initObj = (HashMap<String, String>)msg.obj;
  107. YouYi.getInstance().init(this, initObj.get("gameId"), initObj.get("appKey"), initObj.get("gameVersion"));
  108. mInited = true;
  109. callUnityFunc("OnInitSuccess", "");
  110. break;
  111. case MSG_LOGIN:
  112. YouYi.getInstance().login(this, mLoginCallBack);
  113. break;
  114. case MSG_SWITCH_ACCOUNT:
  115. YouYi.getInstance().switchAccountLogin();
  116. break;
  117. case MSG_PAY: {
  118. HashMap<String, Object> payObj = (HashMap<String, Object>) msg.obj;
  119. String cpOrderId = (String) payObj.get("cpOrderId");
  120. String amount = (String) payObj.get("amount");
  121. String goodsId = (String) payObj.get("goodsId");
  122. String goodsName = (String) payObj.get("goodsName");
  123. String roleId = (String) payObj.get("roleId");
  124. String roleName = (String) payObj.get("roleName");
  125. String serverId = (String) payObj.get("serverId");
  126. String serverName = (String) payObj.get("serverName");
  127. int level = (int) payObj.get("level");
  128. String extendParams = (String) payObj.get("extendParams");
  129. YouYi.getInstance().pay(cpOrderId, amount, goodsId, goodsName, roleId, roleName, serverId, serverName, level, extendParams, mPayStatusCallBack);
  130. break;
  131. }
  132. case MSG_CREATE_ROLE_LOG: {
  133. HashMap<String, Object> createRoleLogObj = (HashMap<String, Object>) msg.obj;
  134. String roleId = (String) createRoleLogObj.get("roleId");
  135. String roleName = (String) createRoleLogObj.get("roleName");
  136. String serverId = (String) createRoleLogObj.get("serverId");
  137. String serverName = (String) createRoleLogObj.get("serverName");
  138. int level = (int) createRoleLogObj.get("level");
  139. String spare = (String) createRoleLogObj.get("spare");
  140. YouYi.getInstance().creatingRole(roleId, roleName, serverId, serverName, level, spare);
  141. break;
  142. }
  143. case MSG_LOGIN_ROLE_LOG: {
  144. HashMap<String, Object> createRoleLogObj = (HashMap<String, Object>) msg.obj;
  145. String roleId = (String) createRoleLogObj.get("roleId");
  146. String roleName = (String) createRoleLogObj.get("roleName");
  147. String serverId = (String) createRoleLogObj.get("serverId");
  148. String serverName = (String) createRoleLogObj.get("serverName");
  149. int level = (int) createRoleLogObj.get("level");
  150. String spare = (String) createRoleLogObj.get("spare");
  151. YouYi.getInstance().loginRole(roleId, roleName, serverId, serverName, level, spare);
  152. break;
  153. }
  154. case MSG_LEVEL_LOG: {
  155. HashMap<String, Object> levelLogObj = (HashMap<String, Object>) msg.obj;
  156. String roleId = (String) levelLogObj.get("roleId");
  157. String roleName = (String) levelLogObj.get("roleName");
  158. String serverId = (String) levelLogObj.get("serverId");
  159. String serverName = (String) levelLogObj.get("serverName");
  160. int level = (int) levelLogObj.get("level");
  161. String spare = (String) levelLogObj.get("spare");
  162. YouYi.getInstance().levelLog(roleId, roleName, serverId, serverName, level, spare);
  163. break;
  164. }
  165. case MSG_REPORT_ACTION: {
  166. HashMap<String, Object> reportActionObj = (HashMap<String, Object>) msg.obj;
  167. if (reportActionObj.containsKey("event"))
  168. {
  169. YouYi.getInstance().actionReport((String)reportActionObj.get("event"), reportActionObj);
  170. }
  171. break;
  172. }
  173. case MSG_CAN_ENTER_SERVER_JUDGE: {
  174. HashMap<String, Object> infoObj = (HashMap<String, Object>) msg.obj;
  175. String serverId = (String) infoObj.get("serverId");
  176. String serverName = (String) infoObj.get("serverName");
  177. YouYi.getInstance().serverCanEnter(serverId, serverName, mServerCanEnterCallBack);
  178. break;
  179. }
  180. case MSG_EXIT:
  181. YouYi.getInstance().exit(mExitCallBack);
  182. break;
  183. }
  184. return false;
  185. }
  186. public void init(String gameObjectName)
  187. {
  188. this.gameObjectName = gameObjectName;
  189. if (mInited)
  190. {
  191. callUnityFunc("OnInitSuccess", "");
  192. return;
  193. }
  194. String gameId = "";
  195. String appKey = "";
  196. String gameVersion = "";
  197. try {
  198. PackageManager packageManager = this.getPackageManager();
  199. ApplicationInfo appInfo = packageManager.getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
  200. Bundle metaData = appInfo.metaData;
  201. gameId = String.valueOf(metaData.getInt("YouYi_GameId"));
  202. appKey = metaData.getString("YouYi_AppKey");
  203. PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
  204. if (metaData.containsKey("LEBIAN_VERCODE"))
  205. {
  206. int lebianVersion = metaData.getInt("LEBIAN_VERCODE");
  207. int version = packageInfo.versionCode;
  208. if (lebianVersion > version)
  209. {
  210. int major = lebianVersion / 1000000;
  211. lebianVersion = lebianVersion - major * 1000000;
  212. int minor = lebianVersion / 10000;
  213. lebianVersion = lebianVersion - minor * 10000;
  214. int release = lebianVersion / 100;
  215. lebianVersion = lebianVersion - release * 100;
  216. int patch = lebianVersion;
  217. gameVersion = major + "." + minor + "." + release + "." + patch;
  218. }
  219. else
  220. {
  221. gameVersion = packageInfo.versionName;
  222. }
  223. }
  224. else
  225. {
  226. gameVersion = packageInfo.versionName;
  227. }
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. callUnityFunc("OnInitFailed", "");
  231. return;
  232. }
  233. Message msg = mHandler.obtainMessage(MSG_INIT);
  234. HashMap<String, String> mapObj = new HashMap<>();
  235. mapObj.put("gameId", gameId);
  236. mapObj.put("appKey", appKey);
  237. mapObj.put("gameVersion", gameVersion);
  238. msg.obj = mapObj;
  239. msg.sendToTarget();
  240. }
  241. public void login()
  242. {
  243. mHandler.sendEmptyMessage(MSG_LOGIN);
  244. }
  245. public void switchAccountLogin()
  246. {
  247. mHandler.sendEmptyMessage(MSG_SWITCH_ACCOUNT);
  248. }
  249. public void pay(String cpOrderId, String amount,
  250. String goodsId, String goodsName,
  251. String roleId, String roleName,
  252. String serverId, String serverName,
  253. int level, String extendParams)
  254. {
  255. Message msg = mHandler.obtainMessage(MSG_PAY);
  256. HashMap<String, Object> mapObj = new HashMap<>();
  257. mapObj.put("cpOrderId", cpOrderId);
  258. mapObj.put("amount", amount);
  259. mapObj.put("goodsId", goodsId);
  260. mapObj.put("goodsName", goodsName);
  261. mapObj.put("roleId", roleId);
  262. mapObj.put("roleName", roleName);
  263. mapObj.put("serverId", serverId);
  264. mapObj.put("serverName", serverName);
  265. mapObj.put("level", level);
  266. mapObj.put("extendParams", extendParams);
  267. msg.obj = mapObj;
  268. msg.sendToTarget();
  269. }
  270. public void createRoleLog(String roleId, String roleName,
  271. String serverId, String serverName,
  272. int level, String spare)
  273. {
  274. Message msg = mHandler.obtainMessage(MSG_CREATE_ROLE_LOG);
  275. HashMap<String, Object> mapObj = new HashMap<>();
  276. mapObj.put("roleId", roleId);
  277. mapObj.put("roleName", roleName);
  278. mapObj.put("serverId", serverId);
  279. mapObj.put("serverName", serverName);
  280. mapObj.put("level", level);
  281. mapObj.put("spare", spare);
  282. msg.obj = mapObj;
  283. msg.sendToTarget();
  284. }
  285. public void loginRoleLog(String roleId, String roleName,
  286. String serverId, String serverName,
  287. int level, String spare)
  288. {
  289. Message msg = mHandler.obtainMessage(MSG_LOGIN_ROLE_LOG);
  290. HashMap<String, Object> mapObj = new HashMap<>();
  291. mapObj.put("roleId", roleId);
  292. mapObj.put("roleName", roleName);
  293. mapObj.put("serverId", serverId);
  294. mapObj.put("serverName", serverName);
  295. mapObj.put("level", level);
  296. mapObj.put("spare", spare);
  297. msg.obj = mapObj;
  298. msg.sendToTarget();
  299. }
  300. public void levelLog(String roleId, String roleName,
  301. String serverId, String serverName,
  302. int level, String spare)
  303. {
  304. Message msg = mHandler.obtainMessage(MSG_LEVEL_LOG);
  305. HashMap<String, Object> mapObj = new HashMap<>();
  306. mapObj.put("roleId", roleId);
  307. mapObj.put("roleName", roleName);
  308. mapObj.put("serverId", serverId);
  309. mapObj.put("serverName", serverName);
  310. mapObj.put("level", level);
  311. mapObj.put("spare", spare);
  312. msg.obj = mapObj;
  313. msg.sendToTarget();
  314. }
  315. public void reportAction(HashMap<String, Object> params)
  316. {
  317. Message msg = mHandler.obtainMessage(MSG_REPORT_ACTION);
  318. msg.obj = params;
  319. msg.sendToTarget();
  320. }
  321. public void canEnterServerJudge(String serverId, String serverName)
  322. {
  323. Message msg = mHandler.obtainMessage(MSG_CAN_ENTER_SERVER_JUDGE);
  324. HashMap<String, Object> mapObj = new HashMap<>();
  325. mapObj.put("serverId", serverId);
  326. mapObj.put("serverName", serverName);
  327. msg.obj = mapObj;
  328. msg.sendToTarget();
  329. }
  330. public void exit()
  331. {
  332. mHandler.sendEmptyMessage(MSG_EXIT);
  333. }
  334. // Setup activity layout
  335. @Override protected void onCreate(Bundle savedInstanceState)
  336. {
  337. super.onCreate(savedInstanceState);
  338. YouYi.getInstance().onCreate(this, savedInstanceState);
  339. }
  340. // Quit Unity
  341. @Override protected void onDestroy ()
  342. {
  343. super.onDestroy();
  344. YouYi.getInstance().onDestroy();
  345. }
  346. // Pause Unity
  347. @Override protected void onPause()
  348. {
  349. super.onPause();
  350. YouYi.getInstance().onPause();
  351. }
  352. // Resume Unity
  353. @Override protected void onResume()
  354. {
  355. super.onResume();
  356. YouYi.getInstance().onResume();
  357. }
  358. @Override protected void onStart()
  359. {
  360. super.onStart();
  361. YouYi.getInstance().onStart();
  362. }
  363. @Override protected void onStop()
  364. {
  365. super.onStop();
  366. YouYi.getInstance().onStop();
  367. }
  368. @Override protected void onRestart()
  369. {
  370. super.onRestart();
  371. YouYi.getInstance().onRestart();
  372. }
  373. @Override protected void onNewIntent(Intent intent)
  374. {
  375. super.onNewIntent(intent);
  376. YouYi.getInstance().onNewIntent(intent);
  377. }
  378. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
  379. {
  380. super.onActivityResult(requestCode, resultCode, data);
  381. YouYi.getInstance().onActivityResult(requestCode, resultCode, data);
  382. }
  383. @Override protected void onSaveInstanceState(Bundle outState)
  384. {
  385. YouYi.getInstance().onSaveInstanceState(outState);
  386. }
  387. @Override protected void onRestoreInstanceState(Bundle savedInstanceState)
  388. {
  389. YouYi.getInstance().onRestoreInstanceState(savedInstanceState);
  390. }
  391. @Override public void onConfigurationChanged(Configuration newConfig)
  392. {
  393. YouYi.getInstance().onConfigurationChanged(newConfig);
  394. super.onConfigurationChanged(newConfig);
  395. }
  396. @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
  397. {
  398. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  399. YouYi.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
  400. }
  401. }