提交项目
This commit is contained in:
253
app/src/main/java/com/gh/download/DownloadManager.java
Normal file
253
app/src/main/java/com/gh/download/DownloadManager.java
Normal file
@ -0,0 +1,253 @@
|
||||
package com.gh.download;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.gh.common.constant.Constants;
|
||||
import com.gh.common.util.FileUtils;
|
||||
import com.gh.common.util.Trace;
|
||||
|
||||
public class DownloadManager {
|
||||
private static DownloadManager mInstance;
|
||||
private Context context;
|
||||
|
||||
private DownloadManager(Context context) {
|
||||
this.context = context;
|
||||
context.startService(new Intent(context, DownloadService.class));
|
||||
}
|
||||
|
||||
public static DownloadManager getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DownloadManager(context.getApplicationContext());
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
private Intent getIntent(DownloadEntry entry, DownloadStatus status) {
|
||||
Intent service = new Intent(context, DownloadService.class);
|
||||
service.putExtra(Constants.KEY_DOWNLOAD_ENTRY, entry);
|
||||
service.putExtra(Constants.KEY_DOWNLOAD_ACTION, status.name());
|
||||
return service;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据url到本地sqlite数据库中查找并获取该文件的保存路径, 并检查改路径下文件是否存在,不存在则删除该条无效记录
|
||||
*
|
||||
* @param url
|
||||
* 下载任务的标识url
|
||||
*/
|
||||
private boolean checkDownloadEntryRecordValidate(String url) {
|
||||
DownloadEntry entry = get(url);
|
||||
if (entry != null) {
|
||||
File file = new File(entry.getPath());
|
||||
if (!file.exists()) {
|
||||
DownloadDao.getInstance(context).delete(url);
|
||||
Trace.getInstance().debug(
|
||||
DownloadManager.class.getSimpleName(),
|
||||
"文件不存在,删除该条无效数据库记录!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查任务是否已经下载完成
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public boolean isFileCompleted(String url) {
|
||||
DownloadEntry entry = get(url);
|
||||
|
||||
if (entry != null) {
|
||||
if (entry.getPercent() == 100) {
|
||||
Trace.getInstance().debug(
|
||||
DownloadManager.class.getSimpleName(), "文件已经下载完成!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isTaskDownloading(String url) {
|
||||
if (DataChanger.getInstance().getDownloadingTasks().get(url) != null) {
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
url + "正在下载!");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个下载任务
|
||||
*
|
||||
* @param entry
|
||||
*/
|
||||
public void add(DownloadEntry entry) {
|
||||
if (entry != null) {
|
||||
String url = entry.getUrl();
|
||||
checkDownloadEntryRecordValidate(url);
|
||||
if (!isFileCompleted(url) && !isTaskDownloading(url)) {
|
||||
context.startService(getIntent(entry, DownloadStatus.add));
|
||||
}
|
||||
}
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(), "add");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据任务url暂停下载
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
public void pause(String url) {
|
||||
checkDownloadEntryRecordValidate(url);
|
||||
DownloadEntry entry = DataChanger.getInstance().getDownloadEntries()
|
||||
.get(url);
|
||||
if (entry != null) {
|
||||
context.startService(getIntent(entry, DownloadStatus.pause));
|
||||
}
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"pause");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据url恢复下载
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
public void resume(String url) {
|
||||
DownloadEntry entry = get(url);
|
||||
|
||||
// 暂停任务后,把文件删除,然后点继续,文件不存在,需要重新加入下载队列进行下载
|
||||
if (checkDownloadEntryRecordValidate(url)) {
|
||||
Toast.makeText(context, "文件不存在!已重新加入下载队列", Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
add(entry);
|
||||
} else {
|
||||
if (entry != null) {
|
||||
if (!isFileCompleted(url) && !isTaskDownloading(url)) {
|
||||
context.startService(getIntent(entry, DownloadStatus.resume));
|
||||
}
|
||||
}
|
||||
}
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"resume");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据url取消下载,并删除已下载的文件
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public void cancel(String url) {
|
||||
cancel(url, true);
|
||||
}
|
||||
|
||||
public void cancel(String url, boolean isDeleteFile) {
|
||||
|
||||
DownloadEntry temp = DownloadDao.getInstance(context).get(url);
|
||||
if (temp != null) {
|
||||
if (isDeleteFile) {
|
||||
FileUtils.deleteFile(temp.getPath());
|
||||
}
|
||||
DownloadDao.getInstance(context).delete(url);
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"cancle==>file and record were deleted!");
|
||||
}
|
||||
|
||||
DownloadEntry entry = DataChanger.getInstance().getDownloadEntries()
|
||||
.get(url);
|
||||
if (entry != null) {
|
||||
context.startService(getIntent(entry, DownloadStatus.cancel));
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"cancel");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消并删除所有下载任务(包括下载中、等待、暂停状态的任务)
|
||||
*/
|
||||
public void cancleAll() {
|
||||
for (Entry<String, DownloadEntry> entry : DataChanger.getInstance()
|
||||
.getDownloadEntries().entrySet()) {
|
||||
cancel(entry.getValue().getUrl(), true);
|
||||
}
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"cancel all");
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始所有下载任务
|
||||
*/
|
||||
public void startAll() {
|
||||
for (Entry<String, DownloadEntry> entry : DataChanger.getInstance()
|
||||
.getDownloadEntries().entrySet()) {
|
||||
add(entry.getValue());
|
||||
}
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"start all");
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停所有正在下载的任务
|
||||
*/
|
||||
public void pauseAll() {
|
||||
for (Entry<String, DownloadEntry> entry : DataChanger.getInstance()
|
||||
.getDownloadEntries().entrySet()) {
|
||||
if (entry.getValue().getStatus() == DownloadStatus.downloading) {
|
||||
pause(entry.getValue().getUrl());
|
||||
}
|
||||
}
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"pause all");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据url获取某一个下载任务
|
||||
*
|
||||
* @param url
|
||||
* @return null表示下载列表中不存在该任务,否则返回下载任务
|
||||
*/
|
||||
public DownloadEntry get(String url) {
|
||||
return DownloadDao.getInstance(context).get(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有下载列表中的任务
|
||||
*
|
||||
* @return null表示没有下载任务
|
||||
*/
|
||||
public List<DownloadEntry> getAll() {
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"getAll");
|
||||
return DownloadDao.getInstance(context).getAll();
|
||||
}
|
||||
|
||||
public void addObserver(DataWatcher dataWatcher) {
|
||||
DataChanger.getInstance().addObserver(dataWatcher);
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"addObserver");
|
||||
}
|
||||
|
||||
public void removeObserver(DataWatcher dataWatcher) {
|
||||
DataChanger.getInstance().deleteObserver(dataWatcher);
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"removeObserver");
|
||||
}
|
||||
|
||||
public void removeObservers() {
|
||||
DataChanger.getInstance().deleteObservers();
|
||||
Trace.getInstance().debug(DownloadManager.class.getSimpleName(),
|
||||
"removeObservers");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user