DownloadItemUtils.updateItem 参数简化

This commit is contained in:
huangzhuanghua
2017-02-13 11:31:52 +08:00
parent a29c0bd466
commit c292e79d94
17 changed files with 291 additions and 431 deletions

View File

@ -9,8 +9,6 @@ import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@ -22,6 +20,8 @@ import com.gh.download.DownloadManager;
import com.gh.download.DownloadStatus;
import com.gh.gamecenter.DownloadManagerActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.GameViewHolder;
import com.gh.gamecenter.db.LibaoDao;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.manager.PackageManager;
@ -87,14 +87,7 @@ public class DownloadItemUtils {
}
// 更新正常的条目只有一个apk包
public static void updateNormalItem(Context context,
TextView textView,
ProgressBar game_progressbar,
LinearLayout game_ll_info,
TextView download_speed,
TextView download_percentage,
TextView downloadBtn,
GameEntity gameEntity,
public static void updateNormalItem(Context context, GameViewHolder holder, GameEntity gameEntity,
boolean isShowPlatform) {
ArrayMap<String, DownloadEntity> entryMap = gameEntity.getEntryMap();
@ -102,71 +95,63 @@ public class DownloadItemUtils {
DownloadEntity downloadEntity = entryMap.get(gameEntity.getApk().get(0).getPlatform());
if (downloadEntity != null) {
// 更改进度条和提示文本的状态
changeStatus(context, textView, game_progressbar, game_ll_info, download_speed, download_percentage, downloadBtn,
downloadEntity, isShowPlatform, true);
changeStatus(context, holder, downloadEntity, isShowPlatform, true);
return;
}
}
textView.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
holder.gameDes.setVisibility(View.VISIBLE);
holder.gameProgressbar.setVisibility(View.GONE);
holder.gameInfo.setVisibility(View.GONE);
downloadBtn.setTextColor(Color.WHITE);
holder.gameDownloadBtn.setTextColor(Color.WHITE);
if (gameEntity.isPluggable()) {
downloadBtn.setText("插件化");
holder.gameDownloadBtn.setText("插件化");
DownloadEntity downloadEntity = DownloadManager.getInstance(context).getByPackage(
gameEntity.getApk().get(0).getPackageName());
if (downloadEntity == null
|| downloadEntity.getUrl().equals(gameEntity.getApk().get(0).getUrl())) {
downloadBtn.setClickable(true);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
holder.gameDownloadBtn.setClickable(true);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
} else {
downloadBtn.setClickable(false);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_up);
holder.gameDownloadBtn.setClickable(false);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_up);
}
} else if (PackageManager.isInstalled(gameEntity.getApk().get(0).getPackageName())) {
if (PackageManager.isCanUpdate(gameEntity.getId(), gameEntity.getApk().get(0).getPackageName())) {
downloadBtn.setText("更新");
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
holder.gameDownloadBtn.setText("更新");
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
} else {
if (gameEntity.getTag() != null && gameEntity.getTag().size() != 0
&& !TextUtils.isEmpty(gameEntity.getApk().get(0).getGhVersion())
&& !PackageUtils.isSignature(context, gameEntity.getApk().get(0).getPackageName())) {
downloadBtn.setText("插件化");
holder.gameDownloadBtn.setText("插件化");
DownloadEntity downloadEntity = DownloadManager.getInstance(context).getByPackage(
gameEntity.getApk().get(0).getPackageName());
if (downloadEntity == null
|| downloadEntity.getUrl().equals(gameEntity.getApk().get(0).getUrl())) {
downloadBtn.setClickable(true);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
holder.gameDownloadBtn.setClickable(true);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
} else {
downloadBtn.setClickable(false);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_up);
holder.gameDownloadBtn.setClickable(false);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_up);
}
} else {
downloadBtn.setText("启动");
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_launch_style);
holder.gameDownloadBtn.setText("启动");
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_launch_style);
}
}
} else {
downloadBtn.setText("下载");
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
holder.gameDownloadBtn.setText("下载");
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
}
}
// 更新插件的条目有多个apk包
public static void updatePluginItem(Context context,
TextView textView,
ProgressBar game_progressbar,
LinearLayout game_ll_info,
TextView download_speed,
TextView download_percentage,
TextView downloadBtn,
GameEntity gameEntity,
public static void updatePluginItem(Context context, GameViewHolder holder, GameEntity gameEntity,
boolean isShowPlatform) {
GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn);
GameUtils.setDownloadBtnStatus(context, gameEntity, holder.gameDownloadBtn);
ArrayMap<String, DownloadEntity> entryMap = gameEntity.getEntryMap();
if (entryMap != null && !entryMap.isEmpty()) {
@ -182,31 +167,22 @@ public class DownloadItemUtils {
if (downloadEntity != null) {
// 更改进度条和提示文本的状态
changeStatus(context, textView, game_progressbar, game_ll_info, download_speed,
download_percentage, downloadBtn, downloadEntity, isShowPlatform, false);
changeStatus(context, holder, downloadEntity, isShowPlatform, false);
return;
}
}
textView.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
holder.gameDes.setVisibility(View.VISIBLE);
holder.gameProgressbar.setVisibility(View.GONE);
holder.gameInfo.setVisibility(View.GONE);
}
// 更改进度条和提示文本的状态
public static void changeStatus(Context context,
TextView textView,
ProgressBar game_progressbar,
LinearLayout game_ll_info,
TextView download_speed,
TextView download_percentage,
TextView downloadBtn,
DownloadEntity downloadEntity,
boolean isShowPlatform,
boolean isNormal) {
textView.setVisibility(View.GONE);
game_progressbar.setVisibility(View.VISIBLE);
game_ll_info.setVisibility(View.VISIBLE);
public static void changeStatus(Context context, GameViewHolder holder, DownloadEntity downloadEntity,
boolean isShowPlatform, boolean isNormal) {
holder.gameDes.setVisibility(View.GONE);
holder.gameProgressbar.setVisibility(View.VISIBLE);
holder.gameInfo.setVisibility(View.VISIBLE);
String platform = PlatformUtils.getInstance(context)
.getPlatformName(downloadEntity.getPlatform());
@ -214,119 +190,108 @@ public class DownloadItemUtils {
DownloadStatus status = downloadEntity.getStatus();
if (status.equals(DownloadStatus.downloading)) {
if (!"pause".equals(DownloadManager.getInstance(context).getStatus(downloadEntity.getUrl()))) {
game_progressbar.setProgress((int) (downloadEntity.getPercent() * 10));
holder.gameProgressbar.setProgress((int) (downloadEntity.getPercent() * 10));
if (isShowPlatform && platform != null) {
download_speed.setText(String.format("%s - %s(剩%s)", platform,
holder.gameDownloadSpeed.setText(String.format("%s - %s(剩%s)", platform,
SpeedUtils.getSpeed(downloadEntity.getSpeed()),
SpeedUtils.getRemainTime(downloadEntity.getSize(), downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
} else {
download_speed.setText(String.format("%s(剩%s)", SpeedUtils.getSpeed(downloadEntity.getSpeed()),
holder.gameDownloadSpeed.setText(String.format("%s(剩%s)", SpeedUtils.getSpeed(downloadEntity.getSpeed()),
SpeedUtils.getRemainTime(downloadEntity.getSize(), downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
}
download_percentage.setText(downloadEntity.getPercent() + "%");
holder.gameDownloadPercentage.setText(downloadEntity.getPercent() + "%");
}
if (isNormal) {
downloadBtn.setText("下载中");
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
downloadBtn.setTextColor(context.getResources().getColorStateList(R.color.text_downloading_style));
holder.gameDownloadBtn.setText("下载中");
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
holder.gameDownloadBtn.setTextColor(context.getResources().getColorStateList(R.color.text_downloading_style));
}
} else if (status.equals(DownloadStatus.waiting)) {
game_progressbar.setProgress((int) (downloadEntity.getPercent() * 10));
holder.gameProgressbar.setProgress((int) (downloadEntity.getPercent() * 10));
if (isShowPlatform && platform != null) {
download_speed.setText(String.format("%s - 等待", platform));
holder.gameDownloadSpeed.setText(String.format("%s - 等待", platform));
} else {
download_speed.setText("等待");
holder.gameDownloadSpeed.setText("等待");
}
download_percentage.setText(downloadEntity.getPercent() + "%");
holder.gameDownloadPercentage.setText(downloadEntity.getPercent() + "%");
if (isNormal) {
downloadBtn.setText("下载中");
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
downloadBtn.setTextColor(context.getResources().getColorStateList(R.color.text_downloading_style));
holder.gameDownloadBtn.setText("下载中");
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
holder.gameDownloadBtn.setTextColor(context.getResources().getColorStateList(R.color.text_downloading_style));
}
} else if (status.equals(DownloadStatus.pause)
|| status.equals(DownloadStatus.timeout)
|| status.equals(DownloadStatus.neterror)) {
game_progressbar.setProgress((int) (downloadEntity.getPercent() * 10));
holder.gameProgressbar.setProgress((int) (downloadEntity.getPercent() * 10));
if (isShowPlatform && platform != null) {
download_speed.setText(String.format("%s - 暂停", platform));
holder.gameDownloadSpeed.setText(String.format("%s - 暂停", platform));
} else {
download_speed.setText("暂停");
holder.gameDownloadSpeed.setText("暂停");
}
download_percentage.setText(downloadEntity.getPercent() + "%");
holder.gameDownloadPercentage.setText(downloadEntity.getPercent() + "%");
if (isNormal) {
downloadBtn.setText("下载中");
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
downloadBtn.setTextColor(context.getResources().getColorStateList(R.color.text_downloading_style));
holder.gameDownloadBtn.setText("下载中");
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
holder.gameDownloadBtn.setTextColor(context.getResources().getColorStateList(R.color.text_downloading_style));
}
} else if (status.equals(DownloadStatus.done)) {
game_progressbar.setProgress(1000);
holder.gameProgressbar.setProgress(1000);
if (isShowPlatform && platform != null) {
download_speed.setText(String.format("%s - 下载完成", platform));
holder.gameDownloadSpeed.setText(String.format("%s - 下载完成", platform));
} else {
download_speed.setText("下载完成");
holder.gameDownloadSpeed.setText("下载完成");
}
download_percentage.setText(R.string.hundred_percent);
holder.gameDownloadPercentage.setText(R.string.hundred_percent);
if (isNormal) {
downloadBtn.setText("安装");
downloadBtn.setTextColor(Color.WHITE);
holder.gameDownloadBtn.setText("安装");
holder.gameDownloadBtn.setTextColor(Color.WHITE);
if (downloadEntity.isPluggable()
&& PackageManager.isInstalled(downloadEntity.getPackageName())) {
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
} else {
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
}
}
}
}
public static void updateItem(Context context,
TextView textView,
ProgressBar game_progressbar,
LinearLayout game_ll_info,
TextView download_speed,
TextView download_percentage,
TextView downloadBtn,
GameEntity entity,
boolean isShowPlatform) {
public static void updateItem(Context context, GameEntity gameEntity, GameViewHolder holder, boolean isShowPlatform) {
// 控制是否显示下载按钮
if (!Config.isShow(context) || "光环助手".equals(entity.getName())) {
downloadBtn.setVisibility(View.GONE);
if (!Config.isShow(context) || "光环助手".equals(gameEntity.getName())) {
holder.gameDownloadBtn.setVisibility(View.GONE);
} else {
downloadBtn.setVisibility(View.VISIBLE);
holder.gameDownloadBtn.setVisibility(View.VISIBLE);
}
if (entity.getApk() == null || entity.getApk().isEmpty()) {
textView.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_style);
downloadBtn.setText("暂无");
downloadBtn.setClickable(false);
} else if (entity.getApk().size() == 1) {
updateNormalItem(context, textView, game_progressbar, game_ll_info, download_speed,
download_percentage, downloadBtn, entity, isShowPlatform);
LibaoDao libaoDao = new LibaoDao(context);
if (libaoDao.isExist(gameEntity.getId())) {
holder.gameLibaoIcon.setVisibility(View.VISIBLE);
} else {
updatePluginItem(context, textView, game_progressbar, game_ll_info, download_speed,
download_percentage, downloadBtn, entity, isShowPlatform);
if (gameEntity.isLibaoExists()) {
holder.gameLibaoIcon.setVisibility(View.VISIBLE);
} else {
holder.gameLibaoIcon.setVisibility(View.GONE);
}
}
}
if (gameEntity.getApk() == null || gameEntity.getApk().isEmpty()) {
holder.gameDes.setVisibility(View.VISIBLE);
holder.gameProgressbar.setVisibility(View.GONE);
holder.gameInfo.setVisibility(View.GONE);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_style);
holder.gameDownloadBtn.setText("暂无");
holder.gameDownloadBtn.setClickable(false);
} else if (gameEntity.getApk().size() == 1) {
updateNormalItem(context, holder, gameEntity, isShowPlatform);
} else {
updatePluginItem(context, holder, gameEntity, isShowPlatform);
}
public static void updateItem(Context context,
TextView textView,
ProgressBar game_progressbar,
LinearLayout game_ll_info,
TextView download_speed,
TextView download_percentage,
TextView downloadBtn,
GameEntity entity) {
updateItem(context, textView, game_progressbar, game_ll_info, download_speed,
download_percentage, downloadBtn, entity, true);
}
private static void setNormalOnClickListener(final Context context,