# xnetfile
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/461fece0e06feff2e421bf94dfb8b425.png)
api 'org.xutils:xutils:3.5.0'
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6b873eb4b8998c0f3e203e1668b39ddf.jpeg)
# Download Services
private ArrayList<DownloadModel> downloadList;
private ArrayList<DownloadModel> downloadListOut;
private EventHandler handler = new EventHandler(this);
private List<OnDownLoadBackListener> loadBackListeners = new ArrayList<>();
private HashMap<String, XDownload> map;
private void startDownLoad(DownloadModel downloadModel) throws DbException {
}
private void stopDownloadItem(DownloadModel downloadModel) throws DbException {
}
private void delDownloadItem(DownloadModel downloadModel) throws DbException {
}
private void sendHandler() {
if (!handler.hasMessages(FLAG_DOWNLOAD_CHANGE_TYPE)) {
handler.sendEmptyMessage(FLAG_DOWNLOAD_CHANGE_TYPE);
}
}
#Binder
public void registeredDownLoadListener(OnDownLoadBackListener loadBackListener) {
loadBackListeners.add(loadBackListener);
}
public void unRegisteredDownLoadListener(OnDownLoadBackListener loadBackListener) {
loadBackListeners.remove(loadBackListener);
}
public void appendToCurrentList(DownloadModel info) throws DbException {
if (info != null) {
boolean existed = false;
for (int j = 0; j < downloadList.size(); j++) {
if ((downloadList.get(j).getDownloadUrl()).equals(info.getDownloadUrl()){
existed = true;
}
}
if (!existed) {
downloadList.add(info);
startDownLoad(info);
}
}
}
public void stopDownload(DownloadModel downloadModel) throws DbException {
if (downloadModel != null && !TextUtils.isEmpty(downloadModel.getDownloadUrl())) {
stopDownloadItem(downloadModel);
}
}
public void delDownLoad(DownloadModel downloadModel) throws DbException {
delDownloadItem(downloadModel);
}
#EventHandler
private static class EventHandler extends Handler {
private DownloadServices services;
public EventHandler(DownloadServices services) {
this.services = services;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == FLAG_DOWNLOAD_CHANGE_TYPE) {
for (int i = 0; i < services.loadBackListeners.size(); i++) {
services.downloadListOut.clear();
for (int i1 = 0; i1 < services.downloadList.size(); i1++) {
DownloadModel d = services.downloadList.get(i1);
if (services.map.get(d.getFileId()) == null) {
services.downloadListOut.add(d);
} else {
XDownload xDownload = services.map.get(d.getFileId());
if (xDownload != null) {
DownloadModel downloadModel = xDownload.getDownloadModel();
if (downloadModel.getProgress() == 100) {
services.map.remove(downloadModel.getFileId());
try {
DownloadHelper.getInstance().delDownloadFileId(downloadModel.getFileId());
} catch (DbException e) {
e.printStackTrace();
}
services.downloadList.remove(i1);
i1--;
} else {
services.downloadListOut.add(xDownload.getDownloadModel());
if (xDownload.getDownloadModel().getDownloadStatus() == XDownloadStatusType.ERROR) {
xDownload.downLoadCancel();
services.map.remove(downloadModel.getFileId());
}
}
}
}
}
services.loadBackListeners.get(i).downloadSize(services.downloadListOut);
}
if (services.map.size() == 0) {
services.handler.removeMessages(FLAG_DOWNLOAD_CHANGE_TYPE);
}else{
services.handler.sendEmptyMessageDelayed(FLAG_DOWNLOAD_CHANGE_TYPE, 200);
}
}
}
}
# XDownloaded
private void initX() {
RequestParams params = new RequestParams();
params.setUri(downloadModel.getDownloadUrl());
params.setAutoResume(true);
params.setAutoRename(false);
params.setExecutor(new PriorityExecutor(1, true));
params.setSaveFilePath(downloadModel.getFileSavePath() + downloadModel.getFileName());
cancelable = x.http().get(params, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
downloadModel.setProgress(100);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
downloadModel.setDownloadStatus(XDownloadStatusType.ERROR);
}
@Override
public void onCancelled(CancelledException cex) {
downloadModel.setDownloadStatus(XDownloadStatusType.CANCELLED);
}
@Override
public void onFinished() {
downloadModel.setDownloadStatus(XDownloadStatusType.ERROR);
}
@Override
public void onWaiting() {
downloadModel.setDownloadStatus(XDownloadStatusType.WAITING);
}
@Override
public void onStarted() {
downloadModel.setDownloadStatus(XDownloadStatusType.STARTED);
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
downloadModel.setProgress(current * 100 / total);
}
});
}
XNetFile 源码地址