MainActivity.java 12 KB

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