From c2ab04b67b5deaa8a9117eb05abfdbff24698187 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Thu, 6 Jul 2017 15:48:27 +0800 Subject: [PATCH] https://github.com/AriaLyy/Aria/issues/66 --- .../core/command/normal/CancelAllCmd.java | 9 ++-- .../core/download/DownloadGroupTarget.java | 26 +++++++++-- .../aria/core/download/DownloadReceiver.java | 26 ++--------- .../aria/core/download/DownloadTarget.java | 43 ++++++++++++++++--- .../aria/core/download/DownloadTask.java | 5 ++- .../core/download/downloader/Downloader.java | 2 +- .../aria/core/upload/UploadReceiver.java | 30 +++---------- .../aria/core/upload/UploadTarget.java | 31 ++++++++++--- .../java/com/arialyy/aria/orm/DbEntity.java | 4 +- .../java/com/arialyy/aria/orm/DbUtil.java | 14 +++++- .../java/com/arialyy/aria/orm/SqlHelper.java | 2 +- .../com/arialyy/aria/util/CommonUtil.java | 22 ++++++---- .../simple/download/SingleTaskActivity.java | 9 ++-- 13 files changed, 137 insertions(+), 86 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java index f68b3bcc..bf3b5fb5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java @@ -20,6 +20,7 @@ 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.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; import java.util.List; @@ -49,8 +50,8 @@ final class CancelAllCmd extends AbsNormalCmd { * 处理上传的删除 */ private void handleUploadRemove() { - List allEntity = DbEntity.findAllData(UploadEntity.class); - for (UploadEntity entity : allEntity) { + List allEntity = DbEntity.findAllData(UploadTaskEntity.class); + for (UploadTaskEntity entity : allEntity) { CommonUtil.delUploadTaskConfig(mTaskEntity.removeFile, entity); } } @@ -59,8 +60,8 @@ final class CancelAllCmd extends AbsNormalCmd { * 处理下载的删除 */ private void handleDownloadRemove() { - List allEntity = DbEntity.findAllData(DownloadEntity.class); - for (DownloadEntity entity : allEntity) { + List allEntity = DbEntity.findAllData(DownloadTaskEntity.class); + for (DownloadTaskEntity entity : allEntity) { CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, entity); } } 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 9983edad..d9afa40e 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 @@ -17,7 +17,9 @@ package com.arialyy.aria.core.download; import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CheckUtil; +import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.util.List; @@ -28,12 +30,28 @@ public class DownloadGroupTarget extends AbsGroupTarget { private List mUrls; - DownloadGroupTarget(DownloadGroupEntity entity, String targetName, List urls) { - this.mEntity = entity; + DownloadGroupTarget(List urls, String targetName) { this.mTargetName = targetName; this.mUrls = urls; - mTaskEntity = new DownloadGroupTaskEntity(); - mTaskEntity.entity = entity; + mTaskEntity = + DbEntity.findData(DownloadGroupTaskEntity.class, "key=?", CommonUtil.getMd5Code(urls)); + if (mTaskEntity == null) { + mTaskEntity = new DownloadGroupTaskEntity(); + mTaskEntity.entity = new DownloadGroupEntity(); + } + if (mTaskEntity.entity == null) { + mTaskEntity.entity = getDownloadGroupEntity(urls); + } + mEntity = mTaskEntity.entity; + } + + private DownloadGroupEntity getDownloadGroupEntity(List urls) { + DownloadGroupEntity entity = + DbEntity.findData(DownloadGroupEntity.class, "urlHash=?", CommonUtil.getMd5Code(urls)); + if (entity == null) { + entity = new DownloadGroupEntity(); + } + return entity; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 5b6589c6..260988bf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -60,17 +60,7 @@ public class DownloadReceiver extends AbsReceiver { */ public DownloadTarget load(@NonNull String downloadUrl) { CheckUtil.checkDownloadUrl(downloadUrl); - DownloadEntity entity = - DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); - if (entity == null) { - entity = new DownloadEntity(); - } - File file = new File(entity.getDownloadPath()); - if (!file.exists()) { - entity.setState(IEntity.STATE_WAIT); - } - entity.setDownloadUrl(downloadUrl); - return new DownloadTarget(entity, targetName); + return new DownloadTarget(downloadUrl, targetName); } /** @@ -78,13 +68,7 @@ public class DownloadReceiver extends AbsReceiver { */ public DownloadGroupTarget load(List urls) { CheckUtil.checkDownloadUrls(urls); - DownloadGroupEntity entity = - DbEntity.findData(DownloadGroupEntity.class, "urlHash=?", CommonUtil.getMd5Code(urls)); - - if (entity == null) { - entity = new DownloadGroupEntity(); - } - return new DownloadGroupTarget(entity, targetName, urls); + return new DownloadGroupTarget(urls, targetName); } /** @@ -179,10 +163,8 @@ public class DownloadReceiver extends AbsReceiver { */ @Override public void removeAllTask(boolean removeFile) { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); - AriaManager.getInstance(AriaManager.APP) - .setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(), - NormalCmdFactory.TASK_CANCEL_ALL)) - .exe(); + ariaManager.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(), + NormalCmdFactory.TASK_CANCEL_ALL)).exe(); Set keys = ariaManager.getReceiver().keySet(); for (String key : keys) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index e6bab790..1fa76a8f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -18,21 +18,52 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsNormalTarget; +import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.queue.DownloadTaskQueue; +import com.arialyy.aria.orm.DbEntity; import java.io.File; /** * Created by lyy on 2016/12/5. * https://github.com/AriaLyy/Aria */ -public class DownloadTarget extends - AbsNormalTarget { +public class DownloadTarget + extends AbsNormalTarget { DownloadTarget(DownloadEntity entity, String targetName) { - mEntity = entity; + this(entity.getDownloadUrl(), targetName); + } + + DownloadTarget(String url, String targetName) { mTargetName = targetName; - mTaskEntity = new DownloadTaskEntity(); - mTaskEntity.entity = mEntity; + mTaskEntity = DbEntity.findData(DownloadTaskEntity.class, "key=?", url); + if (mTaskEntity == null) { + mTaskEntity = new DownloadTaskEntity(); + mTaskEntity.entity = new DownloadEntity(); + } + if (mTaskEntity.entity == null) { + mTaskEntity.entity = getEntity(url); + } + mEntity = mTaskEntity.entity; + } + + /** + * 如果任务存在,但是下载实体不存在,则通过下载地址获取下载实体 + * + * @param downloadUrl 下载地址 + */ + private DownloadEntity getEntity(String downloadUrl) { + DownloadEntity entity = + DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); + if (entity == null) { + entity = new DownloadEntity(); + } + File file = new File(entity.getDownloadPath()); + if (!file.exists()) { + entity.setState(IEntity.STATE_WAIT); + } + entity.setDownloadUrl(downloadUrl); + return entity; } /** @@ -56,7 +87,6 @@ public class DownloadTarget extends return this; } - /** * 下载任务是否存在 */ @@ -64,7 +94,6 @@ public class DownloadTarget extends return DownloadTaskQueue.getInstance().getTask(mEntity.getDownloadUrl()) != null; } - /** * 设置文件存储路径 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 2e0dbccd..5c36b1d9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.download; import android.content.Context; -import android.content.Intent; import android.os.Handler; import android.util.Log; import com.arialyy.aria.core.AriaManager; @@ -138,7 +137,7 @@ public class DownloadTask extends AbsNormalTask { mUtil.stopDownload(); } else { mEntity.setState(isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP); - mEntity.save(); + mEntity.update(); if (mOutHandler != null) { mOutHandler.obtainMessage(ISchedulers.STOP, this).sendToTarget(); } @@ -184,6 +183,7 @@ public class DownloadTask extends AbsNormalTask { DownloadTask task = new DownloadTask(taskEntity, outHandler); task.setTargetName(targetName); taskEntity.getEntity().save(); + taskEntity.save(); return task; } } @@ -305,6 +305,7 @@ public class DownloadTask extends AbsNormalTask { entity.setDownloadComplete(state == IEntity.STATE_COMPLETE); entity.setCurrentProgress(location); entity.update(); + } } } \ No newline at end of file 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 ad127c1b..6fb08517 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 @@ -82,7 +82,7 @@ class Downloader implements Runnable, IDownloadUtil { /** * 开始下载流程 */ - private void startFlow(){ + private void startFlow() { checkTask(); mConstance.cleanState(); mConstance.isDownloading = true; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 38ca3487..3d47e2ca 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -48,17 +48,7 @@ public class UploadReceiver extends AbsReceiver { */ public UploadTarget load(@NonNull String filePath) { CheckUtil.checkUploadPath(filePath); - UploadEntity entity = UploadEntity.findData(UploadEntity.class, "filePath=?", filePath); - if (entity == null) { - entity = new UploadEntity(); - } - String regex = "[/|\\\\|//]"; - Pattern p = Pattern.compile(regex); - String[] strs = p.split(filePath); - String fileName = strs[strs.length - 1]; - entity.setFileName(fileName); - entity.setFilePath(filePath); - return new UploadTarget(entity, targetName); + return new UploadTarget(filePath, targetName); } /** @@ -81,15 +71,10 @@ public class UploadReceiver extends AbsReceiver { } @Override public void stopAllTask() { - List allEntity = DbEntity.findAllData(UploadEntity.class); - List stopCmds = new ArrayList<>(); - for (UploadEntity entity : allEntity) { - if (entity.getState() == IEntity.STATE_RUNNING) { - stopCmds.add( - CommonUtil.createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP)); - } - } - AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe(); + AriaManager.getInstance(AriaManager.APP) + .setCmd(NormalCmdFactory.getInstance() + .createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL)) + .exe(); } /** @@ -101,9 +86,8 @@ public class UploadReceiver extends AbsReceiver { @Override public void removeAllTask(boolean removeFile) { final AriaManager am = AriaManager.getInstance(AriaManager.APP); - AriaManager.getInstance(AriaManager.APP) - .setCmd( - CommonUtil.createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_CANCEL_ALL)) + am.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(), + NormalCmdFactory.TASK_CANCEL_ALL)) .exe(); Set keys = am.getReceiver().keySet(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index 5e97d0cb..94d5ca07 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -18,17 +18,39 @@ package com.arialyy.aria.core.upload; import android.support.annotation.NonNull; import com.arialyy.aria.core.inf.AbsNormalTarget; import com.arialyy.aria.core.queue.UploadTaskQueue; +import com.arialyy.aria.orm.DbEntity; +import java.util.regex.Pattern; /** * Created by lyy on 2017/2/28. */ public class UploadTarget extends AbsNormalTarget { - UploadTarget(UploadEntity entity, String targetName) { - this.mEntity = entity; + UploadTarget(String filePath, String targetName) { this.mTargetName = targetName; - mTaskEntity = new UploadTaskEntity(); - mTaskEntity.entity = entity; + mTaskEntity = DbEntity.findData(UploadTaskEntity.class, "key=?", filePath); + if (mTaskEntity == null) { + mTaskEntity = new UploadTaskEntity(); + mTaskEntity.entity = new UploadEntity(); + } + if (mTaskEntity.entity == null) { + mTaskEntity.entity = getUploadEntity(filePath); + } + mEntity = mTaskEntity.entity; + } + + private UploadEntity getUploadEntity(String filePath) { + UploadEntity entity = UploadEntity.findData(UploadEntity.class, "filePath=?", filePath); + if (entity == null) { + entity = new UploadEntity(); + } + String regex = "[/|\\\\|//]"; + Pattern p = Pattern.compile(regex); + String[] strs = p.split(filePath); + String fileName = strs[strs.length - 1]; + entity.setFileName(fileName); + entity.setFilePath(filePath); + return entity; } /** @@ -91,5 +113,4 @@ public class UploadTarget extends AbsNormalTarget boolean isExist(Class clazz, int rowId) { + checkDb(); + String sql = "SELECT rowid FROM " + CommonUtil.getClassName(clazz) + " WHERE rowid=" + rowId; + Cursor cursor = mDb.rawQuery(sql, null); + boolean isExist = cursor.getCount() > 0; + cursor.close(); + return isExist; + } + /** * 条件查寻数据 */ @@ -127,7 +139,7 @@ public class DbUtil { SqlHelper.createTable(mDb, clazz, tableName); } - private void checkDb(){ + private void checkDb() { if (mDb == null || !mDb.isOpen()) { mDb = mHelper.getReadableDatabase(); } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java index 4eeaa338..65c4a961 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -613,7 +613,7 @@ final class SqlHelper extends SQLiteOpenHelper { String primaryData = cursor.getString(kc); if (TextUtils.isEmpty(primaryData) || primaryData.equalsIgnoreCase("null")) continue; List list = findForeignData(db, primaryData, params); - if (list != null && list.size() > 1) { + if (list != null && list.size() > 0) { field.set(entity, list.get(0)); } } 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 bceff11e..97edabe7 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -26,8 +26,10 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.command.normal.AbsNormalCmd; 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.core.upload.UploadTaskEntity; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -134,19 +136,21 @@ public class CommonUtil { * @param removeFile {@code true} 删除已经上传完成的任务,不仅删除上传记录,还会删除已经上传完成的文件,{@code false} * 如果文件已经上传完成,只删除上传记录 */ - public static void delUploadTaskConfig(boolean removeFile, UploadEntity entity) { + public static void delUploadTaskConfig(boolean removeFile, UploadTaskEntity tEntity) { + UploadEntity uEntity = tEntity.getEntity(); if (removeFile) { - File file = new File(entity.getFilePath()); + File file = new File(uEntity.getFilePath()); if (file.exists()) { file.delete(); } } File config = new File( - AriaManager.APP.getFilesDir().getPath() + "/temp/" + entity.getFileName() + ".properties"); + AriaManager.APP.getFilesDir().getPath() + "/temp/" + uEntity.getFileName() + ".properties"); if (config.exists()) { config.delete(); } - entity.deleteData(); + uEntity.deleteData(); + tEntity.deleteData(); } /** @@ -155,19 +159,21 @@ public class CommonUtil { * @param removeFile{@code true} 删除已经下载完成的任务,不仅删除下载记录,还会删除已经下载完成的文件,{@code false} * 如果文件已经下载完成,只删除下载记录 */ - public static void delDownloadTaskConfig(boolean removeFile, DownloadEntity entity) { + public static void delDownloadTaskConfig(boolean removeFile, DownloadTaskEntity tEntity) { + DownloadEntity dEntity = tEntity.getEntity(); if (removeFile) { - File file = new File(entity.getDownloadPath()); + File file = new File(dEntity.getDownloadPath()); if (file.exists()) { file.delete(); } } File config = new File( - AriaManager.APP.getFilesDir().getPath() + "/temp/" + entity.getFileName() + ".properties"); + AriaManager.APP.getFilesDir().getPath() + "/temp/" + dEntity.getFileName() + ".properties"); if (config.exists()) { config.delete(); } - entity.deleteData(); + dEntity.deleteData(); + tEntity.deleteData(); } /** diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 785e14a8..06a1c425 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -62,10 +62,10 @@ public class SingleTaskActivity extends BaseActivity { 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://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; //"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815"; - //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; + "http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; //"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt"; //"http://down8.androidgame-store.com/201706122321/97967927DD4E53D9905ECAA7874C8128/new/game1/19/45319/com.neuralprisma-2.5.2.174-2000174_1494784835.apk?f=web_1"; //不支持断点的链接 @@ -209,10 +209,6 @@ public class SingleTaskActivity extends BaseActivity { @Download.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) { mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL); L.d(TAG, "task__cancel"); - //Aria.download(this) - // .load(DOWNLOAD_URL) - // .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") - // .add(); } @Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) { @@ -255,6 +251,7 @@ public class SingleTaskActivity extends BaseActivity { if (text.equals("重新开始?") || text.equals("开始")) { Aria.download(this) .load(DOWNLOAD_URL) + .addHeader("key", "value") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") .start(); } else if (text.equals("恢复")) {