From 9f6c0d727add6d9e98a0392e6a9d1af0a879d353 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 16 Oct 2017 22:42:40 +0800 Subject: [PATCH] =?UTF-8?q?orm=20=E6=B7=BB=E5=8A=A0=E5=A4=96=E9=94=AE?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/TaskManager.java | 2 +- .../aria/core/download/DownloadEntity.java | 8 +++- .../download/downloader/AbsGroupUtil.java | 25 ++++++++-- .../arialyy/aria/core/inf/AbsGroupEntity.java | 5 +- .../arialyy/aria/core/inf/AbsGroupTask.java | 2 +- .../aria/core/upload/UploadEntity.java | 6 +-- .../java/com/arialyy/aria/orm/Foreign.java | 36 ++++++++++++++ .../arialyy/aria/orm/PrimaryAndForeign.java | 36 ++++++++++++++ .../java/com/arialyy/aria/orm/SqlHelper.java | 34 ++++++++++---- .../java/com/arialyy/aria/orm/SqlUtil.java | 47 ++++++++++++------- .../com/arialyy/aria/util/CommonUtil.java | 3 +- .../arialyy/annotations/DownloadGroup.java | 12 ++--- .../download/group/ChildHandleDialog.java | 4 +- .../multi_download/MultiTaskActivity.java | 4 +- .../res/layout/dialog_sub_task_handler.xml | 14 +++--- 15 files changed, 184 insertions(+), 54 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/orm/Foreign.java create mode 100644 Aria/src/main/java/com/arialyy/aria/orm/PrimaryAndForeign.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java index 4954ff96..ebdea6ae 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java @@ -77,7 +77,7 @@ public class TaskManager { /** * 通过key获取任务 * - * @return 入梅找不到任务,返回null,否则返回key对应的任务 + * @return 如果找不到任务,返回null,否则返回key对应的任务 */ public AbsTask getTask(String key) { return map.get(CommonUtil.keyToHashKey(key)); 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 ef4769eb..7ef4b3b4 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 @@ -21,6 +21,7 @@ import android.os.Parcelable; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; +import com.arialyy.aria.orm.Foreign; import com.arialyy.aria.orm.Primary; import com.arialyy.aria.util.CommonUtil; @@ -34,7 +35,12 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { /** * 所属任务组 */ - private String groupName = ""; + @Foreign(table = DownloadGroupEntity.class, column = "groupName") private String groupName = ""; + + /** + * 下载任务实体的key + */ + @Foreign(table = DownloadTaskEntity.class, column = "key") private String taskKey = ""; /** * 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息,如果服务器没有返回,则不会设置该信息 diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 8ed893a1..729dea68 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -168,11 +168,26 @@ public abstract class AbsGroupUtil implements IUtil { * * @param url 子任务下载地址 */ - public void cancelSunTask(String url) { - Downloader d = getDownloader(url, false); - if (d != null) { - d.cancel(); - } + public void cancelSubTask(String url) { + //List urls = mTaskEntity.entity.getUrls(); + //if (urls != null && !urls.isEmpty() && urls.contains(url)) { + // urls.remove(url); + // DownloadTaskEntity det = + // DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url); + // if (det != null) { + // mTotalLen -= det.getEntity().getFileSize(); + // mGroupSize--; + // if (mGroupSize == 0) { + // closeTimer(false); + // mListener.onCancel(); + // } + // } + // mTaskEntity.getEntity().update(); + //} + //Downloader d = getDownloader(url, false); + //if (d != null) { + // d.cancel(); + //} } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java index b0bc10aa..132b78f7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java @@ -17,6 +17,8 @@ package com.arialyy.aria.core.inf; import android.os.Parcel; import android.os.Parcelable; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.orm.Foreign; import com.arialyy.aria.orm.NormalList; import com.arialyy.aria.orm.Primary; import java.util.ArrayList; @@ -29,7 +31,8 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { /** * 组名,组名为任务地址相加的urlMd5 */ - @Primary protected String groupName = ""; + @Primary @Foreign(table = DownloadGroupTaskEntity.class, column = "key") protected String + groupName = ""; /** * 任务组别名 diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java index ae3a910e..41883028 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java @@ -59,7 +59,7 @@ public abstract class AbsGroupTask */ public void cancelSubTask(String url) { if (mUtil != null) { - mUtil.cancelSunTask(url); + mUtil.cancelSubTask(url); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java index c21b32b4..03649478 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java @@ -18,17 +18,17 @@ package com.arialyy.aria.core.upload; import android.os.Parcel; import android.os.Parcelable; import com.arialyy.aria.core.inf.AbsNormalEntity; +import com.arialyy.aria.orm.Foreign; import com.arialyy.aria.orm.Ignore; import com.arialyy.aria.orm.Primary; +import com.arialyy.aria.orm.PrimaryAndForeign; /** * Created by lyy on 2017/2/9. * 上传文件实体 */ public class UploadEntity extends AbsNormalEntity implements Parcelable { - @Primary - private String filePath; //文件路径 - + @Primary @Foreign(table = UploadTaskEntity.class, column = "key") private String filePath; //文件路径 public String getFilePath() { return filePath; diff --git a/Aria/src/main/java/com/arialyy/aria/orm/Foreign.java b/Aria/src/main/java/com/arialyy/aria/orm/Foreign.java new file mode 100644 index 00000000..cc577d24 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/orm/Foreign.java @@ -0,0 +1,36 @@ +/* + * 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.orm; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by AriaL on 2017/7/4. + * 外键约束 + */ +@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Foreign { + /** + * 关联的表 + */ + Class table(); + /** + * 关联的列 + */ + String column(); +} diff --git a/Aria/src/main/java/com/arialyy/aria/orm/PrimaryAndForeign.java b/Aria/src/main/java/com/arialyy/aria/orm/PrimaryAndForeign.java new file mode 100644 index 00000000..2b50b18d --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/orm/PrimaryAndForeign.java @@ -0,0 +1,36 @@ +/* + * 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.orm; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by AriaL on 2017/7/4. + * 主键和外键约束 + */ +@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface PrimaryAndForeign { + /** + * 关联的表 + */ + Class table(); + /** + * 关联的列 + */ + String column(); +} 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 6d326e26..29e78688 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -251,6 +251,18 @@ final class SqlHelper extends SQLiteOpenHelper { String... expression) { db = checkDb(db); CheckUtil.checkSqlExpression(expression); + + //List fields = CommonUtil.getAllFields(clazz); + //for (Field field : fields) { + // if (SqlUtil.isOneToOne(field)) { + // OneToOne oto = field.getAnnotation(OneToOne.class); + // delData(db, oto.table(), oto.key() + "=?", ); + // } else if (SqlUtil.isOneToMany(field)) { + // OneToMany otm = field.getAnnotation(OneToMany.class); + // delData(db, otm.table(), otm.key() + "=?", otm.key()); + // } + //} + String sql = "DELETE FROM " + CommonUtil.getClassName(clazz) + " WHERE " + expression[0] + " "; sql = sql.replace("?", "%s"); Object[] params = new String[expression.length - 1]; @@ -422,11 +434,8 @@ final class SqlHelper extends SQLiteOpenHelper { } Class type = field.getType(); sb.append(field.getName()); - if (type == String.class - || type == Map.class - || type == List.class - || SqlUtil.isOneToOne(field) - || type.isEnum()) { + if (type == String.class || type == Map.class || type == List.class || SqlUtil.isOneToOne( + field) || type.isEnum()) { sb.append(" varchar"); } else if (type == int.class || type == Integer.class) { sb.append(" interger"); @@ -446,8 +455,17 @@ final class SqlHelper extends SQLiteOpenHelper { continue; } if (SqlUtil.isPrimary(field)) { - //sb.append(" PRIMARY KEY"); - sb.append(" NOT NULL"); + sb.append(" PRIMARY KEY"); + } + if (SqlUtil.isForeign(field)) { + Foreign foreign = field.getAnnotation(Foreign.class); + sb.append(",FOREIGN KEY (") + .append(field.getName()) + .append(") REFERENCES ") + .append(foreign.table()) + .append("(") + .append(foreign.column()) + .append(")"); } sb.append(","); } @@ -587,7 +605,7 @@ final class SqlHelper extends SQLiteOpenHelper { return findData(db, params[0], params[1] + "=?", primary); } - private static void closeCursor(Cursor cursor){ + private static void closeCursor(Cursor cursor) { if (cursor != null && !cursor.isClosed()) { try { cursor.close(); diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java b/Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java index b6e8eed0..bbb37627 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java @@ -15,9 +15,7 @@ */ package com.arialyy.aria.orm; -import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; -import android.util.Log; import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -136,8 +134,6 @@ final class SqlUtil { return TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1); } - - /** * @return true 忽略该字段 */ @@ -146,13 +142,9 @@ final class SqlUtil { Ignore ignore = field.getAnnotation(Ignore.class); int modifiers = field.getModifiers(); String fieldName = field.getName(); - return (ignore != null && ignore.value()) - || fieldName.equals("rowID") - || fieldName.equals("shadow$_klass_") - || fieldName.equals("shadow$_monitor_") - || field.isSynthetic() - || Modifier.isStatic(modifiers) - || Modifier.isFinal(modifiers); + return (ignore != null && ignore.value()) || fieldName.equals("rowID") || fieldName.equals( + "shadow$_klass_") || fieldName.equals("shadow$_monitor_") || field.isSynthetic() || Modifier + .isStatic(modifiers) || Modifier.isFinal(modifiers); } /** @@ -172,27 +164,48 @@ final class SqlUtil { } /** - * 判断是否是主键 + * 判断是否是主键约束 + * + * @return {@code true}主键约束 */ static boolean isPrimary(Field field) { Primary pk = field.getAnnotation(Primary.class); return pk != null; } + /** + * 判断是否是外键约束 + * + * @return {@code true}外键约束 + */ + static boolean isForeign(Field field) { + Foreign fk = field.getAnnotation(Foreign.class); + return fk != null; + } + + /** + * 判断是否是主外键约束 + * + * @return {@code true}主外键约束 + */ + static boolean isPrimaryAndForeign(Field field) { + PrimaryAndForeign fk = field.getAnnotation(PrimaryAndForeign.class); + return fk != null; + } + private static Object checkData(String type, String data) { - if (type.equalsIgnoreCase("java.lang.String")){ + if (type.equalsIgnoreCase("java.lang.String")) { return data; - }else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")){ + } else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")) { return Integer.parseInt(data); - }else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")){ + } else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")) { return Double.parseDouble(data); - }else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")){ + } else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")) { return Float.parseFloat(data); } return null; } - /** * 查找class的主键字段 * 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 9d6a1730..99f19bcb 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -301,7 +301,8 @@ public class CommonUtil { if (config.exists()) { config.delete(); } - dEntity.deleteData(); + //dEntity.deleteData(); + //tEntity.deleteData(); tEntity.deleteData(); } diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java index 45cdd262..11521b9d 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java @@ -128,12 +128,12 @@ import java.lang.annotation.Target; String[] value() default { AriaConstance.NO_URL }; } - ///** - // * 任务组子任务删除的注解 - // */ - //@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @interface onSubTaskCancel { - // String[] value() default { AriaConstance.NO_URL }; - //} + /** + * 任务组子任务删除的注解 + */ + @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @interface onSubTaskCancel { + String[] value() default { AriaConstance.NO_URL }; + } /** * 任务组子任务失败的注解 diff --git a/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java b/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java index 474557b1..0e16664d 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java @@ -18,6 +18,7 @@ package com.arialyy.simple.download.group; import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -109,6 +110,7 @@ import java.util.List; //@DownloadGroup.onSubTaskCancel void onSubTaskCancel(DownloadGroupTask groupTask, // DownloadEntity subEntity) { + // Log.d(TAG, "new Size: " + groupTask.getConvertFileSize()); // mSub.setText("子任务:" + mChildName + ",状态:取消下载"); //} @@ -129,7 +131,7 @@ import java.util.List; return R.layout.dialog_sub_task_handler; } - @OnClick({ R.id.start, R.id.stop }) void onClick(View view) { + @OnClick({ R.id.start, R.id.stop, R.id.cancel }) void onClick(View view) { switch (view.getId()) { case R.id.start: Aria.download(this).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl()); diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java index fe422b61..607a2690 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java @@ -66,8 +66,8 @@ public class MultiTaskActivity extends BaseActivity { dialog.show(getSupportFragmentManager(), "download_num"); break; case R.id.stop_all: - Aria.download(this).stopAllTask(); - //Aria.download(this).removeAllTask(false); + //Aria.download(this).stopAllTask(); + Aria.download(this).removeAllTask(false); break; case R.id.turn: startActivity(new Intent(this, MultiDownloadActivity.class)); diff --git a/app/src/main/res/layout/dialog_sub_task_handler.xml b/app/src/main/res/layout/dialog_sub_task_handler.xml index d833226f..f801ef64 100644 --- a/app/src/main/res/layout/dialog_sub_task_handler.xml +++ b/app/src/main/res/layout/dialog_sub_task_handler.xml @@ -62,13 +62,13 @@ /> - - - - - - - + + + + + + +