修复空格导致不能下载的问题 https://github.com/AriaLyy/Aria/issues/131

pull/330/head
AriaLyy 7 years ago
parent 8fd64a9147
commit 70539c6539
  1. 6
      Aria/build.gradle
  2. 71
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  3. 1
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  4. 14
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  5. 12
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  7. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  8. 30
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java
  9. 1
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  10. 6
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java
  11. 56
      Aria/src/main/java/com/arialyy/aria/core/manager/DGTEntityFactory.java
  12. 57
      Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java
  13. 34
      Aria/src/main/java/com/arialyy/aria/core/manager/IGTEntityFactory.java
  14. 34
      Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java
  15. 0
      Aria/src/main/java/com/arialyy/aria/core/manager/SubTaskManager.java
  16. 93
      Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java
  17. 0
      Aria/src/main/java/com/arialyy/aria/core/manager/TaskManager.java
  18. 57
      Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java
  19. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  20. 20
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  21. 10
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  22. 11
      Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java
  23. 29
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  24. 10
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  25. 4
      Aria/src/main/java/com/arialyy/aria/util/Regular.java
  26. 2
      app/build.gradle
  27. 26
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  28. 5
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java
  29. 2
      build.gradle

@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion '25.0.3'
buildToolsVersion '26.0.2'
defaultConfig {
minSdkVersion 9
@ -23,8 +23,8 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile project(':AriaAnnotations')
// compile 'com.arialyy.aria:aria-ftp-plug:1.0.3'
compile 'com.arialyy.aria:aria-ftp-plug:1.0.3'
compile project(':AriaFtpPlug')
// compile project(':AriaFtpPlug')
}
apply from: 'bintray-release.gradle'

@ -35,6 +35,7 @@ import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadReceiver;
@ -78,7 +79,7 @@ import org.xml.sax.SAXException;
public static final int LOG_DEFAULT = LOG_LEVEL_DEBUG;
@SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null;
private Map<String, IReceiver> mReceivers = new ConcurrentHashMap<>();
private Map<String, AbsReceiver> mReceivers = new ConcurrentHashMap<>();
public static Context APP;
private List<ICmd> mCommands = new ArrayList<>();
private Configuration.DownloadConfig mDConfig;
@ -100,7 +101,7 @@ import org.xml.sax.SAXException;
return INSTANCE;
}
public Map<String, IReceiver> getReceiver() {
public Map<String, AbsReceiver> getReceiver() {
return mReceivers;
}
@ -264,25 +265,54 @@ import org.xml.sax.SAXException;
}
if (receiver == null) {
AbsReceiver absReceiver;
if (isDownload) {
DownloadReceiver dReceiver = new DownloadReceiver();
dReceiver.targetName = obj.getClass().getName();
dReceiver.obj = obj;
dReceiver.needRmReceiver = needRmReceiver;
mReceivers.put(key, dReceiver);
receiver = dReceiver;
absReceiver = new DownloadReceiver();
} else {
UploadReceiver uReceiver = new UploadReceiver();
uReceiver.targetName = obj.getClass().getName();
uReceiver.obj = obj;
uReceiver.needRmReceiver = needRmReceiver;
mReceivers.put(key, uReceiver);
receiver = uReceiver;
absReceiver = new UploadReceiver();
}
receiver = checkTarget(key, absReceiver, obj, needRmReceiver);
}
return receiver;
}
/**
* 不允许在"onDestroy""finish""onStop"这三个方法中添加注册器
*/
private AbsReceiver checkTarget(String key, AbsReceiver receiver, Object obj, boolean needRmReceiver) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
int i = 0;
for (StackTraceElement e : stack) {
String name = e.getClassName();
if (!name.equals(AriaManager.class.getName())) {
i++;
} else {
break;
}
}
i += 4;
String methodName = stack[i].getMethodName();
boolean isDestroyed =
methodName.equals("onDestroy") || methodName.equals("finish") || methodName.equals(
"onStop");
if (isDestroyed) {
ALog.w(TAG,
"请不要在Activity或Fragment的onDestroy、finish、onStop等方法中调用Aria,Aria的unRegister会在Activity页面销毁时自动执行");
}
if (obj instanceof Activity && isDestroyed) {
return receiver;
} else if (obj instanceof Fragment && isDestroyed) {
return receiver;
}
receiver.targetName = obj.getClass().getName();
receiver.obj = obj;
receiver.needRmListener = needRmReceiver;
mReceivers.put(key, receiver);
return receiver;
}
/**
* 根据功能类型和控件类型获取对应的key
*/
@ -394,10 +424,11 @@ import org.xml.sax.SAXException;
* 移除指定对象的receiver
*/
public void removeReceiver(Object obj) {
if (obj == null) return;
String clsName = obj.getClass().getName();
for (Iterator<Map.Entry<String, IReceiver>> iter = mReceivers.entrySet().iterator();
for (Iterator<Map.Entry<String, AbsReceiver>> iter = mReceivers.entrySet().iterator();
iter.hasNext(); ) {
Map.Entry<String, IReceiver> entry = iter.next();
Map.Entry<String, AbsReceiver> entry = iter.next();
String key = entry.getKey();
if (key.contains(clsName)) {
iter.remove();
@ -410,14 +441,14 @@ import org.xml.sax.SAXException;
*/
void destroySchedulerListener(Object obj) {
String clsName = obj.getClass().getName();
for (Iterator<Map.Entry<String, IReceiver>> iter = mReceivers.entrySet().iterator();
for (Iterator<Map.Entry<String, AbsReceiver>> iter = mReceivers.entrySet().iterator();
iter.hasNext(); ) {
Map.Entry<String, IReceiver> entry = iter.next();
Map.Entry<String, AbsReceiver> entry = iter.next();
String key = entry.getKey();
if (key.contains(clsName)) {
IReceiver receiver = mReceivers.get(key);
AbsReceiver receiver = mReceivers.get(key);
if (receiver != null) {
receiver.unRegister();
receiver.unRegisterListener();
receiver.destroy();
}
iter.remove();

@ -139,6 +139,7 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
private void handleTask(List<AbsTaskEntity> waitList) {
for (AbsTaskEntity te : waitList) {
if (te.getEntity() == null) continue;
if (te instanceof DownloadTaskEntity) {
mQueue = DownloadTaskQueue.getInstance();
} else if (te instanceof UploadTaskEntity) {

@ -67,6 +67,7 @@ public class DownloadReceiver extends AbsReceiver {
* @param refreshInfo 是否刷新下载信息
*/
public DownloadTarget load(DownloadEntity entity, boolean refreshInfo) {
CheckUtil.checkDownloadUrl(entity.getUrl());
return new DownloadTarget(entity, targetName, refreshInfo);
}
@ -152,9 +153,17 @@ public class DownloadReceiver extends AbsReceiver {
}
/**
* 取消注册
* 取消注册如果是Activity或fragmentAria会界面销毁时自动调用该方法
* 如果是Dialog或popupwindow需要你在撤销界面时调用该方法
*/
@Override public void unRegister() {
if (needRmListener) {
unRegisterListener();
}
AriaManager.getInstance(AriaManager.APP).removeReceiver(obj);
}
@Override public void unRegisterListener() {
String className = obj.getClass().getName();
Set<String> dCounter = ProxyHelper.getInstance().downloadCounter;
Set<String> dgCounter = ProxyHelper.getInstance().downloadGroupCounter;
@ -166,9 +175,6 @@ public class DownloadReceiver extends AbsReceiver {
&& dgsCounter.contains(className))) {
DownloadGroupSchedulers.getInstance().unRegister(obj);
}
if (needRmReceiver) {
AriaManager.getInstance(AriaManager.APP).removeReceiver(obj);
}
}
@Override public void destroy() {

@ -50,8 +50,7 @@ public class DownloadTarget
DownloadTarget(String url, String targetName, boolean refreshInfo) {
this.url = url;
mTargetName = targetName;
DownloadEntity entity = getEntity(url);
initTask(entity);
initTask(getEntity(url));
mTaskEntity.refreshInfo = refreshInfo;
}
@ -83,7 +82,6 @@ public class DownloadTarget
entity = new DownloadEntity();
entity.setUrl(downloadUrl);
entity.setGroupChild(false);
entity.save();
}
File file = new File(entity.getDownloadPath());
if (!file.exists()) {
@ -107,6 +105,8 @@ public class DownloadTarget
/**
* 下载任务是否存在
*
* @return {@code true}任务存在
*/
@Override public boolean taskExists() {
return DownloadTaskQueue.getInstance().getTask(mEntity.getUrl()) != null;
@ -125,16 +125,18 @@ public class DownloadTarget
}
File file = new File(downloadPath);
if (file.isDirectory()) {
throw new IllegalArgumentException("文件不能为文件夹");
throw new IllegalArgumentException("保存路径不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip");
}
if (!downloadPath.equals(mEntity.getDownloadPath())) {
if (!mTaskEntity.refreshInfo && DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", downloadPath)) {
throw new IllegalArgumentException("保存路径【" + downloadPath + "】已经被其它任务占用,请设置其它保存路径");
}
File oldFile = new File(mEntity.getDownloadPath());
File newFile = new File(downloadPath);
if (TextUtils.isEmpty(mEntity.getDownloadPath()) || oldFile.renameTo(newFile)) {
mEntity.setDownloadPath(downloadPath);
mEntity.setFileName(newFile.getName());
mTaskEntity.key = downloadPath;
mEntity.update();
mTaskEntity.update();
CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName());
}

@ -166,7 +166,7 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
try {
outHandler = new Handler(schedulers);
} catch (Exception e) {
e.printStackTrace();
ALog.w(TAG, ALog.getExceptionString(e));
outHandler = new Handler(Looper.getMainLooper(), schedulers);
}
return this;

@ -16,8 +16,6 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.inf.AbsNormalTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.Ignore;
import com.arialyy.aria.orm.NoNull;
import com.arialyy.aria.orm.OneToOne;
@ -52,10 +50,12 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
}
public void save(DownloadEntity entity) {
url = entity.getUrl();
key = entity.getDownloadPath();
this.entity = entity;
entity.save();
if (entity != null) {
url = entity.getUrl();
key = entity.getDownloadPath();
entity.save();
}
save();
}
}

@ -42,36 +42,6 @@ public class FtpDownloadTarget extends DownloadTarget {
mTaskEntity.requestType = AbsTaskEntity.FTP;
}
/**
* 设置文件保存文件夹路径
* 关于文件名
* 1如果保存路径是该文件的保存路径/mnt/sdcard/file.zip则使用路径中的文件名file.zip
* 2如果保存路径是文件夹路径/mnt/sdcard/则使用FTP服务器该文件的文件名
*
* @param downloadPath 路径必须为文件路径不能为文件夹路径
*/
@Override public FtpDownloadTarget setDownloadPath(@NonNull String downloadPath) {
if (TextUtils.isEmpty(downloadPath)) {
throw new IllegalArgumentException("文件保持路径不能为null");
}
File file = new File(downloadPath);
if (file.isDirectory()) {
downloadPath += mEntity.getFileName();
}
if (!downloadPath.equals(mEntity.getDownloadPath())) {
File oldFile = new File(mEntity.getDownloadPath());
File newFile = new File(downloadPath);
if (TextUtils.isEmpty(mEntity.getDownloadPath()) || oldFile.renameTo(newFile)) {
mEntity.setDownloadPath(downloadPath);
mEntity.setFileName(newFile.getName());
mTaskEntity.key = downloadPath;
mTaskEntity.update();
CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName());
}
}
return this;
}
/**
* 设置字符编码
*/

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download.downloader;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;

@ -26,5 +26,9 @@ public abstract class AbsReceiver<ENTITY extends AbsEntity> implements IReceiver
/**
* 当dialogdialogFragmentpopupwindow已经被设置了关闭监听时需要手动移除receiver
*/
public boolean needRmReceiver = false;
public boolean needRmListener = false;
public void unRegisterListener(){
}
}

@ -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;
}
}

@ -17,6 +17,7 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer;
import android.os.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
@ -66,10 +67,15 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
}
@Override public void unRegister(Object obj) {
if (!mObservers.containsKey(obj.getClass().getName())) {
return;
}
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalEntity>>> iter =
mObservers.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalEntity>> entry = iter.next();
if (entry.getKey().equals(obj.getClass().getName())) iter.remove();
if (entry.getKey().equals(obj.getClass().getName())) {
iter.remove();
}
}
}

@ -18,11 +18,10 @@ package com.arialyy.aria.core.upload;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.orm.DbEntity;
@ -80,7 +79,8 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
@Override public void stopAllTask() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(NormalCmdFactory.getInstance()
.createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL, ICmd.TASK_TYPE_UPLOAD))
.createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL,
ICmd.TASK_TYPE_UPLOAD))
.exe();
}
@ -119,14 +119,22 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
return this;
}
/**
* 取消注册如果是Activity或fragmentAria会界面销毁时自动调用该方法
* 如果是Dialog或popupwindow需要你在撤销界面时调用该方法
*/
@Override public void unRegister() {
if (needRmListener) {
unRegisterListener();
}
AriaManager.getInstance(AriaManager.APP).removeReceiver(obj);
}
@Override public void unRegisterListener() {
String className = obj.getClass().getName();
Set<String> dCounter = ProxyHelper.getInstance().uploadCounter;
if (dCounter != null && dCounter.contains(className)) {
UploadSchedulers.getInstance().unRegister(obj);
}
if (needRmReceiver) {
AriaManager.getInstance(AriaManager.APP).removeReceiver(obj);
}
}
}

@ -36,6 +36,16 @@ public class DbEntity {
}
/**
* 检查某个字段的值是否存在
*
* @param expression 字段和值"downloadPath=?"
* @return {@code true}该字段的对应的value已存在
*/
public static boolean checkDataExist(Class clazz, String... expression) {
return DbUtil.getInstance().checkDataExist(clazz, expression);
}
/**
* 清空表数据
*/

@ -64,6 +64,17 @@ public class DbUtil {
return INSTANCE;
}
/**
* 检查某个字段的值是否存在
*
* @param expression 字段和值"url=xxx"
* @return {@code true}该字段的对应的value已存在
*/
synchronized boolean checkDataExist(Class clazz, String... expression) {
checkDb();
return SqlHelper.checkDataExist(mDb, clazz, expression);
}
/**
* 清空表数据
*/

@ -162,6 +162,32 @@ final class SqlHelper extends SQLiteOpenHelper {
return count;
}
/**
* 检查某个字段的值是否存在
*
* @param expression 字段和值"url=xxx"
* @return {@code true}该字段的对应的value已存在
*/
static synchronized boolean checkDataExist(SQLiteDatabase db, Class clazz,
String... expression) {
db = checkDb(db);
CheckUtil.checkSqlExpression(expression);
String sql =
"SELECT rowid, * FROM " + CommonUtil.getClassName(clazz) + " WHERE " + expression[0] + " ";
sql = sql.replace("?", "%s");
Object[] params = new String[expression.length - 1];
for (int i = 0, len = params.length; i < len; i++) {
params[i] = "'" + expression[i + 1] + "'";
}
sql = String.format(sql, params);
print(FIND_DATA, sql);
Cursor cursor = db.rawQuery(sql, null);
final boolean isExist = cursor.getCount() > 0;
closeCursor(cursor);
close(db);
return isExist;
}
/**
* 条件查寻数据
*/
@ -456,7 +482,8 @@ final class SqlHelper extends SQLiteOpenHelper {
continue;
}
if (SqlUtil.isPrimary(field)) {
sb.append(" PRIMARY KEY");
//sb.append(" PRIMARY KEY");
}
if (SqlUtil.isForeign(field)) {
foreignArray.add(field);

@ -134,8 +134,10 @@ public class CommonUtil {
*/
public static String convertUrl(String url) {
if (hasDoubleCharacter(url)) {
//预先处理空格,URLEncoder只会把空格转换为+
url = url.replaceAll(" ", "%20");
//匹配双字节字符(包括汉字在内)
String regex = "[^\\x00-\\xff]";
String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
Set<String> strs = new HashSet<>();
@ -146,6 +148,7 @@ public class CommonUtil {
for (String str : strs) {
url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
@ -154,7 +157,7 @@ public class CommonUtil {
}
/**
* 判断是否有双字节字符(包括汉字在内)
* 判断是否有双字节字符(包括汉字在内) 和空格制表符回车
*
* @param chineseStr 需要进行判断的字符串
* @return {@code true}有双字节字符{@code false} 无双字节字符
@ -162,7 +165,8 @@ public class CommonUtil {
public static boolean hasDoubleCharacter(String chineseStr) {
char[] charArray = chineseStr.toCharArray();
for (char aCharArray : charArray) {
if ((aCharArray >= 0x0391) && (aCharArray <= 0xFFE5)) {
if (((aCharArray >= 0x0391) && (aCharArray <= 0xFFE5)) || (aCharArray == 0x0d) || (aCharArray
== 0x0a) || (aCharArray == 0x20)) {
return true;
}
}

@ -25,4 +25,8 @@ public interface Regular {
*/
String REG_IP_V4 = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
/**
* 匹配双字节字符空格制表符换行符
*/
String REG_DOUBLE_CHAR_AND_SPACE = "[^\\x00-\\xff]|[\\s\\p{Zs}]";
}

@ -5,7 +5,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '25.0.3'
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.arialyy.simple"

@ -47,7 +47,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1";
//"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi";
//"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt";
@ -59,6 +59,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
//"http://172.18.104.50:8080/download/_302turn";
//"http://gdown.baidu.com/data/wisegame/0904344dee4a2d92/QQ_718.apk";
"http://172.21.1.99:8080/download/test+ 中文123.zip";
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@ -73,8 +74,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
* 设置start stop 按钮状态
*/
private void setBtState(boolean state) {
//mStart.setEnabled(state);
//mStop.setEnabled(!state);
mStart.setEnabled(state);
mStop.setEnabled(!state);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
@ -117,7 +118,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
return true;
}
@Download.onPre(DOWNLOAD_URL) protected void onPre(DownloadTask task) {
@Download.onPre protected void onPre(DownloadTask task) {
setBtState(false);
}
@ -125,7 +126,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
getBinding().setFileSize(task.getConvertFileSize());
}
@Download.onTaskRunning(DOWNLOAD_URL) protected void running(DownloadTask task) {
@Download.onTaskRunning protected void running(DownloadTask task) {
long len = task.getFileSize();
if (len == 0) {
@ -136,18 +137,18 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
getBinding().setSpeed(task.getConvertSpeed());
}
@Download.onTaskResume(DOWNLOAD_URL) void taskResume(DownloadTask task) {
@Download.onTaskResume void taskResume(DownloadTask task) {
mStart.setText("暂停");
setBtState(false);
}
@Download.onTaskStop(DOWNLOAD_URL) void taskStop(DownloadTask task) {
@Download.onTaskStop void taskStop(DownloadTask task) {
mStart.setText("恢复");
setBtState(true);
getBinding().setSpeed("");
}
@Download.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) {
@Download.onTaskCancel void taskCancel(DownloadTask task) {
getBinding().setProgress(0);
Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setText("开始");
@ -155,12 +156,12 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
getBinding().setSpeed("");
}
@Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) {
@Download.onTaskFail void taskFail(DownloadTask task) {
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true);
}
@Download.onTaskComplete(DOWNLOAD_URL) void taskComplete(DownloadTask task) {
@Download.onTaskComplete void taskComplete(DownloadTask task) {
getBinding().setProgress(100);
Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?");
@ -170,7 +171,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
}
@Download.onNoSupportBreakPoint(DOWNLOAD_URL)
@Download.onNoSupportBreakPoint
public void onNoSupportBreakPoint(DownloadTask task) {
T.showShort(SingleTaskActivity.this, "该下载链接不支持断点");
}
@ -220,8 +221,9 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void startD(){
Aria.download(this).load("aaaa.apk");
Aria.download(SingleTaskActivity.this)
.load(DOWNLOAD_URL, true)
.load(DOWNLOAD_URL)
.addHeader("groupName", "value")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/hhhhhhhh.apk")
.start();

@ -136,4 +136,9 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
@DownloadGroup.onTaskRunning() void groupTaskRunning(DownloadGroupTask task) {
mAdapter.setProgress(task.getEntity());
}
@Override protected void onDestroy() {
super.onDestroy();
//Aria.download(this).unRegister();
}
}

@ -39,7 +39,7 @@ task clean(type: Delete) {
ext {
userOrg = 'arialyy'
groupId = 'com.arialyy.aria'
publishVersion = '3.3.5'
publishVersion = '3.3.6_dev_1'
// publishVersion = '1.0.3' //FTP插件
repoName='maven'
desc = 'android 下载框架'

Loading…
Cancel
Save