parent
							
								
									fa7741b3e9
								
							
						
					
					
						commit
						294c21db74
					
				| @ -1,110 +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.download.target; | ||||
| 
 | ||||
| import android.text.TextUtils; | ||||
| import com.arialyy.aria.core.common.AbsNormalTarget; | ||||
| import com.arialyy.aria.core.download.DGTaskWrapper; | ||||
| import com.arialyy.aria.core.download.DownloadGroupEntity; | ||||
| import com.arialyy.aria.core.event.ErrorEvent; | ||||
| import com.arialyy.aria.core.inf.AbsTarget; | ||||
| import com.arialyy.aria.core.inf.IConfigHandler; | ||||
| import com.arialyy.aria.core.manager.SubTaskManager; | ||||
| import com.arialyy.aria.core.manager.TaskWrapperManager; | ||||
| import com.arialyy.aria.core.queue.DGroupTaskQueue; | ||||
| import com.arialyy.aria.core.task.DownloadGroupTask; | ||||
| import com.arialyy.aria.orm.DbEntity; | ||||
| import com.arialyy.aria.util.CommonUtil; | ||||
| 
 | ||||
| /** | ||||
|  * Created by lyy on 2019/4/9. | ||||
|  * 下载组合任务功能 | ||||
|  */ | ||||
| abstract class AbsGroupConfigHandler<TARGET extends AbsTarget> implements IConfigHandler { | ||||
|   protected String TAG; | ||||
|   private TARGET mTarget; | ||||
|   private DGTaskWrapper mWrapper; | ||||
| 
 | ||||
|   private SubTaskManager mSubTaskManager; | ||||
| 
 | ||||
|   AbsGroupConfigHandler(TARGET target, long taskId) { | ||||
|     TAG = CommonUtil.getClassName(getClass()); | ||||
|     mTarget = target; | ||||
|     mWrapper = TaskWrapperManager.getInstance().getGroupWrapper(DGTaskWrapper.class, taskId); | ||||
|     // 判断已存在的任务
 | ||||
|     if (mTarget instanceof AbsNormalTarget) { | ||||
|       if (taskId < 0) { | ||||
|         mWrapper.setErrorEvent(new ErrorEvent(taskId, "任务id为空")); | ||||
|       } else if (mWrapper.getEntity().getId() < 0) { | ||||
|         mWrapper.setErrorEvent(new ErrorEvent(taskId, "任务信息不存在")); | ||||
|       } | ||||
|     } | ||||
| 
 | ||||
|     mTarget.setTaskWrapper(mWrapper); | ||||
|     if (getEntity() != null) { | ||||
|       getTaskWrapper().setDirPathTemp(getEntity().getDirPath()); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * 获取子任务管理器 | ||||
|    * | ||||
|    * @return 子任务管理器 | ||||
|    */ | ||||
|   SubTaskManager getSubTaskManager() { | ||||
|     if (mSubTaskManager == null) { | ||||
|       mSubTaskManager = new SubTaskManager(getTaskWrapper()); | ||||
|     } | ||||
|     return mSubTaskManager; | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * 设置任务组别名 | ||||
|    */ | ||||
|   void setGroupAlias(String alias) { | ||||
|     if (TextUtils.isEmpty(alias)) { | ||||
|       return; | ||||
|     } | ||||
|     getEntity().setAlias(alias); | ||||
|   } | ||||
| 
 | ||||
|   @Override public boolean isRunning() { | ||||
|     DownloadGroupTask task = DGroupTaskQueue.getInstance().getTask(getEntity().getKey()); | ||||
|     return task != null && task.isRunning(); | ||||
|   } | ||||
| 
 | ||||
|   TARGET setDirPath(String dirPath) { | ||||
|     mWrapper.setDirPathTemp(dirPath); | ||||
|     return mTarget; | ||||
|   } | ||||
| 
 | ||||
|   @Override public DownloadGroupEntity getEntity() { | ||||
|     return mWrapper.getEntity(); | ||||
|   } | ||||
| 
 | ||||
|   @Override public boolean taskExists() { | ||||
|     return getEntity().getId() != -1 && DbEntity.checkDataExist(DownloadGroupEntity.class, | ||||
|         "rowid=?", String.valueOf(getEntity().getId())); | ||||
|   } | ||||
| 
 | ||||
|   DGTaskWrapper getTaskWrapper() { | ||||
|     return mWrapper; | ||||
|   } | ||||
| 
 | ||||
|   TARGET getTarget() { | ||||
|     return mTarget; | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,113 @@ | ||||
| /* | ||||
|  * 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 | ||||
| 
 | ||||
| import androidx.annotation.Keep | ||||
| import com.arialyy.aria.core.DuaContext | ||||
| import com.arialyy.aria.core.command.ICmdHandler | ||||
| import com.arialyy.aria.core.command.StartCmd | ||||
| import com.arialyy.aria.core.event.DeleteAllEvent | ||||
| import com.arialyy.aria.core.event.Event | ||||
| import com.arialyy.aria.core.event.EventMsgUtil | ||||
| import com.arialyy.aria.core.event.ResumeAllEvent | ||||
| import com.arialyy.aria.core.event.StopAllEvent | ||||
| import com.arialyy.aria.http.download.HttpDTaskAdapter | ||||
| import com.arialyy.aria.util.FileUtils | ||||
| import com.arialyy.aria.util.isNotComplete | ||||
| import kotlinx.coroutines.Dispatchers | ||||
| import kotlinx.coroutines.launch | ||||
| import timber.log.Timber | ||||
| 
 | ||||
| /** | ||||
|  * @Author laoyuyu | ||||
|  * @Description | ||||
|  * @Date 12:40 PM 2023/4/2 | ||||
|  **/ | ||||
| @Keep | ||||
| internal object HttpCmdHandler : ICmdHandler { | ||||
|   override fun initHandler() { | ||||
|     EventMsgUtil.getDefault().register(this) | ||||
|   } | ||||
| 
 | ||||
|   @Event | ||||
|   fun resumeAll(event: ResumeAllEvent) { | ||||
|     DuaContext.duaScope.launch(Dispatchers.IO) { | ||||
|       resumeDTask() | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   @Event | ||||
|   fun stopAll(event: StopAllEvent) { | ||||
|     DuaContext.duaScope.launch(Dispatchers.IO) { | ||||
|       DuaContext.getServiceManager().getDownloadQueue().stopAllTask() | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   @Event | ||||
|   fun removeAll(event: DeleteAllEvent) { | ||||
|     DuaContext.duaScope.launch(Dispatchers.IO) { | ||||
|       removeAllDTask(event) | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   private suspend fun removeAllDTask(event: DeleteAllEvent) { | ||||
|     val dao = DuaContext.getServiceManager().getDbService().getDuaDb().getDEntityDao() | ||||
|     val entityList = dao.queryDEntityList() | ||||
|     DuaContext.getServiceManager().getDownloadQueue().deleteAllTask() | ||||
|     dao.deleteAll() | ||||
|     if (event.onlyRemoveRecord) { | ||||
|       Timber.d("Only remove record") | ||||
|       return | ||||
|     } | ||||
|     // Delete the downloaded file | ||||
|     entityList.forEach { | ||||
|       if (it.fileIsComplete()) { | ||||
|         val path = it.getFilePath() | ||||
|         Timber.d("Delete file: $path") | ||||
|         FileUtils.deleteFile(path) | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * Recovery status is an unfinished task | ||||
|    */ | ||||
|   private suspend fun resumeDTask() { | ||||
|     val entityList = DuaContext.getServiceManager().getDbService().getDuaDb().getDEntityDao() | ||||
|       .queryAllNotCompleteEntityList() | ||||
|     if (entityList.isNullOrEmpty()) { | ||||
|       Timber.w("No tasks to recover") | ||||
|       return | ||||
|     } | ||||
|     entityList.forEach { | ||||
|       if (!it.isNotComplete()) { | ||||
|         Timber.d("Ignore the task, task status: ${it.state}") | ||||
|         return@forEach | ||||
|       } | ||||
|       val taskOption = HttpTaskOption() | ||||
|       taskOption.sourUrl = it.sourceUrl | ||||
|       val tempTask = HttpUtil.getSingDTask(taskOption) | ||||
|       if (tempTask == null) { | ||||
|         Timber.e("Resume task fail, url: ${it.sourceUrl}") | ||||
|         return@forEach | ||||
|       } | ||||
|       val taskAdapter = HttpDTaskAdapter() | ||||
|       tempTask.adapter = taskAdapter | ||||
|       StartCmd(tempTask).executeCmd() | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
| } | ||||
| @ -1,116 +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.command; | ||||
| 
 | ||||
| import com.arialyy.aria.core.download.DGTaskWrapper; | ||||
| import com.arialyy.aria.core.download.DTaskWrapper; | ||||
| import com.arialyy.aria.core.download.DownloadEntity; | ||||
| import com.arialyy.aria.core.download.DownloadGroupEntity; | ||||
| import com.arialyy.aria.core.wrapper.AbsTaskWrapper; | ||||
| import com.arialyy.aria.core.manager.TaskWrapperManager; | ||||
| import com.arialyy.aria.core.queue.DGroupTaskQueue; | ||||
| import com.arialyy.aria.core.queue.DTaskQueue; | ||||
| import com.arialyy.aria.core.queue.UTaskQueue; | ||||
| import com.arialyy.aria.core.upload.UTaskWrapper; | ||||
| import com.arialyy.aria.core.upload.UploadEntity; | ||||
| import com.arialyy.aria.orm.DbEntity; | ||||
| import com.arialyy.aria.util.ALog; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * Created by AriaL on 2017/6/27. | ||||
|  * 删除所有任务,并且删除所有回掉 | ||||
|  */ | ||||
| final public class CancelAllCmd<T extends AbsTaskWrapper> extends AbsNormalCmd<T> { | ||||
|   /** | ||||
|    * removeFile {@code true} 删除已经下载完成的任务,不仅删除下载记录,还会删除已经下载完成的文件,{@code false} | ||||
|    * 如果文件已经下载完成,只删除下载记录 | ||||
|    */ | ||||
|   public boolean removeFile = false; | ||||
| 
 | ||||
|   CancelAllCmd(T entity, int taskType) { | ||||
|     super(entity, taskType); | ||||
|   } | ||||
| 
 | ||||
|   @Override public void executeCmd() { | ||||
|     if (!canExeCmd) return; | ||||
|     if (isDownloadCmd) { | ||||
|       removeAllDTask(); | ||||
|       removeAllDGTask(); | ||||
|     } else { | ||||
|       removeUTask(); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * 删除所有普通下载任务 | ||||
|    */ | ||||
|   private void removeAllDTask() { | ||||
|     List<DownloadEntity> entities = | ||||
|         DbEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false"); | ||||
|     if (entities != null && !entities.isEmpty()) { | ||||
|       for (DownloadEntity entity : entities) { | ||||
|         remove(TaskWrapperManager.getInstance() | ||||
|             .getNormalTaskWrapper(DTaskWrapper.class, entity.getId())); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * 删除所有下载任务组任务 | ||||
|    */ | ||||
|   private void removeAllDGTask() { | ||||
|     List<DownloadGroupEntity> entities = | ||||
|         DbEntity.findDatas(DownloadGroupEntity.class, "state!=?", "-1"); | ||||
|     if (entities != null && !entities.isEmpty()) { | ||||
|       for (DownloadGroupEntity entity : entities) { | ||||
|         remove(TaskWrapperManager.getInstance() | ||||
|             .getGroupWrapper(DGTaskWrapper.class, entity.getId())); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * 删除所有普通上传任务 | ||||
|    */ | ||||
|   private void removeUTask() { | ||||
|     List<UploadEntity> entities = | ||||
|         DbEntity.findDatas(UploadEntity.class, "isGroupChild=?", "false"); | ||||
|     if (entities != null && !entities.isEmpty()) { | ||||
|       for (UploadEntity entity : entities) { | ||||
|         remove(TaskWrapperManager.getInstance() | ||||
|             .getNormalTaskWrapper(UTaskWrapper.class, entity.getId())); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   private void remove(AbsTaskWrapper te) { | ||||
|     if (te == null) { | ||||
|       ALog.w(TAG, "取消任务失败,任务为空"); | ||||
|       return; | ||||
|     } | ||||
|     if (te instanceof DTaskWrapper) { | ||||
|       mQueue = DTaskQueue.getInstance(); | ||||
|     } else if (te instanceof UTaskWrapper) { | ||||
|       mQueue = UTaskQueue.getInstance(); | ||||
|     } else if (te instanceof DGTaskWrapper) { | ||||
|       mQueue = DGroupTaskQueue.getInstance(); | ||||
|     } | ||||
|     te.setRemoveFile(removeFile); | ||||
|     removeTask(te); | ||||
|   } | ||||
| } | ||||
| @ -1,16 +1,30 @@ | ||||
| /* | ||||
|  * 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.command; | ||||
| 
 | ||||
| import com.arialyy.aria.core.event.EventMsgUtil; | ||||
| import com.arialyy.aria.core.event.StopAllEvent; | ||||
| 
 | ||||
| /** | ||||
|  * Created by AriaL on 2017/6/13. | ||||
|  * 停止所有任务的命令,并清空所有等待队列 | ||||
|  */ | ||||
| final class StopAllCmd extends AbsCmd { | ||||
|   StopAllCmd(T entity, int taskType) { | ||||
|     super(entity, taskType); | ||||
|   } | ||||
| 
 | ||||
|   @Override public void executeCmd() { | ||||
|     stopAll(); | ||||
| final class StopAllCmd implements ICmd { | ||||
|   @Override public CmdResp executeCmd() { | ||||
|     EventMsgUtil.getDefault().post(new StopAllEvent()); | ||||
|     return new CmdResp(CmdResp.CODE_COMPLETE); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -1,28 +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.event; | ||||
| 
 | ||||
| /** | ||||
|  * 组合任务最大下载任务数事件 | ||||
|  */ | ||||
| public class DGMaxNumEvent { | ||||
| 
 | ||||
|   public int maxNum; | ||||
| 
 | ||||
|   public DGMaxNumEvent(int maxNum) { | ||||
|     this.maxNum = maxNum; | ||||
|   } | ||||
| } | ||||
| @ -1,26 +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.event; | ||||
| 
 | ||||
| public class ErrorEvent { | ||||
|   public long taskId; | ||||
|   public String errorMsg; | ||||
| 
 | ||||
|   public ErrorEvent(long taskId, String errorMsg) { | ||||
|     this.taskId = taskId; | ||||
|     this.errorMsg = errorMsg; | ||||
|   } | ||||
| } | ||||
| @ -1,28 +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.event; | ||||
| 
 | ||||
| /** | ||||
|  * 最大上传任务数事件 | ||||
|  */ | ||||
| public class UMaxNumEvent { | ||||
| 
 | ||||
|   public int maxNum; | ||||
| 
 | ||||
|   public UMaxNumEvent(int maxNum) { | ||||
|     this.maxNum = maxNum; | ||||
|   } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue