修复空格导致不能下载的问题 https://github.com/AriaLyy/Aria/issues/131
parent
8fd64a9147
commit
70539c6539
@ -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.manager; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/11/1. |
||||||
|
* 任务实体工厂 |
||||||
|
*/ |
||||||
|
class DGTaskEntityFactory |
||||||
|
implements ITaskEntityFactory<DownloadGroupEntity, DownloadGroupTaskEntity> { |
||||||
|
private static final String TAG = "DTaskEntityFactory"; |
||||||
|
private static volatile DGTaskEntityFactory INSTANCE = null; |
||||||
|
|
||||||
|
private DGTaskEntityFactory() { |
||||||
|
} |
||||||
|
|
||||||
|
public static DGTaskEntityFactory getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (DGTaskEntityFactory.class) { |
||||||
|
INSTANCE = new DGTaskEntityFactory(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadGroupTaskEntity create(DownloadGroupEntity entity) { |
||||||
|
DownloadGroupTaskEntity dgTaskEntity = |
||||||
|
DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", entity.getGroupName()); |
||||||
|
if (dgTaskEntity == null) { |
||||||
|
dgTaskEntity = new DownloadGroupTaskEntity(); |
||||||
|
dgTaskEntity.save(entity); |
||||||
|
} |
||||||
|
if (dgTaskEntity.entity == null || TextUtils.isEmpty(dgTaskEntity.entity.getKey())) { |
||||||
|
dgTaskEntity.save(entity); |
||||||
|
} |
||||||
|
return dgTaskEntity; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.manager; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/11/1. |
||||||
|
* 任务实体工厂 |
||||||
|
*/ |
||||||
|
class DTaskEntityFactory implements ITaskEntityFactory<DownloadEntity, DownloadTaskEntity> { |
||||||
|
private static final String TAG = "DTaskEntityFactory"; |
||||||
|
private static volatile DTaskEntityFactory INSTANCE = null; |
||||||
|
|
||||||
|
private DTaskEntityFactory() { |
||||||
|
} |
||||||
|
|
||||||
|
public static DTaskEntityFactory getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (DTaskEntityFactory.class) { |
||||||
|
INSTANCE = new DTaskEntityFactory(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadTaskEntity create(DownloadEntity entity) { |
||||||
|
DownloadTaskEntity taskEntity = |
||||||
|
DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false' and url=?", |
||||||
|
entity.getDownloadPath(), entity.getUrl()); |
||||||
|
if (taskEntity == null) { |
||||||
|
taskEntity = new DownloadTaskEntity(); |
||||||
|
taskEntity.save(entity); |
||||||
|
} else if (taskEntity.entity == null || TextUtils.isEmpty(taskEntity.entity.getUrl())) { |
||||||
|
taskEntity.save(entity); |
||||||
|
} else if (!taskEntity.entity.getUrl().equals(entity.getUrl())) { //处理地址切换而保存路径不变
|
||||||
|
taskEntity.save(entity); |
||||||
|
} |
||||||
|
return taskEntity; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
/* |
||||||
|
* 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.manager; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/11/1. |
||||||
|
*/ |
||||||
|
interface IEntityFactory<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> { |
||||||
|
/** |
||||||
|
* 通过信息实体创建任务实体 |
||||||
|
*/ |
||||||
|
TASK_ENTITY create(ENTITY entity); |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过key创建任务,只适应于单任务 |
||||||
|
*/ |
||||||
|
TASK_ENTITY create(String key); |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
/* |
||||||
|
* 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.manager; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.inf.AbsEntity; |
||||||
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/11/1. |
||||||
|
*/ |
||||||
|
interface IEntityFactory<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> { |
||||||
|
/** |
||||||
|
* 通过信息实体创建任务实体 |
||||||
|
*/ |
||||||
|
TASK_ENTITY create(ENTITY entity); |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过key创建任务,只适应于单任务 |
||||||
|
*/ |
||||||
|
TASK_ENTITY create(String key); |
||||||
|
} |
@ -0,0 +1,93 @@ |
|||||||
|
/* |
||||||
|
* 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.manager; |
||||||
|
|
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupEntity; |
||||||
|
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.upload.UploadEntity; |
||||||
|
import com.arialyy.aria.core.upload.UploadTaskEntity; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.concurrent.ConcurrentHashMap; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/11/1. |
||||||
|
* 任务实体管理器,负责 |
||||||
|
*/ |
||||||
|
public class TaskEntityManager { |
||||||
|
private static final String TAG = "TaskManager"; |
||||||
|
private static volatile TaskEntityManager INSTANCE = null; |
||||||
|
private Map<String, AbsTaskEntity> map = new ConcurrentHashMap<>(); |
||||||
|
|
||||||
|
public static TaskEntityManager getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (TaskEntityManager.class) { |
||||||
|
INSTANCE = new TaskEntityManager(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过下载实体获取下载任务实体,如果查找不到任务实体,则重新创建任务实体 |
||||||
|
*/ |
||||||
|
public DownloadTaskEntity getDTEntity(DownloadEntity entity) { |
||||||
|
AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); |
||||||
|
if (tEntity == null || !(tEntity instanceof DownloadTaskEntity)) { |
||||||
|
tEntity = DTEntityFactory.getInstance().create(entity); |
||||||
|
map.put(convertKey(entity.getKey()), tEntity); |
||||||
|
} |
||||||
|
return (DownloadTaskEntity) tEntity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过下载实体获取下载任务组实体,如果查找不到任务组实体,则重新创建任务组实体 |
||||||
|
*/ |
||||||
|
public DownloadGroupTaskEntity getDGTEntity(DownloadGroupEntity entity) { |
||||||
|
AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); |
||||||
|
if (tEntity == null || !(tEntity instanceof DownloadGroupTaskEntity)) { |
||||||
|
tEntity = DGTEntityFactory.getInstance().create(entity); |
||||||
|
map.put(convertKey(entity.getKey()), tEntity); |
||||||
|
} |
||||||
|
return (DownloadGroupTaskEntity) tEntity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过下载实体获取下载任务组实体,如果查找不到任务组实体,则重新创建任务组实体 |
||||||
|
*/ |
||||||
|
public UploadTaskEntity getUTEntity(UploadEntity entity) { |
||||||
|
AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); |
||||||
|
if (tEntity == null || !(tEntity instanceof UploadTaskEntity)) { |
||||||
|
tEntity = UTEntityFactory.getInstance().create(entity); |
||||||
|
map.put(convertKey(entity.getKey()), tEntity); |
||||||
|
} |
||||||
|
return (UploadTaskEntity) tEntity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过key删除任务实体 |
||||||
|
*/ |
||||||
|
public AbsTaskEntity removeTEntity(String key) { |
||||||
|
return map.remove(convertKey(key)); |
||||||
|
} |
||||||
|
|
||||||
|
private String convertKey(String key) { |
||||||
|
return CommonUtil.encryptBASE64(key); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.manager; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/11/1. |
||||||
|
* 任务实体工厂 |
||||||
|
*/ |
||||||
|
class DTaskEntityFactory implements ITaskEntityFactory<DownloadEntity, DownloadTaskEntity> { |
||||||
|
private static final String TAG = "DTaskEntityFactory"; |
||||||
|
private static volatile DTaskEntityFactory INSTANCE = null; |
||||||
|
|
||||||
|
private DTaskEntityFactory() { |
||||||
|
} |
||||||
|
|
||||||
|
public static DTaskEntityFactory getInstance() { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (DTaskEntityFactory.class) { |
||||||
|
INSTANCE = new DTaskEntityFactory(); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadTaskEntity create(DownloadEntity entity) { |
||||||
|
DownloadTaskEntity taskEntity = |
||||||
|
DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false' and url=?", |
||||||
|
entity.getDownloadPath(), entity.getUrl()); |
||||||
|
if (taskEntity == null) { |
||||||
|
taskEntity = new DownloadTaskEntity(); |
||||||
|
taskEntity.save(entity); |
||||||
|
} else if (taskEntity.entity == null || TextUtils.isEmpty(taskEntity.entity.getUrl())) { |
||||||
|
taskEntity.save(entity); |
||||||
|
} else if (!taskEntity.entity.getUrl().equals(entity.getUrl())) { //处理地址切换而保存路径不变
|
||||||
|
taskEntity.save(entity); |
||||||
|
} |
||||||
|
return taskEntity; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue