任务组bug修复,修复子任务下载完成后重新下载的问题

This commit is contained in:
AriaLyy
2017-07-18 16:18:15 +08:00
parent b8a86890a7
commit 489f44c0f9
27 changed files with 226 additions and 249 deletions

View File

@ -23,7 +23,6 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
// compile project(':AriaCompiler')
compile project(':AriaAnnotations')
compile project(':AriaAnnotations') //gradle 打包时打开这个
}
apply from: 'bintray-release.gradle'
//apply from: 'jcenter.gradle'

View File

@ -28,7 +28,7 @@ import java.lang.ref.WeakReference;
/**
* 下载监听类
*/
final class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
extends DownloadListener {
private WeakReference<Handler> outHandler;
private long lastLen = 0; //上一次发送长度
@ -81,7 +81,6 @@ final class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
}
@Override public void onStop(long stopLocation) {
//saveData(IEntity.STATE_STOP, stopLocation);
saveData(isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP, stopLocation);
handleSpeed(0);
sendInState2Target(ISchedulers.STOP);

View File

@ -106,10 +106,11 @@ public class DownloadGroupUtil implements IDownloadUtil {
mTotalSize += entity.getFileSize();
mCompleteNum++;
mInitNum++;
mCurrentLocation += entity.getFileSize();
} else {
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity));
mCurrentLocation += entity.getCurrentProgress();
}
mCurrentLocation += entity.getCurrentProgress();
}
}
@ -284,6 +285,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
private DownloadTaskEntity createChildDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity = mTasksMap.get(entity.getDownloadUrl());
if (taskEntity != null) {
taskEntity.entity = entity;
return taskEntity;
}
taskEntity = new DownloadTaskEntity();
@ -335,16 +337,21 @@ public class DownloadGroupUtil implements IDownloadUtil {
}
@Override public void onProgress(long currentLocation) {
mCurrentLocation += (currentLocation - lastLen);
long speed = currentLocation - lastLen;
mCurrentLocation += speed;
lastLen = currentLocation;
entity.setCurrentProgress(currentLocation);
handleSpeed(speed);
}
@Override public void onStop(long stopLocation) {
saveData(IEntity.STATE_STOP, stopLocation);
handleSpeed(0);
}
@Override public void onCancel() {
saveData(IEntity.STATE_CANCEL, -1);
handleSpeed(0);
}
@Override public void onComplete() {
@ -354,11 +361,13 @@ public class DownloadGroupUtil implements IDownloadUtil {
closeTimer();
mListener.onComplete();
}
handleSpeed(0);
}
@Override public void onFail() {
entity.setFailNum(entity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, -1);
handleSpeed(0);
reTry();
}
@ -374,6 +383,11 @@ public class DownloadGroupUtil implements IDownloadUtil {
}
}
private void handleSpeed(long speed) {
entity.setSpeed(speed);
entity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s");
}
private void saveData(int state, long location) {
entity.setState(state);
entity.setComplete(state == IEntity.STATE_COMPLETE);

View File

@ -90,28 +90,18 @@ class Downloader implements Runnable, IDownloadUtil {
mListener.onPostPre(mEntity.getFileSize());
mConstance.cleanState();
mConstance.isDownloading = true;
try {
if (!mTaskEntity.isSupportBP) {
mThreadNum = 1;
mConstance.THREAD_NUM = mThreadNum;
handleNoSupportBreakpointDownload();
} else {
mThreadNum = isNewTask ? (mEntity.getFileSize() <= SUB_LEN ? 1
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum())
: mRealThreadNum;
mConstance.THREAD_NUM = mThreadNum;
mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum);
handleBreakpoint();
}
startTimer();
} catch (IOException e) {
failDownload("下载失败【downloadUrl:"
+ mEntity.getDownloadUrl()
+ "\n【filePath:"
+ mEntity.getDownloadPath()
+ "\n"
+ CommonUtil.getPrintException(e));
if (!mTaskEntity.isSupportBP) {
mThreadNum = 1;
mConstance.THREAD_NUM = mThreadNum;
handleNoSupportBreakpointDownload();
} else {
mThreadNum = isNewTask ? (mEntity.getFileSize() <= SUB_LEN ? 1
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum()) : mRealThreadNum;
mConstance.THREAD_NUM = mThreadNum;
mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum);
handleBreakpoint();
}
startTimer();
}
/**
@ -170,6 +160,7 @@ class Downloader implements Runnable, IDownloadUtil {
@Override public void stopDownload() {
closeTimer();
if (mConstance.isComplete()) return;
mConstance.isStop = true;
mConstance.isDownloading = false;
if (mFixedThreadPool != null) {
@ -326,7 +317,7 @@ class Downloader implements Runnable, IDownloadUtil {
/**
* 处理断点
*/
private void handleBreakpoint() throws IOException {
private void handleBreakpoint() {
long fileLength = mEntity.getFileSize();
Properties pro = CommonUtil.loadConfig(mConfigFile);
int blockSize = (int) (fileLength / mThreadNum);
@ -336,14 +327,8 @@ class Downloader implements Runnable, IDownloadUtil {
}
int rl = 0;
if (isNewTask) {
CommonUtil.createFile(mTempFile.getPath());
BufferedRandomAccessFile file =
new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192);
//设置文件长度
file.setLength(fileLength);
file.close();
createNewFile(fileLength);
}
for (int i = 0; i < mThreadNum; i++) {
long startL = i * blockSize, endL = (i + 1) * blockSize;
Object state = pro.getProperty(mTempFile.getName() + "_state_" + i);
@ -375,6 +360,34 @@ class Downloader implements Runnable, IDownloadUtil {
startSingleTask(recordL);
}
/**
* 创建新的下载文件
*/
private void createNewFile(long fileLength) {
CommonUtil.createFile(mTempFile.getPath());
BufferedRandomAccessFile file = null;
try {
file = new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192);
//设置文件长度
file.setLength(fileLength);
} catch (IOException e) {
failDownload("下载失败【downloadUrl:"
+ mEntity.getDownloadUrl()
+ "\n【filePath:"
+ mEntity.getDownloadPath()
+ "\n"
+ CommonUtil.getPrintException(e));
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 处理不支持断点的下载
*/

View File

@ -16,70 +16,27 @@
package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.inf.IEventListener;
/**
* 下载监听
*/
public interface IDownloadListener {
interface IDownloadListener extends IEventListener {
/**
* 支持断点回调
*
* @param support true,支持false 不支持
*/
public void supportBreakpoint(boolean support);
/**
* 取消下载
*/
public void onCancel();
/**
* 下载失败
*/
public void onFail();
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态
*/
public void onPre();
/**
* 预处理完成,准备下载---开始下载之间
*/
public void onPostPre(long fileSize);
/**
* 下载监听
*/
public void onProgress(long currentLocation);
void supportBreakpoint(boolean support);
/**
* 单一线程的结束位置
*/
public void onChildComplete(long finishLocation);
/**
* 开始
*/
public void onStart(long startLocation);
void onChildComplete(long finishLocation);
/**
* 子程恢复下载的位置
*/
public void onChildResume(long resumeLocation);
/**
* 恢复位置
*/
public void onResume(long resumeLocation);
/**
* 停止
*/
public void onStop(long stopLocation);
/**
* 下载完成
*/
public void onComplete();
void onChildResume(long resumeLocation);
}

View File

@ -31,7 +31,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
/**
* 单位转换后的速度
*/
@Ignore private String convertSpeed = "0b/s";
@Ignore private String convertSpeed = "";
/**
* 下载失败计数每次开始都重置为0
*/

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.inf;
/**
* Created by Aria.Lao on 2017/7/18.
* 基础事件
*/
public interface IEventListener {
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态
*/
void onPre();
/**
* 预处理完成,准备下载---开始下载之间
*/
void onPostPre(long fileSize);
/**
* 开始
*/
void onStart(long startLocation);
/**
* 恢复位置
*/
void onResume(long resumeLocation);
/**
* 下载监听
*/
void onProgress(long currentLocation);
/**
* 停止
*/
void onStop(long stopLocation);
/**
* 下载完成
*/
void onComplete();
/**
* 取消下载
*/
void onCancel();
/**
* 下载失败
*/
void onFail();
}

View File

@ -15,60 +15,12 @@
*/
package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.inf.IEventListener;
/**
* Created by lyy on 2017/2/9.
* 上传监听
*/
public interface IUploadListener {
interface IUploadListener extends IEventListener {
/**
* 预处理
*/
public void onPre();
/**
* 预处理完成
*/
public void onPostPre(long fileSize);
/**
* 开始上传
*/
public void onStart();
/**
* 恢复上传
*
* @param resumeLocation 上次上传停止位置
*/
public void onResume(long resumeLocation);
/**
* 停止上传
*
* @param stopLocation 上传停止位置
*/
public void onStop(long stopLocation);
/**
* 上传进度
*
* @param currentLocation 当前进度
*/
public void onProgress(long currentLocation);
/**
* 取消上传
*/
public void onCancel();
/**
* 上传成功
*/
public void onComplete();
/**
* 上传失败
*/
public void onFail();
}

View File

@ -18,8 +18,7 @@ package com.arialyy.aria.core.upload;
/**
* Created by lyy on 2017/2/23.
*/
public class UploadListener implements IUploadListener {
class UploadListener implements IUploadListener {
@Override public void onPre() {
}
@ -28,7 +27,7 @@ public class UploadListener implements IUploadListener {
}
@Override public void onStart() {
@Override public void onStart(long startLocation) {
}

View File

@ -28,10 +28,10 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
UploadTarget(String filePath, String targetName) {
this.mTargetName = targetName;
mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "groupName=?", filePath);
mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "key=?", filePath);
if (mTaskEntity == null) {
mTaskEntity = new UploadTaskEntity();
mTaskEntity.entity = new UploadEntity();
mTaskEntity.entity = getUploadEntity(filePath);
}
if (mTaskEntity.entity == null) {
mTaskEntity.entity = getUploadEntity(filePath);
@ -43,13 +43,14 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
if (entity == null) {
entity = new UploadEntity();
String regex = "[/|\\\\|//]";
Pattern p = Pattern.compile(regex);
String[] strs = p.split(filePath);
String fileName = strs[strs.length - 1];
entity.setFileName(fileName);
entity.setFilePath(filePath);
entity.insert();
}
String regex = "[/|\\\\|//]";
Pattern p = Pattern.compile(regex);
String[] strs = p.split(filePath);
String fileName = strs[strs.length - 1];
entity.setFileName(fileName);
entity.setFilePath(filePath);
return entity;
}

View File

@ -109,7 +109,7 @@ final class UploadUtil implements Runnable {
for (String key : keys) {
addFormField(writer, key, mTaskEntity.formFields.get(key));
}
mListener.onStart();
mListener.onStart(0);
uploadFile(writer, mTaskEntity.attachment, uploadFile);
Log.d(TAG, finish(writer) + "");
} catch (IOException e) {

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

View File

@ -1,51 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
/>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/img"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/checkbox"
android:layout_toRightOf="@+id/img"
android:ellipsize="end"
android:maxLines="1"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/title"
android:layout_below="@+id/title"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/checkbox"
/>
</RelativeLayout>

View File

@ -1,8 +0,0 @@
<resources>
<string name="app_name">Aria</string>
<string name="error_entity_null">下载实体不能为空</string>
<string name="error_download_url_null">下载链接不能为空</string>
<string name="error_download_path_null">存储地址不能为空</string>
<string name="error_file_name_null">文件名不能为空</string>
</resources>