Files
assistant-android/app/src/main/java/com/gh/common/util/LibaoUtils.java

564 lines
28 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.view.View;
import android.widget.TextView;
import com.gh.base.AppController;
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.geetest.GeetestUtils;
import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import de.greenrobot.event.EventBus;
import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
* Created by khy on 2016/12/16.
* 礼包工具类, 包括联网操作和领取按钮状态
*/
public class LibaoUtils {
// 礼包去重
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, final boolean isCheck) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<List<LibaoEntity>>>() {
@Override
public Observable<List<LibaoEntity>> call(String token) {
return RetrofitManager.getLibao().getCunHaoXiang(token);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<LibaoEntity>>() {
@Override
public void onResponse(List<LibaoEntity> response) {
LibaoDao libaoDao = new LibaoDao(context);
for (LibaoEntity libaoEntity : response) {
LibaoInfo libaoInfo = LibaoInfo.createLibaoInfo(libaoEntity);
libaoInfo.setActive(libaoEntity.isActive());
libaoDao.add(libaoInfo);
}
EventBus.getDefault().post(new EBReuse("libaoChanged"));
}
@Override
public void onFailure(HttpException e) {
if (e != null && e.code() == 401) {
getCunHaoXiang(context, false);
}
}
});
}
private static void postLibaoLing(final Context context, final String libaoId, final boolean isCheck,
final PostLibaoListener listener, final String captchaCode) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(String token) {
if (!TextUtils.isEmpty(captchaCode)) {
return RetrofitManager.getLibao().postLibaoLing(token, captchaCode, libaoId);
} else {
return RetrofitManager.getLibao().postLibaoLing(token, 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) {
if (e != null && e.code() == 401) {
postLibaoLing(context, libaoId, false, listener, captchaCode);
return;
} else if (e.code() == 410) { // 该接口已废弃
Utils.toast(context, "领取失败,请安装最新版本的光环助手");
} else if (e.code() == 412) {
//TODO 需要验证码
}
listener.postFailed(e);
}
});
}
private static void postLibaoTao(final Context context, final String libaoId, final boolean isCheck,
final PostLibaoListener listener) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(String token) {
return RetrofitManager.getLibao().postLibaoTao(token, 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) {
if (e != null && e.code() == 401) {
postLibaoTao(context, libaoId, false, listener);
return;
}
listener.postFailed(e);
}
});
}
public static void deleteLibaoCode(final Context context, final String code, final boolean isCheck,
final PostLibaoListener listener) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(String token) {
return RetrofitManager.getLibao().deleteLibaoCode(token, 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) {
if (e != null && e.code() == 401) {
deleteLibaoCode(context, code, false, listener);
return;
}
listener.postFailed(e);
}
});
}
public static void getLibaoStatus(String ids, final PostLibaoListener listener) {
RetrofitManager.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 interface PostLibaoListener {
void postSucced(Object response);
void postFailed(Throwable error);
}
public static void initLibaoBtn(final TextView libaoBtn, final LibaoEntity libaoEntity, final LibaoDao libaoDao,
final boolean isInstallRequired, final LibaoDetailAdapter adapter, final String entrance) {
libaoBtn.setTextColor(Color.WHITE);
switch (libaoEntity.getStatus()) {
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;
default:
libaoBtn.setBackgroundResource(R.drawable.textview_cancel_style);
libaoBtn.setText("异常");
}
libaoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 领取限制
if ("领取".equals(libaoBtn.getText().toString()) || "淘号".equals(libaoBtn.getText().toString())) {
if (isInstallRequired && !isAppInstalled(libaoBtn.getContext(), libaoEntity.getPackageName())) {
String platform;
if (TextUtils.isEmpty(libaoEntity.getPlatform())) {
platform = "";
} else {
platform = PlatformUtils.getInstance(libaoBtn.getContext())
.getPlatformName(libaoEntity.getPlatform()) + "";
}
DialogUtils.showWarningDialog(libaoBtn.getContext(), "条件不符",
Html.fromHtml("请先"+ "<font color=\"#06D0A8\">"
+ "安装《" + libaoEntity.getGame().getName() + ""
+ platform + "</font>"), "关闭", "立即安装"
, new DialogUtils.ConfiremListener() {
@Override
public void onConfirem() {
adapter.openDownload();
}
}, null);
return;
}
}
switch (libaoBtn.getText().toString()) {
case "未开始":
Utils.toast(libaoBtn.getContext(), "还没到开始领取时间");
break;
case "查看":
AppController.put("libaoEntity", libaoEntity);
Intent intent = new Intent(libaoBtn.getContext(), LibaoDetailActivity.class);
intent.putExtra("entrance", entrance);
libaoBtn.getContext().startActivity(intent);
break;
case "领取":
libaoLing(libaoBtn, libaoEntity, adapter, isInstallRequired, libaoDao, null, entrance);
break;
case "淘号":
postLibaoTao(libaoBtn.getContext(), 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)) {
Utils.toast(libaoBtn.getContext(), "淘号异常");
return;
}
Utils.toast(libaoBtn.getContext(), "淘号成功");
LibaoInfo libaoInfo = LibaoInfo.createLibaoInfo(libaoEntity);
libaoInfo.setCode(libaoCode);
// libaoInfo.setTime(String.valueOf(new Date().getTime()));
libaoDao.add(libaoInfo);
EventBus.getDefault().post(new EBReuse("libaoChanged"));
libaoEntity.setStatus("taoed");
adapter.initLibaoDao();
adapter.notifyDataSetChanged();
final String finalLibaoCode = libaoCode;
DialogUtils.showWarningDialog(libaoBtn.getContext(), "淘号成功",Html.fromHtml("礼包码:"
+ "<font color=\"#ffb13c\">" + libaoCode + "</font>" +
"<br/>淘号礼包不保证可用,请尽快进入游戏尝试兑换")
, "关闭", " 复制礼包码"
, new DialogUtils.ConfiremListener() {
@Override
public void onConfirem() {
copyLink(finalLibaoCode, libaoBtn.getContext());
if (isInstallRequired) {
libaoBtn.postDelayed(new Runnable() {
@Override
public void run() {
lunningAppDialog(libaoBtn.getContext()
, 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(libaoBtn.getContext(), "返回::" + detail);
if ("coming".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "礼包领取时间未开始");
} else if ("finish".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "礼包领取时间已结束");
} else if ("fetched".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "你已领过这个礼包了");
getCunHaoXiang(libaoBtn.getContext(), true);
libaoBtn.setText("复制");
libaoBtn.setBackgroundResource(R.drawable.textview_blue_style);
} else if ("try tao".equals(detail) || "used up".equals(detail)) {
DialogUtils.showHintDialog(libaoBtn.getContext(), "礼包已领光"
, "手速不够快,礼包已经被抢光了,十分抱歉", "知道了");
} else if ("maintaining".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "网络状态异常,请稍后再试");
} else if ("fail to compete".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "淘号失败,稍后重试");
} else {
Utils.toast(libaoBtn.getContext(), "操作失败");
}
} catch (Exception ex) {
ex.printStackTrace();
Utils.toast(libaoBtn.getContext(),"礼包处理异常"+ ex.toString());
}
return;
}
}
Utils.toast(libaoBtn.getContext(), "发生异常");
}
});
break;
}
}
});
}
private static void libaoLing(final TextView libaoBtn, final LibaoEntity libaoEntity, final LibaoDetailAdapter adapter,
final boolean isInstallRequired, final LibaoDao libaoDao, String captchaCode, final String entrance) {
postLibaoLing(libaoBtn.getContext(), 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(libaoBtn.getContext(), "领取异常");
return;
}
libaoEntity.setAvailable(libaoEntity.getAvailable() - 1);
LibaoInfo libaoInfo = LibaoInfo.createLibaoInfo(libaoEntity);
libaoInfo.setCode(libaoCode);
libaoDao.add(libaoInfo);
EventBus.getDefault().post(new EBReuse("libaoChanged"));
libaoEntity.setStatus("linged");
adapter.initLibaoDao();
adapter.notifyDataSetChanged();
final String finalLibaoCode = libaoCode;
DialogUtils.showWarningDialog(libaoBtn.getContext(), "领取成功", Html.fromHtml("礼包码:"
+ "<font color=\"#00B7FA\">" + libaoCode + "</font>" +
"<br/>请尽快使用礼包码将于60分钟后进入淘号池")
, "关闭", " 复制礼包码"
, new DialogUtils.ConfiremListener() {
@Override
public void onConfirem() {
copyLink(finalLibaoCode, libaoBtn.getContext());
if (isInstallRequired) {
libaoBtn.postDelayed(new Runnable() {
@Override
public void run() {
lunningAppDialog(libaoBtn.getContext()
, 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");
if ("coming".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "礼包领取时间未开始");
} else if ("finish".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "礼包领取时间已结束");
} else if ("fetched".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "你已领过这个礼包了");
getCunHaoXiang(libaoBtn.getContext(), true);
libaoBtn.setText("复制");
libaoBtn.setBackgroundResource(R.drawable.textview_blue_style);
} else if ("try tao".equals(detail) || "used up".equals(detail)) {
DialogUtils.showHintDialog(libaoBtn.getContext(), "礼包已领光"
, "手速不够快,礼包已经被抢光了,十分抱歉", "知道了");
libaoEntity.setStatus("used_up");
initLibaoBtn(libaoBtn, libaoEntity, libaoDao, isInstallRequired, adapter, entrance);
} else if ("maintaining".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "网络状态异常,请稍后再试");
} else {
Utils.toast(libaoBtn.getContext(), "操作失败");
}
} catch (Exception ex) {
ex.printStackTrace();
Utils.toast(libaoBtn.getContext(),"礼包处理异常");
}
return;
} else if (exception.code() == 412) {
// 需要验证
GeetestUtils.getInstance(libaoBtn.getContext())
.showDialog(new GeetestUtils.GeetestListener() {
@Override
public void succed(String response) {
libaoLing(libaoBtn, libaoEntity, adapter, isInstallRequired, libaoDao, response, entrance);
}
});
return;
}
}
Utils.toast(libaoBtn.getContext(), "发生异常");
}
}, 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.ConfiremListener() {
@Override
public void onConfirem() {
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 + " 复制成功");
}
}