From e6867c717390908033ec233e1618dccd58af7798 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 3 Feb 2023 22:24:56 +0800 Subject: [PATCH] BlockManager.kt update --- .../http/download/HttpDBlockInterceptor.kt | 64 +++++++++++++++++++ .../http/download/HttpDHeaderInterceptor.kt | 2 +- .../arialyy/aria/core/task/BlockManager.kt | 23 +------ .../arialyy/aria/core/task/DownloadTask.java | 5 ++ .../com/arialyy/aria/core/task/ITask.java | 2 + .../aria/core/task/ITaskInterceptor.kt | 2 +- .../com/arialyy/aria/core/task/MergeUtil.kt | 27 ++++++++ .../main/java/com/arialyy/aria/orm/DuaDb.kt | 3 + .../com/arialyy/aria/orm/dao/RecordDao.kt | 2 +- 9 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/task/MergeUtil.kt 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 new file mode 100644 index 00000000..1ecb2658 --- /dev/null +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt @@ -0,0 +1,64 @@ +/* + * 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 com.arialyy.aria.core.DuaContext +import com.arialyy.aria.core.task.BlockManager +import com.arialyy.aria.core.task.ITask +import com.arialyy.aria.core.task.ITaskInterceptor +import com.arialyy.aria.core.task.TaskChain +import com.arialyy.aria.core.task.TaskResp +import timber.log.Timber + +/** + * block interceptor + * 1. create block record + * 2. check downloaded block + */ +internal class HttpDBlockInterceptor : ITaskInterceptor { + private lateinit var task: ITask + + override suspend fun interceptor(chain: TaskChain): TaskResp { + task = chain.getTask() + if (task.taskState.fileSize < 0) { + Timber.e("file size < 0") + return TaskResp(TaskResp.CODE_GET_FILE_INFO_FAIL) + } + + } + + /** + * check task record, if record no exist, create taskRecord + */ + private suspend fun checkRecord() { + val recordWrapper = DuaContext.getServiceManager().getDbService().getDuaDb()?.getRecordDao() + ?.getTaskRecordByKey(task.taskKey) + if (recordWrapper == null){ + + } + } + + private fun createBlockManager(): BlockManager { + + } + + /** + * if block already exist, upload progress + */ + private fun checkBlock() { + + } +} \ 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 7ec03b1e..7b06a4c6 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 @@ -51,7 +51,7 @@ internal class HttpDHeaderInterceptor : ITaskInterceptor { ) } - override fun interceptor(chain: TaskChain): TaskResp { + override suspend fun interceptor(chain: TaskChain): TaskResp { if (Looper.myLooper() == Looper.getMainLooper()) { throw IllegalThreadStateException("io operations cannot be in the main thread") } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/BlockManager.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/BlockManager.kt index 39b5a70a..aaa732dc 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/BlockManager.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/BlockManager.kt @@ -26,8 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger class BlockManager( val mListener: IEventListener, val looper: Looper, - private val blockNum: Int, - val blockParentDir: String + private val blockNum: Int ) : IThreadStateManager { private val blockList = mutableListOf() private val canceledNum = AtomicInteger(0) // 已经取消的线程的数 @@ -40,7 +39,6 @@ class BlockManager( private var progress: Long = 0 //当前总进度 private val callback = Callback { msg -> - checkLooper() when (msg.what) { IThreadStateManager.STATE_STOP -> { stoppedNum.getAndIncrement() @@ -69,15 +67,6 @@ class BlockManager( completedNum.getAndIncrement() if (isCompleted) { Timber.d("isComplete, completeNum = %s", completedNum) - if (mTaskRecord.isBlock || mTaskRecord.threadNum === 1) { - if (mergeFile()) { - mListener.onComplete() - } else { - mListener.onFail(false, null) - } - quitLooper() - break - } mListener.onComplete() quitLooper() } @@ -89,9 +78,7 @@ class BlockManager( progress += len } } - IThreadStateManager.STATE_UPDATE_PROGRESS -> if (msg.obj == null) { - progress = updateBlockProgress() - } else if (msg.obj is Long) { + IThreadStateManager.STATE_UPDATE_PROGRESS -> { progress = msg.obj as Long } } @@ -148,10 +135,4 @@ class BlockManager( return callback } - private fun mergeFile(): Boolean { - if (blockNum == 1){ - - return true - } - } } \ 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 9e714558..bd1acfea 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 @@ -19,6 +19,7 @@ package com.arialyy.aria.core.task; import android.net.Uri; import com.arialyy.aria.core.download.DTaskOption; import com.arialyy.aria.core.inf.ITaskUtil; +import java.util.Objects; /** * Created by lyy on 2016/8/11. @@ -45,4 +46,8 @@ public class DownloadTask extends AbsTask { @Override public int getTaskType() { return ITask.DOWNLOAD; } + + @Override public String getTaskKey() { + return Objects.requireNonNull(getTaskOption(DTaskOption.class).getSavePathUri()).toString(); + } } \ No newline at end of file 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 383e789e..e03847c5 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 @@ -116,5 +116,7 @@ public interface ITask { int getTaskId(); + String getTaskKey(); + T getTaskOption(Class clazz); } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITaskInterceptor.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITaskInterceptor.kt index 2d3bbc27..01d842aa 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITaskInterceptor.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITaskInterceptor.kt @@ -22,7 +22,7 @@ package com.arialyy.aria.core.task **/ interface ITaskInterceptor { - fun interceptor(chain: TaskChain): TaskResp + suspend fun interceptor(chain: TaskChain): TaskResp interface IChain { fun getTask(): ITask diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/MergeUtil.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/MergeUtil.kt new file mode 100644 index 00000000..6cbb4971 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/MergeUtil.kt @@ -0,0 +1,27 @@ +package com.arialyy.aria.core.task + +/* + * 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. + */ +object MergeUtil { + + /** + * merge block file,if success,return true else return false + */ + fun merge(): Boolean { + + return false + } +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt index 1647dc11..6c576c3b 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt @@ -20,6 +20,7 @@ import androidx.room.RoomDatabase import com.arialyy.aria.orm.dao.DEntityDao import com.arialyy.aria.orm.dao.DGEntityDao import com.arialyy.aria.orm.dao.MEntityDao +import com.arialyy.aria.orm.dao.RecordDao import com.arialyy.aria.orm.dao.UEntityDao import com.arialyy.aria.orm.entity.BlockRecord import com.arialyy.aria.orm.entity.DGEntity @@ -40,4 +41,6 @@ abstract class DuaDb : RoomDatabase() { abstract fun getDGEntityDao(): DGEntityDao abstract fun getMEntityDao(): MEntityDao + + abstract fun getRecordDao(): RecordDao } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/RecordDao.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/RecordDao.kt index 307992fa..1c5e0cdf 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/RecordDao.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/RecordDao.kt @@ -35,7 +35,7 @@ interface RecordDao { @Transaction @Query("SELECT * FROM TaskRecord WHERE :key=taskKey") - suspend fun getTaskRecordByKey(key: String): RecordRelation + suspend fun getTaskRecordByKey(key: String): RecordRelation? @Insert @Deprecated(