package com.gh.common.util; import android.content.Context; import android.graphics.Color; import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.widget.TextView; import com.gh.download.DownloadManager; import com.gh.gamecenter.R; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameCollectionEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.GameUpdateEntity; import com.gh.gamecenter.manager.PackageManager; import com.lightgame.download.DownloadEntity; import com.lightgame.download.DownloadStatus; import java.util.List; public class GameUtils { /** * 去除与重复sourceList相同的数据 */ 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 (GameEntity gameEntity : sourceList) { if (id.equals(gameEntity.getId())) { rawList.remove(i); i--; break; } } } return rawList; } /** * 设置下载按钮状态 */ public static void setDownloadBtnStatus(Context context, GameEntity gameEntity, TextView downloadBtn) { String status = getDownloadBtnText(context, gameEntity); downloadBtn.setTextColor(Color.WHITE); downloadBtn.setText(status); if ("插件化".equals(status)) { downloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style); } else if ("打开".equals(status)) { downloadBtn.setBackgroundResource(R.drawable.detail_downloading_normal_style); downloadBtn.setTextColor(ContextCompat.getColor(context, R.color.theme)); } else { downloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style); } } /** * 获取下载按钮文案 */ public static String getDownloadBtnText(Context context, GameEntity gameEntity) { int doneCount = 0; // 下载完成数量 int pluginCount = 0; // 可插件化数量 int updateCount = 0; // 可更新数量 int installCount = 0; // 已安装数量 DownloadEntity downloadEntity; Object gh_id; for (ApkEntity apkEntity : gameEntity.getApk()) { // 去除下载合集判断 boolean isCollection = false; for (GameCollectionEntity collectionEntity : gameEntity.getCollection()) { if (collectionEntity.getPackage().contains(apkEntity.getPackageName())) isCollection = true; } if (isCollection) continue; downloadEntity = DownloadManager.getInstance(context).getDownloadEntityByUrl(apkEntity.getUrl()); if (downloadEntity != null) { if (downloadEntity.getStatus().equals(DownloadStatus.done)) { doneCount++; } else if (downloadEntity.isPluggable()) { pluginCount++; } else if (downloadEntity.isUpdate()) { updateCount++; } } if (PackageManager.INSTANCE.isCanUpdate(gameEntity.getId(), apkEntity.getPackageName())) { updateCount++; } if (PackageManager.INSTANCE.isInstalled(apkEntity.getPackageName())) { gh_id = PackageUtils.getMetaData(context, apkEntity.getPackageName(), "gh_id"); if (gameEntity.getTag() != null && gameEntity.getTag().size() != 0 && !TextUtils.isEmpty(apkEntity.getGhVersion()) && !PackageUtils.isSignature(context, apkEntity.getPackageName())) { pluginCount++; } else if (gh_id == null || gh_id.equals(gameEntity.getId())) { installCount++; } } } if (doneCount != 0) { return "安装"; } else if (pluginCount != 0) { return "插件化"; } else if (updateCount != 0) { return "更新"; } else if (installCount != 0) { return "打开"; } else { return "下载"; } } /** * 获取GameUpdateEntity */ public static GameUpdateEntity getGameUpdateEntity(GameEntity gameEntity, ApkEntity apkEntity) { GameUpdateEntity gameUpdateEntity = new GameUpdateEntity(); gameUpdateEntity.setId(gameEntity.getId()); gameUpdateEntity.setIcon(gameEntity.getIcon()); gameUpdateEntity.setName(gameEntity.getName()); gameUpdateEntity.setPackageName(apkEntity.getPackageName()); gameUpdateEntity.setSize(apkEntity.getSize()); gameUpdateEntity.setVersion(apkEntity.getVersion()); gameUpdateEntity.setGhVersion(apkEntity.getGhVersion()); gameUpdateEntity.setUrl(apkEntity.getUrl()); gameUpdateEntity.setPlatform(apkEntity.getPlatform()); gameUpdateEntity.setEtag(apkEntity.getEtag()); gameUpdateEntity.setPluggable(true); gameUpdateEntity.setTag(gameEntity.getTag()); gameUpdateEntity.setBrief(gameEntity.getBrief()); return gameUpdateEntity; } }