fix: 修复下载任务暂停时,下载实体会被脏数据覆盖的问题
This commit is contained in:
@ -22,6 +22,7 @@ import com.lightgame.download.DownloadStatus;
|
||||
import com.lightgame.download.ForegroundNotificationManager;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class NDownloadService extends Service {
|
||||
@ -138,7 +139,6 @@ public class NDownloadService extends Service {
|
||||
}
|
||||
|
||||
synchronized void subscribeDownload(DownloadEntity entry) {
|
||||
NDataChanger.INSTANCE.getDownloadEntries().put(entry.getUrl(), entry);
|
||||
DownloadDao.getInstance(this).newOrUpdate(entry);
|
||||
NDataChanger.INSTANCE.notifyDataChanged(entry);
|
||||
DownloadManager.getInstance().onTaskAdded(entry);
|
||||
@ -148,7 +148,6 @@ public class NDownloadService extends Service {
|
||||
if (NDataChanger.INSTANCE.getDownloadingTaskUrlSet().size() >= DownloadConfig.MAX_DOWNLOADING_SIZE) {
|
||||
// 1.改任务队列的状态
|
||||
entry.setStatus(DownloadStatus.waiting);
|
||||
NDataChanger.INSTANCE.getDownloadEntries().put(entry.getUrl(), entry);
|
||||
// 2.改数据库状态
|
||||
DownloadDao.getInstance(this).newOrUpdate(entry);
|
||||
// 3.通知更新
|
||||
@ -183,7 +182,6 @@ public class NDownloadService extends Service {
|
||||
NDataChanger.INSTANCE.getDownloadingTaskUrlSet().remove(downloadEntity.getUrl());
|
||||
NDataChanger.INSTANCE.notifyDataChanged(downloadEntity);
|
||||
}
|
||||
NDataChanger.INSTANCE.getDownloadEntries().remove(downloadEntity.getUrl());
|
||||
NDataChanger.INSTANCE.notifyDataChanged(downloadEntity);
|
||||
DownloadManager.getInstance().onTaskCancelled(downloadEntity);
|
||||
}
|
||||
@ -196,7 +194,6 @@ public class NDownloadService extends Service {
|
||||
entry.getMeta().put(DownloadEntity.DOWNLOAD_STARTUP_STATUS_KEY, entry.getStatus().getStatus());
|
||||
// 1.改任务队列的状态
|
||||
entry.setStatus(DownloadStatus.downloading);
|
||||
NDataChanger.INSTANCE.getDownloadEntries().put(entry.getUrl(), entry);
|
||||
NDataChanger.INSTANCE.getDownloadingTaskUrlSet().add(entry.getUrl());
|
||||
|
||||
// 2.改数据库状态
|
||||
@ -213,27 +210,22 @@ public class NDownloadService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 DataChanger 里移除掉数据,并找到下一个处于 waiting 状态的任务继续
|
||||
* @param downloadEntity 需要移除的数据
|
||||
* @param removeCompletely 是否完全移除,false 时只从下载中 hashMap 移除
|
||||
* 找到下一个处于 waiting 状态的任务继续
|
||||
* @param url 需要移除的 url 对应的数据
|
||||
*/
|
||||
public synchronized void removeFromDataChangerAndResumeWaitingTask(DownloadEntity downloadEntity, boolean removeCompletely) {
|
||||
NDataChanger.INSTANCE.getDownloadingTaskUrlSet().remove(downloadEntity.getUrl());
|
||||
if (removeCompletely) {
|
||||
NDataChanger.INSTANCE.getDownloadEntries().remove(downloadEntity.getUrl());
|
||||
}
|
||||
synchronized (NDataChanger.INSTANCE.getDownloadEntries()) {
|
||||
for (DownloadEntity entry : NDataChanger.INSTANCE.getDownloadEntries().values()) {
|
||||
if (DownloadStatus.waiting.equals(entry.getStatus())) {
|
||||
// 刷新状态(从等待中到开始下载,状态应该变为继续下载)
|
||||
entry.setStatus(DownloadStatus.resume);
|
||||
startDownload(entry);
|
||||
public synchronized void removeFromDataChangerAndResumeWaitingTask(String url) {
|
||||
NDataChanger.INSTANCE.getDownloadingTaskUrlSet().remove(url);
|
||||
ArrayList<DownloadEntity> downloadEntitySnapshotList = DownloadManager.getInstance().getAllDownloadEntitySnapshots();
|
||||
for (DownloadEntity entry : downloadEntitySnapshotList) {
|
||||
if (DownloadStatus.waiting.equals(entry.getStatus())) {
|
||||
// 刷新状态(从等待中到开始下载,状态应该变为继续下载)
|
||||
entry.setStatus(DownloadStatus.resume);
|
||||
startDownload(entry);
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Utils.log(NDownloadService.class.getSimpleName(), entry.getPackageName() + "==>" + entry.getStatus().getStatus());
|
||||
}
|
||||
return;
|
||||
if (BuildConfig.DEBUG) {
|
||||
Utils.log(NDownloadService.class.getSimpleName(), entry.getPackageName() + "==>" + entry.getStatus().getStatus());
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user