From ef754f04ccba12ee057360fa2158bb9a6f95d111 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sat, 11 Mar 2023 17:10:31 +0800 Subject: [PATCH] optimize controller code --- .../arialyy/aria/core/queue/AbsTaskQueue.java | 368 ------------------ .../http/download/HttpDBlockInterceptor.kt | 3 +- .../http/download/HttpDCheckInterceptor.kt | 12 +- .../http/download/HttpDHeaderInterceptor.kt | 7 +- .../http/download/HttpDStartController.kt | 39 +- .../aria/http/download/HttpDStopController.kt | 13 +- .../http/download/HttpDStopController2.kt | 83 ---- .../aria/http/download/HttpDownloader.kt | 9 - .../arialyy/aria/core/command/StartCmd.java | 4 + .../com/arialyy/aria/core/inf/BaseEntity.kt | 3 +- .../arialyy/aria/core/task/DownloadTask.java | 13 +- .../com/arialyy/aria/core/task/ITask.java | 2 + .../arialyy/aria/core/task/TaskCachePool.kt | 27 +- .../com/arialyy/aria/core/task/TaskState.kt | 7 + .../com/arialyy/aria/orm/entity/DEntity.kt | 4 +- 15 files changed, 87 insertions(+), 507 deletions(-) delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java delete mode 100644 Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java deleted file mode 100644 index 2ca227ef..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ /dev/null @@ -1,368 +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.queue; - -import android.text.TextUtils; -import com.arialyy.aria.core.common.AbsEntity; -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.ITaskQueue; -import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.manager.ThreadTaskManager; -import com.arialyy.aria.core.queue.pool.DGLoadSharePool; -import com.arialyy.aria.core.queue.pool.DLoadSharePool; -import com.arialyy.aria.core.queue.pool.UploadSharePool; -import com.arialyy.aria.core.task.DownloadGroupTask; -import com.arialyy.aria.core.task.DownloadTask; -import com.arialyy.aria.core.task.ITask; -import com.arialyy.aria.core.task.UploadTask; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by lyy on 2017/2/23. 任务队列 - */ -public abstract class AbsTaskQueue - implements ITaskQueue { - final int TYPE_D_QUEUE = 1; - final int TYPE_DG_QUEUE = 2; - final int TYPE_U_QUEUE = 3; - - private final String TAG = CommonUtil.getClassName(this); - BaseCachePool mCachePool; - BaseExecutePool mExecutePool; - - AbsTaskQueue() { - switch (getQueueType()) { - case TYPE_D_QUEUE: - mCachePool = DLoadSharePool.getInstance().cachePool; - mExecutePool = DLoadSharePool.getInstance().executePool; - break; - case TYPE_DG_QUEUE: - mCachePool = DGLoadSharePool.getInstance().cachePool; - mExecutePool = DGLoadSharePool.getInstance().executePool; - break; - case TYPE_U_QUEUE: - mCachePool = UploadSharePool.getInstance().cachePool; - mExecutePool = UploadSharePool.getInstance().executePool; - break; - } - } - - abstract int getQueueType(); - - /** - * 获取执行中的任务 - * - * @return 没有执行中的任务,返回null - */ - public List getRunningTask(Class type) { - List exeTask = mExecutePool.getAllTask(); - List cacheTask = mCachePool.getAllTask(); - List entities = new ArrayList<>(); - if (exeTask != null && !exeTask.isEmpty()) { - for (TASK task : exeTask) { - entities.add((T) task.getTaskWrapper().getEntity()); - } - } - if (cacheTask != null && !cacheTask.isEmpty()) { - for (TASK task : cacheTask) { - entities.add((T) task.getTaskWrapper().getEntity()); - } - } - return entities.isEmpty() ? null : entities; - } - - @Override public boolean taskIsRunning(String key) { - if (TextUtils.isEmpty(key)) { - ALog.w(TAG, "key为空,无法确认任务是否执行"); - return false; - } - TASK task = mExecutePool.getTask(key); - if (task == null && ThreadTaskManager.getInstance().taskIsRunning(key)) { - ThreadTaskManager.getInstance().removeTaskThread(key); - } - return task != null && task.isRunning() && taskExists(key); - } - - /** - * 恢复任务 如果执行队列任务未满,则直接启动任务。 如果执行队列已经满了,则暂停执行队列队首任务,并恢复指定任务 - * - * @param task 需要恢复的任务 - */ - @Override public void resumeTask(TASK task) { - if (task == null) { - ALog.w(TAG, "resume task fail, task is null"); - return; - } - if (mExecutePool.taskExist(task.getKey())) { - ALog.w(TAG, String.format("task【%s】running", task.getKey())); - return; - } - if (mExecutePool.size() >= getMaxTaskNum()) { - task.getTaskWrapper().getEntity().setState(IEntity.STATE_WAIT); - mCachePool.putTaskToFirst(task); - stopTask(mExecutePool.pollTask()); - } else { - startTask(task); - } - } - - /** - * 停止所有任务 - */ - @Override public void stopAllTask() { - for (TASK task : mExecutePool.getAllTask()) { - if (task != null) { - int state = task.getTaskState(); - if (task.isRunning() || (state != IEntity.STATE_COMPLETE - && state != IEntity.STATE_CANCEL)) { - task.stop(TaskSchedulerType.TYPE_STOP_NOT_NEXT); - } - } - } - - //for (String key : mCachePool.getAllTask().keySet()) { - // TASK task = mCachePool.getAllTask().get(key); - // if (task != null) { - // task.stop(TaskSchedulerType.TYPE_STOP_NOT_NEXT); - // } - //} - for (TASK task : mCachePool.getAllTask()) { - if (task != null) { - task.stop(TaskSchedulerType.TYPE_STOP_NOT_NEXT); - } - } - ThreadTaskManager.getInstance().removeAllThreadTask(); - mCachePool.clear(); - } - - /** - * 获取配置文件旧的最大任务数 - */ - public abstract int getOldMaxNum(); - - /** - * 获取任务执行池 - */ - public BaseExecutePool getExecutePool() { - return mExecutePool; - } - - /** - * 获取缓存池 - */ - public BaseCachePool getCachePool() { - return mCachePool; - } - - /** - * 获取缓存任务数 - * - * @return 获取缓存的任务数 - */ - @Override public int getCurrentCachePoolNum() { - return mCachePool.size(); - } - - /** - * 获取执行池中的任务数量 - * - * @return 当前正在执行的任务数 - */ - @Override public int getCurrentExePoolNum() { - return mExecutePool.size(); - } - - @Override public void setMaxTaskNum(int maxNum) { - int oldMaxSize = getOldMaxSize(); - int diff = maxNum - oldMaxSize; - if (oldMaxSize == maxNum) { - ALog.w(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过"); - return; - } - //设置的任务数小于配置任务数 - if (diff <= -1 && mExecutePool.size() >= oldMaxSize) { - for (int i = 0, len = Math.abs(diff); i < len; i++) { - TASK eTask = mExecutePool.pollTask(); - if (eTask != null) { - stopTask(eTask); - } - } - } - mExecutePool.setPoolSize(maxNum); - if (diff >= 1) { - for (int i = 0; i < diff; i++) { - TASK nextTask = getNextTask(); - if (nextTask != null && nextTask.getTaskState() == IEntity.STATE_WAIT) { - startTask(nextTask); - } - } - } - } - - @Override public TASK createTask(TASK_WRAPPER wrapper) { - TaskWrapperManager.getInstance().putTaskWrapper(wrapper); - return null; - } - - @Override public boolean taskExists(String key) { - return getTask(key) != null; - } - - @Override public TASK getTask(String key) { - TASK task = mExecutePool.getTask(key); - if (task == null) { - task = mCachePool.getTask(key); - } - ALog.i(TAG, "获取任务,key:" + key); - return task; - } - - /** - * 添加等待中的任务 - * - * @param task {@link DownloadTask}、{@link UploadTask}、{@link DownloadGroupTask} - */ - void addTask(TASK task) { - if (task == null) { - ALog.w(TAG, "add task fail, task is null"); - return; - } - if (!mCachePool.taskExits(task.getKey())) { - mCachePool.putTask(task); - } - } - - @Override public void startTask(TASK task) { - startTask(task, TaskSchedulerType.TYPE_DEFAULT); - } - - @Override public void startTask(TASK task, int action) { - if (task == null) { - ALog.w(TAG, "create fail, task is null"); - } - if (mExecutePool.taskExist(task.getKey())) { - ALog.w(TAG, String.format("任务【%s】执行中", task.getKey())); - return; - } - ALog.i(TAG, "添加任务,key:" + task.getKey()); - mCachePool.removeTask(task); - mExecutePool.putTask(task); - task.getTaskWrapper().getEntity().setFailNum(0); - task.start(action); - } - - @Override public void stopTask(TASK task) { - if (task == null) { - ALog.w(TAG, "stop fail, task is null"); - return; - } - int state = task.getTaskState(); - boolean canStop = false; - switch (state) { - case IEntity.STATE_WAIT: - mCachePool.removeTask(task); - canStop = true; - break; - case IEntity.STATE_POST_PRE: - case IEntity.STATE_PRE: - case IEntity.STATE_RUNNING: - mExecutePool.removeTask(task); - canStop = true; - break; - case IEntity.STATE_STOP: - case IEntity.STATE_OTHER: - case IEntity.STATE_FAIL: - ALog.w(TAG, String.format("停止任务【%s】失败,原因:已停止", task.getTaskName())); - if (taskIsRunning(task.getKey())) { - removeTaskFormQueue(task.getKey()); - if (ThreadTaskManager.getInstance().taskIsRunning(task.getKey())) { - ThreadTaskManager.getInstance().removeTaskThread(task.getKey()); - } - } - break; - case IEntity.STATE_CANCEL: - ALog.w(TAG, String.format("停止任务【%s】失败,原因:任务已删除", task.getTaskName())); - break; - case IEntity.STATE_COMPLETE: - ALog.w(TAG, String.format("停止任务【%s】失败,原因:已完成", task.getTaskName())); - break; - } - - if (canStop) { - task.stop(); - } - } - - @Override public void removeTaskFormQueue(String key) { - TASK task = mExecutePool.getTask(key); - if (task != null) { - ALog.d(TAG, String.format("从执行池删除任务【%s】%s", task.getTaskName(), - (mExecutePool.removeTask(task) ? "成功" : "失败"))); - } - task = mCachePool.getTask(key); - if (task != null) { - ALog.d(TAG, String.format("从缓存池删除任务【%s】%s", task.getTaskName(), - (mCachePool.removeTask(task) ? "成功" : "失败"))); - } - } - - @Override public void reTryStart(TASK task) { - if (task == null) { - ALog.e(TAG, "reTry fail, task is null"); - return; - } - - int state = task.getTaskState(); - switch (state) { - case IEntity.STATE_POST_PRE: - case IEntity.STATE_PRE: - case IEntity.STATE_RUNNING: - ALog.w(TAG, String.format("任务【%s】没有停止,即将重新下载", task.getTaskName())); - task.stop(TaskSchedulerType.TYPE_STOP_NOT_NEXT); - task.start(); - break; - case IEntity.STATE_WAIT: - case IEntity.STATE_STOP: - case IEntity.STATE_OTHER: - case IEntity.STATE_FAIL: - task.start(); - break; - case IEntity.STATE_CANCEL: - ALog.e(TAG, String.format("任务【%s】重试失败,原因:任务已删除", task.getTaskName())); - break; - case IEntity.STATE_COMPLETE: - ALog.e(TAG, String.format("任务【%s】重试失败,原因:已完成", task.getTaskName())); - break; - } - } - - @Override public void cancelTask(TASK task) { - cancelTask(task, TaskSchedulerType.TYPE_DEFAULT); - } - - @Override public void cancelTask(TASK task, int action) { - task.cancel(action); - } - - @Override public TASK getNextTask() { - return mCachePool.pollTask(); - } -} diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt index 285038c3..71e19959 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt @@ -18,14 +18,15 @@ package com.arialyy.aria.http.download import com.arialyy.aria.core.DuaContext import com.arialyy.aria.core.inf.IBlockManager import com.arialyy.aria.core.inf.ITaskManager -import com.arialyy.aria.util.BlockUtil import com.arialyy.aria.core.task.ITask import com.arialyy.aria.core.task.ITaskInterceptor +import com.arialyy.aria.core.task.TaskCachePool import com.arialyy.aria.core.task.TaskChain import com.arialyy.aria.core.task.TaskResp import com.arialyy.aria.http.HttpTaskOption import com.arialyy.aria.orm.entity.BlockRecord import com.arialyy.aria.orm.entity.TaskRecord +import com.arialyy.aria.util.BlockUtil import com.arialyy.aria.util.FileUri import com.arialyy.aria.util.FileUtils import kotlinx.coroutines.Dispatchers diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDCheckInterceptor.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDCheckInterceptor.kt index ed9e4b2c..a4d0b747 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDCheckInterceptor.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDCheckInterceptor.kt @@ -22,7 +22,6 @@ import com.arialyy.aria.core.task.TaskChain import com.arialyy.aria.core.task.TaskResp import com.arialyy.aria.http.HttpTaskOption import com.arialyy.aria.orm.entity.DEntity -import timber.log.Timber /** * @Author laoyuyu @@ -34,12 +33,12 @@ class HttpDCheckInterceptor : ITaskInterceptor { /** * find DEntity, if that not exist, create and save it */ - private suspend fun findDEntityBySavePath(option: HttpTaskOption): DEntity? { + private suspend fun findDEntityBySavePath(option: HttpTaskOption): DEntity { val savePath = option.savePathUri val dao = DuaContext.getServiceManager().getDbService().getDuaDb().getDEntityDao() val de = dao.getDEntityBySavePath(savePath.toString()) if (de != null) { - return null + return de } val newDe = DEntity( sourceUrl = option.sourUrl!!, @@ -52,11 +51,8 @@ class HttpDCheckInterceptor : ITaskInterceptor { override suspend fun interceptor(chain: TaskChain): TaskResp { val option = chain.getTask().getTaskOption(HttpTaskOption::class.java) val dEntity = findDEntityBySavePath(option) - if (dEntity == null) { - Timber.e("file already exists, ${option.savePathUri}") - return TaskResp(TaskResp.CODE_INTERRUPT) - } + chain.getTask().taskState.entity = dEntity TaskCachePool.putEntity(chain.getTask().taskId, dEntity) - return TaskResp(TaskResp.CODE_SUCCESS) + return chain.proceed(chain.getTask()) } } \ No newline at end of file diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDHeaderInterceptor.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDHeaderInterceptor.kt index d02cb64e..ffef524b 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDHeaderInterceptor.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDHeaderInterceptor.kt @@ -63,13 +63,18 @@ internal class HttpDHeaderInterceptor : ITaskInterceptor { } Timber.i("step 1. get file info") task = chain.getTask() + if (task.taskState.fileSize > 0L) { + Timber.d("not required repeat get file size: ${task.taskState.fileSize}") + return chain.proceed(task) + } taskOption = task.getTaskOption(HttpTaskOption::class.java) try { val fileSize = getFileSize() if (fileSize >= 0) { task.taskState.fileSize = fileSize getOptionAdapter().isSupportResume = fileSize != 0L - getOptionAdapter().isSupportBlock = getOptionAdapter().isSupportResume && fileSize > BlockRecord.BLOCK_SIZE + getOptionAdapter().isSupportBlock = + getOptionAdapter().isSupportResume && fileSize > BlockRecord.BLOCK_SIZE return chain.proceed(task) } } catch (e: IOException) { diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStartController.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStartController.kt index bf5f1b0d..8eedfd26 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStartController.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStartController.kt @@ -18,7 +18,9 @@ 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.StartCmd +import com.arialyy.aria.core.command.StopCmd import com.arialyy.aria.core.processor.IHttpFileLenAdapter import com.arialyy.aria.core.task.DownloadTask import com.arialyy.aria.core.task.ITaskInterceptor @@ -26,6 +28,7 @@ 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 timber.log.Timber import java.net.HttpURLConnection /** @@ -77,10 +80,17 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll return this } - private fun createTask(): DownloadTask { + private fun getTask(createNewTask: Boolean = true): DownloadTask? { if (HttpUtil.checkHttpDParams(httpTaskOption)) { throw IllegalArgumentException("invalid params") } + val tempTask = TaskCachePool.getTaskByUrl(url) + if (tempTask != null) { + return tempTask as DownloadTask + } + if (!createNewTask) { + return null + } val task = DownloadTask(httpTaskOption) task.adapter = HttpDTaskAdapter() TaskCachePool.putTask(task) @@ -91,22 +101,39 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll if (!HttpUtil.checkHttpDParams(httpTaskOption)) { return -1 } - val task = createTask() + val task = getTask() val resp = AddCmd(task).executeCmd() - return if (resp.isInterrupt()) -1 else task.taskId + return if (resp.isInterrupt()) -1 else task?.taskId ?: -1 } fun start(): Int { if (!HttpUtil.checkHttpDParams(httpTaskOption)) { return -1 } - val task = createTask() + val task = getTask() val resp = StartCmd(task).executeCmd() - return if (resp.isInterrupt()) -1 else task.taskId + return if (resp.isInterrupt()) -1 else task?.taskId ?: -1 } fun resume(): Int { - TaskCachePool.findTaskByPath(url) return start() } + + fun cancel() { + val task = getTask(true) + if (task == null) { + Timber.e("not found task, url: $url") + return + } + CancelCmd(task).executeCmd() + } + + fun stop() { + val task = getTask(false) + if (task == null) { + Timber.e("task not running, url: $url") + return + } + StopCmd(task).executeCmd() + } } \ No newline at end of file diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt index 694537fa..9db5d876 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt @@ -16,8 +16,13 @@ package com.arialyy.aria.http.download import android.net.Uri +import com.arialyy.aria.core.DuaContext +import com.arialyy.aria.core.command.CancelCmd import com.arialyy.aria.core.command.StopCmd import com.arialyy.aria.core.task.TaskCachePool +import com.arialyy.aria.util.FileUtils +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import timber.log.Timber /** @@ -28,16 +33,16 @@ import timber.log.Timber class HttpDStopController(val taskId: Int) { fun cancel() { - val task = TaskCachePool.getTask(taskId) + val task = TaskCachePool.getTaskById(taskId) if (task == null) { Timber.e("task not found, taskId: $taskId") return } - HttpDStopController2(Uri.parse(task.filePath)).cancel() + CancelCmd(task).executeCmd() } fun stop() { - val task = TaskCachePool.getTask(taskId) + val task = TaskCachePool.getTaskById(taskId) if (task == null) { Timber.e("task not found, taskId: $taskId") return @@ -46,7 +51,7 @@ class HttpDStopController(val taskId: Int) { } fun resume() { - val task = TaskCachePool.getTask(taskId) + val task = TaskCachePool.getTaskById(taskId) if (task == null) { Timber.e("task not found, taskId: $taskId") return diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt deleted file mode 100644 index 24920f35..00000000 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt +++ /dev/null @@ -1,83 +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.http.download - -import android.net.Uri -import com.arialyy.aria.core.DuaContext -import com.arialyy.aria.core.command.CancelCmd -import com.arialyy.aria.core.command.StopCmd -import com.arialyy.aria.core.common.TaskOption -import com.arialyy.aria.core.task.DownloadTask -import com.arialyy.aria.core.task.TaskCachePool -import com.arialyy.aria.util.FileUtils -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import timber.log.Timber - -/** - * @Author laoyuyu - * @Description - * @Date 20:25 2023/2/19 - **/ -class HttpDStopController2(val filePath: Uri) { - - private suspend fun checkUri(): Boolean { - if (!FileUtils.uriEffective(filePath)) { - Timber.e("invalid uri: $filePath") - return false - } - return withContext(Dispatchers.IO) { - val entity = DuaContext.getServiceManager().getDbService().getDuaDb().getDEntityDao() - .getDEntityBySavePath(filePath.toString()) - if (entity == null) { - Timber.e("No found task, uri: $filePath") - return@withContext false - } - return@withContext true - } - } - - fun cancel() { - DuaContext.duaScope.launch { - if (checkUri()) { - var task = TaskCachePool.findTaskByPath(filePath) - if (task == null) { - Timber.i("task not exist, create new task") - task = DownloadTask(TaskOption()) - task.adapter = HttpDTaskAdapter() - } - TaskCachePool.putTask(task) - CancelCmd(task).executeCmd() - } - } - } - - fun stop() { - DuaContext.duaScope.launch { - if (checkUri()) { - TaskCachePool.findTaskByPath(filePath)?.let { - StopCmd(it).executeCmd() - return@launch - } - - Timber.e("not found task, uri: $filePath") - return@launch - } - Timber.e("stop fail, invalid uri: $filePath") - } - } -} \ No newline at end of file diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDownloader.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDownloader.kt index bb39fea9..1f7b83b4 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDownloader.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDownloader.kt @@ -15,7 +15,6 @@ */ package com.arialyy.aria.http.download -import android.net.Uri import com.arialyy.annotations.TaskEnum import com.arialyy.annotations.TaskEnum.DOWNLOAD import com.arialyy.aria.core.inf.IDownloader @@ -47,12 +46,4 @@ class HttpDownloader(val target: Any) : IDownloader { return HttpDStopController(taskId) } - /** - * stop, cancel a task - * @param filePath filePath - */ - fun load(filePath: Uri): HttpDStopController2 { - return HttpDStopController2(filePath) - } - } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/StartCmd.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/StartCmd.java index dff0d5f9..9c9f7322 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/StartCmd.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/command/StartCmd.java @@ -34,6 +34,10 @@ final public class StartCmd extends AbsCmd { Timber.w("interruption occurred, cancel cmd"); return resp; } + if (mTask.isRunning()) { + Timber.w("task is running"); + return resp; + } getTaskQueue().startTask(mTask); return resp; } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/BaseEntity.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/BaseEntity.kt index 765d23a1..bd327c66 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/BaseEntity.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/BaseEntity.kt @@ -15,7 +15,6 @@ */ package com.arialyy.aria.core.inf - /** * @Author laoyuyu * @Description @@ -40,4 +39,6 @@ abstract class BaseEntity : IEntity { * extended Information */ var ext: String? = null + + val fileSize: Long = 0 } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java index bc71ce16..c0ebf5c1 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java @@ -16,7 +16,6 @@ package com.arialyy.aria.core.task; -import android.net.Uri; import com.arialyy.aria.core.common.TaskOption; import java.util.Objects; @@ -30,14 +29,6 @@ public class DownloadTask extends AbsTask { super(taskOption); } - public Uri getSavePath() { - return getTaskOption(TaskOption.class).getSavePathUri(); - } - - public String getSourceUrl() { - return getTaskOption(TaskOption.class).getSourUrl(); - } - /** * 获取当前下载任务的下载地址 */ @@ -45,6 +36,10 @@ public class DownloadTask extends AbsTask { return ITask.DOWNLOAD; } + @Override public String getUrl() { + return getTaskOption(TaskOption.class).getSourUrl(); + } + @Override public String getFilePath() { return Objects.requireNonNull(getTaskOption(TaskOption.class).getSavePathUri()).toString(); } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java index a1f38af1..9d7587ed 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java @@ -119,6 +119,8 @@ public interface ITask { int getTaskId(); + String getUrl(); + /** * @return task unique identifier, like: savePath */ diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskCachePool.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskCachePool.kt index b7f83a62..cfdcdc07 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskCachePool.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskCachePool.kt @@ -33,29 +33,28 @@ object TaskCachePool { */ private val entityMap = ConcurrentHashMap() private val taskAdapterMap = ConcurrentHashMap() - private val taskMap = ConcurrentHashMap() - fun removeTask(taskId: Int) { - taskMap.remove(taskId) + /** + * task cache map; key: url + */ + private val taskMap = ConcurrentHashMap() + + fun removeTask(url: String) { + taskMap.remove(url) } /** * if task is completed, stopped, canceled, return null */ - fun getTask(taskId: Int) = taskMap[taskId] + fun getTaskByUrl(url: String) = taskMap[url] - fun putTask(task: ITask) { - taskMap[task.taskId] = task - taskAdapterMap[Uri.parse(task.filePath)] = task.adapter + fun getTaskById(taskId: Int): ITask? { + return taskMap.values.find { it.taskId == taskId } } - /** - * find task by url - */ - fun findTaskByUrl(url:String):ITask?{ - taskMap.values.forEach { - if (it.getTaskOption().) - } + fun putTask(task: ITask) { + taskMap[task.url] = task + taskAdapterMap[Uri.parse(task.filePath)] = task.adapter } /** diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskState.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskState.kt index c53ccc9e..ca314fb9 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskState.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskState.kt @@ -16,6 +16,7 @@ package com.arialyy.aria.core.task import com.arialyy.aria.core.config.Configuration +import com.arialyy.aria.core.inf.BaseEntity import com.arialyy.aria.core.inf.IEntity import com.arialyy.aria.orm.entity.BlockRecord import com.arialyy.aria.orm.entity.TaskRecord @@ -30,6 +31,12 @@ class TaskState { lateinit var taskRecord: TaskRecord + var entity: BaseEntity? = null + set(value) { + fileSize = value?.fileSize ?: 0 + field = value + } + /** * need to try again?,default: false */ diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DEntity.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DEntity.kt index 6a6581b5..8b0b51e7 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DEntity.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DEntity.kt @@ -47,9 +47,7 @@ data class DEntity( */ val savePath: Uri, - val isSub: Boolean = false, - - val fileSize: Long = 0, + val isSub: Boolean = false ) : BaseEntity() { override fun update() {