parent
fdb607e10f
commit
27fb964fef
@ -0,0 +1,192 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.common; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.common.controller.INormalFeature; |
||||||
|
import com.arialyy.aria.core.common.controller.NormalController; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsNormalEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTarget; |
||||||
|
import com.arialyy.aria.core.inf.IEntity; |
||||||
|
import com.arialyy.aria.core.manager.TaskWrapperManager; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import com.arialyy.aria.util.RecordUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理任务停止、恢复、删除等功能 |
||||||
|
*/ |
||||||
|
public abstract class AbsNormalTarget<TARGET extends AbsNormalTarget> extends AbsTarget<TARGET> |
||||||
|
implements INormalFeature { |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务是否在执行 |
||||||
|
* |
||||||
|
* @return {@code true} 任务正在执行 |
||||||
|
*/ |
||||||
|
public abstract boolean isRunning(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务是否存在 |
||||||
|
* |
||||||
|
* @return {@code true} 任务存在 |
||||||
|
*/ |
||||||
|
public abstract boolean taskExists(); |
||||||
|
|
||||||
|
private NormalController mNormalController; |
||||||
|
|
||||||
|
private synchronized NormalController getController() { |
||||||
|
if (mNormalController == null) { |
||||||
|
mNormalController = new NormalController(getTaskWrapper()); |
||||||
|
} |
||||||
|
return mNormalController; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除记录,如果任务正在执行,则会删除正在下载的任务 |
||||||
|
*/ |
||||||
|
public void removeRecord() { |
||||||
|
if (isRunning()) { |
||||||
|
ALog.d("AbsTarget", "任务正在下载,即将删除任务"); |
||||||
|
cancel(); |
||||||
|
} else { |
||||||
|
if (getEntity() instanceof AbsNormalEntity) { |
||||||
|
RecordUtil.delTaskRecord((AbsNormalEntity) getEntity(), getTaskWrapper().isRemoveFile()); |
||||||
|
} else if (getEntity() instanceof DownloadGroupEntity) { |
||||||
|
RecordUtil.delGroupTaskRecord(((DownloadGroupEntity) getEntity()), |
||||||
|
getTaskWrapper().isRemoveFile(), |
||||||
|
true); |
||||||
|
} |
||||||
|
TaskWrapperManager.getInstance().removeTaskWrapper(getTaskWrapper()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务进度,如果任务存在,则返回当前进度 |
||||||
|
* |
||||||
|
* @return 该任务进度 |
||||||
|
*/ |
||||||
|
public long getCurrentProgress() { |
||||||
|
return getEntity() == null ? -1 : getEntity().getCurrentProgress(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务文件大小 |
||||||
|
* |
||||||
|
* @return 文件大小 |
||||||
|
*/ |
||||||
|
public long getFileSize() { |
||||||
|
return getEntity() == null ? 0 : getEntity().getFileSize(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取单位转换后的文件大小 |
||||||
|
* |
||||||
|
* @return 文件大小{@code xxx mb} |
||||||
|
*/ |
||||||
|
public String getConvertFileSize() { |
||||||
|
return getEntity() == null ? "0b" : CommonUtil.formatFileSize(getEntity().getFileSize()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取存放的扩展字段 |
||||||
|
* 设置扩展字段{@link #setExtendField(String)} |
||||||
|
*/ |
||||||
|
public String getExtendField() { |
||||||
|
return getEntity().getStr(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务状态 |
||||||
|
* |
||||||
|
* @return {@link IEntity} |
||||||
|
*/ |
||||||
|
public int getTaskState() { |
||||||
|
return getEntity().getState(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务进度百分比 |
||||||
|
* |
||||||
|
* @return 返回任务进度 |
||||||
|
*/ |
||||||
|
public int getPercent() { |
||||||
|
if (getEntity() == null) { |
||||||
|
ALog.e("AbsTarget", "下载管理器中没有该任务"); |
||||||
|
return 0; |
||||||
|
} |
||||||
|
if (getEntity().getFileSize() != 0) { |
||||||
|
return (int) (getEntity().getCurrentProgress() * 100 / getEntity().getFileSize()); |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 停止任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void stop() { |
||||||
|
getController().stop(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 恢复任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void resume() { |
||||||
|
getController().resume(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void cancel() { |
||||||
|
getController().cancel(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务重试 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void reTry() { |
||||||
|
getController().reTry(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除任务 |
||||||
|
* |
||||||
|
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 |
||||||
|
* {@code false}如果任务已经完成,只删除任务数据库记录, |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void cancel(boolean removeFile) { |
||||||
|
getController().cancel(removeFile); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 重新下载 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void reStart() { |
||||||
|
getController().reStart(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void save() { |
||||||
|
getController().save(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,70 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.common; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.common.controller.IStartFeature; |
||||||
|
import com.arialyy.aria.core.common.controller.StartController; |
||||||
|
import com.arialyy.aria.core.inf.AbsTarget; |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理第一次下载 |
||||||
|
*/ |
||||||
|
public abstract class AbsStartTarget<TARGET extends AbsStartTarget> extends AbsTarget<TARGET> |
||||||
|
implements IStartFeature { |
||||||
|
|
||||||
|
private StartController mStartController; |
||||||
|
|
||||||
|
private synchronized StartController getController() { |
||||||
|
if (mStartController == null) { |
||||||
|
mStartController = new StartController(getTaskWrapper()); |
||||||
|
} |
||||||
|
return mStartController; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加任务 |
||||||
|
* |
||||||
|
* @return 正常添加,返回任务id,否则返回-1 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public long add() { |
||||||
|
return getController().add(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 开始任务 |
||||||
|
* |
||||||
|
* @return 正常启动,返回任务id,否则返回-1 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public long start() { |
||||||
|
return getController().start(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 将任务设置为最高优先级任务,最高优先级任务有以下特点: |
||||||
|
* 1、在下载队列中,有且只有一个最高优先级任务 |
||||||
|
* 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 |
||||||
|
* 3、任务调度器不会暂停最高优先级任务 |
||||||
|
* 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 |
||||||
|
* 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 |
||||||
|
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public long setHighestPriority() { |
||||||
|
return getController().setHighestPriority(); |
||||||
|
} |
||||||
|
} |
@ -1,27 +1,23 @@ |
|||||||
/* |
/* |
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
package com.arialyy.aria.core.upload; |
package com.arialyy.aria.core.common; |
||||||
|
|
||||||
import com.arialyy.aria.core.inf.AbsTarget; |
public interface Suggest { |
||||||
|
|
||||||
/** |
String TASK_CONTROLLER = "after use #add()、#start()、#stop()、#cancel()、#resume()、#save()?"; |
||||||
* Created by AriaL on 2017/6/29. |
|
||||||
* 普通上传任务接收器 |
String TO_CONTROLLER = "after use #controller()?"; |
||||||
*/ |
} |
||||||
abstract class AbsUploadTarget<TARGET extends AbsUploadTarget> extends AbsTarget<TARGET> { |
|
||||||
|
|
||||||
|
|
||||||
} |
|
@ -0,0 +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.common.controller; |
||||||
|
|
||||||
|
import android.os.Handler; |
||||||
|
import android.os.Looper; |
||||||
|
import com.arialyy.aria.core.download.CheckDEntityUtil; |
||||||
|
import com.arialyy.aria.core.download.CheckDGEntityUtil; |
||||||
|
import com.arialyy.aria.core.download.CheckFtpDirEntityUtil; |
||||||
|
import com.arialyy.aria.core.download.DGTaskWrapper; |
||||||
|
import com.arialyy.aria.core.download.DTaskWrapper; |
||||||
|
import com.arialyy.aria.core.inf.AbsEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
import com.arialyy.aria.core.inf.ITask; |
||||||
|
import com.arialyy.aria.core.scheduler.DownloadSchedulers; |
||||||
|
import com.arialyy.aria.core.scheduler.ISchedulers; |
||||||
|
import com.arialyy.aria.core.scheduler.UploadSchedulers; |
||||||
|
import com.arialyy.aria.core.upload.CheckUEntityUtil; |
||||||
|
import com.arialyy.aria.core.upload.UTaskWrapper; |
||||||
|
import java.lang.reflect.Constructor; |
||||||
|
import java.lang.reflect.InvocationTargetException; |
||||||
|
|
||||||
|
/** |
||||||
|
* 功能控制器 |
||||||
|
*/ |
||||||
|
public abstract class FeatureController { |
||||||
|
|
||||||
|
private AbsTaskWrapper mTaskWrapper; |
||||||
|
|
||||||
|
FeatureController(AbsTaskWrapper wrapper) { |
||||||
|
mTaskWrapper = wrapper; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 使用对应等控制器,注意: |
||||||
|
* 1、对于不存在的任务(第一次下载),只能使用{@link ControllerType#START_CONTROLLER} |
||||||
|
* 2、对于已存在的任务,只能使用{@link ControllerType#NORMAL_CONTROLLER} |
||||||
|
* |
||||||
|
* @param clazz {@link ControllerType#START_CONTROLLER}、{@link ControllerType#NORMAL_CONTROLLER} |
||||||
|
*/ |
||||||
|
public static <T extends FeatureController> T newInstance(@ControllerType Class<T> clazz, |
||||||
|
AbsTaskWrapper wrapper) { |
||||||
|
if (wrapper.getEntity().getId() == -1 && clazz != ControllerType.START_CONTROLLER) { |
||||||
|
throw new IllegalArgumentException("对于不存在的任务(第一次下载),只能使用\"ControllerType.START_CONTROLLER\""); |
||||||
|
} |
||||||
|
if (wrapper.getEntity().getId() != -1 && clazz != ControllerType.NORMAL_CONTROLLER) { |
||||||
|
throw new IllegalArgumentException("对于已存在的任务,只能使用\" ControllerType.NORMAL_CONTROLLER\""); |
||||||
|
} |
||||||
|
|
||||||
|
Class[] paramTypes = { AbsTaskWrapper.class }; |
||||||
|
Object[] params = { wrapper }; |
||||||
|
try { |
||||||
|
Constructor<T> con = clazz.getConstructor(paramTypes); |
||||||
|
return con.newInstance(params); |
||||||
|
} catch (NoSuchMethodException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} catch (InvocationTargetException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} catch (InstantiationException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} catch (IllegalAccessException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
protected AbsTaskWrapper getTaskWrapper() { |
||||||
|
return mTaskWrapper; |
||||||
|
} |
||||||
|
|
||||||
|
protected AbsEntity getEntity() { |
||||||
|
return mTaskWrapper.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
int checkTaskType() { |
||||||
|
int taskType = 0; |
||||||
|
if (mTaskWrapper instanceof DTaskWrapper) { |
||||||
|
taskType = ITask.DOWNLOAD; |
||||||
|
} else if (mTaskWrapper instanceof DGTaskWrapper) { |
||||||
|
taskType = ITask.DOWNLOAD_GROUP; |
||||||
|
} else if (mTaskWrapper instanceof UTaskWrapper) { |
||||||
|
taskType = ITask.UPLOAD; |
||||||
|
} |
||||||
|
return taskType; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果检查实体失败,将错误回调 |
||||||
|
*/ |
||||||
|
boolean checkConfig() { |
||||||
|
boolean b = checkEntity(); |
||||||
|
ISchedulers schedulers = getScheduler(); |
||||||
|
if (!b && schedulers != null) { |
||||||
|
new Handler(Looper.getMainLooper(), schedulers).obtainMessage(ISchedulers.CHECK_FAIL, |
||||||
|
checkTaskType(), -1, null).sendToTarget(); |
||||||
|
} |
||||||
|
|
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
private ISchedulers getScheduler() { |
||||||
|
if (mTaskWrapper instanceof DTaskWrapper) { |
||||||
|
return DownloadSchedulers.getInstance(); |
||||||
|
} |
||||||
|
if (mTaskWrapper instanceof UTaskWrapper) { |
||||||
|
return UploadSchedulers.getInstance(); |
||||||
|
} |
||||||
|
if (mTaskWrapper instanceof DGTaskWrapper) { |
||||||
|
return DownloadSchedulers.getInstance(); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean checkEntity() { |
||||||
|
if (mTaskWrapper instanceof DTaskWrapper) { |
||||||
|
return CheckDEntityUtil.newInstance((DTaskWrapper) mTaskWrapper).checkEntity(); |
||||||
|
} |
||||||
|
if (mTaskWrapper instanceof DGTaskWrapper) { |
||||||
|
if (mTaskWrapper.getRequestType() == AbsTaskWrapper.D_FTP_DIR) { |
||||||
|
return CheckFtpDirEntityUtil.newInstance((DGTaskWrapper) mTaskWrapper).checkEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
if (mTaskWrapper.getRequestType() == AbsTaskWrapper.DG_HTTP) { |
||||||
|
return CheckDGEntityUtil.newInstance((DGTaskWrapper) mTaskWrapper).checkEntity(); |
||||||
|
} |
||||||
|
} |
||||||
|
if (mTaskWrapper instanceof UTaskWrapper) { |
||||||
|
return CheckUEntityUtil.newInstance((UTaskWrapper) mTaskWrapper).checkEntity(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,122 @@ |
|||||||
|
/* |
||||||
|
* 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.controller; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.command.CancelCmd; |
||||||
|
import com.arialyy.aria.core.command.NormalCmdFactory; |
||||||
|
import com.arialyy.aria.core.event.EventMsgUtil; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* 启动控制器 |
||||||
|
*/ |
||||||
|
public final class NormalController extends FeatureController implements INormalFeature { |
||||||
|
private String TAG = "NormalController"; |
||||||
|
|
||||||
|
public NormalController(AbsTaskWrapper wrapper) { |
||||||
|
super(wrapper); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 停止任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void stop() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_STOP, |
||||||
|
checkTaskType())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 恢复任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void resume() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, |
||||||
|
checkTaskType())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除任务 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void cancel() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL, |
||||||
|
checkTaskType())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务重试 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void reTry() { |
||||||
|
if (checkConfig()) { |
||||||
|
int taskType = checkTaskType(); |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_STOP, taskType)); |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post( |
||||||
|
CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, taskType)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除任务 |
||||||
|
* |
||||||
|
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 |
||||||
|
* {@code false}如果任务已经完成,只删除任务数据库记录, |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void cancel(boolean removeFile) { |
||||||
|
if (checkConfig()) { |
||||||
|
CancelCmd cancelCmd = |
||||||
|
(CancelCmd) CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL, |
||||||
|
checkTaskType()); |
||||||
|
cancelCmd.removeFile = removeFile; |
||||||
|
EventMsgUtil.getDefault().post(cancelCmd); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 重新下载 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void reStart() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_RESTART, |
||||||
|
checkTaskType())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void save() { |
||||||
|
if (!checkConfig()) { |
||||||
|
ALog.e(TAG, "保存修改失败"); |
||||||
|
} else { |
||||||
|
ALog.i(TAG, "保存成功"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,72 @@ |
|||||||
|
/* |
||||||
|
* 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.controller; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.command.NormalCmdFactory; |
||||||
|
import com.arialyy.aria.core.event.EventMsgUtil; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* 启动控制器 |
||||||
|
*/ |
||||||
|
public final class StartController extends FeatureController implements IStartFeature { |
||||||
|
|
||||||
|
public StartController(AbsTaskWrapper wrapper) { |
||||||
|
super(wrapper); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加任务 |
||||||
|
* |
||||||
|
* @return 正常添加,返回任务id,否则返回-1 |
||||||
|
*/ |
||||||
|
public long add() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CREATE, |
||||||
|
checkTaskType())); |
||||||
|
return getEntity().getId(); |
||||||
|
} |
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 开始任务 |
||||||
|
* |
||||||
|
* @return 正常启动,返回任务id,否则返回-1 |
||||||
|
*/ |
||||||
|
public long start() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, |
||||||
|
checkTaskType())); |
||||||
|
return getEntity().getId(); |
||||||
|
} |
||||||
|
|
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public long setHighestPriority() { |
||||||
|
if (checkConfig()) { |
||||||
|
EventMsgUtil.getDefault() |
||||||
|
.post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_HIGHEST_PRIORITY, |
||||||
|
checkTaskType())); |
||||||
|
return getEntity().getId(); |
||||||
|
} |
||||||
|
return -1; |
||||||
|
} |
||||||
|
} |
@ -1,30 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.common.http; |
|
||||||
|
|
||||||
import com.arialyy.aria.core.common.RequestEnum; |
|
||||||
import com.arialyy.aria.core.inf.AbsTarget; |
|
||||||
|
|
||||||
/** |
|
||||||
* post处理委托类 |
|
||||||
*/ |
|
||||||
public class PostDelegate<TARGET extends AbsTarget> extends HttpDelegate<TARGET> { |
|
||||||
|
|
||||||
public PostDelegate(TARGET target) { |
|
||||||
super(target); |
|
||||||
mTarget.getTaskWrapper().asHttp().setRequestEnum(RequestEnum.POST); |
|
||||||
} |
|
||||||
} |
|
@ -1,68 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.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.inf.AbsTaskWrapper; |
|
||||||
import com.arialyy.aria.core.manager.SubTaskManager; |
|
||||||
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; |
|
||||||
import com.arialyy.aria.orm.DbEntity; |
|
||||||
import com.arialyy.aria.util.ALog; |
|
||||||
import java.io.File; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by lyy on 2017/7/26. |
|
||||||
*/ |
|
||||||
abstract class AbsDGTarget<TARGET extends AbsDGTarget> extends AbsTarget<TARGET> { |
|
||||||
|
|
||||||
private SubTaskManager mSubTaskManager; |
|
||||||
|
|
||||||
/** |
|
||||||
* 获取子任务管理器 |
|
||||||
* |
|
||||||
* @return 子任务管理器 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public SubTaskManager getSubTaskManager() { |
|
||||||
if (mSubTaskManager == null) { |
|
||||||
mSubTaskManager = new SubTaskManager(getTargetName(), getTaskWrapper()); |
|
||||||
} |
|
||||||
return mSubTaskManager; |
|
||||||
} |
|
||||||
|
|
||||||
@Override public DownloadGroupEntity getEntity() { |
|
||||||
return (DownloadGroupEntity) super.getEntity(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public DGTaskWrapper getTaskWrapper() { |
|
||||||
return (DGTaskWrapper) super.getTaskWrapper(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置任务组别名 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public TARGET setGroupAlias(String alias) { |
|
||||||
if (TextUtils.isEmpty(alias)) return (TARGET) this; |
|
||||||
getEntity().setAlias(alias); |
|
||||||
return (TARGET) this; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,183 @@ |
|||||||
|
/* |
||||||
|
* 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.common.RecordHandler; |
||||||
|
import com.arialyy.aria.core.inf.ITargetHandler; |
||||||
|
import com.arialyy.aria.core.inf.ITaskWrapper; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import com.arialyy.aria.util.CheckUtil; |
||||||
|
import com.arialyy.aria.util.RecordUtil; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查下载任务实体 |
||||||
|
*/ |
||||||
|
public class CheckDEntityUtil { |
||||||
|
private final String TAG = "CheckDLoadEntity"; |
||||||
|
private DTaskWrapper mWrapper; |
||||||
|
private DownloadEntity mEntity; |
||||||
|
|
||||||
|
public static CheckDEntityUtil newInstance(DTaskWrapper wrapper) { |
||||||
|
return new CheckDEntityUtil(wrapper); |
||||||
|
} |
||||||
|
|
||||||
|
private CheckDEntityUtil(DTaskWrapper wrapper) { |
||||||
|
mWrapper = wrapper; |
||||||
|
mEntity = mWrapper.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean checkEntity() { |
||||||
|
boolean b = checkFtps() && checkUrl() && checkFilePath(); |
||||||
|
if (b) { |
||||||
|
mEntity.save(); |
||||||
|
} |
||||||
|
if (mWrapper.getRequestType() == ITaskWrapper.M3U8_VOD |
||||||
|
|| mWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { |
||||||
|
handleM3U8(); |
||||||
|
} |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
private void handleM3U8() { |
||||||
|
File file = new File(mWrapper.getmTempFilePath()); |
||||||
|
// 缓存文件夹格式:问文件夹/.文件名_码率
|
||||||
|
String cacheDir = String.format("%s/.%s_%s", file.getParent(), file.getName(), |
||||||
|
mWrapper.asM3U8().getBandWidth()); |
||||||
|
mWrapper.asM3U8().setCacheDir(cacheDir); |
||||||
|
mEntity.getM3U8Entity().setCacheDir(cacheDir); |
||||||
|
if (mWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { |
||||||
|
if (mEntity.getFileSize() == 0) { |
||||||
|
ALog.w(TAG, |
||||||
|
"由于m3u8协议的特殊性质,无法有效快速获取到正确到文件长度,如果你需要显示文件中长度,你需要自行设置文件长度:.asM3U8().asVod().setFileSize(xxx)"); |
||||||
|
} |
||||||
|
} else if (mWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { |
||||||
|
if (file.exists()) { |
||||||
|
ALog.w(TAG, "对于直播来说,每次下载都是一个新文件,所以你需要设置新都文件路径,否则Aria框架将会覆盖已下载的文件"); |
||||||
|
file.delete(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (mWrapper.asM3U8().getBandWidthUrlConverter() != null |
||||||
|
&& mWrapper.asM3U8().getBandWidth() == 0) { |
||||||
|
ALog.w(TAG, "你已经设置了码率url转换器,但是没有设置码率,Aria框架将采用第一个获取到的码率"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private boolean checkFilePath() { |
||||||
|
String filePath = mWrapper.getmTempFilePath(); |
||||||
|
if (TextUtils.isEmpty(filePath)) { |
||||||
|
ALog.e(TAG, "下载失败,文件保存路径为null"); |
||||||
|
return false; |
||||||
|
} else if (!filePath.startsWith("/")) { |
||||||
|
ALog.e(TAG, String.format("下载失败,文件保存路径【%s】错误", filePath)); |
||||||
|
return false; |
||||||
|
} |
||||||
|
File file = new File(filePath); |
||||||
|
if (file.isDirectory()) { |
||||||
|
if (mWrapper.getRequestType() == ITargetHandler.D_HTTP |
||||||
|
|| mWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { |
||||||
|
ALog.e(TAG, |
||||||
|
String.format("下载失败,保存路径【%s】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip", filePath)); |
||||||
|
return false; |
||||||
|
} else if (mWrapper.getRequestType() == ITargetHandler.D_FTP) { |
||||||
|
filePath += mEntity.getFileName(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
// http文件名设置
|
||||||
|
if (TextUtils.isEmpty(mEntity.getFileName())) { |
||||||
|
mEntity.setFileName(file.getName()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return checkPathConflicts(filePath); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查路径冲突 |
||||||
|
*/ |
||||||
|
private boolean checkPathConflicts(String filePath) { |
||||||
|
//设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
|
||||||
|
if (!filePath.equals(mEntity.getFilePath())) { |
||||||
|
// 检查路径冲突
|
||||||
|
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { |
||||||
|
if (!mWrapper.isForceDownload()) { |
||||||
|
ALog.e(TAG, String.format("下载失败,保存路径【%s】已经被其它任务占用,请设置其它保存路径", filePath)); |
||||||
|
return false; |
||||||
|
} else { |
||||||
|
ALog.w(TAG, String.format("保存路径【%s】已经被其它任务占用,当前任务将覆盖该路径的文件", filePath)); |
||||||
|
RecordUtil.delTaskRecord(filePath, RecordHandler.TYPE_DOWNLOAD); |
||||||
|
} |
||||||
|
} |
||||||
|
File oldFile = new File(mEntity.getFilePath()); |
||||||
|
File newFile = new File(filePath); |
||||||
|
mEntity.setFilePath(filePath); |
||||||
|
mEntity.setFileName(newFile.getName()); |
||||||
|
|
||||||
|
// 如过使用Content-Disposition中的文件名,将不会执行重命名工作
|
||||||
|
if (mWrapper.asHttp().isUseServerFileName() |
||||||
|
|| mWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
if (oldFile.exists()) { |
||||||
|
// 处理普通任务的重命名
|
||||||
|
RecordUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); |
||||||
|
ALog.i(TAG, String.format("将任务重命名为:%s", newFile.getName())); |
||||||
|
} else if (RecordUtil.blockTaskExists(oldFile.getPath())) { |
||||||
|
// 处理分块任务的重命名
|
||||||
|
RecordUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); |
||||||
|
ALog.i(TAG, String.format("将分块任务重命名为:%s", newFile.getName())); |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean checkUrl() { |
||||||
|
final String url = mEntity.getUrl(); |
||||||
|
if (TextUtils.isEmpty(url)) { |
||||||
|
ALog.e(TAG, "下载失败,url为null"); |
||||||
|
return false; |
||||||
|
} else if (!CheckUtil.checkUrlNotThrow(url)) { |
||||||
|
ALog.e(TAG, "下载失败,url【" + url + "】错误"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
int index = url.indexOf("://"); |
||||||
|
if (index == -1) { |
||||||
|
ALog.e(TAG, "下载失败,url【" + url + "】不合法"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (!TextUtils.isEmpty(mWrapper.getmTempUrl())) { |
||||||
|
mEntity.setUrl(mWrapper.getmTempUrl()); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean checkFtps() { |
||||||
|
if (mWrapper.asFtp().getUrlEntity().isFtps) { |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { |
||||||
|
ALog.e(TAG, "证书路径为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { |
||||||
|
ALog.e(TAG, "证书别名为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,254 @@ |
|||||||
|
/* |
||||||
|
* 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.common.RequestEnum; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import com.arialyy.aria.util.RecordUtil; |
||||||
|
import java.io.File; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
public class CheckDGEntityUtil { |
||||||
|
|
||||||
|
private final String TAG = "CheckDGEntityUtil"; |
||||||
|
private DGTaskWrapper mWrapper; |
||||||
|
private DownloadGroupEntity mEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* 是否需要修改路径 |
||||||
|
*/ |
||||||
|
private boolean needModifyPath = false; |
||||||
|
|
||||||
|
public static CheckDGEntityUtil newInstance(DGTaskWrapper wrapper) { |
||||||
|
return new CheckDGEntityUtil(wrapper); |
||||||
|
} |
||||||
|
|
||||||
|
private CheckDGEntityUtil(DGTaskWrapper wrapper) { |
||||||
|
mWrapper = wrapper; |
||||||
|
mEntity = mWrapper.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查并设置文件夹路径 |
||||||
|
* |
||||||
|
* @return {@code true} 合法 |
||||||
|
*/ |
||||||
|
private boolean checkDirPath() { |
||||||
|
if (TextUtils.isEmpty(mWrapper.getDirPathTemp())) { |
||||||
|
ALog.e(TAG, "文件夹路径不能为null"); |
||||||
|
return false; |
||||||
|
} else if (!mWrapper.getDirPathTemp().startsWith("/")) { |
||||||
|
ALog.e(TAG, String.format("文件夹路径【%s】错误", mWrapper.getDirPathTemp())); |
||||||
|
return false; |
||||||
|
} |
||||||
|
File file = new File(mWrapper.getDirPathTemp()); |
||||||
|
if (file.isFile()) { |
||||||
|
ALog.e(TAG, String.format("路径【%s】是文件,请设置文件夹路径", mWrapper.getDirPathTemp())); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if ((mEntity.getDirPath() == null || !mEntity.getDirPath().equals(mWrapper.getDirPathTemp())) |
||||||
|
&& DbEntity.checkDataExist(DownloadGroupEntity.class, "dirPath=?", |
||||||
|
mWrapper.getDirPathTemp())) { |
||||||
|
ALog.e(TAG, String.format("文件夹路径【%s】已被其它任务占用,请重新设置文件夹路径", mWrapper.getDirPathTemp())); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (TextUtils.isEmpty(mEntity.getDirPath()) || !mEntity.getDirPath() |
||||||
|
.equals(mWrapper.getDirPathTemp())) { |
||||||
|
if (!file.exists()) { |
||||||
|
file.mkdirs(); |
||||||
|
} |
||||||
|
needModifyPath = true; |
||||||
|
mEntity.setDirPath(mWrapper.getDirPathTemp()); |
||||||
|
ALog.i(TAG, String.format("文件夹路径改变,将更新文件夹路径为:%s", mWrapper.getDirPathTemp())); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 改变任务组文件夹路径,修改文件夹路径会将子任务所有路径更换 |
||||||
|
* |
||||||
|
* @param newDirPath 新的文件夹路径 |
||||||
|
*/ |
||||||
|
private void reChangeDirPath(String newDirPath) { |
||||||
|
ALog.d(TAG, String.format("修改新路径为:%s", newDirPath)); |
||||||
|
List<DTaskWrapper> subTasks = mWrapper.getSubTaskWrapper(); |
||||||
|
if (subTasks != null && !subTasks.isEmpty()) { |
||||||
|
for (DTaskWrapper dte : subTasks) { |
||||||
|
DownloadEntity de = dte.getEntity(); |
||||||
|
String oldPath = de.getFilePath(); |
||||||
|
String newPath = newDirPath + "/" + de.getFileName(); |
||||||
|
File file = new File(oldPath); |
||||||
|
if (file.exists()) { |
||||||
|
file.renameTo(new File(newPath)); |
||||||
|
} |
||||||
|
de.setFilePath(newPath); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public boolean checkEntity() { |
||||||
|
if (!checkDirPath()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (!checkSubName()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (!checkUrls()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (!mWrapper.isUnknownSize() && mEntity.getFileSize() == 0) { |
||||||
|
ALog.e(TAG, "组合任务必须设置文件文件大小,默认需要强制设置文件大小。如果无法获取到总长度,请调用#unknownSize()来标志该组合任务"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (mWrapper.asHttp().getRequestEnum() == RequestEnum.POST) { |
||||||
|
for (DTaskWrapper subTask : mWrapper.getSubTaskWrapper()) { |
||||||
|
subTask.asHttp().setRequestEnum(RequestEnum.POST); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (needModifyPath) { |
||||||
|
reChangeDirPath(mWrapper.getDirPathTemp()); |
||||||
|
} |
||||||
|
|
||||||
|
if (!mWrapper.getSubNameTemp().isEmpty()) { |
||||||
|
updateSingleSubFileName(); |
||||||
|
} |
||||||
|
saveEntity(); |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private void saveEntity() { |
||||||
|
mEntity.save(); |
||||||
|
DbEntity.saveAll(mEntity.getSubEntities()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新所有改动的子任务文件名 |
||||||
|
*/ |
||||||
|
private void updateSingleSubFileName() { |
||||||
|
List<DTaskWrapper> entities = mWrapper.getSubTaskWrapper(); |
||||||
|
int i = 0; |
||||||
|
for (DTaskWrapper taskWrapper : entities) { |
||||||
|
if (i < mWrapper.getSubNameTemp().size()) { |
||||||
|
String newName = mWrapper.getSubNameTemp().get(i); |
||||||
|
DownloadEntity entity = taskWrapper.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; |
||||||
|
} |
||||||
|
|
||||||
|
RecordUtil.modifyTaskRecord(oldPath, newPath); |
||||||
|
entity.setFilePath(newPath); |
||||||
|
entity.setFileName(newName); |
||||||
|
} |
||||||
|
} |
||||||
|
i++; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查urls是否合法,并删除不合法的子任务 |
||||||
|
* |
||||||
|
* @return {@code true} 合法 |
||||||
|
*/ |
||||||
|
private boolean checkUrls() { |
||||||
|
if (mEntity.getUrls().isEmpty()) { |
||||||
|
|
||||||
|
ALog.e(TAG, "下载失败,子任务下载列表为null"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
Set<String> repeated = new HashSet<>(); |
||||||
|
List<String> results = new ArrayList<>(); |
||||||
|
for (String url : mEntity.getUrls()) { |
||||||
|
if (!repeated.add(url)) { |
||||||
|
results.add(url); |
||||||
|
} |
||||||
|
} |
||||||
|
if (!results.isEmpty()) { |
||||||
|
ALog.e(TAG, String.format("组合任务中有url重复,重复的url:%s", Arrays.toString(results.toArray()))); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
Set<Integer> delItem = new HashSet<>(); |
||||||
|
|
||||||
|
int i = 0; |
||||||
|
for (String url : mEntity.getUrls()) { |
||||||
|
if (TextUtils.isEmpty(url)) { |
||||||
|
ALog.e(TAG, "子任务url为null,即将删除该子任务。"); |
||||||
|
delItem.add(i); |
||||||
|
continue; |
||||||
|
} else if (!url.startsWith("http")) { |
||||||
|
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) { |
||||||
|
mEntity.getUrls().remove(index); |
||||||
|
if (mWrapper.getSubNameTemp() != null && !mWrapper.getSubNameTemp().isEmpty()) { |
||||||
|
mWrapper.getSubNameTemp().remove(index); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
mEntity.setGroupHash(CommonUtil.getMd5Code(mEntity.getUrls())); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果用户设置了子任务文件名,检查子任务文件名 |
||||||
|
* |
||||||
|
* @return {@code true} 合法 |
||||||
|
*/ |
||||||
|
private boolean checkSubName() { |
||||||
|
if (mWrapper.getSubNameTemp() == null || mWrapper.getSubNameTemp().isEmpty()) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
if (mEntity.getUrls().size() != mWrapper.getSubNameTemp().size()) { |
||||||
|
ALog.e(TAG, "子任务文件名必须和子任务数量一致"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +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.download; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.FtpUrlEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
public class CheckFtpDirEntityUtil { |
||||||
|
private final String TAG = "CheckFtpDirEntityUtil"; |
||||||
|
private DGTaskWrapper mWrapper; |
||||||
|
private DownloadGroupEntity mEntity; |
||||||
|
|
||||||
|
public static CheckFtpDirEntityUtil newInstance(DGTaskWrapper wrapper) { |
||||||
|
return new CheckFtpDirEntityUtil(wrapper); |
||||||
|
} |
||||||
|
|
||||||
|
private CheckFtpDirEntityUtil(DGTaskWrapper wrapper) { |
||||||
|
mWrapper = wrapper; |
||||||
|
mEntity = mWrapper.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查并设置文件夹路径 |
||||||
|
* |
||||||
|
* @return {@code true} 合法 |
||||||
|
*/ |
||||||
|
private boolean checkDirPath() { |
||||||
|
if (TextUtils.isEmpty(mWrapper.getDirPathTemp())) { |
||||||
|
ALog.e(TAG, "文件夹路径不能为null"); |
||||||
|
return false; |
||||||
|
} else if (!mWrapper.getDirPathTemp().startsWith("/")) { |
||||||
|
ALog.e(TAG, String.format("文件夹路径【%s】错误", mWrapper.getDirPathTemp())); |
||||||
|
return false; |
||||||
|
} |
||||||
|
File file = new File(mWrapper.getDirPathTemp()); |
||||||
|
if (file.isFile()) { |
||||||
|
ALog.e(TAG, String.format("路径【%s】是文件,请设置文件夹路径", mWrapper.getDirPathTemp())); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if ((mEntity.getDirPath() == null || !mEntity.getDirPath().equals(mWrapper.getDirPathTemp())) |
||||||
|
&& DbEntity.checkDataExist(DownloadGroupEntity.class, "dirPath=?", |
||||||
|
mWrapper.getDirPathTemp())) { |
||||||
|
ALog.e(TAG, String.format("文件夹路径【%s】已被其它任务占用,请重新设置文件夹路径", mWrapper.getDirPathTemp())); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (TextUtils.isEmpty(mEntity.getDirPath()) || !mEntity.getDirPath() |
||||||
|
.equals(mWrapper.getDirPathTemp())) { |
||||||
|
if (!file.exists()) { |
||||||
|
file.mkdirs(); |
||||||
|
} |
||||||
|
mEntity.setDirPath(mWrapper.getDirPathTemp()); |
||||||
|
ALog.i(TAG, String.format("文件夹路径改变,将更新文件夹路径为:%s", mWrapper.getDirPathTemp())); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean checkEntity() { |
||||||
|
boolean b = checkDirPath() && checkUrl(); |
||||||
|
if (b) { |
||||||
|
mEntity.save(); |
||||||
|
if (mWrapper.getSubTaskWrapper() != null) { |
||||||
|
//初始化子项的登录信息
|
||||||
|
FtpUrlEntity tUrlEntity = mWrapper.asFtp().getUrlEntity(); |
||||||
|
for (DTaskWrapper wrapper : mWrapper.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 (mWrapper.asFtp().getUrlEntity().isFtps) { |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { |
||||||
|
ALog.e(TAG, "证书路径为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { |
||||||
|
ALog.e(TAG, "证书别名为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查普通任务的下载地址 |
||||||
|
* |
||||||
|
* @return {@code true}地址合法 |
||||||
|
*/ |
||||||
|
private boolean checkUrl() { |
||||||
|
final String url = mEntity.getKey(); |
||||||
|
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; |
||||||
|
} |
||||||
|
} |
@ -1,161 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.download; |
|
||||||
|
|
||||||
import android.support.annotation.CheckResult; |
|
||||||
import android.support.annotation.NonNull; |
|
||||||
import com.arialyy.aria.core.common.http.GetDelegate; |
|
||||||
import com.arialyy.aria.core.common.http.HttpDelegate; |
|
||||||
import com.arialyy.aria.core.common.http.PostDelegate; |
|
||||||
import com.arialyy.aria.core.download.m3u8.M3U8Delegate; |
|
||||||
import com.arialyy.aria.core.inf.IHttpFileLenAdapter; |
|
||||||
import java.net.Proxy; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by lyy on 2016/12/5. |
|
||||||
* https://github.com/AriaLyy/Aria
|
|
||||||
*/ |
|
||||||
public class DownloadTarget extends AbsDTarget<DownloadTarget> { |
|
||||||
private HttpDelegate<DownloadTarget> mHttpDelegate; |
|
||||||
private DNormalConfigHandler<DownloadTarget> mConfigHandler; |
|
||||||
|
|
||||||
DownloadTarget(DownloadEntity entity, String targetName) { |
|
||||||
this(entity.getUrl(), targetName); |
|
||||||
} |
|
||||||
|
|
||||||
DownloadTarget(String url, String targetName) { |
|
||||||
mConfigHandler = new DNormalConfigHandler<>(this, url, targetName); |
|
||||||
mHttpDelegate = new HttpDelegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Post处理 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public PostDelegate asPost() { |
|
||||||
mHttpDelegate = new PostDelegate<>(this); |
|
||||||
return (PostDelegate) mHttpDelegate; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* get处理 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public GetDelegate asGet() { |
|
||||||
mHttpDelegate = new GetDelegate<>(this); |
|
||||||
return (GetDelegate) mHttpDelegate; |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
public M3U8Delegate<DownloadTarget> asM3U8() { |
|
||||||
return new M3U8Delegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment;filename=***} |
|
||||||
* 如果获取不到服务器文件名,则使用用户设置的文件名 |
|
||||||
* |
|
||||||
* @param use {@code true} 使用 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public DownloadTarget useServerFileName(boolean use) { |
|
||||||
getTaskWrapper().asHttp().setUseServerFileName(use); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 |
|
||||||
* 如:原文件路径 /mnt/sdcard/test.zip |
|
||||||
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip |
|
||||||
* |
|
||||||
* @param filePath 路径必须为文件路径,不能为文件夹路径 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public DownloadTarget setFilePath(@NonNull String filePath) { |
|
||||||
mConfigHandler.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) { |
|
||||||
mConfigHandler.setTempFilePath(filePath); |
|
||||||
mConfigHandler.setForceDownload(forceDownload); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 如果你需要使用header中特定的key来设置文件长度,或有定制文件长度的需要,那么你可以通过该方法自行处理文件长度 |
|
||||||
*/ |
|
||||||
public DownloadTarget setFileLenAdapter(IHttpFileLenAdapter adapter) { |
|
||||||
return mHttpDelegate.setFileLenAdapter(adapter); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 从header中获取文件描述信息 |
|
||||||
*/ |
|
||||||
public String getContentDisposition() { |
|
||||||
return getEntity().getDisposition(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public DownloadTarget updateUrl(String newUrl) { |
|
||||||
return mConfigHandler.updateUrl(newUrl); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public int getTargetType() { |
|
||||||
return D_HTTP; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置URL的代理 |
|
||||||
* |
|
||||||
* @param proxy {@link Proxy} |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public DownloadTarget setUrlProxy(Proxy proxy) { |
|
||||||
return mHttpDelegate.setUrlProxy(proxy); |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
public DownloadTarget addHeader(@NonNull String key, @NonNull String value) { |
|
||||||
return mHttpDelegate.addHeader(key, value); |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
public DownloadTarget addHeaders(Map<String, String> headers) { |
|
||||||
return mHttpDelegate.addHeaders(headers); |
|
||||||
} |
|
||||||
|
|
||||||
@Override protected boolean checkEntity() { |
|
||||||
return mConfigHandler.checkEntity(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean isRunning() { |
|
||||||
return mConfigHandler.isRunning(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean taskExists() { |
|
||||||
return mConfigHandler.taskExists(); |
|
||||||
} |
|
||||||
} |
|
@ -1,110 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.download; |
|
||||||
|
|
||||||
import android.support.annotation.CheckResult; |
|
||||||
import com.arialyy.aria.core.common.ftp.FTPSDelegate; |
|
||||||
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
|
||||||
import com.arialyy.aria.core.inf.IFtpTarget; |
|
||||||
import com.arialyy.aria.core.manager.TaskWrapperManager; |
|
||||||
import java.net.Proxy; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by Aria.Lao on 2017/7/26. |
|
||||||
* ftp文件夹下载 |
|
||||||
*/ |
|
||||||
public class FtpDirDownloadTarget extends AbsDGTarget<FtpDirDownloadTarget> |
|
||||||
implements IFtpTarget<FtpDirDownloadTarget> { |
|
||||||
private FtpDelegate<FtpDirDownloadTarget> mFtpDelegate; |
|
||||||
private FtpDirConfigHandler mConfigHandler; |
|
||||||
|
|
||||||
FtpDirDownloadTarget(String url, String targetName) { |
|
||||||
setTargetName(targetName); |
|
||||||
init(url); |
|
||||||
} |
|
||||||
|
|
||||||
private void init(String key) { |
|
||||||
mConfigHandler = new FtpDirConfigHandler(this, |
|
||||||
TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, key)); |
|
||||||
mFtpDelegate = new FtpDelegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public int getTargetType() { |
|
||||||
return GROUP_FTP_DIR; |
|
||||||
} |
|
||||||
|
|
||||||
@Override protected boolean checkEntity() { |
|
||||||
return mConfigHandler.checkEntity(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean isRunning() { |
|
||||||
return mConfigHandler.isRunning(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean taskExists() { |
|
||||||
return mConfigHandler.taskExists(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 |
|
||||||
* 如:groupDirPath = "/mnt/sdcard/download/group_test" |
|
||||||
* <pre> |
|
||||||
* {@code |
|
||||||
* + mnt |
|
||||||
* + sdcard |
|
||||||
* + download |
|
||||||
* + group_test |
|
||||||
* - task1.apk |
|
||||||
* - task2.apk |
|
||||||
* - task3.apk |
|
||||||
* .... |
|
||||||
* |
|
||||||
* } |
|
||||||
* </pre> |
|
||||||
* |
|
||||||
* @param dirPath 任务组保存文件夹路径 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public FtpDirDownloadTarget setDirPath(String dirPath) { |
|
||||||
return mConfigHandler.setDirPath(dirPath); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 是否是FTPS协议 |
|
||||||
* 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} |
|
||||||
* 设置证书信息 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public FTPSDelegate<FtpDirDownloadTarget> asFtps() { |
|
||||||
getTaskWrapper().asFtp().getUrlEntity().isFtps = true; |
|
||||||
return new FTPSDelegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
@Override public FtpDirDownloadTarget charSet(String charSet) { |
|
||||||
return mFtpDelegate.charSet(charSet); |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
@Override public FtpDirDownloadTarget login(String userName, String password) { |
|
||||||
return mFtpDelegate.login(userName, password); |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
@Override public FtpDirDownloadTarget login(String userName, String password, String account) { |
|
||||||
return mFtpDelegate.login(userName, password, account); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,80 @@ |
|||||||
|
/* |
||||||
|
* 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 com.arialyy.aria.core.common.AbsNormalTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/7/26. |
||||||
|
* ftp文件夹下载 |
||||||
|
*/ |
||||||
|
public class FtpDirNormalTarget extends AbsNormalTarget<FtpDirNormalTarget> { |
||||||
|
private FtpDirConfigHandler<FtpDirNormalTarget> mConfigHandler; |
||||||
|
|
||||||
|
FtpDirNormalTarget(long taskId, String targetName) { |
||||||
|
setTargetName(targetName); |
||||||
|
mConfigHandler = new FtpDirConfigHandler<>(this, taskId); |
||||||
|
getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getEntity().getKey())); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mConfigHandler.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return mConfigHandler.taskExists(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 |
||||||
|
* 如:groupDirPath = "/mnt/sdcard/download/group_test" |
||||||
|
* <pre> |
||||||
|
* {@code |
||||||
|
* + mnt |
||||||
|
* + sdcard |
||||||
|
* + download |
||||||
|
* + group_test |
||||||
|
* - task1.apk |
||||||
|
* - task2.apk |
||||||
|
* - task3.apk |
||||||
|
* .... |
||||||
|
* |
||||||
|
* } |
||||||
|
* </pre> |
||||||
|
* |
||||||
|
* @param dirPath 任务组保存文件夹路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDirNormalTarget modifyDirPath(String dirPath) { |
||||||
|
return mConfigHandler.setDirPath(dirPath); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置登陆、字符串编码、ftps等参数 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDelegate<FtpDirNormalTarget> option() { |
||||||
|
return new FtpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadGroupEntity getEntity() { |
||||||
|
return (DownloadGroupEntity) super.getEntity(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,82 @@ |
|||||||
|
/* |
||||||
|
* 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 com.arialyy.aria.core.common.AbsStartTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/7/26. |
||||||
|
* ftp文件夹下载 |
||||||
|
*/ |
||||||
|
public class FtpDirStartTarget extends AbsStartTarget<FtpDirStartTarget> { |
||||||
|
private FtpDirConfigHandler<FtpDirStartTarget> mConfigHandler; |
||||||
|
|
||||||
|
FtpDirStartTarget(String url, String targetName) { |
||||||
|
setTargetName(targetName); |
||||||
|
mConfigHandler = new FtpDirConfigHandler<>(this, -1); |
||||||
|
getEntity().setGroupHash(url); |
||||||
|
getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 |
||||||
|
* 如:groupDirPath = "/mnt/sdcard/download/group_test" |
||||||
|
* <pre> |
||||||
|
* {@code |
||||||
|
* + mnt |
||||||
|
* + sdcard |
||||||
|
* + download |
||||||
|
* + group_test |
||||||
|
* - task1.apk |
||||||
|
* - task2.apk |
||||||
|
* - task3.apk |
||||||
|
* .... |
||||||
|
* |
||||||
|
* } |
||||||
|
* </pre> |
||||||
|
* |
||||||
|
* @param dirPath 任务组保存文件夹路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDirStartTarget setDirPath(String dirPath) { |
||||||
|
return mConfigHandler.setDirPath(dirPath); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置任务组别名 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDirStartTarget setGroupAlias(String alias) { |
||||||
|
mConfigHandler.setGroupAlias(alias); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置登陆、字符串编码、ftps等参数 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDelegate<FtpDirStartTarget> option() { |
||||||
|
return new FtpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadGroupEntity getEntity() { |
||||||
|
return (DownloadGroupEntity) super.getEntity(); |
||||||
|
} |
||||||
|
} |
@ -1,150 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.download; |
|
||||||
|
|
||||||
import android.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 AbsDTarget<FtpDownloadTarget> |
|
||||||
implements IFtpTarget<FtpDownloadTarget> { |
|
||||||
private FtpDelegate<FtpDownloadTarget> mFtpDelegate; |
|
||||||
private DNormalConfigHandler<FtpDownloadTarget> mConfigHandler; |
|
||||||
|
|
||||||
FtpDownloadTarget(DownloadEntity entity, String targetName) { |
|
||||||
this(entity.getUrl(), targetName); |
|
||||||
} |
|
||||||
|
|
||||||
FtpDownloadTarget(String url, String targetName) { |
|
||||||
mConfigHandler = new DNormalConfigHandler<>(this, url, targetName); |
|
||||||
init(); |
|
||||||
} |
|
||||||
|
|
||||||
private void init() { |
|
||||||
int lastIndex = mConfigHandler.getUrl().lastIndexOf("/"); |
|
||||||
getEntity().setFileName(mConfigHandler.getUrl().substring(lastIndex + 1)); |
|
||||||
getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(mConfigHandler.getUrl())); |
|
||||||
getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP); |
|
||||||
|
|
||||||
mFtpDelegate = new FtpDelegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 是否是FTPS协议 |
|
||||||
* 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} |
|
||||||
* 设置证书信息 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public FTPSDelegate<FtpDownloadTarget> 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 mConfigHandler.checkEntity(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean isRunning() { |
|
||||||
return mConfigHandler.isRunning(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean taskExists() { |
|
||||||
return mConfigHandler.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) { |
|
||||||
mConfigHandler.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) { |
|
||||||
mConfigHandler.setTempFilePath(filePath); |
|
||||||
mConfigHandler.setForceDownload(forceDownload); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
@Override public int getTargetType() { |
|
||||||
return D_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); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public FtpDownloadTarget updateUrl(String newUrl) { |
|
||||||
return mConfigHandler.updateUrl(newUrl); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,80 @@ |
|||||||
|
/* |
||||||
|
* 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.AbsNormalTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2016/12/5. |
||||||
|
* https://github.com/AriaLyy/Aria
|
||||||
|
*/ |
||||||
|
public class FtpNormalTarget extends AbsNormalTarget<FtpNormalTarget> { |
||||||
|
private DNormalConfigHandler<FtpNormalTarget> mConfigHandler; |
||||||
|
|
||||||
|
FtpNormalTarget(long taskId, String targetName) { |
||||||
|
mConfigHandler = new DNormalConfigHandler<>(this, taskId, targetName); |
||||||
|
getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(mConfigHandler.getUrl())); |
||||||
|
getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置登陆、字符串编码、ftps等参数 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDelegate<FtpNormalTarget> option() { |
||||||
|
return new FtpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置文件保存文件夹路径 |
||||||
|
* 关于文件名: |
||||||
|
* 1、如果保存路径是该文件的保存路径,如:/mnt/sdcard/file.zip,则使用路径中的文件名file.zip |
||||||
|
* 2、如果保存路径是文件夹路径,如:/mnt/sdcard/,则使用FTP服务器该文件的文件名 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpNormalTarget modifyFilePath(@NonNull String filePath) { |
||||||
|
int lastIndex = mConfigHandler.getUrl().lastIndexOf("/"); |
||||||
|
getEntity().setFileName(mConfigHandler.getUrl().substring(lastIndex + 1)); |
||||||
|
mConfigHandler.setTempFilePath(filePath); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新下载地址 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpNormalTarget updateUrl(String newUrl) { |
||||||
|
return mConfigHandler.updateUrl(newUrl); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mConfigHandler.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return mConfigHandler.taskExists(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadEntity getEntity() { |
||||||
|
return (DownloadEntity) super.getEntity(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
/* |
||||||
|
* 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.AbsStartTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理ftp第一次下载等逻辑 |
||||||
|
*/ |
||||||
|
public class FtpStartTarget extends AbsStartTarget<FtpStartTarget> { |
||||||
|
private DNormalConfigHandler<FtpStartTarget> mConfigHandler; |
||||||
|
|
||||||
|
FtpStartTarget(String url, String targetName) { |
||||||
|
mConfigHandler = new DNormalConfigHandler<>(this, -1, targetName); |
||||||
|
mConfigHandler.setUrl(url); |
||||||
|
getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); |
||||||
|
getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置登陆、字符串编码、ftps等参数 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDelegate<FtpStartTarget> option() { |
||||||
|
return new FtpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置文件保存文件夹路径 |
||||||
|
* 关于文件名: |
||||||
|
* 1、如果保存路径是该文件的保存路径,如:/mnt/sdcard/file.zip,则使用路径中的文件名file.zip |
||||||
|
* 2、如果保存路径是文件夹路径,如:/mnt/sdcard/,则使用FTP服务器该文件的文件名 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpStartTarget setFilePath(@NonNull String filePath) { |
||||||
|
int lastIndex = mConfigHandler.getUrl().lastIndexOf("/"); |
||||||
|
getEntity().setFileName(mConfigHandler.getUrl().substring(lastIndex + 1)); |
||||||
|
mConfigHandler.setTempFilePath(filePath); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 |
||||||
|
* 如:原文件路径 /mnt/sdcard/test.zip |
||||||
|
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip |
||||||
|
* |
||||||
|
* @param filePath 路径必须为文件路径,不能为文件夹路径 |
||||||
|
* @param forceDownload {@code true}强制下载,不考虑文件路径是否被占用 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpStartTarget setFilePath(@NonNull String filePath, boolean forceDownload) { |
||||||
|
mConfigHandler.setTempFilePath(filePath); |
||||||
|
mConfigHandler.setForceDownload(forceDownload); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadEntity getEntity() { |
||||||
|
return (DownloadEntity) super.getEntity(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,113 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
|
import android.support.annotation.CheckResult; |
||||||
|
import com.arialyy.aria.core.common.AbsNormalTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.http.HttpDelegate; |
||||||
|
import com.arialyy.aria.core.manager.SubTaskManager; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 下载任务组 |
||||||
|
*/ |
||||||
|
public class GroupNormalTarget extends AbsNormalTarget<GroupNormalTarget> { |
||||||
|
private HttpGroupConfigHandler<GroupNormalTarget> mConfigHandler; |
||||||
|
|
||||||
|
GroupNormalTarget(long taskId, String targetName) { |
||||||
|
setTargetName(targetName); |
||||||
|
mConfigHandler = new HttpGroupConfigHandler<>(this, taskId); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置http请求参数,header等信息 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpDelegate<GroupNormalTarget> option() { |
||||||
|
return new HttpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取子任务管理器 |
||||||
|
* |
||||||
|
* @return 子任务管理器 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public SubTaskManager getSubTaskManager() { |
||||||
|
return mConfigHandler.getSubTaskManager(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置任务组别名 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public GroupNormalTarget setGroupAlias(String alias) { |
||||||
|
mConfigHandler.setGroupAlias(alias); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新组合任务下载地址 |
||||||
|
* |
||||||
|
* @param urls 新的组合任务下载地址列表 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public GroupNormalTarget updateUrls(List<String> urls) { |
||||||
|
return mConfigHandler.updateUrls(urls); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 |
||||||
|
* 如:groupDirPath = "/mnt/sdcard/download/group_test" |
||||||
|
* <pre> |
||||||
|
* {@code |
||||||
|
* + mnt |
||||||
|
* + sdcard |
||||||
|
* + download |
||||||
|
* + group_test |
||||||
|
* - task1.apk |
||||||
|
* - task2.apk |
||||||
|
* - task3.apk |
||||||
|
* .... |
||||||
|
* |
||||||
|
* } |
||||||
|
* </pre> |
||||||
|
* |
||||||
|
* @param dirPath 任务组保存文件夹路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public GroupNormalTarget modifyDirPath(String dirPath) { |
||||||
|
return mConfigHandler.setDirPath(dirPath); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新子任务文件名,该方法必须在{@link #modifyDirPath(String)}之后调用,否则不生效 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public GroupNormalTarget modifySubFileName(List<String> subTaskFileName) { |
||||||
|
return mConfigHandler.setSubFileName(subTaskFileName); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mConfigHandler.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return mConfigHandler.taskExists(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,85 @@ |
|||||||
|
/* |
||||||
|
* 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 com.arialyy.aria.core.common.AbsNormalTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.http.HttpDelegate; |
||||||
|
import com.arialyy.aria.core.download.m3u8.M3U8Delegate; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2016/12/5. |
||||||
|
* https://github.com/AriaLyy/Aria
|
||||||
|
*/ |
||||||
|
public class HttpNormalTarget extends AbsNormalTarget<HttpNormalTarget> { |
||||||
|
private DNormalConfigHandler<HttpNormalTarget> mConfigHandler; |
||||||
|
|
||||||
|
HttpNormalTarget(long taskId, String targetName) { |
||||||
|
mConfigHandler = new DNormalConfigHandler<>(this, taskId, targetName); |
||||||
|
} |
||||||
|
|
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public M3U8Delegate<HttpNormalTarget> asM3U8() { |
||||||
|
return new M3U8Delegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置http请求参数,header等信息 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpDelegate<HttpNormalTarget> option() { |
||||||
|
return new HttpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新文件保存路径 |
||||||
|
* 如:原文件路径 /mnt/sdcard/test.zip |
||||||
|
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpNormalTarget modifyFilePath(String filePath) { |
||||||
|
mConfigHandler.setTempFilePath(filePath); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 从header中获取文件描述信息 |
||||||
|
*/ |
||||||
|
public String getContentDisposition() { |
||||||
|
return getEntity().getDisposition(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新下载地址 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpNormalTarget updateUrl(String newUrl) { |
||||||
|
return mConfigHandler.updateUrl(newUrl); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadEntity getEntity() { |
||||||
|
return (DownloadEntity) super.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mConfigHandler.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return mConfigHandler.taskExists(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,98 @@ |
|||||||
|
/* |
||||||
|
* 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.AbsStartTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.http.HttpDelegate; |
||||||
|
import com.arialyy.aria.core.download.m3u8.M3U8Delegate; |
||||||
|
import com.arialyy.aria.core.inf.IHttpFileLenAdapter; |
||||||
|
|
||||||
|
public class HttpStartTarget extends AbsStartTarget<HttpStartTarget> { |
||||||
|
|
||||||
|
private DNormalConfigHandler<HttpStartTarget> mConfigHandler; |
||||||
|
|
||||||
|
HttpStartTarget(String url, String targetName) { |
||||||
|
mConfigHandler = new DNormalConfigHandler<>(this, -1, targetName); |
||||||
|
mConfigHandler.setUrl(url); |
||||||
|
} |
||||||
|
|
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) public M3U8Delegate<HttpStartTarget> asM3U8() { |
||||||
|
return new M3U8Delegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置http请求参数,header等信息 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpDelegate<HttpStartTarget> option() { |
||||||
|
return new HttpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment;filename=***} |
||||||
|
* 如果获取不到服务器文件名,则使用用户设置的文件名 |
||||||
|
* |
||||||
|
* @param use {@code true} 使用 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpStartTarget useServerFileName(boolean use) { |
||||||
|
getTaskWrapper().asHttp().setUseServerFileName(use); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 |
||||||
|
* 如:原文件路径 /mnt/sdcard/test.zip |
||||||
|
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip |
||||||
|
* |
||||||
|
* @param filePath 路径必须为文件路径,不能为文件夹路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpStartTarget setFilePath(@NonNull String filePath) { |
||||||
|
mConfigHandler.setTempFilePath(filePath); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置文件存储路径,如果需要修改新的文件名,修改路径便可。 |
||||||
|
* 如:原文件路径 /mnt/sdcard/test.zip |
||||||
|
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip |
||||||
|
* |
||||||
|
* @param filePath 路径必须为文件路径,不能为文件夹路径 |
||||||
|
* @param forceDownload {@code true}强制下载,不考虑文件路径是否被占用 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpStartTarget setFilePath(@NonNull String filePath, boolean forceDownload) { |
||||||
|
mConfigHandler.setTempFilePath(filePath); |
||||||
|
mConfigHandler.setForceDownload(forceDownload); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你需要使用header中特定的key来设置文件长度,或有定制文件长度的需要,那么你可以通过该方法自行处理文件长度 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpStartTarget setFileLenAdapter(IHttpFileLenAdapter adapter) { |
||||||
|
if (adapter == null) { |
||||||
|
throw new IllegalArgumentException("adapter为空"); |
||||||
|
} |
||||||
|
getTaskWrapper().asHttp().setFileLenAdapter(adapter); |
||||||
|
return this; |
||||||
|
} |
||||||
|
} |
@ -1,57 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.inf; |
|
||||||
|
|
||||||
import android.support.annotation.CheckResult; |
|
||||||
import java.net.Proxy; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by laoyuyu on 2018/3/9. |
|
||||||
*/ |
|
||||||
public interface IFtpTarget<TARGET extends ITargetHandler> { |
|
||||||
/** |
|
||||||
* 设置字符编码 |
|
||||||
*/ |
|
||||||
@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);
|
|
||||||
} |
|
@ -1,50 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.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. |
|
||||||
* D_HTTP Header功能接口 |
|
||||||
*/ |
|
||||||
public interface IHttpHeaderDelegate<TARGET extends ITargetHandler> { |
|
||||||
|
|
||||||
/** |
|
||||||
* 给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<String, String> headers); |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
TARGET setUrlProxy(Proxy proxy); |
|
||||||
} |
|
@ -0,0 +1,109 @@ |
|||||||
|
/* |
||||||
|
* 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.util.ALog; |
||||||
|
import com.arialyy.aria.util.CheckUtil; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
public class CheckUEntityUtil { |
||||||
|
private final String TAG = "CheckUEntityUtil"; |
||||||
|
private UTaskWrapper mWrapper; |
||||||
|
private UploadEntity mEntity; |
||||||
|
|
||||||
|
public static CheckUEntityUtil newInstance(UTaskWrapper wrapper) { |
||||||
|
return new CheckUEntityUtil(wrapper); |
||||||
|
} |
||||||
|
|
||||||
|
private CheckUEntityUtil(UTaskWrapper wrapper) { |
||||||
|
mWrapper = wrapper; |
||||||
|
mEntity = mWrapper.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean checkEntity() { |
||||||
|
boolean b = checkFtps() && checkUrl() && checkFilePath(); |
||||||
|
if (b) { |
||||||
|
mEntity.save(); |
||||||
|
} |
||||||
|
if (mWrapper.asFtp().getUrlEntity() != null && mWrapper.asFtp().getUrlEntity().isFtps) { |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { |
||||||
|
ALog.e(TAG, "证书路径为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { |
||||||
|
ALog.e(TAG, "证书别名为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean checkUrl() { |
||||||
|
|
||||||
|
final String url = mWrapper.getTempUrl(); |
||||||
|
if (TextUtils.isEmpty(url)) { |
||||||
|
ALog.e(TAG, "上传失败,url为null"); |
||||||
|
return false; |
||||||
|
} else if (!CheckUtil.checkUrlNotThrow(url)) { |
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean checkFtps() { |
||||||
|
if (mWrapper.asFtp().getUrlEntity().isFtps) { |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { |
||||||
|
ALog.e(TAG, "证书路径为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { |
||||||
|
ALog.e(TAG, "证书别名为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +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.upload; |
||||||
|
|
||||||
|
import android.support.annotation.CheckResult; |
||||||
|
import com.arialyy.aria.core.common.AbsNormalTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/7/27. |
||||||
|
* ftp单任务上传 |
||||||
|
*/ |
||||||
|
public class FtpNormalTarget extends AbsNormalTarget<FtpNormalTarget> { |
||||||
|
private UNormalConfigHandler<FtpNormalTarget> mConfigHandler; |
||||||
|
|
||||||
|
FtpNormalTarget(long taskId, String targetName) { |
||||||
|
mConfigHandler = new UNormalConfigHandler<>(this, taskId, targetName); |
||||||
|
getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getEntity().getUrl())); |
||||||
|
getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置登陆、字符串编码、ftps等参数 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDelegate<FtpNormalTarget> option() { |
||||||
|
return new FtpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public UploadEntity getEntity() { |
||||||
|
return (UploadEntity) super.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mConfigHandler.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return mConfigHandler.taskExists(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,65 @@ |
|||||||
|
/* |
||||||
|
* 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.AbsStartTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.ftp.FtpDelegate; |
||||||
|
import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/7/27. |
||||||
|
* ftp单任务上传 |
||||||
|
*/ |
||||||
|
public class FtpStartTarget extends AbsStartTarget<FtpStartTarget> { |
||||||
|
private UNormalConfigHandler<FtpStartTarget> mConfigHandler; |
||||||
|
|
||||||
|
FtpStartTarget(String filePath, String targetName) { |
||||||
|
mConfigHandler = new UNormalConfigHandler<>(this, -1, targetName); |
||||||
|
mConfigHandler.setFilePath(filePath); |
||||||
|
getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置上传路径 |
||||||
|
* |
||||||
|
* @param tempUrl 上传路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpStartTarget setUploadUrl(String tempUrl) { |
||||||
|
mConfigHandler.setTempUrl(tempUrl); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* FTP文件上传拦截器,如果远端已有同名文件,可使用该拦截器控制覆盖文件或修改该文件上传到服务器端端的文件名 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpStartTarget setUploadInterceptor(@NonNull IFtpUploadInterceptor uploadInterceptor) { |
||||||
|
return mConfigHandler.setUploadInterceptor(uploadInterceptor); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置登陆、字符串编码、ftps等参数 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public FtpDelegate<FtpStartTarget> option() { |
||||||
|
return new FtpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
} |
@ -1,110 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.upload; |
|
||||||
|
|
||||||
import android.support.annotation.CheckResult; |
|
||||||
import android.support.annotation.NonNull; |
|
||||||
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.common.ftp.IFtpUploadInterceptor; |
|
||||||
import com.arialyy.aria.util.CheckUtil; |
|
||||||
import java.net.Proxy; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by Aria.Lao on 2017/7/27. |
|
||||||
* ftp单任务上传 |
|
||||||
*/ |
|
||||||
public class FtpUploadTarget extends AbsUploadTarget<FtpUploadTarget> |
|
||||||
implements IFtpTarget<FtpUploadTarget> { |
|
||||||
private FtpDelegate<FtpUploadTarget> mFtpDelegate; |
|
||||||
private UNormalConfigHandler<FtpUploadTarget> mConfigHandler; |
|
||||||
|
|
||||||
FtpUploadTarget(String filePath, String targetName) { |
|
||||||
mConfigHandler = new UNormalConfigHandler<>(this, filePath, targetName); |
|
||||||
initTask(); |
|
||||||
} |
|
||||||
|
|
||||||
private void initTask() { |
|
||||||
getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP); |
|
||||||
mFtpDelegate = new FtpDelegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置上传路径 |
|
||||||
* |
|
||||||
* @param tempUrl 上传路径 |
|
||||||
*/ |
|
||||||
public FtpUploadTarget setUploadUrl(String tempUrl) { |
|
||||||
mConfigHandler.setTempUrl(tempUrl); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* FTP文件上传拦截器,如果远端已有同名文件,可使用该拦截器控制覆盖文件或修改该文件上传到服务器端端的文件名 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public FtpUploadTarget setUploadInterceptor(@NonNull IFtpUploadInterceptor uploadInterceptor) { |
|
||||||
return mConfigHandler.setUploadInterceptor(uploadInterceptor); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 是否是FTPS协议 |
|
||||||
* 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} |
|
||||||
* 设置证书信息 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public FTPSDelegate<FtpUploadTarget> 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) { |
|
||||||
CheckUtil.checkFtpUploadUrl(mConfigHandler.getTempUrl()); |
|
||||||
return mFtpDelegate.login(userName, password, account); |
|
||||||
} |
|
||||||
|
|
||||||
@Override protected boolean checkEntity() { |
|
||||||
return mConfigHandler.checkEntity(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean isRunning() { |
|
||||||
return mConfigHandler.isRunning(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean taskExists() { |
|
||||||
return mConfigHandler.taskExists(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public int getTargetType() { |
|
||||||
return U_FTP; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,63 @@ |
|||||||
|
/* |
||||||
|
* 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.common.AbsNormalTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.http.HttpDelegate; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/2/28. |
||||||
|
* http 单文件上传 |
||||||
|
*/ |
||||||
|
public class HttpNormalTarget extends AbsNormalTarget<HttpNormalTarget> { |
||||||
|
private UNormalConfigHandler<HttpNormalTarget> mConfigHandler; |
||||||
|
|
||||||
|
HttpNormalTarget(long taskId, String targetName) { |
||||||
|
mConfigHandler = new UNormalConfigHandler<>(this, taskId, targetName); |
||||||
|
getTaskWrapper().setSupportBP(false); |
||||||
|
getTaskWrapper().setRequestType(AbsTaskWrapper.U_HTTP); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置上传路径 |
||||||
|
* |
||||||
|
* @param tempUrl 上传路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpNormalTarget setUploadUrl(String tempUrl) { |
||||||
|
mConfigHandler.setTempUrl(tempUrl); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置http请求参数,header等信息 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpDelegate<HttpNormalTarget> option() { |
||||||
|
return new HttpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mConfigHandler.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return mConfigHandler.taskExists(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
/* |
||||||
|
* 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.common.AbsStartTarget; |
||||||
|
import com.arialyy.aria.core.common.Suggest; |
||||||
|
import com.arialyy.aria.core.common.http.HttpDelegate; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/2/28. |
||||||
|
* http 单文件上传 |
||||||
|
*/ |
||||||
|
public class HttpStartTarget extends AbsStartTarget<HttpStartTarget> { |
||||||
|
private UNormalConfigHandler<HttpStartTarget> mConfigHandler; |
||||||
|
|
||||||
|
HttpStartTarget(String filePath, String targetName) { |
||||||
|
|
||||||
|
mConfigHandler = new UNormalConfigHandler<>(this, -1, targetName); |
||||||
|
mConfigHandler.setFilePath(filePath); |
||||||
|
//http暂时不支持断点上传
|
||||||
|
getTaskWrapper().setSupportBP(false); |
||||||
|
getTaskWrapper().setRequestType(AbsTaskWrapper.U_HTTP); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置上传路径 |
||||||
|
* |
||||||
|
* @param tempUrl 上传路径 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpStartTarget setUploadUrl(String tempUrl) { |
||||||
|
mConfigHandler.setTempUrl(tempUrl); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置http请求参数,header等信息 |
||||||
|
*/ |
||||||
|
@CheckResult(suggest = Suggest.TASK_CONTROLLER) |
||||||
|
public HttpDelegate<HttpStartTarget> option() { |
||||||
|
return new HttpDelegate<>(this, getTaskWrapper()); |
||||||
|
} |
||||||
|
} |
@ -1,125 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package com.arialyy.aria.core.upload; |
|
||||||
|
|
||||||
import android.support.annotation.CheckResult; |
|
||||||
import android.support.annotation.NonNull; |
|
||||||
import com.arialyy.aria.core.common.http.HttpDelegate; |
|
||||||
import com.arialyy.aria.core.common.http.PostDelegate; |
|
||||||
import com.arialyy.aria.core.inf.AbsTaskWrapper; |
|
||||||
import java.net.Proxy; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by lyy on 2017/2/28. |
|
||||||
* http 单文件上传 |
|
||||||
*/ |
|
||||||
public class UploadTarget extends AbsUploadTarget<UploadTarget> { |
|
||||||
private HttpDelegate<UploadTarget> mHttpDelegate; |
|
||||||
private UNormalConfigHandler<UploadTarget> mConfigHandler; |
|
||||||
|
|
||||||
UploadTarget(String filePath, String targetName) { |
|
||||||
mConfigHandler = new UNormalConfigHandler<>(this, filePath, targetName); |
|
||||||
initTask(); |
|
||||||
} |
|
||||||
|
|
||||||
private void initTask() { |
|
||||||
//http暂时不支持断点上传
|
|
||||||
getTaskWrapper().setSupportBP(false); |
|
||||||
getTaskWrapper().setRequestType(AbsTaskWrapper.U_HTTP); |
|
||||||
mHttpDelegate = new HttpDelegate<>(this); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置上传路径 |
|
||||||
* |
|
||||||
* @param tempUrl 上传路径 |
|
||||||
*/ |
|
||||||
public UploadTarget setUploadUrl(String tempUrl) { |
|
||||||
mConfigHandler.setTempUrl(tempUrl); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Post处理 |
|
||||||
*/ |
|
||||||
@CheckResult |
|
||||||
public PostDelegate asPost() { |
|
||||||
mHttpDelegate = new PostDelegate<>(this); |
|
||||||
return (PostDelegate) mHttpDelegate; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 设置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 |
|
||||||
public UploadTarget addHeader(@NonNull String key, @NonNull String value) { |
|
||||||
return mHttpDelegate.addHeader(key, value); |
|
||||||
} |
|
||||||
|
|
||||||
@CheckResult |
|
||||||
public UploadTarget addHeaders(Map<String, String> headers) { |
|
||||||
return mHttpDelegate.addHeaders(headers); |
|
||||||
} |
|
||||||
|
|
||||||
public UploadTarget setUrlProxy(Proxy proxy) { |
|
||||||
return mHttpDelegate.setUrlProxy(proxy); |
|
||||||
} |
|
||||||
|
|
||||||
@Override protected boolean checkEntity() { |
|
||||||
return mConfigHandler.checkEntity(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean isRunning() { |
|
||||||
return mConfigHandler.isRunning(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean taskExists() { |
|
||||||
return mConfigHandler.taskExists(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public int getTargetType() { |
|
||||||
return U_HTTP; |
|
||||||
} |
|
||||||
} |
|
@ -1,244 +1,254 @@ |
|||||||
/* |
/* |
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
package com.arialyy.simple.core.download; |
package com.arialyy.simple.core.download; |
||||||
|
|
||||||
import android.os.Bundle; |
import android.os.Bundle; |
||||||
import android.os.Environment; |
import android.os.Environment; |
||||||
import android.support.v7.widget.LinearLayoutManager; |
import android.support.v7.widget.LinearLayoutManager; |
||||||
import android.support.v7.widget.RecyclerView; |
import android.support.v7.widget.RecyclerView; |
||||||
import android.view.Menu; |
import android.view.Menu; |
||||||
import android.view.MenuItem; |
import android.view.MenuItem; |
||||||
import android.view.View; |
import android.view.View; |
||||||
import android.widget.Button; |
import android.widget.Button; |
||||||
import android.widget.TextView; |
import android.widget.TextView; |
||||||
import com.arialyy.annotations.Download; |
import com.arialyy.annotations.Download; |
||||||
import com.arialyy.aria.core.Aria; |
import com.arialyy.aria.core.Aria; |
||||||
import com.arialyy.aria.core.download.DownloadEntity; |
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.download.DownloadTask; |
import com.arialyy.aria.core.inf.AbsEntity; |
||||||
import com.arialyy.aria.core.inf.AbsEntity; |
import com.arialyy.aria.core.inf.IEntity; |
||||||
import com.arialyy.aria.core.inf.IEntity; |
import com.arialyy.frame.util.show.L; |
||||||
import com.arialyy.frame.util.show.L; |
import com.arialyy.simple.R; |
||||||
import com.arialyy.simple.R; |
import com.arialyy.simple.base.BaseActivity; |
||||||
import com.arialyy.simple.base.BaseActivity; |
import com.arialyy.simple.core.download.multi_download.DownloadAdapter; |
||||||
import com.arialyy.simple.databinding.ActivityHighestPriorityBinding; |
import com.arialyy.simple.databinding.ActivityHighestPriorityBinding; |
||||||
import com.arialyy.simple.core.download.multi_download.DownloadAdapter; |
import com.arialyy.simple.util.AppUtil; |
||||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; |
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; |
||||||
import java.util.ArrayList; |
import java.util.ArrayList; |
||||||
import java.util.HashSet; |
import java.util.HashSet; |
||||||
import java.util.List; |
import java.util.List; |
||||||
import java.util.Set; |
import java.util.Set; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by lyy on 2017/6/2. |
* Created by lyy on 2017/6/2. |
||||||
* 最高优先级任务Demo |
* 最高优先级任务Demo |
||||||
*/ |
*/ |
||||||
public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorityBinding> { |
public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorityBinding> { |
||||||
private HorizontalProgressBarWithNumber mPb; |
private HorizontalProgressBarWithNumber mPb; |
||||||
private Button mStart; |
private Button mStart; |
||||||
private Button mStop; |
private Button mStop; |
||||||
private Button mCancel; |
private Button mCancel; |
||||||
private TextView mSize; |
private TextView mSize; |
||||||
private TextView mSpeed; |
private TextView mSpeed; |
||||||
private RecyclerView mList; |
private RecyclerView mList; |
||||||
|
private DownloadEntity mEntity; |
||||||
private String mTaskName = "光明大陆"; |
|
||||||
private static final String DOWNLOAD_URL = |
private String mTaskName = "光明大陆"; |
||||||
"https://res5.d.cn/6f78ee3bcfdd033e64892a8553a95814cf5b4a62b12a76d9eb2a694905f0dc30fa5c7f728806a4ee0b3479e7b26a38707dac92b136add91191ac1219aadb4a3aa70bfa6d06d2d8db.apk"; |
private static final String DOWNLOAD_URL = |
||||||
private DownloadAdapter mAdapter; |
"https://res5.d.cn/6f78ee3bcfdd033e64892a8553a95814cf5b4a62b12a76d9eb2a694905f0dc30fa5c7f728806a4ee0b3479e7b26a38707dac92b136add91191ac1219aadb4a3aa70bfa6d06d2d8db.apk"; |
||||||
private List<AbsEntity> mData = new ArrayList<>(); |
private DownloadAdapter mAdapter; |
||||||
private Set<String> mRecord = new HashSet<>(); |
private List<AbsEntity> mData = new ArrayList<>(); |
||||||
|
private Set<String> mRecord = new HashSet<>(); |
||||||
@Override protected int setLayoutId() { |
|
||||||
return R.layout.activity_highest_priority; |
@Override protected int setLayoutId() { |
||||||
} |
return R.layout.activity_highest_priority; |
||||||
|
} |
||||||
@Override protected void init(Bundle savedInstanceState) { |
|
||||||
super.init(savedInstanceState); |
@Override protected void init(Bundle savedInstanceState) { |
||||||
mPb = findViewById(R.id.progressBar); |
super.init(savedInstanceState); |
||||||
mStart = findViewById(R.id.start); |
mPb = findViewById(R.id.progressBar); |
||||||
mStop = findViewById(R.id.stop); |
mStart = findViewById(R.id.start); |
||||||
mCancel = findViewById(R.id.cancel); |
mStop = findViewById(R.id.stop); |
||||||
mSize = findViewById(R.id.size); |
mCancel = findViewById(R.id.cancel); |
||||||
mSpeed = findViewById(R.id.speed); |
mSize = findViewById(R.id.size); |
||||||
mList = findViewById(R.id.list); |
mSpeed = findViewById(R.id.speed); |
||||||
|
mList = findViewById(R.id.list); |
||||||
setTitle("最高优先级任务"); |
|
||||||
getBinding().setTaskName("任务名:" + mTaskName + " (最高优先级任务)"); |
setTitle("最高优先级任务"); |
||||||
initWidget(); |
getBinding().setTaskName("任务名:" + mTaskName + " (最高优先级任务)"); |
||||||
Aria.download(this).register(); |
initWidget(); |
||||||
} |
Aria.download(this).register(); |
||||||
|
} |
||||||
private void initWidget() { |
|
||||||
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); |
private void initWidget() { |
||||||
mPb.setProgress(target.getPercent()); |
mEntity = Aria.download(this).getFirstDownloadEntity(DOWNLOAD_URL); |
||||||
if (target.getTaskState() == IEntity.STATE_STOP) { |
|
||||||
mStart.setText("恢复"); |
if (mEntity != null) { |
||||||
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); |
mPb.setProgress(mEntity.getPercent()); |
||||||
setBtState(true); |
if (mEntity.getState() == IEntity.STATE_STOP) { |
||||||
} else if (target.isRunning()) { |
mStart.setText("恢复"); |
||||||
setBtState(false); |
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); |
||||||
} |
setBtState(true); |
||||||
mSize.setText(target.getConvertFileSize()); |
} else if (mEntity.getState() == IEntity.STATE_RUNNING) { |
||||||
List<DownloadEntity> temp = Aria.download(this).getTaskList(); |
setBtState(false); |
||||||
if (temp != null && !temp.isEmpty()) { |
} |
||||||
for (DownloadEntity entity : temp) { |
mSize.setText(mEntity.getConvertFileSize()); |
||||||
if (entity.getUrl().equals(DOWNLOAD_URL)) continue; |
} |
||||||
mData.add(entity); |
|
||||||
mRecord.add(entity.getUrl()); |
List<DownloadEntity> temp = Aria.download(this).getTaskList(); |
||||||
} |
if (temp != null && !temp.isEmpty()) { |
||||||
} |
for (DownloadEntity entity : temp) { |
||||||
mAdapter = new DownloadAdapter(this, mData); |
if (entity.getUrl().equals(DOWNLOAD_URL)) continue; |
||||||
mList.setLayoutManager(new LinearLayoutManager(this)); |
mData.add(entity); |
||||||
mList.setAdapter(mAdapter); |
mRecord.add(entity.getUrl()); |
||||||
} |
} |
||||||
|
} |
||||||
@Override public boolean onCreateOptionsMenu(Menu menu) { |
mAdapter = new DownloadAdapter(this, mData); |
||||||
getMenuInflater().inflate(R.menu.menu_highest_priority, menu); |
mList.setLayoutManager(new LinearLayoutManager(this)); |
||||||
return super.onCreateOptionsMenu(menu); |
mList.setAdapter(mAdapter); |
||||||
} |
} |
||||||
|
|
||||||
@Override public boolean onMenuItemClick(MenuItem item) { |
@Override public boolean onCreateOptionsMenu(Menu menu) { |
||||||
switch (item.getItemId()) { |
getMenuInflater().inflate(R.menu.menu_highest_priority, menu); |
||||||
case R.id.add_task: |
return super.onCreateOptionsMenu(menu); |
||||||
List<DownloadEntity> temp = getModule(DownloadModule.class).getHighestTestList(); |
} |
||||||
for (DownloadEntity entity : temp) { |
|
||||||
String url = entity.getUrl(); |
@Override public boolean onMenuItemClick(MenuItem item) { |
||||||
if (mRecord.contains(url)) { |
switch (item.getItemId()) { |
||||||
continue; |
case R.id.add_task: |
||||||
} |
List<DownloadEntity> temp = getModule(DownloadModule.class).getHighestTestList(); |
||||||
mAdapter.addDownloadEntity(entity); |
for (DownloadEntity entity : temp) { |
||||||
mRecord.add(url); |
String url = entity.getUrl(); |
||||||
} |
if (mRecord.contains(url)) { |
||||||
mAdapter.notifyDataSetChanged(); |
continue; |
||||||
break; |
} |
||||||
case R.id.help: |
mAdapter.addDownloadEntity(entity); |
||||||
String title = "最高优先级任务介绍"; |
mRecord.add(url); |
||||||
String msg = " 将任务设置为最高优先级任务,最高优先级任务有以下特点:\n" |
} |
||||||
+ " 1、在下载队列中,有且只有一个最高优先级任务\n" |
mAdapter.notifyDataSetChanged(); |
||||||
+ " 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成\n" |
break; |
||||||
+ " 3、任务调度器不会暂停最高优先级任务\n" |
case R.id.help: |
||||||
+ " 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效\n" |
String title = "最高优先级任务介绍"; |
||||||
+ " 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行\n" |
String msg = " 将任务设置为最高优先级任务,最高优先级任务有以下特点:\n" |
||||||
+ " 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务"; |
+ " 1、在下载队列中,有且只有一个最高优先级任务\n" |
||||||
showMsgDialog(title, msg); |
+ " 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成\n" |
||||||
break; |
+ " 3、任务调度器不会暂停最高优先级任务\n" |
||||||
} |
+ " 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效\n" |
||||||
return true; |
+ " 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行\n" |
||||||
} |
+ " 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务"; |
||||||
|
showMsgDialog(title, msg); |
||||||
public void onClick(View view) { |
break; |
||||||
switch (view.getId()) { |
} |
||||||
case R.id.start: |
return true; |
||||||
String text = ((TextView) view).getText().toString(); |
} |
||||||
if (text.equals("重新开始?") || text.equals("开始")) { |
|
||||||
Aria.download(this) |
public void onClick(View view) { |
||||||
.load(DOWNLOAD_URL) |
switch (view.getId()) { |
||||||
.setFilePath(Environment.getExternalStorageDirectory().getPath() |
case R.id.start: |
||||||
+ "/Download/" |
if (AppUtil.chekEntityValid(mEntity)) { |
||||||
+ mTaskName |
Aria.download(this) |
||||||
+ ".apk") |
.load(DOWNLOAD_URL) |
||||||
.setHighestPriority(); |
.setFilePath(Environment.getExternalStorageDirectory().getPath() |
||||||
} else if (text.equals("恢复")) { |
+ "/Download/" |
||||||
Aria.download(this).load(DOWNLOAD_URL).resume(); |
+ mTaskName |
||||||
} |
+ ".apk") |
||||||
break; |
.setHighestPriority(); |
||||||
case R.id.stop: |
} else { |
||||||
Aria.download(this).load(DOWNLOAD_URL).pause(); |
Aria.download(this).load(mEntity.getId()).resume(); |
||||||
break; |
} |
||||||
case R.id.cancel: |
((TextView) view).setText(getString(R.string.stop)); |
||||||
Aria.download(this).load(DOWNLOAD_URL).cancel(); |
break; |
||||||
break; |
case R.id.stop: |
||||||
} |
if (AppUtil.chekEntityValid(mEntity)) { |
||||||
} |
Aria.download(this).load(mEntity.getId()).stop(); |
||||||
|
} |
||||||
/** |
((TextView) view).setText(getString(R.string.resume)); |
||||||
* 设置start 和 stop 按钮状态 |
break; |
||||||
*/ |
case R.id.cancel: |
||||||
private void setBtState(boolean state) { |
if (AppUtil.chekEntityValid(mEntity)) { |
||||||
mStart.setEnabled(state); |
Aria.download(this).load(mEntity.getId()).cancel(); |
||||||
mStop.setEnabled(!state); |
} |
||||||
} |
break; |
||||||
|
} |
||||||
@Download.onPre public void onPre(DownloadTask task) { |
} |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
|
||||||
} |
/** |
||||||
|
* 设置start 和 stop 按钮状态 |
||||||
@Download.onTaskPre public void onTaskPre(DownloadTask task) { |
*/ |
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
private void setBtState(boolean state) { |
||||||
mSize.setText(task.getConvertFileSize()); |
mStart.setEnabled(state); |
||||||
} |
mStop.setEnabled(!state); |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
} |
||||||
} |
|
||||||
|
@Download.onPre public void onPre(DownloadTask task) { |
||||||
@Download.onTaskStart public void onTaskStart(DownloadTask task) { |
mAdapter.updateState(task.getDownloadEntity()); |
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
} |
||||||
setBtState(false); |
|
||||||
} |
@Download.onTaskPre public void onTaskPre(DownloadTask task) { |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
} |
mSize.setText(task.getConvertFileSize()); |
||||||
|
} |
||||||
@Download.onTaskResume public void onTaskResume(DownloadTask task) { |
mAdapter.updateState(task.getDownloadEntity()); |
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
} |
||||||
setBtState(false); |
|
||||||
} |
@Download.onTaskStart public void onTaskStart(DownloadTask task) { |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
} |
setBtState(false); |
||||||
|
} |
||||||
@Download.onTaskStop public void onTaskStop(DownloadTask task) { |
mAdapter.updateState(task.getDownloadEntity()); |
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
} |
||||||
setBtState(true); |
|
||||||
mStart.setText("恢复"); |
@Download.onTaskResume public void onTaskResume(DownloadTask task) { |
||||||
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
} |
setBtState(false); |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
} |
||||||
} |
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) { |
|
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
@Download.onTaskStop public void onTaskStop(DownloadTask task) { |
||||||
setBtState(true); |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
mStart.setText("开始"); |
setBtState(true); |
||||||
mPb.setProgress(0); |
mStart.setText("恢复"); |
||||||
} |
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
} |
||||||
} |
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
@Download.onTaskFail public void onTaskFail(DownloadTask task) { |
|
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) { |
||||||
setBtState(true); |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
} else { |
setBtState(true); |
||||||
L.d(TAG, "download fail【" + task.getKey() + "】"); |
mStart.setText("开始"); |
||||||
} |
mPb.setProgress(0); |
||||||
} |
} |
||||||
|
mAdapter.updateState(task.getDownloadEntity()); |
||||||
@Download.onTaskComplete public void onTaskComplete(DownloadTask task) { |
} |
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
|
||||||
setBtState(true); |
@Download.onTaskFail public void onTaskFail(DownloadTask task) { |
||||||
mStart.setText("重新开始"); |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
mStart.setTextColor(getResources().getColor(android.R.color.holo_green_light)); |
setBtState(true); |
||||||
mPb.setProgress(100); |
} else { |
||||||
} |
L.d(TAG, "download fail【" + task.getKey() + "】"); |
||||||
mAdapter.updateState(task.getDownloadEntity()); |
} |
||||||
} |
} |
||||||
|
|
||||||
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) { |
@Download.onTaskComplete public void onTaskComplete(DownloadTask task) { |
||||||
if (task.getKey().equals(DOWNLOAD_URL)) { |
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
mPb.setProgress(task.getPercent()); |
setBtState(true); |
||||||
mSpeed.setText(task.getConvertSpeed()); |
mStart.setText("重新开始"); |
||||||
} |
mStart.setTextColor(getResources().getColor(android.R.color.holo_green_light)); |
||||||
mAdapter.setProgress(task.getDownloadEntity()); |
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()); |
||||||
|
} |
||||||
|
} |
||||||
|
@ -1,91 +0,0 @@ |
|||||||
package com.arialyy.simple.core.test; |
|
||||||
|
|
||||||
import android.os.Bundle; |
|
||||||
import android.util.Log; |
|
||||||
import android.view.View; |
|
||||||
import com.arialyy.annotations.Download; |
|
||||||
import com.arialyy.aria.core.Aria; |
|
||||||
import com.arialyy.aria.core.download.DownloadTask; |
|
||||||
import com.arialyy.aria.util.CommonUtil; |
|
||||||
import com.arialyy.simple.R; |
|
||||||
import com.arialyy.simple.base.BaseActivity; |
|
||||||
import com.arialyy.simple.databinding.ActivityTestBinding; |
|
||||||
import java.io.File; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by lyy on 2019/5/28. |
|
||||||
* Ftp 下载 |
|
||||||
* <a href="https://aria.laoyuyu.me/aria_doc/">文档</> |
|
||||||
*/ |
|
||||||
public class TestFTPActivity extends BaseActivity<ActivityTestBinding> { |
|
||||||
String TAG = "TestFTPActivity"; |
|
||||||
private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar"; |
|
||||||
private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; |
|
||||||
|
|
||||||
@Override protected int setLayoutId() { |
|
||||||
return R.layout.activity_test; |
|
||||||
} |
|
||||||
|
|
||||||
@Override protected void init(Bundle savedInstanceState) { |
|
||||||
super.init(savedInstanceState); |
|
||||||
mBar.setVisibility(View.GONE); |
|
||||||
Aria.download(this).register(); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onWait void onWait(DownloadTask task) { |
|
||||||
Log.d(TAG, "wait ==> " + task.getEntity().getFileName()); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onPre protected void onPre(DownloadTask task) { |
|
||||||
Log.d(TAG, "onPre"); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskStart void taskStart(DownloadTask task) { |
|
||||||
Log.d(TAG, "onStart"); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskRunning protected void running(DownloadTask task) { |
|
||||||
Log.d(TAG, "running,speed=" + task.getConvertSpeed()); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskResume void taskResume(DownloadTask task) { |
|
||||||
Log.d(TAG, "resume"); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskStop void taskStop(DownloadTask task) { |
|
||||||
Log.d(TAG, "stop"); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskCancel void taskCancel(DownloadTask task) { |
|
||||||
Log.d(TAG, "cancel"); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskFail void taskFail(DownloadTask task) { |
|
||||||
Log.d(TAG, "fail"); |
|
||||||
} |
|
||||||
|
|
||||||
@Download.onTaskComplete void taskComplete(DownloadTask task) { |
|
||||||
Log.d(TAG, "complete, md5 => " + CommonUtil.getFileMD5(new File(task.getKey()))); |
|
||||||
} |
|
||||||
|
|
||||||
public void onClick(View view) { |
|
||||||
switch (view.getId()) { |
|
||||||
case R.id.start: |
|
||||||
Aria.download(this) |
|
||||||
.loadFtp(URL) |
|
||||||
.setFilePath(FILE_PATH) |
|
||||||
.login("lao", "123456") |
|
||||||
//.asFtps() // ftps 配置
|
|
||||||
//.setStorePath("/mnt/sdcard/Download/server.crt") //设置证书路径
|
|
||||||
// .setAlias("www.laoyuyu.me") // 设置证书别名
|
|
||||||
.start(); |
|
||||||
break; |
|
||||||
case R.id.stop: |
|
||||||
Aria.download(this).loadFtp(FILE_PATH).stop(); |
|
||||||
break; |
|
||||||
case R.id.cancel: |
|
||||||
Aria.download(this).loadFtp(FILE_PATH).cancel(); |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,104 +0,0 @@ |
|||||||
package com.arialyy.simple.core.test; |
|
||||||
|
|
||||||
import android.os.Bundle; |
|
||||||
import android.os.Environment; |
|
||||||
import android.view.View; |
|
||||||
import com.arialyy.annotations.DownloadGroup; |
|
||||||
import com.arialyy.aria.core.Aria; |
|
||||||
import com.arialyy.aria.core.download.DownloadGroupTask; |
|
||||||
import com.arialyy.frame.util.show.L; |
|
||||||
import com.arialyy.simple.R; |
|
||||||
import com.arialyy.simple.base.BaseActivity; |
|
||||||
import com.arialyy.simple.databinding.ActivityTestBinding; |
|
||||||
import com.arialyy.simple.core.download.group.GroupModule; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by Administrator on 2018/4/12. |
|
||||||
*/ |
|
||||||
|
|
||||||
public class TestFTPDirActivity extends BaseActivity<ActivityTestBinding> { |
|
||||||
List<String> mUrls; |
|
||||||
private static final String dir = "ftps://192.168.29.140:990/upload/测试"; |
|
||||||
|
|
||||||
@Override protected int setLayoutId() { |
|
||||||
return R.layout.activity_test; |
|
||||||
} |
|
||||||
|
|
||||||
@Override protected void init(Bundle savedInstanceState) { |
|
||||||
super.init(savedInstanceState); |
|
||||||
mBar.setVisibility(View.GONE); |
|
||||||
Aria.download(this).register(); |
|
||||||
mUrls = getModule(GroupModule.class).getUrls(); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onWait void taskWait(DownloadGroupTask task) { |
|
||||||
L.d(TAG, task.getTaskName() + "wait"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onPre() protected void onPre(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group pre"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task pre"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task start"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task running"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task resume"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task stop"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task cancel"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task fail"); |
|
||||||
} |
|
||||||
|
|
||||||
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) { |
|
||||||
L.d(TAG, "group task complete"); |
|
||||||
} |
|
||||||
|
|
||||||
public void onClick(View view) { |
|
||||||
switch (view.getId()) { |
|
||||||
case R.id.start: |
|
||||||
//Aria.download(this)
|
|
||||||
// .loadGroup(mUrls)
|
|
||||||
// .setDirPath(Environment.getExternalStorageDirectory().getPath() + "/download/test/")
|
|
||||||
// .resetState()
|
|
||||||
// .start();
|
|
||||||
Aria.download(this) |
|
||||||
.loadFtpDir(dir) |
|
||||||
.setDirPath( |
|
||||||
Environment.getExternalStorageDirectory().getPath() + "/Download/ftp_dir") |
|
||||||
.setGroupAlias("ftp文件夹下载") |
|
||||||
//.setSubTaskFileName(getModule(GroupModule.class).getSubName())
|
|
||||||
.login("lao", "123456") |
|
||||||
.asFtps() |
|
||||||
.setStorePath("/mnt/sdcard/Download/server.crt") |
|
||||||
.setAlias("www.laoyuyu.me") |
|
||||||
.setStorePass("123456") |
|
||||||
.start(); |
|
||||||
break; |
|
||||||
case R.id.stop: |
|
||||||
Aria.download(this).loadFtpDir(dir).stop(); |
|
||||||
break; |
|
||||||
case R.id.cancel: |
|
||||||
Aria.download(this).loadFtpDir(dir).cancel(); |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,80 +0,0 @@ |
|||||||
package com.arialyy.simple.core.test; |
|
||||||
|
|
||||||
import android.os.Environment; |
|
||||||
import android.view.View; |
|
||||||
import com.arialyy.aria.core.Aria; |
|
||||||
import com.arialyy.aria.core.common.QueueMod; |
|
||||||
import com.arialyy.simple.R; |
|
||||||
import com.arialyy.simple.base.BaseActivity; |
|
||||||
import com.arialyy.simple.databinding.TestActivityMultiBinding; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by AriaL on 2017/6/15. |
|
||||||
*/ |
|
||||||
|
|
||||||
public class TestMutilTaskSysDownload extends BaseActivity<TestActivityMultiBinding> { |
|
||||||
|
|
||||||
@Override protected int setLayoutId() { |
|
||||||
return R.layout.test_activity_multi; |
|
||||||
} |
|
||||||
|
|
||||||
public void onClick(View view) { |
|
||||||
String baseUrl = "http://file.bmob.cn/"; |
|
||||||
String[] urlArray = { |
|
||||||
"M02/3B/A4/oYYBAFaOeUSAc1QiAAFTbmA7AHs052.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeUaAfYC-AAFD8zf9NXc879.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeUuAOxhnAACSdmbqSac702.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeU2AFAIGAAFICximvXc924.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeVCAPWMQAAFm2KWCq_E721.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeVOAbiv9AAFfCTTgr94948.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeVaAMR3tAAFf3yTuuCM577.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeVmACEWhAAEt72ecbpg468.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeVyAHHt4AAFg9e9bRio507.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeV-AClYXAAESLGY0gag424.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeWKAA7N0AAF3omYOJUI703.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeWWAD2lrAAFN7eRFxBs575.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeWiAdCVEAAFg4273Dus313.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeWyAJDm5AAF8JVoGVb0705.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeW-AUoA8AAGjKiHkXUo181.jpg", |
|
||||||
"M02/3B/A4/oYYBAFaOeXKABIamAAFU7J7vraE265.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeXaAW09jAAFf37qdwDA457.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeXmAWmS7AAFtLNpWjgo967.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeX2AQf9cAAF2fhwS2UE145.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeYCAKGnLAAFVAzks-qU937.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeYOAMODNAAF6HjTTMq4819.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeYeAbn8uAAFLSQLw48Q042.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeYqAMJThAAFtrNe4UNM047.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeY2AbnQvAAFNSXWn0Dc026.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeZCAIsr0AAFHZFEVhPc682.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeZOAGvITAAFqPmfcc9c471.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeZaATvjbAAFHDmALnhE003.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeZmAJPuVAAFfPJC2wsE319.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeZyAXtAmAAFfArJNwtM371.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeZ-AGZN0AAFgqwYYCS8004.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeaOAbbrGAAFcq59JjUo205.jpg", |
|
||||||
"M02/3B/A5/oYYBAFaOeaSAdFyoAACaxVxgUJA092.jpg" |
|
||||||
}; |
|
||||||
int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum(); |
|
||||||
Aria.get(this).setDownloadQueueMod(QueueMod.NOW); |
|
||||||
for (int i = 0; i < urlArray.length; i++) { |
|
||||||
Aria.download(this) |
|
||||||
.load(baseUrl + urlArray[i]) |
|
||||||
.setFilePath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") |
|
||||||
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
|
|
||||||
.start(); |
|
||||||
//if (i < maxNum) {
|
|
||||||
// Aria.download(this)
|
|
||||||
// .load(baseUrl + urlArray[i])
|
|
||||||
// .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
|
|
||||||
// //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
|
|
||||||
// .start();
|
|
||||||
//} else {
|
|
||||||
// Aria.download(this)
|
|
||||||
// .load(baseUrl + urlArray[i])
|
|
||||||
// .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
|
|
||||||
// //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
|
|
||||||
// .add();
|
|
||||||
//}
|
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue