Add the function of delete and stop all tasks

v4
laoyuyu 2 years ago
parent fa7741b3e9
commit 294c21db74
  1. 10
      Aria/src/main/java/com/arialyy/aria/core/common/controller/NormalController.java
  2. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  3. 110
      Aria/src/main/java/com/arialyy/aria/core/download/target/AbsGroupConfigHandler.java
  4. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  5. 113
      Http/src/main/java/com/arialyy/aria/http/HttpCmdHandler.kt
  6. 10
      Http/src/main/java/com/arialyy/aria/http/HttpComponent.kt
  7. 32
      Http/src/main/java/com/arialyy/aria/http/HttpUtil.kt
  8. 2
      Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockManager.kt
  9. 35
      Http/src/main/java/com/arialyy/aria/http/download/HttpDStartController.kt
  10. 6
      Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt
  11. 2
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGAdapter.kt
  12. 13
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGLoader.kt
  13. 4
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGStartController.kt
  14. 4
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGTaskManager.kt
  15. 116
      PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelAllCmd.java
  16. 36
      PublicComponent/src/main/java/com/arialyy/aria/core/command/DeleteAllCmd.java
  17. 8
      PublicComponent/src/main/java/com/arialyy/aria/core/command/DeleteCmd.kt
  18. 18
      PublicComponent/src/main/java/com/arialyy/aria/core/command/ICmdHandler.kt
  19. 9
      PublicComponent/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.kt
  20. 28
      PublicComponent/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java
  21. 28
      PublicComponent/src/main/java/com/arialyy/aria/core/event/DGMaxNumEvent.java
  22. 20
      PublicComponent/src/main/java/com/arialyy/aria/core/event/DeleteAllEvent.kt
  23. 26
      PublicComponent/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java
  24. 16
      PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java
  25. 3
      PublicComponent/src/main/java/com/arialyy/aria/core/event/ResumeAllEvent.kt
  26. 17
      PublicComponent/src/main/java/com/arialyy/aria/core/event/StopAllEvent.kt
  27. 28
      PublicComponent/src/main/java/com/arialyy/aria/core/event/UMaxNumEvent.java
  28. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IPool.java
  29. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskManager.kt
  30. 7
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskQueue.java
  31. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java
  32. 4
      PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java
  33. 4
      PublicComponent/src/main/java/com/arialyy/aria/core/task/ThreadTaskManager2.kt
  34. 10
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DEntityDao.kt
  35. 8
      PublicComponent/src/main/java/com/arialyy/aria/util/Ext.kt
  36. 36
      Queue/src/main/java/com/arialyy/aria/queue/AbsTaskQueue.java
  37. 2
      Queue/src/main/java/com/arialyy/aria/queue/BaseCachePool.java
  38. 2
      Queue/src/main/java/com/arialyy/aria/queue/BaseExecutePool.java
  39. 8
      Schedulers/src/main/java/com/arialyy/aria/schedulers/TaskSchedulers.java

@ -15,7 +15,7 @@
*/
package com.arialyy.aria.core.common.controller;
import com.arialyy.aria.core.command.CancelCmd;
import com.arialyy.aria.core.command.DeleteCmd;
import com.arialyy.aria.core.command.CmdHelper;
import com.arialyy.aria.core.command.NormalCmdFactory;
import com.arialyy.aria.core.command.StartCmd;
@ -106,11 +106,11 @@ public final class NormalController extends FeatureController implements INormal
public void cancel(boolean removeFile) {
setAction(ACTION_CANCEL);
if (checkConfig()) {
CancelCmd cancelCmd =
(CancelCmd) CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL,
DeleteCmd deleteCmd =
(DeleteCmd) CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL,
checkTaskType());
cancelCmd.removeFile = removeFile;
EventMsgUtil.getDefault().post(cancelCmd);
deleteCmd.removeFile = removeFile;
EventMsgUtil.getDefault().post(deleteCmd);
}
}

@ -18,7 +18,7 @@ package com.arialyy.aria.core.download;
import com.arialyy.annotations.TaskEnum;
import com.arialyy.aria.core.AriaConfig;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.CancelAllCmd;
import com.arialyy.aria.core.command.DeleteAllCmd;
import com.arialyy.aria.core.command.CmdHelper;
import com.arialyy.aria.core.command.NormalCmdFactory;
import com.arialyy.aria.core.common.AbsBuilderTarget;
@ -506,8 +506,8 @@ public class DownloadReceiver extends AbsReceiver {
*/
public void removeAllTask(boolean removeFile) {
final AriaManager ariaManager = AriaManager.getInstance();
CancelAllCmd cancelCmd =
(CancelAllCmd) CmdHelper.createNormalCmd(new DTaskWrapper(null),
DeleteAllCmd cancelCmd =
(DeleteAllCmd) CmdHelper.createNormalCmd(new DTaskWrapper(null),
NormalCmdFactory.TASK_CANCEL_ALL, ITask.SINGLE_DOWNLOAD);
cancelCmd.removeFile = removeFile;
EventMsgUtil.getDefault().post(cancelCmd);

@ -1,110 +0,0 @@
/*
* 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.download.target;
import android.text.TextUtils;
import com.arialyy.aria.core.common.AbsNormalTarget;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.event.ErrorEvent;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.IConfigHandler;
import com.arialyy.aria.core.manager.SubTaskManager;
import com.arialyy.aria.core.manager.TaskWrapperManager;
import com.arialyy.aria.core.queue.DGroupTaskQueue;
import com.arialyy.aria.core.task.DownloadGroupTask;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
/**
* Created by lyy on 2019/4/9.
* 下载组合任务功能
*/
abstract class AbsGroupConfigHandler<TARGET extends AbsTarget> implements IConfigHandler {
protected String TAG;
private TARGET mTarget;
private DGTaskWrapper mWrapper;
private SubTaskManager mSubTaskManager;
AbsGroupConfigHandler(TARGET target, long taskId) {
TAG = CommonUtil.getClassName(getClass());
mTarget = target;
mWrapper = TaskWrapperManager.getInstance().getGroupWrapper(DGTaskWrapper.class, taskId);
// 判断已存在的任务
if (mTarget instanceof AbsNormalTarget) {
if (taskId < 0) {
mWrapper.setErrorEvent(new ErrorEvent(taskId, "任务id为空"));
} else if (mWrapper.getEntity().getId() < 0) {
mWrapper.setErrorEvent(new ErrorEvent(taskId, "任务信息不存在"));
}
}
mTarget.setTaskWrapper(mWrapper);
if (getEntity() != null) {
getTaskWrapper().setDirPathTemp(getEntity().getDirPath());
}
}
/**
* 获取子任务管理器
*
* @return 子任务管理器
*/
SubTaskManager getSubTaskManager() {
if (mSubTaskManager == null) {
mSubTaskManager = new SubTaskManager(getTaskWrapper());
}
return mSubTaskManager;
}
/**
* 设置任务组别名
*/
void setGroupAlias(String alias) {
if (TextUtils.isEmpty(alias)) {
return;
}
getEntity().setAlias(alias);
}
@Override public boolean isRunning() {
DownloadGroupTask task = DGroupTaskQueue.getInstance().getTask(getEntity().getKey());
return task != null && task.isRunning();
}
TARGET setDirPath(String dirPath) {
mWrapper.setDirPathTemp(dirPath);
return mTarget;
}
@Override public DownloadGroupEntity getEntity() {
return mWrapper.getEntity();
}
@Override public boolean taskExists() {
return getEntity().getId() != -1 && DbEntity.checkDataExist(DownloadGroupEntity.class,
"rowid=?", String.valueOf(getEntity().getId()));
}
DGTaskWrapper getTaskWrapper() {
return mWrapper;
}
TARGET getTarget() {
return mTarget;
}
}

@ -19,7 +19,7 @@ import android.text.TextUtils;
import com.arialyy.annotations.TaskEnum;
import com.arialyy.aria.core.AriaConfig;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.CancelAllCmd;
import com.arialyy.aria.core.command.DeleteAllCmd;
import com.arialyy.aria.core.command.CmdHelper;
import com.arialyy.aria.core.command.NormalCmdFactory;
import com.arialyy.aria.core.common.AbsBuilderTarget;
@ -251,8 +251,8 @@ public class UploadReceiver extends AbsReceiver {
*/
public void removeAllTask(boolean removeFile) {
final AriaManager am = AriaManager.getInstance();
CancelAllCmd cancelCmd =
(CancelAllCmd) CmdHelper.createNormalCmd(new UTaskWrapper(null),
DeleteAllCmd cancelCmd =
(DeleteAllCmd) CmdHelper.createNormalCmd(new UTaskWrapper(null),
NormalCmdFactory.TASK_CANCEL_ALL, ITask.UPLOAD);
cancelCmd.removeFile = removeFile;

@ -0,0 +1,113 @@
/*
* 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.http
import androidx.annotation.Keep
import com.arialyy.aria.core.DuaContext
import com.arialyy.aria.core.command.ICmdHandler
import com.arialyy.aria.core.command.StartCmd
import com.arialyy.aria.core.event.DeleteAllEvent
import com.arialyy.aria.core.event.Event
import com.arialyy.aria.core.event.EventMsgUtil
import com.arialyy.aria.core.event.ResumeAllEvent
import com.arialyy.aria.core.event.StopAllEvent
import com.arialyy.aria.http.download.HttpDTaskAdapter
import com.arialyy.aria.util.FileUtils
import com.arialyy.aria.util.isNotComplete
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import timber.log.Timber
/**
* @Author laoyuyu
* @Description
* @Date 12:40 PM 2023/4/2
**/
@Keep
internal object HttpCmdHandler : ICmdHandler {
override fun initHandler() {
EventMsgUtil.getDefault().register(this)
}
@Event
fun resumeAll(event: ResumeAllEvent) {
DuaContext.duaScope.launch(Dispatchers.IO) {
resumeDTask()
}
}
@Event
fun stopAll(event: StopAllEvent) {
DuaContext.duaScope.launch(Dispatchers.IO) {
DuaContext.getServiceManager().getDownloadQueue().stopAllTask()
}
}
@Event
fun removeAll(event: DeleteAllEvent) {
DuaContext.duaScope.launch(Dispatchers.IO) {
removeAllDTask(event)
}
}
private suspend fun removeAllDTask(event: DeleteAllEvent) {
val dao = DuaContext.getServiceManager().getDbService().getDuaDb().getDEntityDao()
val entityList = dao.queryDEntityList()
DuaContext.getServiceManager().getDownloadQueue().deleteAllTask()
dao.deleteAll()
if (event.onlyRemoveRecord) {
Timber.d("Only remove record")
return
}
// Delete the downloaded file
entityList.forEach {
if (it.fileIsComplete()) {
val path = it.getFilePath()
Timber.d("Delete file: $path")
FileUtils.deleteFile(path)
}
}
}
/**
* Recovery status is an unfinished task
*/
private suspend fun resumeDTask() {
val entityList = DuaContext.getServiceManager().getDbService().getDuaDb().getDEntityDao()
.queryAllNotCompleteEntityList()
if (entityList.isNullOrEmpty()) {
Timber.w("No tasks to recover")
return
}
entityList.forEach {
if (!it.isNotComplete()) {
Timber.d("Ignore the task, task status: ${it.state}")
return@forEach
}
val taskOption = HttpTaskOption()
taskOption.sourUrl = it.sourceUrl
val tempTask = HttpUtil.getSingDTask(taskOption)
if (tempTask == null) {
Timber.e("Resume task fail, url: ${it.sourceUrl}")
return@forEach
}
val taskAdapter = HttpDTaskAdapter()
tempTask.adapter = taskAdapter
StartCmd(tempTask).executeCmd()
}
}
}

@ -1,9 +1,6 @@
package com.arialyy.aria.http
import android.content.Context
import com.arialyy.aria.core.event.Event
import com.arialyy.aria.core.event.EventMsgUtil
import com.arialyy.aria.core.event.ResumeAllEvent
import com.arialyy.aria.core.inf.IComponentInit
/**
@ -13,12 +10,7 @@ import com.arialyy.aria.core.inf.IComponentInit
**/
class HttpComponent : IComponentInit {
override fun init(context: Context) {
EventMsgUtil.getDefault().register(this)
}
@Event
fun resumeAll(resumeAllEvent: ResumeAllEvent) {
HttpCmdHandler.initHandler()
}
}

@ -16,6 +16,9 @@
package com.arialyy.aria.http
import android.text.TextUtils
import com.arialyy.aria.core.task.SingleDownloadTask
import com.arialyy.aria.core.task.TaskCachePool
import com.arialyy.aria.http.download.HttpDTaskAdapter
import com.arialyy.aria.util.FileUtils
import com.arialyy.aria.util.Regular
import timber.log.Timber
@ -33,6 +36,33 @@ import java.util.zip.InflaterInputStream
**/
object HttpUtil {
/**
* get or create single task
* @param createNewTask if not found task in [TaskCachePool], create new task
*/
fun getSingDTask(
httpTaskOption: HttpTaskOption,
createNewTask: Boolean = true
): SingleDownloadTask? {
if (checkHttpDParams(httpTaskOption)) {
throw IllegalArgumentException("invalid params")
}
if (httpTaskOption.sourUrl.isNullOrEmpty()) {
return null
}
val tempTask = TaskCachePool.getTaskByKey(httpTaskOption.sourUrl!!)
if (tempTask != null) {
return tempTask as SingleDownloadTask
}
if (!createNewTask) {
return null
}
val task = SingleDownloadTask(httpTaskOption)
task.adapter = HttpDTaskAdapter()
TaskCachePool.putTask(task)
return task
}
/**
* 拦截window.location.replace数据
*
@ -95,7 +125,7 @@ object HttpUtil {
Timber.e("invalid uri, ${option.savePathDir}")
return false
}
if (!FileUtils.uriIsDir(option.savePathDir!!)){
if (!FileUtils.uriIsDir(option.savePathDir!!)) {
Timber.e("invalid uri, that path not a dir")
return false
}

@ -43,7 +43,7 @@ class HttpDBlockManager(task: ITask) : DBlockManager(task) {
}
}
override fun cancel() {
override fun delete() {
optionAdapter.threadList.forEach {
it.cancel()
}

@ -18,16 +18,14 @@ package com.arialyy.aria.http.download
import android.net.Uri
import com.arialyy.aria.core.DuaContext
import com.arialyy.aria.core.command.AddCmd
import com.arialyy.aria.core.command.CancelCmd
import com.arialyy.aria.core.command.DeleteCmd
import com.arialyy.aria.core.command.StartCmd
import com.arialyy.aria.core.command.StopCmd
import com.arialyy.aria.http.IHttpFileLenAdapter
import com.arialyy.aria.core.task.SingleDownloadTask
import com.arialyy.aria.core.task.ITaskInterceptor
import com.arialyy.aria.core.task.TaskCachePool
import com.arialyy.aria.http.HttpBaseStartController
import com.arialyy.aria.http.HttpOption
import com.arialyy.aria.http.HttpUtil
import com.arialyy.aria.http.IHttpFileLenAdapter
import timber.log.Timber
import java.net.HttpURLConnection
@ -92,28 +90,11 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll
return this
}
private fun getTask(createNewTask: Boolean = true): SingleDownloadTask? {
if (HttpUtil.checkHttpDParams(httpTaskOption)) {
throw IllegalArgumentException("invalid params")
}
val tempTask = TaskCachePool.getTaskByKey(url)
if (tempTask != null) {
return tempTask as SingleDownloadTask
}
if (!createNewTask) {
return null
}
val task = SingleDownloadTask(httpTaskOption)
task.adapter = HttpDTaskAdapter()
TaskCachePool.putTask(task)
return task
}
fun add(): Int {
if (!HttpUtil.checkHttpDParams(httpTaskOption)) {
return -1
}
val task = getTask()
val task = HttpUtil.getSingDTask(httpTaskOption)
val resp = AddCmd(task).executeCmd()
return if (resp.isInterrupt()) -1 else task?.taskId ?: -1
}
@ -122,7 +103,7 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll
if (!HttpUtil.checkHttpDParams(httpTaskOption)) {
return -1
}
val task = getTask()
val task = HttpUtil.getSingDTask(httpTaskOption)
val resp = StartCmd(task).executeCmd()
return if (resp.isInterrupt()) -1 else task?.taskId ?: -1
}
@ -131,17 +112,17 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll
return start()
}
fun cancel() {
val task = getTask(true)
fun delete() {
val task = HttpUtil.getSingDTask(httpTaskOption, true)
if (task == null) {
Timber.e("not found task, url: $url")
return
}
CancelCmd(task).executeCmd()
DeleteCmd(task).executeCmd()
}
fun stop() {
val task = getTask(false)
val task = HttpUtil.getSingDTask(httpTaskOption, false)
if (task == null) {
Timber.e("task not running, url: $url")
return

@ -15,7 +15,7 @@
*/
package com.arialyy.aria.http.download
import com.arialyy.aria.core.command.CancelCmd
import com.arialyy.aria.core.command.DeleteCmd
import com.arialyy.aria.core.command.StopCmd
import com.arialyy.aria.core.task.TaskCachePool
import timber.log.Timber
@ -27,13 +27,13 @@ import timber.log.Timber
**/
class HttpDStopController(val taskId: Int) {
fun cancel() {
fun delete() {
val task = TaskCachePool.getTaskById(taskId)
if (task == null) {
Timber.e("task not found, taskId: $taskId")
return
}
CancelCmd(task).executeCmd()
DeleteCmd(task).executeCmd()
}
fun stop() {

@ -59,7 +59,7 @@ internal class HttpDGAdapter : AbsTaskAdapter() {
Timber.w("task already canceled, taskId: ${getTask().taskId}")
return
}
it.cancel()
it.delete()
}
}

@ -32,6 +32,19 @@ class HttpDGLoader(private val target: Any) : IDownloader {
/**
* start, create a task
*
*```
* + mnt
* + sdcard
* + download
* + group_test
* - task1.apk
* - task2.apk
* - task3.apk
* ....
*
*```
*
* @param savePath download url
*/
fun load(savePath: Uri): HttpDGStartController {

@ -18,7 +18,7 @@ package com.arialyy.dua.group
import android.net.Uri
import com.arialyy.aria.core.DuaContext
import com.arialyy.aria.core.command.AddCmd
import com.arialyy.aria.core.command.CancelCmd
import com.arialyy.aria.core.command.DeleteCmd
import com.arialyy.aria.core.command.StartCmd
import com.arialyy.aria.core.command.StopCmd
import com.arialyy.aria.core.task.ITaskInterceptor
@ -149,7 +149,7 @@ class HttpDGStartController(target: Any, private val savePath: Uri) :
Timber.e("not found task, savePath: $savePath")
return
}
CancelCmd(task).executeCmd()
DeleteCmd(task).executeCmd()
}
fun stop() {

@ -132,10 +132,10 @@ internal class HttpDGTaskManager(private val task: HttpDGTask) : ITaskManager, I
}
}
override fun cancel() {
override fun delete() {
scope.launch(Dispatchers.IO) {
task.subTaskList.forEach {
it.cancel(TaskSchedulerType.TYPE_DEFAULT)
it.delete(TaskSchedulerType.TYPE_DEFAULT)
}
task.subTaskList.clear()
}

@ -1,116 +0,0 @@
/*
* 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.command;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.manager.TaskWrapperManager;
import com.arialyy.aria.core.queue.DGroupTaskQueue;
import com.arialyy.aria.core.queue.DTaskQueue;
import com.arialyy.aria.core.queue.UTaskQueue;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import java.util.List;
/**
* Created by AriaL on 2017/6/27.
* 删除所有任务并且删除所有回掉
*/
final public class CancelAllCmd<T extends AbsTaskWrapper> extends AbsNormalCmd<T> {
/**
* removeFile {@code true} 删除已经下载完成的任务不仅删除下载记录还会删除已经下载完成的文件{@code false}
* 如果文件已经下载完成只删除下载记录
*/
public boolean removeFile = false;
CancelAllCmd(T entity, int taskType) {
super(entity, taskType);
}
@Override public void executeCmd() {
if (!canExeCmd) return;
if (isDownloadCmd) {
removeAllDTask();
removeAllDGTask();
} else {
removeUTask();
}
}
/**
* 删除所有普通下载任务
*/
private void removeAllDTask() {
List<DownloadEntity> entities =
DbEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false");
if (entities != null && !entities.isEmpty()) {
for (DownloadEntity entity : entities) {
remove(TaskWrapperManager.getInstance()
.getNormalTaskWrapper(DTaskWrapper.class, entity.getId()));
}
}
}
/**
* 删除所有下载任务组任务
*/
private void removeAllDGTask() {
List<DownloadGroupEntity> entities =
DbEntity.findDatas(DownloadGroupEntity.class, "state!=?", "-1");
if (entities != null && !entities.isEmpty()) {
for (DownloadGroupEntity entity : entities) {
remove(TaskWrapperManager.getInstance()
.getGroupWrapper(DGTaskWrapper.class, entity.getId()));
}
}
}
/**
* 删除所有普通上传任务
*/
private void removeUTask() {
List<UploadEntity> entities =
DbEntity.findDatas(UploadEntity.class, "isGroupChild=?", "false");
if (entities != null && !entities.isEmpty()) {
for (UploadEntity entity : entities) {
remove(TaskWrapperManager.getInstance()
.getNormalTaskWrapper(UTaskWrapper.class, entity.getId()));
}
}
}
private void remove(AbsTaskWrapper te) {
if (te == null) {
ALog.w(TAG, "取消任务失败,任务为空");
return;
}
if (te instanceof DTaskWrapper) {
mQueue = DTaskQueue.getInstance();
} else if (te instanceof UTaskWrapper) {
mQueue = UTaskQueue.getInstance();
} else if (te instanceof DGTaskWrapper) {
mQueue = DGroupTaskQueue.getInstance();
}
te.setRemoveFile(removeFile);
removeTask(te);
}
}

@ -13,32 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.command;
import com.arialyy.aria.core.event.EventMsgUtil;
import com.arialyy.aria.core.event.DeleteAllEvent;
/**
* 命令处理器
* Created by AriaL on 2017/6/27.
* 删除所有任务并且删除所有回掉
*/
public class CommandManager {
final public class DeleteAllCmd implements ICmd {
private static CommandManager instance;
private CommandManager() {
}
private boolean onlyDeleteRecord;
public static void init() {
if (instance == null) {
synchronized (CommandManager.class) {
if (instance == null) {
instance = new CommandManager();
}
}
}
/**
* @param onlyDeleteRecord true: Delete records only;
* false: Delete file and records
*/
public DeleteAllCmd(boolean onlyDeleteRecord) {
this.onlyDeleteRecord = onlyDeleteRecord;
}
public void exeCmd(ICmd cmd) {
//if (CommonUtil.isFastDoubleClick()) {
// return;
//}
cmd.executeCmd();
@Override public CmdResp executeCmd() {
EventMsgUtil.getDefault().post(new DeleteAllEvent(onlyDeleteRecord));
return new CmdResp(CmdResp.CODE_COMPLETE);
}
}

@ -27,7 +27,7 @@ import timber.log.Timber
* Created by lyy on 2016/9/20.
* 取消命令
*/
class CancelCmd(task: ITask) : AbsCmd(task) {
class DeleteCmd(task: ITask) : AbsCmd(task) {
override fun executeCmd(): CmdResp {
val resp = interceptor()
@ -35,12 +35,6 @@ class CancelCmd(task: ITask) : AbsCmd(task) {
Timber.w("interruption occurred, cancel cmd")
return resp
}
if (!taskQueue.taskIsRunning(mTask?.taskId ?: -1)) {
// remove already file
return CmdResp(CmdResp.CODE_COMPLETE)
}
DuaContext.duaScope.launch(Dispatchers.IO) {
ThreadTaskManager2.stopThreadTask(mTask!!.taskId, true)
}

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.event;
package com.arialyy.aria.core.command
public class USpeedEvent {
public int speed;
public USpeedEvent(int speed) {
this.speed = speed;
}
}
/**
* @Author laoyuyu
* @Description
* @Date 21:51 2023/4/2
**/
interface ICmdHandler {
fun initHandler()
}

@ -15,9 +15,8 @@
*/
package com.arialyy.aria.core.command
import com.arialyy.aria.core.DuaContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import com.arialyy.aria.core.event.EventMsgUtil
import com.arialyy.aria.core.event.ResumeAllEvent
/**
* Created by AriaL on 2017/6/13.
@ -30,9 +29,7 @@ import kotlinx.coroutines.launch
class ResumeAllCmd() : ICmd {
override fun executeCmd(): CmdResp {
DuaContext.duaScope.launch(Dispatchers.IO) {
}
EventMsgUtil.getDefault().post(ResumeAllEvent())
return CmdResp(CmdResp.CODE_COMPLETE)
}
}

@ -1,16 +1,30 @@
/*
* 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.command;
import com.arialyy.aria.core.event.EventMsgUtil;
import com.arialyy.aria.core.event.StopAllEvent;
/**
* Created by AriaL on 2017/6/13.
* 停止所有任务的命令并清空所有等待队列
*/
final class StopAllCmd extends AbsCmd {
StopAllCmd(T entity, int taskType) {
super(entity, taskType);
}
@Override public void executeCmd() {
stopAll();
final class StopAllCmd implements ICmd {
@Override public CmdResp executeCmd() {
EventMsgUtil.getDefault().post(new StopAllEvent());
return new CmdResp(CmdResp.CODE_COMPLETE);
}
}

@ -1,28 +0,0 @@
/*
* 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.event;
/**
* 组合任务最大下载任务数事件
*/
public class DGMaxNumEvent {
public int maxNum;
public DGMaxNumEvent(int maxNum) {
this.maxNum = maxNum;
}
}

@ -13,16 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.event;
package com.arialyy.aria.core.event
/**
* 最大下载任务数事件
*/
public class DMaxNumEvent {
import androidx.annotation.Keep
public int maxNum;
public DMaxNumEvent(int maxNum) {
this.maxNum = maxNum;
}
}
/**
* @Author laoyuyu
* @Description
* @Date 22:12 2023/4/2
**/
@Keep
data class DeleteAllEvent(val onlyRemoveRecord: Boolean = true)

@ -1,26 +0,0 @@
/*
* 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.event;
public class ErrorEvent {
public long taskId;
public String errorMsg;
public ErrorEvent(long taskId, String errorMsg) {
this.taskId = taskId;
this.errorMsg = errorMsg;
}
}

@ -15,7 +15,6 @@
*/
package com.arialyy.aria.core.event;
import com.arialyy.aria.util.ALog;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@ -28,17 +27,17 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import timber.log.Timber;
/**
* 消息发送工具
*/
public class EventMsgUtil {
private static final String TAG = "EventUtil";
private static EventMsgUtil defaultInstance;
private Map<Object, List<EventMethodInfo>> mEventMethods =
private final Map<Object, List<EventMethodInfo>> mEventMethods =
new ConcurrentHashMap<>();
private ArrayBlockingQueue<Object> mEventQueue = new ArrayBlockingQueue<>(10);
private ExecutorService mPool = Executors.newFixedThreadPool(5);
private final ArrayBlockingQueue<Object> mEventQueue = new ArrayBlockingQueue<>(10);
private final ExecutorService mPool = Executors.newFixedThreadPool(5);
private EventMsgUtil() {
ExecutorService pool = Executors.newSingleThreadExecutor();
@ -102,15 +101,14 @@ public class EventMsgUtil {
continue;
}
Class<?>[] clazz = method.getParameterTypes();
if (clazz.length == 0 || clazz.length > 1) {
ALog.e(TAG,
String.format("%s.%s参数数量为0或参数数量大于1", obj.getClass().getName(), method.getName()));
if (clazz.length != 1) {
Timber.e("%s.%s参数数量为0或参数数量大于1", obj.getClass().getName(), method.getName());
continue;
}
int modifier = method.getModifiers();
if (Modifier.isStatic(modifier) || Modifier.isAbstract(modifier) || Modifier.isFinal(
modifier)) {
ALog.e(TAG, "注册的方法不能使用final、static、abstract修饰");
Timber.e("注册的方法不能使用final、static、abstract修饰");
continue;
}

@ -15,9 +15,12 @@
*/
package com.arialyy.aria.core.event
import androidx.annotation.Keep
/**
* @Author laoyuyu
* @Description
* @Date 8:26 PM 2023/3/21
**/
@Keep
class ResumeAllEvent

@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.event;
package com.arialyy.aria.core.event
public class DSpeedEvent {
import androidx.annotation.Keep
public int speed;
public DSpeedEvent(int speed) {
this.speed = speed;
}
}
/**
* @Author laoyuyu
* @Description
* @Date 21:57 2023/4/2
**/
@Keep
class StopAllEvent

@ -1,28 +0,0 @@
/*
* 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.event;
/**
* 最大上传任务数事件
*/
public class UMaxNumEvent {
public int maxNum;
public UMaxNumEvent(int maxNum) {
this.maxNum = maxNum;
}
}

@ -59,7 +59,7 @@ public interface IPool<T extends ITask> {
/**
* @return true:移除成功
*/
boolean removeTask(int taskId);
boolean deleteTask(int taskId);
/**
* 池子大小

@ -47,7 +47,7 @@ interface ITaskManager {
fun stop()
fun cancel()
fun delete()
/**
* 任务是否已经完成

@ -59,7 +59,12 @@ public interface ITaskQueue<TASK extends ITask> extends IService {
/**
* remove task
*/
void removeTask(TASK task);
void deleteTask(TASK task);
/**
* Delete all active tasks
*/
void deleteAllTask();
/**
* 重试

@ -183,7 +183,7 @@ public abstract class AbsTask implements ITask {
mAdapter.stop();
}
@Override public void cancel(int type) {
@Override public void delete(int type) {
if (mAdapter == null) {
Timber.e("util is null");
return;

@ -91,11 +91,11 @@ public interface ITask {
void stop(int type);
/**
* 停止任务
* 删除任务
*
* @param type {@link TaskSchedulerType}
*/
void cancel(int type);
void delete(int type);
/**
* 任务是否停止了

@ -48,7 +48,7 @@ object ThreadTaskManager2 {
return
}
if (isRemoveTask) {
taskManager.cancel()
taskManager.delete()
} else {
taskManager.stop()
}
@ -63,7 +63,7 @@ object ThreadTaskManager2 {
return
}
taskManagerMap.forEach {
it.value.cancel()
it.value.delete()
}
taskManagerMap.clear()
}

@ -21,6 +21,7 @@ import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.arialyy.aria.core.inf.IEntity
import com.arialyy.aria.orm.entity.DEntity
/**
@ -38,6 +39,10 @@ interface DEntityDao {
@Query("SELECT * FROM DEntity WHERE :savePath=savePath")
suspend fun queryDEntityBySavePath(savePath: String): DEntity?
@Transaction
@Query("SELECT * FROM DEntity WHERE state!=${IEntity.STATE_COMPLETE}")
suspend fun queryAllNotCompleteEntityList(): List<DEntity>?
@Query("SELECT * FROM DEntity WHERE :dId=dId")
suspend fun queryDEntityById(did: String): DEntity?
@ -52,4 +57,9 @@ interface DEntityDao {
@Delete
suspend fun delete(dEntity: DEntity)
@Transaction
@Query("DELETE FROM DEntity")
suspend fun deleteAll()
}

@ -19,6 +19,8 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import com.arialyy.aria.core.DuaContext
import com.arialyy.aria.core.inf.IEntity
import com.arialyy.aria.orm.entity.DEntity
import timber.log.Timber
import java.io.File
import java.math.BigInteger
@ -29,7 +31,7 @@ fun File.uri(): Uri {
return Uri.parse(path)
}
fun String.uri():Uri{
fun String.uri(): Uri {
return Uri.parse(this)
}
@ -81,4 +83,8 @@ inline fun <R> Uri.use(block: Uri.() -> R): R {
}
}
return block()
}
fun DEntity.isNotComplete(): Boolean {
return state != IEntity.STATE_COMPLETE && state != IEntity.STATE_CANCEL
}

@ -99,18 +99,18 @@ public abstract class AbsTaskQueue<TASK extends ITask> implements ITaskQueue<TAS
return b ? task.getTaskId() : -1;
}
@Override public void removeTask(TASK task) {
@Override public void deleteTask(TASK task) {
if (task == null) {
Timber.e("task is null");
return;
}
if (getCachePool().taskExist(task.getTaskId())) {
Timber.i("cache pool has task, which will be removed from the cache pool");
getCachePool().removeTask(task.getTaskId());
getCachePool().deleteTask(task.getTaskId());
}
if (getExePool().taskExist(task.getTaskId())) {
stopTask(task);
getExePool().removeTask(task.getTaskId());
getExePool().deleteTask(task.getTaskId());
}
}
@ -135,6 +135,28 @@ public abstract class AbsTaskQueue<TASK extends ITask> implements ITaskQueue<TAS
}
ThreadTaskManager2.INSTANCE.stopAllThreadTask();
getCachePool().clear();
getExePool().clear();
}
@Override public void deleteAllTask() {
for (TASK task : getExePool().getAllTask()) {
if (task != null) {
int state = task.getTaskState().getState();
if (task.isRunning()
|| (state != IEntity.STATE_COMPLETE && state != IEntity.STATE_CANCEL)) {
task.delete(TaskSchedulerType.TYPE_STOP_NOT_NEXT);
}
}
}
for (TASK task : getCachePool().getAllTask()) {
if (task != null) {
task.delete(TaskSchedulerType.TYPE_STOP_NOT_NEXT);
}
}
ThreadTaskManager2.INSTANCE.stopAllThreadTask();
getCachePool().clear();
getExePool().clear();
}
/**
@ -151,13 +173,13 @@ public abstract class AbsTaskQueue<TASK extends ITask> implements ITaskQueue<TAS
boolean canStop = false;
switch (state) {
case IEntity.STATE_WAIT:
getCachePool().removeTask(task.getTaskId());
getCachePool().deleteTask(task.getTaskId());
canStop = true;
break;
case IEntity.STATE_POST_PRE:
case IEntity.STATE_PRE:
case IEntity.STATE_RUNNING:
getExePool().removeTask(task.getTaskId());
getExePool().deleteTask(task.getTaskId());
canStop = true;
break;
case IEntity.STATE_STOP:
@ -165,8 +187,8 @@ public abstract class AbsTaskQueue<TASK extends ITask> implements ITaskQueue<TAS
case IEntity.STATE_FAIL:
Timber.w("stop task fail,it already topped, taskId: %d", task.getTaskId());
if (taskIsRunning(task.getTaskId())) {
getCachePool().removeTask(task.getTaskId());
getExePool().removeTask(task.getTaskId());
getCachePool().deleteTask(task.getTaskId());
getExePool().deleteTask(task.getTaskId());
if (ThreadTaskManager2.INSTANCE.taskIsRunning(task.getTaskId())) {
ThreadTaskManager2.INSTANCE.stopThreadTask(task.getTaskId(), false);
}

@ -122,7 +122,7 @@ public class BaseCachePool<TASK extends ITask> implements IPool<TASK> {
return getTask(taskId) != null;
}
@Override public boolean removeTask(int taskId) {
@Override public boolean deleteTask(int taskId) {
if (taskId <= 0) {
Timber.e("invalid taskId: %s", taskId);
return false;

@ -146,7 +146,7 @@ public class BaseExecutePool<TASK extends ITask> implements IPool<TASK> {
return getTask(taskId) != null;
}
@Override public boolean removeTask(int taskId) {
@Override public boolean deleteTask(int taskId) {
if (taskId < 0) {
Timber.e("invalid taskId");
return false;

@ -312,7 +312,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
}
break;
case CANCEL:
queue.removeTask(task);
queue.deleteTask(task);
if (!queue.isFull()) {
Timber.d("remove task success, if there is a task, start the next task, taskId: %s",
@ -323,7 +323,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
}
break;
case COMPLETE:
queue.removeTask(task);
queue.deleteTask(task);
Timber.d("task complete, taskId: %s", task.getTaskId());
queue.startNextTask();
break;
@ -502,7 +502,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
*/
private void handleFailTask(final ITaskQueue<ITask> queue, final TASK task) {
if (!task.getTaskState().getNeedRetry() || task.isStopped() || task.isCanceled()) {
queue.removeTask(task);
queue.deleteTask(task);
queue.startNextTask();
normalTaskCallback(FAIL, task);
return;
@ -511,7 +511,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
boolean isNotNetRetry = mAriaConfig.getAConfig().isNotNetRetry();
if ((!NetUtils.isConnected(mAriaConfig.getAPP()) && !isNotNetRetry)) {
queue.removeTask(task);
queue.deleteTask(task);
queue.startNextTask();
normalTaskCallback(FAIL, task);
return;

Loading…
Cancel
Save