GameActivity.java 19.74 KiB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
package com.hl.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.hoolai.access.account.ui.WebviewActivity;
import com.hoolai.access.open.fastaccess.channel.FastSdk;
import com.hoolai.access.open.fastaccess.channel.request.EventType;
import com.hoolai.access.open.fastaccess.channel.request.PayParams;
import com.hoolai.access.open.fastaccess.channel.request.ProductParams;
import com.hoolai.access.open.fastaccess.channel.request.PlayerInfo;
import com.hoolai.access.open.fastaccess.channel.result.LoginResult;
import com.hoolai.access.open.fastaccess.interf.HLAccountListener;
import com.hoolai.access.open.fastaccess.interf.HLPaymentListener;
import com.hoolai.access.open.fastaccess.interf.HLProductInfoListener;
import com.hoolai.access.open.fastaccess.interf.HLRealNameListener;
import com.hoolai.access.open.fastaccess.interf.HLSystemListener;
import com.hoolai.access.open.fastaccess.utils.Logger;
import com.hoolai.access.open.fastaccess.utils.ToastUtils;
import com.hoolai.demo.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Hoolai_sdk接入说明
 * 接入文档地址 http://support.hoolai.com/docs/#/android/complete
 */
public class GameActivity extends Activity {

    private TextView contentTv;
    private LoginResult userResponse;
    private EditText et_tv, et_rpc, et_pwd;
    private EditText et_ip_host, et_ip_pwd, et_ip_port;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

        contentTv = findViewById(R.id.main_tv);
        et_tv = findViewById(R.id.et_main);
        et_rpc = findViewById(R.id.et_rpc);
        et_pwd = findViewById(R.id.et_pwd);
        et_ip_host = findViewById(R.id.et_ip_host);
        et_ip_port = findViewById(R.id.et_ip_port);
        et_ip_pwd = findViewById(R.id.et_ip_pwd);
        //hoolai sdk初始化操作
        Map<String, String> map = new HashMap<>();
        map.put("aa", "bbb");
        OneKeyLoginManager.getInstance().setDebug(true);
        settingProxy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        FastSdk.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        FastSdk.onStart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        FastSdk.onResume(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        FastSdk.onStop(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        FastSdk.onPause(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        FastSdk.onRestart(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FastSdk.onDestroy(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        FastSdk.onNewIntent(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FastSdk.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        FastSdk.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        FastSdk.onConfigurationChanged(this, newConfig, getResources());
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        //如果游戏需要捕获返回事件,请在这里做逻辑判断调用FastSdk.onBackPressed();
        FastSdk.onBackPressed();
        exit();
    }

    /**
     * hoolai sdk初始化,必须放到onCreate中,并且在主线程中调用
     */
    private void hoolaiSdkInit() {
        FastSdk.hlSystemListener = new HLSystemListener() {
            @Override
            public void onInitSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!contentTv.getText().toString().contains("登录成功")) {
                            contentTv.setText("初始化成功!\n获取初始化返回配置\n");
                        }
                        LogUtil.i("Hoolai sdk初始化成功:");
                        Toast.makeText(GameActivity.this, "初始化成功!", Toast.LENGTH_SHORT).show();

                    }
                });

            }

            @Override
            public void onInitFailed(String reason) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i("Hoolai sdk初始化失败:" + reason);
                        Toast.makeText(GameActivity.this,"初始化失败:"+reason,Toast.LENGTH_SHORT).show();
                    }
                });

                //sdk初始化失败
                //注意初始化未成功不得调用sdk任何接口,游戏可在这里做提示或者重新初始化操作

            }

            @Override
            public void onCustomExit() {
                Toast.makeText(GameActivity.this, "弹出游戏的退出界面", Toast.LENGTH_SHORT).show();
                /**
                 * demo 只作为参考,游戏应该根据自己所需逻辑实现
                 * 渠道没有退出二次确认框
                 * 根据需要加二次确认框
                 */
                // 渠道不存在退出界面,如百度移动游戏等,此时需在此处弹出游戏退出确认界面,否则会出现渠道审核不通过情况
                // 游戏实现自己的退出界面 ,实现退出逻辑,请勿直接照搬demo
                // 根据需要加二次确认框
                new AlertDialog.Builder(GameActivity.this)
                        .setTitle("游戏退出弹窗")
                        .setMessage("我是游戏的弹窗界面哦")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", (dialog, which) -> onExitSuccess(""))
                        .setCancelable(false).show();
            }

            @Override
            public void onExitSuccess(String result) {
                //当前渠道如果有退出界面,触发该回调
                //处理退出逻辑,
                moveTaskToBack(true);
                finish();
                //建议游戏按照下面代码逻辑,退出程序兼容性最好,否则可能会造成oppop、vivo等渠道退出不彻底,导致再次点击启动应用黑屏。
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                android.os.Process.killProcess(android.os.Process.myPid());
            }

            @Override
            public void onUpdate(String data) {

            }
        };
        FastSdk.hlAccountListener = new HLAccountListener() {
            @Override
            public void onRefreshUser(LoginResult result) {
                LogUtil.i("Hoolai refreshUser:" + result.toString());
                Toast.makeText(GameActivity.this,"小号切换成功"+result.getNickName(),Toast.LENGTH_SHORT).show();
                contentTv.setText("用户信息刷新:" + result.toString());

            }

            @Override
            public void onLoginSuccess(LoginResult result) {
                GameActivity.this.userResponse = result;
                contentTv.setText("登录成功:" + result.toString());

            }

            @Override
            public void onLoginFailed(String reason) {
                LogUtil.i("登录失败:" + reason);
                runOnUiThread(() -> {
                    contentTv.setText(reason);
                });
            }

            @Override
            public void onLogout(Object... var1) {
                contentTv.setText("已登出");
            }
        };
        FastSdk.hlPaymentListener = new HLPaymentListener() {
            @Override
            public void onPaySuccess(String result) {

            }

            @Override
            public void onPayFailed(String reason) {

            }
        };
        FastSdk.onCreate(this);
    }

    //=========================以下是模拟游戏调用,都是必接接口========================================

    /**
     * 登录调用,必接
     *
     * @param view
     */
    public void onClickLogin(View view) {
        FastSdk.login();
    }

    /**
     * 登出,必接
     */
    public void onClickInit(View view) {
        hoolaiSdkInit();
    }

    public void logout(View view) {
        FastSdk.logout();
        if (userResponse != null) {
        } else {
            Toast.makeText(GameActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 海外版sdk需要通过此接口获取商品信息进行UI展示,商品信息会根据区域自动转化币种
     * 上国内渠道此接口可不接入,海外版必接
     */
    public void getPrceInfo(View view) {
        if (userResponse == null) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(userResponse.getUid() + "")) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }

        FastSdk.getProductInfo(null, new HLProductInfoListener() {
            @Override
            public void onQuerySuccess(final List<ProductParams> list) {
                LogUtil.i("商品信息获取成功:" + list.toString());
                if (list.size() == 0) {
                    Toast.makeText(GameActivity.this, "商品信息获取为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                //游戏获取到商品信息之后可以展示到游戏商品界面
                //游戏需要有一套默认的商品信息配置,如果通过此接口获取不到商品信息时使用默认的商品配置

                //---------------以下是demo代码-----------------------------------------------
                Context context;
                AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
                ListView listView = new ListView(GameActivity.this);
                listView.setAdapter(new ArrayAdapter(GameActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, list));
                listView.setOnItemClickListener((parent, view1, position, id) -> {
                    ProductParams payInfo = list.get(position);
                    // 游戏支付时需要传的参数 示例
                    PayParams payParams = new PayParams();
                    payParams.setItemId(payInfo.getItemId());
                    payParams.setItemName(payInfo.getPriceTag());
                    payParams.setAmount(Integer.parseInt(payInfo.getPrice()));
                    payParams.setCount(1);
                    //扩展信息,回调时原样返回,因为各渠道回调参数限制不一致。回调参数暂时只支持长度50。callBackInfo中不要使用这些符号:“|”、“=”、“+”、“/”。
                    payParams.setCallbackInfo("支付扩展信息,游戏透传参数"+ UUID.randomUUID().toString().replace("-",""));
                    payParams.addExtendParam("isSupportThirdPayment", "true");//是否支持google之外的三方支付
                    payParams.addExtendParam("payType", "1");// 1 钱包(默认此类型) 2 点卡,短代类支付,需要通过FastSdk.supportShortterm查询
                    payParams.addExtendParam("currency", payInfo.getPriceLocal());//币种

                    FastSdk.pay(GameActivity.this, payParams);
                });
                builder.setView(listView);
                builder.show();
            }

            @Override
            public void onQueryFailed(String s) {
                LogUtil.i("商品信息获取失败:" + s);
                Toast.makeText(GameActivity.this, "商品信息获取失败:" + s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 支付调用,必接
     */
    public void onPay(View view) {
        pay(1);
    }


    public void customPay(View view) {
        String amount = String.valueOf(et_tv.getText());
        if (TextUtils.isEmpty(amount)) {
            Toast.makeText(GameActivity.this, "输入金额不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        pay(Integer.valueOf(amount));
    }

    private void pay(int amount) {
        if (userResponse == null) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }

        PayParams payParams = new PayParams();
        payParams.setItemId("aaa.bbb.cc");//商品,要保证游戏内各个商品唯一(可以不传)
        //ItemName要求:这些特殊符号都不要有: + % # “ & / ? $ ^ *:) \ < > , |。否则会造成某些渠道支付报错如:华为渠道
        payParams.setItemName("钻石");//购买物品名称
        payParams.setAmount(amount);//金额(分)
        payParams.setCount(1);//购买商品数量,例如50元宝,传值50
        payParams.setCallbackInfo("callbackInfo");//扩展信息,回调时原样返回,因为各渠道回调参数限制不一致。回调参数暂时只支持长度50。callBackInfo中不要使用这些符号:“|”、“=”、“+”、“/”。
        payParams.setCurrency("CNY");//国内可不传或CNY
        FastSdk.pay(GameActivity.this, payParams);
        //支付服务端回调地址,请在产品信息里配置。

    }

    /**
     * 退出接口调用,必接
     */
    public void onExit(View view) {
        exit();
    }

    public void exit() {
        FastSdk.exit();
    }

    /**
     * 账号管理,母包没有点击效果,渠道包会展示账号管理界面
     *
     * @param view
     */
    public void onClickAccountmanage(View view) {      // 管理账号接口
        WebviewActivity.start(this);
//        if(userResponse==null){
//            Toast.makeText(this,"请先登录!",Toast.LENGTH_SHORT).show();
//            return ;
//        }
    }

    public void onRealName(View view) {
        if (userResponse == null) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }
        /* *
         *  activity 游戏activity
         *  RealNameAuthCallBack 查询回调
         *  needRealNameAuth 预留字段暂未使用,未实名时是否需要引导玩家实名
         */
        FastSdk.getRealNameAuth(new HLRealNameListener() {
            /**
             * 已实名回调
             * @param isAdult 是否成年,部分渠道未提供为null
             * @param age 年龄,部分渠道未提供为null
             * @param realName 真实姓名,部分渠道未提供为null
             */
            @Override
            public void onSuccess(Boolean isAdult, Integer age, String realName) {
                //游戏根据回调信息进行防沉迷处理
                Toast.makeText(GameActivity.this, "已实名,已成年:" + isAdult + ",年龄:" + age + ",姓名:" + realName, Toast.LENGTH_SHORT).show();
            }

            /**
             * 未实名或查询异常
             * @param msg 信息
             */
            @Override
            public void onFail(String msg) {
                //游戏进行防沉迷处理
                Toast.makeText(GameActivity.this, "实名失败:" + msg, Toast.LENGTH_SHORT).show();
            }

            /**
             * 渠道不支持查询实名信息
             */
            @Override
            public void notSupport() {
                //游戏进行防沉迷处理
                Toast.makeText(GameActivity.this, "渠道不支持实名查询", Toast.LENGTH_SHORT).show();

            }
        }, true);
    }


    /**
     * 数据上报,必接
     *
     * @param view
     */
    public void createRole(View view) {
        if (userResponse == null) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }
        Logger.i("上报创角");
        sendPlayRoleInfo(EventType.CreateRole);
//        sendPlayRoleInfo(EventType.CustomerAction);
    }
    public void enterSever(View view) {
        if (userResponse == null) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }
        Logger.i("上报进服");
        sendPlayRoleInfo(EventType.EnterServer);
    }
    public void levelUp(View view) {
        if (userResponse == null) {
            Toast.makeText(this, "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }
        Logger.i("上报升级");
        sendPlayRoleInfo(EventType.LevelUp);
    }

    private void sendPlayRoleInfo(EventType eventType) {
        PlayerInfo playerInfo = new PlayerInfo();
        playerInfo.setRoleId("32424");//角色唯一标识id
        playerInfo.setRoleName("昵称");
        playerInfo.setRoleLevel("6");
        playerInfo.setZoneId("1");
        playerInfo.setZoneName("华东大区");
        playerInfo.setServerId("1区");
        playerInfo.setServerName("区名称");
        playerInfo.setBalance("66");
        playerInfo.setVip("5");
        playerInfo.setPartyName("帮派名称");
        playerInfo.setClassField("");
        playerInfo.setExtendAction("点位名称");
        playerInfo.setExtra("a:aaam,b:bbbb");//扩展信息,格式:key:value,key:value
//        playerInfo.setAppVersion("");
//        playerInfo.setAppResVersion("");
//        playerInfo.setRoleCreateTime("");
//        playerInfo.setPhylum("");
        //对于sdk海外版自定义事件报送,需要在传ACTION的时候做如下处理,中文版可以忽略
        //userExtData.put(UserExtDataKeys.ACTION, "oversea_custom");
        //userExtData.put(UserExtDataKeys.CUSTOM_KEY,"海外版自定义事件名称");
        FastSdk.report(eventType, playerInfo);
    }



    public void setProxy(View v) {
        settingProxy();
    }

    private void settingProxy() {
        String pwd = et_ip_pwd.getText().toString();
        String host = et_ip_host.getText().toString();
        String port = et_ip_port.getText().toString();
        if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(pwd) && !TextUtils.isEmpty(port)) {
            FastSdk.setProxy(pwd, host, port);
            ToastUtils.toast("设置代理:"+host+",pwd:"+pwd+",prot:"+port);
            hoolaiSdkInit();
        } else {
            hoolaiSdkInit();
        }
    }

    public void setRpc(View v) {
        String pwd = et_pwd.getText().toString();
        String host = et_rpc.getText().toString();
        if (TextUtils.isEmpty(host) || TextUtils.isEmpty(pwd)) {
            ToastUtils.toast("ip或密码不可为空");
            return;
        }
        //FastSdk.setRpc(pwd, host);
    }


    @Override
    public Resources getResources() {
        return FastSdk.getResources(super.getResources());
    }
}