diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java index cdb2bcb5..6e3c9aaa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; +import com.arialyy.aria.core.common.M3U8Entity; import com.arialyy.aria.core.common.controller.FeatureController; import com.arialyy.aria.core.inf.ICheckEntityUtil; import com.arialyy.aria.core.inf.IOptionConstant; diff --git a/Http/src/main/java/com/arialyy/aria/http/HttpTaskOption.kt b/Http/src/main/java/com/arialyy/aria/http/HttpTaskOption.kt index 22aa09b0..fd17ccf7 100644 --- a/Http/src/main/java/com/arialyy/aria/http/HttpTaskOption.kt +++ b/Http/src/main/java/com/arialyy/aria/http/HttpTaskOption.kt @@ -15,7 +15,7 @@ */ package com.arialyy.aria.http -import com.arialyy.aria.core.download.TaskOption +import com.arialyy.aria.core.common.TaskOption import com.arialyy.aria.core.processor.IHttpFileLenAdapter import com.arialyy.aria.core.task.ITaskInterceptor diff --git a/Http/src/main/java/com/arialyy/aria/http/HttpUtil.kt b/Http/src/main/java/com/arialyy/aria/http/HttpUtil.kt index 991fec64..d563410a 100644 --- a/Http/src/main/java/com/arialyy/aria/http/HttpUtil.kt +++ b/Http/src/main/java/com/arialyy/aria/http/HttpUtil.kt @@ -17,9 +17,9 @@ package com.arialyy.aria.http import android.net.Uri import android.text.TextUtils -import com.arialyy.aria.core.inf.ITaskUtil +import com.arialyy.aria.core.inf.ITaskAdapter import com.arialyy.aria.core.task.TaskCachePool -import com.arialyy.aria.http.download.HttpDTaskUtil +import com.arialyy.aria.http.download.HttpDTaskAdapter import com.arialyy.aria.util.FileUtils import com.arialyy.aria.util.Regular import timber.log.Timber @@ -37,10 +37,10 @@ import java.util.zip.InflaterInputStream **/ internal object HttpUtil { - fun getDTaskUtil(filePath: Uri): ITaskUtil { - var util = TaskCachePool.getTaskUtil(filePath) + fun getDTaskUtil(filePath: Uri): ITaskAdapter { + var util = TaskCachePool.getTaskAdapter(filePath) if (util == null) { - util = HttpDTaskUtil() + util = HttpDTaskAdapter() TaskCachePool.putTaskUtil(filePath, util) } return util diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java b/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java index 9f90fcb3..d30c52f1 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java @@ -21,13 +21,12 @@ import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.group.AbsGroupLoader; -import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.group.AbsSubDLoadAdapter; import com.arialyy.aria.core.listener.DownloadGroupListener; import com.arialyy.aria.core.loader.IInfoTask; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.exception.AriaException; import com.arialyy.aria.exception.AriaHTTPException; -import java.io.File; /** * http 组合任务加载器 @@ -45,8 +44,9 @@ final class HttpDGLoader extends AbsGroupLoader { } @Override - protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { - HttpSubDLoaderUtil subUtil = new HttpSubDLoaderUtil(getScheduler(), needGetFileInfo, getKey()); + protected AbsSubDLoadAdapter createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { + HttpSubDLoaderAdapter + subUtil = new HttpSubDLoaderAdapter(getScheduler(), needGetFileInfo, getKey()); subUtil.setParams(wrapper, null); return subUtil; } diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java b/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java deleted file mode 100644 index e3dff075..00000000 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java +++ /dev/null @@ -1,45 +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.http.download; - -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.group.AbsGroupLoader; -import com.arialyy.aria.core.group.AbsGroupLoaderUtil; -import com.arialyy.aria.core.listener.DownloadGroupListener; -import com.arialyy.aria.core.loader.LoaderStructure; -import com.arialyy.aria.http.HttpTaskOption; - -/** - * Created by AriaL on 2017/6/30. - * 任务组下载工具 - */ -public final class HttpDGLoaderUtil extends AbsGroupLoaderUtil { - - @Override protected AbsGroupLoader getLoader() { - if (mLoader == null) { - getTaskWrapper().generateTaskOption(HttpTaskOption.class); - mLoader = new HttpDGLoader(getTaskWrapper(), (DownloadGroupListener) getListener()); - } - return mLoader; - } - - @Override protected LoaderStructure buildLoaderStructure() { - LoaderStructure structure = new LoaderStructure(); - structure.addComponent(new HttpDGInfoTask((DGTaskWrapper) getTaskWrapper())); - structure.accept(getLoader()); - return structure; - } -} \ No newline at end of file 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 dd711273..9aa2ad15 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 @@ -84,13 +84,8 @@ class HttpDStartController(target: Any, val url: String) : HttpBaseStartControll if (HttpUtil.checkHttpDParams(httpTaskOption)) { throw IllegalArgumentException("invalid params") } - val savePath = httpTaskOption.savePathUri!! - var util = TaskCachePool.getTaskUtil(savePath) - if (util == null) { - util = HttpDTaskUtil() - TaskCachePool.putTaskUtil(savePath, util) - } - val task = DownloadTask(httpTaskOption, util) + val task = DownloadTask(httpTaskOption) + task.adapter = HttpDTaskAdapter() DuaContext.duaScope.launch { val dEntity = findDEntityBySavePath(httpTaskOption) TaskCachePool.putEntity(task.taskId, dEntity) diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt index 26a02c49..694537fa 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController.kt @@ -16,6 +16,7 @@ package com.arialyy.aria.http.download import android.net.Uri +import com.arialyy.aria.core.command.StopCmd import com.arialyy.aria.core.task.TaskCachePool import timber.log.Timber @@ -41,7 +42,7 @@ class HttpDStopController(val taskId: Int) { Timber.e("task not found, taskId: $taskId") return } - HttpDStopController2(Uri.parse(task.filePath)).stop() + StopCmd(task).executeCmd() } fun resume() { @@ -50,7 +51,7 @@ class HttpDStopController(val taskId: Int) { Timber.e("task not found, taskId: $taskId") return } - val util = TaskCachePool.getTaskUtil(Uri.parse(task.filePath)) + val util = TaskCachePool.getTaskAdapter(Uri.parse(task.filePath)) if (util == null) { Timber.e("resume fail, please restart task, taskId: $taskId") return diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt index 72a60d36..24920f35 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDStopController2.kt @@ -17,7 +17,11 @@ package com.arialyy.aria.http.download import android.net.Uri import com.arialyy.aria.core.DuaContext -import com.arialyy.aria.http.HttpUtil +import com.arialyy.aria.core.command.CancelCmd +import com.arialyy.aria.core.command.StopCmd +import com.arialyy.aria.core.common.TaskOption +import com.arialyy.aria.core.task.DownloadTask +import com.arialyy.aria.core.task.TaskCachePool import com.arialyy.aria.util.FileUtils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -50,7 +54,14 @@ class HttpDStopController2(val filePath: Uri) { fun cancel() { DuaContext.duaScope.launch { if (checkUri()) { - HttpUtil.getDTaskUtil(filePath).cancel() + var task = TaskCachePool.findTaskByPath(filePath) + if (task == null) { + Timber.i("task not exist, create new task") + task = DownloadTask(TaskOption()) + task.adapter = HttpDTaskAdapter() + } + TaskCachePool.putTask(task) + CancelCmd(task).executeCmd() } } } @@ -58,8 +69,15 @@ class HttpDStopController2(val filePath: Uri) { fun stop() { DuaContext.duaScope.launch { if (checkUri()) { - HttpUtil.getDTaskUtil(filePath).stop() + TaskCachePool.findTaskByPath(filePath)?.let { + StopCmd(it).executeCmd() + return@launch + } + + Timber.e("not found task, uri: $filePath") + return@launch } + Timber.e("stop fail, invalid uri: $filePath") } } } \ No newline at end of file diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt similarity index 96% rename from Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt rename to Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt index 3fafe7c2..1cc01219 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskAdapter.kt @@ -18,7 +18,7 @@ package com.arialyy.aria.http.download import android.os.Looper import com.arialyy.aria.core.DuaContext import com.arialyy.aria.core.inf.IBlockManager -import com.arialyy.aria.core.task.AbsTaskUtil +import com.arialyy.aria.core.task.AbsTaskAdapter import com.arialyy.aria.core.task.BlockManager import com.arialyy.aria.core.task.DownloadTask import com.arialyy.aria.core.task.TaskResp @@ -33,7 +33,7 @@ import kotlinx.coroutines.launch * @Description * @Date 1:47 PM 2023/1/28 **/ -internal class HttpDTaskUtil : AbsTaskUtil() { +internal class HttpDTaskAdapter : AbsTaskAdapter() { private var blockManager: BlockManager? = null diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java b/Http/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderAdapter.java similarity index 90% rename from Http/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java rename to Http/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderAdapter.java index 4e9d1c64..9782e659 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderAdapter.java @@ -17,7 +17,7 @@ package com.arialyy.aria.http.download; import android.os.Handler; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.group.AbsSubDLoadAdapter; import com.arialyy.aria.core.group.SubRecordHandler; import com.arialyy.aria.core.loader.GroupSubThreadStateManager; import com.arialyy.aria.core.loader.LoaderStructure; @@ -28,12 +28,12 @@ import com.arialyy.aria.core.loader.SubLoader; * @author lyy * Date: 2019-09-28 */ -final class HttpSubDLoaderUtil extends AbsSubDLoadUtil { +final class HttpSubDLoaderAdapter extends AbsSubDLoadAdapter { /** * @param schedulers 调度器 * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - HttpSubDLoaderUtil( Handler schedulers, boolean needGetInfo, String parentKey) { + HttpSubDLoaderAdapter( Handler schedulers, boolean needGetInfo, String parentKey) { super(schedulers, needGetInfo, parentKey); } diff --git a/Http/src/main/java/com/arialyy/aria/http/upload/HttpUTaskUtil.kt b/Http/src/main/java/com/arialyy/aria/http/upload/HttpUTaskAdapter.kt similarity index 92% rename from Http/src/main/java/com/arialyy/aria/http/upload/HttpUTaskUtil.kt rename to Http/src/main/java/com/arialyy/aria/http/upload/HttpUTaskAdapter.kt index ccaccd32..f398e3cc 100644 --- a/Http/src/main/java/com/arialyy/aria/http/upload/HttpUTaskUtil.kt +++ b/Http/src/main/java/com/arialyy/aria/http/upload/HttpUTaskAdapter.kt @@ -18,14 +18,12 @@ package com.arialyy.aria.http.upload import android.os.Looper import com.arialyy.aria.core.DuaContext import com.arialyy.aria.core.inf.IBlockManager -import com.arialyy.aria.core.task.AbsTaskUtil +import com.arialyy.aria.core.task.AbsTaskAdapter import com.arialyy.aria.core.task.BlockManager import com.arialyy.aria.core.task.TaskResp import com.arialyy.aria.exception.AriaException import com.arialyy.aria.http.HttpTaskOption import com.arialyy.aria.http.download.HttpBlockThreadInterceptor -import com.arialyy.aria.http.download.HttpDBlockInterceptor -import com.arialyy.aria.http.download.HttpDHeaderInterceptor import com.arialyy.aria.http.download.TimerInterceptor import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -35,7 +33,7 @@ import kotlinx.coroutines.launch * @Description * @Date 9:21 PM 2023/2/21 **/ -class HttpUTaskUtil : AbsTaskUtil() { +class HttpUTaskAdapter : AbsTaskAdapter() { private var blockManager: BlockManager? = null override fun getBlockManager(): IBlockManager { diff --git a/Http/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.ITaskUtil b/Http/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.ITaskAdapter similarity index 100% rename from Http/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.ITaskUtil rename to Http/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.ITaskAdapter diff --git a/HttpGroup/src/main/java/com/arialyy/dua/group/DGroupUtil.kt b/HttpGroup/src/main/java/com/arialyy/dua/group/DGroupAdapter.kt similarity index 91% rename from HttpGroup/src/main/java/com/arialyy/dua/group/DGroupUtil.kt rename to HttpGroup/src/main/java/com/arialyy/dua/group/DGroupAdapter.kt index f9b8e68e..16413fc8 100644 --- a/HttpGroup/src/main/java/com/arialyy/dua/group/DGroupUtil.kt +++ b/HttpGroup/src/main/java/com/arialyy/dua/group/DGroupAdapter.kt @@ -16,14 +16,14 @@ package com.arialyy.dua.group import com.arialyy.aria.core.inf.IBlockManager -import com.arialyy.aria.core.task.AbsTaskUtil +import com.arialyy.aria.core.task.AbsTaskAdapter /** * @Author laoyuyu * @Description * @Date 21:58 2023/2/20 **/ -internal class DGroupUtil : AbsTaskUtil() { +internal class DGroupAdapter : AbsTaskAdapter() { override fun getBlockManager(): IBlockManager { TODO("Not yet implemented") } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java deleted file mode 100644 index 1bac8498..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java +++ /dev/null @@ -1,132 +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; - -import com.arialyy.aria.core.common.BaseOption; -import com.arialyy.aria.core.inf.IEventHandler; -import com.arialyy.aria.core.inf.IOptionConstant; -import com.arialyy.aria.core.processor.FtpInterceptHandler; -import com.arialyy.aria.core.processor.IBandWidthUrlConverter; -import com.arialyy.aria.core.processor.IFtpUploadInterceptor; -import com.arialyy.aria.core.processor.IHttpFileLenAdapter; -import com.arialyy.aria.core.processor.IHttpFileNameAdapter; -import com.arialyy.aria.core.processor.IKeyUrlConverter; -import com.arialyy.aria.core.processor.ILiveTsUrlConverter; -import com.arialyy.aria.core.processor.ITsMergeHandler; -import com.arialyy.aria.core.processor.IVodTsUrlConverter; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 任务配置参数 - * - * @author lyy - * Date: 2019-09-10 - */ -public class TaskOptionParams { - - private static List PROCESSORES = new ArrayList<>(); - - /** - * 普通参数 - */ - private Map params = new HashMap<>(); - - /** - * 事件处理对象 - */ - private Map handler = new HashMap<>(); - - static { - PROCESSORES.add(FtpInterceptHandler.class); - PROCESSORES.add(IBandWidthUrlConverter.class); - PROCESSORES.add(IFtpUploadInterceptor.class); - PROCESSORES.add(IHttpFileLenAdapter.class); - PROCESSORES.add(IHttpFileNameAdapter.class); - PROCESSORES.add(ILiveTsUrlConverter.class); - PROCESSORES.add(ITsMergeHandler.class); - PROCESSORES.add(IVodTsUrlConverter.class); - PROCESSORES.add(IKeyUrlConverter.class); - } - - /** - * 设置任务参数 - * - * @param option 任务配置 - */ - public void setParams(BaseOption option) { - List fields = CommonUtil.getAllFields(option.getClass()); - - for (Field field : fields) { - field.setAccessible(true); - try { - - if (PROCESSORES.contains(field.getType())) { - Object eventHandler = field.get(option); - if (eventHandler != null) { - setObjs(field.getName(), (IEventHandler) eventHandler); - } - } else { - Object params = field.get(option); - if (params != null) { - setParams(field.getName(), params); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - /** - * 设置普通参数 - * - * @param key {@link IOptionConstant} - */ - public TaskOptionParams setParams(String key, Object value) { - params.put(key, value); - return this; - } - - /** - * 设置对象参数 - */ - public TaskOptionParams setObjs(String key, IEventHandler handler) { - this.handler.put(key, handler); - return this; - } - - public Map getParams() { - return params; - } - - public Object getParam(String key) { - return params.get(key); - } - - public IEventHandler getHandler(String key) { - return handler.get(key); - } - - public Map getHandler() { - return handler; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskRecord.java b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskRecord.java deleted file mode 100644 index 75d3d92e..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskRecord.java +++ /dev/null @@ -1,91 +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; - -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.orm.annotation.Ignore; -import com.arialyy.aria.orm.annotation.NoNull; -import com.arialyy.aria.orm.annotation.Unique; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/21. - * 任务上传或下载的任务记录 - */ -public class TaskRecord extends DbEntity { - //public static final int TYPE_HTTP_FTP = 0; - //public static final int TYPE_M3U8_VOD = 1; - //public static final int TYPE_M3U8_LIVE = 2; - - @Ignore - public List threadRecords; - - /** - * 任务线程数 - */ - public int threadNum; - - /** - * 任务文件路径 - */ - public String filePath; - - /** - * 文件长度 - */ - public long fileLength; - - /** - * 任务文件名 - */ - @NoNull - public String fileName; - - /** - * 是否是任务组的子任务记录 - * {@code true}是 - */ - public boolean isGroupRecord = false; - - /** - * 下载任务组名 - */ - public String dGroupHash; - - /** - * 上传组任务名,暂时没有用 - */ - @Ignore - @Deprecated - public String uGroupHash; - - /** - * 是否是分块{@code true}是,{@code false} 不是 - */ - public boolean isBlock = false; - - /** - * 任务类型 - * {@link ITaskWrapper} - */ - public int taskType = 0; - - /** - * m3u8文件码率 - */ - public long bandWidth = 0; -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/ThreadRecord.java b/PublicComponent/src/main/java/com/arialyy/aria/core/ThreadRecord.java deleted file mode 100644 index 546172d7..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/ThreadRecord.java +++ /dev/null @@ -1,68 +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; - -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.DbEntity; - -/** - * Created by laoyuyu on 2018/5/8. - * 任务的线程记录 - */ -public class ThreadRecord extends DbEntity { - - /** - * 任务的文件路径,不是当前线程记录的的分块文件路径 - */ - public String taskKey; - - /** - * 开始位置 - */ - public long startLocation; - - /** - * 结束位置 - */ - public long endLocation; - - /** - * 线程是否完成 - * {@code true}完成,{@code false}未完成 - */ - public boolean isComplete = false; - - /** - * 线程id - */ - public int threadId = 0; - - /** - * 分块长度 - */ - public long blockLen = 0; - - /** - * 线程类型 - * {@link ITaskWrapper} - */ - public int threadType = 0; - - /** - * ts文件的下载地址 - */ - public String tsUrl; -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmd.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmd.java index 12fb6c82..73f97751 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmd.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmd.java @@ -21,6 +21,7 @@ import com.arialyy.aria.core.inf.ITaskQueue; import com.arialyy.aria.core.task.ITask; import java.util.ArrayList; import java.util.List; +import timber.log.Timber; /** * Created by AriaL on 2017/6/29. @@ -53,8 +54,13 @@ public abstract class AbsCmd implements ICmd { * if interruption occurred, stop cmd */ protected CmdResp interceptor() { + if (mTask == null) { + Timber.e("task is null"); + return new CmdResp(CmdResp.CODE_TASK_NOT_FOUND); + } if (userInterceptors == null || userInterceptors.isEmpty()) { - return null; + Timber.e("not any interceptor"); + return new CmdResp(CmdResp.CODE_INTERRUPT); } List interceptors = new ArrayList<>(); interceptors.addAll(userInterceptors); @@ -64,6 +70,10 @@ public abstract class AbsCmd implements ICmd { } public ITaskQueue getTaskQueue() { + if (mTask == null) { + Timber.e("task is null"); + return null; + } ITaskQueue itq = null; switch (mTask.getTaskType()) { case ITask.DOWNLOAD: { diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java deleted file mode 100644 index 1a7cbf34..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java +++ /dev/null @@ -1,32 +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.task.ITask; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; - -/** - * Created by AriaL on 2017/6/29. - * 抽象命令工厂 - */ -public abstract class AbsCmdFactory { - - /** - * @param entity 下载实体 - * {@link ITask#DOWNLOAD}、{@link ITask#DOWNLOAD_GROUP}、{@link ITask#UPLOAD} - */ - public abstract CMD createCmd(TASK_ENTITY entity, int type, int taskType); -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java deleted file mode 100644 index 7efde45b..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java +++ /dev/null @@ -1,198 +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.task.AbsTask; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.task.ITask; -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.listener.ISchedulers; -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; - -/** - * Created by lyy on 2016/8/22. 下载命令 - */ -public abstract class AbsNormalCmd extends AbsCmd { - /** - * 能否执行命令 - */ - boolean canExeCmd = true; - - int taskType; - - /** - * @param taskType 下载任务类型{@link ITask#DOWNLOAD}、{@link ITask#DOWNLOAD_GROUP}、{@link - * ITask#UPLOAD} - */ - AbsNormalCmd(T entity, int taskType) { - this.taskType = taskType; - mTaskWrapper = entity; - TAG = CommonUtil.getClassName(this); - if (taskType == ITask.DOWNLOAD) { - if (!(entity instanceof DTaskWrapper)) { - ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD"); - return; - } - mQueue = DTaskQueue.getInstance(); - } else if (taskType == ITask.DOWNLOAD_GROUP) { - if (!(entity instanceof DGTaskWrapper)) { - ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD_GROUP"); - return; - } - mQueue = DGroupTaskQueue.getInstance(); - } else if (taskType == ITask.UPLOAD) { - if (!(entity instanceof UTaskWrapper)) { - ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_UPLOAD"); - return; - } - mQueue = UTaskQueue.getInstance(); - } else { - ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD、TASK_TYPE_DOWNLOAD_GROUP、TASK_TYPE_UPLOAD"); - return; - } - isDownloadCmd = taskType == ITask.DOWNLOAD || taskType == ITask.DOWNLOAD_GROUP; - } - - /** - * 发送等待状态 - */ - void sendWaitState() { - AbsTask task = getTask(); - if (task == null) { - task = createTask(); - } - sendWaitState(task); - } - - /** - * 发送等待状态 - */ - void sendWaitState(AbsTask task) { - if (task != null) { - task.getTaskWrapper().setState(IEntity.STATE_WAIT); - task.getOutHandler().obtainMessage(ISchedulers.WAIT, task).sendToTarget(); - } - } - - /** - * 停止所有任务 - */ - void stopAll() { - mQueue.stopAllTask(); - } - - /** - * 停止任务 - */ - void stopTask() { - AbsTask task = getTask(); - if (task == null) { - task = createTask(); - } - mQueue.stopTask(task); - } - - /** - * 删除任务 - */ - void removeTask() { - AbsTask task = getTask(); - if (task == null) { - task = createTask(); - } - mQueue.cancelTask(task); - } - - /** - * 删除任务 - */ - void removeTask(AbsTaskWrapper wrapper) { - AbsTask tempTask = getTask(wrapper.getKey()); - if (tempTask == null) { - tempTask = createTask(wrapper); - } - mQueue.cancelTask(tempTask); - } - - /** - * 启动任务 - */ - void startTask() { - AbsTask task = getTask(); - if (task == null) { - task = createTask(); - } - mQueue.startTask(task); - } - - /** - * 恢复任务 - */ - void resumeTask() { - AbsTask task = getTask(); - if (task == null) { - task = createTask(); - } - mQueue.resumeTask(task); - } - - - /** - * 从队列中获取任务 - * - * @return 执行任务 - */ - AbsTask getTask() { - return mQueue.getTask(mTaskWrapper.getEntity().getKey()); - } - - /** - * 从队列中获取任务 - * - * @return 执行任务 - */ - AbsTask getTask(String key) { - return mQueue.getTask(key); - } - - /** - * 创建任务 - * - * @return 创建的任务 - */ - AbsTask createTask() { - return mQueue.createTask(mTaskWrapper); - } - - /** - * 创建指定实体的任务 - * - * @param wrapper 特定的任务实体 - * @return 创建的任务 - */ - AbsTask createTask(AbsTaskWrapper wrapper) { - - return mQueue.createTask(wrapper); - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelCmd.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelCmd.kt similarity index 50% rename from PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelCmd.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelCmd.kt index 117e235d..ab0b00b5 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelCmd.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/command/CancelCmd.kt @@ -16,33 +16,35 @@ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.task.AbsTask; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.DuaContext +import com.arialyy.aria.core.task.ITask +import com.arialyy.aria.core.task.ThreadTaskManager2 +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import timber.log.Timber /** * Created by lyy on 2016/9/20. * 取消命令 */ -final public class CancelCmd extends AbsNormalCmd { - /** - * removeFile {@code true} 删除已经下载完成的任务,不仅删除下载记录,还会删除已经下载完成的文件,{@code false} - * 如果文件已经下载完成,只删除下载记录 - */ - public boolean removeFile = false; +class CancelCmd(task: ITask) : AbsCmd(task) { - CancelCmd(T entity, int taskType) { - super(entity, taskType); - } + override fun executeCmd(): CmdResp { + val resp = interceptor() + if (resp.isInterrupt()) { + Timber.w("interruption occurred, cancel cmd") + return resp + } - @Override public void executeCmd() { - if (!canExeCmd) return; - AbsTask task = getTask(); - if (task == null) { - task = createTask(); + if (!taskQueue.taskIsRunning(mTask?.taskId ?: -1)) { + // remove already file + + return CmdResp(CmdResp.CODE_COMPLETE) } - if (task != null) { - mTaskWrapper.setRemoveFile(removeFile); - removeTask(); + DuaContext.duaScope.launch(Dispatchers.IO) { + ThreadTaskManager2.stopThreadTask(mTask!!.taskId, true) } + return CmdResp(CmdResp.CODE_COMPLETE) } + } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/CmdResp.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/command/CmdResp.kt index 6bd63d80..5c916160 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/CmdResp.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/command/CmdResp.kt @@ -25,6 +25,7 @@ class CmdResp(val code: Int = CODE_DEF) { const val CODE_COMPLETE = 1 const val CODE_INTERRUPT = 999 const val CODE_DEF = 0 + const val CODE_TASK_NOT_FOUND = 2 } /** diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java deleted file mode 100644 index 194b7c32..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java +++ /dev/null @@ -1,114 +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.task.ITask; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; - -/** - * Created by Lyy on 2016/9/23. - * 命令工厂 - */ -public class NormalCmdFactory extends AbsCmdFactory { - /** - * 创建任务 - */ - public static final int TASK_CREATE = 0xb1; - /** - * 启动任务 - */ - public static final int TASK_START = 0xb2; - /** - * 恢复任务 - */ - public static final int TASK_RESUME = 0xb3; - /** - * 取消任务 - */ - public static final int TASK_CANCEL = 0xb4; - /** - * 停止任务 - */ - public static final int TASK_STOP = 0xb5; - /** - * 设置任务为最高优先级 - */ - public static final int TASK_HIGHEST_PRIORITY = 0xb6; - /** - * 停止所有任务 - */ - public static final int TASK_STOP_ALL = 0xb7; - /** - * 恢复所有停止的任务 - */ - public static final int TASK_RESUME_ALL = 0xb8; - /** - * 删除所有任务, - */ - public static final int TASK_CANCEL_ALL = 0xb9; - /** - * 重启任务 - */ - public static final int TASK_RESTART = 0xba; - - private static volatile NormalCmdFactory INSTANCE = null; - - private NormalCmdFactory() { - - } - - public static NormalCmdFactory getInstance() { - if (INSTANCE == null) { - synchronized (NormalCmdFactory.class) { - INSTANCE = new NormalCmdFactory(); - } - } - return INSTANCE; - } - - /** - * @param entity 下载实体 - * @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link - * #TASK_STOP}、{@link #TASK_HIGHEST_PRIORITY}、{@link #TASK_STOP_ALL}、{@link #TASK_RESUME_ALL} - * @param taskType {@link ITask#DOWNLOAD}、{@link ITask#DOWNLOAD_GROUP}、{@link ITask#UPLOAD} - */ - public AbsNormalCmd createCmd(AbsTaskWrapper entity, int type, int taskType) { - switch (type) { - case TASK_CREATE: - return new AddCmd<>(entity, taskType); - case TASK_RESUME: - case TASK_START: - return new StartCmd<>(entity, taskType); - case TASK_CANCEL: - return new CancelCmd<>(entity, taskType); - case TASK_STOP: - return new StopCmd<>(entity, taskType); - case TASK_HIGHEST_PRIORITY: - return new HighestPriorityCmd<>(entity, taskType); - case TASK_STOP_ALL: - return new StopAllCmd<>(entity, taskType); - case TASK_RESUME_ALL: - return new ResumeAllCmd<>(entity, taskType); - case TASK_CANCEL_ALL: - return new CancelAllCmd<>(entity, taskType); - case TASK_RESTART: - return new ReStartCmd<>(entity, taskType); - default: - return null; - } - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/StopCmd.java b/PublicComponent/src/main/java/com/arialyy/aria/core/command/StopCmd.java deleted file mode 100644 index 5032fc83..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/command/StopCmd.java +++ /dev/null @@ -1,47 +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.task.AbsTask; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.util.ALog; - -/** - * Created by lyy on 2016/9/20. - * 停止命令 - */ -final class StopCmd extends AbsNormalCmd { - - StopCmd(T entity, int taskType) { - super(entity, taskType); - } - - @Override public void executeCmd() { - if (!canExeCmd) return; - AbsTask task = getTask(); - if (task == null) { - if (mTaskWrapper.getEntity().getState() == IEntity.STATE_RUNNING) { - stopTask(); - } else { - ALog.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); - } - } else { - stopTask(); - } - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/command/StopCmd.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/command/StopCmd.kt new file mode 100644 index 00000000..bd6bdeec --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/command/StopCmd.kt @@ -0,0 +1,50 @@ +/* + * 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.DuaContext +import com.arialyy.aria.core.command.CmdResp.Companion.CODE_COMPLETE +import com.arialyy.aria.core.command.CmdResp.Companion.CODE_INTERRUPT +import com.arialyy.aria.core.task.ITask +import com.arialyy.aria.core.task.ThreadTaskManager2 +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import timber.log.Timber + +/** + * Created by lyy on 2016/9/20. + * 停止命令 + */ +class StopCmd(task: ITask) : AbsCmd(task) { + + override fun executeCmd(): CmdResp { + val resp = interceptor() + if (resp.isInterrupt()) { + Timber.w("interruption occurred, cancel cmd"); + return resp; + } + + if (!taskQueue.taskIsRunning(mTask?.taskId ?: -1)) { + Timber.e("task not running") + return CmdResp(CODE_INTERRUPT) + } + DuaContext.duaScope.launch(Dispatchers.IO) { + ThreadTaskManager2.stopThreadTask(mTask!!.taskId) + } + return CmdResp(CODE_COMPLETE) + } +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsEntity.java deleted file mode 100644 index 81954d99..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsEntity.java +++ /dev/null @@ -1,267 +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.common; - -import android.os.Parcel; -import android.os.Parcelable; -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.orm.annotation.Default; -import com.arialyy.aria.orm.annotation.Ignore; -import java.io.Serializable; - -/** - * Created by AriaL on 2017/6/29. - */ -public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable, Serializable { - /** - * 速度 - */ - @Ignore private long speed = 0; - /** - * 单位转换后的速度 - */ - @Ignore private String convertSpeed; - /** - * 下载失败计数,每次开始都重置为0 - */ - @Ignore private int failNum = 0; - - /** - * 剩余时间,单位:s - */ - @Ignore private int timeLeft = Integer.MAX_VALUE; - - /** - * 扩展字段 - */ - private String str; - /** - * 文件大小 - */ - private long fileSize = 0; - /** - * 转换后的文件大小 - */ - private String convertFileSize; - - /** - * 任务状态{@link IEntity} - */ - @Default("3") - private int state = STATE_WAIT; - /** - * 当前下载进度 - */ - private long currentProgress = 0; - /** - * 完成时间 - */ - private long completeTime; - - /** - * 进度百分比 - */ - private int percent; - - @Default("false") - private boolean isComplete = false; - - /** - * 上一次停止时间 - */ - private long stopTime = 0; - - @Ignore - private int netCode = 200; - - public int getNetCode() { - return netCode; - } - - public void setNetCode(int netCode) { - this.netCode = netCode; - } - - /** - * 获取剩余时间,单位:s - * 如果是m3u8任务,无法获取剩余时间;m2u8任务如果需要获取剩余时间,请设置文件长度{@link #setFileSize(long)} - */ - public int getTimeLeft() { - return timeLeft; - } - - public void setTimeLeft(int timeLeft) { - this.timeLeft = timeLeft; - } - - public boolean isComplete() { - return isComplete; - } - - public void setComplete(boolean complete) { - isComplete = complete; - } - - public String getConvertFileSize() { - return convertFileSize; - } - - public void setConvertFileSize(String convertFileSize) { - this.convertFileSize = convertFileSize; - } - - public int getFailNum() { - return failNum; - } - - public void setFailNum(int failNum) { - this.failNum = failNum; - } - - public long getSpeed() { - return speed; - } - - public void setSpeed(long speed) { - this.speed = speed; - } - - public String getConvertSpeed() { - return convertSpeed; - } - - public void setConvertSpeed(String convertSpeed) { - this.convertSpeed = convertSpeed; - } - - public String getStr() { - return str; - } - - public void setStr(String str) { - this.str = str; - } - - public long getFileSize() { - return fileSize; - } - - public void setFileSize(long fileSize) { - this.fileSize = fileSize; - } - - public int getState() { - return state; - } - - public void setState(int state) { - this.state = state; - } - - public long getCurrentProgress() { - return currentProgress; - } - - public void setCurrentProgress(long currentProgress) { - this.currentProgress = currentProgress; - } - - public long getCompleteTime() { - return completeTime; - } - - public void setCompleteTime(long completeTime) { - this.completeTime = completeTime; - } - - public int getPercent() { - return percent; - } - - public void setPercent(int percent) { - this.percent = percent; - } - - public long getStopTime() { - return stopTime; - } - - public void setStopTime(long stopTime) { - this.stopTime = stopTime; - } - - public long getId() { - return getRowID(); - } - - /** - * 实体唯一标识符 - * 下载实体:下载url - * 上传实体:文件路径 - * 下载任务组:组名 - * ftp文件夹下载:下载url - */ - public abstract String getKey(); - - /** - * 实体驱动的下载任务类型 - * - * @return {@link ITaskWrapper#D_FTP}、{@link ITaskWrapper#D_FTP_DIR}、{@link - * ITaskWrapper#U_HTTP}... - */ - public abstract int getTaskType(); - - public AbsEntity() { - } - - @Override public int describeContents() { - return 0; - } - - @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeLong(this.rowID); - dest.writeLong(this.speed); - dest.writeString(this.convertSpeed); - dest.writeInt(this.failNum); - dest.writeString(this.str); - dest.writeLong(this.fileSize); - dest.writeString(this.convertFileSize); - dest.writeInt(this.state); - dest.writeLong(this.currentProgress); - dest.writeLong(this.completeTime); - dest.writeInt(this.percent); - dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0); - dest.writeLong(this.stopTime); - } - - protected AbsEntity(Parcel in) { - this.rowID = in.readLong(); - this.speed = in.readLong(); - this.convertSpeed = in.readString(); - this.failNum = in.readInt(); - this.str = in.readString(); - this.fileSize = in.readLong(); - this.convertFileSize = in.readString(); - this.state = in.readInt(); - this.currentProgress = in.readLong(); - this.completeTime = in.readLong(); - this.percent = in.readInt(); - this.isComplete = in.readByte() != 0; - this.stopTime = in.readLong(); - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsGroupEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsGroupEntity.java deleted file mode 100644 index 4acd8d81..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsGroupEntity.java +++ /dev/null @@ -1,103 +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.common; - -import android.os.Parcel; -import android.os.Parcelable; -import com.arialyy.aria.orm.annotation.Unique; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by AriaL on 2017/6/3. - */ -public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { - /** - * 组合任务等hash为: 为子任务地址相加的url的Md5 - * ftpdir为:ftpdir下载地址 - */ - @Unique protected String groupHash; - - /** - * 任务组别名 - */ - private String alias; - - /** - * 任务组下载文件的文件夹地址 - */ - @Unique private String dirPath; - - /** - * 子任务url地址 - */ - private List urls = new ArrayList<>(); - - public String getDirPath() { - return dirPath; - } - - public void setDirPath(String dirPath) { - this.dirPath = dirPath; - } - - public List getUrls() { - return urls; - } - - public void setUrls(List urls) { - this.urls = urls; - } - - /** - * 组合任务等hash为: 为子任务地址相加的url的Md5 - * ftpdir为:ftpdir下载地址 - */ - public String getGroupHash() { - return groupHash; - } - - public String getAlias() { - return alias; - } - - @Override public String getKey() { - return groupHash; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public AbsGroupEntity() { - } - - @Override public int describeContents() { - return 0; - } - - @Override public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeString(this.groupHash); - dest.writeString(this.alias); - } - - protected AbsGroupEntity(Parcel in) { - super(in); - this.groupHash = in.readString(); - this.alias = in.readString(); - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsNormalEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsNormalEntity.java deleted file mode 100644 index c4222b2a..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsNormalEntity.java +++ /dev/null @@ -1,125 +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.common; - -import android.os.Parcel; -import android.os.Parcelable; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.annotation.Default; - -/** - * Created by AriaL on 2017/6/3. - */ -public abstract class AbsNormalEntity extends AbsEntity implements Parcelable { - - /** - * 服务器地址 - */ - private String url; - - /** - * 文件名 - */ - private String fileName; - - /** - * 是否是任务组里面的下载实体 - */ - @Default("false") - private boolean isGroupChild = false; - - @Default("false") - private boolean isRedirect = false; //是否重定向 - private String redirectUrl; //重定向链接 - - /** - * 任务类型 - * {@link ITaskWrapper} - */ - private int taskType; - - - public void setTaskType(int taskType) { - this.taskType = taskType; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public boolean isGroupChild() { - return isGroupChild; - } - - public void setGroupChild(boolean groupChild) { - isGroupChild = groupChild; - } - - public String getFileName() { - return fileName; - } - - public void setFileName(String fileName) { - this.fileName = fileName; - } - - public boolean isRedirect() { - return isRedirect; - } - - public void setRedirect(boolean redirect) { - isRedirect = redirect; - } - - public String getRedirectUrl() { - return redirectUrl; - } - - public void setRedirectUrl(String redirectUrl) { - this.redirectUrl = redirectUrl; - } - - public abstract String getFilePath(); - - public AbsNormalEntity() { - } - - @Override public int describeContents() { - return 0; - } - - @Override public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeString(this.url); - dest.writeString(this.fileName); - dest.writeByte(this.isGroupChild ? (byte) 1 : (byte) 0); - dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0); - dest.writeString(this.redirectUrl); - } - - protected AbsNormalEntity(Parcel in) { - super(in); - this.url = in.readString(); - this.fileName = in.readString(); - this.isGroupChild = in.readByte() != 0; - this.isRedirect = in.readByte() != 0; - this.redirectUrl = in.readString(); - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java deleted file mode 100644 index f6107d28..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java +++ /dev/null @@ -1,42 +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.common; - -import com.arialyy.aria.core.wrapper.ITaskWrapper; - -/** - * Created by AriaL on 2018/3/3. - * 获取文件信息完成后 回调给下载线程的信息 - */ -public class CompleteInfo { - /** - * 自定义的状态码 - */ - public int code; - - public ITaskWrapper wrapper; - - public Object obj; - - public CompleteInfo() { - - } - - public CompleteInfo(int code, ITaskWrapper wrapper) { - this.code = code; - this.wrapper = wrapper; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/ErrorCode.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/ErrorCode.java deleted file mode 100644 index b115b6f0..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/ErrorCode.java +++ /dev/null @@ -1,33 +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.common; - -public enum ErrorCode { - ERROR_CODE_NORMAL(0, "正常"), ERROR_CODE_TASK_ID_NULL(1, "任务id为空的错误码"), - ERROR_CODE_URL_NULL(2, "url 为空"), ERROR_CODE_URL_INVALID(3, "url 无效"), - ERROR_CODE_PAGE_NUM(4, "page和num不能小于1"), ERROR_CODE_GROUP_URL_NULL(5, "组合任务url列表为空"), - ERROR_CODE_UPLOAD_FILE_NULL(7, "上传文件不存在"), - ERROR_CODE_MEMBER_WARNING(8, "为了防止内存泄漏,请使用静态的成员类(public static class xxx)或文件类(A.java)"), - ERROR_CODE_TASK_NOT_EXIST(9, "任务信息不存在"); - - public int code; - public String msg; - - ErrorCode(int code, String msg) { - this.code = code; - this.msg = msg; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/M3U8Entity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/M3U8Entity.java similarity index 99% rename from PublicComponent/src/main/java/com/arialyy/aria/core/download/M3U8Entity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/M3U8Entity.java index b534099b..468ba3ae 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/M3U8Entity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/M3U8Entity.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.common; import android.os.Parcel; import android.os.Parcelable; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java deleted file mode 100644 index 8e5f0c1e..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java +++ /dev/null @@ -1,225 +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.common; - -import com.arialyy.aria.core.TaskRecord; -import com.arialyy.aria.core.ThreadRecord; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.loader.ILoaderVisitor; -import com.arialyy.aria.core.loader.IRecordHandler; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.core.wrapper.RecordWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.DbDataHelper; -import com.arialyy.aria.util.FileUtil; -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -/** - * 处理任务记录,分配线程区间 - */ -public abstract class RecordHandler implements IRecordHandler { - protected final String TAG = CommonUtil.getClassName(this); - - @Deprecated private File mConfigFile; - private TaskRecord mTaskRecord; - private AbsTaskWrapper mTaskWrapper; - private AbsNormalEntity mEntity; - protected String mFilePath; - protected long mFileSize; - - public RecordHandler(AbsTaskWrapper wrapper) { - mTaskWrapper = wrapper; - mEntity = (AbsNormalEntity) mTaskWrapper.getEntity(); - } - - public AbsTaskWrapper getWrapper() { - return mTaskWrapper; - } - - public AbsNormalEntity getEntity() { - return mEntity; - } - - @Override public void onPre() { - - } - - /** - * 获取任务记录,如果任务记录存在,检查任务记录 - * 检查记录 对于分块任务: 子分块不存在或被删除,子线程将重新下载 - * 对于普通任务: 预下载文件不存在,则任务任务呗删除 - * 如果任务记录不存在或线程记录不存在,初始化记录 - * - * @return 任务记录 - */ - @Override - public TaskRecord getRecord(long fileSize) { - mFileSize = fileSize; - mConfigFile = new File(CommonUtil.getFileConfigPath(false, mEntity.getFileName())); - if (mConfigFile.exists()) { - convertDb(); - } else { - onPre(); - mTaskRecord = DbDataHelper.getTaskRecord(getFilePath(), mEntity.getTaskType()); - if (mTaskRecord == null) { - initRecord(true); - }else if (mTaskRecord.threadRecords == null || mTaskRecord.threadRecords.size() == 0){ - if (mTaskRecord.threadRecords == null){ - mTaskRecord.threadRecords = new ArrayList<>(); - } - initRecord(false); - } - handlerTaskRecord(mTaskRecord); - } - saveRecord(); - return mTaskRecord; - } - - /** - * convertDb 是兼容性代码 从3.4.1开始,线程配置信息将存储在数据库中。 将配置文件的内容复制到数据库中,并将配置文件删除 - */ - private void convertDb() { - List records = - DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", - getFilePath()); - if (records == null || records.size() == 0) { - Properties pro = FileUtil.loadConfig(mConfigFile); - if (pro.isEmpty()) { - ALog.d(TAG, "老版本的线程记录为空,任务为新任务"); - initRecord(true); - return; - } - - Set keys = pro.keySet(); - // 老版本记录是5s存一次,但是5s中内,如果线程执行完成,record记录是没有的,只有state记录... - // 第一步应该是record 和 state去重取正确的线程数 - Set set = new HashSet<>(); - for (Object key : keys) { - String str = String.valueOf(key); - int i = Integer.parseInt(str.substring(str.length() - 1)); - set.add(i); - } - int threadNum = set.size(); - if (threadNum == 0) { - ALog.d(TAG, "线程数为空,任务为新任务"); - initRecord(true); - return; - } - mTaskWrapper.setNewTask(false); - mTaskRecord = createTaskRecord(threadNum); - mTaskRecord.isBlock = false; - File tempFile = new File(getFilePath()); - for (int i = 0; i < threadNum; i++) { - ThreadRecord tRecord = new ThreadRecord(); - tRecord.taskKey = mTaskRecord.filePath; - Object state = pro.getProperty(tempFile.getName() + STATE + i); - Object record = pro.getProperty(tempFile.getName() + RECORD + i); - if (state != null && Integer.parseInt(String.valueOf(state)) == 1) { - tRecord.isComplete = true; - continue; - } - if (record != null) { - long temp = Long.parseLong(String.valueOf(record)); - tRecord.startLocation = temp > 0 ? temp : 0; - } else { - tRecord.startLocation = 0; - } - mTaskRecord.threadRecords.add(tRecord); - } - FileUtil.deleteFile(mConfigFile); - } - } - - /** - * 初始化任务记录,分配线程区间,如果任务记录不存在,则创建新的任务记录 - * - * @param newRecord {@code true} 需要创建新{@link TaskRecord} - */ - private void initRecord(boolean newRecord) { - if (newRecord) { - mTaskRecord = createTaskRecord(initTaskThreadNum()); - } - mTaskWrapper.setNewTask(true); - int requestType = mTaskWrapper.getRequestType(); - if (requestType == ITaskWrapper.M3U8_LIVE) { - return; - } - long blockSize = getFileSize() / mTaskRecord.threadNum; - // 处理线程区间记录 - for (int i = 0; i < mTaskRecord.threadNum; i++) { - long startL = i * blockSize, endL = (i + 1) * blockSize; - ThreadRecord tr = createThreadRecord(mTaskRecord, i, startL, endL); - mTaskRecord.threadRecords.add(tr); - } - } - - /** - * 保存任务记录 - */ - private void saveRecord() { - mTaskRecord.threadNum = mTaskRecord.threadRecords.size(); - mTaskRecord.save(); - if (mTaskRecord.threadRecords != null && !mTaskRecord.threadRecords.isEmpty()) { - DbEntity.saveAll(mTaskRecord.threadRecords); - } - ALog.d(TAG, String.format("保存记录,线程记录数:%s", mTaskRecord.threadRecords.size())); - } - - protected long getFileSize() { - return mFileSize; - } - - /** - * 获取任务路径 - * - * @return 任务文件路径 - */ - private String getFilePath() { - if (mEntity instanceof DownloadEntity) { - return ((DownloadEntity) mTaskWrapper.getEntity()).getFilePath(); - } else { - return ((UploadEntity) mTaskWrapper.getEntity()).getFilePath(); - } - } - - @Override public void accept(ILoaderVisitor visitor) { - visitor.addComponent(this); - } - - @Override public boolean checkTaskCompleted() { - if (mTaskRecord == null - || mTaskRecord.threadRecords == null - || mTaskRecord.threadRecords.isEmpty()) { - return false; - } - int completeNum = 0; - for (ThreadRecord tr : mTaskRecord.threadRecords) { - if (tr.isComplete) { - completeNum++; - } - } - return completeNum != 0 && completeNum == mTaskRecord.threadNum; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java deleted file mode 100644 index 365663a4..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java +++ /dev/null @@ -1,207 +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.common; - -import com.arialyy.aria.core.TaskRecord; -import com.arialyy.aria.core.ThreadRecord; -import com.arialyy.aria.core.loader.IRecordHandler; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.BufferedRandomAccessFile; -import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.FileUtil; -import java.io.File; -import java.io.IOException; - -/** - * 任务记录帮助类,用于处理统一的逻辑 - * - * @author lyy - * Date: 2019-09-19 - */ -public class RecordHelper { - private String TAG = CommonUtil.getClassName(getClass()); - - private AbsTaskWrapper mWrapper; - protected TaskRecord mTaskRecord; - - public RecordHelper(AbsTaskWrapper wrapper, TaskRecord record) { - mWrapper = wrapper; - mTaskRecord = record; - } - - /** - * 处理非分块的,多线程任务 - */ - public void handleMultiRecord() { - // 默认线程分块长度 - long blockSize = mWrapper.getEntity().getFileSize() / mTaskRecord.threadRecords.size(); - File temp = new File(mTaskRecord.filePath); - boolean fileExists = false; - if (!temp.exists()) { - createPlaceHolderFile(temp); - } else { - if (temp.length() != mWrapper.getEntity().getFileSize()) { - FileUtil.deleteFile(temp); - createPlaceHolderFile(temp); - } - fileExists = true; - } - // 处理文件被删除的情况 - if (!fileExists) { - ALog.w(TAG, String.format("文件【%s】被删除,重新分配线程区间", mTaskRecord.filePath)); - for (int i = 0; i < mTaskRecord.threadNum; i++) { - long startL = i * blockSize, endL = (i + 1) * blockSize; - ThreadRecord tr = mTaskRecord.threadRecords.get(i); - tr.startLocation = startL; - tr.isComplete = false; - - //最后一个线程的结束位置即为文件的总长度 - if (tr.threadId == (mTaskRecord.threadNum - 1)) { - endL = mWrapper.getEntity().getFileSize(); - } - tr.endLocation = endL; - } - } - //mWrapper.setNewTask(false); - } - - /** - * 创建非分块的占位文件 - */ - private void createPlaceHolderFile(File temp) { - BufferedRandomAccessFile tempFile; - try { - tempFile = new BufferedRandomAccessFile(temp, "rw"); - tempFile.setLength(mWrapper.getEntity().getFileSize()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * 处理分块任务的记录,分块文件(blockFileLen)长度必须需要小于等于线程区间(threadRectLen)的长度 - */ - public void handleBlockRecord() { - // 默认线程分块长度 - long normalRectLen = mWrapper.getEntity().getFileSize() / mTaskRecord.threadRecords.size(); - for (ThreadRecord tr : mTaskRecord.threadRecords) { - long threadRect = tr.blockLen; - - File temp = - new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, tr.threadId)); - if (!temp.exists()) { - ALog.i(TAG, String.format("分块文件【%s】不存在,该分块将重新开始", temp.getPath())); - tr.isComplete = false; - tr.startLocation = tr.threadId * normalRectLen; - } else { - if (!tr.isComplete) { - ALog.i(TAG, String.format( - "startLocation = %s; endLocation = %s; block = %s; tempLen = %s; threadId = %s", - tr.startLocation, tr.endLocation, threadRect, temp.length(), tr.threadId)); - - long blockFileLen = temp.length(); // 磁盘中的分块文件长度 - /* - * 检查磁盘中的分块文件 - */ - if (blockFileLen > threadRect) { - ALog.i(TAG, String.format("分块【%s】错误,分块长度【%s】 > 线程区间长度【%s】,将重新开始该分块", - tr.threadId, blockFileLen, threadRect)); - temp.delete(); - tr.startLocation = tr.threadId * threadRect; - continue; - } - - //正常情况下,该线程的startLocation的位置 - long realLocation = tr.threadId * normalRectLen + blockFileLen; - /* - * 检查记录文件 - */ - if (blockFileLen == threadRect && blockFileLen != 0) { - ALog.i(TAG, String.format("分块【%s】已完成,更新记录", temp.getPath())); - tr.startLocation = blockFileLen; - tr.isComplete = true; - } else if (tr.startLocation != realLocation) { // 处理记录小于分块文件长度的情况 - ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); - tr.startLocation = realLocation; - } else { - ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); - tr.startLocation = realLocation; - tr.isComplete = false; - } - } else { - ALog.i(TAG, String.format("分块【%s】已完成", temp.getPath())); - } - } - } - //mWrapper.setNewTask(false); - } - - /** - * 处理单线程的任务的记录 - */ - public void handleSingleThreadRecord() { - // mTaskRecord.isBlock是为了兼容以前的文件格式 - File file = new File( - mTaskRecord.isBlock ? String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, 0) - : mTaskRecord.filePath); - ThreadRecord tr = mTaskRecord.threadRecords.get(0); - if (!file.exists()) { - // 目标文件 - File targetFile = new File(mTaskRecord.filePath); - // 处理组合任务其中一个子任务完成的情况 - if (tr.isComplete - && targetFile.exists() - && targetFile.length() != 0 - && targetFile.length() == mWrapper.getEntity().getFileSize()) { - tr.isComplete = true; - } else { - ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); - tr.startLocation = 0; - tr.isComplete = false; - tr.endLocation = mWrapper.getEntity().getFileSize(); - } - } else if (file.length() > mWrapper.getEntity().getFileSize()) { - ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath())); - FileUtil.deleteFile(file); - tr.startLocation = 0; - tr.isComplete = false; - tr.endLocation = mWrapper.getEntity().getFileSize(); - } else if (file.length() != 0 && file.length() == mWrapper.getEntity().getFileSize()) { - ALog.d(TAG, "文件长度一致,线程完成"); - tr.isComplete = true; - } else { - if (file.length() != tr.startLocation) { - ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length())); - tr.startLocation = file.length(); - tr.isComplete = false; - } - } - //mWrapper.setNewTask(false); - } - - /** - * 处理不支持断点的记录 - */ - public void handleNoSupportBPRecord() { - ThreadRecord tr = mTaskRecord.threadRecords.get(0); - tr.startLocation = 0; - tr.endLocation = mWrapper.getEntity().getFileSize(); - tr.taskKey = mTaskRecord.filePath; - tr.blockLen = tr.endLocation; - tr.isComplete = false; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java deleted file mode 100644 index 3c49c334..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java +++ /dev/null @@ -1,113 +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.common; - -import android.os.Handler; -import com.arialyy.aria.core.AriaConfig; -import com.arialyy.aria.core.ThreadRecord; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import java.io.File; - -/** - * 子线程下载信息类 - */ -public class SubThreadConfig { - public static final int TYPE_HTTP = 1; - public static final int TYPE_FTP = 2; - public static final int TYPE_M3U8_PEER = 3; - public static final int TYPE_HTTP_DG_SUB = 4; - public static final int TYPE_FTP_DG_SUB = 5; - - public AbsTaskWrapper taskWrapper; - public boolean isBlock = false; - // 启动的线程 - public int startThreadNum; - // 真正的下载地址,如果是30x,则是30x后的地址 - public String url; - public File tempFile; - // 线程记录 - public ThreadRecord record; - // 状态处理器 - public Handler stateHandler; - // m3u8切片索引 - public int peerIndex; - // 线程任务类型 - public int threadType = TYPE_HTTP; - // 进度更新间隔,单位:毫秒 - public long updateInterval = 1000; - // 扩展数据 - public Object obj; - // 忽略失败 - public boolean ignoreFailure = false; - - /** - * 转换线程任务类型 - * - * @param requestType {@link AbsTaskWrapper#getRequestType()} - * @return {@link #threadType} - */ - public static int getThreadType(int requestType) { - int threadType = SubThreadConfig.TYPE_HTTP; - switch (requestType) { - case ITaskWrapper.D_HTTP: - case ITaskWrapper.U_HTTP: - threadType = SubThreadConfig.TYPE_HTTP; - break; - case ITaskWrapper.D_FTP: - case ITaskWrapper.U_FTP: - threadType = SubThreadConfig.TYPE_FTP; - break; - case ITaskWrapper.D_FTP_DIR: - threadType = SubThreadConfig.TYPE_FTP_DG_SUB; - break; - case ITaskWrapper.DG_HTTP: - threadType = SubThreadConfig.TYPE_HTTP_DG_SUB; - break; - case ITaskWrapper.M3U8_LIVE: - case ITaskWrapper.M3U8_VOD: - threadType = SubThreadConfig.TYPE_M3U8_PEER; - break; - } - return threadType; - } - - /** - * 根据配置肚脐更新间隔 - * - * @param requestType {@link AbsTaskWrapper#getRequestType()} - * @return {@link #updateInterval} - */ - public static long getUpdateInterval(int requestType) { - long updateInterval = 1000; - switch (requestType) { - case ITaskWrapper.D_HTTP: - case ITaskWrapper.D_FTP: - case ITaskWrapper.M3U8_LIVE: - case ITaskWrapper.M3U8_VOD: - updateInterval = AriaConfig.getInstance().getDConfig().getUpdateInterval(); - break; - case ITaskWrapper.D_FTP_DIR: - case ITaskWrapper.DG_HTTP: - updateInterval = AriaConfig.getInstance().getDGConfig().getUpdateInterval(); - break; - case ITaskWrapper.U_HTTP: - case ITaskWrapper.U_FTP: - updateInterval = AriaConfig.getInstance().getUConfig().getUpdateInterval(); - } - return updateInterval; - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/TaskOption.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/common/TaskOption.kt similarity index 95% rename from PublicComponent/src/main/java/com/arialyy/aria/core/download/TaskOption.kt rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/TaskOption.kt index 361c2b9a..29f6b627 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/TaskOption.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/TaskOption.kt @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download +package com.arialyy.aria.core.common import android.net.Uri import com.arialyy.aria.core.inf.ITaskOption diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/AbsGroupTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/AbsGroupTaskWrapper.java deleted file mode 100644 index f3055b6c..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/AbsGroupTaskWrapper.java +++ /dev/null @@ -1,48 +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; - -import com.arialyy.aria.core.common.AbsEntity; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import java.util.List; - -/** - * 组合任务实体包裹器,用于加载和任务相关的参数,如:组合任务实体{@link DownloadGroupEntity} - */ -public abstract class AbsGroupTaskWrapper - extends AbsTaskWrapper { - - public AbsGroupTaskWrapper(ENTITY entity) { - super(entity); - } - - public abstract List getSubTaskWrapper(); - - public abstract void setSubTaskWrapper(List subTaskWrapper); - - /** - * {@code true} 忽略任务冲突,不考虑组任务hash冲突的情况 - */ - private boolean ignoreTaskOccupy = false; - - public boolean isIgnoreTaskOccupy() { - return ignoreTaskOccupy; - } - - public void setIgnoreTaskOccupy(boolean ignoreTaskOccupy) { - this.ignoreTaskOccupy = ignoreTaskOccupy; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java deleted file mode 100644 index 0e63a544..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java +++ /dev/null @@ -1,44 +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; - -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.orm.AbsDbWrapper; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Wrapper; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/30. - * 任务组实体和子任务实体的关系 - */ -@Wrapper -public class DGEntityWrapper extends AbsDbWrapper { - - @One - public DownloadGroupEntity groupEntity; - - @Many(parentColumn = "groupHash", entityColumn = "groupHash") - public List subEntity; - - @Override protected void handleConvert() { - if (subEntity != null && !subEntity.isEmpty()) { - groupEntity.setSubEntities(subEntity); - } - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java deleted file mode 100644 index 2df6b1fd..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java +++ /dev/null @@ -1,89 +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; - -import com.arialyy.aria.core.config.Configuration; -import com.arialyy.aria.core.config.DGroupConfig; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by AriaL on 2017/7/1. 任务组的任务实体修饰器 - */ -public class DGTaskWrapper extends AbsGroupTaskWrapper { - - private List subWrappers; - - private boolean unknownSize = false; - - /** - * 保存临时设置的文件夹路径 - */ - private String dirPathTemp; - - /** - * 子任务文件名 - */ - private List subNameTemp = new ArrayList<>(); - - public DGTaskWrapper(DownloadGroupEntity entity) { - super(entity); - } - - public List getSubNameTemp() { - return subNameTemp; - } - - public void setSubNameTemp(List subNameTemp) { - this.subNameTemp = subNameTemp; - } - - public String getDirPathTemp() { - return dirPathTemp; - } - - public void setDirPathTemp(String mDirPathTemp) { - this.dirPathTemp = mDirPathTemp; - } - - @Override - public void setSubTaskWrapper(List subTaskEntities) { - this.subWrappers = subTaskEntities; - } - - public boolean isUnknownSize() { - return unknownSize; - } - - public void setUnknownSize(boolean unknownSize) { - this.unknownSize = unknownSize; - } - - @Override public String getKey() { - return getEntity().getKey(); - } - - @Override public DGroupConfig getConfig() { - return Configuration.getInstance().dGroupCfg; - } - - @Override public List getSubTaskWrapper() { - if (subWrappers == null) { - subWrappers = new ArrayList<>(); - } - return subWrappers; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java deleted file mode 100644 index 041d8f64..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java +++ /dev/null @@ -1,122 +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; - -import com.arialyy.aria.core.TaskOptionParams; -import com.arialyy.aria.core.config.Configuration; -import com.arialyy.aria.core.config.DownloadConfig; -import com.arialyy.aria.core.inf.ITaskOption; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.util.ComponentUtil; - -/** - * Created by lyy on 2017/1/23. 下载任务实体和下载实体为一对一关系,下载实体删除,任务实体自动删除 - */ -public class DTaskWrapper extends AbsTaskWrapper { - - /** - * 所属的任务组组名,如果不属于任务组,则为null - */ - private String groupHash; - - /** - * 该任务是否属于任务组 - */ - private boolean isGroupTask = false; - - /** - * M3u8任务配置信息 - */ - private ITaskOption m3u8Option; - - private TaskOptionParams m3u8Params = new TaskOptionParams(); - - /** - * 文件下载url的临时保存变量 - */ - private String mTempUrl; - - /** - * 文件保存路径的临时变量 - */ - private String mTempFilePath; - - public DTaskWrapper(DownloadEntity entity) { - super(entity); - } - - public ITaskOption getM3u8Option() { - return m3u8Option; - } - - public void generateM3u8Option(Class clazz) { - m3u8Option = ComponentUtil.getInstance().buildTaskOption(clazz, m3u8Params); - } - - public TaskOptionParams getM3U8Params() { - if (m3u8Params == null) { - m3u8Params = new TaskOptionParams(); - } - return m3u8Params; - } - - /** - * Task实体对应的key,下载url - */ - @Override public String getKey() { - return getEntity().getKey(); - } - - @Override public DownloadConfig getConfig() { - if (isGroupTask) { - return Configuration.getInstance().dGroupCfg.getSubConfig(); - } else { - return Configuration.getInstance().downloadCfg; - } - } - - public String getGroupHash() { - return groupHash; - } - - public boolean isGroupTask() { - return isGroupTask; - } - - public void setGroupHash(String groupHash) { - this.groupHash = groupHash; - } - - public void setGroupTask(boolean groupTask) { - isGroupTask = groupTask; - } - - public String getTempUrl() { - return mTempUrl; - } - - public void setTempUrl(String mTempUrl) { - this.mTempUrl = mTempUrl; - } - - public String getTempFilePath() { - return mTempFilePath; - } - - public void setTempFilePath(String mTempFilePath) { - this.mTempFilePath = mTempFilePath; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java deleted file mode 100644 index 974209f2..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ /dev/null @@ -1,214 +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; - -import android.os.Parcel; -import android.os.Parcelable; -import android.text.TextUtils; -import com.arialyy.aria.core.common.AbsNormalEntity; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.orm.annotation.Ignore; -import com.arialyy.aria.orm.annotation.Unique; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; - -/** - * Created by lyy on 2015/12/25. - * 下载实体 - */ -public class DownloadEntity extends AbsNormalEntity implements Parcelable, Cloneable { - @Unique private String downloadPath; //保存路径 - - /** - * 所属任务组 - */ - private String groupHash; - - /** - * 从服务器的返回信息中获取的文件md5信息,如果服务器没有返回,则不会设置该信息 - * 如果你已经设置了该任务的MD5信息,Aria也不会从服务器返回的信息中获取该信息 - */ - private String md5Code; - - /** - * 从服务器的返回信息中获取的文件描述信息 - */ - private String disposition; - - /** - * 从disposition获取到的文件名,如果可以获取到,则会赋值到这个字段 - */ - private String serverFileName; - - @Ignore - private M3U8Entity m3U8Entity; - - /** - * 获取m3u8数据信息 - * - * @return 如果m3u8信息为空,则返回null - */ - public M3U8Entity getM3U8Entity() { - if (TextUtils.isEmpty(downloadPath)) { - ALog.e("DownloadEntity", "文件保存路径为空,获取m3u8实体之前需要设置文件保存路径"); - return null; - } - if (m3U8Entity == null) { - m3U8Entity = DbEntity.findFirst(M3U8Entity.class, "filePath=?", downloadPath); - } - return m3U8Entity; - } - - /** - * 设置进来的地址,如果需要获取真实的下载地址,请使用{@link #getRealUrl()} - */ - @Override public String getKey() { - return getUrl(); - } - - public String getRealUrl(){ - return isRedirect() ? getRedirectUrl() : getUrl(); - } - - @Override public int getTaskType() { - int type; - if (getUrl() == null) { - type = ITaskWrapper.ERROR; - } else if (getUrl().startsWith("http")) { - M3U8Entity temp = getM3U8Entity(); - if (temp == null) { - type = ITaskWrapper.D_HTTP; - } else { - type = temp.isLive() ? ITaskWrapper.M3U8_LIVE : ITaskWrapper.M3U8_VOD; - } - } else if (getUrl().startsWith("ftp")) { - type = ITaskWrapper.D_FTP; - } else if (getUrl().startsWith("sftp")) { - type = ITaskWrapper.D_SFTP; - } else { - type = ITaskWrapper.ERROR; - } - return type; - } - - public DownloadEntity() { - } - - public String getMd5Code() { - return md5Code; - } - - public void setMd5Code(String md5Code) { - this.md5Code = md5Code; - } - - public String getDisposition() { - return TextUtils.isEmpty(disposition) ? "" : CommonUtil.decryptBASE64(disposition); - } - - public void setDisposition(String disposition) { - this.disposition = disposition; - } - - public String getServerFileName() { - return serverFileName; - } - - public void setServerFileName(String serverFileName) { - this.serverFileName = serverFileName; - } - - public String getGroupHash() { - return groupHash; - } - - public void setGroupHash(String groupHash) { - this.groupHash = groupHash; - } - - @Override - public String getFilePath() { - return downloadPath; - } - - public DownloadEntity setFilePath(String filePath) { - this.downloadPath = filePath; - return this; - } - - @Override public DownloadEntity clone() throws CloneNotSupportedException { - return (DownloadEntity) super.clone(); - } - - @Override public String toString() { - return "DownloadEntity{" - + "downloadPath='" - + downloadPath - + '\'' - + ", groupHash='" - + groupHash - + '\'' - + ", fileName='" - + getFileName() - + '\'' - + ", md5Code='" - + md5Code - + '\'' - + ", disposition='" - + disposition - + '\'' - + ", serverFileName='" - + serverFileName - + '\'' - + '}'; - } - - @Override public int describeContents() { - return 0; - } - - @Override public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeString(this.downloadPath); - dest.writeString(this.groupHash); - dest.writeString(this.md5Code); - dest.writeString(this.disposition); - dest.writeString(this.serverFileName); - dest.writeParcelable(this.m3U8Entity, flags); - } - - protected DownloadEntity(Parcel in) { - super(in); - this.downloadPath = in.readString(); - this.groupHash = in.readString(); - this.md5Code = in.readString(); - this.disposition = in.readString(); - this.serverFileName = in.readString(); - this.m3U8Entity = in.readParcelable(M3U8Entity.class.getClassLoader()); - } - - public static final Creator CREATOR = new Creator() { - @Override public DownloadEntity createFromParcel(Parcel source) { - return new DownloadEntity(source); - } - - @Override public DownloadEntity[] newArray(int size) { - return new DownloadEntity[size]; - } - }; -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java deleted file mode 100644 index 9aa6f10e..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java +++ /dev/null @@ -1,86 +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; - -import android.os.Parcel; -import android.text.TextUtils; -import com.arialyy.aria.core.common.AbsGroupEntity; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.annotation.Ignore; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by AriaL on 2017/6/29. 下载任务组实体 - */ -public class DownloadGroupEntity extends AbsGroupEntity { - - @Ignore private List subEntities; - - /** - * 子任务实体列表 - */ - public List getSubEntities() { - if (subEntities == null) { - subEntities = new ArrayList<>(); - } - return subEntities; - } - - public void setSubEntities(List subTasks) { - this.subEntities = subTasks; - } - - public void setGroupHash(String key) { - this.groupHash = key; - } - - @Override public int getTaskType() { - if (getSubEntities() == null || getSubEntities().isEmpty() || TextUtils.isEmpty( - getSubEntities().get(0).getUrl())) { - return ITaskWrapper.ERROR; - } - return (groupHash.startsWith("ftp") || groupHash.startsWith("sftp")) ? ITaskWrapper.D_FTP_DIR - : ITaskWrapper.DG_HTTP; - } - - public DownloadGroupEntity() { - } - - @Override public int describeContents() { - return 0; - } - - @Override public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeTypedList(this.subEntities); - } - - protected DownloadGroupEntity(Parcel in) { - super(in); - this.subEntities = in.createTypedArrayList(DownloadEntity.CREATOR); - } - - public static final Creator CREATOR = new Creator() { - @Override public DownloadGroupEntity createFromParcel(Parcel source) { - return new DownloadGroupEntity(source); - } - - @Override public DownloadGroupEntity[] newArray(int size) { - return new DownloadGroupEntity[size]; - } - }; -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java index 4430a471..a8a82a90 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java @@ -52,7 +52,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { private boolean isStop = false, isCancel = false; private Handler mScheduler; private SimpleSubQueue mSubQueue = SimpleSubQueue.newInstance(); - private Map mExeLoader = new WeakHashMap<>(); + private Map mExeLoader = new WeakHashMap<>(); private Map mCache = new WeakHashMap<>(); private DGTaskWrapper mGTWrapper; private GroupRunState mState; @@ -75,7 +75,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { * * @param needGetFileInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - protected abstract AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo); + protected abstract AbsSubDLoadAdapter createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo); protected IDGroupListener getListener() { return mListener; @@ -156,7 +156,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { if (!mState.isRunning.get()) { startTimer(); } - AbsSubDLoadUtil d = getDownloader(url, false); + AbsSubDLoadAdapter d = getDownloader(url, false); if (d != null && !d.isRunning()) { mSubQueue.startTask(d); } @@ -171,7 +171,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { if (!checkSubTask(url, "停止")) { return; } - AbsSubDLoadUtil d = getDownloader(url, false); + AbsSubDLoadAdapter d = getDownloader(url, false); if (d != null && d.isRunning()) { mSubQueue.stopTask(d); } @@ -203,8 +203,8 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { * * @param url 子任务下载地址 */ - private AbsSubDLoadUtil getDownloader(String url, boolean needGetFileInfo) { - AbsSubDLoadUtil d = mExeLoader.get(url); + private AbsSubDLoadAdapter getDownloader(String url, boolean needGetFileInfo) { + AbsSubDLoadAdapter d = mExeLoader.get(url); if (d == null) { return createSubLoader(mCache.get(url), needGetFileInfo); } @@ -311,7 +311,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { /** * 启动子任务下载器 */ - protected void startSubLoader(AbsSubDLoadUtil loader) { + protected void startSubLoader(AbsSubDLoadAdapter loader) { mExeLoader.put(loader.getKey(), loader); mSubQueue.startTask(loader); } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderAdapter.java similarity index 92% rename from PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderAdapter.java index f5236eee..8835020b 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderAdapter.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.group; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; @@ -26,7 +26,7 @@ import com.arialyy.aria.util.CommonUtil; * Created by AriaL on 2017/6/30. * 任务组核心逻辑 */ -public abstract class AbsGroupLoaderUtil implements ITaskUtil { +public abstract class AbsGroupLoaderAdapter implements ITaskAdapter { protected String TAG = CommonUtil.getClassName(getClass()); private IEventListener mListener; @@ -35,7 +35,7 @@ public abstract class AbsGroupLoaderUtil implements ITaskUtil { private boolean isStop = false, isCancel = false; - @Override public ITaskUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { + @Override public ITaskAdapter setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { mTaskWrapper = taskWrapper; mListener = listener; mLoader = getLoader(); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadAdapter.java similarity index 92% rename from PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadAdapter.java index 3635ddd8..66ef66c2 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadAdapter.java @@ -19,7 +19,7 @@ import android.os.Handler; import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.loader.LoaderStructure; @@ -31,7 +31,7 @@ import com.arialyy.aria.util.CommonUtil; /** * 子任务下载器工具,需要在线程池中执行 */ -public abstract class AbsSubDLoadUtil implements ITaskUtil, Runnable { +public abstract class AbsSubDLoadAdapter implements ITaskAdapter, Runnable { protected final String TAG = CommonUtil.getClassName(getClass()); protected SubLoader mDLoader; @@ -45,13 +45,13 @@ public abstract class AbsSubDLoadUtil implements ITaskUtil, Runnable { * @param schedulers 调度器 * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - protected AbsSubDLoadUtil(Handler schedulers, boolean needGetInfo, String parentKey) { + protected AbsSubDLoadAdapter(Handler schedulers, boolean needGetInfo, String parentKey) { mSchedulers = schedulers; this.parentKey = parentKey; this.needGetInfo = needGetInfo; } - @Override public ITaskUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { + @Override public ITaskAdapter setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { mWrapper = (DTaskWrapper) taskWrapper; mDLoader = getLoader(); return this; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java index 947a4b71..5a914ea9 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.group; import com.arialyy.aria.core.loader.AbsNormalLoader; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.config.DGroupConfig; /** @@ -24,7 +24,7 @@ import com.arialyy.aria.core.config.DGroupConfig; * * @param {@link AbsNormalLoader}下载器 */ -interface ISubQueue { +interface ISubQueue { /** * 添加任务 diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java index 4cb0995e..a984f5e7 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java @@ -34,7 +34,7 @@ import java.io.File; /** * 组合任务子任务调度器,用于调度任务的开始、停止、失败、完成等情况 - * 该调度器生命周期和{@link AbsGroupLoaderUtil}生命周期一致 + * 该调度器生命周期和{@link AbsGroupLoaderAdapter}生命周期一致 */ final class SimpleSchedulers implements Handler.Callback { private final String TAG = CommonUtil.getClassName(this); @@ -59,7 +59,7 @@ final class SimpleSchedulers implements Handler.Callback { return true; } String threadName = b.getString(IBlockManager.DATA_THREAD_NAME); - AbsSubDLoadUtil loaderUtil = mQueue.getLoaderUtil(threadName); + AbsSubDLoadAdapter loaderUtil = mQueue.getLoaderUtil(threadName); if (loaderUtil == null) { ALog.e(TAG, String.format("子任务loader不存在,state:%s,key:%s", msg.what, threadName)); return true; @@ -104,7 +104,7 @@ final class SimpleSchedulers implements Handler.Callback { * * @param needRetry true 需要重试,false 不需要重试 */ - private synchronized void handleFail(final AbsSubDLoadUtil loaderUtil, boolean needRetry) { + private synchronized void handleFail(final AbsSubDLoadAdapter loaderUtil, boolean needRetry) { Log.d(TAG, String.format("handleFail, size = %s, completeNum = %s, failNum = %s, stopNum = %s", mGState.getSubSize(), mGState.getCompleteNum(), mGState.getFailNum(), mGState.getSubSize())); @@ -149,7 +149,7 @@ final class SimpleSchedulers implements Handler.Callback { * 1、所有的子任务已经停止,则认为组合任务停止 * 2、completeNum + failNum + stopNum = subSize,则认为组合任务停止 */ - private synchronized void handleStop(AbsSubDLoadUtil loadUtil, long curLocation) { + private synchronized void handleStop(AbsSubDLoadAdapter loadUtil, long curLocation) { Log.d(TAG, String.format("handleStop, size = %s, completeNum = %s, failNum = %s, stopNum = %s", mGState.getSubSize(), mGState.getCompleteNum(), mGState.getFailNum(), mGState.getSubSize())); @@ -177,7 +177,7 @@ final class SimpleSchedulers implements Handler.Callback { * GroupRunState#getFailNum()}不为0,则认为组合任务被停止 * 3、只有有缓存的子任务,则任务组合任务没有完成 */ - private synchronized void handleComplete(AbsSubDLoadUtil loader) { + private synchronized void handleComplete(AbsSubDLoadAdapter loader) { ALog.d(TAG, String.format("子任务【%s】完成", loader.getEntity().getFileName())); Log.d(TAG, String.format("handleComplete, size = %s, completeNum = %s, failNum = %s, stopNum = %s", @@ -219,7 +219,7 @@ final class SimpleSchedulers implements Handler.Callback { if (mQueue.isStopAll()) { return; } - AbsSubDLoadUtil next = mQueue.getNextTask(); + AbsSubDLoadAdapter next = mQueue.getNextTask(); if (next != null) { ALog.d(TAG, String.format("启动任务:%s", next.getEntity().getFileName())); mQueue.startTask(next); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java index 811df698..ae3c4809 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java @@ -27,18 +27,18 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** - * 组合任务队列,该队列生命周期和{@link AbsGroupLoaderUtil}生命周期一致 + * 组合任务队列,该队列生命周期和{@link AbsGroupLoaderAdapter}生命周期一致 */ -final class SimpleSubQueue implements ISubQueue { +final class SimpleSubQueue implements ISubQueue { private final String TAG = CommonUtil.getClassName(getClass()); /** * 缓存下载器 */ - private Map mCache = new ConcurrentHashMap<>(); + private Map mCache = new ConcurrentHashMap<>(); /** * 执行中的下载器 */ - private Map mExec = new ConcurrentHashMap<>(); + private Map mExec = new ConcurrentHashMap<>(); /** * 最大执行任务数 @@ -58,8 +58,8 @@ final class SimpleSubQueue implements ISubQueue { return new SimpleSubQueue(); } - synchronized AbsSubDLoadUtil getLoaderUtil(String key) { - AbsSubDLoadUtil sub = mExec.get(key); + synchronized AbsSubDLoadAdapter getLoaderUtil(String key) { + AbsSubDLoadAdapter sub = mExec.get(key); if (sub != null) { return sub; } @@ -81,11 +81,11 @@ final class SimpleSubQueue implements ISubQueue { return isStopAll; } - @Override public void addTask(AbsSubDLoadUtil fileer) { + @Override public void addTask(AbsSubDLoadAdapter fileer) { mCache.put(fileer.getKey(), fileer); } - @Override public void startTask(AbsSubDLoadUtil fileer) { + @Override public void startTask(AbsSubDLoadAdapter fileer) { if (mExec.size() < mMaxExecSize) { mCache.remove(fileer.getKey()); mExec.put(fileer.getKey(), fileer); @@ -98,7 +98,7 @@ final class SimpleSubQueue implements ISubQueue { addTask(fileer); } - @Override public void stopTask(AbsSubDLoadUtil fileer) { + @Override public void stopTask(AbsSubDLoadAdapter fileer) { fileer.stop(); mExec.remove(fileer.getKey()); } @@ -109,7 +109,7 @@ final class SimpleSubQueue implements ISubQueue { mCache.clear(); Set keys = mExec.keySet(); for (String key : keys) { - AbsSubDLoadUtil loader = mExec.get(key); + AbsSubDLoadAdapter loader = mExec.get(key); if (loader != null) { ALog.d(TAG, String.format("停止子任务:%s", loader.getEntity().getFileName())); loader.stop(); @@ -133,7 +133,7 @@ final class SimpleSubQueue implements ISubQueue { if (oldSize < num) { // 处理队列变小的情况,该情况下将停止队尾任务,并将这些任务添加到缓存队列中 if (mExec.size() > num) { Set keys = mExec.keySet(); - List caches = new ArrayList<>(); + List caches = new ArrayList<>(); int i = 0; for (String key : keys) { if (i > num) { @@ -141,12 +141,12 @@ final class SimpleSubQueue implements ISubQueue { } i++; } - Collection temp = mCache.values(); + Collection temp = mCache.values(); mCache.clear(); - for (AbsSubDLoadUtil cache : caches) { + for (AbsSubDLoadAdapter cache : caches) { addTask(cache); } - for (AbsSubDLoadUtil t : temp) { + for (AbsSubDLoadAdapter t : temp) { addTask(t); } } @@ -155,7 +155,7 @@ final class SimpleSubQueue implements ISubQueue { // 处理队列变大的情况,该情况下将增加任务 if (mExec.size() < num) { for (int i = 0; i < diff; i++) { - AbsSubDLoadUtil next = getNextTask(); + AbsSubDLoadAdapter next = getNextTask(); if (next != null) { startTask(next); } else { @@ -165,11 +165,11 @@ final class SimpleSubQueue implements ISubQueue { } } - @Override public void removeTaskFromExecQ(AbsSubDLoadUtil fileer) { + @Override public void removeTaskFromExecQ(AbsSubDLoadAdapter fileer) { mExec.remove(fileer.getKey()); } - @Override public void removeTask(AbsSubDLoadUtil fileer) { + @Override public void removeTask(AbsSubDLoadAdapter fileer) { removeTaskFromExecQ(fileer); mCache.remove(fileer.getKey()); } @@ -178,7 +178,7 @@ final class SimpleSubQueue implements ISubQueue { ALog.d(TAG, "删除组合任务"); Set keys = mExec.keySet(); for (String key : keys) { - AbsSubDLoadUtil loader = mExec.get(key); + AbsSubDLoadAdapter loader = mExec.get(key); if (loader != null) { ALog.d(TAG, String.format("停止子任务:%s", loader.getEntity().getFileName())); loader.cancel(); @@ -186,7 +186,7 @@ final class SimpleSubQueue implements ISubQueue { } } - @Override public AbsSubDLoadUtil getNextTask() { + @Override public AbsSubDLoadAdapter getNextTask() { Iterator keys = mCache.keySet().iterator(); if (keys.hasNext()) { return mCache.get(keys.next()); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubRetryQueue.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubRetryQueue.java index 0d95cd05..3d8cb2c2 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubRetryQueue.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubRetryQueue.java @@ -43,7 +43,7 @@ final class SimpleSubRetryQueue { } - void offer(AbsSubDLoadUtil subDLoadUtil) { + void offer(AbsSubDLoadAdapter subDLoadUtil) { pool.submit(subDLoadUtil.getLoader()); } } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java similarity index 97% rename from PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java index bab111e6..4aac52f5 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskAdapter.java @@ -24,7 +24,7 @@ import org.jetbrains.annotations.NotNull; * Created by lyy on 2016/10/31. * 任务功能接口 */ -public interface ITaskUtil { +public interface ITaskAdapter { void init(@NotNull ITask task, @NotNull IEventListener listener); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java deleted file mode 100644 index e7e28a3b..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java +++ /dev/null @@ -1,60 +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.listener; - -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.task.DownloadTask; -import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.DeleteDRecord; - -/** - * 下载监听类 - */ -public class BaseDListener extends AbsEventListener implements IDLoadListener { - - @Override - public void onPostPre(long fileSize) { - mEntity.setFileSize(fileSize); - mEntity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); - saveData(IEntity.STATE_POST_PRE, -1); - sendInState2Target(ISchedulers.POST_PRE); - } - - @Override - public void supportBreakpoint(boolean support) { - if (!support) { - sendInState2Target(ISchedulers.NO_SUPPORT_BREAK_POINT); - } - } - - @Override protected void handleCancel() { - int sType = getTask(DownloadTask.class).getSchedulerType(); - if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { - mEntity.setComplete(false); - mEntity.setState(IEntity.STATE_WAIT); - - DeleteDRecord.getInstance().deleteRecord(mEntity, mTaskWrapper.isRemoveFile(), false); - //RecordUtil.delTaskRecord(mEntity.getFilePath(), IRecordHandler.TYPE_DOWNLOAD, - // mTaskWrapper.isRemoveFile(), false); - } else { - //RecordUtil.delTaskRecord(mEntity.getFilePath(), IRecordHandler.TYPE_DOWNLOAD, - // mTaskWrapper.isRemoveFile(), true); - - DeleteDRecord.getInstance().deleteRecord(mEntity, mTaskWrapper.isRemoveFile(), true); - } - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseUListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseUListener.java deleted file mode 100644 index d1f23c90..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseUListener.java +++ /dev/null @@ -1,38 +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.listener; - -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.task.UploadTask; -import com.arialyy.aria.util.DeleteURecord; - -/** - * 下载监听类 - */ -public class BaseUListener extends AbsEventListener implements IUploadListener { - - @Override protected void handleCancel() { - int sType = getTask(UploadTask.class).getSchedulerType(); - if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { - mEntity.setComplete(false); - mEntity.setState(IEntity.STATE_WAIT); - DeleteURecord.getInstance().deleteRecord(mEntity, mTaskWrapper.isRemoveFile(), false); - } else { - DeleteURecord.getInstance().deleteRecord(mEntity, mTaskWrapper.isRemoveFile(), true); - } - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderAdapter.java similarity index 92% rename from PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderAdapter.java index cc04d52d..e982f040 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderAdapter.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.loader; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.exception.AriaException; @@ -27,17 +27,17 @@ import com.arialyy.aria.util.CommonUtil; * Created by lyy on 2015/8/25. * HTTP\FTP单任务下载工具 */ -public abstract class AbsNormalLoaderUtil implements ITaskUtil { +public abstract class AbsNormalLoaderAdapter implements ITaskAdapter { protected String TAG = CommonUtil.getClassName(getClass()); private IEventListener mListener; protected AbsNormalLoader mLoader; private AbsTaskWrapper mTaskWrapper; private boolean isStop = false, isCancel = false; - protected AbsNormalLoaderUtil() { + protected AbsNormalLoaderAdapter() { } - @Override public ITaskUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { + @Override public ITaskAdapter setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { mTaskWrapper = taskWrapper; mListener = listener; mLoader = getLoader(); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java index 7810aa41..67d0e0b1 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.processor; -import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.common.M3U8Entity; import com.arialyy.aria.core.inf.IEventHandler; import java.util.List; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java index 2eac0864..643461f2 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java @@ -16,9 +16,8 @@ package com.arialyy.aria.core.task; import android.text.TextUtils; -import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.inf.ITaskOption; -import com.arialyy.aria.core.inf.ITaskUtil; import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.util.CommonUtil; import java.util.HashMap; @@ -31,7 +30,7 @@ import timber.log.Timber; public abstract class AbsTask implements ITask { protected ITaskOption mTaskOption; private boolean isCancel = false, isStop = false; - private ITaskUtil mUtil; + private ITaskAdapter mAdapter; /** * 该任务的调度类型 */ @@ -40,20 +39,23 @@ public abstract class AbsTask implements ITask { private int taskId = -1; private final Map mExpand = new HashMap<>(); - protected AbsTask(ITaskOption taskOption, ITaskUtil util) { + protected AbsTask(ITaskOption taskOption) { mTaskOption = taskOption; - mUtil = util; taskId = TaskStatePool.INSTANCE.buildTaskId$PublicComponent_debug(); TaskStatePool.INSTANCE.putTaskState(getTaskId(), mTaskState); - util.init(this, taskOption.eventListener); } - @Override public void setState(int state) { - mTaskState.setState(state); + public void setAdapter(ITaskAdapter adapter) { + mAdapter = adapter; + mAdapter.init(this, mTaskOption.eventListener); + } + + @Override public ITaskAdapter getAdapter() { + return mAdapter; } - ITaskUtil getTaskUtil() { - return mUtil; + @Override public void setState(int state) { + mTaskState.setState(state); } @Override public T getTaskOption(Class clazz) { @@ -66,7 +68,6 @@ public abstract class AbsTask implements ITask { /** * 获取剩余时间,单位:s - * 如果是m3u8任务,无法获取剩余时间;m2u8任务如果需要获取剩余时间,请设置文件长度{@link AbsEntity#setFileSize(long)} */ public int getTimeLeft() { return mTaskState.getTimeLeft(); @@ -152,47 +153,44 @@ public abstract class AbsTask implements ITask { @Override public void start(int type) { mSchedulerType = type; - mUtil = getTaskUtil(); - if (mUtil == null) { + if (mAdapter == null) { Timber.e("util is null"); return; } if (type == TaskSchedulerType.TYPE_START_AND_RESET_STATE) { - if (getTaskUtil().isRunning()) { + if (mAdapter.isRunning()) { Timber.e("task restart fail"); return; } - mUtil.start(); + mAdapter.start(); Timber.e("task restart success"); return; } - if (getTaskUtil().isRunning()) { + if (mAdapter.isRunning()) { Timber.d("task is running"); return; } - getTaskUtil().start(); + mAdapter.start(); } @Override public void stop(int type) { - mUtil = getTaskUtil(); - if (mUtil == null) { + if (mAdapter == null) { Timber.e("util is null"); return; } isStop = true; mSchedulerType = type; - getTaskUtil().stop(); + mAdapter.stop(); } @Override public void cancel(int type) { - mUtil = getTaskUtil(); - if (mUtil == null) { + if (mAdapter == null) { Timber.e("util is null"); return; } isCancel = true; mSchedulerType = type; - getTaskUtil().cancel(); + mAdapter.cancel(); } /** @@ -201,7 +199,7 @@ public abstract class AbsTask implements ITask { * @return {@code true} 正在下载 */ @Override public boolean isRunning() { - return getTaskUtil().isRunning(); + return mAdapter.isRunning(); } /** diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTaskUtil.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTaskAdapter.kt similarity index 95% rename from PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTaskUtil.kt rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTaskAdapter.kt index 676b56b5..6c104a06 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTaskUtil.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTaskAdapter.kt @@ -16,7 +16,7 @@ package com.arialyy.aria.core.task import com.arialyy.aria.core.inf.IBlockManager -import com.arialyy.aria.core.inf.ITaskUtil +import com.arialyy.aria.core.inf.ITaskAdapter import com.arialyy.aria.core.listener.IEventListener import com.arialyy.aria.core.task.ITaskInterceptor.IChain @@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.ITaskInterceptor.IChain * @Description * @Date 1:12 PM 2023/1/28 **/ -abstract class AbsTaskUtil : ITaskUtil { +abstract class AbsTaskAdapter : ITaskAdapter { private lateinit var mTask: ITask private lateinit var mEventListener: IEventListener diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java index 9fa2b650..380476a2 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.task; import com.arialyy.aria.core.download.DTaskOption; import com.arialyy.aria.core.inf.ITaskOption; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.inf.ITaskAdapter; import java.util.Objects; /** @@ -26,7 +26,7 @@ import java.util.Objects; */ public class DownloadGroupTask extends AbsTask { - protected DownloadGroupTask(ITaskOption taskOption, ITaskUtil util) { + protected DownloadGroupTask(ITaskOption taskOption, ITaskAdapter util) { super(taskOption, util); } 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 06d198b2..bc71ce16 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 @@ -17,8 +17,7 @@ package com.arialyy.aria.core.task; import android.net.Uri; -import com.arialyy.aria.core.download.TaskOption; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.common.TaskOption; import java.util.Objects; /** @@ -27,8 +26,8 @@ import java.util.Objects; */ public class DownloadTask extends AbsTask { - public DownloadTask(TaskOption taskOption, ITaskUtil util) { - super(taskOption, util); + public DownloadTask(TaskOption taskOption) { + super(taskOption); } public Uri getSavePath() { 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 ce383c18..a1f38af1 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 @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.task; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.inf.ITaskOption; import com.arialyy.aria.core.inf.TaskSchedulerType; @@ -52,6 +53,8 @@ public interface ITask { */ int OTHER = -1; + ITaskAdapter getAdapter(); + /** * 获取任务类型 * 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 152e6c22..c4ed8e54 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 @@ -18,7 +18,7 @@ package com.arialyy.aria.core.task import android.net.Uri import com.arialyy.aria.core.inf.BaseEntity import com.arialyy.aria.core.inf.IEntity -import com.arialyy.aria.core.inf.ITaskUtil +import com.arialyy.aria.core.inf.ITaskAdapter import timber.log.Timber import java.util.concurrent.ConcurrentHashMap @@ -32,7 +32,7 @@ object TaskCachePool { * key: taskId */ private val entityMap = ConcurrentHashMap() - private val taskUtilMap = ConcurrentHashMap() + private val taskAdapterMap = ConcurrentHashMap() private val taskMap = ConcurrentHashMap() fun removeTask(taskId: Int) { @@ -46,20 +46,34 @@ object TaskCachePool { fun putTask(task: ITask) { taskMap[task.taskId] = task + taskAdapterMap[Uri.parse(task.filePath)] = task.adapter } /** - * @param filePath task unique identifier, like: savePath, sourceUrl + * find task by filePath */ - fun putTaskUtil(filePath: Uri, taskUtil: ITaskUtil) { - taskUtilMap[filePath] = taskUtil + fun findTaskByPath(filePath: Uri): ITask? { + taskMap.values.forEach { + if (it.filePath == filePath.toString()) { + return it + } + } + return null } +// /** +// * @param filePath task unique identifier, like: savePath, sourceUrl +// */ +// fun putTaskUtil(filePath: Uri, taskUtil: ITaskAdapter) { +// taskAdapterMap[filePath] = taskUtil +// taskUtil.stop() +// } + /** * @param filePath unique identifier, like: savePath, sourceUrl */ - fun getTaskUtil(filePath: Uri): ITaskUtil? { - return taskUtilMap[filePath] + fun getTaskAdapter(filePath: Uri): ITaskAdapter? { + return taskAdapterMap[filePath] } fun putEntity(taskId: Int, entity: BaseEntity) { diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java deleted file mode 100644 index 42e5d4c8..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java +++ /dev/null @@ -1,54 +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.upload; - -import com.arialyy.aria.core.config.Configuration; -import com.arialyy.aria.core.config.UploadConfig; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; - -/** - * Created by lyy on 2017/2/9. 上传任务实体 - */ -public class UTaskWrapper extends AbsTaskWrapper { - - /** - * 保存临时设置的上传路径 - */ - private String tempUrl; - - public UTaskWrapper(UploadEntity entity) { - super(entity); - } - - public String getTempUrl() { - return tempUrl; - } - - public void setTempUrl(String tempUrl) { - this.tempUrl = tempUrl; - } - - /** - * 文件保存路径 - */ - @Override public String getKey() { - return getEntity().getKey(); - } - - @Override public UploadConfig getConfig() { - return Configuration.getInstance().uploadCfg; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java deleted file mode 100644 index 34a6320f..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ /dev/null @@ -1,94 +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.upload; - -import android.os.Parcel; -import android.os.Parcelable; -import android.text.TextUtils; -import com.arialyy.aria.core.common.AbsNormalEntity; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.orm.annotation.Primary; - -/** - * Created by lyy on 2017/2/9. - * 上传文件实体 - */ -public class UploadEntity extends AbsNormalEntity implements Parcelable { - /** - * 文件上传路径 - */ - @Primary private String filePath; - - /** - * 上传完成后服务器返回的数据 - */ - private String responseStr = ""; - - public String getResponseStr() { - return responseStr; - } - - public void setResponseStr(String responseStr) { - this.responseStr = responseStr; - } - - @Override - public String getFilePath() { - return filePath; - } - - public void setFilePath(String filePath) { - this.filePath = filePath; - } - - @Override public String getKey() { - return filePath; - } - - @Override public int getTaskType() { - if (TextUtils.isEmpty(getUrl())) { - return ITaskWrapper.ERROR; - } - return getUrl().startsWith("ftp") ? ITaskWrapper.U_FTP : ITaskWrapper.U_HTTP; - } - - public UploadEntity() { - } - - @Override public int describeContents() { - return 0; - } - - @Override public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeString(this.filePath); - } - - protected UploadEntity(Parcel in) { - super(in); - this.filePath = in.readString(); - } - - public static final Creator CREATOR = new Creator() { - @Override public UploadEntity createFromParcel(Parcel source) { - return new UploadEntity(source); - } - - @Override public UploadEntity[] newArray(int size) { - return new UploadEntity[size]; - } - }; -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java deleted file mode 100644 index f59e4fbb..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java +++ /dev/null @@ -1,197 +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.wrapper; - -import com.arialyy.aria.core.TaskOptionParams; -import com.arialyy.aria.core.common.AbsEntity; -import com.arialyy.aria.core.config.BaseTaskConfig; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.event.ErrorEvent; -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.ITaskOption; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.util.ComponentUtil; - -/** - * Created by lyy on 2017/2/23. 所有任务实体的父类 - */ -public abstract class AbsTaskWrapper implements ITaskWrapper { - - /** - * 刷新信息 {@code true} 重新刷新下载信息 - */ - private boolean refreshInfo = false; - - /** - * 是否是新任务,{@code true} 新任务 - */ - private boolean isNewTask = false; - - /** - * 请求类型 {@link ITaskWrapper#D_HTTP}、{@link ITaskWrapper#D_FTP}、{@link - * ITaskWrapper#D_FTP_DIR}.. - */ - private int requestType = D_HTTP; - - /** - * 删除任务时,是否删除已下载完成的文件 未完成的任务,不管true还是false,都会删除文件 {@code true} 删除任务数据库记录,并且删除已经下载完成的文件 {@code - * false} 如果任务已经完成,只删除任务数据库记录 - */ - private boolean removeFile = false; - - /** - * 是否支持断点, {@code true} 为支持断点 - */ - private boolean isSupportBP = true; - - /** - * 状态码 - */ - private int code; - - /** - * {@link DownloadEntity} or {@link UploadEntity} or {@link DownloadGroupEntity} - */ - private ENTITY entity; - - /** - * 错误信息 - */ - private ErrorEvent errorEvent; - - /** - * 任务配置 - */ - private ITaskOption taskOption; - - /** - * 任务配置信息 - */ - private TaskOptionParams optionParams = new TaskOptionParams(); - - /** - * {@code true}强制下载\上传,不考虑文件路径是否被占用 - */ - private boolean ignoreFilePathOccupy = false; - - public boolean isIgnoreFilePathOccupy() { - return ignoreFilePathOccupy; - } - - public void setIgnoreFilePathOccupy(boolean ignoreFilePathOccupy) { - this.ignoreFilePathOccupy = ignoreFilePathOccupy; - } - - public void setTaskOption(ITaskOption option) { - this.taskOption = option; - } - - public ITaskOption getTaskOption() { - return taskOption; - } - - public void generateTaskOption(Class clazz) { - taskOption = ComponentUtil.getInstance().buildTaskOption(clazz, optionParams); - } - - public TaskOptionParams getOptionParams() { - if (optionParams == null) { - optionParams = new TaskOptionParams(); - } - return optionParams; - } - - public AbsTaskWrapper(ENTITY entity) { - this.entity = entity; - } - - public ErrorEvent getErrorEvent() { - return errorEvent; - } - - public void setErrorEvent(ErrorEvent errorEvent) { - this.errorEvent = errorEvent; - } - - @Override public ENTITY getEntity() { - return entity; - } - - /** - * 获取任务下载状态 - * - * @return {@link IEntity} - */ - public int getState() { - return getEntity().getState(); - } - - public abstract BaseTaskConfig getConfig(); - - public boolean isRefreshInfo() { - return refreshInfo; - } - - public void setRefreshInfo(boolean refreshInfo) { - this.refreshInfo = refreshInfo; - } - - public boolean isNewTask() { - return isNewTask; - } - - public void setNewTask(boolean newTask) { - isNewTask = newTask; - } - - public void setState(int state) { - entity.setState(state); - } - - @Override - public int getRequestType() { - return requestType; - } - - public void setRequestType(int requestType) { - this.requestType = requestType; - } - - public boolean isRemoveFile() { - return removeFile; - } - - public void setRemoveFile(boolean removeFile) { - this.removeFile = removeFile; - } - - public boolean isSupportBP() { - return isSupportBP; - } - - public void setSupportBP(boolean supportBP) { - isSupportBP = supportBP; - } - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/ITaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/ITaskWrapper.java deleted file mode 100644 index b5442513..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/ITaskWrapper.java +++ /dev/null @@ -1,111 +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.wrapper; - -import com.arialyy.aria.core.inf.IEntity; - -/** - * 组合任务实体包裹器,用于加载和任务相关的参数 - */ -public interface ITaskWrapper { - - int ERROR = 0; - - /** - * HTTP单任务载 - */ - int D_HTTP = 1; - - /** - * HTTP任务组下载 - */ - int DG_HTTP = 2; - - /** - * FTP单文件下载 - */ - int D_FTP = 3; - - /** - * FTP文件夹下载,为避免登录过多,子任务由单线程进行处理 - */ - int D_FTP_DIR = 4; - - /** - * HTTP单文件上传 - */ - int U_HTTP = 5; - - /** - * FTP单文件上传 - */ - int U_FTP = 6; - - /** - * M3u8点播 - */ - int M3U8_VOD = 7; - - /** - * m3u8直播 - */ - int M3U8_LIVE = 8; - - /** - * TCP 文件下载 - */ - int D_TCP = 9; - - /** - * TCP 文件上传 - */ - int U_TCP = 10; - - /** - * TCP 分片上传 - */ - int U_TCP_PEER = 11; - - /** - * SFTP 下载 - */ - int D_SFTP = 12; - - /** - * SFTP 上传 - */ - int U_SFTP = 13; - - /** - * 获取任务类型 - * - * @return {@link ITaskWrapper} - */ - int getRequestType(); - - /** - * 获取任务实体 - */ - IEntity getEntity(); - - /** - * 获取任务唯一标志: - * 下载任务,对应文件的下载地址 - * 上传任务,对应需要上传的文件路径 - * 组合任务,对应组合任务的groupHash - */ - String getKey(); -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/RecordWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/RecordWrapper.java deleted file mode 100644 index bbd9c4c1..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/RecordWrapper.java +++ /dev/null @@ -1,47 +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.wrapper; - -import com.arialyy.aria.core.TaskRecord; -import com.arialyy.aria.core.ThreadRecord; -import com.arialyy.aria.orm.AbsDbWrapper; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Wrapper; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/30. - * 任务记录和线程记录的关系 - */ -@Wrapper -public class RecordWrapper extends AbsDbWrapper { - - @One - public TaskRecord taskRecord; - - @Many(parentColumn = "filePath", entityColumn = "taskKey") - public List threadRecords; - - @Override protected void handleConvert() { - if (threadRecords != null && !threadRecords.isEmpty()) { - taskRecord.threadRecords = threadRecords; - } else { - taskRecord.threadRecords = new ArrayList<>(); - } - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java deleted file mode 100644 index c3910b01..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java +++ /dev/null @@ -1,27 +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.orm; - -/** - * Created by laoyuyu on 2018/3/30. - */ -public abstract class AbsDbWrapper { - - /** - * 处理转换 - */ - protected abstract void handleConvert(); -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDelegate.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDelegate.java deleted file mode 100644 index a95fe26e..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDelegate.java +++ /dev/null @@ -1,39 +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.orm; - -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - -/** - * Created by laoyuyu on 2018/3/22. - */ -public abstract class AbsDelegate { - static final String TAG = "AbsDelegate"; - - void closeCursor(Cursor cursor) { - SqlUtil.closeCursor(cursor); - } - - /** - * 检查数据库是否关闭,已经关闭的话,打开数据库 - * - * @return 返回数据库 - */ - SQLiteDatabase checkDb(SQLiteDatabase db) { - return SqlUtil.checkDb(db); - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/ActionPolicy.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/ActionPolicy.java deleted file mode 100644 index be362bec..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/ActionPolicy.java +++ /dev/null @@ -1,62 +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.orm; - -import com.arialyy.aria.orm.annotation.Foreign; - -/** - * Created by laoyuyu on 2018/3/22. - * on update 或 on delete 都可跟不同action功能 - * - * @see - * {@link Foreign#onDelete()}、{@link Foreign#onUpdate()} - */ -public enum ActionPolicy { - - /** - * 如果子表中有匹配的记录,则不允许对父表对应候选键进行update/delete操作 - */ - NO_ACTION("NO ACTION"), - - /** - * 和NO ACTION 作用一致,和NO ACTION的区别是: - * 主表update/delete执行时,马上就触发约束; - * 而NO ACTION 是执行完成语句后才触发约束, - */ - RESTRICT("RESTRICT"), - - /** - * 在父表上update/delete记录时,将子表上匹配记录的列设为null (要注意子表的外键列不能为not null) - */ - SET_NULL("SET NULL"), - - /** - * 父表有变更时,子表将外键列设置成一个默认的值,default配置的值 - */ - SET_DEFAULT("SET ERROR"), - - /** - * 在父表上update/delete记录时,同步update/delete掉子表的匹配记录 - */ - CASCADE("CASCADE"); - - String function; - - ActionPolicy(String function) { - this.function = function; - } - -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DBConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DBConfig.java deleted file mode 100644 index e7e3ea28..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ /dev/null @@ -1,61 +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.orm; - -import android.text.TextUtils; -import com.arialyy.aria.core.TaskRecord; -import com.arialyy.aria.core.ThreadRecord; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.M3U8Entity; -import com.arialyy.aria.core.upload.UploadEntity; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Created by lyy on 2017/4/6. - * 数据库配置信息 - */ -class DBConfig { - /*adb pull /mnt/sdcard/Android/data/com.arialyy.simple/files/DB/AriaLyyDb d:/db*/ - static boolean DEBUG = false; - static Map> mapping = new LinkedHashMap<>(); - static String DB_NAME; - static int VERSION = 58; - - /** - * 是否将数据库保存在Sd卡,{@code true} 是 - */ - static final boolean SAVE_IN_SDCARD = false; - - static { - if (TextUtils.isEmpty(DB_NAME)) { - DB_NAME = "AndroidAria.db"; - } - if (VERSION == -1) { - VERSION = 1; - } - } - - static { - mapping.put("DownloadEntity", DownloadEntity.class); - mapping.put("DownloadGroupEntity", DownloadGroupEntity.class); - mapping.put("UploadEntity", UploadEntity.class); - mapping.put("ThreadRecord", ThreadRecord.class); - mapping.put("TaskRecord", TaskRecord.class); - mapping.put("M3U8Entity", M3U8Entity.class); - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DatabaseContext.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DatabaseContext.java deleted file mode 100644 index c606676a..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DatabaseContext.java +++ /dev/null @@ -1,93 +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.orm; - -import android.content.Context; -import android.content.ContextWrapper; -import android.database.DatabaseErrorHandler; -import android.database.sqlite.SQLiteDatabase; -import com.arialyy.aria.util.CommonUtil; -import java.io.File; -import java.io.IOException; - -/** - * 保存在sd卡的数据库使用的Context - */ -class DatabaseContext extends ContextWrapper { - public DatabaseContext(Context context) { - super(context); - } - - /** - * 获得数据库路径,如果不存在,则创建对象对象 - */ - @Override - public File getDatabasePath(String name) { - String dbDir = CommonUtil.getAppPath(getBaseContext()); - - dbDir += "DB";//数据库所在目录 - String dbPath = dbDir + "/" + name;//数据库路径 - //判断目录是否存在,不存在则创建该目录 - File dirFile = new File(dbDir); - if (!dirFile.exists()) { - dirFile.mkdirs(); - } - - //数据库文件是否创建成功 - boolean isFileCreateSuccess = false; - //判断文件是否存在,不存在则创建该文件 - File dbFile = new File(dbPath); - if (!dbFile.exists()) { - try { - isFileCreateSuccess = dbFile.createNewFile();//创建文件 - } catch (IOException e) { - e.printStackTrace(); - } - } else { - isFileCreateSuccess = true; - } - - //返回数据库文件对象 - if (isFileCreateSuccess) { - return dbFile; - } else { - return null; - } - } - - /** - * 重载这个方法,是用来打开SD卡上的数据库的,android 2.3及以下会调用这个方法。 - */ - @Override - public SQLiteDatabase openOrCreateDatabase(String name, int mode, - SQLiteDatabase.CursorFactory factory) { - return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null); - } - - /** - * Android 4.0会调用此方法获取数据库。 - * - * @see android.content.ContextWrapper#openOrCreateDatabase(java.lang.String, int, - * android.database.sqlite.SQLiteDatabase.CursorFactory, - * android.database.DatabaseErrorHandler) - */ - @Override - public SQLiteDatabase openOrCreateDatabase(String name, int mode, - SQLiteDatabase.CursorFactory factory, - DatabaseErrorHandler errorHandler) { - return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null); - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DbContentProvider.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/DbContentProvider.kt deleted file mode 100644 index b184e131..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DbContentProvider.kt +++ /dev/null @@ -1,174 +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.orm - -import android.content.ContentProvider -import android.content.ContentValues -import android.content.Context -import android.database.Cursor -import android.net.Uri -import com.arialyy.aria.util.ALog -import com.arialyy.aria.util.CommonUtil - -/** - * @Author laoyuyu - * @Description - * @Date 2:25 下午 2022/4/25 - **/ -class DbContentProvider : ContentProvider() { - val TAG = "DbContentProvider" - - companion object { - // const val KEY_TABLE_NAME = "tableName" - const val KEY_ROW_ID = "rowId" - const val KEY_TABLE_CLAZZ = "tableClazz" - const val KEY_LIMIT = "limit" - - fun createRequestUrl(context: Context, clazz: Class<*>): Uri { - return Uri.parse("content://${context.packageName}.com.arialyy.aria.provide/request?${KEY_TABLE_CLAZZ}=${clazz.name}") - } - - fun getRequestUrl(context: Context): String { - return "content://${context.packageName}.com.arialyy.aria.provide/request" - } - - fun getResponseUrl(context: Context): String { - return "content://${context.packageName}.com.arialyy.aria.provide/response" - } - } - - /** - * key: clazzPath, value: tableName - */ - private val tableExistMap = mutableMapOf() - - private lateinit var helper: SqlHelper - - override fun onCreate(): Boolean { - helper = SqlHelper.init(context) - return true - } - - private fun getTableName(uri: Uri): String? { - val db = SqlUtil.checkDb(helper.db) - val clazzName = uri.getQueryParameter(KEY_TABLE_CLAZZ) - if (tableExistMap[clazzName!!] != null) { - return tableExistMap[clazzName] - } - - val clazz: Class? = - javaClass.classLoader?.loadClass(clazzName) as Class? - if (clazz == null) { - ALog.e(TAG, "【$clazz】为空") - return null - } - SqlUtil.checkOrCreateTable(db, clazz) - val tableName = CommonUtil.getClassName(clazz) - tableExistMap[clazzName] = tableName - return tableName - } - - override fun query( - uri: Uri, - projection: Array?, - selection: String?, - selectionArgs: Array?, - sortOrder: String? - ): Cursor? { - val db = SqlUtil.checkDb(helper.db) - val tableName = getTableName(uri) - if (tableName.isNullOrBlank()) { - return null - } - val clazzName = uri.getQueryParameter(KEY_TABLE_CLAZZ) - val clazz = javaClass.classLoader?.loadClass(clazzName) as Class? - val columns = SqlUtil.getColumns(clazz) - val limit = uri.getQueryParameter(KEY_LIMIT) - return db.query( - tableName, - columns.toTypedArray(), - selection, - selectionArgs, - null, - null, - null, - limit - ) - } - - override fun getType(uri: Uri): String? { - return null - } - - override fun insert(uri: Uri, values: ContentValues?): Uri? { - val db = SqlUtil.checkDb(helper.db) - val tableName = getTableName(uri) - if (tableName.isNullOrBlank()) { - return null - } - db.beginTransaction() - try { - val rowId = db.insert(tableName, null, values) - if (rowId == -1L) { - return null - } - return Uri.parse("${getResponseUrl(context!!)}?${KEY_ROW_ID}=$rowId") - } finally { - db.endTransaction() - } - } - - override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int { - val db = SqlUtil.checkDb(helper.db) - val tableName = getTableName(uri) - if (tableName.isNullOrBlank()) { - return -1 - } - db.beginTransaction() - try { - val rowId = db.delete(tableName, selection, selectionArgs) - if (rowId == -1) { - return -1 - } - return rowId - } finally { - db.endTransaction() - } - } - - override fun update( - uri: Uri, - values: ContentValues?, - selection: String?, - selectionArgs: Array? - ): Int { - val db = SqlUtil.checkDb(helper.db) - val tableName = getTableName(uri) - if (tableName.isNullOrBlank()) { - return -1 - } - db.beginTransaction() - try { - val rowId = db.update(tableName, values, selection, selectionArgs) - if (rowId == -1) { - return -1 - } - return rowId - } finally { - db.endTransaction() - } - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DbEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DbEntity.java deleted file mode 100644 index c7515a27..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ /dev/null @@ -1,295 +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.orm; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by lyy on 2015/11/2. 所有数据库实体父类 - */ -public abstract class DbEntity { - private static final Object LOCK = new Object(); - public long rowID = -1; - - protected DbEntity() { - - } - - protected long getRowID() { - return rowID; - } - - /** - * 查询关联数据 - * - * DbEntity.findRelationData(DGEntityWrapper.class, "downloadUrl=?", downloadUrl); - * - * - * @param expression 查询条件 - */ - public static List findRelationData(Class clazz, - String... expression) { - return DelegateWrapper.getInstance().findRelationData(clazz, expression); - } - - /** - * 分页查询关联数据 - * - * - * DbEntity.findRelationData(DGEntityWrapper.class, 0, 10, "downloadUrl=?", downloadUrl); - * - * - * @param expression 查询条件 - * @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null - * @param num 每页返回的数量 - * @return 没有数据返回null,如果页数大于总页数,返回null - */ - public static List findRelationData(Class clazz, int page, int num, - String... expression) { - if (page < 1 || num < 1) { - return null; - } - return DelegateWrapper.getInstance().findRelationData(clazz, page, num, expression); - } - - /** - * 检查某个字段的值是否存在 - * - * @param expression 字段和值"downloadPath=?" - * @return {@code true}该字段的对应的value已存在 - */ - public static boolean checkDataExist(Class clazz, String... expression) { - return DelegateWrapper.getInstance().checkDataExist(clazz, expression); - } - - /** - * 清空表数据 - */ - public static void clean(Class clazz) { - DelegateWrapper.getInstance().clean(clazz); - } - - /** - * 直接执行sql语句 - */ - public static void exeSql(String sql) { - DelegateWrapper.getInstance().exeSql(sql); - } - - /** - * 查询所有数据 - * - * @return 没有数据返回null - */ - public static List findAllData(Class clazz) { - return DelegateWrapper.getInstance().findAllData(clazz); - } - - /** - * 查询第一条数据 - */ - public static T findFirst(Class clazz) { - List list = findAllData(clazz); - return (list == null || list.size() == 0) ? null : list.get(0); - } - - /** - * 查询一组数据 - * - * DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); - * - * - * @return 没有数据返回null - */ - public static List findDatas(Class clazz, String... expression) { - return DelegateWrapper.getInstance().findData(clazz, expression); - } - - /** - * 分页查询数据 - * - * DbEntity.findFirst(DownloadEntity.class, 0, 10, "downloadUrl=?", downloadUrl); - * - * - * @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null - * @param num 每页返回的数量 - * @return 没有数据返回null,如果页数大于总页数,返回null - */ - public static List findDatas(Class clazz, int page, int num, - String... expression) { - if (page < 1 || num < 1) { - return null; - } - return DelegateWrapper.getInstance().findData(clazz, page, num, expression); - } - - /** - * 模糊查询一组数据 - * - * DbEntity.findDataByFuzzy(DownloadEntity.class, "downloadUrl like http://"); - * - * - * @return 没有数据返回null - */ - public static List findDataByFuzzy(Class clazz, String conditions) { - return DelegateWrapper.getInstance().findDataByFuzzy(clazz, conditions); - } - - /** - * 模糊查询一组数据 - * - * DbEntity.findDataByFuzzy(DownloadEntity.class, 0, 10, "downloadUrl like http://"); - * - * - * @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null - * @param num 每页返回的数量 - * @return 没有数据返回null,如果页数大于总页数,返回null - */ - public static List findDataByFuzzy(Class clazz, int page, int num, - String conditions) { - return DelegateWrapper.getInstance().findDataByFuzzy(clazz, page, num, conditions); - } - - /** - * 查询一行数据 - * - * DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); - * - * - * @return 没有数据返回null - */ - public static T findFirst(Class clazz, String... expression) { - DelegateWrapper util = DelegateWrapper.getInstance(); - List datas = util.findData(clazz, expression); - return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; - } - - /** - * 插入多条数据 - */ - public static void insertManyData(List entities) { - checkListData(entities); - DelegateWrapper.getInstance().insertManyData(entities); - } - - /** - * 修改多条数据 - */ - public static void updateManyData(List entities) { - checkListData(entities); - DelegateWrapper.getInstance().updateManyData(entities); - } - - /** - * 保存多条数据,通过rowID来判断记录存在以否,如果数据库已有记录,则更新该记录;如果数据库中没有记录,则保存该记录 - */ - public static void saveAll(List entities) { - checkListData(entities); - List insertD = new ArrayList<>(); - List updateD = new ArrayList<>(); - DelegateWrapper wrapper = DelegateWrapper.getInstance(); - for (T entity : entities) { - if (entity.rowID == -1) { - insertD.add(entity); - continue; - } - if (wrapper.isExist(entity.getClass(), entity.rowID)) { - updateD.add(entity); - } else { - insertD.add(entity); - } - } - if (!insertD.isEmpty()) { - wrapper.insertManyData(insertD); - } - if (!updateD.isEmpty()) { - wrapper.updateManyData(updateD); - } - } - - /** - * 检查批量操作的列表数据,如果数据为空,抛出{@link NullPointerException} - */ - private static void checkListData(List entities) { - if (entities == null || entities.isEmpty()) { - throw new NullPointerException("列表数据为空"); - } - } - - /** - * 删除当前数据 - */ - public void deleteData() { - deleteData(getClass(), "rowid=?", rowID + ""); - } - - /** - * 根据条件删除数据 - * - * DbEntity.deleteData(DownloadEntity.class, "downloadUrl=?", downloadUrl); - * - */ - public static void deleteData(Class clazz, String... expression) { - DelegateWrapper util = DelegateWrapper.getInstance(); - util.delData(clazz, expression); - } - - /** - * 修改数据 - */ - public void update() { - DelegateWrapper.getInstance().updateData(this); - } - - /** - * 保存自身,如果表中已经有数据,则更新数据,否则插入数据 只有 target中checkEntity成功后才能保存,创建实体部分也不允许保存 - */ - public void save() { - synchronized (LOCK) { - if (thisIsExist()) { - update(); - } else { - insert(); - } - } - } - - /** - * 查找数据在表中是否存在 - */ - private boolean thisIsExist() { - DelegateWrapper util = DelegateWrapper.getInstance(); - return rowID != -1 && util.isExist(getClass(), rowID); - } - - /** - * 表是否存在 - * - * @return {@code true} 存在 - */ - public static boolean tableExists(Class clazz) { - return DelegateWrapper.getInstance().tableExists(clazz); - } - - /** - * 插入数据,只有 target中checkEntity成功后才能插入,创建实体部分也不允许操作 - */ - public void insert() { - DelegateWrapper.getInstance().insertData(this); - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java deleted file mode 100644 index 9b9a9fac..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ /dev/null @@ -1,639 +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.orm; - -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.text.TextUtils; -import android.util.SparseArray; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Wrapper; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.lang.reflect.Field; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; - -/** - * Created by laoyuyu on 2018/3/22. - * 查询数据 - */ -class DelegateFind extends AbsDelegate { - private final String PARENT_COLUMN_ALIAS = "p"; - private final String CHILD_COLUMN_ALIAS = "c"; - - private DelegateFind() { - } - - /** - * 获取{@link One}和{@link Many}注解的字段 - * - * @return 返回[OneField, ManyField] ,如果注解依赖错误返回null - */ - private Field[] getOneAndManyField(Class clazz) { - Field[] om = new Field[2]; - Field[] fields = clazz.getDeclaredFields(); - Field one = null, many = null; - boolean hasOne = false, hasMany = false; - for (Field field : fields) { - if (SqlUtil.isOne(field)) { - if (hasOne) { - ALog.w(TAG, "查询数据失败,实体中有多个@One 注解"); - return null; - } - hasOne = true; - one = field; - } - if (SqlUtil.isMany(field)) { - if (hasMany) { - ALog.w(TAG, "查询数据失败,实体中有多个@Many 注解"); - return null; - } - if (!field.getType().isAssignableFrom(List.class)) { - ALog.w(TAG, "查询数据失败,@Many 注解的类型不是List"); - return null; - } - hasMany = true; - many = field; - } - } - - if (one == null || many == null) { - ALog.w(TAG, "查询数据失败,实体中没有@One或@Many注解"); - return null; - } - - if (many.getType() != List.class) { - ALog.w(TAG, "查询数据失败,@Many注解的字段必须是List"); - return null; - } - om[0] = one; - om[1] = many; - return om; - } - - /** - * 查找一对多的关联数据 - * 如果查找不到数据或实体没有被{@link Wrapper}注解,将返回null - * 如果实体中没有{@link One}或{@link Many}注解,将返回null - * 如果实体中有多个{@link One}或{@link Many}注解,将返回nul - * {@link One} 的注解对象必须是{@link DbEntity},{@link Many}的注解对象必须是List,并且List中的类型必须是{@link DbEntity} - */ - List findRelationData(SQLiteDatabase db, Class clazz, - String... expression) { - return exeRelationSql(db, clazz, 1, Integer.MAX_VALUE, expression); - } - - /** - * 查找一对多的关联数据 - * 如果查找不到数据或实体没有被{@link Wrapper}注解,将返回null - * 如果实体中没有{@link One}或{@link Many}注解,将返回null - * 如果实体中有多个{@link One}或{@link Many}注解,将返回nul - * {@link One} 的注解对象必须是{@link DbEntity},{@link Many}的注解对象必须是List,并且List中的类型必须是{@link DbEntity} - */ - List findRelationData(SQLiteDatabase db, Class clazz, - int page, int num, String... expression) { - if (page < 1 || num < 1) { - ALog.w(TAG, "page,num 小于1"); - return null; - } - return exeRelationSql(db, clazz, page, num, expression); - } - - /** - * 执行关联查询,如果不需要分页,page和num传-1 - * - * @param page 当前页 - * @param num 一页的数量 - */ - private List exeRelationSql(SQLiteDatabase db, Class wrapperClazz, - int page, int num, String... expression) { - db = checkDb(db); - if (SqlUtil.isWrapper(wrapperClazz)) { - Field[] om = getOneAndManyField(wrapperClazz); - if (om == null) { - return null; - } - StringBuilder sb = new StringBuilder(); - Field one = om[0], many = om[1]; - try { - Many m = many.getAnnotation(Many.class); - Class parentClazz = Class.forName(one.getType().getName()); - Class childClazz = Class.forName(CommonUtil.getListParamType(many).getName()); - // 检查表 - SqlUtil.checkOrCreateTable(db, parentClazz); - SqlUtil.checkOrCreateTable(db, childClazz); - final String pTableName = parentClazz.getSimpleName(); - final String cTableName = childClazz.getSimpleName(); - List pColumn = SqlUtil.getAllNotIgnoreField(parentClazz); - List cColumn = SqlUtil.getAllNotIgnoreField(childClazz); - StringBuilder pSb = new StringBuilder(); - StringBuilder cSb = new StringBuilder(); - - if (pColumn != null) { - pSb.append(pTableName.concat(".rowid AS ").concat(PARENT_COLUMN_ALIAS).concat("rowid,")); - for (Field f : pColumn) { - String temp = PARENT_COLUMN_ALIAS.concat(f.getName()); - pSb.append(pTableName.concat(".").concat(f.getName())) - .append(" AS ") - .append(temp) - .append(","); - } - } - - if (cColumn != null) { - pSb.append(cTableName.concat(".rowid AS ").concat(CHILD_COLUMN_ALIAS).concat("rowid,")); - for (Field f : cColumn) { - String temp = CHILD_COLUMN_ALIAS.concat(f.getName()); - cSb.append(cTableName.concat(".").concat(f.getName())) - .append(" AS ") - .append(temp) - .append(","); - } - } - - String pColumnAlia = pSb.toString(); - String cColumnAlia = cSb.toString(); - if (!TextUtils.isEmpty(pColumnAlia)) { - pColumnAlia = pColumnAlia.substring(0, pColumnAlia.length() - 1); - } - - if (!TextUtils.isEmpty(cColumnAlia)) { - cColumnAlia = cColumnAlia.substring(0, cColumnAlia.length() - 1); - } - - sb.append("SELECT "); - - if (!TextUtils.isEmpty(pColumnAlia)) { - sb.append(pColumnAlia).append(","); - } - if (!TextUtils.isEmpty(cColumnAlia)) { - sb.append(cColumnAlia); - } - if (TextUtils.isEmpty(pColumnAlia) && TextUtils.isEmpty(cColumnAlia)) { - sb.append(" * "); - } - - sb.append(" FROM ") - .append(pTableName) - .append(" INNER JOIN ") - .append(cTableName) - .append(" ON ") - .append(pTableName.concat(".").concat(m.parentColumn())) - .append(" = ") - .append(cTableName.concat(".").concat(m.entityColumn())); - String sql; - if (expression != null && expression.length > 0) { - if (!CommonUtil.checkSqlExpression(expression)) { - return null; - } - sb.append(" WHERE ").append(expression[0]).append(" "); - sql = sb.toString(); - sql = sql.replace("?", "%s"); - Object[] params = new String[expression.length - 1]; - for (int i = 0, len = params.length; i < len; i++) { - params[i] = String.format("'%s'", SqlUtil.encodeStr(expression[i + 1])); - } - sql = String.format(sql, params); - } else { - sql = sb.toString(); - } - boolean paged = false; - if (page != -1 && num != -1) { - paged = true; - sql = sql.concat(String.format(" Group by %s LIMIT %s,%s", - pTableName.concat(".").concat(m.parentColumn()), (page - 1) * num, num)); - } - Cursor cursor = db.rawQuery(sql, null); - List data = - newInstanceEntity(wrapperClazz, parentClazz, childClazz, cursor, pColumn, cColumn, - paged, db, m.entityColumn(), m.parentColumn()); - - closeCursor(cursor); - return data; - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } else { - ALog.e(TAG, "查询数据失败,实体类没有使用@Wrapper 注解"); - return null; - } - return null; - } - - /** - * 创建关联查询的数据 - * - * @param pColumn 父表的所有字段 - * @param cColumn 字表的所有字段 - */ - private synchronized List newInstanceEntity( - Class wrapperClazz, Class

parentClazz, - Class childClazz, - Cursor cursor, - List pColumn, List cColumn, boolean paged, SQLiteDatabase db, - String entityColumn, String parentColumn) { - List wrappers = new ArrayList<>(); - SparseArray> childs = new SparseArray<>(); // 所有子表数据 - SparseArray parents = new SparseArray<>(); // 所有父表数据 - - try { - while (cursor.moveToNext()) { - int pRowId = cursor.getInt(cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat("rowid"))); - if (childs.get(pRowId) == null) { - childs.put(pRowId, new ArrayList()); - parents.put(pRowId, createParent(pRowId, parentClazz, pColumn, cursor)); - } - if (paged) { - List list = createChildren(db, childClazz, pColumn, entityColumn, parentColumn, - parents.get(pRowId)); - if (list != null) { - childs.get(pRowId).addAll(list); - } - } else { - childs.get(pRowId).add(createChild(childClazz, cColumn, cursor)); - } - } - - List wFields = SqlUtil.getAllNotIgnoreField(wrapperClazz); - if (wFields == null || wFields.isEmpty()) { - return null; - } - for (int i = 0; i < parents.size(); i++) { - int pRowId = parents.keyAt(i); - T wrapper = wrapperClazz.newInstance(); - boolean isPSet = false, isCSet = false; // 保证One 或 Many 只设置一次 - for (Field f : wFields) { - if (!isPSet && f.getAnnotation(One.class) != null) { - f.set(wrapper, parents.get(pRowId)); - isPSet = true; - } - if (!isCSet && f.getAnnotation(Many.class) != null) { - f.set(wrapper, childs.get(pRowId)); - isCSet = true; - } - } - wrapper.handleConvert(); //处理下转换 - wrappers.add(wrapper); - } - } catch (Exception e) { - e.printStackTrace(); - } - - return wrappers; - } - - /** - * 创建子对象集合 - */ - private List createChildren(SQLiteDatabase db, Class childClazz, - List pColumn, - String entityColumn, String parentColumn, DbEntity parents) - throws IllegalAccessException { - - for (Field field : pColumn) { - field.setAccessible(true); - if (field.getName().equals(parentColumn)) { - Object o = field.get(parents); - if (o instanceof String) { - o = URLEncoder.encode((String) o); - } - return findData(db, childClazz, entityColumn + "='" + o + "'"); - } - } - return new ArrayList(); - } - - /** - * 创建子对象 - */ - private T createChild(Class childClazz, List cColumn, - Cursor cursor) - throws InstantiationException, IllegalAccessException { - T child = childClazz.newInstance(); - child.rowID = cursor.getInt(cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat("rowid"))); - for (Field field : cColumn) { - field.setAccessible(true); - int columnIndex = cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat(field.getName())); - setFieldValue(field.getType(), field, columnIndex, cursor, child); - } - return child; - } - - /** - * 创建父对象 - */ - private T createParent(int rowId, Class parentClazz, List pColumn, - Cursor cursor) - throws InstantiationException, IllegalAccessException { - T parent = parentClazz.newInstance(); - parent.rowID = rowId; - for (Field field : pColumn) { - field.setAccessible(true); - int columnIndex = cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat(field.getName())); - setFieldValue(field.getType(), field, columnIndex, cursor, parent); - } - return parent; - } - - /** - * 条件查寻数据 - */ - List findData(SQLiteDatabase db, Class clazz, String... expression) { - db = checkDb(db); - if (!CommonUtil.checkSqlExpression(expression)) { - return null; - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz), - expression[0]); - String[] params = new String[expression.length - 1]; - try { - // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 - System.arraycopy(expression, 1, params, 0, params.length); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - - return exeNormalDataSql(db, clazz, sql, params); - } - - /** - * 获取分页数据 - */ - List findData(SQLiteDatabase db, Class clazz, int page, int num, - String... expression) { - if (page < 1 || num < 1) { - ALog.w(TAG, "page, bum 小于1"); - return null; - } - db = checkDb(db); - if (!CommonUtil.checkSqlExpression(expression)) { - return null; - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", - CommonUtil.getClassName(clazz), - expression[0], (page - 1) * num, num); - - String[] params = new String[expression.length - 1]; - try { - // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 - System.arraycopy(expression, 1, params, 0, params.length); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - - return exeNormalDataSql(db, clazz, sql, params); - } - - /** - * 模糊查寻数据 - */ - List findDataByFuzzy(SQLiteDatabase db, Class clazz, - String conditions) { - db = checkDb(db); - if (TextUtils.isEmpty(conditions)) { - throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); - } - if (!conditions.toUpperCase().contains("LIKE")) { - throw new IllegalArgumentException("sql语句表达式未包含LIEK"); - } - String sql = String.format("SELECT rowid, * FROM %s, WHERE %s", CommonUtil.getClassName(clazz), - conditions); - return exeNormalDataSql(db, clazz, sql, null); - } - - /** - * 分页、模糊搜索数据 - */ - List findDataByFuzzy(SQLiteDatabase db, Class clazz, - int page, int num, String conditions) { - if (page < 1 || num < 1) { - ALog.w(TAG, "page, bum 小于1"); - return null; - } - db = checkDb(db); - if (TextUtils.isEmpty(conditions)) { - throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); - } - if (!conditions.toUpperCase().contains("LIKE")) { - throw new IllegalArgumentException("sql语句表达式未包含LIEK"); - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", - CommonUtil.getClassName(clazz), conditions, (page - 1) * num, num); - return exeNormalDataSql(db, clazz, sql, null); - } - - /** - * 查找表的所有数据 - */ - List findAllData(SQLiteDatabase db, Class clazz) { - db = checkDb(db); - String sql = String.format("SELECT rowid, * FROM %s", CommonUtil.getClassName(clazz)); - return exeNormalDataSql(db, clazz, sql, null); - } - - /** - * 执行查询普通数据的sql语句,并创建对象 - * - * @param sql sql 查询语句 - * @param selectionArgs 查询参数,如何sql语句中查询条件含有'?'则该参数不能为空 - */ - private List exeNormalDataSql(SQLiteDatabase db, Class clazz, - String sql, String[] selectionArgs) { - SqlUtil.checkOrCreateTable(db, clazz); - Cursor cursor; - try { - if (selectionArgs != null) { - String[] temp = new String[selectionArgs.length]; - int i = 0; - for (String arg : selectionArgs) { - temp[i] = SqlUtil.encodeStr(arg); - i++; - } - //sql执行失败 android.database.sqlite.SQLiteException: no such column: filePath 异常 - cursor = db.rawQuery(sql, temp); - } else { - cursor = db.rawQuery(sql, null); - } - List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; - closeCursor(cursor); - return data; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - /** - * 根据数据游标创建一个具体的对象 - */ - private synchronized List newInstanceEntity(Class clazz, - Cursor cursor) { - List fields = CommonUtil.getAllFields(clazz); - List entitys = new ArrayList<>(); - if (fields != null && fields.size() > 0) { - try { - while (cursor.moveToNext()) { - T entity = clazz.newInstance(); - String primaryName = ""; - for (Field field : fields) { - field.setAccessible(true); - if (SqlUtil.isIgnore(field)) { - continue; - } - - Class type = field.getType(); - if (SqlUtil.isPrimary(field) && (type == int.class || type == Integer.class)) { - primaryName = field.getName(); - } - - int column = cursor.getColumnIndex(field.getName()); - if (column == -1) continue; - setFieldValue(type, field, column, cursor, entity); - } - //当设置了主键,而且主键的类型为integer时,查询RowID等于主键 - entity.rowID = cursor.getInt( - cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName)); - //mDataCache.put(getCacheKey(entity), entity); - entitys.add(entity); - } - closeCursor(cursor); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - return entitys; - } - - /** - * 设置字段的值 - * - * @throws IllegalAccessException - */ - private void setFieldValue(Class type, Field field, int columnIndex, Cursor cursor, - DbEntity entity) - throws IllegalAccessException { - if (cursor == null || cursor.isClosed()) { - ALog.e(TAG, "cursor没有初始化"); - return; - } - if (type == String.class) { - String temp = cursor.getString(columnIndex); - if (!TextUtils.isEmpty(temp)) { - field.set(entity, URLDecoder.decode(temp)); - } - } else if (type == int.class || type == Integer.class) { - field.setInt(entity, cursor.getInt(columnIndex)); - } else if (type == float.class || type == Float.class) { - field.setFloat(entity, cursor.getFloat(columnIndex)); - } else if (type == double.class || type == Double.class) { - field.setDouble(entity, cursor.getDouble(columnIndex)); - } else if (type == long.class || type == Long.class) { - field.setLong(entity, cursor.getLong(columnIndex)); - } else if (type == boolean.class || type == Boolean.class) { - String temp = cursor.getString(columnIndex); - if (TextUtils.isEmpty(temp)) { - field.setBoolean(entity, false); - } else { - field.setBoolean(entity, !temp.equalsIgnoreCase("false")); - } - } else if (type == java.util.Date.class || type == java.sql.Date.class) { - field.set(entity, new Date(URLDecoder.decode(cursor.getString(columnIndex)))); - } else if (type == byte[].class) { - field.set(entity, cursor.getBlob(columnIndex)); - } else if (type == Map.class) { - String temp = cursor.getString(columnIndex); - if (!TextUtils.isEmpty(temp)) { - field.set(entity, SqlUtil.str2Map(URLDecoder.decode(temp))); - } - } else if (type == List.class) { - String value = cursor.getString(columnIndex); - if (!TextUtils.isEmpty(value)) { - field.set(entity, SqlUtil.str2List(URLDecoder.decode(value), field)); - } - } - } - - /** - * 获取所在行Id - */ - int[] getRowId(SQLiteDatabase db, Class clazz) { - db = checkDb(db); - Cursor cursor = db.rawQuery("SELECT rowid, * FROM " + CommonUtil.getClassName(clazz), null); - int[] ids = new int[cursor.getCount()]; - int i = 0; - while (cursor.moveToNext()) { - ids[i] = cursor.getInt(cursor.getColumnIndex("rowid")); - i++; - } - cursor.close(); - return ids; - } - - /** - * 获取行Id - */ - int getRowId(SQLiteDatabase db, Class clazz, Object[] wheres, Object[] values) { - db = checkDb(db); - if (wheres.length <= 0 || values.length <= 0) { - ALog.e(TAG, "请输入删除条件"); - return -1; - } else if (wheres.length != values.length) { - ALog.e(TAG, "groupHash 和 vaule 长度不相等"); - return -1; - } - StringBuilder sb = new StringBuilder(); - sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE "); - int i = 0; - for (Object where : wheres) { - sb.append(where).append("=").append("'").append(values[i]).append("'"); - sb.append(i >= wheres.length - 1 ? "" : ","); - i++; - } - Cursor c = db.rawQuery(sb.toString(), null); - int id = c.getColumnIndex("rowid"); - c.close(); - return id; - } - - /** - * 通过rowId判断数据是否存在 - */ - boolean itemExist(SQLiteDatabase db, Class clazz, long rowId) { - return itemExist(db, CommonUtil.getClassName(clazz), rowId); - } - - /** - * 通过rowId判断数据是否存在 - */ - boolean itemExist(SQLiteDatabase db, String tableName, long rowId) { - db = checkDb(db); - String sql = "SELECT rowid FROM " + tableName + " WHERE rowid=" + rowId; - Cursor cursor = db.rawQuery(sql, null); - boolean isExist = cursor.getCount() > 0; - cursor.close(); - return isExist; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateManager.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateManager.java deleted file mode 100644 index 802a8bc6..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateManager.java +++ /dev/null @@ -1,69 +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.orm; - -import android.util.SparseArray; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -/** - * Created by laoyuyu on 2018/3/22. - * Delegate管理器 - */ -class DelegateManager { - private final String TAG = "ModuleFactory"; - - private SparseArray mDelegates = new SparseArray<>(); - private static volatile DelegateManager INSTANCE = null; - - private DelegateManager() { - - } - - static DelegateManager getInstance() { - if (INSTANCE == null) { - synchronized (DelegateManager.class) { - INSTANCE = new DelegateManager(); - } - } - return INSTANCE; - } - - /** - * 获取Module - */ - M getDelegate(Class clazz) { - M delegate = (M) mDelegates.get(clazz.hashCode()); - try { - if (delegate == null) { - Constructor c = clazz.getDeclaredConstructor(); - c.setAccessible(true); - delegate = (M) c.newInstance(); - mDelegates.put(clazz.hashCode(), delegate); - return delegate; - } - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return delegate; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java deleted file mode 100644 index 742e7f45..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java +++ /dev/null @@ -1,58 +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.orm; - -import android.content.ContentValues; -import android.content.Context; -import android.net.Uri; -import com.arialyy.aria.util.ALog; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/22. 增加数据、更新数据 - */ -class DelegateUpdate extends AbsDelegate { - private DelegateUpdate() { - } - - /** - * 修改某行数据 - */ - synchronized void updateData(Context context, DbEntity dbEntity) { - Uri uri = DbContentProvider.Companion.createRequestUrl(context, dbEntity.getClass()); - ContentValues values = DbUtil.INSTANCE.createValues(dbEntity); - if (values != null) { - int rowId = context.getContentResolver() - .update(uri, values, "rowid=?", new String[] { String.valueOf(dbEntity.rowID) }); - if (rowId != -1) { - ALog.d(TAG, "更新数据成功,rowid = " + rowId); - } else { - ALog.e(TAG, "更新数据成功,rowid = " + rowId); - } - return; - } - ALog.e(TAG, "更新记录失败,记录没有属性字段"); - } - - /** - * 更新多条记录 - */ - synchronized void updateManyData(Context context, List dbEntities) { - for (DbEntity entity : dbEntities) { - updateData(context, entity); - } - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java deleted file mode 100644 index 037fcc68..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java +++ /dev/null @@ -1,213 +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.orm; - -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import java.util.List; - -/** - * Created by lyy on 2015/2/11. 数据库操作工具 - */ -public class DelegateWrapper { - private static final String TAG = "DelegateWrapper"; - private volatile static DelegateWrapper INSTANCE = null; - - private SQLiteDatabase mDb; - private DelegateManager mDManager; - private Context context; - - private DelegateWrapper() { - - } - - private DelegateWrapper(Context context) { - this.context = context.getApplicationContext(); - SqlHelper helper = SqlHelper.init(context.getApplicationContext()); - mDb = helper.getDb(); - mDManager = DelegateManager.getInstance(); - } - - public static DelegateWrapper init(Context context) { - if (INSTANCE == null) { - synchronized (DelegateWrapper.class) { - if (INSTANCE == null) { - INSTANCE = new DelegateWrapper(context); - } - } - } - return INSTANCE; - } - - static DelegateWrapper getInstance() { - if (INSTANCE == null) { - throw new NullPointerException("请在Application中调用init进行数据库工具注册注册"); - } - return INSTANCE; - } - - /** - * 查询关联表数据 - * - * @param expression 查询条件 - */ - List findRelationData(Class clazz, String... expression) { - return mDManager.getDelegate(DelegateFind.class).findRelationData(mDb, clazz, expression); - } - - /** - * 查询关联表数据 - * - * @param expression 查询条件 - */ - List findRelationData(Class clazz, int page, int num, - String... expression) { - return mDManager.getDelegate(DelegateFind.class) - .findRelationData(mDb, clazz, page, num, expression); - } - - /** - * 检查某个字段的值是否存在 - * - * @param expression 字段和值"url=xxx" - * @return {@code true}该字段的对应的value已存在 - */ - boolean checkDataExist(Class clazz, String... expression) { - return SqlUtil.checkDataExist(mDb, clazz, expression); - } - - /** - * 清空表数据 - */ - void clean(Class clazz) { - SqlUtil.clean(mDb, clazz); - } - - /** - * 执行sql语句 - */ - void exeSql(String sql) { - mDb.execSQL(sql); - } - - /** - * 删除某条数据 - */ - void delData(Class clazz, String... expression) { - mDManager.getDelegate(DelegateDel.class).delData(context, clazz, expression); - } - - /** - * 修改某行数据 - */ - void updateData(DbEntity dbEntity) { - mDManager.getDelegate(DelegateUpdate.class).updateData(context, dbEntity); - } - - /** - * 更新多条数据 - */ - void updateManyData(List dbEntitys) { - mDManager.getDelegate(DelegateUpdate.class).updateManyData(context, dbEntitys); - } - - /** - * 遍历所有数据 - */ - List findAllData(Class clazz) { - return mDManager.getDelegate(DelegateFind.class).findAllData(mDb, clazz); - } - - /** - * 条件查寻数据 - */ - List findData(Class clazz, String... expression) { - return mDManager.getDelegate(DelegateFind.class).findData(mDb, clazz, expression); - } - - /** - * 获取分页数据 - */ - List findData(Class clazz, int page, int num, String... expression) { - return mDManager.getDelegate(DelegateFind.class).findData(mDb, clazz, page, num, expression); - } - - /** - * 模糊查寻数据 - */ - List findDataByFuzzy(Class clazz, String conditions) { - return mDManager.getDelegate(DelegateFind.class).findDataByFuzzy(mDb, clazz, conditions); - } - - /** - * 模糊查寻数据 - */ - List findDataByFuzzy(Class clazz, int page, int num, - String conditions) { - return mDManager.getDelegate(DelegateFind.class) - .findDataByFuzzy(mDb, clazz, page, num, conditions); - } - - /** - * 通过rowId判断数据是否存在 - */ - boolean isExist(Class clazz, long rowId) { - return mDManager.getDelegate(DelegateFind.class).itemExist(mDb, clazz, rowId); - } - - /** - * 通过rowId判断数据是否存在 - */ - boolean isExist(String tableName, long rowId) { - return mDManager.getDelegate(DelegateFind.class).itemExist(mDb, tableName, rowId); - } - - /** - * 插入数据 - */ - void insertData(DbEntity dbEntity) { - mDManager.getDelegate(DelegateInsert.class).insertData(context, dbEntity); - } - - /** - * 插入多条数据 - */ - void insertManyData(List dbEntitys) { - mDManager.getDelegate(DelegateInsert.class).insertManyData(context, dbEntitys); - } - - /** - * 查找某张表是否存在 - */ - public boolean tableExists(Class clazz) { - return SqlUtil.tableExists(mDb, clazz); - } - - /** - * 获取所在行Id - */ - int[] getRowId(Class clazz) { - return mDManager.getDelegate(DelegateFind.class).getRowId(mDb, clazz); - } - - /** - * 获取行Id - */ - int getRowId(Class clazz, Object[] wheres, Object[] values) { - return mDManager.getDelegate(DelegateFind.class).getRowId(mDb, clazz, wheres, values); - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlHelper.java deleted file mode 100644 index 7690d152..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ /dev/null @@ -1,552 +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.orm; - -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.os.Build; -import android.text.TextUtils; -import com.arialyy.aria.core.TaskRecord; -import com.arialyy.aria.core.ThreadRecord; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.M3U8Entity; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.wrapper.ITaskWrapper; -import com.arialyy.aria.util.ALog; -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Created by lyy on 2015/11/2. - * sql帮助类 - */ -final class SqlHelper extends SQLiteOpenHelper { - private static final String TAG = "SqlHelper"; - private static volatile SqlHelper INSTANCE = null; - private static boolean mainTmpDirSet = false; - private Context mContext; - - synchronized static SqlHelper init(Context context) { - if (INSTANCE == null) { - synchronized (SqlHelper.class) { - INSTANCE = new SqlHelper(context.getApplicationContext()); - } - } - return INSTANCE; - } - - static SqlHelper getInstance() { - return INSTANCE; - } - - private SqlHelper(Context context) { - super(DBConfig.SAVE_IN_SDCARD ? new DatabaseContext(context) : context, DBConfig.DB_NAME, null, - DBConfig.VERSION); - mContext = context; - } - - @Override public void onOpen(SQLiteDatabase db) { - super.onOpen(db); - } - - @Override public void onConfigure(SQLiteDatabase db) { - super.onConfigure(db); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - db.setForeignKeyConstraintsEnabled(true); - } else { - // SQLite在3.6.19版本中开始支持外键约束, - // 而在Android中 2.1以前的版本使用的SQLite版本是3.5.9, 在2.2版本中使用的是3.6.22. - // 但是为了兼容以前的程序,默认并没有启用该功能,如果要启用该功能 - // 需要使用如下语句: - db.execSQL("PRAGMA foreign_keys=ON;"); - } - } - - @Override public void onCreate(SQLiteDatabase db) { - Set tables = DBConfig.mapping.keySet(); - for (String tableName : tables) { - Class clazz = DBConfig.mapping.get(tableName); - if (!SqlUtil.tableExists(db, clazz)) { - SqlUtil.createTable(db, clazz); - } - } - } - - @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion < newVersion) { - if (oldVersion < 31) { - handleLowAriaUpdate(db); - } else if (oldVersion < 45) { - handle360AriaUpdate(db); - } else if (oldVersion < 51) { - handle365Update(db); - } else if (oldVersion < 53) { - handle366Update(db); - } else { - handleDbUpdate(db, null); - } - // 处理380版本TaskRecord 增加的记录类型判断 - if (newVersion == 57) { - addTaskRecordType(db); - } - } - } - - @Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion > newVersion) { - handleDbUpdate(db, null); - } - } - - /** - * 获取数据库连接 - */ - SQLiteDatabase getDb() { - SQLiteDatabase db; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - SQLiteDatabase.OpenParams params = new SQLiteDatabase.OpenParams.Builder().setOpenFlags( - SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE | - SQLiteDatabase.CREATE_IF_NECESSARY).build(); - setOpenParams(params); - db = getWritableDatabase(); - } else { - //SQLiteDatabase.openOrCreateDatabase() - File dbFile = mContext.getDatabasePath(DBConfig.DB_NAME); - if (!dbFile.exists()) { - db = getWritableDatabase(); - } else { - // 触发一次SQLiteOpenHelper的流程,再使用NO_LOCALIZED_COLLATORS标志打开数据库 - db = getReadableDatabase(); - db.close(); - db = SQLiteDatabase.openDatabase(dbFile.getPath(), null, - SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE | - SQLiteDatabase.CREATE_IF_NECESSARY); - } - } - db.enableWriteAheadLogging(); - return db; - } - - @Override public SQLiteDatabase getWritableDatabase() { - if (!mainTmpDirSet) { - createDbCacheDir(); - return super.getWritableDatabase(); - } - return super.getWritableDatabase(); - } - - /** - * 用于修复 Too many open files 的问题 - * https://github.com/AriaLyy/Aria/issues/664 - */ - @Override - public SQLiteDatabase getReadableDatabase() { - if (!mainTmpDirSet) { - createDbCacheDir(); - return super.getReadableDatabase(); - } - return super.getReadableDatabase(); - } - - private void createDbCacheDir() { - String cacheDir = mContext.getCacheDir().getPath() + "/AriaDbCacheDir"; - File cacheFile = new File(cacheDir); - if (!cacheFile.exists()){ - boolean rs = cacheFile.mkdirs(); - ALog.d(TAG, rs + ""); - } - super.getReadableDatabase() - .execSQL("PRAGMA temp_store_directory = '" + cacheDir + "'"); - mainTmpDirSet = true; - } - - /** - * 处理数据库升级 - * - * @param modifyColumns 需要修改的表字段的映射,key为表名, - * value{@code Map}中的Map的key为老字段名称,value为该老字段对应的新字段名称 - */ - private void handleDbUpdate(SQLiteDatabase db, Map> modifyColumns) { - if (db == null) { - ALog.e("SqlHelper", "db 为 null"); - return; - } else if (!db.isOpen()) { - ALog.e("SqlHelper", "db已关闭"); - return; - } - - try { - db.beginTransaction(); - Set tables = DBConfig.mapping.keySet(); - for (String tableName : tables) { - Class clazz = DBConfig.mapping.get(tableName); - if (SqlUtil.tableExists(db, clazz)) { - // ----------- 1、获取旧表字段、新表字段 - Cursor columnC = - db.rawQuery(String.format("PRAGMA table_info(%s)", tableName), null); - - // 获取新表的所有字段名称 - List newTabColumns = SqlUtil.getColumns(clazz); - // 获取旧表的所有字段名称 - List oldTabColumns = new ArrayList<>(); - - while (columnC.moveToNext()) { - String columnName = columnC.getString(columnC.getColumnIndex("name")); - oldTabColumns.add(columnName); - } - columnC.close(); - - // ----------- 2、为防止字段增加失败的情况,先给旧表增加字段 - List newAddColum = getNewColumn(newTabColumns, oldTabColumns); - // 删除重命名的字段 - Map modifyMap = null; - if (modifyColumns != null) { - modifyMap = modifyColumns.get(tableName); - if (modifyMap != null) { - Iterator it = newAddColum.iterator(); - while (it.hasNext()) { - String s = it.next(); - if (modifyMap.get(s) != null) { - it.remove(); - } - } - } - } - - // 给旧表增加字段,防止新增字段失败 - if (newAddColum.size() > 0) { - String sql = "ALTER TABLE %s ADD COLUMN %s %s"; - for (String nc : newAddColum) { - String temp = - String.format(sql, tableName, nc, SqlUtil.getColumnTypeByFieldName(clazz, nc)); - ALog.d(TAG, "添加表字段的sql:" + temp); - db.execSQL(temp); - } - } - - // ----------- 3、将旧表备份下,并创建新表 - String alertSql = String.format("ALTER TABLE %s RENAME TO %s_temp", tableName, tableName); - db.execSQL(alertSql); - - //创建新表 - SqlUtil.createTable(db, clazz); - - String sql = String.format("SELECT COUNT(*) FROM %s_temp", tableName); - Cursor cursor = db.rawQuery(sql, null); - cursor.moveToFirst(); - long count = cursor.getLong(0); - cursor.close(); - - // ----------- 4、将旧表数据复制到新表 - if (count > 0) { - - // 旧表需要删除的字段,删除旧表有而新表没的字段 - List diffTab = getDiffColumn(newTabColumns, oldTabColumns); - StringBuilder params = new StringBuilder(); - - // 需要修改的列名映射表 - //Map modifyMap = null; - if (modifyColumns != null) { - modifyMap = modifyColumns.get(tableName); - } - - for (String column : oldTabColumns) { - if (!diffTab.isEmpty() && diffTab.contains(column) - // 如果旧表字段有修改,忽略这个删除 - && !(modifyMap != null && modifyMap.containsKey(column))) { - continue; - } - params.append(column).append(","); - } - - String oldParamStr = params.toString(); - oldParamStr = oldParamStr.substring(0, oldParamStr.length() - 1); - String newParamStr = oldParamStr; - // 处理字段名称改变 - if (modifyMap != null && !modifyMap.isEmpty()) { - Set keys = modifyMap.keySet(); - for (String key : keys) { - if (newParamStr.contains(key)) { - newParamStr = newParamStr.replace(key, modifyMap.get(key)); - } - } - } - - //恢复数据 - String insertSql = - String.format("INSERT INTO %s (%s) SELECT %s FROM %s_temp", tableName, newParamStr, - oldParamStr, tableName); - ALog.d(TAG, "恢复数据的sql:" + insertSql); - - db.execSQL(insertSql); - } - // ----------- 5、删除备份的表 - SqlUtil.dropTable(db, tableName + "_temp"); - } else { - SqlUtil.createTable(db, clazz); - } - } - db.setTransactionSuccessful(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - db.endTransaction(); - } - } - - /** - * 取新表差值(需要删除的字段):旧表有而新表没的字段 - * - * @param newTab 新表字段 - * @param oldTab 旧表字段 - * @return 旧表有而新表没的字段 - */ - private List getDiffColumn(List newTab, List oldTab) { - List temp = new ArrayList<>(oldTab); // 拷贝旧表字段 - temp.removeAll(newTab); - return temp; - } - - /** - * 获取新增字段 - * - * @param newTab 新表字段 - * @param oldTab 就表字段 - * @return 新表有而旧表没的字段 - */ - private List getNewColumn(List newTab, List oldTab) { - List temp = new ArrayList<>(newTab); - temp.removeAll(oldTab); - return temp; - } - - /** - * 给TaskRecord 增加任务类型 - */ - private void addTaskRecordType(SQLiteDatabase db) { - try { - SqlUtil.checkOrCreateTable(db, ThreadRecord.class); - SqlUtil.checkOrCreateTable(db, TaskRecord.class); - SqlUtil.checkOrCreateTable(db, UploadEntity.class); - SqlUtil.checkOrCreateTable(db, DownloadEntity.class); - - db.beginTransaction(); - /* - * 增加下载实体的类型 - */ - String dSql = "SELECT downloadPath, url FROM DownloadEntity"; - Cursor c = db.rawQuery(dSql, null); - while (c.moveToNext()) { - int type; - String filePath = c.getString(0); - String url = c.getString(1); - if (url.startsWith("ftp") || url.startsWith("sftp")) { - type = ITaskWrapper.D_FTP; - } else { - if (SqlUtil.tableExists(db, M3U8Entity.class)) { - Cursor m3u8c = db.rawQuery("SELECT isLive FROM M3U8Entity WHERE filePath=\"" - + SqlUtil.encodeStr(filePath) - + "\"", null); - if (m3u8c.moveToNext()) { - String temp = m3u8c.getString(0); - type = - (TextUtils.isEmpty(temp) ? false : Boolean.valueOf(temp)) ? ITaskWrapper.M3U8_LIVE - : ITaskWrapper.M3U8_VOD; - } else { - type = ITaskWrapper.D_HTTP; - } - m3u8c.close(); - } else { - type = ITaskWrapper.D_HTTP; - } - } - db.execSQL("UPDATE DownloadEntity SET taskType=? WHERE downloadPath=?", - new Object[] { type, filePath }); - db.execSQL("UPDATE TaskRecord SET taskType=? WHERE filePath=?", - new Object[] { type, filePath }); - db.execSQL("UPDATE ThreadRecord SET threadType=? WHERE taskKey=?", - new Object[] { type, filePath }); - } - c.close(); - - /* - * 增加上传实体的类型 - */ - String uSql = "SELECT filePath, url FROM UploadEntity"; - c = db.rawQuery(uSql, null); - while (c.moveToNext()) { - int type; - String filePath = c.getString(c.getColumnIndex("filePath")); - String url = c.getString(c.getColumnIndex("url")); - if (url.startsWith("ftp") || url.startsWith("sftp")) { - type = ITaskWrapper.D_FTP; - } else { - type = ITaskWrapper.D_HTTP; - } - db.execSQL("UPDATE UploadEntity SET taskType=? WHERE filePath=?", - new Object[] { type, filePath }); - db.execSQL("UPDATE TaskRecord SET taskType=? WHERE filePath=?", - new Object[] { type, filePath }); - db.execSQL("UPDATE ThreadRecord SET threadType=? WHERE taskKey=?", - new Object[] { type, filePath }); - } - c.close(); - - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - } - - /** - * 删除重复的repeat数据 - */ - private void delRepeatThreadRecord(SQLiteDatabase db) { - SqlUtil.checkOrCreateTable(db, ThreadRecord.class); - String repeatSql = "DELETE FROM ThreadRecord WHERE (rowid) " - + "IN (SELECT rowid FROM ThreadRecord GROUP BY taskKey, threadId, endLocation HAVING COUNT(*) > 1) " - + "AND rowid NOT IN (SELECT MIN(rowid) FROM ThreadRecord GROUP BY taskKey, threadId, endLocation HAVING COUNT(*)> 1)"; - ALog.d(TAG, repeatSql); - db.execSQL(repeatSql); - } - - /** - * 处理366版本以下的升级 - */ - private void handle366Update(SQLiteDatabase db) { - Map> modifyMap = new HashMap<>(); - // 处理ThreadRecord的key字段名修改 - Map threadRecordModify = new HashMap<>(); - threadRecordModify.put("key", "taskKey"); - modifyMap.put("ThreadRecord", threadRecordModify); - - // 执行升级操作 - handleDbUpdate(db, modifyMap); - delRepeatThreadRecord(db); - } - - /** - * 处理365版本以下的升级 - */ - private void handle365Update(SQLiteDatabase db) { - SqlUtil.checkOrCreateTable(db, ThreadRecord.class); - db.execSQL("UPDATE ThreadRecord SET threadId=0 WHERE threadId=-1"); - - Map> modifyMap = new HashMap<>(); - // 处理ThreadRecord的key字段名修改 - Map threadRecordModify = new HashMap<>(); - threadRecordModify.put("key", "taskKey"); - modifyMap.put("ThreadRecord", threadRecordModify); - - // 执行升级操作 - handleDbUpdate(db, modifyMap); - delRepeatThreadRecord(db); - } - - /** - * 处理3.6以下版本的数据库升级 - */ - private void handle360AriaUpdate(SQLiteDatabase db) { - String[] taskTables = - new String[] { "UploadTaskEntity", "DownloadTaskEntity", "DownloadGroupTaskEntity" }; - for (String taskTable : taskTables) { - if (SqlUtil.tableExists(db, taskTable)) { - SqlUtil.dropTable(db, taskTable); - } - } - - Map> modifyMap = new HashMap<>(); - // 处理DownloadEntity、DownloadGroupEntity的 groupName字段名的修改 - Map entityModify = new HashMap<>(); - entityModify.put("groupName", "groupHash"); - modifyMap.put("DownloadEntity", entityModify); - modifyMap.put("DownloadGroupEntity", entityModify); - - // 处理TaskRecord的dGroupName字段名的修改 - Map taskRecordModify = new HashMap<>(); - taskRecordModify.put("dGroupName", "dGroupHash"); - modifyMap.put("TaskRecord", taskRecordModify); - - // 处理ThreadRecord的key字段名修改 - Map threadRecordModify = new HashMap<>(); - threadRecordModify.put("key", "taskKey"); - modifyMap.put("ThreadRecord", threadRecordModify); - - // 执行升级操作 - handleDbUpdate(db, modifyMap); - delRepeatThreadRecord(db); - } - - /** - * 处理低版本的数据库迁移,主要是修改子表外键字段对应的值 - */ - private void handleLowAriaUpdate(SQLiteDatabase db) { - String[] taskTables = - new String[] { "UploadTaskEntity", "DownloadTaskEntity", "DownloadGroupTaskEntity" }; - for (String taskTable : taskTables) { - if (SqlUtil.tableExists(db, taskTable)) { - SqlUtil.dropTable(db, taskTable); - } - } - - //删除所有主键为null和主键重复的数据 - String[] tables = new String[] { "DownloadEntity", "DownloadGroupEntity" }; - String[] keys = new String[] { "downloadPath", "groupName" }; - int i = 0; - for (String tableName : tables) { - if (!SqlUtil.tableExists(db, tableName)) { - continue; - } - String pColumn = keys[i]; - String nullSql = - String.format("DELETE FROM %s WHERE %s='' OR %s IS NULL", tableName, pColumn, pColumn); - ALog.d(TAG, nullSql); - db.execSQL(nullSql); - - //删除所有主键重复的数据 - String repeatSql = - String.format( - "DELETE FROM %s WHERE %s IN(SELECT %s FROM %s GROUP BY %s HAVING COUNT(%s) > 1)", - tableName, pColumn, pColumn, tableName, pColumn, pColumn); - - ALog.d(TAG, repeatSql); - db.execSQL(repeatSql); - i++; - } - - // 处理数据库版本小于3的字段改变 - Map> modifyMap = new HashMap<>(); - Map dEntityModifyMap = new HashMap<>(); - dEntityModifyMap.put("groupName", "groupHash"); - dEntityModifyMap.put("downloadUrl", "url"); - dEntityModifyMap.put("isDownloadComplete", "isComplete"); - modifyMap.put("DownloadEntity", dEntityModifyMap); - - Map dGEntityModifyMap = new HashMap<>(); - dGEntityModifyMap.put("groupName", "groupHash"); - modifyMap.put("DownloadGroupEntity", dGEntityModifyMap); - - handleDbUpdate(db, modifyMap); - } -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlUtil.java deleted file mode 100644 index f975e34f..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlUtil.java +++ /dev/null @@ -1,590 +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.orm; - -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.text.TextUtils; -import com.arialyy.aria.core.AriaConfig; -import com.arialyy.aria.orm.annotation.Default; -import com.arialyy.aria.orm.annotation.Foreign; -import com.arialyy.aria.orm.annotation.Ignore; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.NoNull; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Primary; -import com.arialyy.aria.orm.annotation.Unique; -import com.arialyy.aria.orm.annotation.Wrapper; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Created by lyy on 2017/7/24. - * sql工具 - */ -final class SqlUtil { - private static final String TAG = CommonUtil.getClassName("SqlUtil"); - - /** - * 检查表是否存在,不存在则创建表 - */ - static void checkOrCreateTable(SQLiteDatabase db, Class clazz) { - if (!tableExists(db, clazz)) { - createTable(db, clazz); - } - } - - static void closeCursor(Cursor cursor) { - synchronized (AbsDelegate.class) { - if (cursor != null && !cursor.isClosed()) { - try { - cursor.close(); - } catch (android.database.SQLException e) { - e.printStackTrace(); - } - } - } - } - - /** - * 查找表是否存在 - * - * @param clazz 数据库实体 - * @return true,该数据库实体对应的表存在;false,不存在 - */ - static boolean tableExists(SQLiteDatabase db, Class clazz) { - return tableExists(db, CommonUtil.getClassName(clazz)); - } - - /** - * 查找表是否存在 - * - * @param tableName 表名 - * @return true,该数据库实体对应的表存在;false,不存在 - */ - static boolean tableExists(SQLiteDatabase db, String tableName) { - db = checkDb(db); - Cursor cursor = null; - try { - String sql = - String.format("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='%s'", - tableName); - cursor = db.rawQuery(sql, null); - if (cursor != null && cursor.moveToNext()) { - int count = cursor.getInt(0); - if (count > 0) { - return true; - } - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - closeCursor(cursor); - } - return false; - } - - /** - * 检查list参数是否合法,list只能是{@code List} - * - * @return {@code true} 合法 - */ - static boolean checkList(Field list) { - Class t = CommonUtil.getListParamType(list); - if (t == String.class) { - return true; - } else { - ALog.d(TAG, "map参数错误,支持List的参数字段"); - return false; - } - } - - /** - * 检查map参数是否合法,map只能是{@code Map} - * - * @return {@code true} 合法 - */ - static boolean checkMap(Field map) { - Class[] ts = CommonUtil.getMapParamType(map); - if (ts != null - && ts[0] != null - && ts[1] != null - && ts[0] == String.class - && ts[1] == String.class) { - return true; - } else { - ALog.d(TAG, "map参数错误,支持Map的参数字段"); - return false; - } - } - - /** - * 删除指定的表 - */ - static void dropTable(SQLiteDatabase db, String tableName) { - db = checkDb(db); - String deleteSQL = String.format("DROP TABLE IF EXISTS %s", tableName); - //db.beginTransaction(); - db.execSQL(deleteSQL); - //db.setTransactionSuccessful(); - //db.endTransaction(); - } - - /** - * 清空表数据 - */ - static void clean(SQLiteDatabase db, Class clazz) { - db = checkDb(db); - String tableName = CommonUtil.getClassName(clazz); - if (tableExists(db, clazz)) { - String sql = "DELETE FROM " + tableName; - db.execSQL(sql); - } - } - - /** - * 检查某个字段的值是否存在 - * - * @param expression 字段和值"url=xxx" - * @return {@code true}该字段的对应的value已存在 - */ - static boolean checkDataExist(SQLiteDatabase db, Class clazz, - String... expression) { - db = checkDb(db); - if (!CommonUtil.checkSqlExpression(expression)) { - return false; - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s ", CommonUtil.getClassName(clazz), - expression[0]); - sql = sql.replace("?", "%s"); - Object[] params = new String[expression.length - 1]; - for (int i = 0, len = params.length; i < len; i++) { - params[i] = String.format("'%s'", SqlUtil.encodeStr(expression[i + 1])); - } - sql = String.format(sql, params); - Cursor cursor = db.rawQuery(sql, null); - final boolean isExist = cursor.getCount() > 0; - closeCursor(cursor); - return isExist; - } - - /** - * 通过class 获取该class的表字段 - * - * @return 表字段列表 - */ - static List getColumns(Class clazz) { - List columns = new ArrayList<>(); - List fields = CommonUtil.getAllFields(clazz); - for (Field field : fields) { - field.setAccessible(true); - if (SqlUtil.isIgnore(field)) { - continue; - } - columns.add(field.getName()); - } - return columns; - } - - /** - * 检查数据库是否关闭,已经关闭的话,打开数据库 - * - * @return 返回数据库 - */ - static SQLiteDatabase checkDb(SQLiteDatabase db) { - if (db == null || !db.isOpen()) { - db = SqlHelper.getInstance().getDb(); - } - return db; - } - - /** - * 创建表 - * - * @param clazz 数据库实体 - */ - static void createTable(SQLiteDatabase db, Class clazz) { - db = checkDb(db); - List fields = CommonUtil.getAllFields(clazz); - if (fields != null && fields.size() > 0) { - //外键Map,在Sqlite3中foreign修饰的字段必须放在最后 - final List foreignArray = new ArrayList<>(); - StringBuilder sb = new StringBuilder(); - sb.append("CREATE TABLE IF NOT EXISTS ") - .append(CommonUtil.getClassName(clazz)) - .append(" ("); - for (Field field : fields) { - field.setAccessible(true); - if (SqlUtil.isIgnore(field)) { - continue; - } - Class type = field.getType(); - String columnType = getColumnType(type); - if (columnType == null) { - continue; - } - sb.append(field.getName()); - sb.append(" ").append(columnType); - - if (SqlUtil.isPrimary(field)) { - Primary pk = field.getAnnotation(Primary.class); - sb.append(" PRIMARY KEY"); - if (pk.autoincrement() && (type == int.class || type == Integer.class)) { - sb.append(" AUTOINCREMENT"); - } - } - - if (SqlUtil.isForeign(field)) { - foreignArray.add(field); - } - - if (SqlUtil.isNoNull(field)) { - sb.append(" NOT NULL"); - } - - if (SqlUtil.isDefault(field)) { - Default d = field.getAnnotation(Default.class); - if (!TextUtils.isEmpty(d.value())) { - sb.append(" ERROR ").append("'").append(d.value()).append("'"); - } - } - - if (SqlUtil.isUnique(field)) { - sb.append(" UNIQUE"); - } - - sb.append(","); - } - - for (Field field : foreignArray) { - Foreign foreign = field.getAnnotation(Foreign.class); - sb.append("FOREIGN KEY (") - .append(field.getName()) - .append(") REFERENCES ") - .append(CommonUtil.getClassName(foreign.parent())) - .append("(") - .append(foreign.column()) - .append(")"); - ActionPolicy update = foreign.onUpdate(); - ActionPolicy delete = foreign.onDelete(); - if (update != ActionPolicy.NO_ACTION) { - sb.append(" ON UPDATE ").append(update.function); - } - - if (delete != ActionPolicy.NO_ACTION) { - sb.append(" ON DELETE ").append(update.function); - } - sb.append(","); - } - - String str = sb.toString(); - str = str.substring(0, str.length() - 1) + ");"; - ALog.d(TAG, "创建表的sql:" + str); - db.execSQL(str); - } - } - - /** - * 根据字段名获取字段类型 - */ - static String getColumnTypeByFieldName(Class tabClass, String fieldName) { - List fields = CommonUtil.getAllFields(tabClass); - for (Field field : fields) { - if (field.getName().equals(fieldName)) { - return getColumnType(field.getType()); - } - } - return null; - } - - /** - * 获取字段类型 - */ - static String getColumnType(Class fieldtype) { - if (fieldtype == String.class || fieldtype.isEnum()) { - return "VARCHAR"; - } else if (fieldtype == int.class || fieldtype == Integer.class) { - return "INTEGER"; - } else if (fieldtype == float.class || fieldtype == Float.class) { - return "FLOAT"; - } else if (fieldtype == double.class || fieldtype == Double.class) { - return "DOUBLE"; - } else if (fieldtype == long.class || fieldtype == Long.class) { - return "BIGINT"; - } else if (fieldtype == boolean.class || fieldtype == Boolean.class) { - return "BOOLEAN"; - } else if (fieldtype == java.util.Date.class || fieldtype == java.sql.Date.class) { - return "DATA"; - } else if (fieldtype == byte.class || fieldtype == Byte.class) { - return "BLOB"; - } else if (fieldtype == Map.class || fieldtype == List.class) { - return "TEXT"; - } else { - return null; - } - } - - /** - * URL编码字符串 - * - * @param str 原始字符串 - * @return 编码后的字符串 - */ - static String encodeStr(String str) { - str = str.replaceAll("\\+", "%2B"); - return URLEncoder.encode(str); - } - - /** - * 获取主键字段名 - */ - static String getPrimaryName(Class clazz) { - List fields = CommonUtil.getAllFields(clazz); - String column; - if (fields != null && !fields.isEmpty()) { - - for (Field field : fields) { - field.setAccessible(true); - if (isPrimary(field)) { - column = field.getName(); - return column; - } - } - } - return null; - } - - /** - * 获取类中所有不被忽略的字段 - */ - static List getAllNotIgnoreField(Class clazz) { - List fields = CommonUtil.getAllFields(clazz); - List temp = new ArrayList<>(); - if (fields != null && fields.size() > 0) { - for (Field f : fields) { - f.setAccessible(true); - if (!isIgnore(f)) { - temp.add(f); - } - } - return temp; - } else { - return null; - } - } - - /** - * 列表数据转字符串 - * - * @param field list反射字段 - */ - static String list2Str(DbEntity dbEntity, Field field) throws IllegalAccessException { - List list = (List) field.get(dbEntity); - if (list == null || list.isEmpty()) return ""; - StringBuilder sb = new StringBuilder(); - for (Object aList : list) { - sb.append(aList).append("$$"); - } - return sb.toString(); - } - - /** - * 字符串转列表 - * - * @param str 数据库中的字段 - * @return 如果str为null,则返回null - */ - static List str2List(String str, Field field) { - if (TextUtils.isEmpty(str)) return null; - String[] datas = str.split("\\$\\$"); - List list = new ArrayList(); - Class clazz = CommonUtil.getListParamType(field); - if (clazz != null) { - String type = clazz.getName(); - for (String data : datas) { - list.add(checkData(type, data)); - } - } - - return list; - } - - /** - * 字符串转Map,只支持 - *

-   *   {@code Map}
-   * 
- */ - static Map str2Map(String str) { - Map map = new HashMap<>(); - if (TextUtils.isEmpty(str)) { - return map; - } - boolean isDecode = false; - if (str.endsWith("_&_decode_&_")) { - isDecode = true; - str = str.substring(0, str.length() - 12); - } - String[] element = str.split(","); - for (String data : element) { - String[] s = data.split("\\$"); - if (isDecode) { - map.put(CommonUtil.decryptBASE64(s[0]), CommonUtil.decryptBASE64(s[1])); - } else { - map.put(s[0], s[1]); - } - } - return map; - } - - /** - * Map转字符串,只支持 - *
-   *   {@code Map}
-   * 
- */ - static String map2Str(Map map) { - StringBuilder sb = new StringBuilder(); - Set keys = map.keySet(); - for (String key : keys) { - sb.append(CommonUtil.encryptBASE64(key)) - .append("$") - .append(CommonUtil.encryptBASE64(map.get(key))) - .append(","); - } - String str = sb.toString(); - str = TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1); - //3.3.10版本之前没有decode,需要加标志 - if (map.size() != 0) { - str += "_&_decode_&_"; - } - return str; - } - - /** - * shadow$_klass_、shadow$_monitor_、{@link Ignore}、rowID、{@link Field#isSynthetic()}、{@link - * Modifier#isFinal(int)}、{@link Modifier#isStatic(int)}将被忽略 - * - * @return true 忽略该字段 - */ - static boolean isIgnore(Field field) { - // field.isSynthetic(), 使用as热启动App时,AS会自动给你的class添加change字段 - Ignore ignore = field.getAnnotation(Ignore.class); - int modifiers = field.getModifiers(); - String fieldName = field.getName(); - return (ignore != null && ignore.value()) || fieldName.equals("rowID") || fieldName.equals( - AriaConfig.IGNORE_CLASS_KLASS) || fieldName.equals(AriaConfig.IGNORE_CLASS_MONITOR) - || field.isSynthetic() || Modifier - .isStatic(modifiers) || Modifier.isFinal(modifiers); - } - - /** - * 判断是否是Wrapper注解 - * - * @return {@code true} 是 - */ - static boolean isWrapper(Class clazz) { - Wrapper w = clazz.getAnnotation(Wrapper.class); - return w != null; - } - - /** - * 判断是否一对多注解 - */ - static boolean isMany(Field field) { - Many oneToMany = field.getAnnotation(Many.class); - return oneToMany != null; - } - - /** - * 判断是否是一对一注解 - */ - static boolean isOne(Field field) { - One oneToOne = field.getAnnotation(One.class); - return oneToOne != null; - } - - /** - * 判断是否是主键约束 - * - * @return {@code true}主键约束 - */ - static boolean isPrimary(Field field) { - Primary pk = field.getAnnotation(Primary.class); - return pk != null; - } - - /** - * 判断是否是外键约束 - * - * @return {@code true}外键约束 - */ - static boolean isForeign(Field field) { - Foreign fk = field.getAnnotation(Foreign.class); - return fk != null; - } - - /** - * 判断是否是非空约束 - * - * @return {@code true}为非空约束 - */ - static boolean isNoNull(Field field) { - NoNull nn = field.getAnnotation(NoNull.class); - return nn != null; - } - - /** - * 判断是否是default - * - * @return {@code true}为default - */ - static boolean isDefault(Field field) { - Default nn = field.getAnnotation(Default.class); - return nn != null; - } - - /** - * 判断是否是Unique - * - * @return {@code true}为Unique - */ - static boolean isUnique(Field field) { - Unique nn = field.getAnnotation(Unique.class); - return nn != null; - } - - private static Object checkData(String type, String data) { - if (type.equalsIgnoreCase("java.lang.String")) { - return data; - } else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")) { - return Integer.parseInt(data); - } else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")) { - return Double.parseDouble(data); - } else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")) { - return Float.parseFloat(data); - } - return null; - } -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Default.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Default.java deleted file mode 100644 index dcc81434..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Default.java +++ /dev/null @@ -1,30 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by lyy on 2015/11/2. - * 默认数据 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Default { - String value() default ""; -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java deleted file mode 100644 index 6371a4d7..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java +++ /dev/null @@ -1,50 +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.orm.annotation; - -import com.arialyy.aria.orm.ActionPolicy; -import com.arialyy.aria.orm.DbEntity; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by AriaL on 2017/7/4. - * 外键约束 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Foreign { - - /** - * 关联的表 - */ - Class parent(); - - /** - * 父表对应的列名 - */ - String column(); - - /** - * ON UPDATE 约束 - */ - ActionPolicy onUpdate() default ActionPolicy.NO_ACTION; - - /** - * ON DELETE 约束 - */ - ActionPolicy onDelete() default ActionPolicy.NO_ACTION; -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java deleted file mode 100644 index d8a9fe0d..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java +++ /dev/null @@ -1,30 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by lyy on 2015/11/2. - * 忽略某个字段 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Ignore { - boolean value() default true; -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Many.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Many.java deleted file mode 100644 index 03f6fd72..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Many.java +++ /dev/null @@ -1,44 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by AriaL on 2017/7/4. - * 一对多 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Many { - /** - * 父表对应的字段名 - */ - String parentColumn(); - - /** - * 父表在子表对应的字段 - */ - String entityColumn(); - - ///** - // * 是否是一对一关系 - // * - // * @return {@code true} 是,{@code false} 不是 - // */ - //boolean isOne2One() default false; -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java deleted file mode 100644 index d29c8ad9..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java +++ /dev/null @@ -1,30 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by lyy on 2015/11/2. - * 非空字段 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface NoNull { - boolean value() default true; -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/One.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/One.java deleted file mode 100644 index 3f8df08e..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/One.java +++ /dev/null @@ -1,29 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by AriaL on 2017/7/4. - * 一 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface One { - -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Primary.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Primary.java deleted file mode 100644 index 4ac2fb1c..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Primary.java +++ /dev/null @@ -1,35 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by lyy on 2015/11/2. - * 表ID字段指定 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Primary { - int value() default -1; - - /** - * 字段需要int类型才可以自增 - */ - boolean autoincrement() default false; -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Table.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Table.java deleted file mode 100644 index e2cbbb94..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Table.java +++ /dev/null @@ -1,30 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by lyy on 2015/11/2. - * 设置表名,如果不是使用该注解,默认为类名 - */ -@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Table { - String tableName(); -} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Unique.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Unique.java deleted file mode 100644 index a0316388..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Unique.java +++ /dev/null @@ -1,29 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by laoyuyu on 2018/5/8. - * 数据库 UNIQUE 约束 - */ -@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) -public @interface Unique { -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java deleted file mode 100644 index e0894440..00000000 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java +++ /dev/null @@ -1,29 +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.orm.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by laoyuyu on 2018/3/21. - * 关系包裹 - */ -@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -public @interface Wrapper { -} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java index c409efb1..b49f5e4c 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java @@ -19,7 +19,7 @@ import android.os.Handler; import com.arialyy.aria.core.TaskOptionParams; import com.arialyy.aria.core.inf.IEventHandler; import com.arialyy.aria.core.inf.ITaskOption; -import com.arialyy.aria.core.inf.ITaskUtil; +import com.arialyy.aria.core.inf.ITaskAdapter; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; @@ -104,7 +104,7 @@ public class ComponentUtil { * * @return 返回任务工具 */ - public synchronized T buildUtil(AbsTaskWrapper wrapper, + public synchronized T buildUtil(AbsTaskWrapper wrapper, IEventListener listener) { int requestType = wrapper.getRequestType(); String className = null; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/DeleteM3u8Record.java b/PublicComponent/src/main/java/com/arialyy/aria/util/DeleteM3u8Record.java index 919eeb70..d61c50f8 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/util/DeleteM3u8Record.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/DeleteM3u8Record.java @@ -20,7 +20,7 @@ import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.common.M3U8Entity; import com.arialyy.aria.orm.DbEntity; import java.io.File; diff --git a/PublicComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener b/PublicComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener deleted file mode 100644 index 6120f8d5..00000000 --- a/PublicComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener +++ /dev/null @@ -1,3 +0,0 @@ -com.arialyy.aria.core.listener.BaseDListener -com.arialyy.aria.core.listener.BaseUListener -com.arialyy.aria.core.listener.DownloadGroupListener \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java index eeda1e80..b335a092 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java @@ -32,7 +32,7 @@ import com.arialyy.annotations.M3U8; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.common.M3U8Entity; import com.arialyy.aria.core.download.m3u8.M3U8VodOption; import com.arialyy.aria.core.processor.IBandWidthUrlConverter; import com.arialyy.aria.core.processor.IKeyUrlConverter; diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java index 0d281500..4dc76144 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java +++ b/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java @@ -1,8 +1,6 @@ package com.arialyy.simple.core.download.m3u8; import android.annotation.SuppressLint; -import android.graphics.Canvas; -import android.graphics.Color; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Build; @@ -15,7 +13,7 @@ import android.widget.SeekBar; import androidx.annotation.RequiresApi; import androidx.lifecycle.ViewModelProviders; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.common.M3U8Entity; import com.arialyy.aria.util.ALog; import com.arialyy.frame.base.BaseFragment; import com.arialyy.simple.R;