1、对下载器任务的状态变化修改
2、对下载器内部的handler的消息屏蔽,pause的修改,剩余download roll未改动。
This commit is contained in:
@ -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();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user