package com.gh.common.util; import android.app.Dialog; import android.content.ClipboardManager; import android.content.Context; import android.content.pm.PackageInfo; import android.graphics.Color; import android.text.Html; import android.text.Spanned; import android.text.TextUtils; import android.util.Log; import android.widget.TextView; import androidx.core.content.ContextCompat; import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.R; import com.gh.gamecenter.adapter.LibaoDetailAdapter; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.LibaoEntity; import com.gh.gamecenter.entity.LibaoStatusEntity; import com.gh.gamecenter.entity.MeEntity; import com.gh.gamecenter.entity.UserDataLibaoEntity; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.eventbus.EBUISwitch; import com.gh.gamecenter.geetest.GeetestUtils; import com.gh.gamecenter.manager.UserManager; 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.ArrayList; import java.util.List; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; import retrofit2.HttpException; /** * 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.postSucceed(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.postSucceed(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(UserManager.getInstance().getUserId(), code) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSucceed(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(UrlFilterUtils.getFilterQuery("libao_ids", ids)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response>() { @Override public void onResponse(List response) { listener.postSucceed(response); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void setLiBaoBtnStatus(final TextView libaoBtn, String status, Context context) { libaoBtn.setTextColor(Color.WHITE); if (TextUtils.isEmpty(status)) return; switch (status) { case "ling": libaoBtn.setText(R.string.libao_ling); libaoBtn.setBackgroundResource(R.drawable.button_normal_style); break; case "tao": libaoBtn.setText(R.string.libao_tao); libaoBtn.setBackgroundResource(R.drawable.button_normal_style); break; case "coming": libaoBtn.setText(R.string.libao_coming); libaoBtn.setBackgroundResource(R.drawable.button_normal_border); libaoBtn.setTextColor(context.getResources().getColor(R.color.theme_font)); break; case "used_up": libaoBtn.setText(R.string.libao_used_up); libaoBtn.setBackgroundResource(R.drawable.button_normal_border); libaoBtn.setTextColor(context.getResources().getColor(R.color.theme_font)); break; case "finish": libaoBtn.setText(R.string.libao_finish); libaoBtn.setBackgroundResource(R.drawable.button_border_gray); libaoBtn.setTextColor(context.getResources().getColor(R.color.button_gray)); break; case "linged": libaoBtn.setText(R.string.libao_linged); libaoBtn.setBackgroundResource(R.drawable.button_normal_border); libaoBtn.setTextColor(context.getResources().getColor(R.color.theme_font)); break; case "taoed": libaoBtn.setText(R.string.libao_taoed); libaoBtn.setBackgroundResource(R.drawable.button_normal_border); libaoBtn.setTextColor(context.getResources().getColor(R.color.theme_font)); break; case "copy": libaoBtn.setText(R.string.libao_copy); libaoBtn.setBackgroundResource(R.drawable.button_normal_style); break; case "repeatLing": libaoBtn.setText(R.string.libao_repeat_ling); libaoBtn.setBackgroundResource(R.drawable.button_normal_border); libaoBtn.setTextColor(context.getResources().getColor(R.color.theme_font)); break; case "repeatLinged": libaoBtn.setText(R.string.libao_repeat_ling); libaoBtn.setBackgroundResource(R.drawable.button_normal_style); break; case "repeatTao": libaoBtn.setText(R.string.libao_repeat_tao); libaoBtn.setBackgroundResource(R.drawable.button_normal_border); libaoBtn.setTextColor(context.getResources().getColor(R.color.theme_font)); break; case "repeatTaoed": libaoBtn.setText(R.string.libao_repeat_tao); libaoBtn.setBackgroundResource(R.drawable.button_normal_style); break; case "unshelve": libaoBtn.setBackgroundResource(R.drawable.button_border_gray); libaoBtn.setText(R.string.libao_unshelve); libaoBtn.setTextColor(context.getResources().getColor(R.color.button_gray)); break; case "check": libaoBtn.setText(R.string.libao_check); libaoBtn.setBackgroundResource(R.drawable.button_normal_style); break; default: libaoBtn.setBackgroundResource(R.drawable.button_border_gray); libaoBtn.setText("异常"); libaoBtn.setTextColor(context.getResources().getColor(R.color.button_gray)); break; } } public static void initLibaoBtn(final Context context, final TextView libaoBtn, final LibaoEntity libaoEntity, final boolean isInstallRequired, final LibaoDetailAdapter adapter, final String entrance) { String status = libaoEntity.getStatus(); setLiBaoBtnStatus(libaoBtn, status, context); libaoBtn.setOnClickListener(v -> { String btnStatus = libaoBtn.getText().toString(); // 领取限制 CheckLoginUtils.checkLogin(context, "礼包详情-[" + btnStatus + "]", () -> { PermissionHelper.checkReadPhoneStatePermissionBeforeAction(context, () -> { if ("领取".equals(btnStatus) || "淘号".equals(btnStatus)) { if (isInstallRequired && !isAppInstalled(context, libaoEntity.getPackageName())) { String platform; if (TextUtils.isEmpty(libaoEntity.getPlatform())) { platform = ""; } else { platform = PlatformUtils.getInstance(context).getPlatformName(libaoEntity.getPlatform()); } boolean isExistPlatform = false; ArrayList apk = adapter.getGameEntity().getApk(); for (ApkEntity apkEntity : apk) { if (TextUtils.isEmpty(libaoEntity.getPlatform())) break; if (libaoEntity.getPlatform().equals(apkEntity.getPlatform())) { isExistPlatform = true; break; } } String dialogContent = context.getString(R.string.ling_rules_dialog, libaoEntity.getGame().getName(), platform); boolean finalIsExistPlatform = isExistPlatform; DialogUtils.showWarningDialog(context, "条件不符", Html.fromHtml(dialogContent), isExistPlatform ? "关闭" : null, isExistPlatform ? "立即安装" : "关闭", () -> { if (finalIsExistPlatform) { adapter.openDownload(libaoEntity.getPlatform()); } }, null); return; } } switch (btnStatus) { case "未开始": Utils.toast(context, "还没到开始领取时间"); break; case "查看": if (!TextUtils.isEmpty(libaoEntity.getDes())) { DialogUtils.showAlertDialog(v.getContext(), "使用说明", Html.fromHtml(libaoEntity.getDes()), "关闭", null, null, null); } break; case "再领一个": case "领取": if ("repeatLing".equals(status)) { DialogUtils.showWarningDialog(context, "礼包刷新提醒" , "礼包每天0点刷新,换新区或者换新角色需要继续领取礼包的童鞋,请于明天0点之后回来即可[再领一个]" , null, "知道了", null, null); } else { libaoLing(context, libaoBtn, libaoEntity, adapter, isInstallRequired, null, entrance); } break; case "再淘一个": case "淘号": if ("repeatTao".equals(status)) { Utils.toast(context, "没到重复淘号时间, 礼包每天0点刷新"); return; } final Dialog loadingDialog = DialogUtils.showWaitDialog(context, "淘号中..."); postLibaoTao(context, libaoEntity.getId(), new PostLibaoListener() { @Override public void postSucceed(Object response) { if (loadingDialog != null) loadingDialog.dismiss(); JSONObject responseBody = (JSONObject) response; 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(context.getString(R.string.taoed_dialog, libaoCode)) , "关闭", " 复制礼包码" , () -> { copyLink(finalLibaoCode, context); if (isInstallRequired) { libaoBtn.postDelayed(() -> { Spanned msg = Html.fromHtml( context.getString(R.string.taoed_copy_dialog , finalLibaoCode)); lunningAppDialog(context , msg, libaoEntity); }, 300); } }, null); } @Override public void postFailed(Throwable error) { Utils.log("---" + error.toString()); if (loadingDialog != null) loadingDialog.dismiss(); 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; } else if (exception.code() == 401) { 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()); } final Dialog loadingDialog = DialogUtils.showWaitDialog(context, "领取中..."); postLibaoLing(context, libaoEntity.getId(), new PostLibaoListener() { @Override public void postSucceed(Object response) { if (loadingDialog != null) loadingDialog.dismiss(); JSONObject responseBody = (JSONObject) response; 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(context.getString(R.string.linged_dialog, libaoCode)) , "关闭", " 复制礼包码" , () -> { copyLink(finalLibaoCode, context); if (isInstallRequired) { libaoBtn.postDelayed(() -> { Spanned msg = Html.fromHtml(context.getString(R.string.linged_copy_dialog, finalLibaoCode)); lunningAppDialog(context , msg, libaoEntity); }, 300); } }, null); } @Override public void postFailed(Throwable error) { if (loadingDialog != null) loadingDialog.dismiss(); 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"); 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")) { JSONObject data = errorJson.getJSONObject("data"); countdown = data.getInt("countdown"); } if (countdown > 0 && countdown < 60 * 10) { EventBus.getDefault().post(new EBUISwitch(REFRESH_LIBAO_TIME, countdown)); } libaoBtn.setText(R.string.libao_linged); 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, captcha -> libaoLing(context, libaoBtn, libaoEntity, adapter, isInstallRequired, captcha, entrance)); return; } else if (exception.code() == 401) { 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 , "关闭", "启动游戏" , () -> { 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()); MeEntity userData = libaoEntity.getMe(); if (userData != null && userData.getUserDataLibaoList() != null && userData.getUserDataLibaoList().size() > 0) { List userDataLibaoList = userData.getUserDataLibaoList(); UserDataLibaoEntity userDataLibaoEntity = userDataLibaoList.get(userDataLibaoList.size() - 1); if ("ling".equals(userDataLibaoEntity.getType())) { // 拿最后一次领取的状态判断 libaoEntity.setStatus("linged"); } else { libaoEntity.setStatus("taoed"); } } else { libaoEntity.setStatus(libaoStatusEntity.getStatus()); } libaoEntity.setAvailable(libaoStatusEntity.getAvailable()); libaoEntity.setTotal(libaoStatusEntity.getTotal()); } } } } public static void initLiBaoEntity(LibaoStatusEntity libaoStatusEntity, LibaoEntity libaoEntity) { if (libaoEntity.getId().equals(libaoStatusEntity.getId())) { libaoEntity.setBeforeStatus(libaoStatusEntity.getStatus()); libaoStatusEntity.setBeforeStatus(libaoStatusEntity.getStatus()); MeEntity userData = libaoEntity.getMe(); if (userData != null && userData.getUserDataLibaoList() != null && userData.getUserDataLibaoList().size() > 0) { List userDataLibaoList = userData.getUserDataLibaoList(); UserDataLibaoEntity userDataLibaoEntity = userDataLibaoList.get(userDataLibaoList.size() - 1); if ("ling".equals(userDataLibaoEntity.getType())) { // 拿最后一次领取的状态判断 libaoEntity.setStatus("linged"); } else { libaoEntity.setStatus("taoed"); } } else { libaoEntity.setStatus(libaoStatusEntity.getStatus()); } libaoEntity.setAvailable(libaoStatusEntity.getAvailable()); libaoEntity.setTotal(libaoStatusEntity.getTotal()); } } public interface PostLibaoListener { void postSucceed(Object response); void postFailed(Throwable error); } }