Files
assistant-android/app/src/main/java/com/gh/common/util/LibaoUtils.java
kehaoyuan 00879964e4 3.0Bug修复/UI优化/礼包优化
暂时未解决:礼包页面切换时,礼包搜索条自动顶上去/LibaoEntity getId和getLibaoId混乱
2017-09-05 11:57:19 +08:00

716 lines
36 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.common.util;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.res.ColorStateList;
import android.graphics.Color;
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.db.LibaoDao;
import com.gh.gamecenter.db.info.LibaoInfo;
import com.gh.gamecenter.entity.LibaoEntity;
import com.gh.gamecenter.entity.LibaoStatusEntity;
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;
import static com.gh.gamecenter.retrofit.RetrofitManager.getLibao;
/**
* Created by khy on 2016/12/16.
* 礼包工具类, 包括联网操作和领取按钮状态
*/
public class LibaoUtils {
public static final String REFRESH_LIBAO_TIME = "refreshLiBaoTime";
// 礼包去重
public static List<LibaoEntity> removeDuplicateData(List<LibaoEntity> sourceList, List<LibaoEntity> 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;
}
//初始化存号箱 获取存号箱所有礼包
public static void getCunHaoXiang(final Context context) {
getLibao()
.getCunHaoXiang(LoginUtils.getToken(context))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<LibaoEntity>>() {
@Override
public void onResponse(List<LibaoEntity> response) {
LibaoDao libaoDao = new LibaoDao(context);
libaoDao.deleteAll(); // 清空之前所有数据
for (LibaoEntity libaoEntity : response) {
if ("ling".equals(libaoEntity.getStatus())) {
libaoEntity.setStatus("linged");
} else {
libaoEntity.setStatus("taoed");
}
LibaoInfo libaoInfo = LibaoInfo.createLibaoInfo(libaoEntity);
libaoInfo.setActive(libaoEntity.isActive());
libaoDao.add(libaoInfo);
}
EventBus.getDefault().post(new EBReuse("libaoChanged"));
}
@Override
public void onFailure(HttpException e) {
}
});
}
private static void postLibaoLing(final Context context, final String libaoId, final boolean isCheck,
final PostLibaoListener listener, final String captchaCode) {
Observable<ResponseBody> observable;
if (!TextUtils.isEmpty(captchaCode)) {
observable = getLibao().postLibaoLing(LoginUtils.getToken(context), captchaCode, libaoId);
} else {
observable = RetrofitManager.getLibao().postLibaoLing(LoginUtils.getToken(context), 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 boolean isCheck,
final PostLibaoListener listener) {
getLibao().postLibaoTao(LoginUtils.getToken(context), 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 boolean isCheck,
final PostLibaoListener listener) {
getLibao().deleteLibaoCode(LoginUtils.getToken(context), code)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
listener.postSucced(response);
}
@Override
public void onFailure(HttpException e) {
listener.postFailed(e);
}
});
}
public static void getLibaoStatus(String ids, final PostLibaoListener listener) {
getLibao().getLibaoStatus(ids)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<LibaoStatusEntity>>() {
@Override
public void onResponse(List<LibaoStatusEntity> 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 LibaoDao libaoDao,
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":
int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_pressed};
states[1] = new int[]{};
int[] colors = new int[]{Color.WHITE,
Color.parseColor("#06D0A8")};
ColorStateList sl = new ColorStateList(states, colors);
libaoBtn.setText("已领取");
libaoBtn.setBackgroundResource(R.drawable.libao_linged_style);
libaoBtn.setTextColor(sl);
break;
case "taoed":
int[][] states2 = new int[2][];
states2[0] = new int[]{android.R.attr.state_pressed};
states2[1] = new int[]{};
int[] colors2 = new int[]{Color.WHITE, Color.parseColor("#ffb13c")};
ColorStateList sl2 = new ColorStateList(states2, colors2);
libaoBtn.setText("已淘号");
libaoBtn.setBackgroundResource(R.drawable.libao_taoed_style);
libaoBtn.setTextColor(sl2);
break;
case "copy":
libaoBtn.setText("复制");
libaoBtn.setBackgroundResource(R.drawable.textview_blue_style);
break;
case "repeatLing":
if (adapter == null) {
int[][] states3 = new int[2][];
states3[0] = new int[]{android.R.attr.state_pressed};
states3[1] = new int[]{};
int[] colors3 = new int[]{Color.WHITE,
Color.parseColor("#06D0A8")};
ColorStateList sl3 = new ColorStateList(states3, colors3);
libaoBtn.setText("已领取");
libaoBtn.setBackgroundResource(R.drawable.libao_linged_style);
libaoBtn.setTextColor(sl3);
} else {
libaoBtn.setText("再领一个");
libaoBtn.setBackgroundResource(R.drawable.textview_cancel_up);
}
break;
case "repeatLinged":
if (adapter == null) {
int[][] states4 = new int[2][];
states4[0] = new int[]{android.R.attr.state_pressed};
states4[1] = new int[]{};
int[] colors4 = new int[]{Color.WHITE,
Color.parseColor("#06D0A8")};
ColorStateList sl4 = new ColorStateList(states4, colors4);
libaoBtn.setText("已领取");
libaoBtn.setBackgroundResource(R.drawable.libao_linged_style);
libaoBtn.setTextColor(sl4);
} else {
libaoBtn.setText("再领一个");
libaoBtn.setBackgroundResource(R.drawable.textview_green_style);
}
break;
default:
libaoBtn.setBackgroundResource(R.drawable.textview_cancel_style);
libaoBtn.setText("异常");
}
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("请先" + "<font color=\"#06D0A8\">"
+ "安装《" + libaoEntity.getGame().getName() + ""
+ platform + "</font>"), "关闭", "立即安装"
, 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, libaoDao, null, entrance);
}
break;
case "淘号":
postLibaoTao(context, libaoEntity.getId(), true, 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");
LibaoInfo libaoInfo = LibaoInfo.createLibaoInfo(libaoEntity);
libaoInfo.setCode(libaoCode);
libaoInfo.setTime(Utils.getTime(context));
libaoDao.add(libaoInfo);
EventBus.getDefault().post(new EBReuse("libaoChanged"));
adapter.initLibaoDao();
adapter.notifyDataSetChanged();
final String finalLibaoCode = libaoCode;
DialogUtils.showWarningDialog(context, "淘号成功", Html.fromHtml("礼包码:"
+ "<font color=\"#ffb13c\">" + libaoCode + "</font>" +
"<br/>淘号礼包不保证可用,请尽快进入游戏尝试兑换")
, "关闭", " 复制礼包码"
, new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
copyLink(finalLibaoCode, context);
if (isInstallRequired) {
libaoBtn.postDelayed(new Runnable() {
@Override
public void run() {
lunningAppDialog(context
, Html.fromHtml("礼包码:"
+ "<font color=\"#ffb13c\">" + finalLibaoCode + "</font>"
+ " 复制成功"
+ "<br/>淘号礼包不保证可用,请尽快进入游戏尝试兑换"), 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, "你已领过这个礼包了");
getCunHaoXiang(context);
int[][] states2 = new int[2][];
states2[0] = new int[]{android.R.attr.state_pressed};
states2[1] = new int[]{};
int[] colors2 = new int[]{Color.WHITE,
Color.parseColor("#ffb13c")};
ColorStateList sl2 = new ColorStateList(states2, colors2);
libaoBtn.setText("已淘号");
libaoBtn.setBackgroundResource(R.drawable.libao_taoed_style);
libaoBtn.setTextColor(sl2);
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, final LibaoDao libaoDao, String captchaCode, final String entrance) {
if (BuildConfig.DEBUG) {
Log.e("LIBAO", "context? " + context + libaoBtn.getContext());
}
postLibaoLing(context, libaoEntity.getId(), true, 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");
LibaoInfo libaoInfo = LibaoInfo.createLibaoInfo(libaoEntity);
libaoInfo.setTime(Utils.getTime(context));
libaoInfo.setCode(libaoCode);
libaoDao.add(libaoInfo);
EventBus.getDefault().post(new EBReuse("libaoChanged"));
adapter.initLibaoDao();
adapter.notifyDataSetChanged();
final String finalLibaoCode = libaoCode;
DialogUtils.showWarningDialog(context, "领取成功", Html.fromHtml("礼包码:"
+ "<font color=\"#00B7FA\">" + libaoCode + "</font>" +
"<br/>请尽快使用礼包码将于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("礼包码:"
+ "<font color=\"#00B7FA\">" + finalLibaoCode + "</font>"
+ " 复制成功" + "<br/>请尽快进入游戏兑换"), 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 {
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")) {
countdown = errorJson.getInt("countdown");
}
if (countdown > 0 && countdown < 60 * 10) {
EventBus.getDefault().post(new EBUISwitch(REFRESH_LIBAO_TIME, countdown));
} else {
getCunHaoXiang(context);
}
int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_pressed};
states[1] = new int[]{};
int[] colors = new int[]{Color.WHITE,
Color.parseColor("#06D0A8")};
ColorStateList sl = new ColorStateList(states, colors);
libaoBtn.setText("已领取");
libaoBtn.setBackgroundResource(R.drawable.libao_linged_style);
libaoBtn.setTextColor(sl);
libaoEntity.setStatus("linged");
break;
case "try tao":
case "used up":
DialogUtils.showHintDialog(context, "礼包已领光"
, "手速不够快,礼包已经被抢光了,十分抱歉", "知道了");
libaoEntity.setStatus("used_up");
initLibaoBtn(context, libaoBtn, libaoEntity, libaoDao, 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, libaoDao, captcha, entrance);
}
});
return;
}
}
Utils.toast(context, "发生异常");
}
}, captchaCode);
}
public static boolean isAppInstalled(Context context, String packageName) {
final android.content.pm.PackageManager packageManager = context.getPackageManager();
List<PackageInfo> 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<LibaoStatusEntity> 和 List<LibaoEntity>
public static void initLiBaoEntity(LibaoDao libaoDao, List<LibaoStatusEntity> statusList,
List<LibaoEntity> libaoEntities, Context mContext) {
for (LibaoInfo libaoInfo : libaoDao.getAll()) {
for (LibaoStatusEntity libaoStatusEntity : statusList) {
if (TextUtils.isEmpty(libaoInfo.getLibaoId()) || TextUtils.isEmpty(libaoStatusEntity.getId())) {
continue;
}
if (TextUtils.isEmpty(libaoStatusEntity.getBeforeStatus())) {
libaoStatusEntity.setBeforeStatus(libaoStatusEntity.getStatus());
}
if (libaoInfo.getLibaoId().equals(libaoStatusEntity.getId())) {
if ("ling".equals(libaoInfo.getStatus()) || "linged".equals(libaoInfo.getStatus())) {
libaoStatusEntity.setStatus("linged");
} else {
libaoStatusEntity.setStatus("taoed");
}
}
}
}
for (LibaoEntity libaoEntity : libaoEntities) {
for (LibaoStatusEntity libaoStatusEntity : statusList) {
if (libaoEntity.getId().equals(libaoStatusEntity.getId())) {
libaoEntity.setAvailable(libaoStatusEntity.getAvailable());
libaoEntity.setTotal(libaoStatusEntity.getTotal());
String beforeStatus = libaoStatusEntity.getBeforeStatus();
if (TextUtils.isEmpty(beforeStatus)) {
beforeStatus = libaoStatusEntity.getStatus();
}
// int repeat = libaoEntity.getRepeat();
// if (repeat > 0
// && libaoDao.isCanLing(libaoEntity.getId(), mContext)
// && ("ling".equals(beforeStatus) || "tao".equals(beforeStatus))) { // 判断是否可以重复领取
// if ("ling".equals(beforeStatus)) {
// if (libaoDao.repeatedLingedCount(libaoStatusEntity.getId()) >= repeat) {
// libaoEntity.setStatus(libaoStatusEntity.getStatus());
// } else {
// libaoEntity.setStatus(beforeStatus);
// }
// } else {
// if (libaoDao.repeatedTaoedCount(libaoStatusEntity.getId()) >= repeat) {
// libaoEntity.setStatus(libaoStatusEntity.getStatus());
// } else {
// libaoEntity.setStatus(beforeStatus);
// }
// }
// } else {
libaoEntity.setStatus(libaoStatusEntity.getStatus());
// }
libaoEntity.setBeforeStatus(beforeStatus);
}
}
}
}
public interface PostLibaoListener {
void postSucced(Object response);
void postFailed(Throwable error);
}
}