BlockManager.kt update

v4
laoyuyu 2 years ago
parent 3cc9b09055
commit e6867c7173
  1. 64
      Http/src/main/java/com/arialyy/aria/http/download/HttpDBlockInterceptor.kt
  2. 2
      Http/src/main/java/com/arialyy/aria/http/download/HttpDHeaderInterceptor.kt
  3. 23
      PublicComponent/src/main/java/com/arialyy/aria/core/task/BlockManager.kt
  4. 5
      PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java
  5. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java
  6. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/task/ITaskInterceptor.kt
  7. 27
      PublicComponent/src/main/java/com/arialyy/aria/core/task/MergeUtil.kt
  8. 3
      PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt
  9. 2
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/RecordDao.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() {
}
}

@ -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()) { if (Looper.myLooper() == Looper.getMainLooper()) {
throw IllegalThreadStateException("io operations cannot be in the main thread") throw IllegalThreadStateException("io operations cannot be in the main thread")
} }

@ -26,8 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger
class BlockManager( class BlockManager(
val mListener: IEventListener, val mListener: IEventListener,
val looper: Looper, val looper: Looper,
private val blockNum: Int, private val blockNum: Int
val blockParentDir: String
) : IThreadStateManager { ) : IThreadStateManager {
private val blockList = mutableListOf<BlockState>() private val blockList = mutableListOf<BlockState>()
private val canceledNum = AtomicInteger(0) // 已经取消的线程的数 private val canceledNum = AtomicInteger(0) // 已经取消的线程的数
@ -40,7 +39,6 @@ class BlockManager(
private var progress: Long = 0 //当前总进度 private var progress: Long = 0 //当前总进度
private val callback = Callback { msg -> private val callback = Callback { msg ->
checkLooper()
when (msg.what) { when (msg.what) {
IThreadStateManager.STATE_STOP -> { IThreadStateManager.STATE_STOP -> {
stoppedNum.getAndIncrement() stoppedNum.getAndIncrement()
@ -69,15 +67,6 @@ class BlockManager(
completedNum.getAndIncrement() completedNum.getAndIncrement()
if (isCompleted) { if (isCompleted) {
Timber.d("isComplete, completeNum = %s", completedNum) 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() mListener.onComplete()
quitLooper() quitLooper()
} }
@ -89,9 +78,7 @@ class BlockManager(
progress += len progress += len
} }
} }
IThreadStateManager.STATE_UPDATE_PROGRESS -> if (msg.obj == null) { IThreadStateManager.STATE_UPDATE_PROGRESS -> {
progress = updateBlockProgress()
} else if (msg.obj is Long) {
progress = msg.obj as Long progress = msg.obj as Long
} }
} }
@ -148,10 +135,4 @@ class BlockManager(
return callback return callback
} }
private fun mergeFile(): Boolean {
if (blockNum == 1){
return true
}
}
} }

@ -19,6 +19,7 @@ package com.arialyy.aria.core.task;
import android.net.Uri; import android.net.Uri;
import com.arialyy.aria.core.download.DTaskOption; import com.arialyy.aria.core.download.DTaskOption;
import com.arialyy.aria.core.inf.ITaskUtil; import com.arialyy.aria.core.inf.ITaskUtil;
import java.util.Objects;
/** /**
* Created by lyy on 2016/8/11. * Created by lyy on 2016/8/11.
@ -45,4 +46,8 @@ public class DownloadTask extends AbsTask {
@Override public int getTaskType() { @Override public int getTaskType() {
return ITask.DOWNLOAD; return ITask.DOWNLOAD;
} }
@Override public String getTaskKey() {
return Objects.requireNonNull(getTaskOption(DTaskOption.class).getSavePathUri()).toString();
}
} }

@ -116,5 +116,7 @@ public interface ITask {
int getTaskId(); int getTaskId();
String getTaskKey();
<T extends ITaskOption> T getTaskOption(Class<T> clazz); <T extends ITaskOption> T getTaskOption(Class<T> clazz);
} }

@ -22,7 +22,7 @@ package com.arialyy.aria.core.task
**/ **/
interface ITaskInterceptor { interface ITaskInterceptor {
fun interceptor(chain: TaskChain): TaskResp suspend fun interceptor(chain: TaskChain): TaskResp
interface IChain { interface IChain {
fun getTask(): ITask fun getTask(): ITask

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

@ -20,6 +20,7 @@ import androidx.room.RoomDatabase
import com.arialyy.aria.orm.dao.DEntityDao import com.arialyy.aria.orm.dao.DEntityDao
import com.arialyy.aria.orm.dao.DGEntityDao import com.arialyy.aria.orm.dao.DGEntityDao
import com.arialyy.aria.orm.dao.MEntityDao 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.dao.UEntityDao
import com.arialyy.aria.orm.entity.BlockRecord import com.arialyy.aria.orm.entity.BlockRecord
import com.arialyy.aria.orm.entity.DGEntity import com.arialyy.aria.orm.entity.DGEntity
@ -40,4 +41,6 @@ abstract class DuaDb : RoomDatabase() {
abstract fun getDGEntityDao(): DGEntityDao abstract fun getDGEntityDao(): DGEntityDao
abstract fun getMEntityDao(): MEntityDao abstract fun getMEntityDao(): MEntityDao
abstract fun getRecordDao(): RecordDao
} }

@ -35,7 +35,7 @@ interface RecordDao {
@Transaction @Transaction
@Query("SELECT * FROM TaskRecord WHERE :key=taskKey") @Query("SELECT * FROM TaskRecord WHERE :key=taskKey")
suspend fun getTaskRecordByKey(key: String): RecordRelation suspend fun getTaskRecordByKey(key: String): RecordRelation?
@Insert @Insert
@Deprecated( @Deprecated(

Loading…
Cancel
Save