package com.gh.common.util; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.graphics.Color; import android.support.v4.content.ContextCompat; import android.text.Html; import android.text.Spanned; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.TextView; import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.LibaoDetailActivity; import com.gh.gamecenter.R; import com.gh.gamecenter.adapter.LibaoDetailAdapter; import com.gh.gamecenter.entity.LibaoEntity; import com.gh.gamecenter.entity.LibaoStatusEntity; import com.gh.gamecenter.entity.UserDataEntity; import com.gh.gamecenter.entity.UserDataLibaoEntity; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.eventbus.EBUISwitch; import com.gh.gamecenter.geetest.GeetestListener; import com.gh.gamecenter.geetest.GeetestUtils; import com.gh.gamecenter.retrofit.JSONObjectResponse; import com.gh.gamecenter.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import com.lightgame.utils.Utils; import org.greenrobot.eventbus.EventBus; import org.json.JSONException; import org.json.JSONObject; import java.util.List; import okhttp3.ResponseBody; import retrofit2.HttpException; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * Created by khy on 2016/12/16. * 礼包工具类, 包括联网操作和领取按钮状态 */ public class LibaoUtils { public static final String REFRESH_LIBAO_TIME = "refreshLiBaoTime"; // 礼包去重 public static List removeDuplicateData(List sourceList, List rawList) { if (sourceList == null || sourceList.isEmpty() || rawList == null || rawList.isEmpty()) { return rawList; } String id; for (int i = 0; i < rawList.size(); i++) { id = rawList.get(i).getId(); for (LibaoEntity libaoEntity : sourceList) { if (id.equals(libaoEntity.getId())) { rawList.remove(i); i--; break; } } } return rawList; } private static void postLibaoLing(final Context context, final String libaoId, final PostLibaoListener listener, final String captchaCode) { Observable observable; if (!TextUtils.isEmpty(captchaCode)) { observable = RetrofitManager.getInstance(context).getApi().postLibaoLing(captchaCode, libaoId); } else { observable = RetrofitManager.getInstance(context).getApi().postLibaoLing(libaoId); } observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { listener.postSucced(response); } @Override public void onFailure(HttpException e) { if (e != null && e.code() == 410) { // 该接口已废弃 Utils.toast(context, "领取失败,请安装最新版本的光环助手"); } listener.postFailed(e); } }); } private static void postLibaoTao(final Context context, final String libaoId, final PostLibaoListener listener) { RetrofitManager.getInstance(context).getApi().postLibaoTao(libaoId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { listener.postSucced(response); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void deleteLibaoCode(final Context context, final String code, final PostLibaoListener listener) { RetrofitManager.getInstance(context).getApi().deleteLibaoCode(code) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSucced(response); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void getLibaoStatus(Context context, String ids, final PostLibaoListener listener) { RetrofitManager.getInstance(context).getApi().getLibaoStatus(ids) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response>() { @Override public void onResponse(List response) { listener.postSucced(response); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void initLibaoBtn(final Context context, final TextView libaoBtn, final LibaoEntity libaoEntity, final boolean isInstallRequired, final LibaoDetailAdapter adapter, final String entrance) { libaoBtn.setTextColor(Color.WHITE); final String status = libaoEntity.getStatus(); if (TextUtils.isEmpty(status)) return; switch (status) { case "coming": libaoBtn.setText("未开抢"); libaoBtn.setBackgroundResource(R.drawable.textview_blue_style); break; case "ling": libaoBtn.setText("领取"); libaoBtn.setBackgroundResource(R.drawable.textview_green_style); break; case "tao": libaoBtn.setText("淘号"); libaoBtn.setBackgroundResource(R.drawable.textview_orange_style); break; case "used_up": libaoBtn.setText("已领光"); libaoBtn.setBackgroundResource(R.drawable.textview_cancel_up); break; case "finish": libaoBtn.setText("已结束"); libaoBtn.setBackgroundResource(R.drawable.textview_cancel_up); break; case "linged": libaoBtn.setText("已领取"); libaoBtn.setBackgroundResource(R.drawable.libao_linged_style); libaoBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.libao_linged_selector)); break; case "taoed": libaoBtn.setText("已淘号"); libaoBtn.setBackgroundResource(R.drawable.libao_taoed_style); libaoBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.libao_taoed_selector)); break; case "copy": libaoBtn.setText("复制"); libaoBtn.setBackgroundResource(R.drawable.textview_blue_style); break; case "repeatLing": libaoBtn.setText("再领一个"); libaoBtn.setBackgroundResource(R.drawable.textview_cancel_up); break; case "repeatLinged": libaoBtn.setText("再领一个"); libaoBtn.setBackgroundResource(R.drawable.textview_green_style); break; case "repeatTao": libaoBtn.setText("再淘一个"); libaoBtn.setBackgroundResource(R.drawable.textview_cancel_up); break; case "repeatTaoed": libaoBtn.setText("再淘一个"); libaoBtn.setBackgroundResource(R.drawable.textview_orange_style); break; case "unshelve": libaoBtn.setBackgroundResource(R.drawable.textview_cancel_style); libaoBtn.setText("已下架"); break; default: libaoBtn.setBackgroundResource(R.drawable.textview_cancel_style); libaoBtn.setText("异常"); break; } libaoBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CheckLoginUtils.checkLogin(context, new CheckLoginUtils.OnLoggenInListener() { @Override public void onLoggedIn() { // 领取限制 if ("领取".equals(libaoBtn.getText().toString()) || "淘号".equals(libaoBtn.getText().toString())) { if (isInstallRequired && !isAppInstalled(context, libaoEntity.getPackageName())) { String platform; if (TextUtils.isEmpty(libaoEntity.getPlatform())) { platform = ""; } else { platform = PlatformUtils.getInstance(context) .getPlatformName(libaoEntity.getPlatform()) + "版"; } DialogUtils.showWarningDialog(context, "条件不符", Html.fromHtml("请先" + "" + "安装《" + libaoEntity.getGame().getName() + "》 " + platform + ""), "关闭", "立即安装" , new DialogUtils.ConfirmListener() { @Override public void onConfirm() { adapter.openDownload(); } }, null); return; } } switch (libaoBtn.getText().toString()) { case "未开始": Utils.toast(context, "还没到开始领取时间"); break; case "查看": Intent intent = LibaoDetailActivity.getIntent(context, libaoEntity, entrance); context.startActivity(intent); break; case "再领一个": case "领取": if (status.equals("repeatLing")) { DialogUtils.showWarningDialog(context, "礼包刷新提醒" , "礼包每天0点刷新,换新区或者换新角色需要继续领取礼包的童鞋,请于明天0点之后回来即可[再领一个]" , null, "知道了", null, null); } else { libaoLing(context, libaoBtn, libaoEntity, adapter, isInstallRequired, null, entrance); } break; case "再淘一个": case "淘号": if (status.equals("repeatTao")) { Utils.toast(context, "没到重复淘号时间, 礼包每天0点刷新"); return; } postLibaoTao(context, libaoEntity.getId(), new PostLibaoListener() { @Override public void postSucced(Object response) { JSONObject responseBody = (JSONObject) response; Utils.log("postLibaoTao=====" + responseBody); String libaoCode = null; try { libaoCode = responseBody.getString("code"); } catch (JSONException e) { e.printStackTrace(); } if (TextUtils.isEmpty(libaoCode)) { try { String detail = responseBody.getString("detail"); switch (detail) { case "maintaining": Utils.toast(context, "网络状态异常,请稍后再试"); break; case "fail to compete": Utils.toast(context, "淘号失败,稍后重试"); break; default: Utils.toast(context, "淘号异常"); break; } } catch (JSONException e) { e.printStackTrace(); } return; } Utils.toast(context, "淘号成功"); libaoEntity.setStatus("taoed"); EventBus.getDefault().post(new EBReuse("libaoChanged")); adapter.initLibaoCode(new UserDataLibaoEntity(libaoCode, "tao", Utils.getTime(context))); final String finalLibaoCode = libaoCode; DialogUtils.showWarningDialog(context, "淘号成功", Html.fromHtml("礼包码:" + "" + libaoCode + "" + "
淘号礼包不保证可用,请尽快进入游戏尝试兑换") , "关闭", " 复制礼包码" , new DialogUtils.ConfirmListener() { @Override public void onConfirm() { copyLink(finalLibaoCode, context); if (isInstallRequired) { libaoBtn.postDelayed(new Runnable() { @Override public void run() { lunningAppDialog(context , Html.fromHtml("礼包码:" + "" + finalLibaoCode + "" + " 复制成功" + "
淘号礼包不保证可用,请尽快进入游戏尝试兑换"), libaoEntity); } }, 300); } } }, null); } @Override public void postFailed(Throwable error) { Utils.log("---" + error.toString()); if (error instanceof HttpException) { HttpException exception = (HttpException) error; if (exception.code() == 403) { try { JSONObject errorJson = new JSONObject(exception.response().errorBody().string()); String detail = errorJson.getString("detail"); Utils.toast(context, "返回::" + detail); switch (detail) { case "coming": Utils.toast(context, "礼包领取时间未开始"); break; case "finish": Utils.toast(context, "礼包领取时间已结束"); break; case "fetched": Utils.toast(context, "你已领过这个礼包了"); libaoBtn.setText("已淘号"); libaoBtn.setBackgroundResource(R.drawable.libao_taoed_style); libaoBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.libao_taoed_selector)); libaoEntity.setStatus("taoed"); break; case "try tao": case "used up": DialogUtils.showHintDialog(context, "礼包已领光" , "手速不够快,礼包已经被抢光了,十分抱歉", "知道了"); break; case "maintaining": Utils.toast(context, "网络状态异常,请稍后再试"); break; case "fail to compete": Utils.toast(context, "淘号失败,稍后重试"); break; default: Utils.toast(context, "操作失败"); break; } } catch (Exception ex) { ex.printStackTrace(); Utils.toast(context, "礼包处理异常" + ex.toString()); } return; } } Utils.toast(context, "发生异常"); } }); break; } } }); } }); } private static void libaoLing(final Context context, final TextView libaoBtn, final LibaoEntity libaoEntity, final LibaoDetailAdapter adapter, final boolean isInstallRequired, String captchaCode, final String entrance) { if (BuildConfig.DEBUG) { Log.e("LIBAO", "context? " + context + libaoBtn.getContext()); } postLibaoLing(context, libaoEntity.getId(), new PostLibaoListener() { @Override public void postSucced(Object response) { JSONObject responseBody = (JSONObject) response; Utils.log("postLibaoLing=====" + responseBody); String libaoCode = null; try { libaoCode = responseBody.getString("code"); } catch (JSONException e) { e.printStackTrace(); } if (TextUtils.isEmpty(libaoCode)) { Utils.toast(context, "领取异常"); return; } libaoEntity.setAvailable(libaoEntity.getAvailable() - 1); libaoEntity.setStatus("linged"); EventBus.getDefault().post(new EBReuse("libaoChanged")); adapter.initLibaoCode(new UserDataLibaoEntity(libaoCode, "ling", Utils.getTime(context))); adapter.notifyDataSetChanged(); final String finalLibaoCode = libaoCode; DialogUtils.showWarningDialog(context, "领取成功", Html.fromHtml("礼包码:" + "" + libaoCode + "" + "
请尽快使用,礼包码将于60分钟后进入淘号池") , "关闭", " 复制礼包码" , new DialogUtils.ConfirmListener() { @Override public void onConfirm() { copyLink(finalLibaoCode, context); if (isInstallRequired) { libaoBtn.postDelayed(new Runnable() { @Override public void run() { lunningAppDialog(context , Html.fromHtml("礼包码:" + "" + finalLibaoCode + "" + " 复制成功" + "
请尽快进入游戏兑换"), libaoEntity); } }, 300); } } }, null); } @Override public void postFailed(Throwable error) { if (error instanceof HttpException) { HttpException exception = (HttpException) error; if (exception.code() == 403) { try { String string = exception.response().errorBody().string(); JSONObject errorJson = new JSONObject(string); String detail = errorJson.getString("detail"); Utils.log("=======detail ::" + errorJson.toString()); switch (detail) { case "coming": Utils.toast(context, "礼包领取时间未开始"); break; case "finish": Utils.toast(context, "礼包领取时间已结束"); break; case "fetched": Utils.toast(context, "你已领过这个礼包了"); int countdown = 0; if (errorJson.toString().contains("countdown")) { countdown = errorJson.getInt("countdown"); } if (countdown > 0 && countdown < 60 * 10) { EventBus.getDefault().post(new EBUISwitch(REFRESH_LIBAO_TIME, countdown)); } libaoBtn.setText("已领取"); libaoBtn.setBackgroundResource(R.drawable.libao_linged_style); libaoBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.libao_linged_selector)); libaoEntity.setStatus("linged"); break; case "try tao": case "used up": DialogUtils.showHintDialog(context, "礼包已领光" , "手速不够快,礼包已经被抢光了,十分抱歉", "知道了"); libaoEntity.setStatus("used_up"); initLibaoBtn(context, libaoBtn, libaoEntity, isInstallRequired, adapter, entrance); break; case "maintaining": Utils.toast(context, "网络状态异常,请稍后再试"); break; default: Utils.toast(context, "操作失败"); break; } } catch (Exception ex) { ex.printStackTrace(); Utils.toast(context, "礼包处理异常"); } return; } else if (exception.code() == 412) { // 需要验证 GeetestUtils.getInstance().showDialog(context, new GeetestListener() { @Override public void onVerified(String captcha) { libaoLing(context, libaoBtn, libaoEntity, adapter, isInstallRequired, captcha, entrance); } }); return; } } Utils.toast(context, "发生异常"); } }, captchaCode); } public static boolean isAppInstalled(Context context, String packageName) { final android.content.pm.PackageManager packageManager = context.getPackageManager(); List pinfo = packageManager.getInstalledPackages(0); if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get(i).packageName; if (pn.equals(packageName)) { return true; } } } return false; } public static void lunningAppDialog(final Context context, Spanned msg, final LibaoEntity libaoEntity) { DialogUtils.showWarningDialog(context, "复制成功", msg , "关闭", "启动游戏" , new DialogUtils.ConfirmListener() { @Override public void onConfirm() { if (LibaoUtils.isAppInstalled(context, libaoEntity.getPackageName())) { PackageUtils.launchApplicationByPackageName(context, libaoEntity.getPackageName()); } else { Utils.toast(context, "请安装游戏:" + libaoEntity.getGame().getName() + PlatformUtils.getInstance(context).getPlatformName(libaoEntity.getPlatform()) + "版"); } } }, null); } //复制文字 public static void copyLink(String copyContent, Context context) { ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); cmb.setText(copyContent); Utils.toast(context, copyContent + " 复制成功"); } // 合并List 和 List public static void initLiBaoEntity(List statusList, List libaoEntities) { for (LibaoEntity libaoEntity : libaoEntities) { for (LibaoStatusEntity libaoStatusEntity : statusList) { if (libaoEntity.getId().equals(libaoStatusEntity.getId())) { libaoEntity.setBeforeStatus(libaoStatusEntity.getStatus()); libaoStatusEntity.setBeforeStatus(libaoStatusEntity.getStatus()); UserDataEntity userData = libaoEntity.getUserData(); if (userData != null && userData.getUserDataLibaoList() != null && userData.getUserDataLibaoList().size() > 0) { List userDataLibaoList = userData.getUserDataLibaoList(); UserDataLibaoEntity userDataLibaoEntity = userDataLibaoList.get(userDataLibaoList.size() - 1); if (userDataLibaoEntity.getType().equals("ling")) { // 拿最后一次领取的状态判断 libaoEntity.setStatus("linged"); } else { libaoEntity.setStatus("taoed"); } } else { libaoEntity.setStatus(libaoStatusEntity.getStatus()); } libaoEntity.setAvailable(libaoStatusEntity.getAvailable()); libaoEntity.setTotal(libaoStatusEntity.getTotal()); } } } } public interface PostLibaoListener { void postSucced(Object response); void postFailed(Throwable error); } }