1、对下载器任务的状态变化修改

2、对下载器内部的handler的消息屏蔽,pause的修改,剩余download roll未改动。
This commit is contained in:
CsHeng
2017-07-02 09:49:21 +08:00
parent 7a6354e781
commit 714a64975f
13 changed files with 236 additions and 176 deletions

View File

@ -386,7 +386,7 @@ public abstract class BaseDetailActivity extends BaseActivity implements View.On
detail_pb_progressbar.setProgress(0);
detail_tv_per.setText("0.0%");
DownloadManager.getInstance(BaseDetailActivity.this).putStatus(apkEntity.getUrl(), "downloading");
// DownloadManager.getInstance(BaseDetailActivity.this).putStatus(apkEntity.getUrl(), "downloading");
} else {
toast(msg);
}

View File

@ -246,7 +246,7 @@ public class DetailDownloadUtils {
mViewHolder.downloadPb.setProgress(0);
mViewHolder.downloadPer.setText("0.0%");
DownloadManager.getInstance(mViewHolder.context).putStatus(apkEntity.getUrl(), "downloading");
// DownloadManager.getInstance(mViewHolder.context).putStatus(apkEntity.getUrl(), "downloading");
} else {
Utils.toast(mViewHolder.context, msg);
}

View File

@ -400,6 +400,10 @@ public class DialogUtils {
}, null);
}
public static void showDownloadDialog(Context context, ConfirmListener listener, CancelListener cancelListener){
showWarningDialog(context, "下载提示", "您当前使用的网络为2G/3G/4G开始下载将会消耗移动流量确定下载", "取消", "确定", listener, cancelListener);
}
public static void showDownloadDialog(Context context, ConfirmListener listener) {
showWarningDialog(context, "下载提示", "您当前使用的网络为2G/3G/4G开始下载将会消耗移动流量确定下载", listener);
}

View File

@ -65,7 +65,12 @@ public class DownloadItemUtils {
} 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;
@ -220,8 +225,7 @@ public class DownloadItemUtils {
holder.gameProgressbar.setVisibility(View.VISIBLE);
holder.gameInfo.setVisibility(View.VISIBLE);
String platform = PlatformUtils.getInstance(context)
.getPlatformName(downloadEntity.getPlatform());
String platform = PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform());
DownloadStatus status = downloadEntity.getStatus();
if (status.equals(DownloadStatus.downloading)) {
@ -399,7 +403,7 @@ public class DownloadItemUtils {
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");
// DownloadManager.getInstance(context).putStatus(gameEntity.getApk().get(0).getUrl(), "downloading");
} else {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
@ -419,7 +423,7 @@ public class DownloadItemUtils {
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");
// DownloadManager.getInstance(context).putStatus(gameEntity.getApk().get(0).getUrl(), "downloading");
} else {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

View File

@ -399,7 +399,7 @@ public class DownloadDialog implements OnCollectionCallBackListener {
if (collectionAdapter != null) {
collectionAdapter.removeDownloadEntityByUrl(url);
}
DownloadManager.getInstance(mContext).putStatus(url, "delete");
// DownloadManager.getInstance(mContext).putStatus(url, "delete");
}
}

View File

@ -10,6 +10,7 @@ import android.support.v4.util.ArrayMap;
import android.widget.Toast;
import com.gh.common.constant.Config;
import com.gh.common.util.AppDebugConfig;
import com.gh.common.util.DataCollectionUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.MD5Utils;
@ -36,7 +37,6 @@ import org.greenrobot.eventbus.EventBus;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.LinkedBlockingQueue;
public class DownloadManager implements DownloadStatusListener {
@ -49,8 +49,14 @@ public class DownloadManager implements DownloadStatusListener {
private ArrayMap<String, Long> lastTimeMap;
private ArrayMap<String, LinkedBlockingQueue<String>> platformMap;
private ArrayMap<String, ArrayMap<String, DownloadEntity>> gameMap;
private ArrayMap<String, String> statusMap;
/**
* TODO change to {@link DownloadStatus}
*/
private ArrayMap<String, String> statusMap;
private ArrayMap<String, DownloadEntity> downloadingMap;
private DownloadDao mDownloadDao;
@Override
public void onTaskCancelled(DownloadEntity entity) {
@ -61,6 +67,9 @@ public class DownloadManager implements DownloadStatusListener {
DownloadNotification.showDownloadingNotification(mContext);
DownloadManager.getInstance(mContext).putStatus(entity.getUrl(), "delete");
downloadingMap.remove(entity.getUrl());
}
@Override
@ -68,11 +77,15 @@ public class DownloadManager implements DownloadStatusListener {
EventBus.getDefault().post(new EBDownloadStatus("download"));
DownloadNotification.showDownloadingNotification(mContext);
downloadingMap.put(entity.getUrl(), entity);
}
@Override
public void onTaskError(DownloadEntity entity) {
DownloadNotification.showDownloadingNotification(mContext);
downloadingMap.remove(entity.getUrl());
}
@Override
@ -84,6 +97,7 @@ public class DownloadManager implements DownloadStatusListener {
public void onTaskDone(DownloadEntity entity) {
DownloadNotification.showDownloadingNotification(mContext);
DownloadNotification.showDownloadDoneNotification(mContext, entity);
downloadingMap.remove(entity.getUrl());
}
@Override
@ -92,7 +106,8 @@ public class DownloadManager implements DownloadStatusListener {
}
private DownloadManager(Context context) {
mContext = context;
mContext = context.getApplicationContext();
mDownloadDao = DownloadDao.getInstance(mContext);
//TODO unregister this
DownloadStatusManager.getInstance().registerTaskStatusListener(this);
@ -106,6 +121,7 @@ public class DownloadManager implements DownloadStatusListener {
platformMap = new ArrayMap<>();
gameMap = new ArrayMap<>();
statusMap = new ArrayMap<>();
downloadingMap = new ArrayMap<>();
mHandler = new Handler(Looper.getMainLooper()) {
@Override
@ -135,6 +151,30 @@ public class DownloadManager implements DownloadStatusListener {
}
}
};
List<DownloadEntity> list = getAll();
for (DownloadEntity downloadEntity : list) {
statusMap.put(downloadEntity.getUrl(), downloadEntity.getStatus().name());
if (!DownloadStatus.done.equals(downloadEntity.getStatus())) {
downloadingMap.put(downloadEntity.getUrl(), downloadEntity);
}
}
}
public ArrayMap<String, DownloadEntity> getDownloadingMap() {
return downloadingMap;
}
public static DownloadManager getInstance(Context context) {
if (mInstance == null) {
synchronized (DownloadManager.class) {
if (mInstance == null) {
mInstance = new DownloadManager(context.getApplicationContext());
}
}
}
return mInstance;
}
public static void createDownload(Context context,
@ -192,7 +232,16 @@ public class DownloadManager implements DownloadStatusListener {
downloadEntity.setPluggable(true);
}
downloadEntity.setPlugin(gameEntity.getTag() != null && gameEntity.getTag().size() != 0);
DownloadManager.getInstance(context.getApplicationContext()).add(downloadEntity);
DownloadManager.getInstance(context).add(downloadEntity);
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(DownloadManager.class, apkEntity.getUrl(), downloadEntity.getUrl(), method, entrance, location);
}
//TODO remove
DownloadManager.getInstance(context).putStatus(downloadEntity.getUrl(), "downloading");
// 收集下载数据
DataCollectionUtils.uploadDownload(context, downloadEntity, "开始");
}
@ -208,25 +257,16 @@ public class DownloadManager implements DownloadStatusListener {
checkDownloadEntryRecordValidate(url);
if (isFileCompleted(url)) {
downloadEntity.setStatus(DownloadStatus.done);
DataChanger.getInstance().notifyDataChanged(downloadEntity);
DataChanger.INSTANCE.notifyDataChanged(downloadEntity);
} else if (!isTaskDownloading(url)) {
mContext.startService(getIntent(downloadEntity, DownloadStatus.add));
}
put(url, System.currentTimeMillis());
putStatus(url, DownloadStatus.downloading.name());
}
Utils.log(DownloadManager.class.getSimpleName(), "add");
}
public static DownloadManager getInstance(Context context) {
if (mInstance == null) {
synchronized (DownloadManager.class) {
if (mInstance == null) {
mInstance = new DownloadManager(context.getApplicationContext());
}
}
}
return mInstance;
}
/**
* 根据url到本地sqlite数据库中查找并获取该文件的保存路径 并检查改路径下文件是否存在,不存在则删除该条无效记录
*
@ -237,7 +277,7 @@ public class DownloadManager implements DownloadStatusListener {
if (entry != null && ((int) entry.getPercent()) != 0) {
File file = new File(entry.getPath());
if (!file.exists()) {
DownloadDao.getInstance(mContext).delete(url);
mDownloadDao.delete(url);
Utils.log(DownloadManager.class.getSimpleName(), "文件不存在,删除该条无效数据库记录!");
return true;
}
@ -254,18 +294,16 @@ public class DownloadManager implements DownloadStatusListener {
public boolean isFileCompleted(String url) {
DownloadEntity entry = get(url);
if (entry != null) {
if (entry.getPercent() == 100) {
Utils.log(DownloadManager.class.getSimpleName(), "文件已经下载完成!");
return true;
}
if (entry != null && entry.getPercent() == 100) {
Utils.log(DownloadManager.class.getSimpleName(), "文件已经下载完成!");
return true;
}
return false;
}
public boolean isTaskDownloading(String url) {
if (DataChanger.getInstance().getDownloadingTasks().get(url) != null) {
if (DataChanger.INSTANCE.getDownloadingTasks().get(url) != null) {
Utils.log(DownloadManager.class.getSimpleName(), url + "正在下载!");
return true;
}
@ -286,7 +324,22 @@ public class DownloadManager implements DownloadStatusListener {
* @return null表示下载列表中不存在该任务否则返回下载任务
*/
public DownloadEntity get(String url) {
return DownloadDao.getInstance(mContext).get(url);
return mDownloadDao.get(url);
}
/**
* 根据包名获取某一个下载任务
*
* @param packageName 包名
* @return null表示下载列表中不存在该任务否则返回下载任务
*/
public DownloadEntity getByPackage(String packageName) {
for (DownloadEntity downloadEntity : mDownloadDao.getAll()) {
if (packageName.equals(downloadEntity.getPackageName())) {
return downloadEntity;
}
}
return null;
}
public void put(String url, long time) {
@ -333,43 +386,48 @@ public class DownloadManager implements DownloadStatusListener {
*/
public List<DownloadEntity> getAll() {
Utils.log(DownloadManager.class.getSimpleName(), "getAll");
return DownloadDao.getInstance(mContext).getAll();
return mDownloadDao.getAll();
}
public ArrayMap<String, DownloadEntity> getEntryMap(String name) {
return gameMap.get(name);
}
public void putStatus(String url, String status) {
void putStatus(String url, String status) {
statusMap.put(url, status);
onTaskStatusChanged(get(url));
}
public String getStatus(String url) {
return statusMap.get(url);
}
public ArrayMap<String, String> getStatusMap() {
return statusMap;
}
public void sendMessageDelayed(Message msg, long delayMillis) {
mHandler.sendMessageDelayed(msg, delayMillis);
}
/**
* 根据url恢复下载
* 根据url恢复下载 //TODO 这个可以删除的全都在add判断add的时候不需要关注任务真实状态
*
* @param url
*/
public void resume(String url) {
@Deprecated
void resume(String url) {
DownloadEntity entry = get(url);
// 暂停任务后,把文件删除,然后点继续,文件不存在,需要重新加入下载队列进行下载
if (checkDownloadEntryRecordValidate(url)) {
Toast.makeText(mContext, "文件不存在!已重新加入下载队列", Toast.LENGTH_SHORT)
.show();
Toast.makeText(mContext, "文件不存在!已重新加入下载队列", Toast.LENGTH_SHORT).show();
add(entry);
} else {
if (entry != null) {
if (isFileCompleted(url)) {
entry.setStatus(DownloadStatus.done);
DataChanger.getInstance().notifyDataChanged(entry);
DataChanger.INSTANCE.notifyDataChanged(entry);
} else if (!isTaskDownloading(url)) {
mContext.startService(getIntent(entry, DownloadStatus.resume));
}
@ -388,13 +446,13 @@ public class DownloadManager implements DownloadStatusListener {
}
public void cancel(String url, boolean isDeleteFile) {
DownloadEntity entry = DownloadDao.getInstance(mContext).get(url);
DownloadEntity entry = mDownloadDao.get(url);
if (entry != null) {
if (isDeleteFile) {
FileUtils.deleteFile(entry.getPath());
}
DownloadDao.getInstance(mContext).delete(url);
Utils.log(DownloadManager.class.getSimpleName(), "cancle==>file and record were deleted!");
mDownloadDao.delete(url);
Utils.log(DownloadManager.class.getSimpleName(), "cancel==>file and record were deleted!");
}
if (entry != null) {
entry.setStatus(DownloadStatus.cancel);
@ -406,10 +464,9 @@ public class DownloadManager implements DownloadStatusListener {
/**
* 取消并删除所有下载任务(包括下载中、等待、暂停状态的任务)
*/
public void cancleAll() {
for (Entry<String, DownloadEntity> entry : DataChanger.getInstance()
.getDownloadEntries().entrySet()) {
cancel(entry.getValue().getUrl(), true);
public void cancelAll() {
for (DownloadEntity entry : DataChanger.INSTANCE.getDownloadEntries().values()) {
cancel(entry.getUrl(), true);
}
Utils.log(DownloadManager.class.getSimpleName(), "cancel all");
}
@ -418,9 +475,8 @@ public class DownloadManager implements DownloadStatusListener {
* 开始所有下载任务
*/
public void startAll() {
for (Entry<String, DownloadEntity> entry : DataChanger.getInstance()
.getDownloadEntries().entrySet()) {
add(entry.getValue());
for (DownloadEntity entry : downloadingMap.values()) {
add(entry);
}
Utils.log(DownloadManager.class.getSimpleName(), "start all");
}
@ -429,10 +485,8 @@ public class DownloadManager implements DownloadStatusListener {
* 暂停所有正在下载的任务
*/
public void pauseAll() {
for (Entry<String, DownloadEntity> entry : DataChanger.getInstance().getDownloadEntries().entrySet()) {
if (entry.getValue().getStatus() == DownloadStatus.downloading) {
pause(entry.getValue().getUrl());
}
for (DownloadEntity entity : DataChanger.INSTANCE.getDownloadEntries().values()) {
pause(entity.getUrl());
}
Utils.log(DownloadManager.class.getSimpleName(), "pause all");
}
@ -444,40 +498,32 @@ public class DownloadManager implements DownloadStatusListener {
*/
public void pause(String url) {
checkDownloadEntryRecordValidate(url);
DownloadEntity entry = DataChanger.getInstance().getDownloadEntries()
.get(url);
DownloadEntity entry = DataChanger.INSTANCE.getDownloadEntries().get(url);
if (entry != null) {
mContext.startService(getIntent(entry, DownloadStatus.pause));
put(url, System.currentTimeMillis());
statusMap.put(url, DownloadStatus.pause.name());
}
Utils.log(DownloadManager.class.getSimpleName(), "pause");
}
/**
* 根据包名获取某一个下载任务
*
* @param packageName 包名
* @return null表示下载列表中不存在该任务否则返回下载任务
*/
public DownloadEntity getByPackage(String packageName) {
for (DownloadEntity downloadEntity : DownloadDao.getInstance(mContext).getAll()) {
if (packageName.equals(downloadEntity.getPackageName())) {
return downloadEntity;
}
public void pause(DownloadEntity entity) {
if (entity != null) {
pause(entity.getUrl());
}
return null;
}
/**
* 检查数据库中当前是下载状态,但并未在下载进程中的下载数据
*/
public void checkAll() {
final List<String> urlList = new ArrayList<>(DataChanger.getInstance().getDownloadingTasks().keySet());
final List<String> urlList = new ArrayList<>(DataChanger.INSTANCE.getDownloadingTasks().keySet());
for (DownloadEntity downloadEntity : getAll()) {
if (!urlList.contains(downloadEntity.getUrl())
&& downloadEntity.getStatus().equals(DownloadStatus.downloading)) {
downloadEntity.setStatus(DownloadStatus.pause);
DownloadDao.getInstance(mContext).newOrUpdate(downloadEntity);
DataChanger.getInstance().notifyDataChanged(downloadEntity);
mDownloadDao.newOrUpdate(downloadEntity);
DataChanger.INSTANCE.notifyDataChanged(downloadEntity);
}
}
@ -489,18 +535,18 @@ public class DownloadManager implements DownloadStatusListener {
}
public void addObserver(DataWatcher dataWatcher) {
DataChanger.getInstance().addObserver(dataWatcher);
Utils.log(DownloadManager.class.getSimpleName(), "addObserver");
DataChanger.INSTANCE.addObserver(dataWatcher);
}
public void removeObserver(DataWatcher dataWatcher) {
DataChanger.getInstance().deleteObserver(dataWatcher);
Utils.log(DownloadManager.class.getSimpleName(), "removeObserver");
}
public void removeObservers() {
DataChanger.getInstance().deleteObservers();
Utils.log(DownloadManager.class.getSimpleName(), "removeObserver");
DataChanger.INSTANCE.deleteObserver(dataWatcher);
}
//
// public void removeObservers() {
// Utils.log(DownloadManager.class.getSimpleName(), "removeObserver");
// DataChanger.INSTANCE.deleteObservers();
// }
}

View File

@ -20,7 +20,6 @@ import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.gh.download.DownloadManager;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
@ -28,6 +27,7 @@ import com.gh.common.util.ImageUtils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.DownloadManagerActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.VoteActivity;
@ -407,7 +407,7 @@ public class PlatformAdapter extends BaseRecyclerAdapter<PlatformViewHolder> {
DataUtils.onEvent(mContext, "插件化", mGameEntity.getName(), kv6);
DownloadManager.createDownload(mContext, apkEntity, mGameEntity, method, mEntrance, mLocation);
DownloadManager.getInstance(mContext).putStatus(apkEntity.getUrl(), "downloading");
// DownloadManager.getInstance(mContext).putStatus(apkEntity.getUrl(), "downloading");
mDownloadItemTvStatus.setText("0.0%");
} else {
@ -483,8 +483,7 @@ public class PlatformAdapter extends BaseRecyclerAdapter<PlatformViewHolder> {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(0xFFC7C7C7);
layers[0] = drawable;
ClipDrawable clipDrawable = new ClipDrawable(new ColorDrawable(
color), Gravity.LEFT, ClipDrawable.HORIZONTAL);
ClipDrawable clipDrawable = new ClipDrawable(new ColorDrawable(color), Gravity.LEFT, ClipDrawable.HORIZONTAL);
layers[1] = clipDrawable;
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setId(0, android.R.id.background);

View File

@ -3,7 +3,6 @@ package com.gh.gamecenter.download;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
@ -20,11 +19,7 @@ import com.gh.common.util.DialogUtils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.view.RecyclerViewExtended;
import com.lightgame.download.DataWatcher;
import com.lightgame.download.DownloadConfig;
import com.lightgame.download.DownloadEntity;
import com.gh.download.DownloadManager;
import com.lightgame.download.DownloadStatus;
import com.gh.gamecenter.DownloadManagerActivity;
import com.gh.gamecenter.MainActivity;
import com.gh.gamecenter.R;
@ -33,12 +28,16 @@ import com.gh.gamecenter.eventbus.EBMiPush;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.eventbus.EBUISwitch;
import com.lightgame.download.DataWatcher;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.DownloadStatus;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.BindView;
import butterknife.OnClick;
/**
@ -47,7 +46,7 @@ import butterknife.BindView;
* @author 黄壮华
*/
public class GameDownloadFragment extends BaseFragment implements View.OnClickListener {
@BindView(R.id.downloadmanager_rv_show)
RecyclerViewExtended mDownloadmanagerRv;
@ -69,7 +68,6 @@ public class GameDownloadFragment extends BaseFragment implements View.OnClickLi
@BindView(R.id.downloadmanager_tv_allstart)
TextView mDownloadmanagerAllstartTv;
LinearLayoutManager layoutManager;
GameDownloadFragmentAdapter adapter;
RelativeLayout.LayoutParams rparams;
@ -191,7 +189,6 @@ public class GameDownloadFragment extends BaseFragment implements View.OnClickLi
}
isScroll = false;
mDownloadmanagerAllstartTv.setOnClickListener(this);
rparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
@ -214,11 +211,11 @@ public class GameDownloadFragment extends BaseFragment implements View.OnClickLi
}, 300);
}
});
mDownloadmanagerRv.setHasFixedSize(true);
adapter = new GameDownloadFragmentAdapter(getActivity(), mNoDataSkip, url);
mDownloadmanagerRv.setAdapter(adapter);
layoutManager = new LinearLayoutManager(getActivity());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mDownloadmanagerRv.setLayoutManager(layoutManager);
((DefaultItemAnimator) mDownloadmanagerRv.getItemAnimator()).setSupportsChangeAnimations(false);
mDownloadmanagerRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@ -376,49 +373,53 @@ public class GameDownloadFragment extends BaseFragment implements View.OnClickLi
}
}
@OnClick(R.id.downloadmanager_tv_allstart)
@Override
public void onClick(View v) {
if (v.getId() == R.id.downloadmanager_tv_allstart) {
String str = ((TextView) v).getText().toString();
if ("全部开始".equals(str)) {
if (NetworkUtils.isWifiConnected(getActivity())) {
allStart();
startAll();
} else {
DialogUtils.showDownloadDialog(getActivity(), new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
allStart();
startAll();
}
});
}
} else {
for (DownloadEntity downloadEntity : adapter.getDownloadingList()) {
DownloadManager.getInstance(getActivity()).put(downloadEntity.getUrl(),
System.currentTimeMillis());
Message msg = Message.obtain();
msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
msg.obj = downloadEntity.getUrl();
DownloadManager.getInstance(getActivity()).sendMessageDelayed(msg, 1000);
}
mDownloadmanagerAllstartTv.setText("全部开始");
mDownloadmanagerAllstartTv.setTextColor(ContextCompat.getColor(getContext(), R.color.theme));
pauseAll();
}
adapter.notifyItemChanged(adapter.getBase());
}
}
private void allStart() {
public void pauseAll() {
// for (DownloadEntity downloadEntity : adapter.getDownloadingList()) {
//// DownloadManager.getInstance(getActivity()).put(downloadEntity.getUrl(),
//// System.currentTimeMillis());
//// Message msg = Message.obtain();
//// msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
//// msg.obj = downloadEntity.getUrl();
//// DownloadManager.getInstance(getActivity()).sendMessageDelayed(msg, 1000);
// DownloadManager.getInstance(getContext()).pause(downloadEntity);
// }
DownloadManager.getInstance(getContext()).pauseAll();
mDownloadmanagerAllstartTv.setText("全部开始");
mDownloadmanagerAllstartTv.setTextColor(ContextCompat.getColor(getContext(), R.color.theme));
}
private void startAll() {
for (DownloadEntity downloadEntity : adapter.getDownloadingList()) {
DownloadManager.getInstance(getActivity()).put(downloadEntity.getUrl(),
System.currentTimeMillis());
Message msg = Message.obtain();
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
msg.obj = downloadEntity.getUrl();
DownloadManager.getInstance(getActivity()).sendMessageDelayed(msg, 1000);
DownloadManager.getInstance(getContext()).add(downloadEntity);
adapter.getStatusMap().put(downloadEntity.getUrl(), "downloading");
}
// DownloadManager.getInstance(getContext()).startAll();
mDownloadmanagerAllstartTv.setText("全部暂停");
mDownloadmanagerAllstartTv.setTextColor(ContextCompat.getColor(getContext(), R.color.btn_gray));
}
}

View File

@ -5,38 +5,37 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.gh.common.util.AppDebugConfig;
import com.gh.common.util.BitmapUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DialogUtils;
import com.lightgame.download.FileUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.SpeedUtils;
import com.lightgame.download.DownloadConfig;
import com.lightgame.download.DownloadEntity;
import com.gh.download.DownloadManager;
import com.lightgame.download.DownloadStatus;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.DownloadHeadViewHolder;
import com.gh.gamecenter.adapter.viewholder.GameDownloadViewHolder;
import com.gh.gamecenter.eventbus.EBDownloadChanged;
import com.gh.gamecenter.manager.PackageManager;
import com.lightgame.adapter.BaseRecyclerAdapter;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.DownloadStatus;
import com.lightgame.download.FileUtils;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
@ -60,6 +59,10 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
private List<DownloadEntity> downloadingList;
private List<DownloadEntity> doneList;
// 1、此处的所有MAP只是对DownloadManager内部Map的引用
// 2、任何对下载器任务的操作通过DownloadManager处理由DownloadManager处理之后抛出对应的状态变化事件
// 3、监听下载任务状态变化刷新界面
// 4、对状态只读不写。
private ArrayMap<String, Integer> locationMap;
private ArrayMap<String, String> statusMap;
private ArrayMap<String, String> urlMap;
@ -74,24 +77,26 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
downloadmanager_tv_none = textView;
locationMap = new ArrayMap<>();
// statusMap = DownloadManager.getInstance(mContext).getStatusMap();
// downloadingList = new ArrayList<>(DownloadManager.getInstance(mContext).getDownloadingMap().values());
statusMap = new ArrayMap<>();
downloadingList = new ArrayList<>();
locationMap = new ArrayMap<>();
urlMap = new ArrayMap<>();
deleteList = new ArrayList<>();
downloadingList = new ArrayList<>();
doneList = new ArrayList<>();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.downloadmanager_item_head, parent, false);
View view = mLayoutInflater.inflate(R.layout.downloadmanager_item_head, parent, false);
return new DownloadHeadViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.fm_downloadmanager_item, parent, false);
View view = mLayoutInflater.inflate(R.layout.fm_downloadmanager_item, parent, false);
return new GameDownloadViewHolder(view);
}
}
@ -244,10 +249,11 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
statusMap.put(url, "downloading");
notifyItemChanged(doneList.isEmpty() ? 0 : 1 + doneList.size());
Message msg = Message.obtain();
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
msg.obj = url;
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
// Message msg = Message.obtain();
// msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
// msg.obj = url;
// DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
DownloadManager.getInstance(mContext).add(downloadEntity);
} else {
DialogUtils.showDownloadDialog(mContext, new DialogUtils.ConfirmListener() {
@Override
@ -269,10 +275,12 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
statusMap.put(url, "downloading");
notifyItemChanged(doneList.isEmpty() ? 0 : 1 + doneList.size());
Message msg = Message.obtain();
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
msg.obj = url;
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
// Message msg = Message.obtain();
// msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
// msg.obj = url;
// DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
DownloadManager.getInstance(mContext).add(downloadEntity);
}
});
}
@ -306,10 +314,12 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
viewHolder.dmDelete.setVisibility(View.VISIBLE);
statusMap.put(url, "pause");
notifyItemChanged(doneList.isEmpty() ? 0 : 1 + doneList.size());
Message msg = Message.obtain();
msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
msg.obj = url;
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
// Message msg = Message.obtain();
// msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
// msg.obj = url;
// DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
DownloadManager.getInstance(mContext).pause(downloadEntity);
break;
case "等待":
Toast.makeText(mContext, "最多只能同时启动3个下载任务", Toast.LENGTH_SHORT).show();
@ -362,18 +372,9 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
} else if (holder instanceof DownloadHeadViewHolder) {
final DownloadHeadViewHolder viewHolder = (DownloadHeadViewHolder) holder;
if (position == 0 && doneList.size() != 0) {
// CardRelativeLayout cardRelativeLayout = (CardRelativeLayout) viewHolder.itemView;
// cardRelativeLayout.setmBottom(DisplayUtils.dip2px(mContext, 8));
viewHolder.dm_item_head_tv_task.setText("已完成");
viewHolder.dm_item_head_tv_allstart.setVisibility(View.GONE);
} else {
// CardRelativeLayout cardRelativeLayout = (CardRelativeLayout) viewHolder.itemView;
// if (doneList.isEmpty()) {
// cardRelativeLayout.setmTop(0);
// } else {
// cardRelativeLayout.setmTop(DisplayUtils.dip2px(mContext, 8));
// }
// cardRelativeLayout.setmBottom(DisplayUtils.dip2px(mContext, 8));
viewHolder.dm_item_head_tv_task.setText("下载中");
viewHolder.dm_item_head_tv_allstart.setVisibility(View.VISIBLE);
@ -397,47 +398,32 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
viewHolder.dm_item_head_tv_allstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, v, ((TextView) v).getText(), ((ToggleButton) v).isChecked());
}
String str = ((TextView) v).getText().toString();
if ("全部开始".equals(str)) {
if (NetworkUtils.isWifiConnected(mContext)) {
for (DownloadEntity downloadEntity : downloadingList) {
DownloadManager.getInstance(mContext).put(downloadEntity.getUrl(),
System.currentTimeMillis());
Message msg = Message.obtain();
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
msg.obj = downloadEntity.getUrl();
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
statusMap.put(downloadEntity.getUrl(), "downloading");
}
viewHolder.dm_item_head_tv_allstart.setText("全部暂停");
viewHolder.dm_item_head_tv_allstart.setTextColor(ContextCompat.getColor(mContext, R.color.btn_gray));
startAll(viewHolder);
} else {
DialogUtils.showDownloadDialog(mContext, new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
for (DownloadEntity downloadEntity : downloadingList) {
DownloadManager.getInstance(mContext).put(downloadEntity.getUrl(),
System.currentTimeMillis());
Message msg = Message.obtain();
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
msg.obj = downloadEntity.getUrl();
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
statusMap.put(downloadEntity.getUrl(), "downloading");
}
viewHolder.dm_item_head_tv_allstart.setText("全部暂停");
viewHolder.dm_item_head_tv_allstart.setTextColor(ContextCompat.getColor(mContext, R.color.btn_gray));
startAll(viewHolder);
}
});
}
} else {
for (DownloadEntity downloadEntity : downloadingList) {
DownloadManager.getInstance(mContext).put(downloadEntity.getUrl(),
System.currentTimeMillis());
Message msg = Message.obtain();
msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
msg.obj = downloadEntity.getUrl();
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
}
// for (DownloadEntity downloadEntity : downloadingList) {
//// DownloadManager.getInstance(mContext).put(downloadEntity.getUrl(),
//// System.currentTimeMillis());
//// Message msg = Message.obtain();
//// msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
//// msg.obj = downloadEntity.getUrl();
//// DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
// DownloadManager.getInstance(mContext).pause(downloadEntity);
// }
DownloadManager.getInstance(mContext).pauseAll();
viewHolder.dm_item_head_tv_allstart.setText("全部开始");
viewHolder.dm_item_head_tv_allstart.setTextColor(ContextCompat.getColor(mContext, R.color.theme));
}
@ -447,6 +433,24 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
}
}
public void startAll(DownloadHeadViewHolder viewHolder) {
for (DownloadEntity downloadEntity : downloadingList) {
// DownloadManager.getInstance(mContext).put(downloadEntity.getUrl(),
// System.currentTimeMillis());
// Message msg = Message.obtain();
// msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
// msg.obj = downloadEntity.getUrl();
// DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
DownloadManager.getInstance(mContext).add(downloadEntity);
statusMap.put(downloadEntity.getUrl(), "downloading");
}
// DownloadManager.getInstance(mContext).startAll();
viewHolder.dm_item_head_tv_allstart.setText("全部暂停");
viewHolder.dm_item_head_tv_allstart.setTextColor(ContextCompat.getColor(mContext, R.color.btn_gray));
}
@Override
public int getItemViewType(int position) {
if (position == 0 || (doneList.size() > 0 && position == 1 + doneList.size())) {

View File

@ -210,7 +210,8 @@ public class MessageFragment extends BaseFragment_ViewPager_Checkable {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
if (positionOffset != 0) {
mLayoutParams.leftMargin = (int) (mWidth * (position + positionOffset + 0.2f));
mMessageSlideLine.setLayoutParams(mLayoutParams);
// mMessageSlideLine.setLayoutParams(mLayoutParams);
mMessageSlideLine.requestLayout();
}
}