MainActivity.java 14 KB

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