From 1e98b835de6dd447079dc6045517d4054fa918a1 Mon Sep 17 00:00:00 2001 From: laoyuyu Date: Thu, 9 Mar 2023 19:55:53 +0800 Subject: [PATCH] Code optimization --- .../http/download/HttpDStartController.kt | 11 ++-- .../aria/http/download/HttpDTaskAdapter.kt | 25 ++++++++ .../arialyy/dua/group/HttpDGOptionAdapter.kt | 3 +- .../dua/group/HttpDGStartController.kt | 21 ++++++- .../dua/group/HttpDGSubTaskInterceptor.kt | 57 ++++++++++++++++++- .../arialyy/dua/group/HttpDGTaskManager.kt | 2 +- .../com/arialyy/aria/core/inf/BaseEntity.kt | 6 ++ .../arialyy/aria/core/inf/IStartController.kt | 43 -------------- .../arialyy/aria/core/inf/ITaskAdapter.java | 2 + .../arialyy/aria/core/task/TaskCachePool.kt | 9 +++ .../com/arialyy/aria/orm/dao/DGEntityDao.kt | 12 +++- .../com/arialyy/aria/orm/entity/DEntity.kt | 48 ++-------------- .../com/arialyy/aria/orm/entity/DGEntity.kt | 15 ++--- androidComponent.gradle | 1 + 14 files changed, 146 insertions(+), 109 deletions(-) delete mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/inf/IStartController.kt 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 ca95e51b..bf5f1b0d 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 @@ -19,7 +19,6 @@ import android.net.Uri import com.arialyy.aria.core.DuaContext import com.arialyy.aria.core.command.AddCmd import com.arialyy.aria.core.command.StartCmd -import com.arialyy.aria.core.inf.IStartController import com.arialyy.aria.core.processor.IHttpFileLenAdapter import com.arialyy.aria.core.task.DownloadTask import com.arialyy.aria.core.task.ITaskInterceptor @@ -34,8 +33,7 @@ import java.net.HttpURLConnection * @Description * @Date 12:38 PM 2023/1/22 **/ -class HttpDStartController(target: Any, val url: String) : HttpBaseStartController(target), - IStartController { +class HttpDStartController(target: Any, val url: String) : HttpBaseStartController(target) { private val taskOptionSupport = HttpDOptionAdapter() init { @@ -89,7 +87,7 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll return task } - override fun add(): Int { + fun add(): Int { if (!HttpUtil.checkHttpDParams(httpTaskOption)) { return -1 } @@ -98,7 +96,7 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll return if (resp.isInterrupt()) -1 else task.taskId } - override fun start(): Int { + fun start(): Int { if (!HttpUtil.checkHttpDParams(httpTaskOption)) { return -1 } @@ -107,7 +105,8 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll return if (resp.isInterrupt()) -1 else task.taskId } - override fun resume(): Int { + fun resume(): Int { + TaskCachePool.findTaskByPath(url) return start() } } \ No newline at end of file diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt index 0097745b..d42dfdb9 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt @@ -99,4 +99,29 @@ internal class HttpDTaskAdapter : AbsTaskAdapter() { Looper.loop() } } + + override fun resume() { + getTask().getTaskOption(HttpTaskOption::class.java).taskInterceptor.let { + if (it.isNotEmpty()) { + addInterceptors(it) + } + } + DuaContext.duaScope.launch(Dispatchers.IO) { + Looper.prepare() + blockManager?.setLooper() + addCoreInterceptor(TimerInterceptor()) + addCoreInterceptor(HttpDBlockInterceptor()) + addCoreInterceptor(HttpBlockThreadInterceptor()) + val resp = interceptor() + if (resp == null || resp.code != TaskResp.CODE_SUCCESS) { + getTask().getTaskOption(HttpTaskOption::class.java).eventListener.onFail( + false, + AriaException("start task fail, task interrupt, code: ${resp?.code ?: TaskResp.CODE_INTERRUPT}") + ) + blockManager?.stop() + return@launch + } + Looper.loop() + } + } } \ No newline at end of file diff --git a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGOptionAdapter.kt b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGOptionAdapter.kt index 0d508c3e..5301de16 100644 --- a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGOptionAdapter.kt +++ b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGOptionAdapter.kt @@ -23,5 +23,6 @@ import com.arialyy.aria.http.IHttpTaskOptionAdapter * @Date 8:14 PM 2023/3/6 **/ internal class HttpDGOptionAdapter : IHttpTaskOptionAdapter { - val subUrl = mutableSetOf() + val subUrlList = mutableSetOf() + val subNameList = mutableListOf() } \ No newline at end of file diff --git a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGStartController.kt b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGStartController.kt index 8a9cab4e..50b04120 100644 --- a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGStartController.kt +++ b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGStartController.kt @@ -22,8 +22,10 @@ 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.util.FileUri import com.arialyy.aria.util.FileUtils import timber.log.Timber +import java.io.File /** * @Author laoyuyu @@ -55,7 +57,15 @@ class HttpDGStartController(target: Any, val savePath: Uri) : HttpBaseStartContr * add sub task download uri */ fun addSubUriResource(subUrlList: List): HttpDGStartController { - optionAdapter.subUrl.addAll(subUrlList) + optionAdapter.subUrlList.addAll(subUrlList) + return this + } + + /** + * map sub task name, [subTaskNameList].size must be consistent [addSubUriResource].size + */ + fun addSubTaskName(subTaskNameList: List): HttpDGStartController { + optionAdapter.subNameList.addAll(subTaskNameList) return this } @@ -78,6 +88,15 @@ class HttpDGStartController(target: Any, val savePath: Uri) : HttpBaseStartContr Timber.e("invalid savePath: $savePath") return -1 } + val dir = File(FileUri.getPathByUri(savePath)!!) + if (dir.exists()) { + Timber.e("invalid savePath, the path existed: $savePath") + return -1 + } + if (optionAdapter.subNameList.isNotEmpty() && optionAdapter.subNameList.size != optionAdapter.subUrlList.size) { + Timber.e("subNameList.size must be consistent subUrlList.size") + return -1 + } return createTask().taskId } } \ No newline at end of file diff --git a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGSubTaskInterceptor.kt b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGSubTaskInterceptor.kt index 2c7408d2..5143e2fb 100644 --- a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGSubTaskInterceptor.kt +++ b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGSubTaskInterceptor.kt @@ -15,10 +15,18 @@ */ package com.arialyy.dua.group +import android.net.Uri import com.arialyy.aria.core.DuaContext +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 com.arialyy.aria.http.HttpTaskOption +import com.arialyy.aria.orm.entity.DEntity +import com.arialyy.aria.orm.entity.DGEntity +import com.arialyy.aria.util.FileUri +import timber.log.Timber +import java.io.File /** * @Author laoyuyu @@ -31,10 +39,55 @@ internal class HttpDGSubTaskInterceptor : ITaskInterceptor { DuaContext.getServiceManager().getDbService().getDuaDb().getDGEntityDao() } + private lateinit var task: ITask + private lateinit var option: HttpTaskOption + private lateinit var dgOption: HttpDGOptionAdapter + override suspend fun interceptor(chain: TaskChain): TaskResp { + task = chain.getTask() + option = task.getTaskOption(HttpTaskOption::class.java) + dgOption = option.getOptionAdapter(HttpDGOptionAdapter::class.java) + if (!checkRecord()) { + return TaskResp(TaskResp.CODE_INTERRUPT) + } + + } - private suspend fun checkRecord() { - // dgDao. + /** + * check dg task record, if dfEntiry exist, return false + * otherwise, save new record + */ + private suspend fun checkRecord(): Boolean { + val entity = dgDao.getDGEntityByPath(option.savePathUri.toString()) + if (entity != null) { + Timber.e("task already exist, filePath: ${option.savePathUri}") + return false + } + // create sub task record + val dgEntity = DGEntity( + savePath = option.savePathUri!!, + urls = dgOption.subUrlList.toList(), + subNameList = dgOption.subNameList + ) + + val subTask = mutableListOf() + val dir = File(FileUri.getPathByUri(option.savePathUri)!!) + dgOption.subUrlList.forEachIndexed { index, it -> + val subFile = File( + dir.path, + if (dgOption.subNameList.isNotEmpty()) dgOption.subNameList[index] else "dgTask${index}" + ) + subTask.add( + DEntity( + sourceUrl = it, + savePath = Uri.parse(subFile.toString()), + ) + ) + } + dgEntity.subList.addAll(subTask) + + dgDao.insert(dgEntity) + return true } } \ No newline at end of file diff --git a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGTaskManager.kt b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGTaskManager.kt index 6d6afbe6..e0b66266 100644 --- a/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGTaskManager.kt +++ b/HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGTaskManager.kt @@ -48,7 +48,7 @@ internal class HttpDGTaskManager : ITaskManager { } fun start(taskOption: HttpTaskOption) { - taskOption.getOptionAdapter(HttpDGOptionAdapter::class.java).subUrl + taskOption.getOptionAdapter(HttpDGOptionAdapter::class.java).subUrlList } override fun setLooper() { 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 0df9cdbc..765d23a1 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,6 +15,7 @@ */ package com.arialyy.aria.core.inf + /** * @Author laoyuyu * @Description @@ -34,4 +35,9 @@ abstract class BaseEntity : IEntity { * current progress */ var progress: Long = 0L + + /** + * extended Information + */ + var ext: String? = null } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IStartController.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IStartController.kt deleted file mode 100644 index bfa51533..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IStartController.kt +++ /dev/null @@ -1,43 +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.inf - -/** - * @Author laoyuyu - * @Description - * @Date 12:32 PM 2023/1/22 - **/ -interface IStartController { - /** - * 添加任务 - * - * @return 正常添加,返回任务id,否则返回-1 - */ - fun add(): Int - - /** - * 创建并开始任务 - * - * @return 正常启动,返回任务id,否则返回-1 - */ - fun start(): Int - - /** - * 恢复任务 - * @return 正常启动,返回任务id,否则返回-1 - */ - fun resume(): Int -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java index 4aac52f5..a8a840ec 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java @@ -49,4 +49,6 @@ public interface ITaskAdapter { * 开始 */ void start(); + + void resume(); } \ No newline at end of file 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 c4ed8e54..b7f83a62 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 @@ -49,6 +49,15 @@ object TaskCachePool { taskAdapterMap[Uri.parse(task.filePath)] = task.adapter } + /** + * find task by url + */ + fun findTaskByUrl(url:String):ITask?{ + taskMap.values.forEach { + if (it.getTaskOption().) + } + } + /** * find task by filePath */ diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DGEntityDao.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DGEntityDao.kt index fca84f55..b727f998 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DGEntityDao.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DGEntityDao.kt @@ -37,6 +37,10 @@ interface DGEntityDao { @Query("SELECT * FROM DGEntity") suspend fun getDGEntityList(): List + @Transaction + @Query("SELECT * FROM DGEntity WHERE :path=savePath") + suspend fun getDGEntityByPath(path: String): DGSubRelation? + @Transaction @Query("SELECT * FROM DGEntity WHERE :gid=gid") suspend fun getDGEntityByGid(gid: Int): DGSubRelation @@ -66,7 +70,7 @@ interface DGEntityDao { "please use ", ReplaceWith("insert(dgEntity)", "com.arialyy.aria.orm.dao.DGEntityDao.insert") ) - suspend fun insertDg(dgEntity: DGEntity) + suspend fun insertDg(dgEntity: DGEntity): Int @Transaction suspend fun delete(dgEntity: DGEntity) { @@ -74,8 +78,12 @@ interface DGEntityDao { deleteDg(dgEntity) } + @Transaction suspend fun insert(dgEntity: DGEntity) { - insertDg(dgEntity) + val gid = insertDg(dgEntity) + dgEntity.subList.forEach { + it.parentId = gid + } insertSubList(dgEntity.subList) } } \ No newline at end of file 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 c2e6a47c..6a6581b5 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 @@ -16,8 +16,6 @@ package com.arialyy.aria.orm.entity import android.net.Uri -import android.os.Parcel -import android.os.Parcelable.Creator import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey @@ -26,16 +24,18 @@ import com.arialyy.aria.core.DuaContext import com.arialyy.aria.core.inf.BaseEntity import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.parcelize.Parcelize /** * Download Entity */ @Entity(indices = [Index(value = ["sourceUrl", "savePath"])]) @TypeConverters(FilePathConverter::class) +@Parcelize data class DEntity( @PrimaryKey(autoGenerate = true) val did: Int = 0, - val parentId: Int = -1, + var parentId: Int = -1, /** * file source url @@ -47,26 +47,10 @@ data class DEntity( */ val savePath: Uri, - /** - * extended Information - */ - var ext: String? = null, - val isSub: Boolean = false, - val fileSize: Long = 0 - + val fileSize: Long = 0, ) : BaseEntity() { - constructor(parcel: Parcel) : this( - parcel.readInt(), - parcel.readInt(), - parcel.readString()!!, - parcel.readParcelable(Uri::class.java.classLoader)!!, - parcel.readString(), - parcel.readByte() != 0.toByte(), - parcel.readLong() - ) { - } override fun update() { updateTime = System.currentTimeMillis() @@ -75,28 +59,4 @@ data class DEntity( .update(this@DEntity) } } - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeInt(did) - parcel.writeInt(parentId) - parcel.writeString(sourceUrl) - parcel.writeParcelable(savePath, flags) - parcel.writeString(ext) - parcel.writeByte(if (isSub) 1 else 0) - parcel.writeLong(fileSize) - } - - override fun describeContents(): Int { - return 0 - } - - companion object CREATOR : Creator { - override fun createFromParcel(parcel: Parcel): DEntity { - return DEntity(parcel) - } - - override fun newArray(size: Int): Array { - return arrayOfNulls(size) - } - } } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGEntity.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGEntity.kt index 8a901123..fe1826b6 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGEntity.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGEntity.kt @@ -21,6 +21,8 @@ import androidx.room.Ignore import androidx.room.Index import androidx.room.PrimaryKey import androidx.room.TypeConverters +import com.arialyy.aria.core.inf.BaseEntity +import kotlinx.parcelize.Parcelize /** * @Author laoyuyu @@ -29,6 +31,7 @@ import androidx.room.TypeConverters **/ @Entity(indices = [Index(value = ["savePath"])]) @TypeConverters(DGUrlConverter::class, FilePathConverter::class) +@Parcelize data class DGEntity( @PrimaryKey(autoGenerate = true) val dgId: Int = 0, @@ -47,15 +50,9 @@ data class DGEntity( */ val urls: List, - /** - * extended Information - */ - var ext: String? = null, - - val createTime: Long, + val subNameList: List? - val updateTime: Long -) { +) : BaseEntity() { @Ignore - internal var subList: MutableList = mutableListOf() + var subList: MutableList = mutableListOf() } \ No newline at end of file diff --git a/androidComponent.gradle b/androidComponent.gradle index 1bbc2373..e979aed3 100644 --- a/androidComponent.gradle +++ b/androidComponent.gradle @@ -2,6 +2,7 @@ apply { plugin "com.android.library" plugin "org.jetbrains.kotlin.android" plugin "kotlin-kapt" + plugin "kotlin-parcelize" } android {