修复任务组不能自动调度问题,任务组进度超出问题,任务组无法回调onResume的问题;普通任务添加修正处理

pull/330/head
laoyuyu 7 years ago
parent 426c148370
commit e68cca5da3
  1. 9
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. 4
      Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  3. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java
  4. 32
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  5. 7
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  7. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java
  9. 5
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  10. 4
      app/src/main/AndroidManifest.xml
  11. 2
      app/src/main/assets/aria_config.xml
  12. 9
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java

@ -16,6 +16,7 @@
package com.arialyy.aria.core.common;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
@ -67,10 +68,8 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
private int mCompleteThreadNum;
private SparseArray<AbsThreadTask> mTask = new SparseArray<>();
private Timer mTimer;
@Deprecated
private File mConfigFile;
@Deprecated private File mConfigFile;
/**
* 进度刷新间隔
*/
@ -465,6 +464,10 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
threadId[rl] = i;
rl++;
}
if (mConstance.CURRENT_LOCATION != 0 && mConstance.CURRENT_LOCATION != mEntity.getCurrentProgress()) {
ALog.d(TAG, "进度修正");
mEntity.setCurrentProgress(mConstance.CURRENT_LOCATION);
}
saveRecord();
startThreadTask(threadId);
}

@ -34,13 +34,13 @@ import java.lang.ref.WeakReference;
class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>, TASK extends AbsTask<TASK_ENTITY>>
implements IDownloadListener {
private static final String TAG = "BaseDListener";
protected WeakReference<Handler> outHandler;
WeakReference<Handler> outHandler;
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
private long mLastLen = 0; //上一次发送长度
private boolean isFirst = true;
protected ENTITY mEntity;
protected TASK_ENTITY mTaskEntity;
protected TASK mTask;
private TASK mTask;
private boolean isConvertSpeed = false;
boolean isWait = false;
private long mLastSaveTime;

@ -19,6 +19,7 @@ import android.os.Handler;
import com.arialyy.aria.core.download.downloader.IDownloadGroupListener;
import com.arialyy.aria.core.inf.GroupSendParams;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.ALog;
/**
* Created by Aria.Lao on 2017/7/20.
@ -85,10 +86,17 @@ class DownloadGroupListener
}
private void saveCurrentLocation() {
if (mEntity.getSubEntities() == null || mEntity.getSubEntities().isEmpty()) {
ALog.w(TAG, "保存进度失败,子任务为null");
return;
}
long location = 0;
for (DownloadEntity e : mEntity.getSubEntities()) {
location += e.getCurrentProgress();
}
if (location > mEntity.getFileSize()) {
location = mEntity.getFileSize();
}
mEntity.setCurrentProgress(location);
mEntity.update();
}

@ -43,11 +43,11 @@ public abstract class AbsGroupUtil implements IUtil {
/**
* FTP文件夹
*/
protected int FTP_DIR = 0xa1;
int FTP_DIR = 0xa1;
/**
* HTTP 任务组
*/
protected int HTTP_GROUP = 0xa2;
int HTTP_GROUP = 0xa2;
/**
* 任务组所有任务总长度
@ -56,7 +56,7 @@ public abstract class AbsGroupUtil implements IUtil {
long mCurrentLocation = 0;
private ExecutorService mExePool;
protected IDownloadGroupListener mListener;
protected DownloadGroupTaskEntity mGTEntity;
DownloadGroupTaskEntity mGTEntity;
private boolean isRunning = false;
private Timer mTimer;
/**
@ -262,7 +262,7 @@ public abstract class AbsGroupUtil implements IUtil {
}
/**
* 预处理操作由于属性的不同http任务组在构造函数中就可以完成了
* 预处理操作
* 而FTP文件夹的需要获取完成所有子任务信息才算预处理完成
*/
protected void onPre() {
@ -298,14 +298,13 @@ public abstract class AbsGroupUtil implements IUtil {
@Override public void resume() {
start();
mListener.onResume(mCurrentLocation);
}
@Override public void setMaxSpeed(double maxSpeed) {
}
private void clearState(){
private void clearState() {
mDownloaderMap.clear();
mFailMap.clear();
}
@ -325,7 +324,11 @@ public abstract class AbsGroupUtil implements IUtil {
void startRunningFlow() {
closeTimer(true);
mListener.onPostPre(mTotalLen);
if (mCurrentLocation > 0) {
mListener.onResume(mCurrentLocation);
} else {
mListener.onStart(mCurrentLocation);
}
startTimer();
}
@ -336,7 +339,16 @@ public abstract class AbsGroupUtil implements IUtil {
if (!isRunning) {
closeTimer(false);
} else if (mCurrentLocation >= 0) {
mListener.onProgress(mCurrentLocation);
long t = 0;
for (DownloadTaskEntity te : mGTEntity.getSubTaskEntities()) {
if (te.getState() == IEntity.STATE_COMPLETE) {
t += te.getEntity().getFileSize();
} else {
t += te.getEntity().getCurrentProgress();
}
}
mCurrentLocation = t;
mListener.onProgress(t);
}
}
}, 0, mUpdateInterval);
@ -345,8 +357,8 @@ public abstract class AbsGroupUtil implements IUtil {
/**
* 创建子任务下载器默认创建完成自动启动
*/
Downloader createChildDownload(DownloadTaskEntity taskEntity) {
return createChildDownload(taskEntity, true);
void createChildDownload(DownloadTaskEntity taskEntity) {
createChildDownload(taskEntity, true);
}
/**
@ -408,7 +420,7 @@ public abstract class AbsGroupUtil implements IUtil {
@Override public void onProgress(long currentLocation) {
long speed = currentLocation - lastLen;
mCurrentLocation += speed;
//mCurrentLocation += speed;
subEntity.setCurrentProgress(currentLocation);
handleSpeed(speed);
mListener.onSubRunning(subEntity);

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download.downloader;
import android.text.TextUtils;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.util.SSLContextUtil;
import java.io.IOException;
@ -44,7 +45,7 @@ class ConnectionHelp {
* @throws IOException
*/
static InputStream convertInputStream(HttpURLConnection connection) throws IOException {
String encoding = connection.getContentEncoding();
String encoding = connection.getHeaderField("Content-Encoding");
if (TextUtils.isEmpty(encoding)) {
return connection.getInputStream();
}
@ -88,7 +89,9 @@ class ConnectionHelp {
*/
static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn)
throws ProtocolException {
conn.setRequestMethod(entity.getRequestEnum().name);
if (entity.getRequestEnum() == RequestEnum.POST){
conn.setDoInput(true);
}
Set<String> keys = null;
if (entity.getHeaders() != null && entity.getHeaders().size() > 0) {
keys = entity.getHeaders().keySet();

@ -35,7 +35,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
private final String TAG = "DownloadGroupUtil";
private ExecutorService mInfoPool;
private int mInitCompleteNum, mInitFailNum;
private boolean isStop = false;
private boolean isStop = false, isStart = false;
/**
* 文件信息回调组
@ -45,7 +45,6 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
public DownloadGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) {
super(listener, taskEntity);
mInfoPool = Executors.newCachedThreadPool();
onPre();
}
@Override int getTaskType() {
@ -69,7 +68,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
}
@Override protected void onStart() {
super.onStart();
onPre();
isStop = false;
if (mCompleteNum == mGroupSize) {
mListener.onComplete();
@ -154,9 +153,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
*/
private void checkStartFlow() {
synchronized (DownloadGroupUtil.class) {
if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) {
if (!isStart && (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize)) {
startRunningFlow();
updateFileSize();
isStart = true;
}
}
}

@ -85,7 +85,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
conn = ConnectionHelp.setConnectParam(mConfig.TASK_ENTITY, conn);
conn.setConnectTimeout(mConnectTimeOut);
conn.setReadTimeout(mReadTimeOut); //设置读取流的等待时间,必须设置该参数
conn.connect();
is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
if (isOpenDynamicFile) {
readDynamicFile(is);

@ -48,7 +48,7 @@ abstract class AbsDelegate {
* @param type {@link DelegateWrapper}
*/
static void print(int type, String sql) {
if (!ALog.DEBUG) {
if (ALog.DEBUG) {
return;
}
String str = "";

@ -435,8 +435,7 @@ public class CommonUtil {
* @param removeFile {@code true} 不仅删除任务数据库记录还会删除已经完成的文件
* {@code false}如果任务已经完成只删除任务数据库记录
*/
public static void delTaskRecord(TaskRecord record, boolean removeFile,
AbsNormalEntity dEntity) {
public static void delTaskRecord(TaskRecord record, boolean removeFile, AbsNormalEntity dEntity) {
if (dEntity == null) return;
File file;
if (dEntity instanceof DownloadEntity) {
@ -890,7 +889,9 @@ public class CommonUtil {
}
TaskRecord record = DbHelper.getTaskRecord(oldPath);
if (record == null) {
if (new File(oldPath).exists()) {
ALog.w(TAG, "修改任务记录失败,文件【" + oldPath + "】对应的任务记录不存在");
}
return;
}
record.filePath = newPath;

@ -15,9 +15,9 @@
android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".test.TestGroupActivity"-->
<!--android:name=".test.TestActivity"-->
<!--android:name=".MainActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<activity
android:name=".test.AnyRunActivity"
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

@ -25,7 +25,7 @@
<threadNum value="3"/>
<!--设置下载队列最大任务数, 默认为2-->
<maxTaskNum value="2"/>
<maxTaskNum value="1"/>
<!--设置下载失败,重试次数,默认为10-->
<reTryNum value="2"/>

@ -49,8 +49,9 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
//"https://static.donguo.me//video/ip/course/pfys_1.mp4";
//"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864";
"https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013";
//"https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013";
//"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1";
"https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe";
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@ -238,8 +239,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//.addHeader("Accept-Encoding", "gzip, deflate")
//.addHeader("DNT", "1")
//.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0")
.useServerFileName(true)
.setRequestMode(RequestEnum.GET)
//.useServerFileName(true)
//.setRequestMode(RequestEnum.GET)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg3.apk")
//.resetState()
.start();

Loading…
Cancel
Save