修复组件出现的几个问题

This commit is contained in:
laoyuyu
2019-09-30 18:19:03 +08:00
parent 2a92321785
commit 75ff995d81
25 changed files with 122 additions and 132 deletions

View File

@@ -78,8 +78,11 @@ public class RecordHandler implements IRecordHandler {
if (!file.exists()) {
ALog.w(TAG, String.format("文件【%s】不存在重新分配线程区间", mTaskRecord.filePath));
DbEntity.deleteData(ThreadRecord.class, "taskKey=?", mTaskRecord.filePath);
mTaskRecord.threadRecords.clear();
mTaskRecord.threadNum = mAdapter.initTaskThreadNum();
initRecord(false);
} else if (mTaskRecord.threadRecords == null || mTaskRecord.threadRecords.isEmpty()) {
mTaskRecord.threadNum = mAdapter.initTaskThreadNum();
initRecord(false);
}
mAdapter.handlerTaskRecord(mTaskRecord);

View File

@@ -23,6 +23,8 @@ import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IUtil;
import com.arialyy.aria.core.listener.IDGroupListener;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.util.Map;
@@ -49,12 +51,12 @@ public abstract class AbsGroupUtil implements IUtil, Runnable {
private DGTaskWrapper mGTWrapper;
private GroupRunState mState;
protected AbsGroupUtil(IDGroupListener listener, DGTaskWrapper groupWrapper) {
mListener = listener;
mGTWrapper = groupWrapper;
protected AbsGroupUtil(AbsTaskWrapper groupWrapper, IEventListener listener) {
mListener = (IDGroupListener) listener;
mGTWrapper = (DGTaskWrapper) groupWrapper;
mUpdateInterval = Configuration.getInstance().downloadCfg.getUpdateInterval();
mState = new GroupRunState(groupWrapper.getKey(), mListener,
groupWrapper.getSubTaskWrapper().size(), mSubQueue);
mGTWrapper.getSubTaskWrapper().size(), mSubQueue);
mScheduler = new Handler(Looper.getMainLooper(), SimpleSchedulers.newInstance(mState));
initState();
}

View File

@@ -23,6 +23,7 @@ import com.arialyy.aria.core.group.GroupSendParams;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IRecordHandler;
import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.task.AbsTask;
import com.arialyy.aria.core.task.DownloadGroupTask;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.ALog;
@@ -34,14 +35,14 @@ import com.arialyy.aria.util.RecordUtil;
* Created by Aria.Lao on 2017/7/20. 任务组下载事件
*/
public class DownloadGroupListener
extends BaseListener<DownloadGroupEntity, DGTaskWrapper, DownloadGroupTask>
extends BaseListener<DownloadGroupEntity, DGTaskWrapper, AbsTask<DGTaskWrapper>>
implements IDGroupListener {
private GroupSendParams<DownloadGroupTask, DownloadEntity> mSeedEntity;
public DownloadGroupListener(DownloadGroupTask task, Handler outHandler) {
public DownloadGroupListener(AbsTask<DGTaskWrapper> task, Handler outHandler) {
super(task, outHandler);
mSeedEntity = new GroupSendParams<>();
mSeedEntity.groupTask = task;
mSeedEntity.groupTask = (DownloadGroupTask) task;
}
@Override

View File

@@ -37,7 +37,7 @@ public abstract class AbsThreadTaskAdapter implements IThreadTaskAdapter {
/**
* 当前线程的下去区间的进度
*/
private long mRangeProgress = 0;
private long mRangeProgress;
private ThreadRecord mThreadRecord;
private IThreadTaskObserver mObserver;
private AbsTaskWrapper mWrapper;
@@ -75,9 +75,7 @@ public abstract class AbsThreadTaskAdapter implements IThreadTaskAdapter {
return mThreadRecord;
}
@Deprecated
protected AbsTaskWrapper getTaskWrapper() {
// TODO: 2019-09-18 需要修改方法名称为getWrapper
return mWrapper;
}
@@ -95,9 +93,7 @@ public abstract class AbsThreadTaskAdapter implements IThreadTaskAdapter {
/**
* 获取线程配置信息
*/
@Deprecated
protected SubThreadConfig getConfig() {
// TODO: 2019-09-18 需要修改方法名称为getThreadConfig
protected SubThreadConfig getThreadConfig() {
return mThreadConfig;
}
@@ -108,7 +104,7 @@ public abstract class AbsThreadTaskAdapter implements IThreadTaskAdapter {
@Override public void setMaxSpeed(int speed) {
if (mSpeedBandUtil == null) {
mSpeedBandUtil =
new BandwidthLimiter(getTaskConfig().getMaxSpeed(), getConfig().startThreadNum);
new BandwidthLimiter(getTaskConfig().getMaxSpeed(), getThreadConfig().startThreadNum);
}
mSpeedBandUtil.setMaxRate(speed);
}

View File

@@ -25,8 +25,8 @@ import com.arialyy.aria.core.AriaConfig;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.listener.ISchedulers;
import com.arialyy.aria.core.inf.IThreadState;
import com.arialyy.aria.core.listener.ISchedulers;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper;
@@ -62,16 +62,16 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
private Handler mStateHandler;
private SubThreadConfig mConfig;
/**
* 当前线程相对于总长度的位置
* 当前线程的下去区间的进度
*/
protected long mChildCurrentLocation = 0;
private long mRangeProgress;
private IThreadTaskAdapter mAdapter;
protected ThreadRecord mRecord;
private ThreadRecord mRecord;
private Thread mConfigThread = new Thread(new Runnable() {
@Override public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
final long currentTemp = mChildCurrentLocation;
final long currentTemp = mRangeProgress;
writeConfig(false, currentTemp);
}
});
@@ -86,7 +86,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
mConfigThreadPool = Executors.newCachedThreadPool();
isNotNetRetry = AriaConfig.getInstance().getAConfig().isNotNetRetry();
mChildCurrentLocation = mRecord.startLocation;
mRangeProgress = mRecord.startLocation;
}
/**
@@ -94,6 +94,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
*/
public void setAdapter(IThreadTaskAdapter adapter) {
mAdapter = adapter;
mAdapter.setThreadStateObserver(this);
}
/**
@@ -166,7 +167,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
public void breakTask() {
taskBreak = true;
if (mTaskWrapper.isSupportBP()) {
final long currentTemp = mChildCurrentLocation;
final long currentTemp = mRangeProgress;
updateState(IThreadState.STATE_STOP, null);
ALog.d(TAG, String.format("任务【%s】thread__%s__中断【停止位置%s】", getFileName(),
mRecord.threadId, currentTemp));
@@ -239,7 +240,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
ALog.i(TAG, String.format("任务【%s】已停止", getFileName()));
} else {
if (mTaskWrapper.isSupportBP()) {
final long stopLocation = mChildCurrentLocation;
final long stopLocation = mRangeProgress;
ALog.d(TAG,
String.format("任务【%s】thread__%s__停止【当前线程停止位置%s】", getFileName(),
mRecord.threadId, stopLocation));
@@ -295,20 +296,20 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
*
* @param needRetry 是否需要重试,一般是网络错误才需要重试
*/
@Override public void updateFailState(@Nullable BaseException e, boolean needRetry) {
@Override public synchronized void updateFailState(@Nullable BaseException e, boolean needRetry) {
fail(mRangeProgress, e, needRetry);
}
@Override
public synchronized void updateProgress(long len) {
mChildCurrentLocation += len;
mRangeProgress += len;
Thread loopThread = mStateHandler.getLooper().getThread();
if (!loopThread.isAlive() || loopThread.isInterrupted()) {
return;
}
mStateHandler.obtainMessage(IThreadState.STATE_RUNNING, len).sendToTarget();
if (System.currentTimeMillis() - mLastSaveTime > 5000
&& mChildCurrentLocation < mRecord.endLocation) {
&& mRangeProgress < mRecord.endLocation) {
mLastSaveTime = System.currentTimeMillis();
if (!mConfigThreadPool.isShutdown()) {
mConfigThreadPool.execute(mConfigThread);
@@ -327,16 +328,6 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
String.format("任务【%s】thread__%s__取消", getFileName(), mRecord.threadId));
}
/**
* 线程任务失败
*
* @param subCurrentLocation 当前线程下载进度
* @param ex 异常信息
*/
protected void fail(long subCurrentLocation, BaseException ex) {
fail(subCurrentLocation, ex, true);
}
/**
* 任务失败
*
@@ -451,7 +442,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
/**
* 发送失败信息
*/
protected void sendFailMsg(@Nullable BaseException e) {
private void sendFailMsg(@Nullable BaseException e) {
if (e != null) {
Bundle b = new Bundle();
b.putSerializable(IThreadState.KEY_ERROR_INFO, e);
@@ -467,7 +458,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
* @param isComplete 当前线程是否完成 {@code true}完成
* @param record 当前下载进度
*/
protected void writeConfig(boolean isComplete, final long record) {
private void writeConfig(boolean isComplete, final long record) {
if (mRecord != null) {
mRecord.isComplete = isComplete;
if (mConfig.isBlock) {
@@ -487,6 +478,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
isDestroy = false;
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
TrafficStats.setThreadStatsTag(UUID.randomUUID().toString().hashCode());
mAdapter.call(this);
return this;
}
}

View File

@@ -20,10 +20,8 @@ import com.arialyy.aria.core.TaskOptionParams;
import com.arialyy.aria.core.inf.IEventHandler;
import com.arialyy.aria.core.inf.ITaskOption;
import com.arialyy.aria.core.inf.IUtil;
import com.arialyy.aria.core.listener.BaseListener;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.task.AbsTask;
import com.arialyy.aria.core.task.ITask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper;
import java.lang.ref.SoftReference;
@@ -42,8 +40,8 @@ public class ComponentUtil {
public static final int COMPONENT_TYPE_FTP = 2;
public static final int COMPONENT_TYPE_M3U8 = 3;
private static volatile ComponentUtil INSTANCE = null;
private String TAG = CommonUtil.getClassName(getClass());
private static volatile ComponentUtil INSTANCE = null;
private ComponentUtil() {
@@ -99,7 +97,7 @@ public class ComponentUtil {
*
* @return 返回下载工具创建失败返回null
*/
public <T extends IUtil> T buildUtil(ITaskWrapper wrapper, IEventListener listener) {
public <T extends IUtil> T buildUtil(AbsTaskWrapper wrapper, IEventListener listener) {
int requestType = wrapper.getRequestType();
String className = null;
switch (requestType) {
@@ -122,10 +120,10 @@ public class ComponentUtil {
className = "com.arialyy.aria.http.upload.HttpULoaderUtil";
break;
case ITaskWrapper.D_FTP_DIR:
className = "com.arialyy.aria.http.download.DGroupLoaderUtil";
className = "com.arialyy.aria.ftp.download.FtpDirDLoaderUtil";
break;
case ITaskWrapper.DG_HTTP:
className = "com.arialyy.aria.ftp.download.FtpDirDLoaderUtil";
className = "com.arialyy.aria.http.download.DGroupLoaderUtil";
break;
}
if (className == null) {
@@ -158,7 +156,7 @@ public class ComponentUtil {
* @param wrapperType 任务类型{@link ITaskWrapper}
* @return 返回事件监听如果创建失败返回null
*/
public <T extends IEventListener> T buildListener(int wrapperType, ITask task,
public <T extends IEventListener> T buildListener(int wrapperType, AbsTask task,
Handler outHandler) {
String className = null, errorStr = "请添加FTP插件";
switch (wrapperType) {
@@ -217,6 +215,7 @@ public class ComponentUtil {
try {
taskOption = clazz.newInstance();
for (Field field : fields) {
field.setAccessible(true);
Class type = field.getType();
String key = field.getName();
if (type != SoftReference.class) {