From 9ff9e1e5dde5aa9925ac8044e2ab7e30887ba178 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Wed, 27 Mar 2019 21:13:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=BB=BB=E5=8A=A1=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=A2=9E=E5=8A=A0=E5=88=86=E9=A1=B5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/download/DownloadEntity.java | 3 + .../aria/core/download/DownloadReceiver.java | 47 +++++++++-- .../com/arialyy/aria/core/inf/AbsTarget.java | 3 + .../aria/core/upload/UploadReceiver.java | 51 ++++++++++-- .../java/com/arialyy/aria/orm/DbEntity.java | 42 ++++++++-- .../com/arialyy/aria/orm/DelegateFind.java | 79 +++++++++++++++---- .../com/arialyy/aria/orm/DelegateWrapper.java | 16 ++++ .../java/com/arialyy/aria/util/CheckUtil.java | 9 +++ .../com/arialyy/aria/util/DbDataHelper.java | 2 +- app/build.gradle | 2 +- .../core/download/SingleTaskActivity.java | 12 ++- gradle.properties | 8 +- 12 files changed, 233 insertions(+), 41 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index a8adb2ce..de69ee0d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -133,6 +133,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { + ", groupHash='" + groupHash + '\'' + + ", fileName='" + + getFileName() + + '\'' + ", md5Code='" + md5Code + '\'' 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 04cea0ce..71a43a48 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 @@ -48,7 +48,9 @@ public class DownloadReceiver extends AbsReceiver { * 设置最大下载速度,单位:kb * * @param maxSpeed 为0表示不限速 + * @deprecated {@code Aria.get(Context).getDownloadConfig().setMaxSpeed(int)} */ + @Deprecated public DownloadReceiver setMaxSpeed(int maxSpeed) { AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMaxSpeed(maxSpeed); return this; @@ -311,19 +313,45 @@ public class DownloadReceiver extends AbsReceiver { /** * 获取所有普通下载任务 - * 获取未完成的普通任务列表{@link #getAllNotCompletTask()} + * 获取未完成的普通任务列表{@link #getAllNotCompleteTask()} * 获取已经完成的普通任务列表{@link #getAllCompleteTask()} */ public List getTaskList() { - return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=? and downloadPath!=''", + return DbEntity.findDatas(DownloadEntity.class, "isGroupChild=? and downloadPath!=''", "false"); } + /** + * 分页获取所有普通下载任务,如果页数大于总页数,返回null + * 获取未完成的普通任务列表{@link #getAllNotCompleteTask()} + * 获取已经完成的普通任务列表{@link #getAllCompleteTask()} + * + * @param page 当前页,不能小于1 + * @param num 每页数量,不能小于1 + */ + public List getTaskList(int page, int num) { + CheckUtil.checkPageParams(page, num); + return DbEntity.findDatas(DownloadEntity.class, page, num, + "isGroupChild=? and downloadPath!=''", "false"); + } + /** * 获取所有未完成的普通下载任务 */ - public List getAllNotCompletTask() { - return DownloadEntity.findDatas(DownloadEntity.class, + public List getAllNotCompleteTask() { + return DbEntity.findDatas(DownloadEntity.class, + "isGroupChild=? and downloadPath!='' and isComplete=?", "false", "false"); + } + + /** + * 分页获取所有未完成的普通下载任务,如果页数大于总页数,返回null + * + * @param page 当前页,不能小于1 + * @param num 每页数量,不能小于1 + */ + public List getAllNotCompleteTask(int page, int num) { + CheckUtil.checkPageParams(page, num); + return DbEntity.findDatas(DownloadEntity.class, page, num, "isGroupChild=? and downloadPath!='' and isComplete=?", "false", "false"); } @@ -331,7 +359,16 @@ public class DownloadReceiver extends AbsReceiver { * 获取所有已经完成的普通任务 */ public List getAllCompleteTask() { - return DownloadEntity.findDatas(DownloadEntity.class, + return DbEntity.findDatas(DownloadEntity.class, + "isGroupChild=? and downloadPath!='' and isComplete=?", "false", "true"); + } + + /** + * 分页获取所有已经完成的普通任务,如果页数大于总页数,返回null + */ + public List getAllCompleteTask(int page, int num) { + CheckUtil.checkPageParams(page, num); + return DbEntity.findDatas(DownloadEntity.class, "isGroupChild=? and downloadPath!='' and isComplete=?", "false", "true"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 80dded92..f43046ab 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -193,7 +193,10 @@ public abstract class AbsTarget getTaskList() { return DbEntity.findAllData(UploadEntity.class); } + /** + * 分页获取所有普通上传任务,如果页数大于总页数,返回null + * 获取未完成的普通任务列表{@link #getAllNotCompleteTask()} + * 获取已经完成的普通任务列表{@link #getAllCompleteTask()} + * + * @param page 当前页,不能小于1 + * @param num 每页数量,不能小于1 + */ + public List getTaskList(int page, int num) { + CheckUtil.checkPageParams(page, num); + return DbEntity.findDatas(UploadEntity.class, page, num, + "isGroupChild=? and downloadPath!=''", "false"); + } + /** * 获取所有未完成的普通上传任务 */ - public List getAllNotCompletTask() { - return UploadEntity.findDatas(UploadEntity.class, + public List getAllNotCompleteTask() { + return DbEntity.findDatas(UploadEntity.class, "isGroupChild=? and isComplete=?", "false", "false"); } /** - * 获取所有已经完成的普通任务 + * 分页获取所有未完成的普通上传任务,如果页数大于总页数,返回null + * + * @param page 当前页,不能小于1 + * @param num 每页数量,不能小于1 + */ + public List getAllNotCompleteTask(int page, int num) { + CheckUtil.checkPageParams(page, num); + return DbEntity.findDatas(UploadEntity.class, page, num, + "isGroupChild=? and downloadPath!='' and isComplete=?", "false", "false"); + } + + /** + * 获取所有已经完成的普通任务,如果页数大于总页数,返回null */ public List getAllCompleteTask() { - return UploadEntity.findDatas(UploadEntity.class, - "isGroupChild=? and isComplete=?", "false", "true"); + return DbEntity.findDatas(UploadEntity.class, "isGroupChild=? and isComplete=?", "false", + "true"); + } + + /** + * 分页获取所有已经完成的普通任务,如果页数大于总页数,返回null + */ + public List getAllCompleteTask(int page, int num) { + CheckUtil.checkPageParams(page, num); + return DbEntity.findDatas(UploadEntity.class, + "isGroupChild=? and downloadPath!='' and isComplete=?", "false", "true"); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java index b41dffd7..07897a6d 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java @@ -77,8 +77,7 @@ public abstract class DbEntity { * @return 没有数据返回null */ public static List findAllData(Class clazz) { - DelegateWrapper util = DelegateWrapper.getInstance(); - return util.findAllData(clazz); + return DelegateWrapper.getInstance().findAllData(clazz); } /** @@ -98,8 +97,25 @@ public abstract class DbEntity { * @return 没有数据返回null */ public static List findDatas(Class clazz, String... expression) { - DelegateWrapper util = DelegateWrapper.getInstance(); - return util.findData(clazz, expression); + return DelegateWrapper.getInstance().findData(clazz, expression); + } + + /** + * 分页查询数据 + * + * DownloadEntity.findFirst(DownloadEntity.class, 0, 10, "downloadUrl=?", downloadUrl); + * + * + * @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null + * @param num 每页返回的数量 + * @return 没有数据返回null,如果页数大于总页数,返回null + */ + public static List findDatas(Class clazz, int page, int num, + String... expression) { + if (page < 1 || num < 1) { + return null; + } + return DelegateWrapper.getInstance().findData(clazz, page, num, expression); } /** @@ -111,8 +127,22 @@ public abstract class DbEntity { * @return 没有数据返回null */ public static List findDataByFuzzy(Class clazz, String conditions) { - DelegateWrapper util = DelegateWrapper.getInstance(); - return util.findDataByFuzzy(clazz, conditions); + return DelegateWrapper.getInstance().findDataByFuzzy(clazz, conditions); + } + + /** + * 模糊查询一组数据 + * + * DownloadEntity.findDatas(DownloadEntity.class, "downloadUrl like http://"); + * + * + * @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null + * @param num 每页返回的数量 + * @return 没有数据返回null,如果页数大于总页数,返回null + */ + public static List findDataByFuzzy(Class clazz, int page, int num, + String conditions) { + return DelegateWrapper.getInstance().findDataByFuzzy(clazz, page, num, conditions); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java index d7fa3e3e..383da34a 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -29,6 +29,7 @@ import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.net.URLDecoder; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; @@ -340,16 +341,38 @@ class DelegateFind extends AbsDelegate { List findData(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] = String.format("'%s'", encodeStr(expression[i + 1])); + String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz), + expression[0]); + String[] params = new String[expression.length - 1]; + System.arraycopy(expression, 1, params, 0, params.length); + + print(FIND_DATA, sql); + Cursor cursor = db.rawQuery(sql, params); + List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; + closeCursor(cursor); + close(db); + return data; + } + + /** + * 获取分页数据 + */ + List findData(SQLiteDatabase db, Class clazz, int page, int num, + String... expression) { + if (page < 1 || num < 1) { + return null; } - sql = String.format(sql, params); + db = checkDb(db); + CheckUtil.checkSqlExpression(expression); + String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", + CommonUtil.getClassName(clazz), + expression[0], (page - 1) * num, num); + + String[] params = new String[expression.length - 1]; + System.arraycopy(expression, 1, params, 0, params.length); + print(FIND_DATA, sql); - Cursor cursor = db.rawQuery(sql, null); + Cursor cursor = db.rawQuery(sql, params); List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; closeCursor(cursor); close(db); @@ -359,16 +382,42 @@ class DelegateFind extends AbsDelegate { /** * 模糊查寻数据 */ - List findDataByFuzzy(SQLiteDatabase db, Class clazz, String conditions) { + List findDataByFuzzy(SQLiteDatabase db, Class clazz, + String conditions) { + db = checkDb(db); + if (TextUtils.isEmpty(conditions)) { + throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); + } + if (!conditions.toUpperCase().contains("LIKE")) { + throw new IllegalArgumentException("sql语句表达式未包含LIEK"); + } + String sql = String.format("SELECT rowid, * FROM %s, WHERE %s", CommonUtil.getClassName(clazz), + conditions); + print(FIND_DATA, sql); + Cursor cursor = db.rawQuery(sql, null); + List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; + closeCursor(cursor); + close(db); + return data; + } + + /** + * 分页、模糊搜索数据 + */ + List findDataByFuzzy(SQLiteDatabase db, Class clazz, + int page, int num, String conditions) { + if (page < 1 || num < 1) { + return null; + } db = checkDb(db); - if(TextUtils.isEmpty(conditions)){ + if (TextUtils.isEmpty(conditions)) { throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); } - if(!conditions.toUpperCase().contains("LIKE")){ + if (!conditions.toUpperCase().contains("LIKE")) { throw new IllegalArgumentException("sql语句表达式未包含LIEK"); } - String sql = - "SELECT rowid, * FROM " + CommonUtil.getClassName(clazz) + " WHERE "+conditions; + String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", + CommonUtil.getClassName(clazz), conditions, (page - 1) * num, num); print(FIND_DATA, sql); Cursor cursor = db.rawQuery(sql, null); List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; @@ -376,6 +425,7 @@ class DelegateFind extends AbsDelegate { close(db); return data; } + /** * 查找表的所有数据 */ @@ -394,7 +444,8 @@ class DelegateFind extends AbsDelegate { /** * 根据数据游标创建一个具体的对象 */ - private synchronized List newInstanceEntity(Class clazz, Cursor cursor) { + private synchronized List newInstanceEntity(Class clazz, + Cursor cursor) { List fields = CommonUtil.getAllFields(clazz); List entitys = new ArrayList<>(); if (fields != null && fields.size() > 0) { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java index 8fdbdf24..9aaf1d3a 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java @@ -124,6 +124,13 @@ public class DelegateWrapper { return mDManager.getDelegate(DelegateFind.class).findData(mDb, clazz, expression); } + /** + * 获取分页数据 + */ + List findData(Class clazz, int page, int num, String... expression) { + return mDManager.getDelegate(DelegateFind.class).findData(mDb, clazz, page, num, expression); + } + /** * 模糊查寻数据 */ @@ -131,6 +138,15 @@ public class DelegateWrapper { return mDManager.getDelegate(DelegateFind.class).findDataByFuzzy(mDb, clazz, conditions); } + /** + * 模糊查寻数据 + */ + List findDataByFuzzy(Class clazz, int page, int num, + String conditions) { + return mDManager.getDelegate(DelegateFind.class) + .findDataByFuzzy(mDb, clazz, page, num, conditions); + } + /** * 通过rowId判断数据是否存在 */ diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java index 7b14f403..278ceb89 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java @@ -41,6 +41,15 @@ public class CheckUtil { if (obj == null) throw new IllegalArgumentException("不能传入空对象"); } + /** + * 检查分页数据,需要查询的页数,从1开始,如果page小于1 或 num 小于1,则抛出{@link NullPointerException} + * @param page 从1 开始 + * @param num 每页数量 + */ + public static void checkPageParams(int page, int num){ + if (page < 1 || num < 1) throw new NullPointerException("page和num不能小于1"); + } + /** * 检查sql的expression是否合法 */ diff --git a/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java b/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java index b736cda1..01f9857c 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java @@ -48,7 +48,7 @@ public class DbDataHelper { /** * 获取组合任务实体 如果数据库不存在该实体,则新创建一个新的任务组实体 * - * @param groupHash 组合任务名称 + * @param groupHash 组合任务Hash * @param urls 子任务url列表 */ public static DownloadGroupEntity getHttpDGEntity(String groupHash, List urls) { diff --git a/app/build.gradle b/app/build.gradle index 03bf545c..14371df2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -51,7 +51,7 @@ dependencies { // compile 'com.arialyy.aria:aria-core:3.3.16' // annotationProcessor 'com.arialyy.aria:aria-compiler:3.3.16' debugApi 'com.amitshekhar.android:debug-db:1.0.0' - compile 'com.github.PhilJay:MPAndroidChart:v3.0.3' + api 'com.github.PhilJay:MPAndroidChart:v3.0.3' implementation project(':AppFrame') } repositories { diff --git a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java index 819cd929..412d4f0c 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java @@ -30,6 +30,7 @@ import android.widget.Toast; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; @@ -44,6 +45,7 @@ import com.arialyy.simple.databinding.ActivitySingleBinding; import java.io.File; import java.util.HashMap; +import java.util.List; import java.util.Map; public class SingleTaskActivity extends BaseActivity { @@ -123,7 +125,7 @@ public class SingleTaskActivity extends BaseActivity { private void setBtState(boolean state) { //mStart.setEnabled(state); //mStop.setEnabled(!state); - }DelegateUpdate + } @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -288,7 +290,9 @@ public class SingleTaskActivity extends BaseActivity { public void onClick(View view) { switch (view.getId()) { case R.id.start: - startD(); + //startD(); + List list = Aria.download(this).getAllNotCompleteTask(5, 2); + ALog.d("Tag", list.size() + ""); break; case R.id.stop: Aria.download(this).load(DOWNLOAD_URL).stop(); @@ -320,7 +324,7 @@ public class SingleTaskActivity extends BaseActivity { //if (file.exists()){ // file.delete(); //} - Aria.download(SingleTaskActivity.this).load(DOWNLOAD_URL).resetState().save(); + //Aria.download(SingleTaskActivity.this).load(DOWNLOAD_URL).resetState().save(); Map params = new HashMap<>(); params.put("key", "value"); params.put("filename", "CentOS-7-x86_64-Minimal-1804.iso"); @@ -339,7 +343,7 @@ public class SingleTaskActivity extends BaseActivity { // + "\"id\":\"你的样子\"\n< > " // + "}") //.resetState() - .start(); + .save(); //.add(); } diff --git a/gradle.properties b/gradle.properties index 4ed5e864..a9bf4bce 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,12 +16,12 @@ #org.gradle.daemon=true #org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 # gradle proxy https://chaosleong.github.io/2017/02/10/Configuring-Gradle-Proxy/ -systemProp.socksProxyHost=127.0.0.1 -systemProp.socksProxyPort=1080 -systemprop.socksProxyVersion=5 +#systemProp.socksProxyHost=127.0.0.1 +#systemProp.socksProxyPort=1080 +#systemprop.socksProxyVersion=5 #Pandroid.debug.obsoleteApi=true -# androidx Ǩhttps://developer.android.com/studio/preview/features/?hl=zh-cn#androidx_migration +# androidx Ǩ��https://developer.android.com/studio/preview/features/?hl=zh-cn#androidx_migration #android.useAndroidX=true #android.enableJetifier=true \ No newline at end of file