package com.gh.common.util; import android.content.Context; import android.graphics.Color; import android.text.TextUtils; import android.widget.TextView; import com.gh.common.constant.Config; import com.gh.common.simulator.SimulatorGameManager; import com.gh.download.DownloadManager; import com.gh.gamecenter.R; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.ApkLink; import com.gh.gamecenter.entity.GameCollectionEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.GameUpdateEntity; import com.gh.gamecenter.entity.PluginLocation; import com.gh.gamecenter.entity.SettingsEntity; import com.gh.gamecenter.manager.PackagesManager; import com.lightgame.download.DownloadEntity; import com.lightgame.download.DownloadStatus; import java.util.ArrayList; import java.util.List; import androidx.annotation.Nullable; 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, PluginLocation pluginLocation) { String status = getDownloadBtnText(context, gameEntity, pluginLocation); downloadBtn.setTextColor(Color.WHITE); downloadBtn.setText(status); if (context.getString(R.string.pluggable).equals(status)) { downloadBtn.setBackgroundResource(R.drawable.download_button_pluggable_style); String pluginDesc = gameEntity.getPluginDesc(); if (pluginDesc.length() > 3) pluginDesc = pluginDesc.substring(0, 3); downloadBtn.setText((pluginDesc + "化")); } else { downloadBtn.setBackgroundResource(R.drawable.download_button_normal_style); } } /** * 获取下载按钮文案 */ public static String getDownloadBtnText(Context context, GameEntity gameEntity, PluginLocation pluginLocation) { if (gameEntity.getApk().size() > 1) { return context.getString(R.string.expand); } int doneCount = 0; // 下载完成数量 int pluginCount = 0; // 可插件化数量 int updateCount = 0; // 可更新数量 int installCount = 0; // 已安装数量 DownloadEntity downloadEntity; Object gh_id; apkFor: for (ApkEntity apkEntity : gameEntity.getApk()) { // filter by packageName SettingsEntity settings = Config.getSettings(); if (settings != null && gameEntity.getApk().size() > 1) { for (String pkgName : settings.getGameDownloadBlackList()) { if (pkgName.equals(apkEntity.getPackageName())) { continue apkFor; } } } 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 (PackagesManager.INSTANCE.isCanUpdate(gameEntity.getId(), apkEntity.getPackageName())) { updateCount++; } if (PackagesManager.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.isSignedByGh(context, apkEntity.getPackageName()) && apkEntity.isShowPlugin(pluginLocation)) { pluginCount++; } else if (gh_id == null || gh_id.equals(gameEntity.getId())) { installCount++; } } if (SimulatorGameManager.isSimulatorGame(gameEntity)) { DownloadEntity simulatorDownloadEntity = SimulatorGameManager.findDownloadEntityByUrl(apkEntity.getUrl()); if (simulatorDownloadEntity != null && gameEntity.getSimulator() != null) { boolean isInstalled = PackageUtils.isInstalledFromAllPackage(context, gameEntity.getSimulator().getApk().getPackageName()); if (isInstalled) { installCount++; } else { doneCount++; } } } } if (installCount != 0) { return context.getString(R.string.launch); } else if (pluginCount != 0 && !SimulatorGameManager.isSimulatorGame(gameEntity)) { return context.getString(R.string.pluggable); } else if (updateCount != 0 && !SimulatorGameManager.isSimulatorGame(gameEntity)) { return context.getString(R.string.update); } else if (doneCount != 0) { return context.getString(R.string.install); } else if (gameEntity.getVersionNumber().contains("无版号") && Config.isGameDomeSwitchOpen() && !SimulatorGameManager.isSimulatorGame(gameEntity)) { return context.getString(R.string.attempt); } else { return context.getString(R.string.download); } } /** * 获取简单的下载按钮文案,只需要知道是否已下载,是否已安装 */ public static String getSimpleDownloadBtnText(Context context, GameEntity gameEntity) { int doneCount = 0; // 下载完成数量 int installCount = 0; // 已安装数量 DownloadEntity downloadEntity; Object gh_id; apkFor: for (ApkEntity apkEntity : gameEntity.getApk()) { // filter by packageName SettingsEntity settings = Config.getSettings(); if (settings != null && gameEntity.getApk().size() > 1) { for (String pkgName : settings.getGameDownloadBlackList()) { if (pkgName.equals(apkEntity.getPackageName())) { continue apkFor; } } } downloadEntity = DownloadManager.getInstance(context).getDownloadEntityByUrl(apkEntity.getUrl()); if (downloadEntity != null) { if (downloadEntity.getStatus().equals(DownloadStatus.done)) { doneCount++; } } if (PackagesManager.INSTANCE.isInstalled(apkEntity.getPackageName())) { gh_id = PackageUtils.getMetaData(context, apkEntity.getPackageName(), "gh_id"); if (gh_id == null || gh_id.equals(gameEntity.getId())) { installCount++; } } } if (doneCount != 0) { return context.getString(R.string.install); } else if (installCount != 0) { return context.getString(R.string.launch); } else { return context.getString(R.string.download); } } /** * 获取GameUpdateEntity(插件化) */ public static GameUpdateEntity getGameUpdateEntity(GameEntity gameEntity, ApkEntity apkEntity) { GameUpdateEntity gameUpdateEntity = new GameUpdateEntity(); gameUpdateEntity.setId(gameEntity.getId()); gameUpdateEntity.setIcon(gameEntity.getIcon()); gameUpdateEntity.setRawIcon(gameEntity.getRawIcon()); gameUpdateEntity.setIconSubscript(gameEntity.getIconSubscript()); 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.setTagStyle(gameEntity.getTagStyle()); gameUpdateEntity.setBrief(gameEntity.getBrief()); gameUpdateEntity.setPlugin(apkEntity.getPlugin()); gameUpdateEntity.setDownload(gameEntity.getDownload()); gameUpdateEntity.setIndexPlugin(gameEntity.getIndexPlugin()); gameUpdateEntity.setPluginDesc(gameEntity.getPluginDesc()); gameUpdateEntity.setFormat(apkEntity.getFormat()); GameCollectionEntity pluggableCollection = getPluggableCollectionFromGameEntity(gameEntity, apkEntity.getPackageName()); if (pluggableCollection != null) { gameUpdateEntity.setPluggableCollection(pluggableCollection); } return gameUpdateEntity; } @Nullable public static GameCollectionEntity getPluggableCollectionFromGameEntity(GameEntity gameEntity, String targetPkg) { // 添加插件化包所在的合集 for (GameCollectionEntity collectionEntity : gameEntity.getCollection()) { if (collectionEntity.getPackages().contains(targetPkg)) { ArrayList saveApkEntity = new ArrayList<>(); for (String pkg : collectionEntity.getPackages()) { for (ApkEntity apk : gameEntity.getApk()) { if (pkg.equals(apk.getPackageName())) { saveApkEntity.add(apk); break; } } } ArrayList apkLinks = gameEntity.getApkLink(); if (apkLinks != null) { for (ApkLink apkLink : apkLinks) { if (apkLink.getCollection().equals(collectionEntity.getId())) { ApkEntity element = new ApkEntity(); element.setApkLink(apkLink); if (saveApkEntity.size() > apkLink.getSort()) { saveApkEntity.add(apkLink.getSort(), element); } else { saveApkEntity.add(element); } } } } collectionEntity.setSaveApkEntity(saveApkEntity); return collectionEntity; } } return null; } }