提交项目

This commit is contained in:
huangzhuanghua
2016-04-25 11:18:59 +08:00
commit 3f29f7b39a
660 changed files with 68059 additions and 0 deletions

View File

@ -0,0 +1,793 @@
package com.gh.common.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.gh.common.constant.Config;
import com.gh.common.constant.Constants;
import com.gh.common.view.DownloadDialog;
import com.gh.download.DownloadEntry;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.R;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.DismissEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBPutUrl;
import com.gh.gamecenter.eventbus.EBRedDot;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.PackageManager;
import com.tendcloud.tenddata.TCAgent;
import de.greenrobot.event.EventBus;
public class DownloadItemUtils {
public static void initializeGameMap(Context context,
ArrayMap<String, ArrayMap<String, DownloadEntry>> gameMap) {
gameMap.clear();
List<DownloadEntry> list = DownloadManager.getInstance(context)
.getAll();
for (int i = 0, size = list.size(); i < size; i++) {
ArrayMap<String, DownloadEntry> map = gameMap.get(list.get(i)
.getName());
if (map == null) {
map = new ArrayMap<String, DownloadEntry>();
}
map.put(list.get(i).getMeta().get("platform"), list.get(i));
gameMap.put(list.get(i).getName(), map);
}
}
public static void processDate(GameEntity detailedEntity,
DownloadEntry downloadEntry,
ArrayMap<String, LinkedBlockingQueue<String>> platformMap,
Handler handler,
RecyclerView.Adapter<RecyclerView.ViewHolder> adapter, int index,
ArrayMap<String, String> statusMap) {
String platform = downloadEntry.getMeta().get("platform");
LinkedBlockingQueue<String> queue = platformMap.get(downloadEntry
.getName());
if (queue == null) {
queue = new LinkedBlockingQueue<String>();
}
ArrayMap<String, DownloadEntry> entryMap = detailedEntity.getEntryMap();
switch (downloadEntry.getStatus()) {
case pause:
case cancel:
case done:
queue.remove(platform);
platformMap.put(downloadEntry.getName(), queue);
if (entryMap == null) {
entryMap = new ArrayMap<String, DownloadEntry>();
detailedEntity.setEntryMap(entryMap);
}
entryMap.put(platform, downloadEntry);
if (!"pause".equals(statusMap.get(downloadEntry.getUrl()))) {
adapter.notifyItemChanged(index);
}
break;
default:
if (!queue.contains(platform)) {
queue.offer(platform);
if (queue.size() == 2) {
Message msg = Message.obtain();
msg.obj = downloadEntry.getName();
msg.what = Constants.DOWNLOAD_ROLL;
handler.sendMessageDelayed(msg, 3000);
}
}
platformMap.put(downloadEntry.getName(), queue);
if (platform.equals(queue.peek())) {
if (entryMap == null) {
entryMap = new ArrayMap<String, DownloadEntry>();
detailedEntity.setEntryMap(entryMap);
}
entryMap.put(platform, downloadEntry);
if (!"pause".equals(statusMap.get(downloadEntry.getUrl()))) {
adapter.notifyItemChanged(index);
}
}
break;
}
}
public static void updateItem(Context context, LinearLayout labelList,
ProgressBar game_progressbar, LinearLayout game_ll_info,
TextView download_speed, TextView download_percentage,
TextView downloadBtn, GameEntity entity,
ArrayMap<String, LinkedBlockingQueue<String>> platformMap,
ArrayMap<String, String> statusMap, boolean isShowPlatform) {
// 控制是否显示下载按钮
if (Config.isShow) {
downloadBtn.setVisibility(View.VISIBLE);
} else {
downloadBtn.setVisibility(View.GONE);
}
if (entity.getApk() != null
&& entity.getApk().size() == 1
&& PackageManager.isInstalled(entity.getApk().get(0)
.getPackageName())) {
labelList.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
downloadBtn.setText("启动");
downloadBtn.setBackgroundResource(R.drawable.textview_orange_style);
downloadBtn.setTextColor(0xFFFFFFFF);
} else {
ArrayMap<String, DownloadEntry> entryMap = entity.getEntryMap();
if (entryMap != null && !entryMap.isEmpty()) {
DownloadEntry downloadEntry;
LinkedBlockingQueue<String> queue = platformMap.get(entity
.getName());
if (queue != null && !queue.isEmpty()) {
downloadEntry = entryMap.get(queue.peek());
} else {
downloadEntry = entryMap.get(entryMap.keyAt(0));
}
if (downloadEntry != null) {
labelList.setVisibility(View.GONE);
game_progressbar.setVisibility(View.VISIBLE);
game_ll_info.setVisibility(View.VISIBLE);
String platform = PlatformUtils.getInstance(context)
.getPlatformName(
downloadEntry.getMeta().get("platform"));
switch (downloadEntry.getStatus()) {
case downloading:
if (!"pause".equals(statusMap.get(downloadEntry
.getUrl()))) {
if (isShowPlatform) {
if (platform == null) {
download_speed
.setText(SpeedUtils
.getSpeed(downloadEntry
.getSpeed())
+ "(剩"
+ SpeedUtils.getRemainTime(
downloadEntry
.getSize(),
downloadEntry
.getProgress(),
downloadEntry
.getSpeed() * 1024)
+ ")");
} else {
download_speed
.setText(platform
+ " - "
+ SpeedUtils
.getSpeed(downloadEntry
.getSpeed())
+ "(剩"
+ SpeedUtils.getRemainTime(
downloadEntry
.getSize(),
downloadEntry
.getProgress(),
downloadEntry
.getSpeed() * 1024)
+ ")");
}
} else {
download_speed
.setText(SpeedUtils
.getSpeed(downloadEntry
.getSpeed())
+ "(剩"
+ SpeedUtils.getRemainTime(
downloadEntry.getSize(),
downloadEntry
.getProgress(),
downloadEntry
.getSpeed() * 1024)
+ ")");
}
}
download_percentage.setText(downloadEntry.getPercent()
+ "%");
if (entity.getApk() != null
&& entity.getApk().size() == 1) {
downloadBtn.setText("暂停");
downloadBtn
.setBackgroundResource(R.drawable.textview_gray_style);
downloadBtn.setTextColor(0xFF999999);
} else {
downloadBtn.setText("下载");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
}
break;
case waiting:
if (isShowPlatform) {
if (platform == null) {
download_speed.setText("等待");
} else {
download_speed.setText(platform + " - 等待");
}
} else {
download_speed.setText("等待");
}
download_percentage.setText(downloadEntry.getPercent()
+ "%");
if (entity.getApk() != null
&& entity.getApk().size() == 1) {
downloadBtn.setText("取消");
downloadBtn
.setBackgroundResource(R.drawable.textview_gray_style);
downloadBtn.setTextColor(0xFF999999);
} else {
downloadBtn.setText("下载");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
}
break;
case done:
if (isShowPlatform) {
if (platform == null) {
download_speed.setText("下载完成");
} else {
download_speed.setText(platform + " - 下载完成");
}
} else {
download_speed.setText("下载完成");
}
download_percentage.setText("100%");
if (entity.getApk() != null
&& entity.getApk().size() == 1) {
EventBus.getDefault().post(
new EBPutUrl(entity.getApk().get(0)
.getPackageName(), downloadEntry
.getUrl()));
downloadBtn.setText("安装");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
} else {
labelList.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
downloadBtn.setText("下载");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
}
break;
case pause:
case neterror:
if (isShowPlatform) {
if (platform == null) {
download_speed.setText("暂停");
} else {
download_speed.setText(platform + " - 暂停");
}
} else {
download_speed.setText("暂停");
}
download_percentage.setText(downloadEntry.getPercent()
+ "%");
if (entity.getApk() != null
&& entity.getApk().size() == 1) {
downloadBtn.setText("继续");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
} else {
downloadBtn.setText("下载");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
}
break;
case cancel:
case hijack:
labelList.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
downloadBtn.setText("下载");
downloadBtn.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
break;
default:
break;
}
game_progressbar.setProgress((int) (downloadEntry
.getPercent() * 10));
}
} else {
labelList.setVisibility(View.VISIBLE);
game_progressbar.setVisibility(View.GONE);
game_ll_info.setVisibility(View.GONE);
if (entity.getApk() != null) {
downloadBtn.setText("下载");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
}
}
}
}
public static void updateItem(Context context, LinearLayout labelList,
ProgressBar game_progressbar, LinearLayout game_ll_info,
TextView download_speed, TextView download_percentage,
TextView downloadBtn, GameEntity entity,
ArrayMap<String, LinkedBlockingQueue<String>> platformMap,
ArrayMap<String, String> statusMap) {
updateItem(context, labelList, game_progressbar, game_ll_info,
download_speed, download_percentage, downloadBtn, entity,
platformMap, statusMap, true);
}
public static void setOnClickListener(Context context,
TextView downloadBtn, TextView download_speed,
TextView download_percentage, GameEntity entity, int position,
RecyclerView.Adapter<RecyclerView.ViewHolder> adapter,
Handler handler, ArrayMap<String, Long> lastTimeMap,
ArrayMap<String, String> statusMap,
ArrayMap<String, LinkedBlockingQueue<String>> platformMap,
String entrance, DismissEntity dismissEntity, String location) {
setOnClickListener(context, downloadBtn, download_speed,
download_percentage, entity, position, adapter, handler,
lastTimeMap, statusMap, platformMap, entrance, true,
dismissEntity, location);
}
public static void setOnClickListener(final Context context,
final TextView downloadBtn, final TextView download_speed,
final TextView download_percentage, final GameEntity entity,
final int position,
final RecyclerView.Adapter<RecyclerView.ViewHolder> adapter,
final Handler handler, final ArrayMap<String, Long> lastTimeMap,
final ArrayMap<String, String> statusMap,
final ArrayMap<String, LinkedBlockingQueue<String>> platformMap,
final String entrance, final boolean isShowPlatform,
final DismissEntity dismissEntity, final String location) {
downloadBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View viewBtn) {
String str = downloadBtn.getText().toString();
if ("下载".equals(str)) {
if (entity.getApk() == null || entity.getApk().isEmpty()) {
Toast.makeText(context, "稍等片刻~!游戏正在上传中...",
Toast.LENGTH_SHORT).show();
} else {
if (NetworkUtils.isWifiConnected(context)) {
if (entity.getApk().size() == 1) {
String msg = FileUtils.isCanDownload(entity
.getApk().get(0).getSize());
if (TextUtils.isEmpty(msg)) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", entity.getApk().get(0)
.getPlatform());
kv.put("状态", "下载开始");
TCAgent.onEvent(context, "游戏下载",
entity.getName(), kv);
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put("版本", entity.getApk().get(0)
.getPlatform());
kv2.put("状态", "下载开始");
kv2.put("位置", entrance + "-开始");
TCAgent.onEvent(context, "游戏下载位置",
entity.getName(), kv2);
Map<String, Object> kv3 = new HashMap<String, Object>();
kv3.put(entrance, "下载数");
kv3.put(entrance, "下载开始");
TCAgent.onEvent(context, "应用数据",
entity.getName(), kv3);
if ("主页-最新插件".equals(entrance)) {
Map<String, Object> kv4 = new HashMap<String, Object>();
kv4.put(entity.getName(), "下载数");
TCAgent.onEvent(context, "主页数据",
"最新插件", kv4);
} else if ("主页-热门卡牌".equals(entrance)) {
Map<String, Object> kv4 = new HashMap<String, Object>();
kv4.put(entity.getName(), "下载数");
TCAgent.onEvent(context, "主页数据",
"热门卡牌", kv4);
} else if ("主页-新测卡牌".equals(entrance)) {
Map<String, Object> kv4 = new HashMap<String, Object>();
kv4.put(entity.getName(), "下载数");
TCAgent.onEvent(context, "主页数据",
"新测卡牌", kv4);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("game", entity.getName());
map.put("method", "正常");
map.put("platform",
PlatformUtils
.getInstance(context)
.getPlatformName(
entity.getApk()
.get(0)
.getPlatform()));
map.put("status", "开始");
map.put("location",
"游戏详情:" + entity.getName());
map.put("from", entrance);
map.put("network", NetworkUtils
.getConnectedType(context));
map.put("createdOn",
System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(context,
"download", map);
DownloadManager.getInstance(context).add(
getDownloadEntry(context, entity,
0, entrance));
Toast.makeText(context,
entity.getName() + "已加入下载队列",
Toast.LENGTH_SHORT).show();
downloadBtn.setText("暂停");
downloadBtn
.setBackgroundResource(R.drawable.textview_gray_style);
downloadBtn.setTextColor(0xFF999999);
EventBus.getDefault().post(new EBRedDot(1));
statusMap.put(entity.getApk().get(0)
.getUrl(), "downloading");
} else {
Toast.makeText(context, msg,
Toast.LENGTH_SHORT).show();
}
} else {
if (!dismissEntity.isShow()) {
dismissEntity.setShow(true);
DownloadDialog.getInstance(context)
.showPopupWindow(viewBtn, entity,
entrance, handler,
lastTimeMap, statusMap,
download_speed,
download_percentage,
location);
}
}
} else {
final Dialog dialog = new Dialog(context);
View view = View
.inflate(
context,
R.layout.search_history_delete_dialog,
null);
TextView title = (TextView) view
.findViewById(R.id.delete_dialog_title);
title.setText("警告");
TextView content = (TextView) view
.findViewById(R.id.delete_dialog_message);
content.setText("您当前的网络为2G/3G/4G下载将会消耗移动流量是否继续下载");
TextView cancel = (TextView) view
.findViewById(R.id.delete_dialog_cancel);
cancel.setText("取消");
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView confirem = (TextView) view
.findViewById(R.id.delete_dialog_confirm);
confirem.setText("继续");
confirem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (entity.getApk() == null
|| entity.getApk().isEmpty()) {
Toast.makeText(context,
"稍等片刻~!游戏正在上传中...",
Toast.LENGTH_SHORT).show();
} else {
if (entity.getApk().size() == 1) {
String msg = FileUtils
.isCanDownload(entity
.getApk().get(0)
.getSize());
if (TextUtils.isEmpty(msg)) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", entity.getApk()
.get(0).getPlatform());
kv.put("状态", "下载开始");
TCAgent.onEvent(context,
"游戏下载",
entity.getName(), kv);
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put("版本", entity.getApk()
.get(0).getPlatform());
kv2.put("状态", "下载开始");
kv2.put("位置", entrance + "-开始");
TCAgent.onEvent(context,
"游戏下载位置",
entity.getName(), kv2);
Map<String, Object> map = new HashMap<String, Object>();
map.put("game",
entity.getName());
map.put("method", "正常");
map.put("platform",
PlatformUtils
.getInstance(
context)
.getPlatformName(
entity.getApk()
.get(0)
.getPlatform()));
map.put("status", "开始");
map.put("location", "游戏详情:"
+ entity.getName());
map.put("from", entrance);
map.put("network",
NetworkUtils
.getConnectedType(context));
map.put("createdOn",
System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(
context, "download",
map);
DownloadManager.getInstance(
context).add(
getDownloadEntry(
context,
entity, 0,
entrance));
Toast.makeText(
context,
entity.getName()
+ "已加入下载队列",
Toast.LENGTH_SHORT)
.show();
downloadBtn.setText("暂停");
downloadBtn
.setBackgroundResource(R.drawable.textview_gray_style);
downloadBtn
.setTextColor(0xFF999999);
EventBus.getDefault().post(
new EBRedDot(1));
statusMap.put(entity.getApk()
.get(0).getUrl(),
"downloading");
} else {
Toast.makeText(context, msg,
Toast.LENGTH_SHORT)
.show();
}
} else {
if (!dismissEntity.isShow()) {
dismissEntity.setShow(true);
DownloadDialog
.getInstance(context)
.showPopupWindow(
viewBtn,
entity,
entrance,
handler,
lastTimeMap,
statusMap,
download_speed,
download_percentage,
location);
}
}
}
}
});
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.show();
}
}
} else if ("暂停".equals(str)) {
ArrayMap<String, DownloadEntry> entryMap = entity
.getEntryMap();
if (entryMap != null && !entryMap.isEmpty()) {
DownloadEntry downloadEntry;
LinkedBlockingQueue<String> queue = platformMap
.get(entity.getName());
if (queue != null && !queue.isEmpty()) {
downloadEntry = entryMap.get(queue.peek());
} else {
downloadEntry = entryMap.get(entryMap.keyAt(0));
}
if (downloadEntry != null) {
String platform = PlatformUtils
.getInstance(context).getPlatformName(
downloadEntry.getMeta().get(
"platform"));
if (isShowPlatform) {
if (platform == null) {
download_speed.setText("暂停");
} else {
download_speed.setText(platform + " - 暂停");
}
} else {
download_speed.setText("暂停");
}
download_percentage.setText(downloadEntry
.getPercent() + "%");
}
}
downloadBtn.setText("继续");
downloadBtn
.setBackgroundResource(R.drawable.textview_blue_style);
downloadBtn.setTextColor(0xFFFFFFFF);
statusMap.put(entity.getApk().get(0).getUrl(), "pause");
Message msg = Message.obtain();
msg.what = Constants.PAUSE_DOWNLOAD_TASK;
msg.obj = entity.getApk().get(0).getUrl();
lastTimeMap.put(entity.getApk().get(0).getUrl(),
System.currentTimeMillis());
handler.sendMessageDelayed(msg, 1000);
} else if ("继续".equals(str)) {
ArrayMap<String, DownloadEntry> entryMap = entity
.getEntryMap();
if (entryMap != null && !entryMap.isEmpty()) {
DownloadEntry downloadEntry;
LinkedBlockingQueue<String> queue = platformMap
.get(entity.getName());
if (queue != null && !queue.isEmpty()) {
downloadEntry = entryMap.get(queue.peek());
} else {
downloadEntry = entryMap.get(entryMap.keyAt(0));
}
if (downloadEntry != null) {
String platform = PlatformUtils
.getInstance(context).getPlatformName(
downloadEntry.getMeta().get(
"platform"));
if (isShowPlatform) {
if (platform == null) {
download_speed.setText(SpeedUtils
.getSpeed(downloadEntry.getSpeed())
+ "(剩"
+ SpeedUtils.getRemainTime(
downloadEntry.getSize(),
downloadEntry.getProgress(),
downloadEntry.getSpeed() * 1024)
+ ")");
} else {
download_speed.setText(platform
+ " - "
+ SpeedUtils.getSpeed(downloadEntry
.getSpeed())
+ "(剩"
+ SpeedUtils.getRemainTime(
downloadEntry.getSize(),
downloadEntry.getProgress(),
downloadEntry.getSpeed() * 1024)
+ ")");
}
} else {
download_speed.setText(SpeedUtils
.getSpeed(downloadEntry.getSpeed())
+ "(剩"
+ SpeedUtils.getRemainTime(
downloadEntry.getSize(),
downloadEntry.getProgress(),
downloadEntry.getSpeed() * 1024)
+ ")");
}
download_percentage.setText(downloadEntry
.getPercent() + "%");
}
}
downloadBtn.setText("暂停");
downloadBtn
.setBackgroundResource(R.drawable.textview_gray_style);
downloadBtn.setTextColor(0xFF999999);
statusMap.put(entity.getApk().get(0).getUrl(),
"downloading");
EventBus.getDefault().post(new EBRedDot(0));
Message msg = Message.obtain();
msg.what = Constants.CONTINUE_DOWNLOAD_TASK;
msg.obj = entity.getApk().get(0).getUrl();
lastTimeMap.put(entity.getApk().get(0).getUrl(),
System.currentTimeMillis());
handler.sendMessageDelayed(msg, 1000);
} else if ("安装".equals(str)) {
String path = entity.getEntryMap()
.get(entity.getEntryMap().keyAt(0)).getPath();
if (FileUtils.isEmptyFile(path)) {
Toast.makeText(context, "解析包错误", Toast.LENGTH_SHORT)
.show();
DownloadManager.getInstance(context).cancel(
entity.getEntryMap()
.get(entity.getEntryMap().keyAt(0))
.getUrl());
entity.getEntryMap().remove(
entity.getEntryMap().keyAt(0));
adapter.notifyItemChanged(position);
} else {
PackageManager manager = new PackageManager(context);
if (manager.launchSetup(path)) {
context.startActivity(PackageUtils
.getInstallIntent(path));
} else {
DialogUtils.showDialog(context, path);
}
}
} else if ("启动".equals(str)) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", entity.getApk().get(0).getPlatform());
TCAgent.onEvent(context, "游戏启动", entity.getName(), kv);
PackageUtils.launchApplicationByPackageName(context, entity
.getApk().get(0).getPackageName());
}
}
});
}
public static DownloadEntry getDownloadEntry(Context context,
GameEntity game, int position, String entrance) {
ApkEntity apkEntity = game.getApk().get(position);
DownloadEntry entry = new DownloadEntry();
entry.setUrl(apkEntity.getUrl());
entry.setName(game.getName());
entry.setPath(FileUtils.getDownloadPath(
context,
MD5Utils.getContentMD5(game.getName() + "_"
+ System.currentTimeMillis())
+ ".apk"));
HashMap<String, String> meta = new HashMap<String, String>();
meta.put("ETag", apkEntity.getEtag());
meta.put("icon", game.getIcon());
meta.put("platform", apkEntity.getPlatform());
meta.put("gameId", game.getId());
meta.put("entrance", entrance);
meta.put("location", "游戏详情:" + game.getName());
entry.setMeta(meta);
return entry;
}
}