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

483 lines
24 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.Context;
import android.graphics.Color;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.gh.common.constant.Config;
import com.gh.common.exposure.ExposureEvent;
import com.gh.common.exposure.ExposureUtils;
import com.gh.common.view.DownloadDialog;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.DownloadManagerActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.GameViewHolder;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.manager.PackageManager;
import com.lightgame.download.DownloadConfig;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.DownloadStatus;
import com.lightgame.download.FileUtils;
import com.lightgame.utils.Utils;
import java.util.concurrent.LinkedBlockingQueue;
public class DownloadItemUtils {
// 更新下载进度条
public static void processDate(Context context,
GameEntity gameEntity,
DownloadEntity downloadEntity,
RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter,
int index) {
if (!gameEntity.getId().equals(downloadEntity.getGameId())) {
adapter.notifyItemChanged(index);
return;
}
LinkedBlockingQueue<String> queue = DownloadManager.getInstance(context).getQueue(downloadEntity.getName());
if (queue == null) {
queue = new LinkedBlockingQueue<>();
DownloadManager.getInstance(context).putQueue(downloadEntity.getName(), queue);
}
String platform = downloadEntity.getPlatform();
ArrayMap<String, DownloadEntity> entryMap = gameEntity.getEntryMap();
DownloadStatus status = downloadEntity.getStatus();
if (status.equals(DownloadStatus.pause)
|| status.equals(DownloadStatus.cancel)
|| status.equals(DownloadStatus.done)) {
queue.remove(platform);
if (entryMap == null) {
entryMap = new ArrayMap<>();
gameEntity.setEntryMap(entryMap);
}
entryMap.put(platform, downloadEntity);
adapter.notifyItemChanged(index);
// if (!DownloadStatus.pause.equals(DownloadManager.getInstance(context).getStatus(downloadEntity.getUrl()))) {
// adapter.notifyItemChanged(index);
// }
} else {
if (!queue.contains(platform)) {
queue.offer(platform);
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(DownloadItemUtils.class, queue.size(), gameEntity.getBrief(), downloadEntity.getPlatform(), index);
}
// 有两个平台同时下载的时候启用
if (queue.size() == 2) {
//TODO fuck this
Message msg = Message.obtain();
msg.obj = downloadEntity.getName();
msg.what = DownloadConfig.DOWNLOAD_ROLL;
DownloadManager.getInstance(context).sendMessageDelayed(msg, 3000);
}
}
if (platform.equals(queue.peek())) {
if (entryMap == null) {
entryMap = new ArrayMap<>();
gameEntity.setEntryMap(entryMap);
}
entryMap.put(platform, downloadEntity);
if (!DownloadStatus.pause.equals(DownloadManager.getInstance(context).getStatus(downloadEntity.getUrl()))) {
adapter.notifyItemChanged(index);
}
}
}
}
public static void updateItem(Context context, GameEntity gameEntity, GameViewHolder holder, boolean isShowPlatform) {
// 控制是否显示下载按钮
if (!Config.isShowDownload(gameEntity.getId()) || context.getString(R.string.app_name).equals(gameEntity.getName())) {
holder.gameDownloadBtn.setVisibility(View.GONE);
} else {
holder.gameDownloadBtn.setVisibility(View.VISIBLE);
}
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.news_detail_comment);
holder.gameDownloadBtn.setText("暂无");
holder.gameDownloadBtn.setTextColor(ContextCompat.getColor(context, R.color.button_gray));
holder.gameDownloadBtn.setClickable(false);
} else if (gameEntity.getApk().size() == 1) {
updateNormalItem(context, holder, gameEntity, isShowPlatform);
} else {
// updateNormalItem(context, holder, gameEntity, isShowPlatform);
updatePluginItem(context, holder, gameEntity, isShowPlatform);
}
}
// 更新正常的条目只有一个apk包
static void updateNormalItem(Context context, GameViewHolder holder, GameEntity gameEntity,
boolean isShowPlatform) {
final ArrayMap<String, DownloadEntity> entryMap = gameEntity.getEntryMap();
final ApkEntity apkEntity = gameEntity.getApk().get(0);
if (entryMap != null && !entryMap.isEmpty()) {
DownloadEntity downloadEntity = entryMap.get(apkEntity.getPlatform());
if (downloadEntity != null) {
// 更改进度条和提示文本的状态
changeStatus(context, holder, downloadEntity, isShowPlatform, true);
return;
}
}
holder.gameDes.setVisibility(View.VISIBLE);
holder.gameProgressbar.setVisibility(View.GONE);
holder.gameInfo.setVisibility(View.GONE);
holder.gameDownloadBtn.setTextColor(Color.WHITE);
final String packageName = apkEntity.getPackageName();
if (gameEntity.isPluggable()) {
holder.gameDownloadBtn.setText(R.string.pluggable);
setwhat(context, holder, apkEntity, packageName);
} else if (PackageManager.INSTANCE.isInstalled(packageName)) {
if (PackageManager.INSTANCE.isCanUpdate(gameEntity.getId(), packageName)) {
holder.gameDownloadBtn.setText(R.string.update);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
} else {
Object gh_id = PackageUtils.getMetaData(context, packageName, "gh_id");
if (gameEntity.getTag() != null && gameEntity.getTag().size() != 0
&& !TextUtils.isEmpty(apkEntity.getGhVersion())
&& !PackageUtils.isSignature(context, packageName)) {
holder.gameDownloadBtn.setText(R.string.pluggable);
setwhat(context, holder, apkEntity, packageName);
} else if (gh_id == null || gh_id.equals(gameEntity.getId())) {
holder.gameDownloadBtn.setText(R.string.launch);
holder.gameDownloadBtn.setTextColor(ContextCompat.getColor(context, R.color.theme));
holder.gameDownloadBtn.setBackgroundResource(R.drawable.detail_download_open_style);
} else {
holder.gameDownloadBtn.setText(R.string.download);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
}
}
} else {
holder.gameDownloadBtn.setText(R.string.download);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
}
}
/**
* 这个干什么鬼?
*
* @param context
* @param holder
* @param apkEntity
* @param packageName
*/
public static void setwhat(Context context, GameViewHolder holder, ApkEntity apkEntity, String packageName) {
DownloadEntity downloadEntity = DownloadManager.getInstance(context).getDownloadEntityByPackageName(packageName);
if (downloadEntity == null || downloadEntity.getUrl().equals(apkEntity.getUrl())) {
holder.gameDownloadBtn.setClickable(true);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
} else {
holder.gameDownloadBtn.setClickable(false);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_pause_up);
}
}
// 更新插件的条目有多个apk包
static void updatePluginItem(Context context, GameViewHolder holder, GameEntity gameEntity,
boolean isShowPlatform) {
GameUtils.setDownloadBtnStatus(context, gameEntity, holder.gameDownloadBtn);
ArrayMap<String, DownloadEntity> entryMap = gameEntity.getEntryMap();
if (entryMap != null && !entryMap.isEmpty()) {
DownloadEntity downloadEntity;
LinkedBlockingQueue<String> queue = DownloadManager.getInstance(context).getQueue(gameEntity.getName());
if (queue != null && !queue.isEmpty()) {
downloadEntity = entryMap.get(queue.peek());
} else {
downloadEntity = entryMap.get(entryMap.keyAt(0));
}
if (downloadEntity != null) {
// 更改进度条和提示文本的状态
changeStatus(context, holder, downloadEntity, isShowPlatform, false);
return;
}
}
holder.gameDes.setVisibility(View.VISIBLE);
holder.gameProgressbar.setVisibility(View.GONE);
holder.gameInfo.setVisibility(View.GONE);
}
// 更改进度条和提示文本的状态
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());
DownloadStatus status = downloadEntity.getStatus();
if (status.equals(DownloadStatus.downloading)) {
if (!DownloadStatus.pause.equals(DownloadManager.getInstance(context).getStatus(downloadEntity.getUrl()))) {
holder.gameProgressbar.setProgress((int) (downloadEntity.getPercent() * 10));
if (isShowPlatform && platform != null) {
holder.gameDownloadSpeed.setText(String.format("%s - %s(剩%s)", platform,
SpeedUtils.getSpeed(downloadEntity.getSpeed()),
SpeedUtils.getRemainTime(downloadEntity.getSize(), downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
} else {
holder.gameDownloadSpeed.setText(String.format("%s(剩%s)", SpeedUtils.getSpeed(downloadEntity.getSpeed()),
SpeedUtils.getRemainTime(downloadEntity.getSize(), downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
}
holder.gameDownloadPercentage.setText(downloadEntity.getPercent() + "%");
}
if (isNormal) {
holder.gameDownloadBtn.setText(R.string.downloading);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
holder.gameDownloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style));
}
} else if (status.equals(DownloadStatus.waiting)) {
holder.gameProgressbar.setProgress((int) (downloadEntity.getPercent() * 10));
if (isShowPlatform && platform != null) {
holder.gameDownloadSpeed.setText(String.format("%s - 等待", platform));
} else {
holder.gameDownloadSpeed.setText("等待");
}
holder.gameDownloadPercentage.setText(downloadEntity.getPercent() + "%");
if (isNormal) {
holder.gameDownloadBtn.setText(R.string.downloading);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
holder.gameDownloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style));
}
} else if (status.equals(DownloadStatus.pause)
|| status.equals(DownloadStatus.timeout)
|| status.equals(DownloadStatus.neterror)) {
holder.gameProgressbar.setProgress((int) (downloadEntity.getPercent() * 10));
if (isShowPlatform && platform != null) {
holder.gameDownloadSpeed.setText(String.format("%s - 暂停", platform));
} else {
holder.gameDownloadSpeed.setText("暂停");
}
holder.gameDownloadPercentage.setText(downloadEntity.getPercent() + "%");
if (isNormal) {
holder.gameDownloadBtn.setText(R.string.downloading);
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
holder.gameDownloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style));
}
} else if (status.equals(DownloadStatus.done)) {
holder.gameProgressbar.setProgress(1000);
if (isShowPlatform && platform != null) {
holder.gameDownloadSpeed.setText(String.format("%s - 下载完成", platform));
} else {
holder.gameDownloadSpeed.setText("下载完成");
}
holder.gameDownloadPercentage.setText(R.string.hundred_percent);
if (isNormal) {
holder.gameDownloadBtn.setText("安装");
holder.gameDownloadBtn.setTextColor(Color.WHITE);
if (downloadEntity.isPluggable()
&& PackageManager.INSTANCE.isInstalled(downloadEntity.getPackageName())) {
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
} else {
holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style);
}
}
}
}
public static void setOnClickListener(final Context context,
final TextView downloadBtn,
final GameEntity gameEntity,
final int position,
final RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter,
final String entrance,
final String location) {
setOnClickListener(context, downloadBtn, gameEntity, position, adapter, entrance, location, null);
}
public static void setOnClickListener(final Context context,
final TextView downloadBtn,
final GameEntity gameEntity,
final int position,
final RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter,
final String entrance,
final String location,
final ExposureEvent traceEvent) {
if (gameEntity.getApk().size() == 1) {
downloadBtn.setOnClickListener(v -> onNormalClick(context, downloadBtn, gameEntity, position, adapter, entrance, location, traceEvent));
} else {
downloadBtn.setOnClickListener(v -> DownloadDialog.getInstance(context).showPopupWindow(v, gameEntity, entrance, location, traceEvent));
}
}
public static void onNormalClick(final Context context,
final TextView downloadBtn,
final GameEntity gameEntity,
final int position,
final RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter,
final String entrance,
final String location) {
onNormalClick(context, downloadBtn, gameEntity, position, adapter, entrance, location, null);
}
public static void onNormalClick(final Context context,
final TextView downloadBtn,
final GameEntity gameEntity,
final int position,
final RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter,
final String entrance,
final String location,
@Nullable final ExposureEvent traceEvent) {
String str = downloadBtn.getText().toString();
switch (str) {
case "下载":
if (NetworkUtils.isWifiConnected(context)) {
download(context, gameEntity, downloadBtn, entrance, location, traceEvent);
} else {
DialogUtils.showDownloadDialog(context, () -> download(context, gameEntity, downloadBtn, entrance, location, traceEvent));
}
break;
case "插件化":
if (entrance.contains("我的游戏")) {
DataUtils.onMtaEvent(context, "我的游戏_启动", "插件化", gameEntity.getName());
}
if (NetworkUtils.isWifiConnected(context)) {
plugin(context, gameEntity, downloadBtn, entrance, location, traceEvent);
} else {
DialogUtils.showDownloadDialog(context, () -> plugin(context, gameEntity, downloadBtn, entrance, location, traceEvent));
}
break;
case "安装":
install(context, gameEntity, position, adapter);
break;
case "启动":
if (entrance.contains("我的游戏")) {
DataUtils.onMtaEvent(context, "我的游戏_启动", "启动", gameEntity.getName());
}
DataUtils.onGameLaunchEvent(context, gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), location);
PackageUtils.launchApplicationByPackageName(context, gameEntity.getApk().get(0).getPackageName());
break;
case "下载中":
context.startActivity(
DownloadManagerActivity.getDownloadMangerIntent(context, gameEntity.getApk().get(0).getUrl(), entrance + "+(" + location.split(":")[0] + ")"));
break;
case "更新":
if (entrance.contains("我的游戏")) {
DataUtils.onMtaEvent(context, "我的游戏_启动", "更新", gameEntity.getName());
}
if (NetworkUtils.isWifiConnected(context)) {
update(context, gameEntity, entrance, location, traceEvent);
} else {
DialogUtils.showDownloadDialog(context, () -> update(context, gameEntity, entrance, location, traceEvent));
}
break;
}
}
//下载
private static void download(Context context,
GameEntity gameEntity,
TextView downloadBtn,
String entrance,
String location,
@Nullable ExposureEvent traceEvent) {
String msg = FileUtils.isCanDownload(context, gameEntity.getApk().get(0).getSize());
if (TextUtils.isEmpty(msg)) {
DataUtils.onGameDownloadEvent(context, gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), entrance, "下载开始");
ExposureEvent downloadExposureEvent = ExposureUtils.INSTANCE.logADownloadExposureEvent(gameEntity, gameEntity.getApk().get(0).getPlatform(), traceEvent, ExposureUtils.DownloadType.DOWNLOAD);
DownloadManager.createDownload(context, gameEntity, context.getString(R.string.download), entrance, location, downloadExposureEvent);
Utils.toast(context, gameEntity.getName() + "已加入下载队列");
downloadBtn.setText(R.string.downloading);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
downloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style));
// DownloadManager.getInstance(context).putStatus(gameEntity.getApk().get(0).getUrl(), "downloading");
} else {
Utils.toast(context, msg);
}
}
//插件化
private static void plugin(Context context, GameEntity gameEntity, TextView downloadBtn, String entrance,
String location, @Nullable ExposureEvent traceEvent) {
String msg = FileUtils.isCanDownload(context, gameEntity.getApk().get(0).getSize());
if (TextUtils.isEmpty(msg)) {
DataUtils.onGameDownloadEvent(context, gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), entrance, "下载开始");
ExposureEvent downloadExposureEvent = ExposureUtils.INSTANCE.logADownloadExposureEvent(gameEntity, gameEntity.getApk().get(0).getPlatform(), traceEvent, ExposureUtils.DownloadType.PLUGIN_DOWNLOAD);
DownloadManager.createDownload(context, gameEntity, "插件化", entrance, location, downloadExposureEvent);
Utils.toast(context, gameEntity.getName() + "已加入下载队列");
downloadBtn.setText(R.string.downloading);
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style);
downloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style));
// DownloadManager.getInstance(context).putStatus(gameEntity.getApk().get(0).getUrl(), "downloading");
} else {
Utils.toast(context, msg);
}
}
//安装
private static void install(final Context context, GameEntity gameEntity, int position,
RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter) {
ApkEntity apkEntity = gameEntity.getApk().get(0);
DownloadEntity downloadEntity = DownloadManager.getInstance(context).getDownloadEntityByUrl(apkEntity.getUrl());
if (downloadEntity != null) {
final String path = downloadEntity.getPath();
if (FileUtils.isEmptyFile(path)) {
Utils.toast(context, R.string.install_failure_hint);
DownloadManager.getInstance(context).cancel(downloadEntity.getUrl());
if (gameEntity.getEntryMap() != null) {
gameEntity.getEntryMap().remove(apkEntity.getPlatform());
}
adapter.notifyItemChanged(position);
} else {
PackageUtils.launchSetup(context, path);
}
}
}
//更新
private static void update(Context context, GameEntity gameEntity, String entrance, String location, @Nullable ExposureEvent traceEvent) {
DataUtils.onGameUpdateEvent(context, gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), "下载开始");
ExposureEvent downloadExposureEvent = ExposureUtils.INSTANCE.logADownloadExposureEvent(gameEntity, gameEntity.getApk().get(0).getPlatform(), traceEvent, ExposureUtils.DownloadType.UPDATE);
DownloadManager.createDownload(context, gameEntity, "更新", entrance, location, downloadExposureEvent);
}
}