From 8c0eeaa035bac0b35e1750b18824d978b3dafceb Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 18 May 2018 00:04:40 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/240 --- .../com/arialyy/aria/core/AriaManager.java | 2 +- .../com/arialyy/aria/core/ConfigHelper.java | 8 +- .../aria/core/command/normal/StartCmd.java | 1 - .../arialyy/aria/core/common/AbsFileer.java | 12 +- .../arialyy/aria/core/common/ProxyHelper.java | 41 +++++-- .../arialyy/aria/core/common/TaskRecord.java | 2 +- .../aria/core/download/BaseNormalTarget.java | 3 +- .../core/download/DownloadGroupTarget.java | 2 +- .../core/download/DownloadTaskEntity.java | 8 +- .../core/download/downloader/Downloader.java | 4 +- .../download/downloader/FtpThreadTask.java | 11 ++ .../downloader/HttpFileInfoThread.java | 2 - .../download/downloader/HttpThreadTask.java | 6 +- .../arialyy/aria/core/inf/AbsTaskEntity.java | 2 + .../core/queue/DownloadGroupTaskQueue.java | 1 - .../aria/core/queue/DownloadTaskQueue.java | 1 - .../aria/core/queue/UploadTaskQueue.java | 1 - .../upload/uploader/FtpFileInfoThread.java | 36 ++++-- .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- .../com/arialyy/aria/util/CommonUtil.java | 112 +++++++++--------- .../java/com/arialyy/aria/util/DbHelper.java | 42 +++++++ .../java/com/arialyy/compiler/CountFiler.java | 4 +- app/src/main/AndroidManifest.xml | 2 +- app/src/main/assets/aria_config.xml | 6 +- .../arialyy/simple/test/AnyRunActivity.java | 15 +-- .../arialyy/simple/test/AnyRunnModule.java | 48 +++++++- .../com/arialyy/aria/core/Configuration.java | 14 +-- build.gradle | 2 +- 28 files changed, 261 insertions(+), 129 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/util/DbHelper.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index fce1c3eb..1a0eb578 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -377,7 +377,7 @@ import org.xml.sax.SAXException; } else { try { String md5Code = CommonUtil.getFileMD5(xmlFile); - File file = new File(APP.getFilesDir().getPath() + "temp.xml"); + File file = new File(APP.getFilesDir().getPath() + "/temp.xml"); if (file.exists()) { file.delete(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index 233c94e4..d6cb98ec 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -59,8 +59,8 @@ class ConfigHelper extends DefaultHandler { String value = attributes.getValue("value"); switch (qName) { - case "useVirtualFile": - loadUseVirtualFile(value); + case "openDynamicFile": + loadOpenDynamicFile(value); break; case "threadNum": loadThreadNum(value); @@ -117,9 +117,9 @@ class ConfigHelper extends DefaultHandler { } } - private void loadUseVirtualFile(String value) { + private void loadOpenDynamicFile(String value) { if (isDownloadConfig) { - mDownloadConfig.useVirtualFile = checkBoolean(value) ? Boolean.valueOf(value) : false; + mDownloadConfig.openDynamicFile = checkBoolean(value) ? Boolean.valueOf(value) : false; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index 680245a0..b3da691f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -56,7 +56,6 @@ class StartCmd extends AbsNormalCmd { return; } String mod; - // TODO: 2018/4/12 配置文件不存在,是否会出现wait获取不到 ? int maxTaskNum = mQueue.getMaxTaskNum(); AriaManager manager = AriaManager.getInstance(AriaManager.APP); if (isDownloadCmd) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index 3651e096..bef04a0c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.DbHelper; import java.io.File; import java.util.ArrayList; import java.util.HashSet; @@ -241,14 +242,11 @@ public abstract class AbsFileer records = - DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", - mTaskEntity.getKey()); - if (records == null || records.size() == 0) { + mRecord = DbHelper.getTaskRecord(mTaskEntity.getKey()); + if (mRecord == null) { initRecord(); mTaskEntity.setNewTask(true); } else { - mRecord = records.get(0).taskRecord; if (mRecord.threadRecords == null || mRecord.threadRecords.isEmpty()) { initRecord(); mTaskEntity.setNewTask(true); @@ -336,8 +334,8 @@ public abstract class AbsFileer(); mRecord.isGroupRecord = mTaskEntity.getEntity().isGroupChild(); - mRecord.isUseVirtualFile = - AriaManager.getInstance(AriaManager.APP).getDownloadConfig().isUseVirtualFile(); + mRecord.isOpenDynamicFile = + AriaManager.getInstance(AriaManager.APP).getDownloadConfig().isOpenDynamicFile(); if (mRecord.isGroupRecord) { if (mTaskEntity.getEntity() instanceof DownloadEntity) { mRecord.dGroupName = ((DownloadEntity) mTaskEntity.getEntity()).getGroupName(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java b/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java index d8da2277..053365ca 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java @@ -16,12 +16,14 @@ package com.arialyy.aria.core.common; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.List; import java.util.Set; -import static java.util.Collections.unmodifiableSet; - /** * Created by Aria.Lao on 2017/7/10. * 代理参数获取 @@ -45,8 +47,19 @@ public class ProxyHelper { } private void init() { + List classes = CommonUtil.getClassName(AriaManager.APP, "com.arialyy.aria"); + for (String className : classes) { + if (!className.startsWith("com.arialyy.aria.ProxyClassCounter")){ + continue; + } + ALog.d("TAG", className); + count(className); + } + } + + private void count(String className) { try { - Class clazz = Class.forName("com.arialyy.aria.ProxyClassCounter"); + Class clazz = Class.forName(className); Method download = clazz.getMethod("getDownloadCounter"); Method downloadGroup = clazz.getMethod("getDownloadGroupCounter"); Method downloadGroupSub = clazz.getMethod("getDownloadGroupSubCounter"); @@ -54,19 +67,31 @@ public class ProxyHelper { Object object = clazz.newInstance(); Object dc = download.invoke(object); if (dc != null) { - downloadCounter = unmodifiableSet((Set) dc); + if (downloadCounter == null) { + downloadCounter = new HashSet<>(); + } + downloadCounter.addAll((Set) dc); } Object dgc = downloadGroup.invoke(object); if (dgc != null) { - downloadGroupCounter = unmodifiableSet((Set) dgc); + if (downloadGroupCounter == null) { + downloadGroupCounter = new HashSet<>(); + } + downloadGroupCounter.addAll((Set) dgc); } Object dgsc = downloadGroupSub.invoke(object); - if (dgsc != null){ - downloadGroupSubCounter = unmodifiableSet((Set) dgsc); + if (dgsc != null) { + if (downloadGroupSubCounter == null) { + downloadGroupSubCounter = new HashSet<>(); + } + downloadGroupSubCounter.addAll((Set) dgsc); } Object uc = upload.invoke(object); if (uc != null) { - uploadCounter = unmodifiableSet((Set) uc); + if (uploadCounter == null) { + uploadCounter = new HashSet<>(); + } + uploadCounter.addAll((Set) uc); } } catch (ClassNotFoundException e) { e.printStackTrace(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java b/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java index 597388b6..cc0cc3fd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java @@ -78,5 +78,5 @@ public class TaskRecord extends DbEntity { * 是否是使用虚拟文件下载的 * {@code true}是,{@code false}不是 */ - public boolean isUseVirtualFile = false; + public boolean isOpenDynamicFile = false; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java index c0b39569..3d0aa3d3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java @@ -150,10 +150,9 @@ abstract class BaseNormalTarget mEntity.setDownloadPath(filePath); mEntity.setFileName(newFile.getName()); mTaskEntity.setKey(filePath); - //mTaskEntity.update(); if (oldFile.exists()) { oldFile.renameTo(newFile); - CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName()); + CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); } } return true; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index 7de286cb..bb872563 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -224,7 +224,7 @@ public class DownloadGroupTarget extends BaseGroupTarget { if (oldFile.exists()) { oldFile.renameTo(new File(newPath)); } - CommonUtil.renameDownloadConfig(oldFile.getName(), newName); + CommonUtil.modifyTaskRecord(oldFile.getPath(), newPath); entity.setDownloadPath(newPath); taskEntity.setKey(newPath); entity.setFileName(newName); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java index 9803d604..0d046d50 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java @@ -73,6 +73,10 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity { return key; } + @Override public void setKey(String key) { + this.key = key; + } + public String getUrl() { return url; } @@ -109,7 +113,5 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity { isGroupTask = groupTask; } - public void setKey(String key) { - this.key = key; - } + } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index 4c9a2ec1..972f939f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -50,7 +50,7 @@ class Downloader extends AbsFileer { mEntity.getFileSize() <= SUB_LEN || mTaskEntity.getRequestType() == AbsTaskEntity.D_FTP_DIR || mTaskEntity.getRequestType() == AbsTaskEntity.DG_HTTP - || mRecord.isUseVirtualFile + || mRecord.isOpenDynamicFile ? 1 : AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); } @@ -61,7 +61,7 @@ class Downloader extends AbsFileer { try { file = new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192); //设置文件长度 - file.setLength(mRecord.isUseVirtualFile ? 1 : mEntity.getFileSize()); + file.setLength(mRecord.isOpenDynamicFile ? 1 : mEntity.getFileSize()); return true; } catch (IOException e) { failDownload("下载失败【downloadUrl:" diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java index 0ec1e326..125c4422 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java @@ -35,6 +35,11 @@ import org.apache.commons.net.ftp.FTPReply; */ class FtpThreadTask extends AbsFtpThreadTask { private final String TAG = "FtpThreadTask"; + private boolean isOpenDynamicFile; + /** + * 2M的动态长度 + */ + private final int LEN_INTERVAL = 1024 * 1024 * 2; FtpThreadTask(StateConstance constance, IDownloadListener listener, SubThreadConfig downloadInfo) { @@ -44,6 +49,7 @@ class FtpThreadTask extends AbsFtpThreadTask mReadTimeOut = manager.getDownloadConfig().getIOTimeOut(); mBufSize = manager.getDownloadConfig().getBuffSize(); isNotNetRetry = manager.getDownloadConfig().isNotNetRetry(); + isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile; setMaxSpeed(manager.getDownloadConfig().getMaxSpeed()); } @@ -98,6 +104,11 @@ class FtpThreadTask extends AbsFtpThreadTask break; } if (mSleepTime > 0) Thread.sleep(mSleepTime); + if (isOpenDynamicFile) { + file.setLength( + STATE.CURRENT_LOCATION + LEN_INTERVAL < mEntity.getFileSize() ? STATE.CURRENT_LOCATION + + LEN_INTERVAL : mEntity.getFileSize()); + } if (mChildCurrentLocation + len >= mConfig.END_LOCATION) { len = (int) (mConfig.END_LOCATION - mChildCurrentLocation); file.write(buffer, 0, len); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index 3db812ad..10183650 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -179,7 +179,6 @@ class HttpFileInfoThread implements Runnable { return; } File oldFile = new File(mEntity.getDownloadPath()); - String oldName = oldFile.getName(); String newPath = oldFile.getParent() + "/" + newName; if (oldFile.exists()) { oldFile.renameTo(new File(newPath)); @@ -187,7 +186,6 @@ class HttpFileInfoThread implements Runnable { mEntity.setFileName(newName); mEntity.setDownloadPath(newPath); mTaskEntity.setKey(newPath); - CommonUtil.renameDownloadConfig(oldName, newName); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 7b5ca5d0..b3a99db6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -42,7 +42,7 @@ final class HttpThreadTask extends AbsThreadTask downloadInfo) { @@ -52,7 +52,7 @@ final class HttpThreadTask extends AbsThreadTask 0) { Thread.sleep(mSleepTime); } - if (useVirtualFile) { + if (isOpenDynamicFile) { file.setLength( STATE.CURRENT_LOCATION + LEN_INTERVAL < mEntity.getFileSize() ? STATE.CURRENT_LOCATION + LEN_INTERVAL : mEntity.getFileSize()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index 9407a302..22d9891c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -137,6 +137,8 @@ public abstract class AbsTaskEntity extends DbEntity { public abstract String getKey(); + public abstract void setKey(String key); + @Override public void update() { if (getEntity() != null) { getEntity().update(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java index 421df17a..5dbc7c44 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java @@ -67,7 +67,6 @@ public class DownloadGroupTaskQueue && mExecutePool.getTask(entity.getEntity().getKey()) == null) { task = (DownloadGroupTask) TaskFactory.getInstance() .createTask(targetName, entity, DownloadGroupSchedulers.getInstance()); - entity.setKey(entity.getEntity().getGroupName()); mCachePool.putTask(task); } else { ALog.w(TAG, "任务已存在"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 4da7b16f..8245d30a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -123,7 +123,6 @@ public class DownloadTaskQueue extends AbsTaskQueue && mExecutePool.getTask(entity.getEntity().getKey()) == null) { task = (UploadTask) TaskFactory.getInstance() .createTask(targetName, entity, UploadSchedulers.getInstance()); - entity.setKey(entity.getEntity().getFilePath()); mCachePool.putTask(task); } else { ALog.w(TAG, "任务已存在"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java index f0a5e0f5..5d07f753 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java @@ -18,17 +18,18 @@ package com.arialyy.aria.core.upload.uploader; import com.arialyy.aria.core.common.AbsFtpInfoThread; import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.OnFileInfoCallback; +import com.arialyy.aria.core.common.TaskRecord; +import com.arialyy.aria.core.common.ThreadRecord; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import java.io.File; -import java.util.Properties; +import com.arialyy.aria.util.DbHelper; +import java.util.ArrayList; import org.apache.commons.net.ftp.FTPFile; /** * Created by Aria.Lao on 2017/9/26. - * 单任务远程服务器文件信息 + * 单任务上传远程服务器文件信息 */ class FtpFileInfoThread extends AbsFtpInfoThread { private static final String TAG = "FtpUploadFtpFileInfoThread"; @@ -67,16 +68,27 @@ class FtpFileInfoThread extends AbsFtpInfoThread + "尝试从位置:" + (ftpFile.getSize() - 1) + "开始上传"); - File configFile = new File(CommonUtil.getFileConfigPath(false, mEntity.getFileName())); - Properties pro = CommonUtil.loadConfig(configFile); - String key = mEntity.getFileName() + "_record_" + 0; mTaskEntity.setNewTask(false); - long oldRecord = Long.parseLong(pro.getProperty(key, "0")); - if (oldRecord == 0 || oldRecord != ftpFile.getSize()) { - //修改本地保存的停止地址为服务器上对应文件的大小 - pro.setProperty(key, (ftpFile.getSize() - 1) + ""); - CommonUtil.saveConfig(configFile, pro); + + // 修改记录 + TaskRecord record = DbHelper.getTaskRecord(mTaskEntity.getKey()); + if (record == null) { + record = new TaskRecord(); + record.fileName = mEntity.getFileName(); + record.filePath = mTaskEntity.getKey(); + record.threadRecords = new ArrayList<>(); + } + ThreadRecord threadRecord; + if (record.threadRecords == null || record.threadRecords.isEmpty()) { + threadRecord = new ThreadRecord(); + threadRecord.key = record.filePath; + } else { + threadRecord = record.threadRecords.get(0); } + //修改本地保存的停止地址为服务器上对应文件的大小 + threadRecord.startLocation = ftpFile.getSize() - 1; + record.save(); + threadRecord.save(); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 480c41a5..17ab3f1d 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -35,7 +35,7 @@ class DBConfig { /*adb pull /mnt/sdcard/Android/data/com.arialyy.simple/files/DB/AriaLyyDb d:/db*/ static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 32; + static int VERSION = 33; /** * 是否将数据库保存在Sd卡,{@code true} 是 diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index f71ee9f1..73c7f28d 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -39,6 +39,7 @@ import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.orm.DbEntity; +import dalvik.system.DexFile; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -57,9 +58,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; +import java.util.Enumeration; import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Properties; @@ -74,6 +74,39 @@ import java.util.regex.Pattern; public class CommonUtil { private static final String TAG = "CommonUtil"; + /** + * 获取某包下所有类 + * + * @param packageName 包名 + * @return 类的完整名称 + */ + public static List getClassName(Context context, String packageName) { + List classNameList = new ArrayList<>(); + try { + String pPath = context.getPackageCodePath(); + File dir = new File(pPath).getParentFile(); + String dPath = dir.getPath(); + for (String path : dir.list()) { + String fPath = dPath + "/" + path; + if (!fPath.endsWith(".apk")) { + continue; + } + ALog.d(TAG, fPath); + DexFile df = new DexFile(fPath);//通过DexFile查找当前的APK中可执行文件 + Enumeration enumeration = df.entries();//获取df中的元素 这里包含了所有可执行的类名 该类名包含了包名+类名的方式 + while (enumeration.hasMoreElements()) {//遍历 + String className = enumeration.nextElement(); + if (className.contains(packageName)) {//在当前所有可执行的类里面查找包含有该包名的所有类 + classNameList.add(className); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return classNameList; + } + /** * 拦截window.location.replace数据 * @@ -469,9 +502,10 @@ public class CommonUtil { public static void createFileFormInputStream(InputStream is, String path) { try { FileOutputStream fos = new FileOutputStream(path); - byte[] buf = new byte[1376]; - while (is.read(buf) > 0) { - fos.write(buf, 0, buf.length); + byte[] buf = new byte[1024]; + int len; + while ((len = is.read(buf)) > 0) { + fos.write(buf, 0, len); } is.close(); fos.flush(); @@ -845,60 +879,28 @@ public class CommonUtil { } /** - * 重命名下载配置文件 - * 如果旧的配置文件名不存在,则使用新的配置文件名新创建一个文件,否则将旧的配置文件重命名为新的位置文件名。 - * 除了重命名配置文件名外,还会将文件中的记录重命名为新的记录,如果没有记录,则不做处理 + * 更新任务记录 * - * @param oldName 旧的下载文件名 - * @param newName 新的下载文件名 + * @param oldPath 旧的文件路径 + * @param newPath 新的文件路径 */ - public static void renameDownloadConfig(String oldName, String newName) { - renameConfig(true, oldName, newName); - } - - /** - * 重命名上传配置文件 - * 如果旧的配置文件名不存在,则使用新的配置文件名新创建一个文件,否则将旧的配置文件重命名为新的位置文件名。 - * 除了重命名配置文件名外,还会将文件中的记录重命名为新的记录,如果没有记录,则不做处理 - * - * @param oldName 旧的上传文件名 - * @param newName 新的上传文件名 - */ - public static void renameUploadConfig(String oldName, String newName) { - renameConfig(false, oldName, newName); - } - - private static void renameConfig(boolean isDownload, String oldName, String newName) { - if (oldName.equals(newName)) return; - File oldFile = new File(getFileConfigPath(isDownload, oldName)); - File newFile = new File(getFileConfigPath(isDownload, oldName)); + public static void modifyTaskRecord(String oldPath, String newPath) { + if (oldPath.equals(newPath)) { + ALog.w(TAG, "修改任务记录失败,新文件路径和旧文件路径一致"); + return; + } + File oldFile = new File(oldPath); if (!oldFile.exists()) { - createFile(newFile.getPath()); - } else { - Properties pro = CommonUtil.loadConfig(oldFile); - if (!pro.isEmpty()) { - Set keys = pro.keySet(); - Set newKeys = new LinkedHashSet<>(); - Set values = new LinkedHashSet<>(); - for (Object key : keys) { - String oldKey = String.valueOf(key); - if (oldKey.contains(oldName)) { - values.add(pro.getProperty(oldKey)); - newKeys.add(oldKey.replace(oldName, newName)); - } - } - - pro.clear(); - Iterator next = values.iterator(); - for (String key : newKeys) { - pro.setProperty(key, next.next()); - } - - CommonUtil.saveConfig(oldFile, pro); - } - - oldFile.renameTo(newFile); + ALog.w(TAG, "修改任务记录失败,文件【" + oldPath + "】不存在"); + return; + } + TaskRecord record = DbHelper.getTaskRecord(oldPath); + if (record == null) { + ALog.w(TAG, "修改任务记录失败,文件【" + oldPath + "】对应的任务记录不存在"); + return; } + record.filePath = newPath; + record.update(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/util/DbHelper.java b/Aria/src/main/java/com/arialyy/aria/util/DbHelper.java new file mode 100644 index 00000000..5277c433 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/DbHelper.java @@ -0,0 +1,42 @@ +/* + * 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.util; + +import com.arialyy.aria.core.common.RecordWrapper; +import com.arialyy.aria.core.common.TaskRecord; +import com.arialyy.aria.orm.DbEntity; +import java.util.List; + +/** + * 数据库帮助类 + */ +public class DbHelper { + + /** + * 获取任务记录 + * + * @param filePath 文件地址 + * @return 没有记录返回null,有记录则返回任务记录 + */ + public static TaskRecord getTaskRecord(String filePath) { + List records = + DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", filePath); + if (records == null || records.size() == 0) { + return null; + } + return records.get(0).taskRecord; + } +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/CountFiler.java b/AriaCompiler/src/main/java/com/arialyy/compiler/CountFiler.java index ec874cd6..7a6ed484 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/CountFiler.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/CountFiler.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.UUID; import javax.annotation.processing.Filer; import javax.lang.model.element.Modifier; @@ -49,7 +50,8 @@ final class CountFiler { void createCountFile() throws IOException { Set keys = mPbUtil.getListenerClass().keySet(); if (keys.size() == 0) return; - TypeSpec.Builder builder = TypeSpec.classBuilder(ProxyConstance.PROXY_COUNTER_NAME) + TypeSpec.Builder builder = TypeSpec.classBuilder( + ProxyConstance.PROXY_COUNTER_NAME + UUID.randomUUID().toString().replace("-", "")) .addModifiers(Modifier.PUBLIC, Modifier.FINAL); FieldSpec mappingField = FieldSpec.builder( diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c11fae91..4696e5dd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,8 +15,8 @@ android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index bc0e1feb..f3b4fb11 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -11,12 +11,12 @@ - + - + @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index d6d3e104..24465868 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -3,12 +3,9 @@ package com.arialyy.simple.test; import android.os.Bundle; import android.view.View; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.util.ALog; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityTestBinding; -import java.util.List; /** * Created by laoyuyu on 2018/4/13. @@ -18,8 +15,9 @@ public class AnyRunActivity extends BaseActivity { AnyRunnModule module; String[] urls; int index = 0; - String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; + //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + private final String URL = "ftp://192.168.29.140:21/download//AriaPrj.rar"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; @Override protected int setLayoutId() { @@ -42,9 +40,12 @@ public class AnyRunActivity extends BaseActivity { // module.start(urls[index]); // index++; //} - module.start(URL); - List list = Aria.download(this).getTotalTaskList(); - ALog.d(TAG, "size ==> " + list.size()); + //for (int i = 0; i < 10; i++) { + + module.startFtp(URL); + //} + //List list = Aria.download(this).getTotalTaskList(); + //ALog.d(TAG, "size ==> " + list.size()); break; case R.id.stop: //List list = Aria.download(this).getTotalTaskList(); diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index b43a421e..041f1f78 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -62,16 +62,58 @@ public class AnyRunnModule { L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); } - void start(String url) { mUrl = url; Aria.download(this) .load(url) - .addHeader("Accept-Encoding", "gzip") + //.addHeader("Accept-Encoding", "gzip") .setRequestMode(RequestEnum.GET) .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg123456.apk") - .resetState() + //.resetState() + .start(); + //String[] urls = new String[] { + // "http://cdn-s1.touchfound.net/1526449199142_3617.png", + // "http://cdn-s1.touchfound.net/mtVmResources/1526287158571.zip", + // "http://cdn-s1.touchfound.net/1526450206960_2186.png", + // "http://cdn-s1.touchfound.net/1526449199025_1476.png" + //}; + //for (int i = 0, len = urls.length; i < len; i++){ + // + // Aria.download(this) + // .load(urls[i]) + // //.addHeader("Accept-Encoding", "gzip") + // .setRequestMode(RequestEnum.GET) + // .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg123456.apk" + i) + // //.resetState() + // .start(); + //} + } + + void startFtp(String url) { + mUrl = url; + Aria.download(this) + .loadFtp(url) + .login("lao", "123456") + //.addHeader("Accept-Encoding", "gzip") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ftp_ggsg16.apk") + //.resetState() .start(); + //String[] urls = new String[] { + // "http://cdn-s1.touchfound.net/1526449199142_3617.png", + // "http://cdn-s1.touchfound.net/mtVmResources/1526287158571.zip", + // "http://cdn-s1.touchfound.net/1526450206960_2186.png", + // "http://cdn-s1.touchfound.net/1526449199025_1476.png" + //}; + //for (int i = 0, len = urls.length; i < len; i++){ + // + // Aria.download(this) + // .load(urls[i]) + // //.addHeader("Accept-Encoding", "gzip") + // .setRequestMode(RequestEnum.GET) + // .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg123456.apk" + i) + // //.resetState() + // .start(); + //} } void stop(String url) { diff --git a/aria/src/main/java/com/arialyy/aria/core/Configuration.java b/aria/src/main/java/com/arialyy/aria/core/Configuration.java index 0500322f..85b71691 100644 --- a/aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -375,23 +375,23 @@ class Configuration { int maxSpeed = 0; /** - * 是否使用虚拟文件,使用虚拟文件初始化时将不占用磁盘空间,下载多少byte,占多少空间,效果见chrome的下载。 + * 是否开启动态文件,开启动态文件后初始化时将不占用磁盘空间,下载多少byte,占多少空间,效果见chrome的下载 * 注意: * 1、使用该功能,将自动关闭多线程下载; * 2、对于已经采用了多线程的任务,依然采用原来的下载方式; * 3、原本参数是true,任务没下载完成,就参数改为false,那么没下载完成的任务还是会按照参数修改前的方式下载,只有新任务才会根据参数调用不同的下载方式 * {@code true}使用 */ - boolean useVirtualFile = true; + boolean openDynamicFile = true; - public DownloadConfig setuseVirtualFile(boolean useVirtualFile) { - this.useVirtualFile = useVirtualFile; - saveKey("useVirtualFile", String.valueOf(useVirtualFile)); + public DownloadConfig setOpenDynamicFile(boolean openDynamicFile) { + this.openDynamicFile = openDynamicFile; + saveKey("openDynamicFile", String.valueOf(openDynamicFile)); return this; } - public boolean isUseVirtualFile() { - return useVirtualFile; + public boolean isOpenDynamicFile() { + return openDynamicFile; } public DownloadConfig setMaxTaskNum(int maxTaskNum) { diff --git a/build.gradle b/build.gradle index 7d7d9d9c..5895a250 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.4' + publishVersion = '3.4.1_dev_2' // publishVersion = '1.0.3' //FTP插件 repoName='maven' desc = 'android 下载框架'