Files
Nebula-domain-server-rk/nebula-sdk/src/main/java/com/android/download/DownLoadManeger.java
2025-09-28 17:54:32 +08:00

289 lines
9.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.android.download;
import android.content.Context;
import android.text.TextUtils;
import com.android.database.DaoManager;
import com.android.database.lib.DownLoadTaskBean;
import com.android.util.GsonUtil;
import com.blankj.utilcode.util.LogUtils;
import java.io.File;
import java.util.List;
public class DownLoadManeger {
private static DownLoadManeger instance;
private boolean isAuto = false;
private DownloadListener mDownloadListener;
/**
* 网络异常码
*/
protected static final int ERROR_CODE_NETWORK = -1;
/**
* 读写异常码
*/
protected static final int ERROR_CODE_WRITE = -2;
/**
* 存储空间不够异常码
*/
protected static final int ERROR_CODE_STORAGE_ENOUGH = -3;
/**
* URL格式异常
*/
protected static final int ERROR_CODE_MALFORMEDURL = -4;
public void registerDownloadListener(DownloadListener downloadListener) {
this.mDownloadListener = downloadListener;
}
public void unRegisterDownloadListener(DownloadListener downloadListener) {
this.mDownloadListener = null;
}
private DownLoadManeger(Context context) {
DaoManager.initeDao(context);
}
public static DownLoadManeger init(Context context) {
if (instance == null)
instance = new DownLoadManeger(context);
return instance;
}
public static DownLoadManeger getInstance() {
return instance;
}
/**
* 添加新的下载任务
*/
public void addDownloadTask(DownloadBuilder builder) {
if (TextUtils.isEmpty(builder.url)) {
return;
}
DownLoadTaskBean downLoadTaskBean = null;
if (!isExistTask(builder.url)) {
downLoadTaskBean = createTaskBean(builder);
LogUtils.e("postServerAppUpdate--->新下载任务");
} else {
File file = new File(builder.path, builder.name + ".tmp");
if (file.exists()) {
if (file.length() == builder.total) {
LogUtils.e("postServerAppUpdate--->该下载任务已存在并下载完成");
return;
} else {
downLoadTaskBean = createTaskBean(builder);
LogUtils.e("postServerAppUpdate--->该下载任务已存在但未完成,当前进度:" + file.length() + "--->" + builder.total);
}
} else {
downLoadTaskBean = createTaskBean(builder);
LogUtils.e("postServerAppUpdate--->该下载任务已存在并下载完成");
}
}
//添加下载任务队列
if (!TaskQueue.getInstance().isExistTask(downLoadTaskBean)) {
TaskQueue.getInstance().add(new DownLoadTaskThread(downLoadTaskBean, observer));//添加到下载队列中
} else {
LogUtils.e("postServerAppUpdate--->该下载任务已存在:" + GsonUtil.GsonString(downLoadTaskBean));
}
}
/**
* 下载没有完成的任务管理
*/
public void restDownloadTask() {
List<DownLoadTaskBean> list = DaoManager.getInstance().queryList(DownLoadTaskBean.class); //未完成下载的任务
LogUtils.e("postServerAppUpdate--->restDownloadTask DownloadTask:" + GsonUtil.GsonString(list));
if (list.size() > 0) {
LogUtils.e("postServerAppUpdate--->start download");
for (DownLoadTaskBean downLoadTaskBean : list) {
//添加下载任务队列
if (!TaskQueue.getInstance().isExistTask(downLoadTaskBean)) {
TaskQueue.getInstance().add(new DownLoadTaskThread(downLoadTaskBean, observer));//添加到下载队列中
}
}
} else {
LogUtils.e("postServerAppUpdate--->no download task");
}
}
/**
* 清除任务队列
*/
public void clear() {
TaskQueue.getInstance().clear();
}
public boolean isExistTask(String url) {//判断是否存在下载任务
List<DownLoadTaskBean> downLoadTaskBeans = DaoManager.getInstance().queryByKeyList(DownLoadTaskBean.class, "url", url);
if (downLoadTaskBeans != null && downLoadTaskBeans.size() == 1) {
return true;
}
return false;
}
private DownLoadTaskBean createTaskBean(DownloadBuilder builder) {//创建下载任务bean和创建下载任务
DownLoadTaskBean bean = new DownLoadTaskBean();
bean.setFileName(builder.name);
bean.setPath(builder.path);
bean.setUrl(builder.url);
bean.setTaskId(System.currentTimeMillis());
bean.setListener(builder.listener);
bean.setTotal(builder.total);
bean.setMinType(builder.minType);
bean.setTaskType(builder.taskType);
DownLoadTaskThread task = new DownLoadTaskThread(bean, observer);
DaoManager.getInstance().insert(DownLoadTaskBean.class, bean);//保存到数据库
return bean;
}
//自动下载
public void setAutoDownLoad(boolean isAuto) {
this.isAuto = isAuto;
}
public void pauseAll() {//全部暂停
TaskQueue.getInstance().pause();
}
public static class DownloadBuilder {//构造器
protected String url;//下载地址
protected String path;//下载路径
protected String name;//文件名字
protected long total;//现在总进度
/**
* 文件类型
*/
protected String minType;
/**
* 任务类型
*/
protected int taskType;
protected DownLoadTaskBean.DownLoad2Listener listener;//下载监听
public DownloadBuilder setMinType(String minType) {
this.minType = minType;
return this;
}
public DownloadBuilder url(String url) {
this.url = url;
return this;
}
public DownloadBuilder filePath(String path) {
this.path = path;
return this;
}
public DownloadBuilder listener(DownLoadTaskBean.DownLoad2Listener listener) {
this.listener = listener;
return this;
}
public DownloadBuilder fileName(String name) {
this.name = name;
return this;
}
public DownloadBuilder taskType(int taskType) {
this.taskType = taskType;
return this;
}
public DownloadBuilder fileTotal(long fileSize) {
this.total = fileSize;
return this;
}
}
public interface DownloadListener {
public void onStart(DownLoadTaskBean bean, long taskId);
public void onError(DownLoadTaskBean bean, long taskId, String erroMsg);
public void onProgress(DownLoadTaskBean bean, long taskId, long progress, long total);
public void onFinish(DownLoadTaskBean bean, long taskId);
}
//下载线程的观察者
private DownLoadTaskThread.DownloadTaskObserver observer = new DownLoadTaskThread.DownloadTaskObserver() {
@Override
protected void onStart(DownLoadTaskBean bean, long taskId) {//下载开始,回调总进度给监听,方便设置进度大小
LogUtils.e("postServerAppUpdate--->taskId=" + taskId + ">>>>>" + bean.getTotal());
bean.setStatus(0);
AppExecutors.getAppExecutors().mainThread().execute(() -> {//切换到主线程
if (bean.getListener() != null) {
bean.getListener().onStart(bean.getTotal());
}
});
}
@Override
protected void onProgress(DownLoadTaskBean bean, long taskId, long progress, long total) {//进度调用
synchronized (this) {//这里必须加同步代码块,因为这共用进度等变量
if (mDownloadListener != null) {
mDownloadListener.onProgress(bean, taskId, progress, total);
}
}
}
@Override
protected void onFinish(DownLoadTaskBean bean, long taskId, long lastProgress) {
synchronized (this) {
TaskQueue.getInstance().remove(bean);//单个任务完成删除
// DaoManager.getInstance().delete(DownLoadTaskBean.class, bean);
if (mDownloadListener != null) {
mDownloadListener.onFinish(bean, taskId);
}
}
}
@Override
protected void onPause(DownLoadTaskBean bean, long taskId, long currentProgress) {
synchronized (this) {//暂停这里必须加上同步,不然会出现实际进度跟显示进度不一致问题,其实也就是缓存没存进去,第二个线程又来了
// DaoManager.getInstance().update(DownLoadTaskBean.class, bean);//更新数据库退出app再进来就不会从头下载
}
}
@Override
protected void onError(DownLoadTaskBean bean, long taskId, String erroMsg, int errorCode) {//错误提示
TaskQueue.getInstance().remove(bean);
switch (errorCode) {
case ERROR_CODE_NETWORK:
break;
case ERROR_CODE_WRITE:
case ERROR_CODE_MALFORMEDURL:
case ERROR_CODE_STORAGE_ENOUGH:
DaoManager.getInstance().delete(DownLoadTaskBean.class, bean);//删除数据库缓存
File file = new File(bean.getPath(), bean.getFileName() + "-tmp"); //下载异常时删除临时文件
if (file.exists()) {
file.delete();
}
break;
}
LogUtils.e("postServerAppUpdate--->errmsg:" + erroMsg);
if (mDownloadListener != null) {
mDownloadListener.onError(bean, taskId, erroMsg);
}
}
};
}