|
@@ -0,0 +1,308 @@
|
|
|
+package com.gamesdk.qingshi;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.text.InputType;
|
|
|
+import android.view.KeyEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.EditText;
|
|
|
+import android.widget.LinearLayout;
|
|
|
+import android.widget.ScrollView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import cn.qingshi.gamesdk.base.QSGameSdk;
|
|
|
+import cn.qingshi.gamesdk.base.entity.QSChargeInfo;
|
|
|
+import cn.qingshi.gamesdk.base.entity.QSRoleInfo;
|
|
|
+import cn.qingshi.gamesdk.base.internal.ICallback;
|
|
|
+import cn.yyxx.support.permission.Permission;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author #Suyghur.
|
|
|
+ * Created on 2021/05/24
|
|
|
+ */
|
|
|
+public class DemoActivity extends Activity implements View.OnClickListener {
|
|
|
+
|
|
|
+
|
|
|
+ private final List<Item> events = new ArrayList<Item>() {{
|
|
|
+ add(new Item(0, "00 账号登录"));
|
|
|
+ add(new Item(1, "01 账号登出"));
|
|
|
+ add(new Item(2, "02 角色创建上报"));
|
|
|
+ add(new Item(3, "03 角色登录上报"));
|
|
|
+ add(new Item(4, "04 角色升级上报"));
|
|
|
+ add(new Item(5, "05 定额充值"));
|
|
|
+ add(new Item(6, "06 权限申请"));
|
|
|
+ }};
|
|
|
+
|
|
|
+ private EditText editText;
|
|
|
+ private CacheRoleInfo.RoleInfo cacheRoleInfo = null;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ initView();
|
|
|
+ QSGameSdk.getInstance().initialize(this, true, new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ Toast.makeText(DemoActivity.this, result, Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initView() {
|
|
|
+ LinearLayout layout = new LinearLayout(this);
|
|
|
+ layout.setOrientation(LinearLayout.VERTICAL);
|
|
|
+ for (Item event : events) {
|
|
|
+ if (event.getId() == 5) {
|
|
|
+ editText = new EditText(this);
|
|
|
+ editText.setHint("请输入金额,单位分");
|
|
|
+ editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_PHONE);
|
|
|
+ editText.setText("100");
|
|
|
+ layout.addView(editText);
|
|
|
+ }
|
|
|
+
|
|
|
+ Button button = new Button(this);
|
|
|
+ button.setTag(event.getId());
|
|
|
+ button.setText(event.getName());
|
|
|
+ button.setOnClickListener(this);
|
|
|
+ layout.addView(button);
|
|
|
+ }
|
|
|
+
|
|
|
+ ScrollView scrollView = new ScrollView(this);
|
|
|
+ scrollView.addView(layout);
|
|
|
+ setContentView(scrollView);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ if (v == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ switch ((Integer) v.getTag()) {
|
|
|
+ case 0:
|
|
|
+ QSGameSdk.getInstance().login(this, new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = new JSONObject(result);
|
|
|
+ String openId = jsonObject.getString("open_id");
|
|
|
+ String gameToken = jsonObject.getString("game_token");
|
|
|
+ Toast.makeText(DemoActivity.this, "SDK登录成功\nopen id: " + openId + "\ngame token: " + gameToken, Toast.LENGTH_SHORT).show();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ cacheRoleInfo = CacheRoleInfo.getDemoRoleInfo(DemoActivity.this, QSGameSdk.getInstance().getCurrentOpenId());
|
|
|
+ } else {
|
|
|
+ Toast.makeText(DemoActivity.this, "账号登录失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ QSGameSdk.getInstance().logout(this, new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ Toast.makeText(DemoActivity.this, "账号登出成功", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ Toast.makeText(DemoActivity.this, "账号登出失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ if (QSGameSdk.getInstance().getCurrentOpenId().equals("")) {
|
|
|
+ Toast.makeText(DemoActivity.this, "请先登录账号", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ cacheRoleInfo = null;
|
|
|
+ cacheRoleInfo = CacheRoleInfo.setDemoRoleInfo(this, QSGameSdk.getInstance().getCurrentOpenId());
|
|
|
+ QSGameSdk.getInstance().roleCreate(this, getQSRoleInfo(), new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ Toast.makeText(DemoActivity.this, "角色信息上报成功", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ Toast.makeText(DemoActivity.this, "角色信息上报失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ if (QSGameSdk.getInstance().getCurrentOpenId().equals("")) {
|
|
|
+ Toast.makeText(DemoActivity.this, "请先登录账号", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ QSGameSdk.getInstance().roleLauncher(this, getQSRoleInfo(), new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ Toast.makeText(DemoActivity.this, "角色信息上报成功", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ Toast.makeText(DemoActivity.this, "角色信息上报失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ if (QSGameSdk.getInstance().getCurrentOpenId().equals("")) {
|
|
|
+ Toast.makeText(DemoActivity.this, "请先登录账号", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ QSGameSdk.getInstance().roleUpgrade(this, getQSRoleInfo(), new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ Toast.makeText(DemoActivity.this, "角色信息上报成功", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ Toast.makeText(DemoActivity.this, "角色信息上报失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ if (QSGameSdk.getInstance().getCurrentOpenId().equals("")) {
|
|
|
+ Toast.makeText(DemoActivity.this, "请先登录账号", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (cacheRoleInfo == null) {
|
|
|
+ Toast.makeText(DemoActivity.this, "请先创建/登录角色", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ QSGameSdk.getInstance().charge(this, getQSChargeInfo(), new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ Toast.makeText(DemoActivity.this, "支付流程完成", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ Toast.makeText(DemoActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ List<String> permissions = new ArrayList<String>() {{
|
|
|
+ add(Permission.CAMERA);
|
|
|
+ }};
|
|
|
+ QSGameSdk.getInstance().requestPermission(DemoActivity.this, permissions, new ICallback() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ Toast.makeText(DemoActivity.this, "requestPermission code: " + code + ", result: " + result, Toast.LENGTH_SHORT).show();
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private QSRoleInfo getQSRoleInfo() {
|
|
|
+ QSRoleInfo roleInfo = new QSRoleInfo();
|
|
|
+ roleInfo.setOpenId(QSGameSdk.getInstance().getCurrentOpenId());
|
|
|
+ roleInfo.setRoleId(cacheRoleInfo.getRoleId());
|
|
|
+ roleInfo.setRoleName(cacheRoleInfo.getRoleName());
|
|
|
+ roleInfo.setRoleGender(cacheRoleInfo.getRoleGender());
|
|
|
+ roleInfo.setRoleLevel(cacheRoleInfo.getRoleLevel());
|
|
|
+ roleInfo.setServerNo(cacheRoleInfo.getServerName());
|
|
|
+ roleInfo.setServerName(cacheRoleInfo.getServerName());
|
|
|
+ roleInfo.setRoleCTime(cacheRoleInfo.getRoleCTime());
|
|
|
+ roleInfo.setBalance(cacheRoleInfo.getBalance());
|
|
|
+ roleInfo.setPower(cacheRoleInfo.getPower());
|
|
|
+ roleInfo.setVipLevel(cacheRoleInfo.getVipLevel());
|
|
|
+ roleInfo.setRoleLevelMTime(System.currentTimeMillis() / 1000 + "");
|
|
|
+ return roleInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private QSChargeInfo getQSChargeInfo() {
|
|
|
+ QSChargeInfo chargeInfo = new QSChargeInfo();
|
|
|
+ String ts = System.currentTimeMillis() / 1000 + "";
|
|
|
+ chargeInfo.setOpenId(QSGameSdk.getInstance().getCurrentOpenId());
|
|
|
+ chargeInfo.setRoleId(cacheRoleInfo.getRoleId());
|
|
|
+ chargeInfo.setRoleName(cacheRoleInfo.getRoleName());
|
|
|
+ chargeInfo.setRoleGender(cacheRoleInfo.getRoleGender());
|
|
|
+ chargeInfo.setRoleLevel(cacheRoleInfo.getRoleLevel());
|
|
|
+ chargeInfo.setServerNo(cacheRoleInfo.getServerCode());
|
|
|
+ chargeInfo.setServerName(cacheRoleInfo.getServerName());
|
|
|
+ chargeInfo.setBalance(cacheRoleInfo.getBalance());
|
|
|
+ chargeInfo.setVipLevel(cacheRoleInfo.getVipLevel());
|
|
|
+ chargeInfo.setCpOrderId("order_" + ts);
|
|
|
+ chargeInfo.setProductName("元宝");
|
|
|
+ chargeInfo.setAmount(Integer.parseInt(editText.getText().toString()));
|
|
|
+ chargeInfo.setCpCallbackInfo("cp_callback_info||order_" + ts);
|
|
|
+ return chargeInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
+ if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
+ QSGameSdk.getInstance().openExitView(this, new ICallback() {
|
|
|
+ @Override
|
|
|
+ public void onResult(int code, String result) {
|
|
|
+ if (code == 0) {
|
|
|
+ DemoActivity.this.finish();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return super.onKeyDown(keyCode, event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onStart() {
|
|
|
+ super.onStart();
|
|
|
+ QSGameSdk.getInstance().onStart(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onRestart() {
|
|
|
+ super.onRestart();
|
|
|
+ QSGameSdk.getInstance().onRestart(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ QSGameSdk.getInstance().onResume(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPause() {
|
|
|
+ super.onPause();
|
|
|
+ QSGameSdk.getInstance().onPause(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onStop() {
|
|
|
+ super.onStop();
|
|
|
+ QSGameSdk.getInstance().onStop(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ QSGameSdk.getInstance().onDestroy(this);
|
|
|
+ //注意不能在sdk的退出回调中直接杀掉进程等方式退出,不然不会走Activity的onDestroy回调
|
|
|
+ System.exit(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ QSGameSdk.getInstance().onActivityResult(this, resultCode, resultCode, data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onNewIntent(Intent intent) {
|
|
|
+ super.onNewIntent(intent);
|
|
|
+ QSGameSdk.getInstance().onNewIntent(this, intent);
|
|
|
+ }
|
|
|
+}
|