MainActivity.java 14 KB

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