diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoader.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoader.java index aa0f9f25..fcf259cd 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoader.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoader.java @@ -45,7 +45,9 @@ final class FtpDGLoader extends AbsGroupLoader { @Override protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { - return new FtpSubDLoaderUtil(wrapper, getScheduler(), needGetFileInfo, getKey()); + FtpSubDLoaderUtil subUtil = new FtpSubDLoaderUtil(getScheduler(), needGetFileInfo, getKey()); + subUtil.setParams(wrapper, null); + return subUtil; } /** diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoaderUtil.java index 28f2e716..b11ec04e 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoaderUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoaderUtil.java @@ -18,7 +18,6 @@ package com.arialyy.aria.ftp.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.IEventListener; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.ftp.FtpTaskOption; @@ -29,13 +28,12 @@ import com.arialyy.aria.ftp.FtpTaskOption; */ public final class FtpDGLoaderUtil extends AbsGroupLoaderUtil { - public FtpDGLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(FtpTaskOption.class); - } - @Override protected AbsGroupLoader getLoader() { - return mLoader == null ? new FtpDGLoader(getTaskWrapper(), getListener()) : mLoader; + if (mLoader == null) { + ((AbsTaskWrapper) getTaskWrapper()).generateTaskOption(FtpTaskOption.class); + mLoader = new FtpDGLoader((AbsTaskWrapper) getTaskWrapper(), getListener()); + } + return mLoader; } @Override protected LoaderStructure buildLoaderStructure() { diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java index 129365ac..8d5b5c67 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java @@ -16,14 +16,12 @@ package com.arialyy.aria.ftp.download; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.ftp.FtpTaskOption; /** @@ -32,13 +30,12 @@ import com.arialyy.aria.ftp.FtpTaskOption; */ public final class FtpDLoaderUtil extends AbsNormalLoaderUtil { - public FtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(FtpTaskOption.class); - } - @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new NormalLoader(getTaskWrapper(), getListener()) : mLoader; + if (mLoader == null){ + getTaskWrapper().generateTaskOption(FtpTaskOption.class); + mLoader = new NormalLoader(getTaskWrapper(), getListener()); + } + return mLoader ; } public LoaderStructure BuildLoaderStructure() { diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java index 7ae8c4dc..6a5eaa06 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java @@ -32,9 +32,8 @@ final class FtpSubDLoaderUtil extends AbsSubDLoadUtil { * @param schedulers 调度器 * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - FtpSubDLoaderUtil(DTaskWrapper taskWrapper, Handler schedulers, boolean needGetInfo, - String parentKey) { - super(taskWrapper, schedulers, needGetInfo, parentKey); + FtpSubDLoaderUtil(Handler schedulers, boolean needGetInfo, String parentKey) { + super(schedulers, needGetInfo, parentKey); } @Override protected SubLoader getLoader() { diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java index 6e6c9381..232eac2c 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java @@ -17,7 +17,6 @@ package com.arialyy.aria.ftp.upload; import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter; @@ -26,7 +25,6 @@ import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.task.IThreadTaskAdapter; import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.ftp.FtpTaskOption; /** @@ -35,14 +33,12 @@ import com.arialyy.aria.ftp.FtpTaskOption; */ public final class FtpULoaderUtil extends AbsNormalLoaderUtil { - public FtpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(FtpTaskOption.class); - } - @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new FtpULoader((UTaskWrapper) getTaskWrapper(), getListener()) - : mLoader; + if (mLoader == null) { + getTaskWrapper().generateTaskOption(FtpTaskOption.class); + mLoader = new FtpULoader((UTaskWrapper) getTaskWrapper(), getListener()); + } + return mLoader; } @Override public LoaderStructure BuildLoaderStructure() { diff --git a/FtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil b/FtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil new file mode 100644 index 00000000..dc09cdf1 --- /dev/null +++ b/FtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil @@ -0,0 +1,3 @@ +com.arialyy.aria.ftp.download.FtpDLoaderUtil +com.arialyy.aria.ftp.download.FtpDGLoaderUtil +com.arialyy.aria.ftp.upload.FtpULoaderUtil \ No newline at end of file diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java index 3bd59e0d..9179f151 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java @@ -45,7 +45,9 @@ final class HttpDGLoader extends AbsGroupLoader { @Override protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { - return new HttpSubDLoaderUtil(wrapper, getScheduler(), needGetFileInfo, getKey()); + HttpSubDLoaderUtil subUtil = new HttpSubDLoaderUtil(getScheduler(), needGetFileInfo, getKey()); + subUtil.setParams(wrapper, null); + return subUtil; } private void startSub() { diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java index 2777ddcc..cfeecd2a 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java @@ -19,9 +19,7 @@ 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.listener.IEventListener; import com.arialyy.aria.core.loader.LoaderStructure; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.http.HttpTaskOption; /** @@ -30,14 +28,12 @@ import com.arialyy.aria.http.HttpTaskOption; */ public final class HttpDGLoaderUtil extends AbsGroupLoaderUtil { - public HttpDGLoaderUtil(AbsTaskWrapper taskWrapper, IEventListener listener) { - super(taskWrapper, listener); - taskWrapper.generateTaskOption(HttpTaskOption.class); - } - @Override protected AbsGroupLoader getLoader() { - return mLoader == null ? new HttpDGLoader(getTaskWrapper(), - (DownloadGroupListener) getListener()) : mLoader; + if (mLoader == null) { + getTaskWrapper().generateTaskOption(HttpTaskOption.class); + mLoader = new HttpDGLoader(getTaskWrapper(), (DownloadGroupListener) getListener()); + } + return mLoader; } @Override protected LoaderStructure buildLoaderStructure() { diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java index 4cb2a09c..5feaf9cd 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java @@ -24,6 +24,7 @@ import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.http.HttpRecordHandler; import com.arialyy.aria.http.HttpTaskOption; @@ -32,13 +33,13 @@ import com.arialyy.aria.http.HttpTaskOption; * @Date 2019-09-21 */ public final class HttpDLoaderUtil extends AbsNormalLoaderUtil { - public HttpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(HttpTaskOption.class); - } @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new NormalLoader(getTaskWrapper(), getListener()) : mLoader; + if (mLoader == null){ + getTaskWrapper().generateTaskOption(HttpTaskOption.class); + mLoader = new NormalLoader(getTaskWrapper(), getListener()); + } + return mLoader; } public LoaderStructure BuildLoaderStructure() { diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java index b51a5da6..75182bb2 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java @@ -33,9 +33,8 @@ final class HttpSubDLoaderUtil extends AbsSubDLoadUtil { * @param schedulers 调度器 * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - HttpSubDLoaderUtil(DTaskWrapper taskWrapper, Handler schedulers, boolean needGetInfo, - String parentKey) { - super(taskWrapper, schedulers, needGetInfo, parentKey); + HttpSubDLoaderUtil( Handler schedulers, boolean needGetInfo, String parentKey) { + super(schedulers, needGetInfo, parentKey); } @Override protected SubLoader getLoader() { diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java index b0b3c060..145fd92a 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java @@ -17,7 +17,6 @@ package com.arialyy.aria.http.upload; import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter; @@ -26,7 +25,6 @@ import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.task.IThreadTaskAdapter; import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.http.HttpRecordHandler; import com.arialyy.aria.http.HttpTaskOption; @@ -35,14 +33,13 @@ import com.arialyy.aria.http.HttpTaskOption; * @Date 2019-09-19 */ public final class HttpULoaderUtil extends AbsNormalLoaderUtil { - public HttpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(HttpTaskOption.class); - } @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new HttpULoader((UTaskWrapper) getTaskWrapper(), getListener()) - : mLoader; + if (mLoader == null) { + getTaskWrapper().generateTaskOption(HttpTaskOption.class); + mLoader = new HttpULoader((UTaskWrapper) getTaskWrapper(), getListener()); + } + return mLoader; } @Override public LoaderStructure BuildLoaderStructure() { diff --git a/HttpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil b/HttpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil new file mode 100644 index 00000000..62adaa3f --- /dev/null +++ b/HttpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil @@ -0,0 +1,3 @@ +com.arialyy.aria.http.download.HttpDLoaderUtil +com.arialyy.aria.http.download.HttpDGLoaderUtil +com.arialyy.aria.http.upload.HttpULoaderUtil diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java index c1786641..9f2ce537 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java @@ -16,29 +16,20 @@ package com.arialyy.aria.m3u8; import android.os.Bundle; -import android.os.Handler; import android.os.Message; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.listener.BaseListener; import com.arialyy.aria.core.listener.IDLoadListener; import com.arialyy.aria.core.listener.ISchedulers; -import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.DeleteM3u8Record; /** * 下载监听类 */ -public final class M3U8Listener - extends BaseListener> - implements IDLoadListener { - - public M3U8Listener(AbsTask task, Handler outHandler) { - super(task, outHandler); - } +public final class M3U8Listener extends BaseListener implements IDLoadListener { @Override public void onPostPre(long fileSize) { @@ -88,7 +79,7 @@ public final class M3U8Listener } @Override protected void handleCancel() { - int sType = getTask().getSchedulerType(); + int sType = getTask(DownloadTask.class).getSchedulerType(); if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { mEntity.setComplete(false); mEntity.setState(IEntity.STATE_WAIT); diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java index a447be99..f6a65702 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java @@ -16,10 +16,8 @@ package com.arialyy.aria.m3u8.live; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.m3u8.M3U8InfoTask; import com.arialyy.aria.m3u8.M3U8Listener; @@ -36,8 +34,7 @@ import com.arialyy.aria.m3u8.M3U8TaskOption; */ public class M3U8LiveUtil extends AbsNormalLoaderUtil { - public M3U8LiveUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); + public M3U8LiveUtil() { } @Override public DTaskWrapper getTaskWrapper() { @@ -45,11 +42,12 @@ public class M3U8LiveUtil extends AbsNormalLoaderUtil { } @Override public M3U8LiveLoader getLoader() { - getTaskWrapper().generateM3u8Option(M3U8TaskOption.class); - getTaskWrapper().generateTaskOption(HttpTaskOption.class); - return - mLoader == null ? new M3U8LiveLoader(getTaskWrapper(), (M3U8Listener) getListener()) - : (M3U8LiveLoader) mLoader; + if (mLoader == null) { + getTaskWrapper().generateM3u8Option(M3U8TaskOption.class); + getTaskWrapper().generateTaskOption(HttpTaskOption.class); + mLoader = new M3U8LiveLoader(getTaskWrapper(), (M3U8Listener) getListener()); + } + return (M3U8LiveLoader) mLoader; } @Override public LoaderStructure BuildLoaderStructure() { diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java index b02b57a1..afa03ad4 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java @@ -16,11 +16,9 @@ package com.arialyy.aria.m3u8.vod; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.m3u8.M3U8InfoTask; import com.arialyy.aria.m3u8.M3U8Listener; @@ -36,8 +34,7 @@ import com.arialyy.aria.m3u8.M3U8TaskOption; */ public final class M3U8VodUtil extends AbsNormalLoaderUtil { - public M3U8VodUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); + public M3U8VodUtil() { } @Override public DTaskWrapper getTaskWrapper() { @@ -45,10 +42,12 @@ public final class M3U8VodUtil extends AbsNormalLoaderUtil { } @Override public AbsNormalLoader getLoader() { - getTaskWrapper().generateM3u8Option(M3U8TaskOption.class); - getTaskWrapper().generateTaskOption(HttpTaskOption.class); - return mLoader == null ? new M3U8VodLoader(getTaskWrapper(), (M3U8Listener) getListener()) - : mLoader; + if (mLoader == null) { + getTaskWrapper().generateM3u8Option(M3U8TaskOption.class); + getTaskWrapper().generateTaskOption(HttpTaskOption.class); + mLoader = new M3U8VodLoader(getTaskWrapper(), (M3U8Listener) getListener()); + } + return mLoader; } @Override public LoaderStructure BuildLoaderStructure() { diff --git a/M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil b/M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil new file mode 100644 index 00000000..53dd09b6 --- /dev/null +++ b/M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil @@ -0,0 +1,2 @@ +com.arialyy.aria.m3u8.live.M3U8LiveUtil +com.arialyy.aria.m3u8.vod.M3U8VodUtil \ No newline at end of file diff --git a/M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener b/M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener new file mode 100644 index 00000000..32784f04 --- /dev/null +++ b/M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener @@ -0,0 +1 @@ +com.arialyy.aria.m3u8.M3U8Listener \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java index 678de87d..c333eaee 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java @@ -19,6 +19,7 @@ import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -34,10 +35,12 @@ public abstract class AbsGroupLoaderUtil implements IUtil { private AbsTaskWrapper mTaskWrapper; private boolean isStop = false, isCancel = false; - protected AbsGroupLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - mTaskWrapper = wrapper; + + @Override public IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { + mTaskWrapper = taskWrapper; mListener = listener; mLoader = getLoader(); + return this; } protected abstract AbsGroupLoader 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/AbsSubDLoadUtil.java index 3ddcc0ad..8a31688c 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java @@ -15,20 +15,16 @@ */ package com.arialyy.aria.core.group; -import android.os.Bundle; import android.os.Handler; -import android.os.Message; - 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.IThreadStateManager; import com.arialyy.aria.core.inf.IUtil; -import com.arialyy.aria.core.listener.IDLoadListener; +import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.SubLoader; -import com.arialyy.aria.exception.AriaException; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -49,12 +45,16 @@ public abstract class AbsSubDLoadUtil implements IUtil, Runnable { * @param schedulers 调度器 * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - protected AbsSubDLoadUtil(DTaskWrapper taskWrapper, Handler schedulers, boolean needGetInfo, String parentKey) { - mWrapper = taskWrapper; + protected AbsSubDLoadUtil(Handler schedulers, boolean needGetInfo, String parentKey) { mSchedulers = schedulers; this.parentKey = parentKey; this.needGetInfo = needGetInfo; + } + + @Override public IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { + mWrapper = (DTaskWrapper) taskWrapper; mDLoader = getLoader(); + return this; } /** diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java index a2f29a92..dd23b61b 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java @@ -18,7 +18,9 @@ package com.arialyy.aria.core.inf; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by lyy on 2016/10/31. @@ -26,6 +28,8 @@ import com.arialyy.aria.core.upload.UploadEntity; */ public interface IUtil { + IUtil setParams(AbsTaskWrapper taskWrapper, 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 index 98c113c5..862247b3 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java @@ -15,26 +15,16 @@ */ package com.arialyy.aria.core.listener; -import android.os.Handler; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.loader.IRecordHandler; import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.DeleteDRecord; -import com.arialyy.aria.util.RecordUtil; /** * 下载监听类 */ -public class BaseDListener extends BaseListener> - implements IDLoadListener { - - public BaseDListener(AbsTask task, Handler outHandler) { - super(task, outHandler); - } +public class BaseDListener extends BaseListener implements IDLoadListener { @Override public void onPostPre(long fileSize) { @@ -52,7 +42,7 @@ public class BaseDListener extends BaseListener, - TASK extends AbsTask> - implements IEventListener { - protected static String TAG; - protected static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 +public abstract class BaseListener implements IEventListener { + protected String TAG = getClass().getSimpleName(); + static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 protected SoftReference outHandler; private long mLastLen; //上一次发送长度 private boolean isFirst = true; - private TASK mTask; - protected long mLastSaveTime; - protected ENTITY mEntity; - protected TASK_WRAPPER mTaskWrapper; + private AbsTask mTask; + long mLastSaveTime; + protected AbsEntity mEntity; + protected AbsTaskWrapper mTaskWrapper; private boolean isConvertSpeed; private long mUpdateInterval; - protected BaseListener(TASK task, Handler outHandler) { + @Override public IEventListener setParams(AbsTask task, Handler outHandler) { this.outHandler = new SoftReference<>(outHandler); mTask = new WeakReference<>(task).get(); mEntity = mTask.getTaskWrapper().getEntity(); @@ -54,10 +52,11 @@ public abstract class BaseListener TASK getTask(Class clazz) { + return (TASK) mTask; } @Override public void onPre() { 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 index 3a8dd519..0b16ecfa 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseUListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseUListener.java @@ -15,26 +15,18 @@ */ package com.arialyy.aria.core.listener; -import android.os.Handler; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.task.AbsTask; -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.task.UploadTask; import com.arialyy.aria.util.DeleteURecord; /** * 下载监听类 */ -public class BaseUListener extends BaseListener> - implements IUploadListener { - - public BaseUListener(AbsTask task, Handler outHandler) { - super(task, outHandler); - } +public class BaseUListener extends BaseListener implements IUploadListener { @Override protected void handleCancel() { - int sType = getTask().getSchedulerType(); + int sType = getTask(UploadTask.class).getSchedulerType(); if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { mEntity.setComplete(false); mEntity.setState(IEntity.STATE_WAIT); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java index b6fff3bc..12beb071 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java @@ -16,7 +16,6 @@ package com.arialyy.aria.core.listener; import android.os.Handler; -import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.group.GroupSendParams; @@ -35,15 +34,14 @@ import static com.arialyy.aria.core.task.AbsTask.ERROR_INFO_KEY; /** * Created by Aria.Lao on 2017/7/20. 任务组下载事件 */ -public class DownloadGroupListener - extends BaseListener> - implements IDGroupListener { +public class DownloadGroupListener extends BaseListener implements IDGroupListener { private GroupSendParams mSeedEntity; - public DownloadGroupListener(AbsTask task, Handler outHandler) { - super(task, outHandler); + @Override public IEventListener setParams(AbsTask task, Handler outHandler) { + IEventListener listener = super.setParams(task, outHandler); mSeedEntity = new GroupSendParams<>(); mSeedEntity.groupTask = (DownloadGroupTask) task; + return listener; } @Override @@ -164,12 +162,13 @@ public class DownloadGroupListener } private void saveCurrentLocation() { - if (mEntity.getSubEntities() == null || mEntity.getSubEntities().isEmpty()) { + DownloadGroupEntity dgEntity = (DownloadGroupEntity) mEntity; + if (dgEntity.getSubEntities() == null || dgEntity.getSubEntities().isEmpty()) { ALog.w(TAG, "保存进度失败,子任务为null"); return; } long location = 0; - for (DownloadEntity e : mEntity.getSubEntities()) { + for (DownloadEntity e : dgEntity.getSubEntities()) { location += e.getCurrentProgress(); } if (location > mEntity.getFileSize()) { @@ -193,7 +192,7 @@ public class DownloadGroupListener } @Override protected void handleCancel() { - int sType = getTask().getSchedulerType(); + int sType = getTask(DownloadGroupTask.class).getSchedulerType(); if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { mEntity.setComplete(false); mEntity.setState(IEntity.STATE_WAIT); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java index 2b84a233..cc4156fe 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java @@ -15,6 +15,8 @@ */ package com.arialyy.aria.core.listener; +import android.os.Handler; +import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.exception.AriaException; /** @@ -23,6 +25,8 @@ import com.arialyy.aria.exception.AriaException; */ public interface IEventListener { + IEventListener setParams(AbsTask task, Handler outHandler); + /** * 预处理,有时有些地址链接比较慢,这时可以先在这个地方出来一些界面上的UI,如按钮的状态 */ diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java index b3623245..04002742 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java @@ -19,6 +19,7 @@ package com.arialyy.aria.core.loader; import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.exception.AriaException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -34,10 +35,14 @@ public abstract class AbsNormalLoaderUtil implements IUtil { private AbsTaskWrapper mTaskWrapper; private boolean isStop = false, isCancel = false; - protected AbsNormalLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - mTaskWrapper = wrapper; + protected AbsNormalLoaderUtil() { + } + + @Override public IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) { + mTaskWrapper = taskWrapper; mListener = listener; mLoader = getLoader(); + return this; } /** 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 22b79825..0f6a4872 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 @@ -64,7 +64,7 @@ public abstract class AbsTask return mOutHandler; } - protected synchronized IUtil getUtil() { + synchronized IUtil getUtil() { if (mUtil == null) { mUtil = ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); } 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 index cf1a3fd7..f59e4fbb 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java @@ -29,8 +29,7 @@ import com.arialyy.aria.util.ComponentUtil; /** * Created by lyy on 2017/2/23. 所有任务实体的父类 */ -public abstract class AbsTaskWrapper - implements ITaskWrapper { +public abstract class AbsTaskWrapper implements ITaskWrapper { /** * 刷新信息 {@code true} 重新刷新下载信息 diff --git a/PublicComponent/src/main/java/com/arialyy/aria/exception/AriaComponentException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/AriaComponentException.java new file mode 100644 index 00000000..4b38b9ef --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/exception/AriaComponentException.java @@ -0,0 +1,26 @@ +/* + * 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.exception; + +public class AriaComponentException extends RuntimeException { + public AriaComponentException(String message, Exception e) { + super(message, e); + } + + public AriaComponentException(String s) { + super(s); + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/AriaServiceLoader.java b/PublicComponent/src/main/java/com/arialyy/aria/util/AriaServiceLoader.java new file mode 100644 index 00000000..2061c144 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/AriaServiceLoader.java @@ -0,0 +1,235 @@ +/* + * 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.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +/** + * aria 服务加载器 + * 参考{@link ServiceLoader} + */ +public class AriaServiceLoader { + + private static final String PREFIX = "META-INF/services/"; + + // The class or interface representing the service being loaded + private final Class service; + + // The class loader used to locate, load, and instantiate providers + private final ClassLoader loader; + + // The access control context taken when the ServiceLoader is created + // Android-changed: do not use legacy security code. + // private final AccessControlContext acc; + + private LazyLoader lazyLoader; + + public void reload() { + lazyLoader = new LazyLoader(service, loader); + } + + private AriaServiceLoader(Class svc, ClassLoader cl) { + service = svc; + loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl; + // Android-changed: Do not use legacy security code. + // On Android, System.getSecurityManager() is always null. + // acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null; + reload(); + } + + public static AriaServiceLoader load(Class service, ClassLoader loader) { + return new AriaServiceLoader<>(service, loader); + } + + public static AriaServiceLoader load(Class service) { + Thread.currentThread().setContextClassLoader(AriaServiceLoader.class.getClassLoader()); + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + return AriaServiceLoader.load(service, cl); + } + + public S getService(String serviceName) { + return lazyLoader.loadService(serviceName); + } + + private class LazyLoader { + private Class service; + private ClassLoader loader; + private Enumeration configs = null; + private Iterator pending = null; + + private LazyLoader(Class service, ClassLoader loader) { + this.service = service; + this.loader = loader; + parseConfig(); + } + + // Parse the content of the given URL as a provider-configuration file. + // + // @param service + // The service type for which providers are being sought; + // used to construct error detail strings + // + // @param u + // The URL naming the configuration file to be parsed + // + // @return A (possibly empty) iterator that will yield the provider-class + // names in the given configuration file that are not yet members + // of the returned set + // + // @throws ServiceConfigurationError + // If an I/O error occurs while reading from the given URL, or + // if a configuration-file format error is detected + // + private Iterator parse(Class service, URL u) throws ServiceConfigurationError { + InputStream in = null; + BufferedReader r = null; + ArrayList names = new ArrayList<>(); + try { + in = u.openStream(); + r = new BufferedReader(new InputStreamReader(in, "utf-8")); + int lc = 1; + while ((lc = parseLine(service, u, r, lc, names)) >= 0) ; + } catch (IOException x) { + fail(service, "Error reading configuration file", x); + } finally { + try { + if (r != null) r.close(); + if (in != null) in.close(); + } catch (IOException y) { + fail(service, "Error closing configuration file", y); + } + } + return names.iterator(); + } + + private void fail(Class service, String msg, Throwable cause) + throws ServiceConfigurationError { + throw new ServiceConfigurationError(service.getName() + ": " + msg, + cause); + } + + private void fail(Class service, String msg) + throws ServiceConfigurationError { + throw new ServiceConfigurationError(service.getName() + ": " + msg); + } + + private void fail(Class service, URL u, int line, String msg) + throws ServiceConfigurationError { + fail(service, u + ":" + line + ": " + msg); + } + + // Parse a single line from the given configuration file, adding the name + // on the line to the names list. + // + private int parseLine(Class service, URL u, BufferedReader r, int lc, + List names) + throws IOException, ServiceConfigurationError { + String ln = r.readLine(); + if (ln == null) { + return -1; + } + int ci = ln.indexOf('#'); + if (ci >= 0) ln = ln.substring(0, ci); + ln = ln.trim(); + int n = ln.length(); + if (n != 0) { + if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) { + fail(service, u, lc, "Illegal configuration-file syntax"); + } + int cp = ln.codePointAt(0); + if (!Character.isJavaIdentifierStart(cp)) { + fail(service, u, lc, "Illegal provider-class name: " + ln); + } + for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) { + cp = ln.codePointAt(i); + if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) { + fail(service, u, lc, "Illegal provider-class name: " + ln); + } + } + if (!names.contains(ln)) { + names.add(ln); + } + } + return lc + 1; + } + + private S loadService(String serviceName) { + while (pending.hasNext()) { + if (pending.next().equals(serviceName)) { + Class c = null; + try { + c = Class.forName(serviceName, false, loader); + } catch (ClassNotFoundException x) { + fail(service, + // Android-changed: Let the ServiceConfigurationError have a cause. + "Provider " + serviceName + " not found", x); + // "Provider " + cn + " not found"); + } + if (!service.isAssignableFrom(c)) { + // Android-changed: Let the ServiceConfigurationError have a cause. + ClassCastException cce = new ClassCastException( + service.getCanonicalName() + " is not assignable from " + c.getCanonicalName()); + fail(service, + "Provider " + serviceName + " not a subtype", cce); + // fail(service, + // "Provider " + cn + " not a subtype"); + } + try { + return service.cast(c.newInstance()); + } catch (Throwable x) { + fail(service, "Provider " + serviceName + " could not be instantiated", x); + } + } + } + + throw new Error(); // This cannot happen + } + + /** + * 解析配置文件 + */ + private void parseConfig() { + if (configs == null) { + try { + String fullName = PREFIX + service.getName(); + if (loader == null) { + configs = ClassLoader.getSystemResources(fullName); + } else { + configs = loader.getResources(fullName); + } + } catch (IOException x) { + fail(service, "Error locating configuration files", x); + } + } + while ((pending == null) || !pending.hasNext()) { + if (!configs.hasMoreElements()) { + return; + } + pending = parse(service, configs.nextElement()); + } + } + } +} 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 fb17e30c..286fcbe0 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java @@ -24,10 +24,9 @@ import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.exception.AriaComponentException; import java.lang.ref.SoftReference; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.util.List; /** @@ -44,9 +43,12 @@ public class ComponentUtil { private String TAG = CommonUtil.getClassName(getClass()); private static volatile ComponentUtil INSTANCE = null; + private AriaServiceLoader utilLoader; + private AriaServiceLoader listenerLoader; private ComponentUtil() { - + utilLoader = AriaServiceLoader.load(IUtil.class); + listenerLoader = AriaServiceLoader.load(IEventListener.class); } public static ComponentUtil getInstance() { @@ -99,12 +101,12 @@ public class ComponentUtil { } /** - * 创建下载工具 + * 创建任务工具 * - * @return 返回下载工具,创建失败返回null + * @return 返回任务工具 */ - public synchronized T buildUtil(AbsTaskWrapper wrapper, - IEventListener listener) { + public synchronized IUtil buildUtil(AbsTaskWrapper wrapper, IEventListener listener) { + utilLoader.reload(); int requestType = wrapper.getRequestType(); String className = null; switch (requestType) { @@ -139,29 +141,11 @@ public class ComponentUtil { className = "com.arialyy.aria.sftp.upload.SFtpULoaderUtil"; break; } - if (className == null) { - ALog.e(TAG, "不识别的类名:" + className); - return null; + IUtil util = utilLoader.getService(className); + if (util == null) { + throw new AriaComponentException("加载工具异常,请求类型:" + wrapper.getRequestType()); } - T util = null; - try { - Class clazz = (Class) getClass().getClassLoader().loadClass(className); - Class[] paramTypes = { AbsTaskWrapper.class, IEventListener.class }; - Object[] params = { wrapper, listener }; - Constructor con = clazz.getConstructor(paramTypes); - util = con.newInstance(params); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return util; + return util.setParams(wrapper, listener); } /** @@ -170,7 +154,7 @@ public class ComponentUtil { * @param wrapperType 任务类型{@link ITaskWrapper} * @return 返回事件监听,如果创建失败返回null */ - public synchronized T buildListener(int wrapperType, AbsTask task, + public synchronized IEventListener buildListener(int wrapperType, AbsTask task, Handler outHandler) { String className = null, errorStr = "请添加FTP插件"; switch (wrapperType) { @@ -197,25 +181,12 @@ public class ComponentUtil { if (className == null) { return null; } - T listener = null; - try { - Class clazz = (Class) getClass().getClassLoader().loadClass(className); - Class[] paramTypes = { AbsTask.class, Handler.class }; - Object[] params = { task, outHandler }; - Constructor con = clazz.getConstructor(paramTypes); - listener = con.newInstance(params); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(errorStr); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); + + IEventListener listener = listenerLoader.getService(className); + if (listener == null) { + throw new AriaComponentException(errorStr); } - return listener; + return listener.setParams(task, outHandler); } /** 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 new file mode 100644 index 00000000..6120f8d5 --- /dev/null +++ b/PublicComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener @@ -0,0 +1,3 @@ +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/SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderUtil.java b/SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderUtil.java index 44352a60..c0a32b69 100644 --- a/SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderUtil.java +++ b/SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderUtil.java @@ -16,13 +16,11 @@ package com.arialyy.aria.sftp.download; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.sftp.SFtpTaskOption; /** @@ -32,14 +30,12 @@ import com.arialyy.aria.sftp.SFtpTaskOption; */ public class SFtpDLoaderUtil extends AbsNormalLoaderUtil { - public SFtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(SFtpTaskOption.class); - } - @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new SFtpDLoader((DTaskWrapper) getTaskWrapper(), getListener()) - : mLoader; + if (mLoader == null) { + getTaskWrapper().generateTaskOption(SFtpTaskOption.class); + mLoader = new SFtpDLoader((DTaskWrapper) getTaskWrapper(), getListener()); + } + return mLoader; } @Override public LoaderStructure BuildLoaderStructure() { diff --git a/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java b/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java index 626b7b59..6d730625 100644 --- a/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java +++ b/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java @@ -15,14 +15,12 @@ */ package com.arialyy.aria.sftp.upload; -import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.sftp.SFtpTaskOption; /** @@ -32,14 +30,12 @@ import com.arialyy.aria.sftp.SFtpTaskOption; */ public class SFtpULoaderUtil extends AbsNormalLoaderUtil { - public SFtpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { - super(wrapper, listener); - wrapper.generateTaskOption(SFtpTaskOption.class); - } - @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new SFtpULoader((UTaskWrapper) getTaskWrapper(), getListener()) - : mLoader; + if (mLoader == null) { + getTaskWrapper().generateTaskOption(SFtpTaskOption.class); + mLoader = new SFtpULoader((UTaskWrapper) getTaskWrapper(), getListener()); + } + return mLoader; } @Override public LoaderStructure BuildLoaderStructure() { diff --git a/SFtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil b/SFtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil new file mode 100644 index 00000000..16943901 --- /dev/null +++ b/SFtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil @@ -0,0 +1,2 @@ +com.arialyy.aria.sftp.download.SFtpDLoaderUtil +com.arialyy.aria.sftp.upload.SFtpULoaderUtil \ No newline at end of file