diff --git a/DEV_LOG.md b/DEV_LOG.md index 86ef3d80..307d8a05 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -4,6 +4,7 @@ - 修复40x错误,会继续重试并且无法重试成功的问题 https://github.com/AriaLyy/Aria/issues/619 - 修复wait模式下,resume(true)无效问题 - 修复now模式下的一些问题 https://github.com/AriaLyy/Aria/issues/620 + - 修复组任务,其中一个子任务在获取文件长度失败后,重新恢复组合任务,组合任务状态变为完成的问题 https://github.com/AriaLyy/Aria/issues/628 + v_3.8.6 (2020/2/17) - fix bug https://github.com/AriaLyy/Aria/issues/608 - fix bug https://github.com/AriaLyy/Aria/issues/579#issuecomment-586665035 diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java index 7710805d..82b2d3b0 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java @@ -30,6 +30,7 @@ import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; /** * 组合任务文件信息,用于获取长度未知时,组合任务的长度 @@ -41,8 +42,8 @@ public final class HttpDGInfoTask implements IInfoTask { private final Object LOCK = new Object(); private ExecutorService mPool = null; private boolean getLenComplete = false; - private int count; - private int failCount; + private AtomicInteger count = new AtomicInteger(); + private AtomicInteger failCount = new AtomicInteger(); private DownloadGroupListener listener; /** @@ -50,18 +51,18 @@ public final class HttpDGInfoTask implements IInfoTask { */ private Callback subCallback = new Callback() { @Override public void onSucceed(String url, CompleteInfo info) { - count++; - checkGetSizeComplete(count, failCount); + count.getAndIncrement(); + checkGetSizeComplete(count.get(), failCount.get()); ALog.d(TAG, "获取子任务信息完成"); } @Override public void onFail(AbsEntity entity, AriaException e, boolean needRetry) { ALog.e(TAG, String.format("获取文件信息失败,url:%s", ((DownloadEntity) entity).getUrl())); - count++; - failCount++; + count.getAndIncrement(); + failCount.getAndIncrement(); listener.onSubFail((DownloadEntity) entity, new AriaHTTPException(TAG, String.format("子任务获取文件长度失败,url:%s", ((DownloadEntity) entity).getUrl()))); - checkGetSizeComplete(count, failCount); + checkGetSizeComplete(count.get(), failCount.get()); } }; @@ -79,7 +80,7 @@ public final class HttpDGInfoTask implements IInfoTask { return; } // 处理组合任务大小未知的情况 - if (wrapper.isUnknownSize() && wrapper.getEntity().getFileSize() < 1) { + if (wrapper.isUnknownSize()) { mPool = Executors.newCachedThreadPool(); getGroupSize(); try { @@ -89,7 +90,7 @@ public final class HttpDGInfoTask implements IInfoTask { } catch (InterruptedException e) { e.printStackTrace(); } - if (!mPool.isShutdown()){ + if (!mPool.isShutdown()) { mPool.shutdown(); } } else { @@ -107,6 +108,10 @@ public final class HttpDGInfoTask implements IInfoTask { new Thread(new Runnable() { @Override public void run() { for (DTaskWrapper dTaskWrapper : wrapper.getSubTaskWrapper()) { + if (dTaskWrapper.getEntity().getFileSize() > 0) { + count.getAndIncrement(); + continue; + } cloneHeader(dTaskWrapper); HttpDFileInfoTask infoTask = new HttpDFileInfoTask(dTaskWrapper); infoTask.setCallback(subCallback); @@ -158,7 +163,6 @@ public final class HttpDGInfoTask implements IInfoTask { subOption.setFileNameAdapter(groupOption.getFileNameAdapter()); subOption.setUseServerFileName(groupOption.isUseServerFileName()); - subOption.setFileNameAdapter(groupOption.getFileNameAdapter()); subOption.setRequestEnum(groupOption.getRequestEnum()); subOption.setHeaders(groupOption.getHeaders()); diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java index de2517c9..159c40ca 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.listener.DownloadGroupListener; import com.arialyy.aria.core.loader.IInfoTask; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.exception.AriaException; +import java.io.File; /** * http 组合任务加载器 @@ -53,6 +54,12 @@ final class HttpDGLoader extends AbsGroupLoader { } onPostStart(); for (DTaskWrapper wrapper : getWrapper().getSubTaskWrapper()) { + File subFile = new File(wrapper.getEntity().getFilePath()); + if (wrapper.getEntity().getFileSize() > 0 + && subFile.exists() + && subFile.length() == wrapper.getEntity().getFileSize()) { + continue; + } DownloadEntity dEntity = wrapper.getEntity(); startSubLoader(createSubLoader(wrapper, dEntity.getFileSize() < 0)); } diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java index 3ed9275a..84b5524c 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java @@ -51,6 +51,7 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -75,10 +76,11 @@ final class M3U8VodLoader extends BaseM3U8Loader { private SparseArray mAfterPeer = new SparseArray<>(); private PeerIndexEvent mCurrentEvent; private String mCacheDir; - private int aIndex = 0, bIndex = 0; - private int mCurrentFlagSize; + private AtomicInteger afterPeerIndex = new AtomicInteger(); + private AtomicInteger beforePeerIndex = new AtomicInteger(); + private AtomicInteger mCompleteNum = new AtomicInteger(); + private AtomicInteger mCurrentFlagSize = new AtomicInteger(); private boolean isJump = false, isDestroy = false; - private int mCompleteNum = 0; private ExecutorService mJumpThreadPool; private Thread jumpThread = null; private M3U8TaskOption mM3U8Option; @@ -102,20 +104,20 @@ final class M3U8VodLoader extends BaseM3U8Loader { } int getCompleteNum() { - return mCompleteNum; + return mCompleteNum.get(); } - void setCompleteNum(int mCompleteNum) { - this.mCompleteNum = mCompleteNum; + void setCompleteNum(int completeNum) { + mCompleteNum.set(completeNum); } int getCurrentFlagSize() { - mCurrentFlagSize = mFlagQueue.size(); - return mCurrentFlagSize; + mCurrentFlagSize.set(mFlagQueue.size()); + return mCurrentFlagSize.get(); } void setCurrentFlagSize(int currentFlagSize) { - mCurrentFlagSize = currentFlagSize; + mCurrentFlagSize.set(currentFlagSize); } boolean isJump() { @@ -178,7 +180,7 @@ final class M3U8VodLoader extends BaseM3U8Loader { try { LOCK.lock(); while (mFlagQueue.size() < EXEC_MAX_NUM && !isBreak()) { - if (mCompleteNum == mRecord.threadRecords.size()) { + if (mCompleteNum.get() == mRecord.threadRecords.size()) { break; } @@ -214,16 +216,18 @@ final class M3U8VodLoader extends BaseM3U8Loader { ThreadRecord tr = null; try { // 优先下载peer指针之后的数据 - if (bIndex == 0 && aIndex < mAfterPeer.size()) { + if (beforePeerIndex.get() == 0 && afterPeerIndex.get() < mAfterPeer.size()) { //ALog.d(TAG, String.format("afterArray size:%s, index:%s", mAfterPeer.size(), aIndex)); - tr = mAfterPeer.valueAt(aIndex); - aIndex++; + tr = mAfterPeer.valueAt(afterPeerIndex.get()); + afterPeerIndex.getAndIncrement(); } // 如果指针之后的数组没有切片了,则重新初始化指针位置,并获取指针之前的数组获取切片进行下载 - if (mBeforePeer.size() > 0 && (tr == null || bIndex != 0) && bIndex < mBeforePeer.size()) { - tr = mBeforePeer.valueAt(bIndex); - bIndex++; + if (mBeforePeer.size() > 0 + && (tr == null || beforePeerIndex.get() != 0) + && beforePeerIndex.get() < mBeforePeer.size()) { + tr = mBeforePeer.valueAt(beforePeerIndex.get()); + beforePeerIndex.getAndIncrement(); } } catch (Exception e) { e.printStackTrace(); @@ -256,19 +260,19 @@ final class M3U8VodLoader extends BaseM3U8Loader { return; } // 设置需要下载的切片 - mCompleteNum = 0; + mCompleteNum.set(0); for (ThreadRecord tr : mRecord.threadRecords) { if (!tr.isComplete) { mAfterPeer.put(tr.threadId, tr); } else { - mCompleteNum++; + mCompleteNum.getAndIncrement(); } } getStateManager().updateStateCount(); - if (mCompleteNum <= 0) { + if (mCompleteNum.get() <= 0) { getListener().onStart(0); } else { - int percent = mCompleteNum * 100 / mRecord.threadRecords.size(); + int percent = mCompleteNum.get() * 100 / mRecord.threadRecords.size(); getListener().onResume(percent); } } @@ -329,7 +333,7 @@ final class M3U8VodLoader extends BaseM3U8Loader { isJump = true; notifyWaitLock(false); - mCurrentFlagSize = mFlagQueue.size(); + mCurrentFlagSize.set(mFlagQueue.size()); // 停止所有正在执行的线程任务 try { TempFlag flag; @@ -403,12 +407,12 @@ final class M3U8VodLoader extends BaseM3U8Loader { mBeforePeer.clear(); mAfterPeer.clear(); mFlagQueue.clear(); - aIndex = 0; - bIndex = 0; - mCompleteNum = 0; + afterPeerIndex.set(0); + beforePeerIndex.set(0); + mCompleteNum.set(0); for (ThreadRecord tr : mRecord.threadRecords) { if (tr.isComplete) { - mCompleteNum++; + mCompleteNum.getAndIncrement(); continue; } if (tr.threadId < mCurrentEvent.peerIndex) { diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/VodStateManager.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/VodStateManager.java index ec65abdb..f5dc952c 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/VodStateManager.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/VodStateManager.java @@ -29,8 +29,8 @@ import com.arialyy.aria.core.loader.ILoaderVisitor; import com.arialyy.aria.core.manager.ThreadTaskManager; import com.arialyy.aria.core.processor.ITsMergeHandler; import com.arialyy.aria.core.task.ThreadTask; -import com.arialyy.aria.exception.AriaM3U8Exception; import com.arialyy.aria.exception.AriaException; +import com.arialyy.aria.exception.AriaM3U8Exception; import com.arialyy.aria.m3u8.BaseM3U8Loader; import com.arialyy.aria.m3u8.M3U8Listener; import com.arialyy.aria.m3u8.M3U8TaskOption; @@ -40,6 +40,7 @@ import com.arialyy.aria.util.FileUtil; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; /** * m3u8 点播下载状态管理器 @@ -49,9 +50,9 @@ public final class VodStateManager implements IThreadStateManager { private M3U8Listener listener; private int startThreadNum; // 启动的线程总数 - private int cancelNum = 0; // 已经取消的线程的数 - private int stopNum = 0; // 已经停止的线程数 - private int failNum = 0; // 失败的线程数 + private AtomicInteger cancelNum = new AtomicInteger(0); // 已经取消的线程的数 + private AtomicInteger stopNum = new AtomicInteger(0); // 已经停止的线程数 + private AtomicInteger failNum = new AtomicInteger(0); // 失败的线程数 private long progress; private TaskRecord taskRecord; // 任务记录 private Looper looper; @@ -74,11 +75,11 @@ public final class VodStateManager implements IThreadStateManager { int peerIndex = msg.getData().getInt(ISchedulers.DATA_M3U8_PEER_INDEX); switch (msg.what) { case STATE_STOP: - stopNum++; + stopNum.getAndIncrement(); removeSignThread((ThreadTask) msg.obj); // 处理跳转位置后,恢复任务 if (loader.isJump() - && (stopNum == loader.getCurrentFlagSize() || loader.getCurrentFlagSize() == 0) + && (stopNum.get() == loader.getCurrentFlagSize() || loader.getCurrentFlagSize() == 0) && !loader.isBreak()) { loader.resumeTask(); return true; @@ -90,7 +91,7 @@ public final class VodStateManager implements IThreadStateManager { } break; case STATE_CANCEL: - cancelNum++; + cancelNum.getAndIncrement(); removeSignThread((ThreadTask) msg.obj); if (loader.isBreak()) { @@ -99,7 +100,7 @@ public final class VodStateManager implements IThreadStateManager { } break; case STATE_FAIL: - failNum++; + failNum.getAndIncrement(); for (ThreadRecord tr : taskRecord.threadRecords) { if (tr.threadId == peerIndex) { loader.getBeforePeer().put(peerIndex, tr); @@ -175,9 +176,9 @@ public final class VodStateManager implements IThreadStateManager { }; void updateStateCount() { - cancelNum = 0; - stopNum = 0; - failNum = 0; + cancelNum.set(0); + stopNum.set(0); + failNum.set(0); } @Override public void setLooper(TaskRecord taskRecord, Looper looper) { @@ -233,12 +234,12 @@ public final class VodStateManager implements IThreadStateManager { @Override public boolean isFail() { printInfo("isFail"); - return failNum != 0 && failNum == loader.getCurrentFlagSize() && !loader.isJump(); + return failNum.get() != 0 && failNum.get() == loader.getCurrentFlagSize() && !loader.isJump(); } @Override public boolean isComplete() { if (m3U8Option.isIgnoreFailureTs()) { - return loader.getCompleteNum() + failNum >= taskRecord.threadRecords.size() + return loader.getCompleteNum() + failNum.get() >= taskRecord.threadRecords.size() && !loader.isJump(); } else { return loader.getCompleteNum() == taskRecord.threadRecords.size() && !loader.isJump(); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java index 59adb0d8..bc4e81ff 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java @@ -137,7 +137,7 @@ public class RecordHelper { } else if (tr.startLocation != realLocation) { // 处理记录小于分块文件长度的情况 ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); tr.startLocation = realLocation; - }else { + } else { ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); tr.startLocation = realLocation; tr.isComplete = false; @@ -163,8 +163,10 @@ public class RecordHelper { // 目标文件 File targetFile = new File(mTaskRecord.filePath); // 处理组合任务其中一个子任务完成的情况 - if (tr.isComplete && targetFile.exists() && targetFile.length() == mWrapper.getEntity() - .getFileSize()) { + if (tr.isComplete + && targetFile.exists() + && targetFile.length() != 0 + && targetFile.length() == mWrapper.getEntity().getFileSize()) { tr.isComplete = true; } else { ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java index 2a0232bd..430a3210 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java @@ -153,7 +153,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { if (!checkSubTask(url, "开始")) { return; } - if (!mState.isRunning) { + if (!mState.isRunning.get()) { startTimer(); } AbsSubDLoadUtil d = getDownloader(url, false); @@ -212,7 +212,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { } @Override public boolean isRunning() { - return mState != null && mState.isRunning; + return mState != null && mState.isRunning.get(); } @Override public void cancel() { @@ -275,11 +275,11 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { } private synchronized void startTimer() { - mState.isRunning = true; + mState.isRunning.set(true); mTimer = new ScheduledThreadPoolExecutor(1); mTimer.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - if (!mState.isRunning) { + if (!mState.isRunning.get()) { closeTimer(); } else if (mCurrentLocation >= 0) { long t = 0; @@ -321,7 +321,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { } } - protected void fail(AriaException e, boolean needRetry){ + protected void fail(AriaException e, boolean needRetry) { closeTimer(); getListener().onFail(needRetry, e); } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java index 7dcbcc13..112d6184 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java @@ -19,6 +19,8 @@ import com.arialyy.aria.core.listener.IDGroupListener; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; /** * 组合任务执行中的状态信息 @@ -32,17 +34,18 @@ public final class GroupRunState { /** * 已经完成的任务数 */ - private int mCompleteNum; + private AtomicInteger mCompleteNum = new AtomicInteger(); /** * 失败的任务数 */ - private int mFailNum; + private AtomicInteger mFailNum = new AtomicInteger(); /** * 停止的任务数 */ - private int mStopNum; + private AtomicInteger mStopNum = new AtomicInteger(); + ; /** * 当前进度 @@ -62,7 +65,7 @@ public final class GroupRunState { /** * 是否在执行 */ - boolean isRunning = false; + AtomicBoolean isRunning = new AtomicBoolean(false); /** * 子任务失败、停止记录,用于当子任务失败重新被用户点击开始时,更新{@link #mStopNum}或{@link #mFailNum} @@ -78,7 +81,7 @@ public final class GroupRunState { mGroupHash = groupHash; } - public void setSubSize(int subSize){ + public void setSubSize(int subSize) { mSubSize = subSize; } @@ -88,11 +91,11 @@ public final class GroupRunState { * @return {@code true}组合任务正在执行 */ public boolean isRunning() { - return isRunning; + return isRunning.get(); } public void setRunning(boolean running) { - isRunning = running; + isRunning.set(running); } String getGroupHash() { @@ -110,21 +113,21 @@ public final class GroupRunState { * 获取失败的数量 */ public int getFailNum() { - return mFailNum; + return mFailNum.get(); } /** * 获取停止的数量 */ public int getStopNum() { - return mStopNum; + return mStopNum.get(); } /** * 获取完成的数量 */ public int getCompleteNum() { - return mCompleteNum; + return mCompleteNum.get(); } /** @@ -138,7 +141,7 @@ public final class GroupRunState { * 更新完成的数量,mCompleteNum + 1 */ public void updateCompleteNum() { - mCompleteNum++; + mCompleteNum.getAndIncrement(); } /** @@ -156,10 +159,10 @@ public final class GroupRunState { public void updateCount(String key) { if (mFailTemp.contains(key)) { mFailTemp.remove(key); - mFailNum--; + mFailNum.getAndDecrement(); } else if (mStopTemp.contains(key)) { mStopTemp.remove(key); - mStopNum--; + mStopNum.getAndDecrement(); } } @@ -170,7 +173,7 @@ public final class GroupRunState { */ public void countStopNum(String key) { mStopTemp.add(key); - mStopNum++; + mStopNum.getAndIncrement(); } /** @@ -180,6 +183,6 @@ public final class GroupRunState { */ public void countFailNum(String key) { mFailTemp.add(key); - mFailNum++; + mFailNum.getAndIncrement(); } } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java index a84d59f6..ad2583de 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java @@ -121,9 +121,14 @@ final class SimpleSchedulers implements Handler.Callback { || mGState.getStopNum() + mGState.getFailNum() + mGState.getCompleteNum() == mGState.getSubSize()) { mQueue.clear(); - mGState.isRunning = false; - mGState.listener.onFail(false, new AriaException(TAG, - String.format("任务组【%s】下载失败", mGState.getGroupHash()))); + mGState.isRunning.set(false); + if (mGState.getCompleteNum() > 0) { + ALog.e(TAG, String.format("任务组【%s】停止", mGState.getGroupHash())); + mGState.listener.onStop(mGState.getProgress()); + } else { + mGState.listener.onFail(false, new AriaException(TAG, + String.format("任务组【%s】下载失败", mGState.getGroupHash()))); + } } else { startNext(); } @@ -147,7 +152,7 @@ final class SimpleSchedulers implements Handler.Callback { + mQueue.getCacheSize() == mGState.getSubSize()) { mQueue.clear(); - mGState.isRunning = false; + mGState.isRunning.set(false); mGState.listener.onStop(mGState.getProgress()); } else { startNext(); @@ -183,7 +188,7 @@ final class SimpleSchedulers implements Handler.Callback { mGState.listener.onStop(mGState.getProgress()); } mQueue.clear(); - mGState.isRunning = false; + mGState.isRunning.set(false); } else { startNext(); } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java index 22af4255..57853af7 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java @@ -24,26 +24,28 @@ import com.arialyy.aria.core.inf.IThreadStateManager; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.exception.AriaException; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.FileUtil; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; /** * 线程任务管理器,用于处理多线程下载时任务的状态回调 */ public class NormalThreadStateManager implements IThreadStateManager { - private final String TAG = "ThreadTaskStateManager"; + private final String TAG = CommonUtil.getClassName(this); /** * 任务状态回调 */ private IEventListener mListener; private int mThreadNum; // 启动的线程总数 - private int mCancelNum = 0; // 已经取消的线程的数 - private int mStopNum = 0; // 已经停止的线程数 - private int mFailNum = 0; // 失败的线程数 - private int mCompleteNum = 0; // 完成的线程数 + private AtomicInteger mCancelNum = new AtomicInteger(0); // 已经取消的线程的数 + private AtomicInteger mStopNum = new AtomicInteger(0); // 已经停止的线程数 + private AtomicInteger mFailNum = new AtomicInteger(0); // 失败的线程数 + private AtomicInteger mCompleteNum = new AtomicInteger(0); // 完成的线程数 private long mProgress; //当前总进度 private TaskRecord mTaskRecord; // 任务记录 private Looper mLooper; @@ -75,19 +77,19 @@ public class NormalThreadStateManager implements IThreadStateManager { checkLooper(); switch (msg.what) { case STATE_STOP: - mStopNum++; + mStopNum.getAndIncrement(); if (isStop()) { quitLooper(); } break; case STATE_CANCEL: - mCancelNum++; + mCancelNum.getAndIncrement(); if (isCancel()) { quitLooper(); } break; case STATE_FAIL: - mFailNum++; + mFailNum.getAndIncrement(); if (isFail()) { Bundle b = msg.getData(); mListener.onFail(b.getBoolean(DATA_RETRY, false), @@ -96,7 +98,7 @@ public class NormalThreadStateManager implements IThreadStateManager { } break; case STATE_COMPLETE: - mCompleteNum++; + mCompleteNum.getAndIncrement(); if (isComplete()) { ALog.d(TAG, "isComplete, completeNum = " + mCompleteNum); //if (mTaskRecord.taskType == ITaskWrapper.D_SFTP) { @@ -168,7 +170,7 @@ public class NormalThreadStateManager implements IThreadStateManager { //ALog.d(TAG, // String.format("isStop; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", mStopNum, // mCancelNum, mFailNum, mCompleteNum)); - return mStopNum == mThreadNum || mStopNum + mCompleteNum == mThreadNum; + return mStopNum.get() == mThreadNum || mStopNum.get() + mCompleteNum.get() == mThreadNum; } /** @@ -179,8 +181,8 @@ public class NormalThreadStateManager implements IThreadStateManager { //ALog.d(TAG, // String.format("isFail; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", mStopNum, // mCancelNum, mFailNum, mCompleteNum)); - return mCompleteNum != mThreadNum - && (mFailNum == mThreadNum || mFailNum + mCompleteNum == mThreadNum); + return mCompleteNum.get() != mThreadNum + && (mFailNum.get() == mThreadNum || mFailNum.get() + mCompleteNum.get() == mThreadNum); } /** @@ -192,7 +194,7 @@ public class NormalThreadStateManager implements IThreadStateManager { // String.format("isComplete; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", // mStopNum, // mCancelNum, mFailNum, mCompleteNum)); - return mCompleteNum == mThreadNum; + return mCompleteNum.get() == mThreadNum; } /** @@ -202,7 +204,7 @@ public class NormalThreadStateManager implements IThreadStateManager { //ALog.d(TAG, String.format("isCancel; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", // mStopNum, // mCancelNum, mFailNum, mCompleteNum)); - return mCancelNum == mThreadNum; + return mCancelNum.get() == mThreadNum; } /** diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java index 1128376f..8e3eca0e 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -98,7 +98,7 @@ class DelegateFind extends AbsDelegate { */ List findRelationData(SQLiteDatabase db, Class clazz, String... expression) { - return exeRelationSql(db, clazz, 1, 10, expression); + return exeRelationSql(db, clazz, 1, Integer.MAX_VALUE, expression); } /** diff --git a/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java index b27f5231..74ad904b 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java @@ -89,7 +89,9 @@ public class DownloadGroupActivity extends BaseActivity