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.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.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 retrofit2.adapter.rxjava.HttpException; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * Created by khy on 2016/12/16. * */ public class LibaoUtils { // 礼包去重 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; } //初始化存号箱 获取存号箱所有礼包 public static void getCunHaoXiang(final Context context, final LibaoDao libaoDao) { getCunHaoXiang(context, true, libaoDao); } private static void getCunHaoXiang(final Context context, final boolean isCheck, final LibaoDao libaoDao) { new Thread(new Runnable() { @Override public void run() { RetrofitManager.getLibao().getCunHaoXiang(TokenUtils.getToken(context, isCheck)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response>(){ @Override public void onResponse(List response) { super.onResponse(response); for (LibaoEntity libaoEntity : response) { libaoDao.add(LibaoInfo.createLibaoInfo(libaoEntity)); } EventBus.getDefault().post(new EBReuse("libaoChanged")); } @Override public void onFailure(Throwable e) { super.onFailure(e); if (e instanceof HttpException) { HttpException exception = (HttpException) e; if (exception.code() == 401) { getCunHaoXiang(context, false, libaoDao); return; } } } }); } }).start(); } public static void postLibaoLing(final Context context, final String libaoId, final PostLibaoListener listener) { postLibaoLing(context, libaoId, true, listener); } private static void postLibaoLing(final Context context, final String libaoId, final boolean isCheck , final PostLibaoListener listener) { new Thread(new Runnable() { @Override public void run() { RetrofitManager.getLibao().postLibaoLing(TokenUtils.getToken(context, isCheck), libaoId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { super.onResponse(response); listener.postSucced(response); } @Override public void onFailure(Throwable e) { super.onFailure(e); if (e instanceof HttpException) { HttpException exception = (HttpException) e; if (exception.code() == 401) { postLibaoLing(context, libaoId, false, listener); return; } } listener.postFailed(e); } }); } }).start(); } public static void postLibaoTao(final Context context, final String libaoId, final PostLibaoListener listener) { postLibaoTao(context, libaoId, true, listener); } private static void postLibaoTao(final Context context, final String libaoId, final boolean isCheck , final PostLibaoListener listener) { new Thread(new Runnable() { @Override public void run() { RetrofitManager.getLibao().postLibaoTao(TokenUtils.getToken(context, isCheck), libaoId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse(){ @Override public void onResponse(JSONObject response) { super.onResponse(response); listener.postSucced(response); } @Override public void onFailure(Throwable e) { super.onFailure(e); if (e instanceof HttpException) { HttpException exception = (HttpException) e; if (exception.code() == 401) { postLibaoLing(context, libaoId, false, listener); return; } } listener.postFailed(e); } }); } }).start(); } public static void getLibaoStatus(String ids, final PostLibaoListener listener) { RetrofitManager.getLibao().getLibaoStatus(ids) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response>() { @Override public void onResponse(List response) { super.onResponse(response); listener.postSucced(response); } @Override public void onFailure(Throwable e) { super.onFailure(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) { 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": libaoBtn.setText("已领取"); libaoBtn.setBackgroundResource(R.drawable.libao_linged_bg); libaoBtn.setTextColor(Color.parseColor("#06D0A8")); break; case "taoed": libaoBtn.setText("已淘号"); libaoBtn.setBackgroundResource(R.drawable.libao_taoed_bg); libaoBtn.setTextColor(Color.parseColor("#ffb13c")); 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(), "条件不符", "请先安装《" + libaoEntity.getGame().getName() + "》 " + platform, "关闭", "立即安装" , 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); libaoBtn.getContext().startActivity(intent); break; case "领取": postLibaoLing(libaoBtn.getContext(), 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(libaoBtn.getContext(), "领取异常"); return; } libaoEntity.setAvailable(libaoEntity.getAvailable() - 1); libaoDao.add(LibaoInfo.createLibaoInfo(libaoEntity)); EventBus.getDefault().post(new EBReuse("libaoChanged")); libaoEntity.setStatus("linged"); adapter.initLibaoDao(); adapter.notifyDataSetChanged(); final String finalLibaoCode = libaoCode; DialogUtils.showWarningDialog(libaoBtn.getContext(), "领取成功", "礼包码:" + libaoCode + "\n请尽快使用,礼包码将于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() , "请尽快进入游戏兑换", 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"); 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(), libaoDao); 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 { Utils.toast(libaoBtn.getContext(), "操作失败"); } } catch (Exception ex) { ex.printStackTrace(); Utils.toast(libaoBtn.getContext(),"礼包处理异常"); } return; } } Utils.toast(libaoBtn.getContext(), "网络异常"); } }); break; case "淘号": postLibaoTao(libaoBtn.getContext(), 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)) { Utils.toast(libaoBtn.getContext(), "淘号异常"); return; } Utils.toast(libaoBtn.getContext(), "淘号成功"); libaoDao.add(LibaoInfo.createLibaoInfo(libaoEntity)); EventBus.getDefault().post(new EBReuse("libaoChanged")); libaoEntity.setStatus("taoed"); adapter.initLibaoDao(); adapter.notifyDataSetChanged(); final String finalLibaoCode = libaoCode; DialogUtils.showWarningDialog(libaoBtn.getContext(), "淘号成功", "礼包码:" + libaoCode + "\n淘号礼包不保证可用,请尽快进入游戏尝试兑换" , "关闭", " 复制礼包码" , new DialogUtils.ConfiremListener() { @Override public void onConfirem() { copyLink(finalLibaoCode, libaoBtn.getContext()); if (isInstallRequired) { libaoBtn.postDelayed(new Runnable() { @Override public void run() { lunningAppDialog(libaoBtn.getContext() , "淘号礼包不保证可用,请尽快进入游戏尝试兑换", 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"); 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(), libaoDao); 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(),"礼包处理异常"); } return; } } Utils.toast(libaoBtn.getContext(), "网络异常"); } }); break; } } }); } 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, String 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 + " 复制成功"); } }