From 30c41e4cb28affac92e0b37a74e4e5c2357ce223 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 5 Apr 2019 13:40:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96target=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/arialyy/aria/core/Aria.java | 308 ++++---- .../aria/core/common/ftp/FTPSDelegate.java | 244 +++---- .../aria/core/common/ftp/FtpTaskDelegate.java | 124 ++-- .../aria/core/common/http/HttpDelegate.java | 204 +++--- .../core/common/http/HttpTaskDelegate.java | 374 +++++----- .../aria/core/download/AbsDownloadTarget.java | 223 +++--- .../aria/core/download/BaseGroupTarget.java | 7 +- .../aria/core/download/BaseNormalTarget.java | 406 ++++++----- .../aria/core/download/DNormalDelegate.java | 161 +++++ .../core/download/DownloadGroupTarget.java | 674 +++++++++--------- .../aria/core/download/DownloadTarget.java | 302 ++++---- .../core/download/FtpDirDownloadTarget.java | 290 ++++---- .../aria/core/download/FtpDownloadTarget.java | 282 ++++---- .../com/arialyy/aria/core/inf/AbsTarget.java | 660 +++++++++-------- .../com/arialyy/aria/core/inf/IFtpTarget.java | 114 +-- .../aria/core/inf/IHttpHeaderDelegate.java | 102 ++- .../inf/{ITarget.java => ITargetHandler.java} | 141 ++-- ...Delegate.java => ITargetHeadDelegate.java} | 47 +- .../arialyy/aria/core/inf/ITargetNormal.java | 81 +++ .../java/com/arialyy/aria/core/inf/ITask.java | 261 +++---- .../aria/core/upload/AbsUploadTarget.java | 75 +- .../aria/core/upload/BaseNormalTarget.java | 292 ++++---- .../aria/core/upload/FtpUploadTarget.java | 227 +++--- .../aria/core/upload/UNormalDelegate.java | 132 ++++ .../aria/core/upload/UploadTarget.java | 225 +++--- .../download/HighestPriorityActivity.java | 488 ++++++------- .../core/upload/HttpUploadActivity.java | 2 +- 27 files changed, 3479 insertions(+), 2967 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/DNormalDelegate.java rename Aria/src/main/java/com/arialyy/aria/core/inf/{ITarget.java => ITargetHandler.java} (77%) rename Aria/src/main/java/com/arialyy/aria/core/inf/{ITaskDelegate.java => ITargetHeadDelegate.java} (82%) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/inf/ITargetNormal.java create mode 100644 Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index 3263ada6..e7381e54 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -1,154 +1,154 @@ -/* - * 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 android.annotation.TargetApi; -import android.app.Activity; -import android.app.Application; -import android.app.Dialog; -import android.app.Service; -import android.content.Context; -import android.os.Build; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.Fragment; -import android.widget.PopupWindow; -import com.arialyy.aria.core.download.DownloadReceiver; -import com.arialyy.aria.core.upload.UploadReceiver; -import com.arialyy.aria.util.ALog; - -/** - * Created by lyy on 2016/12/1. - * - * @see Aria - * @see Aria doc - * Aria启动,管理全局任务 - *
- *   
- *   //下载
- *   Aria.download(this)
- *       .load(URL)     //下载地址,必填
- *       //文件保存路径,必填
- *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
- *       .start();
- *   
- *   
- *    //上传
- *    Aria.upload(this)
- *        .load(filePath)     //文件路径,必填
- *        .setUploadUrl(uploadUrl)  //上传路径,必填
- *        .setAttachment(fileKey)   //服务器读取文件的key,必填
- *        .start();
- *   
- * 
- * - * 如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 - * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 - * 然后才能使用{@link #download(Object)}、{@link #upload(Object)} - * - *
- *   
- *       Aria.init(this);
- *
- *      Aria.download(this)
- *       .load(URL)     //下载地址,必填
- *       //文件保存路径,必填
- *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
- *       .start();
- *
- *   
- *
- * 
- */ -@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { - - private Aria() { - } - - /** - * 下载,在当前类中调用Aria方法,参数需要使用this,否则将 - * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 - * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 - * - * @param obj 观察者对象,为本类对象,使用{@code this} - */ - public static DownloadReceiver download(Object obj) { - if (AriaManager.getInstance() != null){ - return AriaManager.getInstance().download(obj); - } - return get(convertContext(obj)).download(obj); - } - - /** - * 上传 - * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 - * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 - * - * @param obj 观察者对象,为本类对象,使用{@code this} - */ - public static UploadReceiver upload(Object obj) { - if (AriaManager.getInstance() != null){ - return AriaManager.getInstance().upload(obj); - } - return get(convertContext(obj)).upload(obj); - } - - /** - * 处理通用事件 - */ - public static AriaManager get(Context context) { - if (context == null) { - throw new NullPointerException("context 无效,在非【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】," - + "请参考【https://aria.laoyuyu.me/aria_doc/start/any_java.html】,参数请使用 download(this) 或 upload(this);" - + "不要使用 download(getContext()) 或 upload(getContext())"); - } - return AriaManager.getInstance(context); - } - - /** - * 初始化Aria,如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 - * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用本方法对Aria进行初始化 - * 只需要初始化一次就可以 - * {@link #download(Object)}、{@link #upload(Object)} - */ - public static AriaManager init(Context context) { - return AriaManager.getInstance(context); - } - - private static Context convertContext(Object obj) { - if (obj instanceof Application) { - return (Application) obj; - } else if (obj instanceof Service) { - return (Service) obj; - } else if (obj instanceof Activity) { - return (Activity) obj; - } else if (obj instanceof DialogFragment) { - return ((DialogFragment) obj).getContext(); - } else if (obj instanceof android.app.DialogFragment) { - return ((android.app.DialogFragment) obj).getActivity(); - } else if (obj instanceof android.support.v4.app.Fragment) { - return ((Fragment) obj).getContext(); - } else if (obj instanceof android.app.Fragment) { - return ((android.app.Fragment) obj).getActivity(); - } else if (obj instanceof Dialog) { - return ((Dialog) obj).getContext(); - } else if (obj instanceof PopupWindow) { - return ((PopupWindow) obj).getContentView().getContext(); - } - ALog.e("Aria", "请使用download(this)或upload(this)"); - return null; - } -} +/* + * 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 android.annotation.TargetApi; +import android.app.Activity; +import android.app.Application; +import android.app.Dialog; +import android.app.Service; +import android.content.Context; +import android.os.Build; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.widget.PopupWindow; +import com.arialyy.aria.core.download.DownloadReceiver; +import com.arialyy.aria.core.upload.UploadReceiver; +import com.arialyy.aria.util.ALog; + +/** + * Created by lyy on 2016/12/1. + * + * @see Aria + * @see Aria doc + * Aria启动,管理全局任务 + *
+ *   
+ *   //下载
+ *   Aria.download(this)
+ *       .load(URL)     //下载地址,必填
+ *       //文件保存路径,必填
+ *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ *       .start();
+ *   
+ *   
+ *    //上传
+ *    Aria.upload(this)
+ *        .load(filePath)     //文件路径,必填
+ *        .setTempUrl(uploadUrl)  //上传路径,必填
+ *        .setAttachment(fileKey)   //服务器读取文件的key,必填
+ *        .start();
+ *   
+ * 
+ * + * 如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * 然后才能使用{@link #download(Object)}、{@link #upload(Object)} + * + *
+ *   
+ *       Aria.init(this);
+ *
+ *      Aria.download(this)
+ *       .load(URL)     //下载地址,必填
+ *       //文件保存路径,必填
+ *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ *       .start();
+ *
+ *   
+ *
+ * 
+ */ +@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { + + private Aria() { + } + + /** + * 下载,在当前类中调用Aria方法,参数需要使用this,否则将 + * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 + * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * + * @param obj 观察者对象,为本类对象,使用{@code this} + */ + public static DownloadReceiver download(Object obj) { + if (AriaManager.getInstance() != null){ + return AriaManager.getInstance().download(obj); + } + return get(convertContext(obj)).download(obj); + } + + /** + * 上传 + * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 + * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * + * @param obj 观察者对象,为本类对象,使用{@code this} + */ + public static UploadReceiver upload(Object obj) { + if (AriaManager.getInstance() != null){ + return AriaManager.getInstance().upload(obj); + } + return get(convertContext(obj)).upload(obj); + } + + /** + * 处理通用事件 + */ + public static AriaManager get(Context context) { + if (context == null) { + throw new NullPointerException("context 无效,在非【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】," + + "请参考【https://aria.laoyuyu.me/aria_doc/start/any_java.html】,参数请使用 download(this) 或 upload(this);" + + "不要使用 download(getContext()) 或 upload(getContext())"); + } + return AriaManager.getInstance(context); + } + + /** + * 初始化Aria,如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用本方法对Aria进行初始化 + * 只需要初始化一次就可以 + * {@link #download(Object)}、{@link #upload(Object)} + */ + public static AriaManager init(Context context) { + return AriaManager.getInstance(context); + } + + private static Context convertContext(Object obj) { + if (obj instanceof Application) { + return (Application) obj; + } else if (obj instanceof Service) { + return (Service) obj; + } else if (obj instanceof Activity) { + return (Activity) obj; + } else if (obj instanceof DialogFragment) { + return ((DialogFragment) obj).getContext(); + } else if (obj instanceof android.app.DialogFragment) { + return ((android.app.DialogFragment) obj).getActivity(); + } else if (obj instanceof android.support.v4.app.Fragment) { + return ((Fragment) obj).getContext(); + } else if (obj instanceof android.app.Fragment) { + return ((android.app.Fragment) obj).getActivity(); + } else if (obj instanceof Dialog) { + return ((Dialog) obj).getContext(); + } else if (obj instanceof PopupWindow) { + return ((PopupWindow) obj).getContentView().getContext(); + } + ALog.e("Aria", "请使用download(this)或upload(this)"); + return null; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java index 5188d59d..4ea18029 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java @@ -1,120 +1,124 @@ -/* - * 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.ftp; - -import android.text.TextUtils; -import com.arialyy.aria.core.FtpUrlEntity; -import com.arialyy.aria.core.common.ProtocolType; -import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.ITarget; - -/** - * FTP SSL/TSL 参数委托 - */ -public class FTPSDelegate implements ITarget { - private final String TAG = "FTPSDelegate"; - private TARGET mTarget; - private FtpUrlEntity mUrlEntity; - - public FTPSDelegate(TARGET target) { - mTarget = target; - mUrlEntity = mTarget.getTaskWrapper().asFtp().getUrlEntity(); - } - - /** - * 设置协议类型 - * - * @param protocol {@link ProtocolType} - */ - public FTPSDelegate setProtocol(@ProtocolType String protocol) { - if (TextUtils.isEmpty(protocol)) { - throw new NullPointerException("协议为空"); - } - mUrlEntity.protocol = protocol; - return this; - } - - /** - * 设置证书别名 - * - * @param keyAlias 别名 - */ - public FTPSDelegate setAlias(String keyAlias) { - if (TextUtils.isEmpty(keyAlias)) { - throw new NullPointerException("别名为空"); - } - mUrlEntity.keyAlias = keyAlias; - return this; - } - - /** - * 设置证书密码 - * - * @param storePass 私钥密码 - */ - public FTPSDelegate setStorePass(String storePass) { - if (TextUtils.isEmpty(storePass)) { - throw new NullPointerException("证书密码为空"); - } - mUrlEntity.storePass = storePass; - return this; - } - - /** - * 设置证书路径 - * - * @param storePath 证书路径 - */ - public FTPSDelegate setStorePath(String storePath) { - if (TextUtils.isEmpty(storePath)) { - throw new NullPointerException("证书路径为空"); - } - mUrlEntity.storePath = storePath; - return this; - } - - @Override public void start() { - mTarget.start(); - } - - @Override public void stop() { - mTarget.stop(); - } - - @Override public void resume() { - mTarget.resume(); - } - - @Override public void cancel() { - mTarget.cancel(); - } - - @Override public void save() { - mTarget.save(); - } - - @Override public void cancel(boolean removeFile) { - mTarget.cancel(removeFile); - } - - @Override public void reTry() { - mTarget.reTry(); - } - - @Override public void reStart() { - mTarget.reStart(); - } -} +/* + * 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.ftp; + +import android.text.TextUtils; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.common.ProtocolType; +import com.arialyy.aria.core.inf.AbsTarget; +import com.arialyy.aria.core.inf.ITargetHandler; + +/** + * FTP SSL/TSL 参数委托 + */ +public class FTPSDelegate implements ITargetHandler { + private final String TAG = "FTPSDelegate"; + private TARGET mTarget; + private FtpUrlEntity mUrlEntity; + + public FTPSDelegate(TARGET target) { + mTarget = target; + mUrlEntity = mTarget.getTaskWrapper().asFtp().getUrlEntity(); + } + + /** + * 设置协议类型 + * + * @param protocol {@link ProtocolType} + */ + public FTPSDelegate setProtocol(@ProtocolType String protocol) { + if (TextUtils.isEmpty(protocol)) { + throw new NullPointerException("协议为空"); + } + mUrlEntity.protocol = protocol; + return this; + } + + /** + * 设置证书别名 + * + * @param keyAlias 别名 + */ + public FTPSDelegate setAlias(String keyAlias) { + if (TextUtils.isEmpty(keyAlias)) { + throw new NullPointerException("别名为空"); + } + mUrlEntity.keyAlias = keyAlias; + return this; + } + + /** + * 设置证书密码 + * + * @param storePass 私钥密码 + */ + public FTPSDelegate setStorePass(String storePass) { + if (TextUtils.isEmpty(storePass)) { + throw new NullPointerException("证书密码为空"); + } + mUrlEntity.storePass = storePass; + return this; + } + + /** + * 设置证书路径 + * + * @param storePath 证书路径 + */ + public FTPSDelegate setStorePath(String storePath) { + if (TextUtils.isEmpty(storePath)) { + throw new NullPointerException("证书路径为空"); + } + mUrlEntity.storePath = storePath; + return this; + } + + @Override public void add() { + mTarget.add(); + } + + @Override public void start() { + mTarget.start(); + } + + @Override public void stop() { + mTarget.stop(); + } + + @Override public void resume() { + mTarget.resume(); + } + + @Override public void cancel() { + mTarget.cancel(); + } + + @Override public void save() { + mTarget.save(); + } + + @Override public void cancel(boolean removeFile) { + mTarget.cancel(removeFile); + } + + @Override public void reTry() { + mTarget.reTry(); + } + + @Override public void reStart() { + mTarget.reStart(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskDelegate.java index 581b609a..5e1fc7d4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskDelegate.java @@ -1,62 +1,62 @@ -/* - * 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.ftp; - -import com.arialyy.aria.core.FtpUrlEntity; -import com.arialyy.aria.core.inf.ITaskDelegate; -import java.net.Proxy; - -/** - * fTP任务设置的信息,如:用户名、密码、端口等信息 - */ -public class FtpTaskDelegate implements ITaskDelegate { - - /** - * 账号和密码 - */ - private FtpUrlEntity urlEntity; - - private Proxy proxy; - - /** - * 字符编码,默认为"utf-8" - */ - private String charSet = "utf-8"; - - public FtpUrlEntity getUrlEntity() { - return urlEntity; - } - - public void setUrlEntity(FtpUrlEntity urlEntity) { - this.urlEntity = urlEntity; - } - - public void setProxy(Proxy proxy) { - this.proxy = proxy; - } - - public Proxy getProxy() { - return proxy; - } - - public String getCharSet() { - return charSet; - } - - public void setCharSet(String charSet) { - this.charSet = charSet; - } -} +/* + * 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.ftp; + +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.inf.ITargetHeadDelegate; +import java.net.Proxy; + +/** + * fTP任务设置的信息,如:用户名、密码、端口等信息 + */ +public class FtpTaskDelegate implements ITargetHeadDelegate { + + /** + * 账号和密码 + */ + private FtpUrlEntity urlEntity; + + private Proxy proxy; + + /** + * 字符编码,默认为"utf-8" + */ + private String charSet = "utf-8"; + + public FtpUrlEntity getUrlEntity() { + return urlEntity; + } + + public void setUrlEntity(FtpUrlEntity urlEntity) { + this.urlEntity = urlEntity; + } + + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } + + public Proxy getProxy() { + return proxy; + } + + public String getCharSet() { + return charSet; + } + + public void setCharSet(String charSet) { + this.charSet = charSet; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java index 3caf18a9..40ac1f48 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java @@ -1,100 +1,104 @@ -/* - * 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.http; - -import android.text.TextUtils; -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupTarget; -import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.ITarget; -import com.arialyy.aria.util.ALog; -import java.util.HashMap; -import java.util.Map; - -/** - * HTTP参数委托 - * @param - */ -class HttpDelegate implements ITarget { - private static final String TAG = "PostDelegate"; - TARGET mTarget; - - HttpDelegate(TARGET target) { - mTarget = target; - } - - public TARGET setParams(Map params) { - mTarget.getTaskWrapper().asHttp().setParams(params); - if (mTarget instanceof DownloadGroupTarget) { - for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) { - subTask.asHttp().setParams(params); - } - } - return mTarget; - } - - public TARGET setParam(String key, String value) { - if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) { - ALog.d(TAG, "key 或value 为空"); - return mTarget; - } - Map params = mTarget.getTaskWrapper().asHttp().getParams(); - if (params == null) { - params = new HashMap<>(); - mTarget.getTaskWrapper().asHttp().setParams(params); - } - params.put(key, value); - if (mTarget instanceof DownloadGroupTarget) { - for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) { - subTask.asHttp().setParams(params); - } - } - return mTarget; - } - - @Override public void start() { - mTarget.start(); - } - - @Override public void stop() { - mTarget.stop(); - } - - @Override public void resume() { - mTarget.resume(); - } - - @Override public void cancel() { - mTarget.cancel(); - } - - @Override public void save() { - mTarget.save(); - } - - @Override public void cancel(boolean removeFile) { - mTarget.cancel(removeFile); - } - - @Override public void reTry() { - mTarget.reTry(); - } - - @Override public void reStart() { - mTarget.reStart(); - } -} +/* + * 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.http; + +import android.text.TextUtils; +import com.arialyy.aria.core.download.DGTaskWrapper; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadGroupTarget; +import com.arialyy.aria.core.inf.AbsTarget; +import com.arialyy.aria.core.inf.ITargetHandler; +import com.arialyy.aria.util.ALog; +import java.util.HashMap; +import java.util.Map; + +/** + * HTTP参数委托 + * @param + */ +class HttpDelegate implements ITargetHandler { + private static final String TAG = "PostDelegate"; + TARGET mTarget; + + HttpDelegate(TARGET target) { + mTarget = target; + } + + public TARGET setParams(Map params) { + mTarget.getTaskWrapper().asHttp().setParams(params); + if (mTarget instanceof DownloadGroupTarget) { + for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) { + subTask.asHttp().setParams(params); + } + } + return mTarget; + } + + public TARGET setParam(String key, String value) { + if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) { + ALog.d(TAG, "key 或value 为空"); + return mTarget; + } + Map params = mTarget.getTaskWrapper().asHttp().getParams(); + if (params == null) { + params = new HashMap<>(); + mTarget.getTaskWrapper().asHttp().setParams(params); + } + params.put(key, value); + if (mTarget instanceof DownloadGroupTarget) { + for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) { + subTask.asHttp().setParams(params); + } + } + return mTarget; + } + + @Override public void add() { + mTarget.add(); + } + + @Override public void start() { + mTarget.start(); + } + + @Override public void stop() { + mTarget.stop(); + } + + @Override public void resume() { + mTarget.resume(); + } + + @Override public void cancel() { + mTarget.cancel(); + } + + @Override public void save() { + mTarget.save(); + } + + @Override public void cancel(boolean removeFile) { + mTarget.cancel(removeFile); + } + + @Override public void reTry() { + mTarget.reTry(); + } + + @Override public void reStart() { + mTarget.reStart(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskDelegate.java index 58b217ac..5dadd800 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskDelegate.java @@ -1,187 +1,187 @@ -/* - * 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.http; - -import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.inf.ITaskDelegate; -import java.net.CookieManager; -import java.net.Proxy; -import java.util.HashMap; -import java.util.Map; - -/** - * Http任务设置的信息,如:cookie、请求参数 - */ -public class HttpTaskDelegate implements ITaskDelegate { - - private CookieManager cookieManager; - - /** - * 请求参数 - */ - private Map params; - - /** - * http 请求头 - */ - private Map headers = new HashMap<>(); - - /** - * 字符编码,默认为"utf-8" - */ - private String charSet = "utf-8"; - - /** - * 网络请求类型 - */ - private RequestEnum requestEnum = RequestEnum.GET; - - /** - * 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment; filename="filename.jpg"} {@code true} - * 使用 - */ - private boolean useServerFileName = false; - - /** - * 重定向链接 - */ - private String redirectUrl = ""; - - /** - * 是否是chunk模式 - */ - private boolean isChunked = false; - /** - * 文件上传需要的key - */ - private String attachment; - /** - * 上传的文件类型 - */ - private String contentType = "multipart/form-data"; - private String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"; - - private Proxy proxy; - /** - * 文件上传表单 - */ - private Map formFields = new HashMap<>(); - - public Map getFormFields() { - return formFields; - } - - public void setFormFields(Map formFields) { - this.formFields = formFields; - } - - public String getAttachment() { - return attachment; - } - - public void setAttachment(String attachment) { - this.attachment = attachment; - } - - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - public String getUserAgent() { - return userAgent; - } - - public void setUserAgent(String userAgent) { - this.userAgent = userAgent; - } - - public boolean isChunked() { - return isChunked; - } - - public void setChunked(boolean chunked) { - isChunked = chunked; - } - - public CookieManager getCookieManager() { - return cookieManager; - } - - public void setCookieManager(CookieManager cookieManager) { - this.cookieManager = cookieManager; - } - - public Proxy getProxy() { - return proxy; - } - - public void setProxy(Proxy proxy) { - this.proxy = proxy; - } - - public Map getHeaders() { - return headers; - } - - public void setHeaders(Map headers) { - this.headers = headers; - } - - public String getCharSet() { - return charSet; - } - - public void setCharSet(String charSet) { - this.charSet = charSet; - } - - public RequestEnum getRequestEnum() { - return requestEnum; - } - - public void setRequestEnum(RequestEnum requestEnum) { - this.requestEnum = requestEnum; - } - - public boolean isUseServerFileName() { - return useServerFileName; - } - - public void setUseServerFileName(boolean useServerFileName) { - this.useServerFileName = useServerFileName; - } - - public String getRedirectUrl() { - return redirectUrl; - } - - public void setRedirectUrl(String redirectUrl) { - this.redirectUrl = redirectUrl; - } - - public Map getParams() { - return params; - } - - public void setParams(Map params) { - this.params = params; - } -} +/* + * 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.http; + +import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.inf.ITargetHeadDelegate; +import java.net.CookieManager; +import java.net.Proxy; +import java.util.HashMap; +import java.util.Map; + +/** + * Http任务设置的信息,如:cookie、请求参数 + */ +public class HttpTaskDelegate implements ITargetHeadDelegate { + + private CookieManager cookieManager; + + /** + * 请求参数 + */ + private Map params; + + /** + * http 请求头 + */ + private Map headers = new HashMap<>(); + + /** + * 字符编码,默认为"utf-8" + */ + private String charSet = "utf-8"; + + /** + * 网络请求类型 + */ + private RequestEnum requestEnum = RequestEnum.GET; + + /** + * 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment; filename="filename.jpg"} {@code true} + * 使用 + */ + private boolean useServerFileName = false; + + /** + * 重定向链接 + */ + private String redirectUrl = ""; + + /** + * 是否是chunk模式 + */ + private boolean isChunked = false; + /** + * 文件上传需要的key + */ + private String attachment; + /** + * 上传的文件类型 + */ + private String contentType = "multipart/form-data"; + private String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"; + + private Proxy proxy; + /** + * 文件上传表单 + */ + private Map formFields = new HashMap<>(); + + public Map getFormFields() { + return formFields; + } + + public void setFormFields(Map formFields) { + this.formFields = formFields; + } + + public String getAttachment() { + return attachment; + } + + public void setAttachment(String attachment) { + this.attachment = attachment; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public String getUserAgent() { + return userAgent; + } + + public void setUserAgent(String userAgent) { + this.userAgent = userAgent; + } + + public boolean isChunked() { + return isChunked; + } + + public void setChunked(boolean chunked) { + isChunked = chunked; + } + + public CookieManager getCookieManager() { + return cookieManager; + } + + public void setCookieManager(CookieManager cookieManager) { + this.cookieManager = cookieManager; + } + + public Proxy getProxy() { + return proxy; + } + + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } + + public Map getHeaders() { + return headers; + } + + public void setHeaders(Map headers) { + this.headers = headers; + } + + public String getCharSet() { + return charSet; + } + + public void setCharSet(String charSet) { + this.charSet = charSet; + } + + public RequestEnum getRequestEnum() { + return requestEnum; + } + + public void setRequestEnum(RequestEnum requestEnum) { + this.requestEnum = requestEnum; + } + + public boolean isUseServerFileName() { + return useServerFileName; + } + + public void setUseServerFileName(boolean useServerFileName) { + this.useServerFileName = useServerFileName; + } + + public String getRedirectUrl() { + return redirectUrl; + } + + public void setRedirectUrl(String redirectUrl) { + this.redirectUrl = redirectUrl; + } + + public Map getParams() { + return params; + } + + public void setParams(Map params) { + this.params = params; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java index ab19a525..2e589d3b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java @@ -1,102 +1,121 @@ -/* - * 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.AriaManager; -import com.arialyy.aria.core.command.normal.NormalCmdFactory; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.util.CommonUtil; - -/** - * Created by lyy on 2017/2/28. - */ -abstract class AbsDownloadTarget - extends AbsTarget { - - static final int HTTP = 1; - static final int FTP = 2; - //HTTP任务组 - static final int GROUP_HTTP = 3; - //FTP文件夹 - static final int GROUP_FTP_DIR = 4; - - /** - * 设置的文件保存路径的临时变量 - */ - String mTempFilePath; - - /** - * {@code true}强制下载,不考虑文件路径是否被占用 - */ - boolean forceDownload = false; - - /** - * 将任务设置为最高优先级任务,最高优先级任务有以下特点: - * 1、在下载队列中,有且只有一个最高优先级任务 - * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 - * 3、任务调度器不会暂停最高优先级任务 - * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 - * 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 - * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 - */ - protected void setHighestPriority() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_HIGHEST_PRIORITY, - checkTaskType())) - .exe(); - } - } - - /** - * 添加任务 - */ - public void add() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CREATE, - checkTaskType())) - .exe(); - } - } - - /** - * 获取任务文件大小 - * - * @return 文件大小 - */ - public long getFileSize() { - return getSize(); - } - - /** - * 获取单位转换后的文件大小 - * - * @return 文件大小{@code xxx mb} - */ - public String getConvertFileSize() { - return getConvertSize(); - } - - /** - * 设置target类型 - * - * @return {@link #HTTP}、{@link #FTP}、{@link #GROUP_HTTP}、{@link #GROUP_FTP_DIR} - */ - protected abstract int getTargetType(); -} +/* + * 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.AriaManager; +import com.arialyy.aria.core.command.normal.NormalCmdFactory; +import com.arialyy.aria.core.inf.AbsTarget; +import com.arialyy.aria.util.CommonUtil; + +/** + * Created by lyy on 2017/2/28. + */ +abstract class AbsDownloadTarget + extends AbsTarget { + + /** + * 设置的文件保存路径的临时变量 + */ + private String mTempFilePath; + + /** + * {@code true}强制下载,不考虑文件路径是否被占用 + */ + private boolean forceDownload = false; + /** + * 资源地址 + */ + private String mUrl, mNewUrl; + + /** + * 更新下载url + * + * @param newUrl 新的下载url + */ + public abstract TARGET updateUrl(String newUrl); + + /** + * 将任务设置为最高优先级任务,最高优先级任务有以下特点: + * 1、在下载队列中,有且只有一个最高优先级任务 + * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 + * 3、任务调度器不会暂停最高优先级任务 + * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 + * 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 + * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 + */ + public void setHighestPriority() { + if (checkEntity()) { + AriaManager.getInstance(AriaManager.APP) + .setCmd( + CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_HIGHEST_PRIORITY, + checkTaskType())) + .exe(); + } + } + + /** + * 是否强制下载文件 {@link DownloadTarget#setFilePath(String, boolean)}、 + * {@link FtpDownloadTarget#setFilePath(String, boolean)} + * + * @return {@code true} 强制下载文件 + */ + boolean isForceDownload() { + return forceDownload; + } + + @Override public void setTaskWrapper(DTaskWrapper taskWrapper) { + super.setTaskWrapper(taskWrapper); + } + + /** + * 文件保存路径的临时变量 + */ + String getTempFilePath() { + return mTempFilePath; + } + + void setForceDownload(boolean forceDownload) { + this.forceDownload = forceDownload; + } + + public String getUrl() { + return mUrl; + } + + void setUrl(String url) { + this.mUrl = url; + } + + String getNewUrl() { + return mNewUrl; + } + + void setNewUrl(String newUrl) { + this.mNewUrl = newUrl; + } + + void setTempFilePath(String mTempFilePath) { + this.mTempFilePath = mTempFilePath; + } + + public void setEntity(DownloadEntity entity) { + mEntity = entity; + } + + @Override public DownloadEntity getEntity() { + return super.getEntity(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java index 45b1f7d8..adb049a4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.download; import android.support.annotation.CheckResult; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.orm.DbEntity; @@ -30,7 +31,7 @@ import java.util.List; * Created by lyy on 2017/7/26. */ abstract class BaseGroupTarget - extends AbsDownloadTarget { + extends AbsTarget { /** * 组任务名 @@ -55,7 +56,7 @@ abstract class BaseGroupTarget @CheckResult public SubTaskManager getSubTaskManager() { if (mSubTaskManager == null) { - mSubTaskManager = new SubTaskManager(mTargetName, mTaskWrapper); + mSubTaskManager = new SubTaskManager(getTargetName(), getTaskWrapper()); } return mSubTaskManager; } @@ -122,7 +123,7 @@ abstract class BaseGroupTarget * @param newDirPath 新的文件夹路径 */ void reChangeDirPath(String newDirPath) { - List subTasks = mTaskWrapper.getSubTaskWrapper(); + List subTasks = getTaskWrapper().getSubTaskWrapper(); if (subTasks != null && !subTasks.isEmpty()) { List des = new ArrayList<>(); for (DTaskWrapper dte : subTasks) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java index 49357622..b7c7bace 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java @@ -1,207 +1,199 @@ -/* - * 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.text.TextUtils; -import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.queue.DownloadTaskQueue; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.io.File; - -/** - * Created by Aria.Lao on 2017/7/26. - */ -abstract class BaseNormalTarget - extends AbsDownloadTarget { - - /** - * 资源地址 - */ - protected String url, newUrl; - - /** - * 通过地址初始化target - */ - void initTarget(String url, String targetName) { - this.url = url; - mTargetName = targetName; - mTaskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); - mEntity = mTaskWrapper.getEntity(); - - if (mEntity != null) { - mTempFilePath = mEntity.getDownloadPath(); - } - } - - /** - * 更新下载url - * - * @param newUrl 新的下载url - */ - public TARGET updateUrl(String newUrl) { - if (TextUtils.isEmpty(newUrl)) { - ALog.e(TAG, "下载url更新失败,newUrl为null"); - return (TARGET) this; - } - if (url.equals(newUrl)) { - ALog.e(TAG, "下载url更新失败,新的下载url和旧的url一致"); - return (TARGET) this; - } - this.newUrl = newUrl; - mTaskWrapper.setRefreshInfo(true); - return (TARGET) this; - } - - /** - * 将任务设置为最高优先级任务,最高优先级任务有以下特点: 1、在下载队列中,有且只有一个最高优先级任务 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 - * 3、任务调度器不会暂停最高优先级任务 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 - * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 - */ - @Override public void setHighestPriority() { - super.setHighestPriority(); - } - - /** - * 下载任务是否存在 - * - * @return {@code true}任务存在 - */ - @Override public boolean taskExists() { - return DbEntity.checkDataExist(DownloadEntity.class, "url=?", url); - } - - /** - * 获取下载实体 - */ - public DownloadEntity getDownloadEntity() { - return mEntity; - } - - /** - * 是否在下载,该api后续版本会删除 - * - * @deprecated {@link #isRunning()} - */ - @Deprecated public boolean isDownloading() { - return isRunning(); - } - - /** - * 是否在下载 - * - * @return {@code true}任务正在下载 - */ - @Override public boolean isRunning() { - DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey()); - return task != null && task.isRunning(); - } - - /** - * 检查下载实体,判断实体是否合法 合法标准为: 1、下载路径不为null,并且下载路径是正常的http或ftp路径 2、保存路径不为null,并且保存路径是android文件系统路径 - * 3、保存路径不能重复 - * - * @return {@code true}合法 - */ - @Override protected boolean checkEntity() { - boolean b = getTargetType() < GROUP_HTTP && checkUrl() && checkFilePath(); - if (b) { - mEntity.save(); - } - return b; - } - - /** - * 检查并设置普通任务的文件保存路径 - * - * @return {@code true}保存路径合法 - */ - private boolean checkFilePath() { - String filePath = mTempFilePath; - if (TextUtils.isEmpty(filePath)) { - ALog.e(TAG, "下载失败,文件保存路径为null"); - return false; - } else if (!filePath.startsWith("/")) { - ALog.e(TAG, "下载失败,文件保存路径【" + filePath + "】错误"); - return false; - } - File file = new File(filePath); - if (file.isDirectory()) { - if (getTargetType() == HTTP) { - ALog.e(TAG, "下载失败,保存路径【" + filePath + "】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip"); - return false; - } else if (getTargetType() == FTP) { - filePath += mEntity.getFileName(); - } - } else { - // http文件名设置 - if (TextUtils.isEmpty(mEntity.getFileName())) { - mEntity.setFileName(file.getName()); - } - } - - //设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径 - if (!filePath.equals(mEntity.getDownloadPath())) { - // 检查路径冲突 - if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { - if (!forceDownload) { - ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径"); - return false; - } else { - ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); - CommonUtil.delTaskRecord(filePath, 1); - mTaskWrapper = - TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); - } - } - File oldFile = new File(mEntity.getDownloadPath()); - File newFile = new File(filePath); - mEntity.setDownloadPath(filePath); - mEntity.setFileName(newFile.getName()); - if (oldFile.exists()) { - oldFile.renameTo(newFile); - CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); - } - } - return true; - } - - /** - * 检查普通任务的下载地址 - * - * @return {@code true}地址合法 - */ - private boolean checkUrl() { - final String url = mEntity.getUrl(); - if (TextUtils.isEmpty(url)) { - ALog.e(TAG, "下载失败,url为null"); - return false; - } else if (!url.startsWith("http") && !url.startsWith("ftp")) { - ALog.e(TAG, "下载失败,url【" + url + "】错误"); - return false; - } - int index = url.indexOf("://"); - if (index == -1) { - ALog.e(TAG, "下载失败,url【" + url + "】不合法"); - return false; - } - if (!TextUtils.isEmpty(newUrl)) { - mEntity.setUrl(newUrl); - } - return true; - } -} +///* +// * 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.text.TextUtils; +//import com.arialyy.aria.core.manager.TaskWrapperManager; +//import com.arialyy.aria.core.queue.DownloadTaskQueue; +//import com.arialyy.aria.orm.DbEntity; +//import com.arialyy.aria.util.ALog; +//import com.arialyy.aria.util.CommonUtil; +//import java.io.File; +// +///** +// * Created by Aria.Lao on 2017/7/26. +// */ +//abstract class BaseNormalTarget +// extends AbsDownloadTarget { +// +// /** +// * 通过地址初始化target +// */ +// void initTarget(String url, String targetName) { +// setUrl(url); +// setTargetName(targetName); +// setTaskWrapper(TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url)); +// mEntity = getTaskWrapper().getEntity(); +// +// if (mEntity != null) { +// setTempFilePath(mEntity.getDownloadPath()); +// } +// } +// +// /** +// * 更新下载url +// * +// * @param newUrl 新的下载url +// */ +// public TARGET updateUrl(String newUrl) { +// if (TextUtils.isEmpty(newUrl)) { +// ALog.e(TAG, "下载url更新失败,newUrl为null"); +// return (TARGET) this; +// } +// if (getUrl().equals(newUrl)) { +// ALog.e(TAG, "下载url更新失败,新的下载url和旧的url一致"); +// return (TARGET) this; +// } +// setNewUrl(newUrl); +// getTaskWrapper().setRefreshInfo(true); +// return (TARGET) this; +// } +// +// /** +// * 将任务设置为最高优先级任务,最高优先级任务有以下特点: +// * 1、在下载队列中,有且只有一个最高优先级任务 +// * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 +// * 3、任务调度器不会暂停最高优先级任务 +// * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 +// * 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 +// * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 +// */ +// @Override public void setHighestPriority() { +// super.setHighestPriority(); +// } +// +// /** +// * 下载任务是否存在 +// * +// * @return {@code true}任务存在 +// */ +// @Override public boolean taskExists() { +// return DbEntity.checkDataExist(DownloadEntity.class, "url=?", getUrl()); +// } +// +// /** +// * 获取下载实体 +// */ +// public DownloadEntity getDownloadEntity() { +// return mEntity; +// } +// +// /** +// * 是否在下载 +// * +// * @return {@code true}任务正在下载 +// */ +// @Override public boolean isRunning() { +// DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey()); +// return task != null && task.isRunning(); +// } +// +// /** +// * 检查下载实体,判断实体是否合法 合法标准为: +// * 1、下载路径不为null,并且下载路径是正常的http或ftp路径 +// * 2、保存路径不为null,并且保存路径是android文件系统路径 +// * 3、保存路径不能重复 +// * +// * @return {@code true}合法 +// */ +// @Override protected boolean checkEntity() { +// boolean b = getTargetType() < GROUP_HTTP && checkUrl() && checkFilePath(); +// if (b) { +// mEntity.save(); +// } +// return b; +// } +// +// /** +// * 检查并设置普通任务的文件保存路径 +// * +// * @return {@code true}保存路径合法 +// */ +// private boolean checkFilePath() { +// String filePath = getTempFilePath(); +// if (TextUtils.isEmpty(filePath)) { +// ALog.e(TAG, "下载失败,文件保存路径为null"); +// return false; +// } else if (!filePath.startsWith("/")) { +// ALog.e(TAG, "下载失败,文件保存路径【" + filePath + "】错误"); +// return false; +// } +// File file = new File(filePath); +// if (file.isDirectory()) { +// if (getTargetType() == HTTP) { +// ALog.e(TAG, "下载失败,保存路径【" + filePath + "】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip"); +// return false; +// } else if (getTargetType() == FTP) { +// filePath += mEntity.getFileName(); +// } +// } else { +// // http文件名设置 +// if (TextUtils.isEmpty(mEntity.getFileName())) { +// mEntity.setFileName(file.getName()); +// } +// } +// +// //设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径 +// if (!filePath.equals(mEntity.getDownloadPath())) { +// // 检查路径冲突 +// if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { +// if (!isForceDownload()) { +// ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径"); +// return false; +// } else { +// ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); +// CommonUtil.delTaskRecord(filePath, 1); +// setTaskWrapper( +// TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, getUrl())); +// } +// } +// File oldFile = new File(mEntity.getDownloadPath()); +// File newFile = new File(filePath); +// mEntity.setDownloadPath(filePath); +// mEntity.setFileName(newFile.getName()); +// if (oldFile.exists()) { +// oldFile.renameTo(newFile); +// CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); +// } +// } +// return true; +// } +// +// /** +// * 检查普通任务的下载地址 +// * +// * @return {@code true}地址合法 +// */ +// private boolean checkUrl() { +// final String url = mEntity.getUrl(); +// if (TextUtils.isEmpty(url)) { +// ALog.e(TAG, "下载失败,url为null"); +// return false; +// } else if (!url.startsWith("http") && !url.startsWith("ftp")) { +// ALog.e(TAG, "下载失败,url【" + url + "】错误"); +// return false; +// } +// int index = url.indexOf("://"); +// if (index == -1) { +// ALog.e(TAG, "下载失败,url【" + url + "】不合法"); +// return false; +// } +// if (!TextUtils.isEmpty(getNewUrl())) { +// mEntity.setUrl(getNewUrl()); +// } +// return true; +// } +//} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DNormalDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/download/DNormalDelegate.java new file mode 100644 index 00000000..d9dd36cc --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DNormalDelegate.java @@ -0,0 +1,161 @@ +/* + * 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.text.TextUtils; +import com.arialyy.aria.core.inf.ITargetHandler; +import com.arialyy.aria.core.inf.ITargetNormal; +import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.core.queue.DownloadTaskQueue; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import java.io.File; + +/** + * Created by AriaL on 2019/4/5. + * 普通下载任务通用功能处理 + */ +class DNormalDelegate implements ITargetNormal { + private final String TAG = "DNormalDelegate"; + private DownloadEntity mEntity; + + private TARGET target; + + DNormalDelegate(TARGET target, String url, String targetName) { + this.target = target; + initTarget(url, targetName); + } + + @Override public void initTarget(String url, String targetName) { + DTaskWrapper taskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); + mEntity = taskWrapper.getEntity(); + + target.setUrl(url); + target.setTargetName(targetName); + target.setTaskWrapper(taskWrapper); + target.setEntity(mEntity); + if (mEntity != null) { + target.setTempFilePath(mEntity.getDownloadPath()); + } + } + + @Override public TARGET updateUrl(String newUrl) { + if (TextUtils.isEmpty(newUrl)) { + ALog.e(TAG, "下载url更新失败,newUrl为null"); + return target; + } + if (target.getUrl().equals(newUrl)) { + ALog.e(TAG, "下载url更新失败,新的下载url和旧的url一致"); + return target; + } + target.setNewUrl(newUrl); + target.getTaskWrapper().setRefreshInfo(true); + return target; + } + + @Override public DownloadEntity getEntity() { + return target.getEntity(); + } + + @Override public boolean taskExists() { + return DbEntity.checkDataExist(DownloadEntity.class, "url=?", target.getUrl()); + } + + @Override public boolean isRunning() { + DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey()); + return task != null && task.isRunning(); + } + + @Override public boolean checkEntity() { + boolean b = checkUrl() && checkFilePath(); + if (b) { + mEntity.save(); + } + return b; + } + + @Override public boolean checkFilePath() { + String filePath = target.getTempFilePath(); + if (TextUtils.isEmpty(filePath)) { + ALog.e(TAG, "下载失败,文件保存路径为null"); + return false; + } else if (!filePath.startsWith("/")) { + ALog.e(TAG, "下载失败,文件保存路径【" + filePath + "】错误"); + return false; + } + File file = new File(filePath); + if (file.isDirectory()) { + if (target.getTargetType() == ITargetHandler.HTTP) { + ALog.e(TAG, "下载失败,保存路径【" + filePath + "】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip"); + return false; + } else if (target.getTargetType() == ITargetHandler.FTP) { + filePath += mEntity.getFileName(); + } + } else { + // http文件名设置 + if (TextUtils.isEmpty(mEntity.getFileName())) { + mEntity.setFileName(file.getName()); + } + } + + //设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径 + if (!filePath.equals(mEntity.getDownloadPath())) { + // 检查路径冲突 + if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { + if (!target.isForceDownload()) { + ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径"); + return false; + } else { + ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); + CommonUtil.delTaskRecord(filePath, 1); + target.setTaskWrapper( + TaskWrapperManager.getInstance() + .getHttpTaskWrapper(DTaskWrapper.class, target.getUrl())); + } + } + File oldFile = new File(mEntity.getDownloadPath()); + File newFile = new File(filePath); + mEntity.setDownloadPath(filePath); + mEntity.setFileName(newFile.getName()); + if (oldFile.exists()) { + oldFile.renameTo(newFile); + CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); + } + } + return true; + } + + @Override public boolean checkUrl() { + final String url = mEntity.getUrl(); + if (TextUtils.isEmpty(url)) { + ALog.e(TAG, "下载失败,url为null"); + return false; + } else if (!url.startsWith("http") && !url.startsWith("ftp")) { + ALog.e(TAG, "下载失败,url【" + url + "】错误"); + return false; + } + int index = url.indexOf("://"); + if (index == -1) { + ALog.e(TAG, "下载失败,url【" + url + "】不合法"); + return false; + } + if (!TextUtils.isEmpty(target.getNewUrl())) { + mEntity.setUrl(target.getNewUrl()); + } + return true; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index 05d21afa..2bf4809c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -1,337 +1,337 @@ -/* - * 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.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import android.text.TextUtils; -import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.common.http.HttpHeaderDelegate; -import com.arialyy.aria.core.common.http.PostDelegate; -import com.arialyy.aria.core.inf.IHttpHeaderDelegate; -import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.io.File; -import java.net.Proxy; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Created by AriaL on 2017/6/29. - * 下载任务组 - */ -public class DownloadGroupTarget extends BaseGroupTarget implements - IHttpHeaderDelegate { - private HttpHeaderDelegate mDelegate; - /** - * 子任务下载地址, - */ - private List mUrls = new ArrayList<>(); - - /** - * 子任务文件名 - */ - private List mSubNameTemp = new ArrayList<>(); - - public DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) { - this.mTargetName = targetName; - if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { - this.mUrls.addAll(groupEntity.getUrls()); - } - init(); - } - - DownloadGroupTarget(List urls, String targetName) { - this.mTargetName = targetName; - this.mUrls = urls; - init(); - } - - private void init() { - mGroupHash = CommonUtil.getMd5Code(mUrls); - mTaskWrapper = TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, mUrls); - mEntity = mTaskWrapper.getEntity(); - if (mEntity != null) { - mDirPathTemp = mEntity.getDirPath(); - } - mDelegate = new HttpHeaderDelegate<>(this); - } - - /** - * Post处理 - */ - public PostDelegate asPost() { - return new PostDelegate<>(this); - } - - /** - * 更新组合任务下载地址 - * - * @param urls 新的组合任务下载地址列表 - */ - @CheckResult - public DownloadGroupTarget updateUrls(List urls) { - if (urls == null || urls.isEmpty()) { - throw new NullPointerException("下载地址列表为空"); - } - if (urls.size() != mUrls.size()) { - throw new IllegalArgumentException("新下载地址数量和旧下载地址数量不一致"); - } - mUrls.clear(); - mUrls.addAll(urls); - mGroupHash = CommonUtil.getMd5Code(urls); - mEntity.setGroupHash(mGroupHash); - mEntity.update(); - if (mEntity.getSubEntities() != null && !mEntity.getSubEntities().isEmpty()) { - for (DownloadEntity de : mEntity.getSubEntities()) { - de.setGroupHash(mGroupHash); - de.update(); - } - } - return this; - } - - /** - * 任务组总任务大小,任务组是一个抽象的概念,没有真实的数据实体,任务组的大小是Aria动态获取子任务大小相加而得到的, - * 如果你知道当前任务组总大小,你也可以调用该方法给任务组设置大小 - * - * 为了更好的用户体验,组合任务必须设置文件大小 - * - * @param fileSize 任务组总大小 - */ - @CheckResult - public DownloadGroupTarget setFileSize(long fileSize) { - if (fileSize <= 0) { - ALog.e(TAG, "文件大小不能小于 0"); - return this; - } - if (mEntity.getFileSize() <= 1 || mEntity.getFileSize() != fileSize) { - mEntity.setFileSize(fileSize); - } - return this; - } - - /** - * 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址 - */ - @CheckResult - public DownloadGroupTarget setGroupUrl(List urls) { - mUrls.clear(); - mUrls.addAll(urls); - return this; - } - - /** - * 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 - * - * @deprecated {@link #setSubFileName(List)} 请使用该api - */ - @CheckResult - @Deprecated public DownloadGroupTarget setSubTaskFileName(List subTaskFileName) { - return setSubFileName(subTaskFileName); - } - - /** - * 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 - */ - @CheckResult - public DownloadGroupTarget setSubFileName(List subTaskFileName) { - if (subTaskFileName == null || subTaskFileName.isEmpty()) { - ALog.e(TAG, "修改子任务的文件名失败:列表为null"); - return this; - } - if (subTaskFileName.size() != mTaskWrapper.getSubTaskWrapper().size()) { - ALog.e(TAG, "修改子任务的文件名失败:子任务文件名列表数量和子任务的数量不匹配"); - return this; - } - mSubNameTemp.clear(); - mSubNameTemp.addAll(subTaskFileName); - return this; - } - - @Override protected int getTargetType() { - return GROUP_HTTP; - } - - @Override protected boolean checkEntity() { - if (getTargetType() == GROUP_HTTP) { - if (!checkDirPath()) { - return false; - } - - if (!checkSubName()) { - return false; - } - - if (!checkUrls()) { - return false; - } - - if (mTaskWrapper.getEntity().getFileSize() == 0) { - ALog.e(TAG, "组合任务必须设置文件文件大小"); - return false; - } - - if (mTaskWrapper.asHttp().getRequestEnum() == RequestEnum.POST) { - for (DTaskWrapper subTask : mTaskWrapper.getSubTaskWrapper()) { - subTask.asHttp().setRequestEnum(RequestEnum.POST); - } - } - - mEntity.save(); - - if (needModifyPath) { - reChangeDirPath(mDirPathTemp); - } - - if (!mSubNameTemp.isEmpty()) { - updateSingleSubFileName(); - } - return true; - } - return false; - } - - /** - * 更新所有改动的子任务文件名 - */ - private void updateSingleSubFileName() { - List entities = mTaskWrapper.getSubTaskWrapper(); - int i = 0; - for (DTaskWrapper entity : entities) { - if (i < mSubNameTemp.size()) { - String newName = mSubNameTemp.get(i); - updateSingleSubFileName(entity, newName); - } - i++; - } - } - - /** - * 检查urls是否合法,并删除不合法的子任务 - * - * @return {@code true} 合法 - */ - private boolean checkUrls() { - if (mUrls.isEmpty()) { - ALog.e(TAG, "下载失败,子任务下载列表为null"); - return false; - } - Set delItem = new HashSet<>(); - - int i = 0; - for (String url : mUrls) { - if (TextUtils.isEmpty(url)) { - ALog.e(TAG, "子任务url为null,即将删除该子任务。"); - delItem.add(i); - continue; - } else if (!url.startsWith("http")) { - //} else if (!url.startsWith("http") && !url.startsWith("ftp")) { - ALog.e(TAG, "子任务url【" + url + "】错误,即将删除该子任务。"); - delItem.add(i); - continue; - } - int index = url.indexOf("://"); - if (index == -1) { - ALog.e(TAG, "子任务url【" + url + "】不合法,即将删除该子任务。"); - delItem.add(i); - continue; - } - - i++; - } - - for (int index : delItem) { - mUrls.remove(index); - if (mSubNameTemp != null && !mSubNameTemp.isEmpty()) { - mSubNameTemp.remove(index); - } - } - - mEntity.setGroupHash(CommonUtil.getMd5Code(mUrls)); - - return true; - } - - /** - * 更新单个子任务文件名 - */ - private void updateSingleSubFileName(DTaskWrapper taskEntity, String newName) { - DownloadEntity entity = taskEntity.getEntity(); - if (!newName.equals(entity.getFileName())) { - String oldPath = mEntity.getDirPath() + "/" + entity.getFileName(); - String newPath = mEntity.getDirPath() + "/" + newName; - if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=? or isComplete='true'", - newPath)) { - ALog.w(TAG, String.format("更新文件名失败,路径【%s】已存在或文件已下载", newPath)); - return; - } - - File oldFile = new File(oldPath); - if (oldFile.exists()) { - oldFile.renameTo(new File(newPath)); - } - - CommonUtil.modifyTaskRecord(oldFile.getPath(), newPath); - entity.setDownloadPath(newPath); - entity.setFileName(newName); - entity.update(); - } - } - - /** - * 如果用户设置了子任务文件名,检查子任务文件名 - * - * @return {@code true} 合法 - */ - private boolean checkSubName() { - if (mSubNameTemp == null || mSubNameTemp.isEmpty()) { - return true; - } - if (mUrls.size() != mSubNameTemp.size()) { - ALog.e(TAG, "子任务文件名必须和子任务数量一致"); - return false; - } - - return true; - } - - @CheckResult - @Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) { - for (DTaskWrapper subTask : mTaskWrapper.getSubTaskWrapper()) { - mDelegate.addHeader(subTask, key, value); - } - return mDelegate.addHeader(key, value); - } - - @CheckResult - @Override public DownloadGroupTarget addHeaders(Map headers) { - for (DTaskWrapper subTask : mTaskWrapper.getSubTaskWrapper()) { - mDelegate.addHeaders(subTask, headers); - } - return mDelegate.addHeaders(headers); - } - - @CheckResult - @Override public DownloadGroupTarget setUrlProxy(Proxy proxy) { - return mDelegate.setUrlProxy(proxy); - } -} +/* + * 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.support.annotation.CheckResult; +import android.support.annotation.NonNull; +import android.text.TextUtils; +import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.common.http.HttpHeaderDelegate; +import com.arialyy.aria.core.common.http.PostDelegate; +import com.arialyy.aria.core.inf.IHttpHeaderDelegate; +import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import java.io.File; +import java.net.Proxy; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created by AriaL on 2017/6/29. + * 下载任务组 + */ +public class DownloadGroupTarget extends BaseGroupTarget implements + IHttpHeaderDelegate { + private HttpHeaderDelegate mDelegate; + /** + * 子任务下载地址, + */ + private List mUrls = new ArrayList<>(); + + /** + * 子任务文件名 + */ + private List mSubNameTemp = new ArrayList<>(); + + public DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) { + setTargetName(targetName); + if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { + this.mUrls.addAll(groupEntity.getUrls()); + } + init(); + } + + DownloadGroupTarget(List urls, String targetName) { + setTargetName(targetName); + this.mUrls = urls; + init(); + } + + private void init() { + mGroupHash = CommonUtil.getMd5Code(mUrls); + setTaskWrapper(TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, mUrls)); + mEntity = getEntity(); + if (mEntity != null) { + mDirPathTemp = mEntity.getDirPath(); + } + mDelegate = new HttpHeaderDelegate<>(this); + } + + /** + * Post处理 + */ + public PostDelegate asPost() { + return new PostDelegate<>(this); + } + + /** + * 更新组合任务下载地址 + * + * @param urls 新的组合任务下载地址列表 + */ + @CheckResult + public DownloadGroupTarget updateUrls(List urls) { + if (urls == null || urls.isEmpty()) { + throw new NullPointerException("下载地址列表为空"); + } + if (urls.size() != mUrls.size()) { + throw new IllegalArgumentException("新下载地址数量和旧下载地址数量不一致"); + } + mUrls.clear(); + mUrls.addAll(urls); + mGroupHash = CommonUtil.getMd5Code(urls); + mEntity.setGroupHash(mGroupHash); + mEntity.update(); + if (mEntity.getSubEntities() != null && !mEntity.getSubEntities().isEmpty()) { + for (DownloadEntity de : mEntity.getSubEntities()) { + de.setGroupHash(mGroupHash); + de.update(); + } + } + return this; + } + + /** + * 任务组总任务大小,任务组是一个抽象的概念,没有真实的数据实体,任务组的大小是Aria动态获取子任务大小相加而得到的, + * 如果你知道当前任务组总大小,你也可以调用该方法给任务组设置大小 + * + * 为了更好的用户体验,组合任务必须设置文件大小 + * + * @param fileSize 任务组总大小 + */ + @CheckResult + public DownloadGroupTarget setFileSize(long fileSize) { + if (fileSize <= 0) { + ALog.e(TAG, "文件大小不能小于 0"); + return this; + } + if (mEntity.getFileSize() <= 1 || mEntity.getFileSize() != fileSize) { + mEntity.setFileSize(fileSize); + } + return this; + } + + /** + * 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址 + */ + @CheckResult + public DownloadGroupTarget setGroupUrl(List urls) { + mUrls.clear(); + mUrls.addAll(urls); + return this; + } + + /** + * 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 + * + * @deprecated {@link #setSubFileName(List)} 请使用该api + */ + @CheckResult + @Deprecated public DownloadGroupTarget setSubTaskFileName(List subTaskFileName) { + return setSubFileName(subTaskFileName); + } + + /** + * 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 + */ + @CheckResult + public DownloadGroupTarget setSubFileName(List subTaskFileName) { + if (subTaskFileName == null || subTaskFileName.isEmpty()) { + ALog.e(TAG, "修改子任务的文件名失败:列表为null"); + return this; + } + if (subTaskFileName.size() != getTaskWrapper().getSubTaskWrapper().size()) { + ALog.e(TAG, "修改子任务的文件名失败:子任务文件名列表数量和子任务的数量不匹配"); + return this; + } + mSubNameTemp.clear(); + mSubNameTemp.addAll(subTaskFileName); + return this; + } + + @Override public int getTargetType() { + return GROUP_HTTP; + } + + @Override protected boolean checkEntity() { + if (getTargetType() == GROUP_HTTP) { + if (!checkDirPath()) { + return false; + } + + if (!checkSubName()) { + return false; + } + + if (!checkUrls()) { + return false; + } + + if (getTaskWrapper().getEntity().getFileSize() == 0) { + ALog.e(TAG, "组合任务必须设置文件文件大小"); + return false; + } + + if (getTaskWrapper().asHttp().getRequestEnum() == RequestEnum.POST) { + for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { + subTask.asHttp().setRequestEnum(RequestEnum.POST); + } + } + + mEntity.save(); + + if (needModifyPath) { + reChangeDirPath(mDirPathTemp); + } + + if (!mSubNameTemp.isEmpty()) { + updateSingleSubFileName(); + } + return true; + } + return false; + } + + /** + * 更新所有改动的子任务文件名 + */ + private void updateSingleSubFileName() { + List entities = getTaskWrapper().getSubTaskWrapper(); + int i = 0; + for (DTaskWrapper entity : entities) { + if (i < mSubNameTemp.size()) { + String newName = mSubNameTemp.get(i); + updateSingleSubFileName(entity, newName); + } + i++; + } + } + + /** + * 检查urls是否合法,并删除不合法的子任务 + * + * @return {@code true} 合法 + */ + private boolean checkUrls() { + if (mUrls.isEmpty()) { + ALog.e(TAG, "下载失败,子任务下载列表为null"); + return false; + } + Set delItem = new HashSet<>(); + + int i = 0; + for (String url : mUrls) { + if (TextUtils.isEmpty(url)) { + ALog.e(TAG, "子任务url为null,即将删除该子任务。"); + delItem.add(i); + continue; + } else if (!url.startsWith("http")) { + //} else if (!url.startsWith("http") && !url.startsWith("ftp")) { + ALog.e(TAG, "子任务url【" + url + "】错误,即将删除该子任务。"); + delItem.add(i); + continue; + } + int index = url.indexOf("://"); + if (index == -1) { + ALog.e(TAG, "子任务url【" + url + "】不合法,即将删除该子任务。"); + delItem.add(i); + continue; + } + + i++; + } + + for (int index : delItem) { + mUrls.remove(index); + if (mSubNameTemp != null && !mSubNameTemp.isEmpty()) { + mSubNameTemp.remove(index); + } + } + + mEntity.setGroupHash(CommonUtil.getMd5Code(mUrls)); + + return true; + } + + /** + * 更新单个子任务文件名 + */ + private void updateSingleSubFileName(DTaskWrapper taskEntity, String newName) { + DownloadEntity entity = taskEntity.getEntity(); + if (!newName.equals(entity.getFileName())) { + String oldPath = mEntity.getDirPath() + "/" + entity.getFileName(); + String newPath = mEntity.getDirPath() + "/" + newName; + if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=? or isComplete='true'", + newPath)) { + ALog.w(TAG, String.format("更新文件名失败,路径【%s】已存在或文件已下载", newPath)); + return; + } + + File oldFile = new File(oldPath); + if (oldFile.exists()) { + oldFile.renameTo(new File(newPath)); + } + + CommonUtil.modifyTaskRecord(oldFile.getPath(), newPath); + entity.setDownloadPath(newPath); + entity.setFileName(newName); + entity.update(); + } + } + + /** + * 如果用户设置了子任务文件名,检查子任务文件名 + * + * @return {@code true} 合法 + */ + private boolean checkSubName() { + if (mSubNameTemp == null || mSubNameTemp.isEmpty()) { + return true; + } + if (mUrls.size() != mSubNameTemp.size()) { + ALog.e(TAG, "子任务文件名必须和子任务数量一致"); + return false; + } + + return true; + } + + @CheckResult + @Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) { + for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { + mDelegate.addHeader(subTask, key, value); + } + return mDelegate.addHeader(key, value); + } + + @CheckResult + @Override public DownloadGroupTarget addHeaders(Map headers) { + for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { + mDelegate.addHeaders(subTask, headers); + } + return mDelegate.addHeaders(headers); + } + + @CheckResult + @Override public DownloadGroupTarget setUrlProxy(Proxy proxy) { + return mDelegate.setUrlProxy(proxy); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index 386f7f63..68266865 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -1,143 +1,159 @@ -/* - * 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.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import com.arialyy.aria.core.common.http.GetDelegate; -import com.arialyy.aria.core.common.http.HttpHeaderDelegate; -import com.arialyy.aria.core.common.http.PostDelegate; -import com.arialyy.aria.core.inf.IHttpHeaderDelegate; -import java.net.Proxy; -import java.util.Map; - -/** - * Created by lyy on 2016/12/5. - * https://github.com/AriaLyy/Aria - */ -public class DownloadTarget extends BaseNormalTarget - implements IHttpHeaderDelegate { - private HttpHeaderDelegate mHeaderDelegate; - - DownloadTarget(DownloadEntity entity, String targetName) { - this(entity.getUrl(), targetName); - } - - DownloadTarget(String url, String targetName) { - initTarget(url, targetName); - mHeaderDelegate = new HttpHeaderDelegate<>(this); - } - - /** - * Post处理 - */ - @CheckResult - public PostDelegate asPost() { - return new PostDelegate<>(this); - } - - /** - * get参数传递 - */ - @CheckResult - public GetDelegate asGet(){ - return new GetDelegate<>(this); - } - - /** - * 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment;filename=***} - * 如果获取不到服务器文件名,则使用用户设置的文件名 - * - * @param use {@code true} 使用 - */ - @CheckResult - public DownloadTarget useServerFileName(boolean use) { - mTaskWrapper.asHttp().setUseServerFileName(use); - return this; - } - - /** - * 设置文件存储路径 - * 该api后续版本会删除 - * - * @param downloadPath 文件保存路径 - * @deprecated {@link #setFilePath(String)} 请使用这个api - */ - @CheckResult - @Deprecated public DownloadTarget setDownloadPath(@NonNull String downloadPath) { - return setFilePath(downloadPath); - } - - /** - * 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 - * 如:原文件路径 /mnt/sdcard/test.zip - * 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip - * - * @param filePath 路径必须为文件路径,不能为文件夹路径 - */ - @CheckResult - public DownloadTarget setFilePath(@NonNull String filePath) { - mTempFilePath = filePath; - return this; - } - - /** - * 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 - * 如:原文件路径 /mnt/sdcard/test.zip - * 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip - * - * @param filePath 路径必须为文件路径,不能为文件夹路径 - * @param forceDownload {@code true}强制下载,不考虑未见路径是否被占用 - */ - @CheckResult - public DownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) { - mTempFilePath = filePath; - this.forceDownload = forceDownload; - return this; - } - - /** - * 从header中获取文件描述信息 - */ - public String getContentDisposition() { - return mEntity.getDisposition(); - } - - @Override protected int getTargetType() { - return HTTP; - } - - /** - * 设置URL的代理 - * - * @param proxy {@link Proxy} - */ - @CheckResult - @Override public DownloadTarget setUrlProxy(Proxy proxy) { - return mHeaderDelegate.setUrlProxy(proxy); - } - - @CheckResult - @Override public DownloadTarget addHeader(@NonNull String key, @NonNull String value) { - return mHeaderDelegate.addHeader(key, value); - } - - @CheckResult - @Override public DownloadTarget addHeaders(Map headers) { - return mHeaderDelegate.addHeaders(headers); - } - -} +/* + * 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.support.annotation.CheckResult; +import android.support.annotation.NonNull; +import com.arialyy.aria.core.common.http.GetDelegate; +import com.arialyy.aria.core.common.http.HttpHeaderDelegate; +import com.arialyy.aria.core.common.http.PostDelegate; +import com.arialyy.aria.core.inf.IHttpHeaderDelegate; +import java.net.Proxy; +import java.util.Map; + +/** + * Created by lyy on 2016/12/5. + * https://github.com/AriaLyy/Aria + */ +public class DownloadTarget extends AbsDownloadTarget + implements IHttpHeaderDelegate { + private HttpHeaderDelegate mHeaderDelegate; + private DNormalDelegate mNormalDelegate; + + DownloadTarget(DownloadEntity entity, String targetName) { + this(entity.getUrl(), targetName); + } + + DownloadTarget(String url, String targetName) { + mNormalDelegate = new DNormalDelegate<>(this, url, targetName); + mHeaderDelegate = new HttpHeaderDelegate<>(this); + } + + /** + * Post处理 + */ + @CheckResult + public PostDelegate asPost() { + return new PostDelegate<>(this); + } + + /** + * get参数传递 + */ + @CheckResult + public GetDelegate asGet() { + return new GetDelegate<>(this); + } + + /** + * 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment;filename=***} + * 如果获取不到服务器文件名,则使用用户设置的文件名 + * + * @param use {@code true} 使用 + */ + @CheckResult + public DownloadTarget useServerFileName(boolean use) { + getTaskWrapper().asHttp().setUseServerFileName(use); + return this; + } + + /** + * 设置文件存储路径 + * 该api后续版本会删除 + * + * @param downloadPath 文件保存路径 + * @deprecated {@link #setFilePath(String)} 请使用这个api + */ + @CheckResult + @Deprecated public DownloadTarget setDownloadPath(@NonNull String downloadPath) { + return setFilePath(downloadPath); + } + + /** + * 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 + * 如:原文件路径 /mnt/sdcard/test.zip + * 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip + * + * @param filePath 路径必须为文件路径,不能为文件夹路径 + */ + @CheckResult + public DownloadTarget setFilePath(@NonNull String filePath) { + setTempFilePath(filePath); + return this; + } + + /** + * 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 + * 如:原文件路径 /mnt/sdcard/test.zip + * 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip + * + * @param filePath 路径必须为文件路径,不能为文件夹路径 + * @param forceDownload {@code true}强制下载,不考虑文件路径是否被占用 + */ + @CheckResult + public DownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) { + setTempFilePath(filePath); + setForceDownload(forceDownload); + return this; + } + + /** + * 从header中获取文件描述信息 + */ + public String getContentDisposition() { + return mEntity.getDisposition(); + } + + @Override public DownloadTarget updateUrl(String newUrl) { + return mNormalDelegate.updateUrl(newUrl); + } + + @Override public int getTargetType() { + return HTTP; + } + + /** + * 设置URL的代理 + * + * @param proxy {@link Proxy} + */ + @CheckResult + @Override public DownloadTarget setUrlProxy(Proxy proxy) { + return mHeaderDelegate.setUrlProxy(proxy); + } + + @CheckResult + @Override public DownloadTarget addHeader(@NonNull String key, @NonNull String value) { + return mHeaderDelegate.addHeader(key, value); + } + + @CheckResult + @Override public DownloadTarget addHeaders(Map headers) { + return mHeaderDelegate.addHeaders(headers); + } + + @Override protected boolean checkEntity() { + return mNormalDelegate.checkEntity(); + } + + @Override public boolean isRunning() { + return mNormalDelegate.isRunning(); + } + + @Override public boolean taskExists() { + return mNormalDelegate.taskExists(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java index e44d3a2b..41be403c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java @@ -1,145 +1,145 @@ -/* - * 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.support.annotation.CheckResult; -import android.text.TextUtils; -import com.arialyy.aria.core.FtpUrlEntity; -import com.arialyy.aria.core.common.ftp.FTPSDelegate; -import com.arialyy.aria.core.common.ftp.FtpDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IFtpTarget; -import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.util.ALog; -import java.net.Proxy; - -/** - * Created by Aria.Lao on 2017/7/26. - * ftp文件夹下载 - */ -public class FtpDirDownloadTarget extends BaseGroupTarget - implements IFtpTarget { - private FtpDelegate mDelegate; - - FtpDirDownloadTarget(String url, String targetName) { - mTargetName = targetName; - init(url); - } - - private void init(String key) { - mGroupHash = key; - mTaskWrapper = TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, key); - mTaskWrapper.setRequestType(AbsTaskWrapper.D_FTP_DIR); - mEntity = mTaskWrapper.getEntity(); - if (mEntity != null) { - mDirPathTemp = mEntity.getDirPath(); - } - mDelegate = new FtpDelegate<>(this); - } - - @Override protected int getTargetType() { - return GROUP_FTP_DIR; - } - - @Override protected boolean checkEntity() { - boolean b = getTargetType() == GROUP_FTP_DIR && checkDirPath() && checkUrl(); - if (b) { - mEntity.save(); - if (mTaskWrapper.getSubTaskWrapper() != null) { - //初始化子项的登录信息 - FtpUrlEntity tUrlEntity = mTaskWrapper.asFtp().getUrlEntity(); - for (DTaskWrapper wrapper : mTaskWrapper.getSubTaskWrapper()) { - FtpUrlEntity urlEntity = wrapper.asFtp().getUrlEntity(); - urlEntity.needLogin = tUrlEntity.needLogin; - urlEntity.account = tUrlEntity.account; - urlEntity.user = tUrlEntity.user; - urlEntity.password = tUrlEntity.password; - // 处理ftps详细 - if (tUrlEntity.isFtps) { - urlEntity.isFtps = true; - urlEntity.protocol = tUrlEntity.protocol; - urlEntity.storePath = tUrlEntity.storePath; - urlEntity.storePass = tUrlEntity.storePass; - urlEntity.keyAlias = tUrlEntity.keyAlias; - } - } - } - } - if (mTaskWrapper.asFtp().getUrlEntity().isFtps) { - if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().storePath)) { - ALog.e(TAG, "证书路径为空"); - return false; - } - if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) { - ALog.e(TAG, "证书别名为空"); - return false; - } - } - return b; - } - - /** - * 检查普通任务的下载地址 - * - * @return {@code true}地址合法 - */ - private boolean checkUrl() { - final String url = mGroupHash; - if (TextUtils.isEmpty(url)) { - ALog.e(TAG, "下载失败,url为null"); - return false; - } else if (!url.startsWith("ftp")) { - ALog.e(TAG, "下载失败,url【" + url + "】错误"); - return false; - } - int index = url.indexOf("://"); - if (index == -1) { - ALog.e(TAG, "下载失败,url【" + url + "】不合法"); - return false; - } - return true; - } - - /** - * 是否是FTPS协议 - * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} - * 设置证书信息 - */ - @CheckResult - public FTPSDelegate asFtps() { - mTaskWrapper.asFtp().getUrlEntity().isFtps = true; - return new FTPSDelegate<>(this); - } - - @CheckResult - @Override public FtpDirDownloadTarget charSet(String charSet) { - return mDelegate.charSet(charSet); - } - - @CheckResult - @Override public FtpDirDownloadTarget login(String userName, String password) { - return mDelegate.login(userName, password); - } - - @CheckResult - @Override public FtpDirDownloadTarget login(String userName, String password, String account) { - return mDelegate.login(userName, password, account); - } - - @Override public FtpDirDownloadTarget setProxy(Proxy proxy) { - return mDelegate.setProxy(proxy); - } -} +/* + * 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.support.annotation.CheckResult; +import android.text.TextUtils; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.common.ftp.FTPSDelegate; +import com.arialyy.aria.core.common.ftp.FtpDelegate; +import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IFtpTarget; +import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.util.ALog; +import java.net.Proxy; + +/** + * Created by Aria.Lao on 2017/7/26. + * ftp文件夹下载 + */ +public class FtpDirDownloadTarget extends BaseGroupTarget + implements IFtpTarget { + private FtpDelegate mDelegate; + + FtpDirDownloadTarget(String url, String targetName) { + setTargetName(targetName); + init(url); + } + + private void init(String key) { + mGroupHash = key; + setTaskWrapper(TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, key)); + getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP_DIR); + mEntity = getEntity(); + if (mEntity != null) { + mDirPathTemp = mEntity.getDirPath(); + } + mDelegate = new FtpDelegate<>(this); + } + + @Override public int getTargetType() { + return GROUP_FTP_DIR; + } + + @Override protected boolean checkEntity() { + boolean b = getTargetType() == GROUP_FTP_DIR && checkDirPath() && checkUrl(); + if (b) { + mEntity.save(); + if (getTaskWrapper().getSubTaskWrapper() != null) { + //初始化子项的登录信息 + FtpUrlEntity tUrlEntity = getTaskWrapper().asFtp().getUrlEntity(); + for (DTaskWrapper wrapper : getTaskWrapper().getSubTaskWrapper()) { + FtpUrlEntity urlEntity = wrapper.asFtp().getUrlEntity(); + urlEntity.needLogin = tUrlEntity.needLogin; + urlEntity.account = tUrlEntity.account; + urlEntity.user = tUrlEntity.user; + urlEntity.password = tUrlEntity.password; + // 处理ftps详细 + if (tUrlEntity.isFtps) { + urlEntity.isFtps = true; + urlEntity.protocol = tUrlEntity.protocol; + urlEntity.storePath = tUrlEntity.storePath; + urlEntity.storePass = tUrlEntity.storePass; + urlEntity.keyAlias = tUrlEntity.keyAlias; + } + } + } + } + if (getTaskWrapper().asFtp().getUrlEntity().isFtps) { + if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().storePath)) { + ALog.e(TAG, "证书路径为空"); + return false; + } + if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().keyAlias)) { + ALog.e(TAG, "证书别名为空"); + return false; + } + } + return b; + } + + /** + * 检查普通任务的下载地址 + * + * @return {@code true}地址合法 + */ + private boolean checkUrl() { + final String url = mGroupHash; + if (TextUtils.isEmpty(url)) { + ALog.e(TAG, "下载失败,url为null"); + return false; + } else if (!url.startsWith("ftp")) { + ALog.e(TAG, "下载失败,url【" + url + "】错误"); + return false; + } + int index = url.indexOf("://"); + if (index == -1) { + ALog.e(TAG, "下载失败,url【" + url + "】不合法"); + return false; + } + return true; + } + + /** + * 是否是FTPS协议 + * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} + * 设置证书信息 + */ + @CheckResult + public FTPSDelegate asFtps() { + getTaskWrapper().asFtp().getUrlEntity().isFtps = true; + return new FTPSDelegate<>(this); + } + + @CheckResult + @Override public FtpDirDownloadTarget charSet(String charSet) { + return mDelegate.charSet(charSet); + } + + @CheckResult + @Override public FtpDirDownloadTarget login(String userName, String password) { + return mDelegate.login(userName, password); + } + + @CheckResult + @Override public FtpDirDownloadTarget login(String userName, String password, String account) { + return mDelegate.login(userName, password, account); + } + + @Override public FtpDirDownloadTarget setProxy(Proxy proxy) { + return mDelegate.setProxy(proxy); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java index 14b3e4d6..3ba5a82e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java @@ -1,127 +1,155 @@ -/* - * 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.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import android.text.TextUtils; -import com.arialyy.aria.core.common.ftp.FTPSDelegate; -import com.arialyy.aria.core.common.ftp.FtpDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IFtpTarget; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.net.Proxy; - -/** - * Created by lyy on 2016/12/5. - * https://github.com/AriaLyy/Aria - */ -public class FtpDownloadTarget extends BaseNormalTarget - implements IFtpTarget { - private FtpDelegate mDelegate; - - public FtpDownloadTarget(DownloadEntity entity, String targetName) { - this(entity.getUrl(), targetName); - } - - FtpDownloadTarget(String url, String targetName) { - initTarget(url, targetName); - init(); - } - - private void init() { - int lastIndex = url.lastIndexOf("/"); - mEntity.setFileName(url.substring(lastIndex + 1)); - mTaskWrapper.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); - mTaskWrapper.setRequestType(AbsTaskWrapper.D_FTP); - - mDelegate = new FtpDelegate<>(this); - } - - /** - * 是否是FTPS协议 - * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} - * 设置证书信息 - */ - @CheckResult - public FTPSDelegate asFtps() { - mTaskWrapper.asFtp().getUrlEntity().isFtps = true; - return new FTPSDelegate<>(this); - } - - @Override protected boolean checkEntity() { - if (mTaskWrapper.asFtp().getUrlEntity().isFtps){ - if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().storePath)){ - ALog.e(TAG, "证书路径为空"); - return false; - } - if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)){ - ALog.e(TAG, "证书别名为空"); - return false; - } - } - return super.checkEntity(); - } - - /** - * 设置文件保存文件夹路径 - * - * @param filePath 文件保存路径 - * @deprecated {@link #setFilePath(String)} 请使用这个api - */ - @Deprecated - @CheckResult - public FtpDownloadTarget setDownloadPath(@NonNull String filePath) { - return setFilePath(filePath); - } - - /** - * 设置文件保存文件夹路径 - * 关于文件名: - * 1、如果保存路径是该文件的保存路径,如:/mnt/sdcard/file.zip,则使用路径中的文件名file.zip - * 2、如果保存路径是文件夹路径,如:/mnt/sdcard/,则使用FTP服务器该文件的文件名 - */ - @CheckResult - public FtpDownloadTarget setFilePath(@NonNull String filePath) { - mTempFilePath = filePath; - return this; - } - - @Override protected int getTargetType() { - return FTP; - } - - @CheckResult - @Override public FtpDownloadTarget charSet(String charSet) { - return mDelegate.charSet(charSet); - } - - @CheckResult - @Override public FtpDownloadTarget login(String userName, String password) { - return mDelegate.login(userName, password); - } - - @CheckResult - @Override public FtpDownloadTarget login(String userName, String password, String account) { - return mDelegate.login(userName, password, account); - } - - @CheckResult - @Override public FtpDownloadTarget setProxy(Proxy proxy) { - return mDelegate.setProxy(proxy); - } -} +/* + * 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.support.annotation.CheckResult; +import android.support.annotation.NonNull; +import android.text.TextUtils; +import com.arialyy.aria.core.common.ftp.FTPSDelegate; +import com.arialyy.aria.core.common.ftp.FtpDelegate; +import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IFtpTarget; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import java.net.Proxy; + +/** + * Created by lyy on 2016/12/5. + * https://github.com/AriaLyy/Aria + */ +public class FtpDownloadTarget extends AbsDownloadTarget + implements IFtpTarget { + private FtpDelegate mFtpDelegate; + private DNormalDelegate mNormalDelegate; + + FtpDownloadTarget(DownloadEntity entity, String targetName) { + this(entity.getUrl(), targetName); + } + + FtpDownloadTarget(String url, String targetName) { + mNormalDelegate = new DNormalDelegate<>(this, url, targetName); + init(); + } + + private void init() { + int lastIndex = getUrl().lastIndexOf("/"); + mEntity.setFileName(getUrl().substring(lastIndex + 1)); + getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getUrl())); + getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP); + + mFtpDelegate = new FtpDelegate<>(this); + } + + /** + * 是否是FTPS协议 + * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} + * 设置证书信息 + */ + @CheckResult + public FTPSDelegate asFtps() { + getTaskWrapper().asFtp().getUrlEntity().isFtps = true; + return new FTPSDelegate<>(this); + } + + @Override protected boolean checkEntity() { + if (getTaskWrapper().asFtp().getUrlEntity().isFtps) { + if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().storePath)) { + ALog.e(TAG, "证书路径为空"); + return false; + } + if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().keyAlias)) { + ALog.e(TAG, "证书别名为空"); + return false; + } + } + return mNormalDelegate.checkEntity(); + } + + @Override public boolean isRunning() { + return mNormalDelegate.isRunning(); + } + + @Override public boolean taskExists() { + return mNormalDelegate.taskExists(); + } + + /** + * 设置文件保存文件夹路径 + * + * @param filePath 文件保存路径 + * @deprecated {@link #setFilePath(String)} 请使用这个api + */ + @Deprecated + @CheckResult + public FtpDownloadTarget setDownloadPath(@NonNull String filePath) { + return setFilePath(filePath); + } + + /** + * 设置文件保存文件夹路径 + * 关于文件名: + * 1、如果保存路径是该文件的保存路径,如:/mnt/sdcard/file.zip,则使用路径中的文件名file.zip + * 2、如果保存路径是文件夹路径,如:/mnt/sdcard/,则使用FTP服务器该文件的文件名 + */ + @CheckResult + public FtpDownloadTarget setFilePath(@NonNull String filePath) { + setTempFilePath(filePath); + return this; + } + + /** + * 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 + * 如:原文件路径 /mnt/sdcard/test.zip + * 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip + * + * @param filePath 路径必须为文件路径,不能为文件夹路径 + * @param forceDownload {@code true}强制下载,不考虑文件路径是否被占用 + */ + @CheckResult + public FtpDownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) { + setTempFilePath(filePath); + setForceDownload(forceDownload); + return this; + } + + @Override public int getTargetType() { + return FTP; + } + + @CheckResult + @Override public FtpDownloadTarget charSet(String charSet) { + return mFtpDelegate.charSet(charSet); + } + + @CheckResult + @Override public FtpDownloadTarget login(String userName, String password) { + return mFtpDelegate.login(userName, password); + } + + @CheckResult + @Override public FtpDownloadTarget login(String userName, String password, String account) { + return mFtpDelegate.login(userName, password, account); + } + + @CheckResult + @Override public FtpDownloadTarget setProxy(Proxy proxy) { + return mFtpDelegate.setProxy(proxy); + } + + @Override public FtpDownloadTarget updateUrl(String newUrl) { + return mNormalDelegate.updateUrl(newUrl); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 44ad1b90..9b380196 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -1,312 +1,348 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.inf; - -import android.support.annotation.CheckResult; -import android.text.TextUtils; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.command.ICmd; -import com.arialyy.aria.core.command.normal.CancelCmd; -import com.arialyy.aria.core.command.normal.NormalCmdFactory; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by AriaL on 2017/7/3. - */ -public abstract class AbsTarget - implements ITarget { - protected String TAG; - protected ENTITY mEntity; - protected TASK_WRAPPER mTaskWrapper; - protected String mTargetName; - - protected AbsTarget() { - TAG = CommonUtil.getClassName(this); - } - - /** - * 重置状态,将任务状态设置为未开始状态 - * 注意:如果在后续方法调用链中没有调用 {@link #start()}、{@link #stop()}、{@link #cancel()}、{@link #resume()} - * 等操作任务的方法,那么你需要调用{@link #save()}才能将修改保存到数据库 - */ - @CheckResult(suggest = "after use #start()、#stop()、#cancel()、#resume()、#save()?") - public TARGET resetState() { - mTaskWrapper.getEntity().setState(IEntity.STATE_WAIT); - mTaskWrapper.setRefreshInfo(true); - return (TARGET) this; - } - - /** - * 删除记录,如果任务正在执行,则会删除正在下载的任务 - */ - public void removeRecord() { - if (isRunning()) { - ALog.d("AbsTarget", "任务正在下载,即将删除任务"); - cancel(); - } else { - if (mEntity instanceof AbsNormalEntity) { - TaskRecord record = - DbEntity.findFirst(TaskRecord.class, "TaskRecord.filePath=?", mTaskWrapper.getKey()); - if (record != null) { - CommonUtil.delTaskRecord(record, mTaskWrapper.isRemoveFile(), (AbsNormalEntity) mEntity); - } else { - mEntity.deleteData(); - } - } else if (mEntity instanceof DownloadGroupEntity) { - CommonUtil.delGroupTaskRecord(mTaskWrapper.isRemoveFile(), ((DownloadGroupEntity) mEntity)); - } - TaskWrapperManager.getInstance().removeTaskWrapper(mEntity.getKey()); - } - } - - /** - * 获取任务实体 - */ - public TASK_WRAPPER getTaskWrapper() { - return mTaskWrapper; - } - - /** - * 获取任务进度,如果任务存在,则返回当前进度 - * - * @return 该任务进度 - */ - public long getCurrentProgress() { - return mEntity == null ? -1 : mEntity.getCurrentProgress(); - } - - /** - * 获取任务文件大小 - * - * @return 文件大小 - */ - public long getSize() { - return mEntity == null ? 0 : mEntity.getFileSize(); - } - - /** - * 获取单位转换后的文件大小 - * - * @return 文件大小{@code xxx mb} - */ - public String getConvertSize() { - return mEntity == null ? "0b" : CommonUtil.formatFileSize(mEntity.getFileSize()); - } - - /** - * 设置扩展字段,用来保存你的其它数据,如果你的数据比较多,你可以把你的数据转换为JSON字符串,然后再存到Aria中 - * 注意:如果在后续方法调用链中没有调用 {@link #start()}、{@link #stop()}、{@link #cancel()}、{@link #resume()} - * 等操作任务的方法,那么你需要调用{@link #save()}才能将修改保存到数据库 - * - * @param str 扩展数据 - */ - @CheckResult(suggest = "after use #start()、#stop()、#cancel()、#resume()、#save()?") - public TARGET setExtendField(String str) { - if (TextUtils.isEmpty(str)) return (TARGET) this; - if (TextUtils.isEmpty(mEntity.getStr()) || !mEntity.getStr().equals(str)) { - mEntity.setStr(str); - } else { - ALog.e(TAG, "设置扩展字段失败,扩展字段为一致"); - } - - return (TARGET) this; - } - - /** - * 获取存放的扩展字段 - * 设置扩展字段{@link #setExtendField(String)} - */ - public String getExtendField() { - return mEntity.getStr(); - } - - /** - * 获取任务状态 - * - * @return {@link IEntity} - */ - public int getTaskState() { - return mEntity.getState(); - } - - /** - * 获取任务进度百分比 - * - * @return 返回任务进度 - */ - public int getPercent() { - if (mEntity == null) { - ALog.e("AbsTarget", "下载管理器中没有该任务"); - return 0; - } - if (mEntity.getFileSize() != 0) { - return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize()); - } - return 0; - } - - /** - * 检查实体是否合法,如果实体合法,将保存实体到数据库,或更新数据库中的实体对象 - * - * @return {@code true} 合法 - */ - protected abstract boolean checkEntity(); - - protected int checkTaskType() { - int taskType = 0; - if (mTaskWrapper instanceof DTaskWrapper) { - taskType = ICmd.TASK_TYPE_DOWNLOAD; - } else if (mTaskWrapper instanceof DGTaskWrapper) { - taskType = ICmd.TASK_TYPE_DOWNLOAD_GROUP; - } else if (mTaskWrapper instanceof UTaskWrapper) { - taskType = ICmd.TASK_TYPE_UPLOAD; - } - return taskType; - } - - /** - * 保存修改 - */ - @Override public void save() { - if (!checkEntity()) { - ALog.e(TAG, "保存修改失败"); - } else { - ALog.i(TAG, "保存成功"); - } - } - - /** - * 任务是否在执行 - * - * @return {@code true} 任务正在执行 - */ - public abstract boolean isRunning(); - - /** - * 任务是否存在 - * - * @return {@code true} 任务存在 - */ - public abstract boolean taskExists(); - - /** - * 开始任务 - */ - @Override public void start() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd( - CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_START, - checkTaskType())) - .exe(); - } - } - - /** - * 停止任务 - * - * @see #stop() - */ - @Deprecated public void pause() { - if (checkEntity()) { - stop(); - } - } - - @Override public void stop() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd( - CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_STOP, checkTaskType())) - .exe(); - } - } - - /** - * 恢复任务 - */ - @Override public void resume() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd( - CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_START, - checkTaskType())) - .exe(); - } - } - - /** - * 删除任务 - */ - @Override public void cancel() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CANCEL, - checkTaskType())) - .exe(); - } - } - - /** - * 任务重试 - */ - @Override public void reTry() { - if (checkEntity()) { - List cmds = new ArrayList<>(); - int taskType = checkTaskType(); - cmds.add( - CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_STOP, taskType)); - cmds.add(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_START, taskType)); - AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe(); - } - } - - /** - * 删除任务 - * - * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 - * {@code false}如果任务已经完成,只删除任务数据库记录, - */ - @Override public void cancel(boolean removeFile) { - if (checkEntity()) { - CancelCmd cancelCmd = - (CancelCmd) CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CANCEL, - checkTaskType()); - cancelCmd.removeFile = removeFile; - AriaManager.getInstance(AriaManager.APP).setCmd(cancelCmd).exe(); - } - } - - /** - * 重新下载 - */ - @Override public void reStart() { - if (checkEntity()) { - cancel(); - start(); - } - } -} +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import android.support.annotation.CheckResult; +import android.text.TextUtils; +import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.command.ICmd; +import com.arialyy.aria.core.command.normal.CancelCmd; +import com.arialyy.aria.core.command.normal.NormalCmdFactory; +import com.arialyy.aria.core.common.TaskRecord; +import com.arialyy.aria.core.download.DGTaskWrapper; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.core.upload.UTaskWrapper; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by AriaL on 2017/7/3. + */ +public abstract class AbsTarget + implements ITargetHandler { + + protected String TAG; + protected ENTITY mEntity; + private TASK_WRAPPER mTaskWrapper; + private String mTargetName; + + protected AbsTarget() { + TAG = CommonUtil.getClassName(this); + } + + /** + * 重置状态,将任务状态设置为未开始状态 + * 注意:如果在后续方法调用链中没有调用 {@link #start()}、{@link #stop()}、{@link #cancel()}、{@link #resume()} + * 等操作任务的方法,那么你需要调用{@link #save()}才能将修改保存到数据库 + */ + @CheckResult(suggest = "after use #start()、#stop()、#cancel()、#resume()、#save()?") + public TARGET resetState() { + mTaskWrapper.getEntity().setState(IEntity.STATE_WAIT); + mTaskWrapper.setRefreshInfo(true); + return (TARGET) this; + } + + /** + * 删除记录,如果任务正在执行,则会删除正在下载的任务 + */ + public void removeRecord() { + if (isRunning()) { + ALog.d("AbsTarget", "任务正在下载,即将删除任务"); + cancel(); + } else { + if (mEntity instanceof AbsNormalEntity) { + TaskRecord record = + DbEntity.findFirst(TaskRecord.class, "TaskRecord.filePath=?", mTaskWrapper.getKey()); + if (record != null) { + CommonUtil.delTaskRecord(record, mTaskWrapper.isRemoveFile(), (AbsNormalEntity) mEntity); + } else { + mEntity.deleteData(); + } + } else if (mEntity instanceof DownloadGroupEntity) { + CommonUtil.delGroupTaskRecord(mTaskWrapper.isRemoveFile(), ((DownloadGroupEntity) mEntity)); + } + TaskWrapperManager.getInstance().removeTaskWrapper(mEntity.getKey()); + } + } + + public ENTITY getEntity() { + return mEntity; + } + + public void setTaskWrapper(TASK_WRAPPER mTaskWrapper) { + this.mTaskWrapper = mTaskWrapper; + } + + public String getTargetName() { + return mTargetName; + } + + public void setTargetName(String mTargetName) { + this.mTargetName = mTargetName; + } + + /** + * 获取任务实体 + */ + public TASK_WRAPPER getTaskWrapper() { + return mTaskWrapper; + } + + /** + * 获取任务进度,如果任务存在,则返回当前进度 + * + * @return 该任务进度 + */ + public long getCurrentProgress() { + return mEntity == null ? -1 : mEntity.getCurrentProgress(); + } + + /** + * 获取任务文件大小 + * + * @return 文件大小 + */ + public long getFileSize() { + return mEntity == null ? 0 : mEntity.getFileSize(); + } + + /** + * 获取单位转换后的文件大小 + * + * @return 文件大小{@code xxx mb} + */ + public String getConvertFileSize() { + return mEntity == null ? "0b" : CommonUtil.formatFileSize(mEntity.getFileSize()); + } + + /** + * 设置扩展字段,用来保存你的其它数据,如果你的数据比较多,你可以把你的数据转换为JSON字符串,然后再存到Aria中 + * 注意:如果在后续方法调用链中没有调用 {@link #start()}、{@link #stop()}、{@link #cancel()}、{@link #resume()} + * 等操作任务的方法,那么你需要调用{@link #save()}才能将修改保存到数据库 + * + * @param str 扩展数据 + */ + @CheckResult(suggest = "after use #start()、#stop()、#cancel()、#resume()、#save()?") + public TARGET setExtendField(String str) { + if (TextUtils.isEmpty(str)) return (TARGET) this; + if (TextUtils.isEmpty(mEntity.getStr()) || !mEntity.getStr().equals(str)) { + mEntity.setStr(str); + } else { + ALog.e(TAG, "设置扩展字段失败,扩展字段为一致"); + } + + return (TARGET) this; + } + + /** + * 获取存放的扩展字段 + * 设置扩展字段{@link #setExtendField(String)} + */ + public String getExtendField() { + return mEntity.getStr(); + } + + /** + * 获取任务状态 + * + * @return {@link IEntity} + */ + public int getTaskState() { + return mEntity.getState(); + } + + /** + * 获取任务进度百分比 + * + * @return 返回任务进度 + */ + public int getPercent() { + if (mEntity == null) { + ALog.e("AbsTarget", "下载管理器中没有该任务"); + return 0; + } + if (mEntity.getFileSize() != 0) { + return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize()); + } + return 0; + } + + /** + * 检查实体是否合法,如果实体合法,将保存实体到数据库,或更新数据库中的实体对象 + * + * @return {@code true} 合法 + */ + protected abstract boolean checkEntity(); + + protected int checkTaskType() { + int taskType = 0; + if (mTaskWrapper instanceof DTaskWrapper) { + taskType = ICmd.TASK_TYPE_DOWNLOAD; + } else if (mTaskWrapper instanceof DGTaskWrapper) { + taskType = ICmd.TASK_TYPE_DOWNLOAD_GROUP; + } else if (mTaskWrapper instanceof UTaskWrapper) { + taskType = ICmd.TASK_TYPE_UPLOAD; + } + return taskType; + } + + /** + * 保存修改 + */ + @Override public void save() { + if (!checkEntity()) { + ALog.e(TAG, "保存修改失败"); + } else { + ALog.i(TAG, "保存成功"); + } + } + + /** + * 任务是否在执行 + * + * @return {@code true} 任务正在执行 + */ + public abstract boolean isRunning(); + + /** + * 任务是否存在 + * + * @return {@code true} 任务存在 + */ + public abstract boolean taskExists(); + + /** + * 设置target类型 + * + * @return {@link #HTTP}、{@link #FTP}、{@link #GROUP_HTTP}、{@link #GROUP_FTP_DIR} + */ + public abstract int getTargetType(); + + /** + * 添加任务 + */ + @Override public void add() { + if (checkEntity()) { + AriaManager.getInstance(AriaManager.APP) + .setCmd(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CREATE, + checkTaskType())) + .exe(); + } + } + + /** + * 开始任务 + */ + @Override public void start() { + if (checkEntity()) { + AriaManager.getInstance(AriaManager.APP) + .setCmd( + CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_START, + checkTaskType())) + .exe(); + } + } + + /** + * 停止任务 + * + * @see #stop() + */ + @Deprecated public void pause() { + if (checkEntity()) { + stop(); + } + } + + @Override public void stop() { + if (checkEntity()) { + AriaManager.getInstance(AriaManager.APP) + .setCmd( + CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_STOP, checkTaskType())) + .exe(); + } + } + + /** + * 恢复任务 + */ + @Override public void resume() { + if (checkEntity()) { + AriaManager.getInstance(AriaManager.APP) + .setCmd( + CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_START, + checkTaskType())) + .exe(); + } + } + + /** + * 删除任务 + */ + @Override public void cancel() { + if (checkEntity()) { + AriaManager.getInstance(AriaManager.APP) + .setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CANCEL, + checkTaskType())) + .exe(); + } + } + + /** + * 任务重试 + */ + @Override public void reTry() { + if (checkEntity()) { + List cmds = new ArrayList<>(); + int taskType = checkTaskType(); + cmds.add( + CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_STOP, taskType)); + cmds.add(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_START, taskType)); + AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe(); + } + } + + /** + * 删除任务 + * + * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 + * {@code false}如果任务已经完成,只删除任务数据库记录, + */ + @Override public void cancel(boolean removeFile) { + if (checkEntity()) { + CancelCmd cancelCmd = + (CancelCmd) CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CANCEL, + checkTaskType()); + cancelCmd.removeFile = removeFile; + AriaManager.getInstance(AriaManager.APP).setCmd(cancelCmd).exe(); + } + } + + /** + * 重新下载 + */ + @Override public void reStart() { + if (checkEntity()) { + cancel(); + start(); + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java index 2fc81a17..c1b5faf6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java @@ -1,57 +1,57 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.inf; - -import android.support.annotation.CheckResult; -import java.net.Proxy; - -/** - * Created by laoyuyu on 2018/3/9. - */ -public interface IFtpTarget { - /** - * 设置字符编码 - */ - @CheckResult - TARGET charSet(String charSet); - - /** - * ftp 用户登录信。 - * - * @param userName ftp用户名 - * @param password ftp用户密码 - */ - @CheckResult - TARGET login(String userName, String password); - - /** - * ftp 用户登录信息 - * - * @param userName ftp用户名 - * @param password ftp用户密码 - * @param account ftp账号 - */ - @CheckResult - TARGET login(String userName, String password, String account); - - /** - * 设置代理 - * - * @param proxy {@link Proxy} - */ - @CheckResult - TARGET setProxy(Proxy proxy); -} +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import android.support.annotation.CheckResult; +import java.net.Proxy; + +/** + * Created by laoyuyu on 2018/3/9. + */ +public interface IFtpTarget { + /** + * 设置字符编码 + */ + @CheckResult + TARGET charSet(String charSet); + + /** + * ftp 用户登录信。 + * + * @param userName ftp用户名 + * @param password ftp用户密码 + */ + @CheckResult + TARGET login(String userName, String password); + + /** + * ftp 用户登录信息 + * + * @param userName ftp用户名 + * @param password ftp用户密码 + * @param account ftp账号 + */ + @CheckResult + TARGET login(String userName, String password, String account); + + /** + * 设置代理 + * + * @param proxy {@link Proxy} + */ + @CheckResult + TARGET setProxy(Proxy proxy); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java index 80307556..fdc930ba 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java @@ -1,52 +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.inf; - -import android.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.download.DownloadEntity; -import java.net.Proxy; -import java.util.Map; - -/** - * Created by laoyuyu on 2018/3/9. - * HTTP Header功能接口 - */ -public interface IHttpHeaderDelegate { - - /** - * 给url请求添加Header数据 - * 如果新的header数据和数据保存的不一致,则更新数据库中对应的header数据 - * - * @param key header对应的key - * @param value header对应的value - */ - @CheckResult - TARGET addHeader(@NonNull String key, @NonNull String value); - - /** - * 给url请求添加一组header数据 - * 如果新的header数据和数据保存的不一致,则更新数据库中对应的header数据 - * - * @param headers 一组http header数据 - */ - @CheckResult - TARGET addHeaders(Map headers); - - @CheckResult - TARGET setUrlProxy(Proxy proxy); -} +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import android.support.annotation.CheckResult; +import android.support.annotation.NonNull; +import java.net.Proxy; +import java.util.Map; + +/** + * Created by laoyuyu on 2018/3/9. + * HTTP Header功能接口 + */ +public interface IHttpHeaderDelegate { + + /** + * 给url请求添加Header数据 + * 如果新的header数据和数据保存的不一致,则更新数据库中对应的header数据 + * + * @param key header对应的key + * @param value header对应的value + */ + @CheckResult + TARGET addHeader(@NonNull String key, @NonNull String value); + + /** + * 给url请求添加一组header数据 + * 如果新的header数据和数据保存的不一致,则更新数据库中对应的header数据 + * + * @param headers 一组http header数据 + */ + @CheckResult + TARGET addHeaders(Map headers); + + @CheckResult + TARGET setUrlProxy(Proxy proxy); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHandler.java similarity index 77% rename from Aria/src/main/java/com/arialyy/aria/core/inf/ITarget.java rename to Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHandler.java index ce1ef35c..0c2ae824 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHandler.java @@ -1,64 +1,77 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.inf; - -/** - * Created by AriaL on 2017/6/29. - */ -public interface ITarget { - /** - * 开始下载 - */ - void start(); - - /** - * 停止下载 - */ - void stop(); - - /** - * 恢复下载 - */ - void resume(); - - /** - * 取消下载 - */ - void cancel(); - - /** - * 保存修改 - */ - void save(); - - /** - * 删除任务 - * - * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 - * {@code false}如果任务已经完成,只删除任务数据库记录, - */ - void cancel(boolean removeFile); - - /** - * 任务重试 - */ - void reTry(); - - /** - * 重新下载 - */ - void reStart(); -} +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +/** + * Created by AriaL on 2017/6/29. + * 任务接收器的控制接口,处理添加任务、开始任务、停止任务、删除任务等功能 + */ +public interface ITargetHandler { + int HTTP = 1; + int FTP = 2; + //HTTP任务组 + int GROUP_HTTP = 3; + //FTP文件夹 + int GROUP_FTP_DIR = 4; + + /** + * 添加任务 + */ + void add(); + + /** + * 开始下载 + */ + void start(); + + /** + * 停止下载 + */ + void stop(); + + /** + * 恢复下载 + */ + void resume(); + + /** + * 取消下载 + */ + void cancel(); + + /** + * 保存修改 + */ + void save(); + + /** + * 删除任务 + * + * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 + * {@code false}如果任务已经完成,只删除任务数据库记录, + */ + void cancel(boolean removeFile); + + /** + * 任务重试 + */ + void reTry(); + + /** + * 重新下载 + */ + void reStart(); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHeadDelegate.java similarity index 82% rename from Aria/src/main/java/com/arialyy/aria/core/inf/ITaskDelegate.java rename to Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHeadDelegate.java index 5f357575..ee49769f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHeadDelegate.java @@ -1,23 +1,24 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.inf; - -/** - * 任务信息 - */ -public interface ITaskDelegate { - -} +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +/** + * Created by AriaL on 2017/6/29. + * 处理任务头部信息等设置等接口 + */ +public interface ITargetHeadDelegate { + +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetNormal.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetNormal.java new file mode 100644 index 00000000..a2d4a9e4 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITargetNormal.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +/** + * Created by lyy on 2019/4/5. + * 普通任务接收器功能接口 + */ +public interface ITargetNormal { + + /** + * 通过地址初始化target + * + * @param url 下载url、上传url + * @param targetName 接收器名称 + */ + void initTarget(String url, String targetName); + + /** + * 更新下载url + * + * @param newUrl 新的下载url + */ + TARGET updateUrl(String newUrl); + + /** + * 获取实体 + */ + AbsEntity getEntity(); + + /** + * 任务是否存在 + * + * @return {@code true}任务存在,{@code false} 任务不存在 + */ + boolean taskExists(); + + /** + * 任务是否在执行 + * + * @return {@code true} 任务正在执行,{@code false} 任务没有执行 + */ + boolean isRunning(); + + /** + * 检查下载实体,判断实体是否合法 合法标准为: + * 1、下载路径不为null,并且下载路径是正常的http或ftp路径 + * 2、保存路径不为null,并且保存路径是android文件系统路径 + * 3、保存路径不能重复 + * + * @return {@code true}合法 + */ + boolean checkEntity(); + + /** + * 检查并设置普通任务的文件保存路径 + * + * @return {@code true}保存路径合法 + */ + boolean checkFilePath(); + + /** + * 检查普通任务的下载地址 + * + * @return {@code true}地址合法 + */ + boolean checkUrl(); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java index e8759e5d..9540ec57 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java @@ -1,128 +1,133 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.inf; - -/** - * Created by lyy on 2017/2/13. - */ -public interface ITask { - - /** - * 普通下载任务 - */ - int DOWNLOAD = 1; - /** - * 上传任务 - */ - int UPLOAD = 2; - /** - * 组合任务 - */ - int DOWNLOAD_GROUP = 3; - /** - * 组合任务的子任务 - */ - int DOWNLOAD_GROUP_SUB = 4; - /** - * 未知 - */ - int OTHER = -1; - - /** - * 获取任务类型 - * - * @return {@link #DOWNLOAD}、{@link #UPLOAD}、{@link #DOWNLOAD_GROUP} - */ - int getTaskType(); - - /** - * 获取下载状态 - */ - int getState(); - - /** - * 唯一标识符,DownloadTask 为下载地址,UploadTask 为文件路径 - */ - String getKey(); - - /** - * 任务是否正在执行 - * - * @return true,正在执行; - */ - boolean isRunning(); - - /** - * 获取信息实体 - */ - TASK_WRAPPER getTaskWrapper(); - - void start(); - - /** - * 停止任务 - */ - void stop(); - - /** - * 停止任务 - * - * @param type {@code 0}默认操作,{@code 1}停止任务不自动执行下一任务 - */ - void stop(int type); - - /** - * 删除任务 - */ - void cancel(); - - /** - * 原始byte速度 - */ - long getSpeed(); - - /** - * 转换单位后的速度 - */ - String getConvertSpeed(); - - /** - * 获取百分比进度 - */ - int getPercent(); - - /** - * 原始文件byte长度 - */ - long getFileSize(); - - /** - * 转换单位后的文件长度 - */ - String getConvertFileSize(); - - /** - * 获取当前进度 - */ - long getCurrentProgress(); - - /** - * 获取单位转换后的进度 - * - * @return 返回 3mb - */ - String getConvertCurrentProgress(); -} +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.upload.UploadTask; + +/** + * Created by lyy on 2017/2/13. + * 任务接口{@link DownloadTask}、{@link UploadTask}、{@link DownloadGroupTask} + */ +public interface ITask { + + /** + * 普通下载任务 + */ + int DOWNLOAD = 1; + /** + * 上传任务 + */ + int UPLOAD = 2; + /** + * 组合任务 + */ + int DOWNLOAD_GROUP = 3; + /** + * 组合任务的子任务 + */ + int DOWNLOAD_GROUP_SUB = 4; + /** + * 未知 + */ + int OTHER = -1; + + /** + * 获取任务类型 + * + * @return {@link #DOWNLOAD}、{@link #UPLOAD}、{@link #DOWNLOAD_GROUP} + */ + int getTaskType(); + + /** + * 获取下载状态 + */ + int getState(); + + /** + * 唯一标识符,DownloadTask 为下载地址,UploadTask 为文件路径 + */ + String getKey(); + + /** + * 任务是否正在执行 + * + * @return true,正在执行; + */ + boolean isRunning(); + + /** + * 获取信息实体 + */ + TASK_WRAPPER getTaskWrapper(); + + void start(); + + /** + * 停止任务 + */ + void stop(); + + /** + * 停止任务 + * + * @param type {@code 0}默认操作,{@code 1}停止任务不自动执行下一任务 + */ + void stop(int type); + + /** + * 删除任务 + */ + void cancel(); + + /** + * 原始byte速度 + */ + long getSpeed(); + + /** + * 转换单位后的速度 + */ + String getConvertSpeed(); + + /** + * 获取百分比进度 + */ + int getPercent(); + + /** + * 原始文件byte长度 + */ + long getFileSize(); + + /** + * 转换单位后的文件长度 + */ + String getConvertFileSize(); + + /** + * 获取当前进度 + */ + long getCurrentProgress(); + + /** + * 获取单位转换后的进度 + * + * @return 返回 3mb + */ + String getConvertCurrentProgress(); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/AbsUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/AbsUploadTarget.java index a5f39961..d1b03718 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/AbsUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/AbsUploadTarget.java @@ -1,28 +1,47 @@ -/* - * 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.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; - -/** - * Created by AriaL on 2017/6/29. - */ -abstract class AbsUploadTarget - extends AbsTarget { - -} +/* + * 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.inf.AbsTarget; + +/** + * Created by AriaL on 2017/6/29. + * 普通上传任务接收器 + */ +abstract class AbsUploadTarget + extends AbsTarget { + + /** + * 上传路径 + */ + private String mTempUrl; + + @Override public void setTaskWrapper(UTaskWrapper mTaskWrapper) { + super.setTaskWrapper(mTaskWrapper); + } + + String getTempUrl() { + return mTempUrl; + } + + void setTempUrl(String tempUrl) { + this.mTempUrl = tempUrl; + } + + void setEntity(UploadEntity entity) { + this.mEntity = entity; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java index f29668e2..7b095415 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java @@ -1,146 +1,146 @@ -/* - * 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.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import android.text.TextUtils; -import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.queue.UploadTaskQueue; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.ALog; -import java.io.File; - -/** - * Created by AriaL on 2018/3/9. - */ -abstract class BaseNormalTarget - extends AbsUploadTarget { - - protected String mTempUrl; - - void initTarget(String filePath) { - mTaskWrapper = - TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath); - mEntity = mTaskWrapper.getEntity(); - File file = new File(filePath); - mEntity.setFileName(file.getName()); - mEntity.setFileSize(file.length()); - mTempUrl = mEntity.getUrl(); - } - - /** - * 设置上传路径 - * - * @param uploadUrl 上传路径 - */ - @CheckResult - public TARGET setUploadUrl(@NonNull String uploadUrl) { - mTempUrl = uploadUrl; - return (TARGET) this; - } - - /** - * 上传任务是否存在 - * - * @return {@code true}存在 - */ - @Override public boolean taskExists() { - return DbEntity.checkDataExist(UploadEntity.class, "key=?", mEntity.getFilePath()); - } - - /** - * 是否在上传 - * - * @deprecated {@link #isRunning()} - */ - public boolean isUploading() { - return isRunning(); - } - - @Override public boolean isRunning() { - UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey()); - return task != null && task.isRunning(); - } - - @Override protected boolean checkEntity() { - boolean b = checkUrl() && checkFilePath(); - if (b) { - mEntity.save(); - } - if (mTaskWrapper.asFtp().getUrlEntity() != null && mTaskWrapper.asFtp().getUrlEntity().isFtps) { - //if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) { - // ALog.e(TAG, "证书路径为空"); - // return false; - //} - if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) { - ALog.e(TAG, "证书别名为空"); - return false; - } - } - return b; - } - - /** - * 检查上传文件路径是否合法 - * - * @return {@code true} 合法 - */ - private boolean checkFilePath() { - String filePath = mEntity.getFilePath(); - if (TextUtils.isEmpty(filePath)) { - ALog.e(TAG, "上传失败,文件路径为null"); - return false; - } else if (!filePath.startsWith("/")) { - ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法"); - return false; - } - - File file = new File(mEntity.getFilePath()); - if (!file.exists()) { - ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在"); - return false; - } - if (file.isDirectory()) { - ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹"); - return false; - } - return true; - } - - /** - * 检查普通任务的下载地址 - * - * @return {@code true}地址合法 - */ - protected boolean checkUrl() { - final String url = mTempUrl; - if (TextUtils.isEmpty(url)) { - ALog.e(TAG, "上传失败,url为null"); - return false; - } else if (!url.startsWith("http") && !url.startsWith("ftp")) { - ALog.e(TAG, "上传失败,url【" + url + "】错误"); - return false; - } - int index = url.indexOf("://"); - if (index == -1) { - ALog.e(TAG, "上传失败,url【" + url + "】不合法"); - return false; - } - mEntity.setUrl(url); - return true; - } -} +///* +// * 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.support.annotation.CheckResult; +//import android.support.annotation.NonNull; +//import android.text.TextUtils; +//import com.arialyy.aria.core.manager.TaskWrapperManager; +//import com.arialyy.aria.core.queue.UploadTaskQueue; +//import com.arialyy.aria.orm.DbEntity; +//import com.arialyy.aria.util.ALog; +//import java.io.File; +// +///** +// * Created by AriaL on 2018/3/9. +// */ +//abstract class BaseNormalTarget +// extends AbsUploadTarget { +// +// protected String mTempUrl; +// +// void initTarget(String filePath) { +// mTaskWrapper = +// TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath); +// mEntity = mTaskWrapper.getEntity(); +// File file = new File(filePath); +// mEntity.setFileName(file.getName()); +// mEntity.setFileSize(file.length()); +// mTempUrl = mEntity.getUrl(); +// } +// +// /** +// * 设置上传路径 +// * +// * @param uploadUrl 上传路径 +// */ +// @CheckResult +// public TARGET setTempUrl(@NonNull String uploadUrl) { +// mTempUrl = uploadUrl; +// return (TARGET) this; +// } +// +// /** +// * 上传任务是否存在 +// * +// * @return {@code true}存在 +// */ +// @Override public boolean taskExists() { +// return DbEntity.checkDataExist(UploadEntity.class, "key=?", mEntity.getFilePath()); +// } +// +// /** +// * 是否在上传 +// * +// * @deprecated {@link #isRunning()} +// */ +// public boolean isUploading() { +// return isRunning(); +// } +// +// @Override public boolean isRunning() { +// UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey()); +// return task != null && task.isRunning(); +// } +// +// @Override protected boolean checkEntity() { +// boolean b = checkUrl() && checkFilePath(); +// if (b) { +// mEntity.save(); +// } +// if (mTaskWrapper.asFtp().getUrlEntity() != null && mTaskWrapper.asFtp().getUrlEntity().isFtps) { +// //if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) { +// // ALog.e(TAG, "证书路径为空"); +// // return false; +// //} +// if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) { +// ALog.e(TAG, "证书别名为空"); +// return false; +// } +// } +// return b; +// } +// +// /** +// * 检查上传文件路径是否合法 +// * +// * @return {@code true} 合法 +// */ +// private boolean checkFilePath() { +// String filePath = mEntity.getFilePath(); +// if (TextUtils.isEmpty(filePath)) { +// ALog.e(TAG, "上传失败,文件路径为null"); +// return false; +// } else if (!filePath.startsWith("/")) { +// ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法"); +// return false; +// } +// +// File file = new File(mEntity.getFilePath()); +// if (!file.exists()) { +// ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在"); +// return false; +// } +// if (file.isDirectory()) { +// ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹"); +// return false; +// } +// return true; +// } +// +// /** +// * 检查普通任务的下载地址 +// * +// * @return {@code true}地址合法 +// */ +// protected boolean checkUrl() { +// final String url = mTempUrl; +// if (TextUtils.isEmpty(url)) { +// ALog.e(TAG, "上传失败,url为null"); +// return false; +// } else if (!url.startsWith("http") && !url.startsWith("ftp")) { +// ALog.e(TAG, "上传失败,url【" + url + "】错误"); +// return false; +// } +// int index = url.indexOf("://"); +// if (index == -1) { +// ALog.e(TAG, "上传失败,url【" + url + "】不合法"); +// return false; +// } +// mEntity.setUrl(url); +// return true; +// } +//} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java index 9ba2b609..18ecf446 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java @@ -1,125 +1,102 @@ -/* - * 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.support.annotation.CheckResult; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.FtpUrlEntity; -import com.arialyy.aria.core.command.normal.NormalCmdFactory; -import com.arialyy.aria.core.common.ftp.FTPSDelegate; -import com.arialyy.aria.core.common.ftp.FtpDelegate; -import com.arialyy.aria.core.common.ftp.FtpTaskDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IFtpTarget; -import com.arialyy.aria.util.CommonUtil; -import java.net.Proxy; - -/** - * Created by Aria.Lao on 2017/7/27. - * ftp单任务上传 - */ -public class FtpUploadTarget extends BaseNormalTarget - implements IFtpTarget { - private FtpDelegate mDelegate; - - private String mAccount, mUser, mPw; - private boolean needLogin = false; - - FtpUploadTarget(String filePath, String targetName) { - this.mTargetName = targetName; - initTask(filePath); - } - - private void initTask(String filePath) { - initTarget(filePath); - mTaskWrapper.setRequestType(AbsTaskWrapper.U_FTP); - mDelegate = new FtpDelegate<>(this); - } - - /** - * 添加任务 - */ - public void add() { - if (checkEntity()) { - AriaManager.getInstance(AriaManager.APP) - .setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CREATE, - checkTaskType())) - .exe(); - } - } - - @Override protected boolean checkUrl() { - boolean b = super.checkUrl(); - if (!b) { - return false; - } - FtpTaskDelegate taskDelegate = mTaskWrapper.asFtp(); - FtpUrlEntity temp = taskDelegate.getUrlEntity(); - FtpUrlEntity newEntity = CommonUtil.getFtpUrlInfo(mTempUrl); - if (temp != null) { //处理FTPS的信息 - newEntity.isFtps = temp.isFtps; - newEntity.storePass = temp.storePass; - newEntity.keyAlias = temp.keyAlias; - newEntity.protocol = temp.protocol; - newEntity.storePath = temp.storePath; - } - taskDelegate.setUrlEntity(newEntity); - taskDelegate.getUrlEntity().account = mAccount; - taskDelegate.getUrlEntity().user = mUser; - taskDelegate.getUrlEntity().password = mPw; - taskDelegate.getUrlEntity().needLogin = needLogin; - return true; - } - - /** - * 是否是FTPS协议 - * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} - * 设置证书信息 - */ - @CheckResult - public FTPSDelegate asFtps() { - if (mTaskWrapper.asFtp().getUrlEntity() == null) { - FtpUrlEntity urlEntity = new FtpUrlEntity(); - urlEntity.isFtps = true; - mTaskWrapper.asFtp().setUrlEntity(urlEntity); - } - return new FTPSDelegate<>(this); - } - - @CheckResult - @Override public FtpUploadTarget charSet(String charSet) { - return mDelegate.charSet(charSet); - } - - @Override public FtpUploadTarget login(String userName, String password) { - needLogin = true; - mUser = userName; - mPw = password; - return this; - } - - @Override public FtpUploadTarget login(String userName, String password, String account) { - needLogin = true; - mUser = userName; - mPw = password; - mAccount = account; - return this; - } - - @Override public FtpUploadTarget setProxy(Proxy proxy) { - return mDelegate.setProxy(proxy); - } -} +/* + * 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.support.annotation.CheckResult; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.common.ftp.FTPSDelegate; +import com.arialyy.aria.core.common.ftp.FtpDelegate; +import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IFtpTarget; +import java.net.Proxy; + +/** + * Created by Aria.Lao on 2017/7/27. + * ftp单任务上传 + */ +public class FtpUploadTarget extends AbsUploadTarget + implements IFtpTarget { + private FtpDelegate mFtpDelegate; + private UNormalDelegate mNormalDelegate; + + FtpUploadTarget(String filePath, String targetName) { + mNormalDelegate = new UNormalDelegate<>(this, filePath, targetName); + initTask(); + } + + private void initTask() { + getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP); + mFtpDelegate = new FtpDelegate<>(this); + } + + /** + * 设置上传路径 + * + * @param tempUrl 上传路径 + */ + public FtpUploadTarget setUploadUrl(String tempUrl) { + setTempUrl(tempUrl); + return this; + } + + /** + * 是否是FTPS协议 + * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} + * 设置证书信息 + */ + @CheckResult + public FTPSDelegate asFtps() { + if (getTaskWrapper().asFtp().getUrlEntity() == null) { + FtpUrlEntity urlEntity = new FtpUrlEntity(); + urlEntity.isFtps = true; + getTaskWrapper().asFtp().setUrlEntity(urlEntity); + } + return new FTPSDelegate<>(this); + } + + @CheckResult + @Override public FtpUploadTarget charSet(String charSet) { + return mFtpDelegate.charSet(charSet); + } + + @Override public FtpUploadTarget login(String userName, String password) { + return mFtpDelegate.login(userName, password); + } + + @Override public FtpUploadTarget login(String userName, String password, String account) { + return mFtpDelegate.login(userName, password, account); + } + + @Override public FtpUploadTarget setProxy(Proxy proxy) { + return mFtpDelegate.setProxy(proxy); + } + + @Override protected boolean checkEntity() { + return mNormalDelegate.checkEntity(); + } + + @Override public boolean isRunning() { + return mNormalDelegate.isRunning(); + } + + @Override public boolean taskExists() { + return mNormalDelegate.taskExists(); + } + + @Override public int getTargetType() { + return FTP; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java new file mode 100644 index 00000000..359e7937 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java @@ -0,0 +1,132 @@ +/* + * 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.text.TextUtils; +import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.inf.ITargetNormal; +import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.core.queue.UploadTaskQueue; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.ALog; +import java.io.File; + +/** + * Created by Aria.Lao on 2019/4/5. + * 普通上传任务通用功能处理 + */ +public class UNormalDelegate implements ITargetNormal { + private String TAG = "UNormalDelegate"; + private UploadEntity mEntity; + private TARGET mTarget; + + UNormalDelegate(TARGET target, String filePath, String targetName) { + mTarget = target; + initTarget(filePath, targetName); + } + + @Override public void initTarget(String filePath, String targetName) { + UTaskWrapper taskWrapper = + TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath); + mEntity = taskWrapper.getEntity(); + File file = new File(filePath); + mEntity.setFileName(file.getName()); + mEntity.setFileSize(file.length()); + mTarget.setTargetName(targetName); + mTarget.setTaskWrapper(taskWrapper); + mTarget.setEntity(mEntity); + mTarget.setTempUrl(mEntity.getUrl()); + } + + @Override public TARGET updateUrl(String newUrl) { + mTarget.setTempUrl(mEntity.getUrl()); + return mTarget; + } + + @Override public AbsEntity getEntity() { + return mEntity; + } + + @Override public boolean taskExists() { + return DbEntity.checkDataExist(UploadEntity.class, "key=?", mEntity.getFilePath()); + } + + @Override public boolean isRunning() { + UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey()); + return task != null && task.isRunning(); + } + + @Override public boolean checkEntity() { + boolean b = checkUrl() && checkFilePath(); + if (b) { + mEntity.save(); + } + if (mTarget.getTaskWrapper().asFtp().getUrlEntity() != null && mTarget.getTaskWrapper() + .asFtp() + .getUrlEntity().isFtps) { + //if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) { + // ALog.e(TAG, "证书路径为空"); + // return false; + //} + if (TextUtils.isEmpty(mTarget.getTaskWrapper().asFtp().getUrlEntity().keyAlias)) { + ALog.e(TAG, "证书别名为空"); + return false; + } + } + return b; + } + + @Override public boolean checkFilePath() { + String filePath = mEntity.getFilePath(); + if (TextUtils.isEmpty(filePath)) { + ALog.e(TAG, "上传失败,文件路径为null"); + return false; + } else if (!filePath.startsWith("/")) { + ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法"); + return false; + } + + File file = new File(mEntity.getFilePath()); + if (!file.exists()) { + ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在"); + return false; + } + if (file.isDirectory()) { + ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹"); + return false; + } + return true; + } + + @Override public boolean checkUrl() { + + final String url = mTarget.getTempUrl(); + if (TextUtils.isEmpty(url)) { + ALog.e(TAG, "上传失败,url为null"); + return false; + } else if (!url.startsWith("http") && !url.startsWith("ftp")) { + ALog.e(TAG, "上传失败,url【" + url + "】错误"); + return false; + } + int index = url.indexOf("://"); + if (index == -1) { + ALog.e(TAG, "上传失败,url【" + url + "】不合法"); + return false; + } + mEntity.setUrl(url); + return true; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index 01c5f57d..c6f40c0e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -1,100 +1,125 @@ -/* - * 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.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import com.arialyy.aria.core.common.http.HttpHeaderDelegate; -import com.arialyy.aria.core.common.http.PostDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IHttpHeaderDelegate; -import java.net.Proxy; -import java.util.Map; - -/** - * Created by lyy on 2017/2/28. - * http 单文件上传 - */ -public class UploadTarget extends BaseNormalTarget - implements IHttpHeaderDelegate { - private HttpHeaderDelegate mDelegate; - - UploadTarget(String filePath, String targetName) { - this.mTargetName = targetName; - initTask(filePath); - } - - private void initTask(String filePath) { - initTarget(filePath); - - //http暂时不支持断点上传 - mTaskWrapper.setSupportBP(false); - mTaskWrapper.setRequestType(AbsTaskWrapper.U_HTTP); - mDelegate = new HttpHeaderDelegate<>(this); - } - - /** - * Post处理 - */ - public PostDelegate asPost() { - return new PostDelegate<>(this); - } - - /** - * 设置userAgent - */ - @CheckResult - public UploadTarget setUserAngent(@NonNull String userAgent) { - mTaskWrapper.asHttp().setUserAgent(userAgent); - return this; - } - - /** - * 设置服务器需要的附件key - * - * @param attachment 附件key - */ - @CheckResult - public UploadTarget setAttachment(@NonNull String attachment) { - mTaskWrapper.asHttp().setAttachment(attachment); - return this; - } - - /** - * 设置上传文件类型 - * - * @param contentType tip:multipart/form-data - */ - @CheckResult - public UploadTarget setContentType(String contentType) { - mTaskWrapper.asHttp().setContentType(contentType); - return this; - } - - @CheckResult - @Override public UploadTarget addHeader(@NonNull String key, @NonNull String value) { - return mDelegate.addHeader(key, value); - } - - @CheckResult - @Override public UploadTarget addHeaders(Map headers) { - return mDelegate.addHeaders(headers); - } - - @Override public UploadTarget setUrlProxy(Proxy proxy) { - return mDelegate.setUrlProxy(proxy); - } -} +/* + * 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.support.annotation.CheckResult; +import android.support.annotation.NonNull; +import com.arialyy.aria.core.common.http.HttpHeaderDelegate; +import com.arialyy.aria.core.common.http.PostDelegate; +import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IHttpHeaderDelegate; +import java.net.Proxy; +import java.util.Map; + +/** + * Created by lyy on 2017/2/28. + * http 单文件上传 + */ +public class UploadTarget extends AbsUploadTarget + implements IHttpHeaderDelegate { + private HttpHeaderDelegate mHeaderDelegate; + private UNormalDelegate mNormalDelegate; + + UploadTarget(String filePath, String targetName) { + mNormalDelegate = new UNormalDelegate<>(this, filePath, targetName); + initTask(); + } + + private void initTask() { + //http暂时不支持断点上传 + getTaskWrapper().setSupportBP(false); + getTaskWrapper().setRequestType(AbsTaskWrapper.U_HTTP); + mHeaderDelegate = new HttpHeaderDelegate<>(this); + } + + /** + * 设置上传路径 + * + * @param tempUrl 上传路径 + */ + public UploadTarget setUploadUrl(String tempUrl) { + setTempUrl(tempUrl); + return this; + } + + /** + * Post处理 + */ + public PostDelegate asPost() { + return new PostDelegate<>(this); + } + + /** + * 设置userAgent + */ + @CheckResult + public UploadTarget setUserAngent(@NonNull String userAgent) { + getTaskWrapper().asHttp().setUserAgent(userAgent); + return this; + } + + /** + * 设置服务器需要的附件key + * + * @param attachment 附件key + */ + @CheckResult + public UploadTarget setAttachment(@NonNull String attachment) { + getTaskWrapper().asHttp().setAttachment(attachment); + return this; + } + + /** + * 设置上传文件类型 + * + * @param contentType tip:multipart/form-data + */ + @CheckResult + public UploadTarget setContentType(String contentType) { + getTaskWrapper().asHttp().setContentType(contentType); + return this; + } + + @CheckResult + @Override public UploadTarget addHeader(@NonNull String key, @NonNull String value) { + return mHeaderDelegate.addHeader(key, value); + } + + @CheckResult + @Override public UploadTarget addHeaders(Map headers) { + return mHeaderDelegate.addHeaders(headers); + } + + @Override public UploadTarget setUrlProxy(Proxy proxy) { + return mHeaderDelegate.setUrlProxy(proxy); + } + + @Override protected boolean checkEntity() { + return mNormalDelegate.checkEntity(); + } + + @Override public boolean isRunning() { + return mNormalDelegate.isRunning(); + } + + @Override public boolean taskExists() { + return mNormalDelegate.taskExists(); + } + + @Override public int getTargetType() { + return HTTP; + } +} diff --git a/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java b/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java index e140abe4..a400e26d 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java @@ -1,244 +1,244 @@ -/* - * 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.simple.core.download; - -import android.os.Bundle; -import android.os.Environment; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; -import com.arialyy.annotations.Download; -import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTarget; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.frame.util.show.L; -import com.arialyy.simple.R; -import com.arialyy.simple.base.BaseActivity; -import com.arialyy.simple.databinding.ActivityHighestPriorityBinding; -import com.arialyy.simple.core.download.multi_download.DownloadAdapter; -import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Created by lyy on 2017/6/2. - * 最高优先级任务Demo - */ -public class HighestPriorityActivity extends BaseActivity { - private HorizontalProgressBarWithNumber mPb; - private Button mStart; - private Button mStop; - private Button mCancel; - private TextView mSize; - private TextView mSpeed; - private RecyclerView mList; - - private String mTaskName = "光明大陆"; - private static final String DOWNLOAD_URL = - "https://res5.d.cn/6f78ee3bcfdd033e64892a8553a95814cf5b4a62b12a76d9eb2a694905f0dc30fa5c7f728806a4ee0b3479e7b26a38707dac92b136add91191ac1219aadb4a3aa70bfa6d06d2d8db.apk"; - private DownloadAdapter mAdapter; - private List mData = new ArrayList<>(); - private Set mRecord = new HashSet<>(); - - @Override protected int setLayoutId() { - return R.layout.activity_highest_priority; - } - - @Override protected void init(Bundle savedInstanceState) { - super.init(savedInstanceState); - mPb = findViewById(R.id.progressBar); - mStart = findViewById(R.id.start); - mStop = findViewById(R.id.stop); - mCancel = findViewById(R.id.cancel); - mSize = findViewById(R.id.size); - mSpeed = findViewById(R.id.speed); - mList = findViewById(R.id.list); - - setTitle("最高优先级任务"); - getBinding().setTaskName("任务名:" + mTaskName + " (最高优先级任务)"); - initWidget(); - Aria.download(this).register(); - } - - private void initWidget() { - DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); - mPb.setProgress(target.getPercent()); - if (target.getTaskState() == IEntity.STATE_STOP) { - mStart.setText("恢复"); - mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); - setBtState(true); - } else if (target.isRunning()) { - setBtState(false); - } - mSize.setText(target.getConvertFileSize()); - List temp = Aria.download(this).getTaskList(); - if (temp != null && !temp.isEmpty()) { - for (DownloadEntity entity : temp) { - if (entity.getUrl().equals(DOWNLOAD_URL)) continue; - mData.add(entity); - mRecord.add(entity.getUrl()); - } - } - mAdapter = new DownloadAdapter(this, mData); - mList.setLayoutManager(new LinearLayoutManager(this)); - mList.setAdapter(mAdapter); - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_highest_priority, menu); - return super.onCreateOptionsMenu(menu); - } - - @Override public boolean onMenuItemClick(MenuItem item) { - switch (item.getItemId()) { - case R.id.add_task: - List temp = getModule(DownloadModule.class).getHighestTestList(); - for (DownloadEntity entity : temp) { - String url = entity.getUrl(); - if (mRecord.contains(url)) { - continue; - } - mAdapter.addDownloadEntity(entity); - mRecord.add(url); - } - mAdapter.notifyDataSetChanged(); - break; - case R.id.help: - String title = "最高优先级任务介绍"; - String msg = " 将任务设置为最高优先级任务,最高优先级任务有以下特点:\n" - + " 1、在下载队列中,有且只有一个最高优先级任务\n" - + " 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成\n" - + " 3、任务调度器不会暂停最高优先级任务\n" - + " 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效\n" - + " 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行\n" - + " 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务"; - showMsgDialog(title, msg); - break; - } - return true; - } - - public void onClick(View view) { - switch (view.getId()) { - case R.id.start: - String text = ((TextView) view).getText().toString(); - if (text.equals("重新开始?") || text.equals("开始")) { - Aria.download(this) - .load(DOWNLOAD_URL) - .setDownloadPath(Environment.getExternalStorageDirectory().getPath() - + "/Download/" - + mTaskName - + ".apk") - .setHighestPriority(); - } else if (text.equals("恢复")) { - Aria.download(this).load(DOWNLOAD_URL).resume(); - } - break; - case R.id.stop: - Aria.download(this).load(DOWNLOAD_URL).pause(); - break; - case R.id.cancel: - Aria.download(this).load(DOWNLOAD_URL).cancel(); - break; - } - } - - /** - * 设置start 和 stop 按钮状态 - */ - private void setBtState(boolean state) { - mStart.setEnabled(state); - mStop.setEnabled(!state); - } - - @Download.onPre public void onPre(DownloadTask task) { - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskPre public void onTaskPre(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - mSize.setText(task.getConvertFileSize()); - } - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskStart public void onTaskStart(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - setBtState(false); - } - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskResume public void onTaskResume(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - setBtState(false); - } - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskStop public void onTaskStop(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - setBtState(true); - mStart.setText("恢复"); - mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); - } - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskCancel public void onTaskCancel(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - setBtState(true); - mStart.setText("开始"); - mPb.setProgress(0); - } - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskFail public void onTaskFail(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - setBtState(true); - } else { - L.d(TAG, "download fail【" + task.getKey() + "】"); - } - } - - @Download.onTaskComplete public void onTaskComplete(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - setBtState(true); - mStart.setText("重新开始"); - mStart.setTextColor(getResources().getColor(android.R.color.holo_green_light)); - mPb.setProgress(100); - } - mAdapter.updateState(task.getDownloadEntity()); - } - - @Download.onTaskRunning public void onTaskRunning(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - mPb.setProgress(task.getPercent()); - mSpeed.setText(task.getConvertSpeed()); - } - mAdapter.setProgress(task.getDownloadEntity()); - } -} +/* + * 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.simple.core.download; + +import android.os.Bundle; +import android.os.Environment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; +import com.arialyy.annotations.Download; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadTarget; +import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.frame.util.show.L; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityHighestPriorityBinding; +import com.arialyy.simple.core.download.multi_download.DownloadAdapter; +import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Created by lyy on 2017/6/2. + * 最高优先级任务Demo + */ +public class HighestPriorityActivity extends BaseActivity { + private HorizontalProgressBarWithNumber mPb; + private Button mStart; + private Button mStop; + private Button mCancel; + private TextView mSize; + private TextView mSpeed; + private RecyclerView mList; + + private String mTaskName = "光明大陆"; + private static final String DOWNLOAD_URL = + "https://res5.d.cn/6f78ee3bcfdd033e64892a8553a95814cf5b4a62b12a76d9eb2a694905f0dc30fa5c7f728806a4ee0b3479e7b26a38707dac92b136add91191ac1219aadb4a3aa70bfa6d06d2d8db.apk"; + private DownloadAdapter mAdapter; + private List mData = new ArrayList<>(); + private Set mRecord = new HashSet<>(); + + @Override protected int setLayoutId() { + return R.layout.activity_highest_priority; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + mPb = findViewById(R.id.progressBar); + mStart = findViewById(R.id.start); + mStop = findViewById(R.id.stop); + mCancel = findViewById(R.id.cancel); + mSize = findViewById(R.id.size); + mSpeed = findViewById(R.id.speed); + mList = findViewById(R.id.list); + + setTitle("最高优先级任务"); + getBinding().setTaskName("任务名:" + mTaskName + " (最高优先级任务)"); + initWidget(); + Aria.download(this).register(); + } + + private void initWidget() { + DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); + mPb.setProgress(target.getPercent()); + if (target.getTaskState() == IEntity.STATE_STOP) { + mStart.setText("恢复"); + mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); + setBtState(true); + } else if (target.isRunning()) { + setBtState(false); + } + mSize.setText(target.getConvertFileSize()); + List temp = Aria.download(this).getTaskList(); + if (temp != null && !temp.isEmpty()) { + for (DownloadEntity entity : temp) { + if (entity.getUrl().equals(DOWNLOAD_URL)) continue; + mData.add(entity); + mRecord.add(entity.getUrl()); + } + } + mAdapter = new DownloadAdapter(this, mData); + mList.setLayoutManager(new LinearLayoutManager(this)); + mList.setAdapter(mAdapter); + } + + @Override public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_highest_priority, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override public boolean onMenuItemClick(MenuItem item) { + switch (item.getItemId()) { + case R.id.add_task: + List temp = getModule(DownloadModule.class).getHighestTestList(); + for (DownloadEntity entity : temp) { + String url = entity.getUrl(); + if (mRecord.contains(url)) { + continue; + } + mAdapter.addDownloadEntity(entity); + mRecord.add(url); + } + mAdapter.notifyDataSetChanged(); + break; + case R.id.help: + String title = "最高优先级任务介绍"; + String msg = " 将任务设置为最高优先级任务,最高优先级任务有以下特点:\n" + + " 1、在下载队列中,有且只有一个最高优先级任务\n" + + " 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成\n" + + " 3、任务调度器不会暂停最高优先级任务\n" + + " 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效\n" + + " 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行\n" + + " 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务"; + showMsgDialog(title, msg); + break; + } + return true; + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + String text = ((TextView) view).getText().toString(); + if (text.equals("重新开始?") || text.equals("开始")) { + Aria.download(this) + .load(DOWNLOAD_URL) + .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + + "/Download/" + + mTaskName + + ".apk") + .setHighestPriority(); + } else if (text.equals("恢复")) { + Aria.download(this).load(DOWNLOAD_URL).resume(); + } + break; + case R.id.stop: + Aria.download(this).load(DOWNLOAD_URL).pause(); + break; + case R.id.cancel: + Aria.download(this).load(DOWNLOAD_URL).cancel(); + break; + } + } + + /** + * 设置start 和 stop 按钮状态 + */ + private void setBtState(boolean state) { + mStart.setEnabled(state); + mStop.setEnabled(!state); + } + + @Download.onPre public void onPre(DownloadTask task) { + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskPre public void onTaskPre(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + mSize.setText(task.getConvertFileSize()); + } + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskStart public void onTaskStart(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(false); + } + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskResume public void onTaskResume(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(false); + } + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskStop public void onTaskStop(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + mStart.setText("恢复"); + mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); + } + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskCancel public void onTaskCancel(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + mStart.setText("开始"); + mPb.setProgress(0); + } + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskFail public void onTaskFail(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + } else { + L.d(TAG, "download fail【" + task.getKey() + "】"); + } + } + + @Download.onTaskComplete public void onTaskComplete(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + mStart.setText("重新开始"); + mStart.setTextColor(getResources().getColor(android.R.color.holo_green_light)); + mPb.setProgress(100); + } + mAdapter.updateState(task.getDownloadEntity()); + } + + @Download.onTaskRunning public void onTaskRunning(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + mPb.setProgress(task.getPercent()); + mSpeed.setText(task.getConvertSpeed()); + } + mAdapter.setProgress(task.getDownloadEntity()); + } +} diff --git a/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java b/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java index 242a8106..201d9844 100644 --- a/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java @@ -68,7 +68,7 @@ public class HttpUploadActivity extends BaseActivity { Aria.upload(HttpUploadActivity.this).load(FILE_PATH) .setUploadUrl( "http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}") - //.setUploadUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file") + //.setTempUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file") //.addHeader("iplanetdirectorypro", "11a09102fb934ad0bc206f9c611d7933") .asPost() .start();