commit
ce886921a6
@ -0,0 +1,30 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.command; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
*/ |
||||||
|
public abstract class AbsCmdFactory<CMD extends AbsCmd> { |
||||||
|
|
||||||
|
/** |
||||||
|
* @param target 创建任务的对象 |
||||||
|
* @param entity 下载实体 |
||||||
|
*/ |
||||||
|
public abstract <T extends AbsTaskEntity> CMD createCmd(String target, T entity, int type); |
||||||
|
} |
@ -0,0 +1,46 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.command.group; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.command.AbsCmd; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; |
||||||
|
import com.arialyy.aria.core.queue.DownloadTaskQueue; |
||||||
|
import com.arialyy.aria.core.queue.UploadTaskQueue; |
||||||
|
import com.arialyy.aria.core.upload.UploadTaskEntity; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 任务组命令 |
||||||
|
*/ |
||||||
|
public abstract class AbsGroupCmd<T extends AbsTaskEntity> extends AbsCmd<T> { |
||||||
|
|
||||||
|
/** |
||||||
|
* @param targetName 创建任务的对象名 |
||||||
|
*/ |
||||||
|
AbsGroupCmd(String targetName, T entity) { |
||||||
|
mTargetName = targetName; |
||||||
|
mTaskEntity = entity; |
||||||
|
TAG = CommonUtil.getClassName(this); |
||||||
|
if (entity instanceof DownloadGroupTaskEntity) { |
||||||
|
mQueue = DownloadGroupTaskQueue.getInstance(); |
||||||
|
isDownloadCmd = true; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,35 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.command.group; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 删除任务组 |
||||||
|
*/ |
||||||
|
class GroupCancelCmd<T extends AbsTaskEntity> extends AbsGroupCmd<T> { |
||||||
|
/** |
||||||
|
* @param targetName 创建任务的对象名 |
||||||
|
*/ |
||||||
|
GroupCancelCmd(String targetName, T entity) { |
||||||
|
super(targetName, entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void executeCmd() { |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,71 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.command.group; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.command.AbsCmdFactory; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
*/ |
||||||
|
public class GroupCmdFactory extends AbsCmdFactory<AbsGroupCmd> { |
||||||
|
/** |
||||||
|
* 启动任务 |
||||||
|
*/ |
||||||
|
public static final int TASK_START = 0xa1; |
||||||
|
/** |
||||||
|
* 停止任务 |
||||||
|
*/ |
||||||
|
public static final int TASK_STOP = 0xa2; |
||||||
|
/** |
||||||
|
* 取消任务 |
||||||
|
*/ |
||||||
|
public static final int TASK_CANCEL = 0xa3; |
||||||
|
|
||||||
|
private static volatile GroupCmdFactory INSTANCE = null; |
||||||
|
|
||||||
|
private GroupCmdFactory() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static GroupCmdFactory getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (AriaManager.LOCK) { |
||||||
|
INSTANCE = new GroupCmdFactory(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param target 创建任务的对象 |
||||||
|
* @param entity 下载实体 |
||||||
|
* @param type 命令类型{@link #TASK_START}、{@link #TASK_CANCEL}、{@link #TASK_STOP} |
||||||
|
*/ |
||||||
|
public <T extends AbsTaskEntity> AbsGroupCmd<T> createCmd(String target, T entity, int type) { |
||||||
|
switch (type) { |
||||||
|
case TASK_START: |
||||||
|
return new GroupStartCmd<>(target, entity); |
||||||
|
case TASK_STOP: |
||||||
|
return new GroupStopCmd<>(target, entity); |
||||||
|
case TASK_CANCEL: |
||||||
|
return new GroupCancelCmd<>(target, entity); |
||||||
|
default: |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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.command.group; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.QueueMod; |
||||||
|
import com.arialyy.aria.core.inf.AbsGroupTask; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
import com.arialyy.aria.core.inf.IEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 任务组开始命令,该命令负责开始下载或恢复下载的操作 |
||||||
|
*/ |
||||||
|
class GroupStartCmd<T extends AbsTaskEntity> extends AbsGroupCmd<T> { |
||||||
|
/** |
||||||
|
* @param targetName 创建任务的对象名 |
||||||
|
*/ |
||||||
|
GroupStartCmd(String targetName, T entity) { |
||||||
|
super(targetName, entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void executeCmd() { |
||||||
|
String mod; |
||||||
|
int maxTaskNum; |
||||||
|
AriaManager manager = AriaManager.getInstance(AriaManager.APP); |
||||||
|
if (isDownloadCmd) { |
||||||
|
mod = manager.getDownloadConfig().getQueueMod(); |
||||||
|
maxTaskNum = manager.getDownloadConfig().getMaxTaskNum(); |
||||||
|
} else { |
||||||
|
mod = manager.getUploadConfig().getQueueMod(); |
||||||
|
maxTaskNum = manager.getUploadConfig().getMaxTaskNum(); |
||||||
|
} |
||||||
|
|
||||||
|
AbsGroupTask task = (AbsGroupTask) mQueue.getTask(mTaskEntity.getEntity()); |
||||||
|
if (task == null) { |
||||||
|
task = (AbsGroupTask) mQueue.createTask(mTargetName, mTaskEntity); |
||||||
|
if (!TextUtils.isEmpty(mTargetName)) { |
||||||
|
task.setTargetName(mTargetName); |
||||||
|
} |
||||||
|
// 任务不存在时,根据配置不同,对任务执行操作
|
||||||
|
if (mod.equals(QueueMod.NOW.getTag())) { |
||||||
|
mQueue.startTask(task); |
||||||
|
} else if (mod.equals(QueueMod.WAIT.getTag())) { |
||||||
|
if (mQueue.getExePoolSize() < maxTaskNum) { |
||||||
|
mQueue.startTask(task); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
// 任务不存在时,根据配置不同,对任务执行操作
|
||||||
|
if (!task.isRunning() |
||||||
|
&& mod.equals(QueueMod.WAIT.getTag()) |
||||||
|
&& task.getState() == IEntity.STATE_WAIT) { |
||||||
|
mQueue.startTask(task); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,35 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.command.group; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 停止任务组的命令 |
||||||
|
*/ |
||||||
|
class GroupStopCmd<T extends AbsTaskEntity> extends AbsGroupCmd<T>{ |
||||||
|
/** |
||||||
|
* @param targetName 创建任务的对象名 |
||||||
|
*/ |
||||||
|
GroupStopCmd(String targetName, T entity) { |
||||||
|
super(targetName, entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void executeCmd() { |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,56 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.arialyy.aria.core.command.normal; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.command.AbsCmd; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
import com.arialyy.aria.core.queue.DownloadTaskQueue; |
||||||
|
import com.arialyy.aria.core.queue.UploadTaskQueue; |
||||||
|
import com.arialyy.aria.core.upload.UploadTaskEntity; |
||||||
|
import com.arialyy.aria.util.CheckUtil; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2016/8/22. |
||||||
|
* 下载命令 |
||||||
|
*/ |
||||||
|
public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> { |
||||||
|
|
||||||
|
/** |
||||||
|
* 能否执行命令 |
||||||
|
*/ |
||||||
|
boolean canExeCmd = true; |
||||||
|
|
||||||
|
/** |
||||||
|
* @param targetName 产生任务的对象名 |
||||||
|
*/ |
||||||
|
AbsNormalCmd(String targetName, T entity) { |
||||||
|
canExeCmd = CheckUtil.checkCmdEntity(entity, |
||||||
|
!(this instanceof CancelCmd) || !(this instanceof StopCmd)); |
||||||
|
mTargetName = targetName; |
||||||
|
mTaskEntity = entity; |
||||||
|
TAG = CommonUtil.getClassName(this); |
||||||
|
if (entity instanceof DownloadTaskEntity) { |
||||||
|
mQueue = DownloadTaskQueue.getInstance(); |
||||||
|
isDownloadCmd = true; |
||||||
|
} else if (entity instanceof UploadTaskEntity) { |
||||||
|
mQueue = UploadTaskQueue.getInstance(); |
||||||
|
isDownloadCmd = false; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,67 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.arialyy.aria.core.command.normal; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
import com.arialyy.aria.core.upload.UploadEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/27. |
||||||
|
* 删除所有任务,并且删除所有回掉 |
||||||
|
*/ |
||||||
|
final class CancelAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { |
||||||
|
/** |
||||||
|
* @param targetName 产生任务的对象名 |
||||||
|
*/ |
||||||
|
CancelAllCmd(String targetName, T entity) { |
||||||
|
super(targetName, entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void executeCmd() { |
||||||
|
mQueue.removeAllTask(); |
||||||
|
if (mTaskEntity instanceof DownloadTaskEntity) { |
||||||
|
handleDownloadRemove(); |
||||||
|
} else { |
||||||
|
handleUploadRemove(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理上传的删除 |
||||||
|
*/ |
||||||
|
private void handleUploadRemove() { |
||||||
|
List<UploadEntity> allEntity = DbEntity.findAllData(UploadEntity.class); |
||||||
|
for (UploadEntity entity : allEntity) { |
||||||
|
CommonUtil.delUploadTaskConfig(mTaskEntity.removeFile, entity); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理下载的删除 |
||||||
|
*/ |
||||||
|
private void handleDownloadRemove() { |
||||||
|
List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class); |
||||||
|
for (DownloadEntity entity : allEntity) { |
||||||
|
CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, entity); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,67 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
|
import android.os.Parcel; |
||||||
|
import com.arialyy.aria.core.inf.AbsGroupEntity; |
||||||
|
import com.arialyy.aria.orm.OneToMany; |
||||||
|
import java.util.LinkedList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 下载任务组实体 |
||||||
|
*/ |
||||||
|
public class DownloadGroupEntity extends AbsGroupEntity { |
||||||
|
|
||||||
|
@OneToMany(table = DownloadEntity.class, key = "groupName") |
||||||
|
private List<DownloadEntity> mChild = new LinkedList<>(); |
||||||
|
|
||||||
|
public List<DownloadEntity> getChild() { |
||||||
|
return mChild; |
||||||
|
} |
||||||
|
|
||||||
|
public void setChild(List<DownloadEntity> child) { |
||||||
|
this.mChild = child; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public int describeContents() { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void writeToParcel(Parcel dest, int flags) { |
||||||
|
super.writeToParcel(dest, flags); |
||||||
|
dest.writeTypedList(this.mChild); |
||||||
|
} |
||||||
|
|
||||||
|
public DownloadGroupEntity() { |
||||||
|
} |
||||||
|
|
||||||
|
protected DownloadGroupEntity(Parcel in) { |
||||||
|
super(in); |
||||||
|
this.mChild = in.createTypedArrayList(DownloadEntity.CREATOR); |
||||||
|
} |
||||||
|
|
||||||
|
public static final Creator<DownloadGroupEntity> CREATOR = new Creator<DownloadGroupEntity>() { |
||||||
|
@Override public DownloadGroupEntity createFromParcel(Parcel source) { |
||||||
|
return new DownloadGroupEntity(source); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadGroupEntity[] newArray(int size) { |
||||||
|
return new DownloadGroupEntity[size]; |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/30. |
||||||
|
*/ |
||||||
|
|
||||||
|
public class DownloadGroupListener { |
||||||
|
|
||||||
|
} |
@ -0,0 +1,73 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsGroupTarget; |
||||||
|
import com.arialyy.aria.core.inf.IEntity; |
||||||
|
import com.arialyy.aria.util.CheckUtil; |
||||||
|
import java.io.File; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
*/ |
||||||
|
public class DownloadGroupTarget |
||||||
|
extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> { |
||||||
|
private List<String> mUrls; |
||||||
|
|
||||||
|
DownloadGroupTarget(DownloadGroupEntity entity, String targetName, List<String> urls) { |
||||||
|
this.mEntity = entity; |
||||||
|
this.mTargetName = targetName; |
||||||
|
this.mUrls = urls; |
||||||
|
mTaskEntity = new DownloadGroupTaskEntity(); |
||||||
|
mTaskEntity.entity = entity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置保存路径组 |
||||||
|
*/ |
||||||
|
public DownloadGroupTarget setDownloadPaths(List<String> paths) { |
||||||
|
CheckUtil.checkDownloadPaths(paths); |
||||||
|
if (mUrls.size() != paths.size()) { |
||||||
|
throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致"); |
||||||
|
} |
||||||
|
for (int i = 0, len = mUrls.size(); i < len; i++) { |
||||||
|
mTaskEntity.getEntity().getChild().add(createDownloadEntity(mUrls.get(i), paths.get(i))); |
||||||
|
} |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建子任务下载实体 |
||||||
|
* |
||||||
|
* @param url 下载地址 |
||||||
|
* @param path 保存路径 |
||||||
|
*/ |
||||||
|
private DownloadEntity createDownloadEntity(String url, String path) { |
||||||
|
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", url); |
||||||
|
if (entity == null) { |
||||||
|
entity = new DownloadEntity(); |
||||||
|
} |
||||||
|
File file = new File(path); |
||||||
|
if (!file.exists()) { |
||||||
|
entity.setState(IEntity.STATE_WAIT); |
||||||
|
} |
||||||
|
entity.setDownloadPath(path); |
||||||
|
entity.setDownloadUrl(url); |
||||||
|
entity.setFileName(file.getName()); |
||||||
|
return entity; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,205 @@ |
|||||||
|
/* |
||||||
|
* 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.content.Context; |
||||||
|
import android.os.Handler; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.download.downloader.DownloadGroupUtil; |
||||||
|
import com.arialyy.aria.core.download.downloader.DownloadListener; |
||||||
|
import com.arialyy.aria.core.download.downloader.IDownloadUtil; |
||||||
|
import com.arialyy.aria.core.inf.AbsGroupTask; |
||||||
|
import com.arialyy.aria.core.inf.IEntity; |
||||||
|
import com.arialyy.aria.core.scheduler.DownloadSchedulers; |
||||||
|
import com.arialyy.aria.core.scheduler.ISchedulers; |
||||||
|
import com.arialyy.aria.util.CheckUtil; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.lang.ref.WeakReference; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/27. |
||||||
|
* 任务组任务 |
||||||
|
*/ |
||||||
|
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, DownloadGroupEntity> { |
||||||
|
private DListener mListener; |
||||||
|
private IDownloadUtil mUtil; |
||||||
|
|
||||||
|
private DownloadGroupTask(DownloadGroupTaskEntity taskEntity, Handler outHandler) { |
||||||
|
mTaskEntity = taskEntity; |
||||||
|
mEntity = taskEntity.getEntity(); |
||||||
|
mOutHandler = outHandler; |
||||||
|
mContext = AriaManager.APP; |
||||||
|
mListener = new DListener(mContext, this, mOutHandler); |
||||||
|
mUtil = new DownloadGroupUtil(mListener, mTaskEntity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
return mUtil.isDownloading(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void start() { |
||||||
|
mUtil.stopDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void stop() { |
||||||
|
mUtil.startDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void cancel() { |
||||||
|
mUtil.cancelDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 下载监听类 |
||||||
|
*/ |
||||||
|
private static class DListener extends DownloadListener { |
||||||
|
WeakReference<Handler> outHandler; |
||||||
|
WeakReference<DownloadGroupTask> wTask; |
||||||
|
Context context; |
||||||
|
long lastLen = 0; //上一次发送长度
|
||||||
|
boolean isFirst = true; |
||||||
|
DownloadGroupEntity entity; |
||||||
|
DownloadGroupTask task; |
||||||
|
boolean isConvertSpeed = false; |
||||||
|
|
||||||
|
DListener(Context context, DownloadGroupTask task, Handler outHandler) { |
||||||
|
this.context = context; |
||||||
|
this.outHandler = new WeakReference<>(outHandler); |
||||||
|
this.wTask = new WeakReference<>(task); |
||||||
|
this.task = wTask.get(); |
||||||
|
this.entity = this.task.getEntity(); |
||||||
|
final AriaManager manager = AriaManager.getInstance(context); |
||||||
|
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onPre() { |
||||||
|
saveData(IEntity.STATE_PRE, -1); |
||||||
|
sendInState2Target(ISchedulers.PRE); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onPostPre(long fileSize) { |
||||||
|
entity.setFileSize(fileSize); |
||||||
|
saveData(IEntity.STATE_POST_PRE, -1); |
||||||
|
sendInState2Target(ISchedulers.POST_PRE); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onStart(long startLocation) { |
||||||
|
saveData(IEntity.STATE_RUNNING, startLocation); |
||||||
|
sendInState2Target(ISchedulers.START); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onResume(long resumeLocation) { |
||||||
|
saveData(IEntity.STATE_RUNNING, resumeLocation); |
||||||
|
sendInState2Target(ISchedulers.RESUME); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onProgress(long currentLocation) { |
||||||
|
long speed = currentLocation - lastLen; |
||||||
|
if (isFirst) { |
||||||
|
speed = 0; |
||||||
|
isFirst = false; |
||||||
|
} |
||||||
|
handleSpeed(speed); |
||||||
|
lastLen = currentLocation; |
||||||
|
sendInState2Target(ISchedulers.RUNNING); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onStop(long stopLocation) { |
||||||
|
saveData(IEntity.STATE_STOP, stopLocation); |
||||||
|
handleSpeed(0); |
||||||
|
sendInState2Target(ISchedulers.STOP); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onCancel() { |
||||||
|
saveData(IEntity.STATE_CANCEL, -1); |
||||||
|
handleSpeed(0); |
||||||
|
sendInState2Target(ISchedulers.CANCEL); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onComplete() { |
||||||
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
||||||
|
handleSpeed(0); |
||||||
|
sendInState2Target(ISchedulers.COMPLETE); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onFail() { |
||||||
|
entity.setFailNum(entity.getFailNum() + 1); |
||||||
|
saveData(IEntity.STATE_FAIL, -1); |
||||||
|
handleSpeed(0); |
||||||
|
sendInState2Target(ISchedulers.FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
private void handleSpeed(long speed) { |
||||||
|
if (isConvertSpeed) { |
||||||
|
entity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s"); |
||||||
|
} else { |
||||||
|
entity.setSpeed(speed); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 将任务状态发送给下载器 |
||||||
|
* |
||||||
|
* @param state {@link DownloadSchedulers#START} |
||||||
|
*/ |
||||||
|
private void sendInState2Target(int state) { |
||||||
|
if (outHandler.get() != null) { |
||||||
|
outHandler.get().obtainMessage(state, task).sendToTarget(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void saveData(int state, long location) { |
||||||
|
if (state == IEntity.STATE_CANCEL) { |
||||||
|
entity.deleteData(); |
||||||
|
} else { |
||||||
|
entity.setState(state); |
||||||
|
if (location != -1) { |
||||||
|
entity.setCurrentProgress(location); |
||||||
|
} |
||||||
|
entity.update(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static class Builder { |
||||||
|
DownloadGroupTaskEntity taskEntity; |
||||||
|
Handler outHandler; |
||||||
|
String targetName; |
||||||
|
|
||||||
|
public Builder(String targetName, DownloadGroupTaskEntity taskEntity) { |
||||||
|
CheckUtil.checkTaskEntity(taskEntity); |
||||||
|
this.targetName = targetName; |
||||||
|
this.taskEntity = taskEntity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置自定义Handler处理下载状态时间 |
||||||
|
* |
||||||
|
* @param schedulers {@link ISchedulers} |
||||||
|
*/ |
||||||
|
public DownloadGroupTask.Builder setOutHandler(ISchedulers schedulers) { |
||||||
|
this.outHandler = new Handler(schedulers); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public DownloadGroupTask build() { |
||||||
|
DownloadGroupTask task = new DownloadGroupTask(taskEntity, outHandler); |
||||||
|
task.setTargetName(targetName); |
||||||
|
taskEntity.save(); |
||||||
|
return task; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
import com.arialyy.aria.orm.OneToOne; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/7/1. |
||||||
|
*/ |
||||||
|
public class DownloadGroupTaskEntity extends AbsTaskEntity<DownloadGroupEntity> { |
||||||
|
|
||||||
|
@OneToOne(table = DownloadGroupEntity.class, key = "groupName") public DownloadGroupEntity entity; |
||||||
|
|
||||||
|
@Override public DownloadGroupEntity getEntity() { |
||||||
|
return entity; |
||||||
|
} |
||||||
|
} |
@ -1,496 +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.content.Context; |
|
||||||
import android.util.Log; |
|
||||||
import android.util.SparseArray; |
|
||||||
import com.arialyy.aria.core.AriaManager; |
|
||||||
import com.arialyy.aria.util.BufferedRandomAccessFile; |
|
||||||
import com.arialyy.aria.util.CommonUtil; |
|
||||||
import java.io.File; |
|
||||||
import java.io.IOException; |
|
||||||
import java.net.HttpURLConnection; |
|
||||||
import java.net.URL; |
|
||||||
import java.util.Properties; |
|
||||||
import java.util.Set; |
|
||||||
import java.util.concurrent.ExecutorService; |
|
||||||
import java.util.concurrent.Executors; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by lyy on 2015/8/25. |
|
||||||
* 下载工具类 |
|
||||||
*/ |
|
||||||
class DownloadUtil implements IDownloadUtil, Runnable { |
|
||||||
private static final String TAG = "DownloadUtil"; |
|
||||||
/** |
|
||||||
* 线程数 |
|
||||||
*/ |
|
||||||
private int THREAD_NUM; |
|
||||||
/** |
|
||||||
* 小于1m的文件不启用多线程 |
|
||||||
*/ |
|
||||||
private static final long SUB_LEN = 1024 * 1024; |
|
||||||
//下载监听
|
|
||||||
private IDownloadListener mListener; |
|
||||||
private int mConnectTimeOut = 0; //连接超时时间
|
|
||||||
private boolean isNewTask = true; |
|
||||||
private boolean isSupportBreakpoint = true; |
|
||||||
private Context mContext; |
|
||||||
private DownloadEntity mDownloadEntity; |
|
||||||
private DownloadTaskEntity mDownloadTaskEntity; |
|
||||||
private ExecutorService mFixedThreadPool; |
|
||||||
private File mDownloadFile; //下载的文件
|
|
||||||
private File mConfigFile;//下载信息配置文件
|
|
||||||
private SparseArray<Runnable> mTask = new SparseArray<>(); |
|
||||||
private DownloadStateConstance CONSTANCE; |
|
||||||
|
|
||||||
DownloadUtil(Context context, DownloadTaskEntity entity, IDownloadListener downloadListener) { |
|
||||||
mDownloadEntity = entity.downloadEntity; |
|
||||||
mContext = context.getApplicationContext(); |
|
||||||
mDownloadTaskEntity = entity; |
|
||||||
mListener = downloadListener; |
|
||||||
// 线程下载数改变后,新的下载才会生效
|
|
||||||
//mFixedThreadPool = Executors.newFixedThreadPool(Integer.MAX_VALUE);
|
|
||||||
CONSTANCE = new DownloadStateConstance(); |
|
||||||
init(); |
|
||||||
} |
|
||||||
|
|
||||||
private void init() { |
|
||||||
mConnectTimeOut = AriaManager.getInstance(mContext).getDownloadConfig().getConnectTimeOut(); |
|
||||||
mDownloadFile = new File(mDownloadTaskEntity.downloadEntity.getDownloadPath()); |
|
||||||
//读取已完成的线程数
|
|
||||||
mConfigFile = new File(mContext.getFilesDir().getPath() |
|
||||||
+ AriaManager.DOWNLOAD_TEMP_DIR |
|
||||||
+ mDownloadFile.getName() |
|
||||||
+ ".properties"); |
|
||||||
try { |
|
||||||
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
|
|
||||||
isNewTask = true; |
|
||||||
CommonUtil.createFile(mConfigFile.getPath()); |
|
||||||
} else { |
|
||||||
isNewTask = !mDownloadFile.exists(); |
|
||||||
} |
|
||||||
} catch (Exception e) { |
|
||||||
e.printStackTrace(); |
|
||||||
failDownload("下载失败,记录文件被删除"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public IDownloadListener getListener() { |
|
||||||
return mListener; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 获取当前下载位置 |
|
||||||
*/ |
|
||||||
@Override public long getCurrentLocation() { |
|
||||||
return CONSTANCE.CURRENT_LOCATION; |
|
||||||
} |
|
||||||
|
|
||||||
@Override public boolean isDownloading() { |
|
||||||
return CONSTANCE.isDownloading; |
|
||||||
} |
|
||||||
|
|
||||||
public void setMaxSpeed(double maxSpeed) { |
|
||||||
for (int i = 0; i < THREAD_NUM; i++) { |
|
||||||
SingleThreadTask task = (SingleThreadTask) mTask.get(i); |
|
||||||
if (task != null) { |
|
||||||
task.setMaxSpeed(maxSpeed); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 取消下载 |
|
||||||
*/ |
|
||||||
@Override public void cancelDownload() { |
|
||||||
CONSTANCE.isCancel = true; |
|
||||||
CONSTANCE.isDownloading = false; |
|
||||||
if (mFixedThreadPool != null) { |
|
||||||
mFixedThreadPool.shutdown(); |
|
||||||
} |
|
||||||
for (int i = 0; i < THREAD_NUM; i++) { |
|
||||||
SingleThreadTask task = (SingleThreadTask) mTask.get(i); |
|
||||||
if (task != null) { |
|
||||||
task.cancel(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 停止下载 |
|
||||||
*/ |
|
||||||
@Override public void stopDownload() { |
|
||||||
CONSTANCE.isStop = true; |
|
||||||
CONSTANCE.isDownloading = false; |
|
||||||
if (mFixedThreadPool != null) { |
|
||||||
mFixedThreadPool.shutdown(); |
|
||||||
} |
|
||||||
for (int i = 0; i < THREAD_NUM; i++) { |
|
||||||
SingleThreadTask task = (SingleThreadTask) mTask.get(i); |
|
||||||
if (task != null) { |
|
||||||
task.stop(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 删除下载记录文件 |
|
||||||
*/ |
|
||||||
@Override public void delConfigFile() { |
|
||||||
if (mContext != null && mDownloadEntity != null) { |
|
||||||
File dFile = new File(mDownloadEntity.getDownloadPath()); |
|
||||||
File config = |
|
||||||
new File(mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); |
|
||||||
if (config.exists()) { |
|
||||||
config.delete(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 删除temp文件 |
|
||||||
*/ |
|
||||||
@Override public void delTempFile() { |
|
||||||
if (mContext != null && mDownloadEntity != null) { |
|
||||||
File dFile = new File(mDownloadEntity.getDownloadPath()); |
|
||||||
if (dFile.exists()) { |
|
||||||
dFile.delete(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 多线程断点续传下载文件,开始下载 |
|
||||||
*/ |
|
||||||
@Override public void startDownload() { |
|
||||||
CONSTANCE.cleanState(); |
|
||||||
mListener.onPre(); |
|
||||||
new Thread(this).start(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public void resumeDownload() { |
|
||||||
startDownload(); |
|
||||||
} |
|
||||||
|
|
||||||
private void failDownload(String msg) { |
|
||||||
Log.e(TAG, msg); |
|
||||||
CONSTANCE.isDownloading = false; |
|
||||||
mListener.onFail(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override public void run() { |
|
||||||
try { |
|
||||||
URL url = new URL(mDownloadEntity.getDownloadUrl()); |
|
||||||
HttpURLConnection conn = ConnectionHelp.handleConnection(url); |
|
||||||
conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn); |
|
||||||
conn.setRequestProperty("Range", "bytes=" + 0 + "-"); |
|
||||||
conn.setConnectTimeout(mConnectTimeOut); |
|
||||||
conn.connect(); |
|
||||||
handleConnect(conn); |
|
||||||
} catch (IOException e) { |
|
||||||
failDownload("下载失败【downloadUrl:" |
|
||||||
+ mDownloadEntity.getDownloadUrl() |
|
||||||
+ "】\n【filePath:" |
|
||||||
+ mDownloadFile.getPath() |
|
||||||
+ "】\n" |
|
||||||
+ CommonUtil.getPrintException(e)); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 处理状态吗 |
|
||||||
*/ |
|
||||||
private void handleConnect(HttpURLConnection conn) throws IOException { |
|
||||||
int len = conn.getContentLength(); |
|
||||||
//if (len < 0) { //网络被劫持时会出现这个问题
|
|
||||||
// failDownload("下载失败,网络被劫持");
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
int code = conn.getResponseCode(); |
|
||||||
//https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
|
|
||||||
//206支持断点
|
|
||||||
if (code == HttpURLConnection.HTTP_PARTIAL) { |
|
||||||
if (!checkLen(len)) return; |
|
||||||
isSupportBreakpoint = true; |
|
||||||
mListener.supportBreakpoint(true); |
|
||||||
handleBreakpoint(conn); |
|
||||||
} else if (code == HttpURLConnection.HTTP_OK) { |
|
||||||
//在conn.setRequestProperty("Range", "bytes=" + 0 + "-");下,200为不支持断点状态
|
|
||||||
if (!checkLen(len)) return; |
|
||||||
isSupportBreakpoint = false; |
|
||||||
mListener.supportBreakpoint(false); |
|
||||||
Log.w(TAG, "该下载链接不支持断点下载"); |
|
||||||
handleBreakpoint(conn); |
|
||||||
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) { |
|
||||||
Log.w(TAG, "任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:404"); |
|
||||||
mListener.onCancel(); |
|
||||||
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP |
|
||||||
|| code == HttpURLConnection.HTTP_MOVED_PERM |
|
||||||
|| code == HttpURLConnection.HTTP_SEE_OTHER) { |
|
||||||
handle302Turn(conn); |
|
||||||
} else { |
|
||||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:" + code); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 检查长度是否合法 |
|
||||||
* |
|
||||||
* @param len 从服务器获取的文件长度 |
|
||||||
* @return true, 合法 |
|
||||||
*/ |
|
||||||
private boolean checkLen(long len) { |
|
||||||
if (len < 0) { |
|
||||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,文件长度小于0"); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 处理30x跳转 |
|
||||||
*/ |
|
||||||
private void handle302Turn(HttpURLConnection conn) throws IOException { |
|
||||||
String newUrl = conn.getHeaderField(mDownloadTaskEntity.redirectUrlKey); |
|
||||||
Log.d(TAG, "30x跳转,新url为【" + newUrl + "】"); |
|
||||||
mDownloadEntity.setRedirect(true); |
|
||||||
mDownloadEntity.setRedirectUrl(newUrl); |
|
||||||
mDownloadEntity.update(); |
|
||||||
String cookies = conn.getHeaderField("Set-Cookie"); |
|
||||||
conn = (HttpURLConnection) new URL(newUrl).openConnection(); |
|
||||||
conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn); |
|
||||||
conn.setRequestProperty("Cookie", cookies); |
|
||||||
conn.setRequestProperty("Range", "bytes=" + 0 + "-"); |
|
||||||
conn.setConnectTimeout(mConnectTimeOut); |
|
||||||
conn.connect(); |
|
||||||
|
|
||||||
handleConnect(conn); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 处理断点 |
|
||||||
*/ |
|
||||||
private void handleBreakpoint(HttpURLConnection conn) throws IOException { |
|
||||||
//不支持断点只能单线程下载
|
|
||||||
if (!isSupportBreakpoint) { |
|
||||||
handleNoSupportBreakpointDownload(conn); |
|
||||||
return; |
|
||||||
} |
|
||||||
int fileLength = conn.getContentLength(); |
|
||||||
Properties pro = createConfigFile(fileLength); |
|
||||||
int blockSize = fileLength / THREAD_NUM; |
|
||||||
int[] recordL = new int[THREAD_NUM]; |
|
||||||
int rl = 0; |
|
||||||
for (int i = 0; i < THREAD_NUM; i++) { |
|
||||||
recordL[i] = -1; |
|
||||||
} |
|
||||||
for (int i = 0; i < THREAD_NUM; i++) { |
|
||||||
long startL = i * blockSize, endL = (i + 1) * blockSize; |
|
||||||
Object state = pro.getProperty(mDownloadFile.getName() + "_state_" + i); |
|
||||||
if (state != null && Integer.parseInt(state + "") == 1) { //该线程已经完成
|
|
||||||
if (resumeRecordLocation(i, startL, endL)) return; |
|
||||||
continue; |
|
||||||
} |
|
||||||
//分配下载位置
|
|
||||||
Object record = pro.getProperty(mDownloadFile.getName() + "_record_" + i); |
|
||||||
//如果有记录,则恢复下载
|
|
||||||
if (!isNewTask && record != null && Long.parseLong(record + "") > 0) { |
|
||||||
Long r = Long.parseLong(record + ""); |
|
||||||
CONSTANCE.CURRENT_LOCATION += r - startL; |
|
||||||
Log.d(TAG, "任务【" + mDownloadEntity.getFileName() + "】线程__" + i + "__恢复下载"); |
|
||||||
mListener.onChildResume(r); |
|
||||||
startL = r; |
|
||||||
recordL[rl] = i; |
|
||||||
rl++; |
|
||||||
} else { |
|
||||||
handleNewTask(fileLength); |
|
||||||
} |
|
||||||
if (isNewTask) { |
|
||||||
recordL[rl] = i; |
|
||||||
rl++; |
|
||||||
} |
|
||||||
if (i == (THREAD_NUM - 1)) { |
|
||||||
//最后一个线程的结束位置即为文件的总长度
|
|
||||||
endL = fileLength; |
|
||||||
} |
|
||||||
addSingleTask(i, startL, endL, fileLength); |
|
||||||
} |
|
||||||
startSingleTask(recordL); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 处理不支持断点的下载 |
|
||||||
*/ |
|
||||||
private void handleNoSupportBreakpointDownload(HttpURLConnection conn) { |
|
||||||
ChildThreadConfigEntity entity = new ChildThreadConfigEntity(); |
|
||||||
long len = conn.getContentLength(); |
|
||||||
entity.FILE_SIZE = len; |
|
||||||
entity.DOWNLOAD_URL = mDownloadEntity.isRedirect() ? mDownloadEntity.getRedirectUrl() |
|
||||||
: mDownloadEntity.getDownloadUrl(); |
|
||||||
entity.TEMP_FILE = mDownloadFile; |
|
||||||
entity.THREAD_ID = 0; |
|
||||||
entity.START_LOCATION = 0; |
|
||||||
entity.END_LOCATION = entity.FILE_SIZE; |
|
||||||
entity.CONFIG_FILE_PATH = mConfigFile.getPath(); |
|
||||||
entity.IS_SUPPORT_BREAK_POINT = isSupportBreakpoint; |
|
||||||
entity.DOWNLOAD_TASK_ENTITY = mDownloadTaskEntity; |
|
||||||
THREAD_NUM = 1; |
|
||||||
CONSTANCE.THREAD_NUM = THREAD_NUM; |
|
||||||
SingleThreadTask task = new SingleThreadTask(CONSTANCE, mListener, entity); |
|
||||||
mTask.put(0, task); |
|
||||||
mFixedThreadPool.execute(task); |
|
||||||
mListener.onPostPre(len); |
|
||||||
mListener.onStart(0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 创建配置文件 |
|
||||||
*/ |
|
||||||
private Properties createConfigFile(long fileLength) throws IOException { |
|
||||||
Properties pro = null; |
|
||||||
//必须建一个文件
|
|
||||||
CommonUtil.createFile(mDownloadFile.getPath()); |
|
||||||
BufferedRandomAccessFile file = |
|
||||||
new BufferedRandomAccessFile(new File(mDownloadFile.getPath()), "rwd", 8192); |
|
||||||
//设置文件长度
|
|
||||||
file.setLength(fileLength); |
|
||||||
file.close(); |
|
||||||
mListener.onPostPre(fileLength); |
|
||||||
//分配每条线程的下载区间
|
|
||||||
pro = CommonUtil.loadConfig(mConfigFile); |
|
||||||
if (pro.isEmpty()) { |
|
||||||
handleNewTask(fileLength); |
|
||||||
} else { |
|
||||||
Set<Object> keys = pro.keySet(); |
|
||||||
int num = 0; |
|
||||||
for (Object key : keys) { |
|
||||||
if (String.valueOf(key).contains("_record_")) { |
|
||||||
num++; |
|
||||||
} |
|
||||||
} |
|
||||||
if (num == 0) { |
|
||||||
handleNewTask(fileLength); |
|
||||||
return pro; |
|
||||||
} |
|
||||||
THREAD_NUM = num; |
|
||||||
for (int i = 0; i < THREAD_NUM; i++) { |
|
||||||
if (pro.getProperty(mDownloadFile.getName() + "_record_" + i) == null) { |
|
||||||
Object state = pro.getProperty(mDownloadFile.getName() + "_state_" + i); |
|
||||||
if (state != null && Integer.parseInt(state + "") == 1) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
handleNewTask(fileLength); |
|
||||||
return pro; |
|
||||||
} |
|
||||||
} |
|
||||||
isNewTask = false; |
|
||||||
} |
|
||||||
return pro; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 处理新任务 |
|
||||||
*/ |
|
||||||
private void handleNewTask(long fileLength) { |
|
||||||
isNewTask = true; |
|
||||||
THREAD_NUM = fileLength < SUB_LEN ? 1 |
|
||||||
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 恢复记录地址 |
|
||||||
* |
|
||||||
* @return true 表示下载完成 |
|
||||||
*/ |
|
||||||
private boolean resumeRecordLocation(int i, long startL, long endL) { |
|
||||||
CONSTANCE.CURRENT_LOCATION += endL - startL; |
|
||||||
Log.d(TAG, "++++++++++ 线程_" + i + "_已经下载完成 ++++++++++"); |
|
||||||
CONSTANCE.COMPLETE_THREAD_NUM++; |
|
||||||
CONSTANCE.STOP_NUM++; |
|
||||||
CONSTANCE.CANCEL_NUM++; |
|
||||||
if (CONSTANCE.isComplete()) { |
|
||||||
if (mConfigFile.exists()) { |
|
||||||
mConfigFile.delete(); |
|
||||||
} |
|
||||||
mListener.onComplete(); |
|
||||||
CONSTANCE.isDownloading = false; |
|
||||||
return true; |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 创建单线程任务 |
|
||||||
*/ |
|
||||||
private void addSingleTask(int i, long startL, long endL, long fileLength) { |
|
||||||
ChildThreadConfigEntity entity = new ChildThreadConfigEntity(); |
|
||||||
entity.FILE_SIZE = fileLength; |
|
||||||
entity.DOWNLOAD_URL = mDownloadEntity.isRedirect() ? mDownloadEntity.getRedirectUrl() |
|
||||||
: mDownloadEntity.getDownloadUrl(); |
|
||||||
entity.TEMP_FILE = mDownloadFile; |
|
||||||
entity.THREAD_ID = i; |
|
||||||
entity.START_LOCATION = startL; |
|
||||||
entity.END_LOCATION = endL; |
|
||||||
entity.CONFIG_FILE_PATH = mConfigFile.getPath(); |
|
||||||
entity.IS_SUPPORT_BREAK_POINT = isSupportBreakpoint; |
|
||||||
entity.DOWNLOAD_TASK_ENTITY = mDownloadTaskEntity; |
|
||||||
CONSTANCE.THREAD_NUM = THREAD_NUM; |
|
||||||
SingleThreadTask task = new SingleThreadTask(CONSTANCE, mListener, entity); |
|
||||||
mTask.put(i, task); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 启动单线程下载任务 |
|
||||||
*/ |
|
||||||
private void startSingleTask(int[] recordL) { |
|
||||||
if (CONSTANCE.CURRENT_LOCATION > 0) { |
|
||||||
mListener.onResume(CONSTANCE.CURRENT_LOCATION); |
|
||||||
} else { |
|
||||||
mListener.onStart(CONSTANCE.CURRENT_LOCATION); |
|
||||||
} |
|
||||||
mFixedThreadPool = Executors.newFixedThreadPool(recordL.length); |
|
||||||
for (int l : recordL) { |
|
||||||
if (l == -1) continue; |
|
||||||
Runnable task = mTask.get(l); |
|
||||||
if (task != null) { |
|
||||||
mFixedThreadPool.execute(task); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 子线程下载信息类 |
|
||||||
*/ |
|
||||||
final static class ChildThreadConfigEntity { |
|
||||||
//线程Id
|
|
||||||
int THREAD_ID; |
|
||||||
//下载文件大小
|
|
||||||
long FILE_SIZE; |
|
||||||
//子线程启动下载位置
|
|
||||||
long START_LOCATION; |
|
||||||
//子线程结束下载位置
|
|
||||||
long END_LOCATION; |
|
||||||
//下载路径
|
|
||||||
File TEMP_FILE; |
|
||||||
String DOWNLOAD_URL; |
|
||||||
String CONFIG_FILE_PATH; |
|
||||||
DownloadTaskEntity DOWNLOAD_TASK_ENTITY; |
|
||||||
boolean IS_SUPPORT_BREAK_POINT = true; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,24 @@ |
|||||||
|
package com.arialyy.aria.core.download.downloader; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
/** |
||||||
|
* 子线程下载信息类 |
||||||
|
*/ |
||||||
|
final class ChildThreadConfigEntity { |
||||||
|
//线程Id
|
||||||
|
int THREAD_ID; |
||||||
|
//下载文件大小
|
||||||
|
long FILE_SIZE; |
||||||
|
//子线程启动下载位置
|
||||||
|
long START_LOCATION; |
||||||
|
//子线程结束下载位置
|
||||||
|
long END_LOCATION; |
||||||
|
//下载路径
|
||||||
|
File TEMP_FILE; |
||||||
|
String DOWNLOAD_URL; |
||||||
|
String CONFIG_FILE_PATH; |
||||||
|
DownloadTaskEntity DOWNLOAD_TASK_ENTITY; |
||||||
|
boolean IS_SUPPORT_BREAK_POINT = true; |
||||||
|
} |
@ -0,0 +1,312 @@ |
|||||||
|
/* |
||||||
|
* 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.downloader; |
||||||
|
|
||||||
|
import android.util.SparseArray; |
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.core.inf.IEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import java.io.File; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
import java.util.Timer; |
||||||
|
import java.util.TimerTask; |
||||||
|
import java.util.concurrent.ExecutorService; |
||||||
|
import java.util.concurrent.Executors; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/30. |
||||||
|
* 任务组下载工具 |
||||||
|
*/ |
||||||
|
public class DownloadGroupUtil implements IDownloadUtil { |
||||||
|
private static final String TAG = "DownloadGroupUtil"; |
||||||
|
/** |
||||||
|
* 任务组所有任务总大小 |
||||||
|
*/ |
||||||
|
private long mTotalSize = 0; |
||||||
|
private long mCurrentProgress = 0; |
||||||
|
private ExecutorService mInfoPool; |
||||||
|
private ExecutorService mExePool; |
||||||
|
private IDownloadListener mListener; |
||||||
|
private DownloadGroupTaskEntity mTaskEntity; |
||||||
|
private boolean isRunning = true; |
||||||
|
private Timer mTimer; |
||||||
|
/** |
||||||
|
* 初始化完成的任务书数 |
||||||
|
*/ |
||||||
|
private int mInitNum = 0; |
||||||
|
/** |
||||||
|
* 初始化失败的任务数 |
||||||
|
*/ |
||||||
|
private int mFailNum = 0; |
||||||
|
/** |
||||||
|
* 保存所有没有下载完成的任务,key为下载地址 |
||||||
|
*/ |
||||||
|
private Map<String, DownloadTaskEntity> mExeMap = new HashMap<>(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 下载失败的映射表,key为下载地址 |
||||||
|
*/ |
||||||
|
private Map<String, DownloadTaskEntity> mFailMap = new HashMap<>(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 下载器映射表,key为下载地址 |
||||||
|
*/ |
||||||
|
private Map<String, Downloader> mDownloaderMap = new HashMap<>(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 文件信息回调组 |
||||||
|
*/ |
||||||
|
private SparseArray<FileInfoThread.OnFileInfoCallback> mFileInfoCallbacks = new SparseArray<>(); |
||||||
|
|
||||||
|
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) { |
||||||
|
mListener = listener; |
||||||
|
mTaskEntity = taskEntity; |
||||||
|
mInfoPool = Executors.newCachedThreadPool(); |
||||||
|
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
||||||
|
|
||||||
|
for (DownloadEntity entity : mTaskEntity.entity.getChild()) { |
||||||
|
File file = new File(entity.getDownloadPath()); |
||||||
|
if (entity.isDownloadComplete() && file.exists()) { |
||||||
|
mTotalSize += entity.getFileSize(); |
||||||
|
} else { |
||||||
|
mExeMap.put(entity.getDownloadUrl(), createDownloadTask(entity)); |
||||||
|
} |
||||||
|
mCurrentProgress += entity.getCurrentProgress(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public long getFileSize() { |
||||||
|
return mTotalSize; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public long getCurrentLocation() { |
||||||
|
return mCurrentProgress; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isDownloading() { |
||||||
|
return isRunning; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void cancelDownload() { |
||||||
|
isRunning = false; |
||||||
|
closeTimer(); |
||||||
|
if (!mInfoPool.isShutdown()) { |
||||||
|
mInfoPool.shutdown(); |
||||||
|
} |
||||||
|
if (!mExePool.isShutdown()) { |
||||||
|
mExePool.shutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
Set<String> keys = mDownloaderMap.keySet(); |
||||||
|
for (String key : keys) { |
||||||
|
Downloader dt = mDownloaderMap.get(key); |
||||||
|
if (dt != null) { |
||||||
|
dt.cancelDownload(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void stopDownload() { |
||||||
|
isRunning = false; |
||||||
|
closeTimer(); |
||||||
|
if (!mInfoPool.isShutdown()) { |
||||||
|
mInfoPool.shutdown(); |
||||||
|
} |
||||||
|
if (!mExePool.isShutdown()) { |
||||||
|
mExePool.shutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
Set<String> keys = mDownloaderMap.keySet(); |
||||||
|
for (String key : keys) { |
||||||
|
Downloader dt = mDownloaderMap.get(key); |
||||||
|
if (dt != null) { |
||||||
|
dt.stopDownload(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void startDownload() { |
||||||
|
isRunning = true; |
||||||
|
Set<String> keys = mExeMap.keySet(); |
||||||
|
mListener.onPre(); |
||||||
|
for (String key : keys) { |
||||||
|
DownloadTaskEntity taskEntity = mExeMap.get(key); |
||||||
|
if (taskEntity != null) { |
||||||
|
mInfoPool.execute(createFileInfoThread(taskEntity)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void resumeDownload() { |
||||||
|
startDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建文件信息获取线程 |
||||||
|
*/ |
||||||
|
private FileInfoThread createFileInfoThread(DownloadTaskEntity taskEntity) { |
||||||
|
FileInfoThread.OnFileInfoCallback callback = mFileInfoCallbacks.get(taskEntity.hashCode()); |
||||||
|
|
||||||
|
if (callback == null) { |
||||||
|
callback = new FileInfoThread.OnFileInfoCallback() { |
||||||
|
int failNum = 0; |
||||||
|
|
||||||
|
@Override public void onComplete(String url, int code) { |
||||||
|
DownloadTaskEntity te = mExeMap.get(url); |
||||||
|
if (te != null) { |
||||||
|
mTotalSize += te.getEntity().getFileSize(); |
||||||
|
startChildDownload(te); |
||||||
|
} |
||||||
|
mInitNum++; |
||||||
|
if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) { |
||||||
|
startRunningFlow(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onFail(String url, String errorMsg) { |
||||||
|
DownloadTaskEntity te = mExeMap.get(url); |
||||||
|
if (te != null) { |
||||||
|
mFailMap.put(url, te); |
||||||
|
mFileInfoCallbacks.put(te.hashCode(), this); |
||||||
|
} |
||||||
|
mFailNum++; |
||||||
|
failNum++; |
||||||
|
if (failNum < 10) { |
||||||
|
mInfoPool.execute(createFileInfoThread(te)); |
||||||
|
} |
||||||
|
if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) { |
||||||
|
startRunningFlow(); |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
return new FileInfoThread(taskEntity, callback); |
||||||
|
} |
||||||
|
|
||||||
|
private void closeTimer() { |
||||||
|
if (mTimer != null) { |
||||||
|
mTimer.purge(); |
||||||
|
mTimer.cancel(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 开始进度流程 |
||||||
|
*/ |
||||||
|
private void startRunningFlow() { |
||||||
|
mListener.onPostPre(mTotalSize); |
||||||
|
mListener.onStart(mCurrentProgress); |
||||||
|
closeTimer(); |
||||||
|
mTimer = new Timer(true); |
||||||
|
mTimer.schedule(new TimerTask() { |
||||||
|
@Override public void run() { |
||||||
|
mListener.onProgress(mCurrentProgress); |
||||||
|
} |
||||||
|
}, 1000); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 启动子任务下载器 |
||||||
|
*/ |
||||||
|
private void startChildDownload(DownloadTaskEntity taskEntity) { |
||||||
|
ChildDownloadListener listener = new ChildDownloadListener(taskEntity); |
||||||
|
Downloader dt = new Downloader(listener, taskEntity); |
||||||
|
mDownloaderMap.put(taskEntity.getEntity().getDownloadUrl(), dt); |
||||||
|
mExePool.execute(dt); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建子任务下载信息 |
||||||
|
*/ |
||||||
|
private DownloadTaskEntity createDownloadTask(DownloadEntity entity) { |
||||||
|
DownloadTaskEntity taskEntity = |
||||||
|
DbEntity.findData(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl()); |
||||||
|
if (taskEntity != null) { |
||||||
|
return taskEntity; |
||||||
|
} |
||||||
|
taskEntity = new DownloadTaskEntity(); |
||||||
|
taskEntity.entity = entity; |
||||||
|
taskEntity.headers = mTaskEntity.headers; |
||||||
|
taskEntity.requestEnum = mTaskEntity.requestEnum; |
||||||
|
taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey; |
||||||
|
taskEntity.removeFile = mTaskEntity.removeFile; |
||||||
|
return taskEntity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 子任务事件监听 |
||||||
|
*/ |
||||||
|
private class ChildDownloadListener extends DownloadListener { |
||||||
|
|
||||||
|
DownloadTaskEntity taskEntity; |
||||||
|
DownloadEntity entity; |
||||||
|
|
||||||
|
ChildDownloadListener(DownloadTaskEntity entity) { |
||||||
|
this.taskEntity = entity; |
||||||
|
this.entity = taskEntity.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onPre() { |
||||||
|
saveData(IEntity.STATE_PRE, -1); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onPostPre(long fileSize) { |
||||||
|
entity.setFileSize(fileSize); |
||||||
|
saveData(IEntity.STATE_POST_PRE, -1); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onResume(long resumeLocation) { |
||||||
|
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onStart(long startLocation) { |
||||||
|
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onProgress(long currentLocation) { |
||||||
|
mCurrentProgress += currentLocation; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onStop(long stopLocation) { |
||||||
|
saveData(IEntity.STATE_STOP, stopLocation); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onCancel() { |
||||||
|
saveData(IEntity.STATE_CANCEL, -1); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onComplete() { |
||||||
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onFail() { |
||||||
|
entity.setFailNum(entity.getFailNum() + 1); |
||||||
|
saveData(IEntity.STATE_FAIL, -1); |
||||||
|
} |
||||||
|
|
||||||
|
private void saveData(int state, long location) { |
||||||
|
entity.setState(state); |
||||||
|
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE); |
||||||
|
entity.setCurrentProgress(location); |
||||||
|
entity.update(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,99 @@ |
|||||||
|
/* |
||||||
|
* 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.downloader; |
||||||
|
|
||||||
|
import android.util.Log; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2015/8/25. |
||||||
|
* 下载工具类 |
||||||
|
*/ |
||||||
|
public class DownloadUtil implements IDownloadUtil, Runnable { |
||||||
|
private static final String TAG = "DownloadUtil"; |
||||||
|
private IDownloadListener mListener; |
||||||
|
private Downloader mDT; |
||||||
|
private DownloadTaskEntity mTaskEntity; |
||||||
|
|
||||||
|
public DownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) { |
||||||
|
mTaskEntity = entity; |
||||||
|
mListener = downloadListener; |
||||||
|
mDT = new Downloader(downloadListener, entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public long getFileSize() { |
||||||
|
return mDT.getFileSize(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取当前下载位置 |
||||||
|
*/ |
||||||
|
@Override public long getCurrentLocation() { |
||||||
|
return mDT.getCurrentLocation(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isDownloading() { |
||||||
|
return mDT.isDownloading(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 取消下载 |
||||||
|
*/ |
||||||
|
@Override public void cancelDownload() { |
||||||
|
mDT.cancelDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 停止下载 |
||||||
|
*/ |
||||||
|
@Override public void stopDownload() { |
||||||
|
mDT.stopDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 多线程断点续传下载文件,开始下载 |
||||||
|
*/ |
||||||
|
@Override public void startDownload() { |
||||||
|
mListener.onPre(); |
||||||
|
new Thread(this).start(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void resumeDownload() { |
||||||
|
startDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setMaxSpeed(double maxSpeed) { |
||||||
|
mDT.setMaxSpeed(maxSpeed); |
||||||
|
} |
||||||
|
|
||||||
|
private void failDownload(String msg) { |
||||||
|
Log.e(TAG, msg); |
||||||
|
mListener.onFail(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void run() { |
||||||
|
new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() { |
||||||
|
@Override public void onComplete(String url, int code) { |
||||||
|
mDT.startDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onFail(String url, String errorMsg) { |
||||||
|
failDownload(errorMsg); |
||||||
|
} |
||||||
|
})).start(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,376 @@ |
|||||||
|
/* |
||||||
|
* 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.downloader; |
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.util.Log; |
||||||
|
import android.util.SparseArray; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.BufferedRandomAccessFile; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Properties; |
||||||
|
import java.util.Set; |
||||||
|
import java.util.concurrent.ExecutorService; |
||||||
|
import java.util.concurrent.Executors; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/7/1. |
||||||
|
* 文件下载器 |
||||||
|
*/ |
||||||
|
class Downloader implements Runnable, IDownloadUtil { |
||||||
|
private final String TAG = "Downloader"; |
||||||
|
private IDownloadListener mListener; |
||||||
|
private DownloadTaskEntity mTaskEntity; |
||||||
|
private DownloadEntity mEntity; |
||||||
|
private ExecutorService mFixedThreadPool; |
||||||
|
private File mConfigFile;//下载信息配置文件
|
||||||
|
private Context mContext; |
||||||
|
private File mTempFile; //下载的文件
|
||||||
|
private boolean isNewTask = true; |
||||||
|
private int mThreadNum, mRealThreadNum; |
||||||
|
private StateConstance mConstance; |
||||||
|
private SparseArray<Runnable> mTask = new SparseArray<>(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 小于1m的文件不启用多线程 |
||||||
|
*/ |
||||||
|
private static final long SUB_LEN = 1024 * 1024; |
||||||
|
|
||||||
|
Downloader(IDownloadListener listener, DownloadTaskEntity taskEntity) { |
||||||
|
mListener = listener; |
||||||
|
mTaskEntity = taskEntity; |
||||||
|
mEntity = mTaskEntity.getEntity(); |
||||||
|
mContext = AriaManager.APP; |
||||||
|
mConstance = new StateConstance(); |
||||||
|
} |
||||||
|
|
||||||
|
void setMaxSpeed(double maxSpeed) { |
||||||
|
for (int i = 0; i < mThreadNum; i++) { |
||||||
|
SingleThreadTask task = (SingleThreadTask) mTask.get(i); |
||||||
|
if (task != null) { |
||||||
|
task.setMaxSpeed(maxSpeed); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public StateConstance getConstance() { |
||||||
|
return mConstance; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void run() { |
||||||
|
startFlow(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 开始下载流程 |
||||||
|
*/ |
||||||
|
private void startFlow(){ |
||||||
|
checkTask(); |
||||||
|
mConstance.cleanState(); |
||||||
|
mConstance.isDownloading = true; |
||||||
|
try { |
||||||
|
if (!mTaskEntity.isSupportBP) { |
||||||
|
mThreadNum = 1; |
||||||
|
handleNoSupportBreakpointDownload(); |
||||||
|
} else { |
||||||
|
mThreadNum = isNewTask ? (mEntity.getFileSize() <= SUB_LEN ? 1 |
||||||
|
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum()) |
||||||
|
: mRealThreadNum; |
||||||
|
mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum); |
||||||
|
handleBreakpoint(); |
||||||
|
} |
||||||
|
} catch (IOException e) { |
||||||
|
failDownload("下载失败【downloadUrl:" |
||||||
|
+ mEntity.getDownloadUrl() |
||||||
|
+ "】\n【filePath:" |
||||||
|
+ mEntity.getDownloadPath() |
||||||
|
+ "】\n" |
||||||
|
+ CommonUtil.getPrintException(e)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public long getFileSize() { |
||||||
|
return mEntity.getFileSize(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取当前下载位置 |
||||||
|
*/ |
||||||
|
@Override public long getCurrentLocation() { |
||||||
|
return mConstance.CURRENT_LOCATION; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isDownloading() { |
||||||
|
return mConstance.isDownloading; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void cancelDownload() { |
||||||
|
mConstance.isCancel = true; |
||||||
|
mConstance.isDownloading = false; |
||||||
|
if (mFixedThreadPool != null) { |
||||||
|
mFixedThreadPool.shutdown(); |
||||||
|
} |
||||||
|
for (int i = 0; i < mThreadNum; i++) { |
||||||
|
SingleThreadTask task = (SingleThreadTask) mTask.get(i); |
||||||
|
if (task != null) { |
||||||
|
task.cancel(); |
||||||
|
} |
||||||
|
} |
||||||
|
CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, mEntity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void stopDownload() { |
||||||
|
mConstance.isStop = true; |
||||||
|
mConstance.isDownloading = false; |
||||||
|
if (mFixedThreadPool != null) { |
||||||
|
mFixedThreadPool.shutdown(); |
||||||
|
} |
||||||
|
for (int i = 0; i < mThreadNum; i++) { |
||||||
|
SingleThreadTask task = (SingleThreadTask) mTask.get(i); |
||||||
|
if (task != null) { |
||||||
|
task.stop(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 直接调用的时候会自动启动线程执行 |
||||||
|
*/ |
||||||
|
@Override public void startDownload() { |
||||||
|
new Thread(this).start(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void resumeDownload() { |
||||||
|
startDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 返回该下载器的 |
||||||
|
*/ |
||||||
|
public IDownloadListener getListener() { |
||||||
|
return mListener; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查任务是否是新任务,新任务条件: |
||||||
|
* 1、文件不存在 |
||||||
|
* 2、下载记录文件不存在 |
||||||
|
* 3、下载记录文件缺失或不匹配 |
||||||
|
* 4、数据库记录不存在 |
||||||
|
* 5、不支持断点,则是新任务 |
||||||
|
*/ |
||||||
|
private void checkTask() { |
||||||
|
if (!mTaskEntity.isSupportBP) { |
||||||
|
isNewTask = true; |
||||||
|
return; |
||||||
|
} |
||||||
|
mConfigFile = new File(mContext.getFilesDir().getPath() |
||||||
|
+ AriaManager.DOWNLOAD_TEMP_DIR |
||||||
|
+ mEntity.getFileName() |
||||||
|
+ ".properties"); |
||||||
|
mTempFile = new File(mEntity.getDownloadPath()); |
||||||
|
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
|
||||||
|
isNewTask = true; |
||||||
|
CommonUtil.createFile(mConfigFile.getPath()); |
||||||
|
} else if (!mTempFile.exists()) { |
||||||
|
isNewTask = true; |
||||||
|
} else if (DbEntity.findData(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl()) |
||||||
|
== null) { |
||||||
|
isNewTask = true; |
||||||
|
} else { |
||||||
|
isNewTask = checkConfigFile(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查记录文件,如果是新任务返回{@code true},否则返回{@code false} |
||||||
|
*/ |
||||||
|
private boolean checkConfigFile() { |
||||||
|
Properties pro = CommonUtil.loadConfig(mConfigFile); |
||||||
|
if (pro.isEmpty()) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
Set<Object> keys = pro.keySet(); |
||||||
|
int num = 0; |
||||||
|
for (Object key : keys) { |
||||||
|
if (String.valueOf(key).contains("_record_")) { |
||||||
|
num++; |
||||||
|
} |
||||||
|
} |
||||||
|
if (num == 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
mRealThreadNum = num; |
||||||
|
for (int i = 0; i < mRealThreadNum; i++) { |
||||||
|
if (pro.getProperty(mTempFile.getName() + "_record_" + i) == null) { |
||||||
|
Object state = pro.getProperty(mTempFile.getName() + "_state_" + i); |
||||||
|
if (state != null && Integer.parseInt(state + "") == 1) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 恢复记录地址 |
||||||
|
* |
||||||
|
* @return true 表示下载完成 |
||||||
|
*/ |
||||||
|
private boolean resumeRecordLocation(int i, long startL, long endL) { |
||||||
|
mConstance.CURRENT_LOCATION += endL - startL; |
||||||
|
Log.d(TAG, "++++++++++ 线程_" + i + "_已经下载完成 ++++++++++"); |
||||||
|
mConstance.COMPLETE_THREAD_NUM++; |
||||||
|
mConstance.STOP_NUM++; |
||||||
|
mConstance.CANCEL_NUM++; |
||||||
|
if (mConstance.isComplete()) { |
||||||
|
if (mConfigFile.exists()) { |
||||||
|
mConfigFile.delete(); |
||||||
|
} |
||||||
|
mListener.onComplete(); |
||||||
|
mConstance.isDownloading = false; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建单线程任务 |
||||||
|
*/ |
||||||
|
private void addSingleTask(int i, long startL, long endL, long fileLength) { |
||||||
|
ChildThreadConfigEntity entity = new ChildThreadConfigEntity(); |
||||||
|
entity.FILE_SIZE = fileLength; |
||||||
|
entity.DOWNLOAD_URL = |
||||||
|
mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getDownloadUrl(); |
||||||
|
entity.TEMP_FILE = mTempFile; |
||||||
|
entity.THREAD_ID = i; |
||||||
|
entity.START_LOCATION = startL; |
||||||
|
entity.END_LOCATION = endL; |
||||||
|
entity.CONFIG_FILE_PATH = mConfigFile.getPath(); |
||||||
|
entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP; |
||||||
|
entity.DOWNLOAD_TASK_ENTITY = mTaskEntity; |
||||||
|
mConstance.THREAD_NUM = mThreadNum; |
||||||
|
SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); |
||||||
|
mTask.put(i, task); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 启动单线程下载任务 |
||||||
|
*/ |
||||||
|
private void startSingleTask(int[] recordL) { |
||||||
|
if (mConstance.CURRENT_LOCATION > 0) { |
||||||
|
mListener.onResume(mConstance.CURRENT_LOCATION); |
||||||
|
} else { |
||||||
|
mListener.onStart(mConstance.CURRENT_LOCATION); |
||||||
|
} |
||||||
|
mFixedThreadPool = Executors.newFixedThreadPool(recordL.length); |
||||||
|
for (int l : recordL) { |
||||||
|
if (l == -1) continue; |
||||||
|
Runnable task = mTask.get(l); |
||||||
|
if (task != null) { |
||||||
|
mFixedThreadPool.execute(task); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理断点 |
||||||
|
*/ |
||||||
|
private void handleBreakpoint() throws IOException { |
||||||
|
long fileLength = mEntity.getFileSize(); |
||||||
|
Properties pro = CommonUtil.loadConfig(mConfigFile); |
||||||
|
int blockSize = (int) (fileLength / mThreadNum); |
||||||
|
int[] recordL = new int[mThreadNum]; |
||||||
|
for (int i = 0; i < mThreadNum; i++) { |
||||||
|
recordL[i] = -1; |
||||||
|
} |
||||||
|
int rl = 0; |
||||||
|
if (isNewTask) { |
||||||
|
CommonUtil.createFile(mTempFile.getPath()); |
||||||
|
BufferedRandomAccessFile file = |
||||||
|
new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192); |
||||||
|
//设置文件长度
|
||||||
|
file.setLength(fileLength); |
||||||
|
file.close(); |
||||||
|
} |
||||||
|
|
||||||
|
for (int i = 0; i < mThreadNum; i++) { |
||||||
|
long startL = i * blockSize, endL = (i + 1) * blockSize; |
||||||
|
Object state = pro.getProperty(mTempFile.getName() + "_state_" + i); |
||||||
|
if (state != null && Integer.parseInt(state + "") == 1) { //该线程已经完成
|
||||||
|
if (resumeRecordLocation(i, startL, endL)) return; |
||||||
|
continue; |
||||||
|
} |
||||||
|
//分配下载位置
|
||||||
|
Object record = pro.getProperty(mTempFile.getName() + "_record_" + i); |
||||||
|
//如果有记录,则恢复下载
|
||||||
|
if (!isNewTask && record != null && Long.parseLong(record + "") >= 0) { |
||||||
|
Long r = Long.parseLong(record + ""); |
||||||
|
mConstance.CURRENT_LOCATION += r - startL; |
||||||
|
Log.d(TAG, "任务【" + mEntity.getFileName() + "】线程__" + i + "__恢复下载"); |
||||||
|
mListener.onChildResume(r); |
||||||
|
startL = r; |
||||||
|
recordL[rl] = i; |
||||||
|
rl++; |
||||||
|
} else { |
||||||
|
recordL[rl] = i; |
||||||
|
rl++; |
||||||
|
} |
||||||
|
if (i == (mThreadNum - 1)) { |
||||||
|
//最后一个线程的结束位置即为文件的总长度
|
||||||
|
endL = fileLength; |
||||||
|
} |
||||||
|
addSingleTask(i, startL, endL, fileLength); |
||||||
|
} |
||||||
|
startSingleTask(recordL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理不支持断点的下载 |
||||||
|
*/ |
||||||
|
private void handleNoSupportBreakpointDownload() { |
||||||
|
ChildThreadConfigEntity entity = new ChildThreadConfigEntity(); |
||||||
|
long len = mEntity.getFileSize(); |
||||||
|
entity.FILE_SIZE = len; |
||||||
|
entity.DOWNLOAD_URL = |
||||||
|
mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getDownloadUrl(); |
||||||
|
entity.TEMP_FILE = mTempFile; |
||||||
|
entity.THREAD_ID = 0; |
||||||
|
entity.START_LOCATION = 0; |
||||||
|
entity.END_LOCATION = entity.FILE_SIZE; |
||||||
|
entity.CONFIG_FILE_PATH = mConfigFile.getPath(); |
||||||
|
entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP; |
||||||
|
entity.DOWNLOAD_TASK_ENTITY = mTaskEntity; |
||||||
|
mConstance.THREAD_NUM = mThreadNum; |
||||||
|
SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); |
||||||
|
mTask.put(0, task); |
||||||
|
mFixedThreadPool.execute(task); |
||||||
|
mListener.onPostPre(len); |
||||||
|
mListener.onStart(0); |
||||||
|
} |
||||||
|
|
||||||
|
private void failDownload(String errorMsg) { |
||||||
|
Log.e(TAG, errorMsg); |
||||||
|
mConstance.isDownloading = false; |
||||||
|
mListener.onFail(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,161 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download.downloader; |
||||||
|
|
||||||
|
import android.util.Log; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.io.IOException; |
||||||
|
import java.net.HttpURLConnection; |
||||||
|
import java.net.URL; |
||||||
|
|
||||||
|
/** |
||||||
|
* 下载文件信息获取 |
||||||
|
*/ |
||||||
|
class FileInfoThread implements Runnable { |
||||||
|
private final String TAG = "FileInfoThread"; |
||||||
|
private DownloadEntity mEntity; |
||||||
|
private DownloadTaskEntity mTaskEntity; |
||||||
|
private int mConnectTimeOut; |
||||||
|
private OnFileInfoCallback onFileInfoListener; |
||||||
|
|
||||||
|
interface OnFileInfoCallback { |
||||||
|
/** |
||||||
|
* 处理完成 |
||||||
|
* |
||||||
|
* @param code 状态码 |
||||||
|
*/ |
||||||
|
void onComplete(String url, int code); |
||||||
|
|
||||||
|
/** |
||||||
|
* 请求失败 |
||||||
|
* |
||||||
|
* @param errorMsg 错误信息 |
||||||
|
*/ |
||||||
|
void onFail(String url, String errorMsg); |
||||||
|
} |
||||||
|
|
||||||
|
FileInfoThread(DownloadTaskEntity taskEntity) { |
||||||
|
this(taskEntity, null); |
||||||
|
} |
||||||
|
|
||||||
|
FileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) { |
||||||
|
this.mTaskEntity = taskEntity; |
||||||
|
mEntity = taskEntity.getEntity(); |
||||||
|
mConnectTimeOut = |
||||||
|
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getConnectTimeOut(); |
||||||
|
onFileInfoListener = callback; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void run() { |
||||||
|
HttpURLConnection conn = null; |
||||||
|
try { |
||||||
|
URL url = new URL(mEntity.getDownloadUrl()); |
||||||
|
conn = ConnectionHelp.handleConnection(url); |
||||||
|
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn); |
||||||
|
conn.setRequestProperty("Range", "bytes=" + 0 + "-"); |
||||||
|
conn.setConnectTimeout(mConnectTimeOut); |
||||||
|
conn.connect(); |
||||||
|
handleConnect(conn); |
||||||
|
} catch (IOException e) { |
||||||
|
failDownload("下载失败【downloadUrl:" |
||||||
|
+ mEntity.getDownloadUrl() |
||||||
|
+ "】\n【filePath:" |
||||||
|
+ mEntity.getDownloadPath() |
||||||
|
+ "】\n" |
||||||
|
+ CommonUtil.getPrintException(e)); |
||||||
|
} finally { |
||||||
|
if (conn != null) { |
||||||
|
conn.disconnect(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void handleConnect(HttpURLConnection conn) throws IOException { |
||||||
|
int len = conn.getContentLength(); |
||||||
|
int code = conn.getResponseCode(); |
||||||
|
boolean isComplete = false; |
||||||
|
mTaskEntity.code = code; |
||||||
|
if (code == HttpURLConnection.HTTP_PARTIAL) { |
||||||
|
if (!checkLen(len)) return; |
||||||
|
mEntity.setFileSize(len); |
||||||
|
mTaskEntity.isSupportBP = true; |
||||||
|
isComplete = true; |
||||||
|
} else if (code == HttpURLConnection.HTTP_OK) { |
||||||
|
if (!checkLen(len)) return; |
||||||
|
mEntity.setFileSize(len); |
||||||
|
mTaskEntity.isSupportBP = false; |
||||||
|
isComplete = true; |
||||||
|
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) { |
||||||
|
failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,错误码:404"); |
||||||
|
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP |
||||||
|
|| code == HttpURLConnection.HTTP_MOVED_PERM |
||||||
|
|| code == HttpURLConnection.HTTP_SEE_OTHER) { |
||||||
|
mTaskEntity.redirectUrlKey = conn.getHeaderField(mTaskEntity.redirectUrlKey); |
||||||
|
mEntity.setRedirect(true); |
||||||
|
mEntity.setRedirectUrl(mTaskEntity.redirectUrlKey); |
||||||
|
handle302Turn(conn); |
||||||
|
} else { |
||||||
|
failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,错误码:" + code); |
||||||
|
} |
||||||
|
if (isComplete) { |
||||||
|
if (onFileInfoListener != null) { |
||||||
|
onFileInfoListener.onComplete(mEntity.getDownloadUrl(), code); |
||||||
|
} |
||||||
|
mEntity.save(); |
||||||
|
mTaskEntity.save(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理30x跳转 |
||||||
|
*/ |
||||||
|
private void handle302Turn(HttpURLConnection conn) throws IOException { |
||||||
|
String newUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey); |
||||||
|
Log.d(TAG, "30x跳转,新url为【" + newUrl + "】"); |
||||||
|
String cookies = conn.getHeaderField("Set-Cookie"); |
||||||
|
conn = (HttpURLConnection) new URL(newUrl).openConnection(); |
||||||
|
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn); |
||||||
|
conn.setRequestProperty("Cookie", cookies); |
||||||
|
conn.setRequestProperty("Range", "bytes=" + 0 + "-"); |
||||||
|
conn.setConnectTimeout(mConnectTimeOut); |
||||||
|
conn.connect(); |
||||||
|
handleConnect(conn); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查长度是否合法 |
||||||
|
* |
||||||
|
* @param len 从服务器获取的文件长度 |
||||||
|
* @return true, 合法 |
||||||
|
*/ |
||||||
|
private boolean checkLen(long len) { |
||||||
|
if (len < 0) { |
||||||
|
failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,文件长度小于0"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private void failDownload(String errorMsg) { |
||||||
|
Log.e(TAG, errorMsg); |
||||||
|
if (onFileInfoListener != null) { |
||||||
|
onFileInfoListener.onFail(mEntity.getDownloadUrl(), errorMsg); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,71 @@ |
|||||||
|
/* |
||||||
|
* 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.os.Parcel; |
||||||
|
import android.os.Parcelable; |
||||||
|
import com.arialyy.aria.orm.Primary; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/3. |
||||||
|
*/ |
||||||
|
public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { |
||||||
|
/** |
||||||
|
* 组名 |
||||||
|
*/ |
||||||
|
@Primary |
||||||
|
private String groupName = ""; |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务地址相加的urlmd5 |
||||||
|
*/ |
||||||
|
private String urlmd5 = ""; |
||||||
|
|
||||||
|
public String getUrlmd5() { |
||||||
|
return urlmd5; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUrlmd5(String urlmd5) { |
||||||
|
this.urlmd5 = urlmd5; |
||||||
|
} |
||||||
|
|
||||||
|
public String getGroupName() { |
||||||
|
return groupName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setGroupName(String groupName) { |
||||||
|
this.groupName = groupName; |
||||||
|
} |
||||||
|
|
||||||
|
public AbsGroupEntity() { |
||||||
|
} |
||||||
|
|
||||||
|
@Override public int describeContents() { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void writeToParcel(Parcel dest, int flags) { |
||||||
|
super.writeToParcel(dest, flags); |
||||||
|
dest.writeString(this.groupName); |
||||||
|
dest.writeString(this.urlmd5); |
||||||
|
} |
||||||
|
|
||||||
|
protected AbsGroupEntity(Parcel in) { |
||||||
|
super(in); |
||||||
|
this.groupName = in.readString(); |
||||||
|
this.urlmd5 = in.readString(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.inf; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 任务组超类 |
||||||
|
*/ |
||||||
|
public abstract class AbsGroupTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity> |
||||||
|
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> { |
||||||
|
|
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.inf; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
*/ |
||||||
|
public abstract class AbsGroupTask<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsGroupEntity> |
||||||
|
extends AbsTask<ENTITY> { |
||||||
|
|
||||||
|
protected TASK_ENTITY mTaskEntity; |
||||||
|
|
||||||
|
|
||||||
|
@Override public String getKey() { |
||||||
|
return mEntity.getGroupName(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,73 @@ |
|||||||
|
/* |
||||||
|
* 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.os.Parcel; |
||||||
|
import android.os.Parcelable; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.orm.Ignore; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/3. |
||||||
|
*/ |
||||||
|
public abstract class AbsNormalEntity extends AbsEntity implements Parcelable { |
||||||
|
|
||||||
|
/** |
||||||
|
* 文件名 |
||||||
|
*/ |
||||||
|
private String fileName = ""; |
||||||
|
|
||||||
|
/** |
||||||
|
* 是否是任务组里面的下载实体 |
||||||
|
*/ |
||||||
|
private boolean isGroupChild = false; |
||||||
|
|
||||||
|
|
||||||
|
public boolean isGroupChild() { |
||||||
|
return isGroupChild; |
||||||
|
} |
||||||
|
|
||||||
|
public void setGroupChild(boolean groupChild) { |
||||||
|
isGroupChild = groupChild; |
||||||
|
} |
||||||
|
|
||||||
|
public String getFileName() { |
||||||
|
return fileName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setFileName(String fileName) { |
||||||
|
this.fileName = fileName; |
||||||
|
} |
||||||
|
|
||||||
|
public AbsNormalEntity() { |
||||||
|
} |
||||||
|
|
||||||
|
@Override public int describeContents() { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void writeToParcel(Parcel dest, int flags) { |
||||||
|
super.writeToParcel(dest, flags); |
||||||
|
dest.writeString(this.fileName); |
||||||
|
dest.writeByte(this.isGroupChild ? (byte) 1 : (byte) 0); |
||||||
|
} |
||||||
|
|
||||||
|
protected AbsNormalEntity(Parcel in) { |
||||||
|
super(in); |
||||||
|
this.fileName = in.readString(); |
||||||
|
this.isGroupChild = in.readByte() != 0; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,106 @@ |
|||||||
|
/* |
||||||
|
* 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.text.TextUtils; |
||||||
|
import android.util.Log; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.command.normal.NormalCmdFactory; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/2/28. |
||||||
|
*/ |
||||||
|
public abstract class AbsNormalTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity> |
||||||
|
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> { |
||||||
|
|
||||||
|
/** |
||||||
|
* 将任务设置为最高优先级任务,最高优先级任务有以下特点: |
||||||
|
* 1、在下载队列中,有且只有一个最高优先级任务 |
||||||
|
* 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 |
||||||
|
* 3、任务调度器不会暂停最高优先级任务 |
||||||
|
* 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 |
||||||
|
* 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 |
||||||
|
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 |
||||||
|
*/ |
||||||
|
protected void setHighestPriority() { |
||||||
|
AriaManager.getInstance(AriaManager.APP) |
||||||
|
.setCmd( |
||||||
|
CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_HIGHEST_PRIORITY)) |
||||||
|
.exe(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 重定向后,新url的key,默认为location |
||||||
|
*/ |
||||||
|
protected void _setRedirectUrlKey(String redirectUrlKey) { |
||||||
|
if (TextUtils.isEmpty(redirectUrlKey)) { |
||||||
|
Log.w("AbsNormalTarget", "重定向后,新url的key不能为null"); |
||||||
|
return; |
||||||
|
} |
||||||
|
mTaskEntity.redirectUrlKey = redirectUrlKey; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除记录 |
||||||
|
*/ |
||||||
|
public void removeRecord() { |
||||||
|
mEntity.deleteData(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务文件大小 |
||||||
|
* |
||||||
|
* @return 文件大小 |
||||||
|
*/ |
||||||
|
public long getFileSize() { |
||||||
|
return getSize(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 获取单位转换后的文件大小 |
||||||
|
* |
||||||
|
* @return 文件大小{@code xxx mb} |
||||||
|
*/ |
||||||
|
public String getConvertFileSize() { |
||||||
|
return getConvertSize(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 下载任务是否存在 |
||||||
|
*/ |
||||||
|
public boolean taskExists() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加任务 |
||||||
|
*/ |
||||||
|
public void add() { |
||||||
|
AriaManager.getInstance(AriaManager.APP) |
||||||
|
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CREATE)) |
||||||
|
.exe(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 重新下载 |
||||||
|
*/ |
||||||
|
public void reStart() { |
||||||
|
cancel(); |
||||||
|
start(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.inf; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/6/3. |
||||||
|
*/ |
||||||
|
public abstract class AbsNormalTask<ENTITY extends AbsEntity> extends AbsTask<ENTITY> { |
||||||
|
|
||||||
|
private boolean isHeighestTask = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* 暂停任务,并让任务处于等待状态 |
||||||
|
*/ |
||||||
|
public void stopAndWait() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 最高优先级命令,最高优先级命令有以下属性 |
||||||
|
* 1、在下载队列中,有且只有一个最高优先级任务 |
||||||
|
* 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 |
||||||
|
* 3、任务调度器不会暂停最高优先级任务 |
||||||
|
* 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 |
||||||
|
* 5、如果下载队列中已经满了,则会停止队尾的任务 |
||||||
|
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 |
||||||
|
*/ |
||||||
|
public void setHighestPriority(boolean isHighestPriority) { |
||||||
|
isHeighestTask = isHighestPriority; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isHighestPriorityTask() { |
||||||
|
return isHeighestTask; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.arialyy.aria.core.inf; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/27. |
||||||
|
*/ |
||||||
|
|
||||||
|
public abstract class AbsReceiver<ENTITY extends AbsNormalEntity> implements IReceiver<ENTITY>{ |
||||||
|
public String targetName; |
||||||
|
public Object obj; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,86 @@ |
|||||||
|
/* |
||||||
|
* 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.NonNull; |
||||||
|
import com.arialyy.aria.core.RequestEnum; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
*/ |
||||||
|
public interface ITarget<TARGET extends ITarget> { |
||||||
|
/** |
||||||
|
* 任务文件大小 |
||||||
|
*/ |
||||||
|
long getSize(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 转换后的大小 |
||||||
|
*/ |
||||||
|
String getConvertSize(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务进度百分比 |
||||||
|
*/ |
||||||
|
int getPercent(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取任务进度,如果任务存在,则返回当前进度 |
||||||
|
*/ |
||||||
|
long getCurrentProgress(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 给url请求添加头部 |
||||||
|
* |
||||||
|
* @param key 头部key |
||||||
|
* @param header 头部value |
||||||
|
*/ |
||||||
|
TARGET addHeader(@NonNull String key, @NonNull String header) ; |
||||||
|
|
||||||
|
/** |
||||||
|
* 给url请求添加头部 |
||||||
|
*/ |
||||||
|
TARGET addHeaders(Map<String, String> headers); |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置请求类型 |
||||||
|
* |
||||||
|
* @param requestEnum {@link RequestEnum} |
||||||
|
*/ |
||||||
|
TARGET setRequestMode(RequestEnum requestEnum); |
||||||
|
|
||||||
|
/** |
||||||
|
* 开始下载 |
||||||
|
*/ |
||||||
|
void start(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 停止下载 |
||||||
|
*/ |
||||||
|
void stop(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 恢复下载 |
||||||
|
*/ |
||||||
|
void resume(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 取消下载 |
||||||
|
*/ |
||||||
|
void cancel(); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,71 @@ |
|||||||
|
/* |
||||||
|
* 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.queue; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import android.util.Log; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTask; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
||||||
|
import com.arialyy.aria.core.queue.pool.BaseExecutePool; |
||||||
|
import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 任务组下载队列 |
||||||
|
*/ |
||||||
|
public class DownloadGroupTaskQueue |
||||||
|
extends AbsTaskQueue<DownloadGroupTask, DownloadGroupTaskEntity, DownloadGroupEntity> { |
||||||
|
private static volatile DownloadGroupTaskQueue INSTANCE = null; |
||||||
|
|
||||||
|
private final String TAG = "DownloadGroupTaskQueue"; |
||||||
|
|
||||||
|
public static DownloadGroupTaskQueue getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (AriaManager.LOCK) { |
||||||
|
INSTANCE = new DownloadGroupTaskQueue(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
private DownloadGroupTaskQueue() { |
||||||
|
mExecutePool = new BaseExecutePool<>(true); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadGroupTask createTask(String targetName, DownloadGroupTaskEntity entity) { |
||||||
|
DownloadGroupTask task = null; |
||||||
|
if (!TextUtils.isEmpty(targetName)) { |
||||||
|
task = (DownloadGroupTask) TaskFactory.getInstance() |
||||||
|
.createTask(targetName, entity, DownloadGroupSchedulers.getInstance()); |
||||||
|
entity.key = entity.getEntity().getGroupName(); |
||||||
|
mCachePool.putTask(task); |
||||||
|
} else { |
||||||
|
Log.e(TAG, "target name 为 null!!"); |
||||||
|
} |
||||||
|
return task; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public String getKey(DownloadGroupEntity entity) { |
||||||
|
return entity.getGroupName(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public int getConfigMaxNum() { |
||||||
|
return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; |
||||||
|
} |
||||||
|
} |
@ -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.queue.pool; |
||||||
|
|
||||||
|
import android.util.Log; |
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.download.DownloadTask; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.util.Set; |
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/6/29. |
||||||
|
* 单个下载任务的执行池 |
||||||
|
*/ |
||||||
|
public class DownloadExecutePool extends BaseExecutePool<DownloadTask> { |
||||||
|
private final String TAG = "DownloadExecutePool"; |
||||||
|
|
||||||
|
public DownloadExecutePool(boolean isDownload) { |
||||||
|
super(isDownload); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean putTask(DownloadTask task) { |
||||||
|
synchronized (AriaManager.LOCK) { |
||||||
|
if (task == null) { |
||||||
|
Log.e(TAG, "任务不能为空!!"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
String url = task.getKey(); |
||||||
|
if (mExecuteQueue.contains(task)) { |
||||||
|
Log.e(TAG, "队列中已经包含了该任务,任务key【" + url + "】"); |
||||||
|
return false; |
||||||
|
} else { |
||||||
|
if (mExecuteQueue.size() >= mSize) { |
||||||
|
Set<String> keys = mExecuteMap.keySet(); |
||||||
|
for (String key : keys) { |
||||||
|
if (mExecuteMap.get(key).isHighestPriorityTask()) return false; |
||||||
|
} |
||||||
|
if (pollFirstTask()) { |
||||||
|
return putNewTask(task); |
||||||
|
} |
||||||
|
} else { |
||||||
|
return putNewTask(task); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override boolean pollFirstTask() { |
||||||
|
try { |
||||||
|
DownloadTask oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); |
||||||
|
if (oldTask == null) { |
||||||
|
Log.e(TAG, "移除任务失败"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (oldTask.isHighestPriorityTask()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
oldTask.stop(); |
||||||
|
String key = CommonUtil.keyToHashKey(oldTask.getKey()); |
||||||
|
mExecuteMap.remove(key); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
/* |
||||||
|
* 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.scheduler; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.AriaManager; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTask; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
||||||
|
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/7/2. |
||||||
|
*/ |
||||||
|
public class DownloadGroupSchedulers extends |
||||||
|
AbsSchedulers<DownloadGroupTaskEntity, DownloadGroupEntity, DownloadGroupTask, DownloadGroupTaskQueue> { |
||||||
|
private final String TAG = "DownloadGroupSchedulers"; |
||||||
|
private static volatile DownloadGroupSchedulers INSTANCE = null; |
||||||
|
|
||||||
|
private DownloadGroupSchedulers() { |
||||||
|
mQueue = DownloadGroupTaskQueue.getInstance(); |
||||||
|
isDownload = true; |
||||||
|
} |
||||||
|
|
||||||
|
public static DownloadGroupSchedulers getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (AriaManager.LOCK) { |
||||||
|
INSTANCE = new DownloadGroupSchedulers(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.orm; |
||||||
|
|
||||||
|
import java.lang.annotation.ElementType; |
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
import java.lang.annotation.Target; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/7/4. |
||||||
|
* 一对多 |
||||||
|
*/ |
||||||
|
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface OneToMany { |
||||||
|
/** |
||||||
|
* 关联的表 |
||||||
|
*/ |
||||||
|
Class<? extends DbEntity> table(); |
||||||
|
/** |
||||||
|
* 关联的主键 |
||||||
|
*/ |
||||||
|
String key(); |
||||||
|
} |
@ -0,0 +1,38 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.orm; |
||||||
|
|
||||||
|
import java.lang.annotation.ElementType; |
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
import java.lang.annotation.Target; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2017/7/4. |
||||||
|
* 一对一 |
||||||
|
*/ |
||||||
|
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface OneToOne { |
||||||
|
|
||||||
|
/** |
||||||
|
* 关联的表 |
||||||
|
*/ |
||||||
|
Class<? extends DbEntity> table(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 关联的主键 |
||||||
|
*/ |
||||||
|
String key(); |
||||||
|
} |
Loading…
Reference in new issue