Code optimization

v4
laoyuyu 2 years ago
parent 61d8989afd
commit 1e98b835de
  1. 11
      Http/src/main/java/com/arialyy/aria/http/download/HttpDStartController.kt
  2. 25
      Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt
  3. 3
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGOptionAdapter.kt
  4. 21
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGStartController.kt
  5. 57
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGSubTaskInterceptor.kt
  6. 2
      HttpGroup/src/main/java/com/arialyy/dua/group/HttpDGTaskManager.kt
  7. 6
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/BaseEntity.kt
  8. 43
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IStartController.kt
  9. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java
  10. 9
      PublicComponent/src/main/java/com/arialyy/aria/core/task/TaskCachePool.kt
  11. 12
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DGEntityDao.kt
  12. 48
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DEntity.kt
  13. 15
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGEntity.kt
  14. 1
      androidComponent.gradle

@ -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()
}
}

@ -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()
}
}
}

@ -23,5 +23,6 @@ import com.arialyy.aria.http.IHttpTaskOptionAdapter
* @Date 8:14 PM 2023/3/6
**/
internal class HttpDGOptionAdapter : IHttpTaskOptionAdapter {
val subUrl = mutableSetOf<String>()
val subUrlList = mutableSetOf<String>()
val subNameList = mutableListOf<String>()
}

@ -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<String>): 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<String>): 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
}
}

@ -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)
}
}
/**
* 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<DEntity>()
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)
private suspend fun checkRecord() {
// dgDao.
dgDao.insert(dgEntity)
return true
}
}

@ -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() {

@ -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
}

@ -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
}

@ -49,4 +49,6 @@ public interface ITaskAdapter {
* 开始
*/
void start();
void resume();
}

@ -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
*/

@ -37,6 +37,10 @@ interface DGEntityDao {
@Query("SELECT * FROM DGEntity")
suspend fun getDGEntityList(): List<DGSubRelation>
@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)
}
}

@ -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<DEntity> {
override fun createFromParcel(parcel: Parcel): DEntity {
return DEntity(parcel)
}
override fun newArray(size: Int): Array<DEntity?> {
return arrayOfNulls(size)
}
}
}

@ -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<String>,
/**
* extended Information
*/
var ext: String? = null,
val createTime: Long,
val subNameList: List<String>?
val updateTime: Long
) {
) : BaseEntity() {
@Ignore
internal var subList: MutableList<DEntity> = mutableListOf()
var subList: MutableList<DEntity> = mutableListOf()
}

@ -2,6 +2,7 @@ apply {
plugin "com.android.library"
plugin "org.jetbrains.kotlin.android"
plugin "kotlin-kapt"
plugin "kotlin-parcelize"
}
android {

Loading…
Cancel
Save