任务组事件和普通任务事件同时使用

This commit is contained in:
AriaLyy
2017-07-14 17:49:07 +08:00
parent 71a0ae4492
commit 041df07617
44 changed files with 657 additions and 235 deletions

View File

@ -76,7 +76,7 @@ artifacts {
//################################# jcenter 上传配置 start #########################################
bintray {
// user = hasProperty("bintrayUser") ? getProperty("bintrayUser") : getProperty("BINTRAY_USER")
// key = hasProperty("bintrayKey") ? getProperty("bintrayKey") : getProperty("BINTRAY_KEY")
// groupName = hasProperty("bintrayKey") ? getProperty("bintrayKey") : getProperty("BINTRAY_KEY")
user = BINTRAY_USER
key = BINTRAY_KEY
configurations = ['archives']

View File

@ -56,7 +56,7 @@ class GroupStartCmd<T extends AbsTaskEntity> extends AbsGroupCmd<T> {
if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task);
} else if (mod.equals(QueueMod.WAIT.getTag())) {
if (mQueue.getExePoolSize() < maxTaskNum) {
if (mQueue.getCurrentExePoolNum() < maxTaskNum) {
mQueue.startTask(task);
}
}

View File

@ -27,7 +27,7 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
List<DownloadEntity> allEntity =
DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + "");
for (DownloadEntity entity : allEntity) {
int exeNum = mQueue.getExePoolSize();
int exeNum = mQueue.getCurrentExePoolNum();
if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) {
AbsTask task = createTask(entity);
mQueue.startTask(task);

View File

@ -19,9 +19,15 @@ package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.scheduler.DQueueMapping;
import com.arialyy.aria.orm.Primary;
/**
* Created by lyy on 2016/8/22.
@ -57,14 +63,13 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task);
} else if (mod.equals(QueueMod.WAIT.getTag())) {
if (mQueue.getExePoolSize() < maxTaskNum) {
if (mQueue.getCurrentExePoolNum() < maxTaskNum || task.getState() == IEntity.STATE_STOP) {
mQueue.startTask(task);
}
}
} else {
// 任务不存在时,根据配置不同,对任务执行操作
if (!task.isRunning() && mod.equals(QueueMod.WAIT.getTag()) && (task.getState()
== IEntity.STATE_WAIT || task.getState() == IEntity.STATE_STOP)) {
if (!task.isRunning()) {
mQueue.startTask(task);
}
}

View File

@ -53,6 +53,10 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
*/
private String serverFileName = "";
@Override public String getKey() {
return downloadUrl;
}
public DownloadEntity() {
}

View File

@ -17,6 +17,7 @@ package com.arialyy.aria.core.download;
import android.os.Parcel;
import com.arialyy.aria.core.inf.AbsGroupEntity;
import com.arialyy.aria.orm.NormalList;
import com.arialyy.aria.orm.OneToMany;
import java.util.ArrayList;
import java.util.List;
@ -27,16 +28,26 @@ import java.util.List;
*/
public class DownloadGroupEntity extends AbsGroupEntity {
@OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity> subtask = new ArrayList<>();
@OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity> subtask =
new ArrayList<>();
//任务组下载文件的文件夹地址
/**
* 子任务链接组
*/
@NormalList(clazz = String.class) private List<String> urls = new ArrayList<>();
/**
* 任务组下载文件的文件夹地址
*
* @see DownloadGroupTarget#setDownloadDirPath(String)
*/
private String dirPath = "";
public List<DownloadEntity> getSubTask() {
return subtask;
}
public void setSubTasks(List<DownloadEntity> subTasks) {
void setSubTasks(List<DownloadEntity> subTasks) {
this.subtask = subTasks;
}
@ -48,6 +59,18 @@ public class DownloadGroupEntity extends AbsGroupEntity {
this.dirPath = dirPath;
}
public List<String> getUrls() {
return urls;
}
void setUrls(List<String> urls) {
this.urls = urls;
}
void setGroupName(String key) {
this.groupName = key;
}
public DownloadGroupEntity() {
}

View File

@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsGroupTarget;
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.ArrayList;
@ -40,14 +41,26 @@ public class DownloadGroupTarget
*/
private boolean isSetDirPathed = false;
DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) {
this.mTargetName = targetName;
if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) {
this.mUrls.addAll(groupEntity.getUrls());
}
init(groupEntity.getGroupName());
}
DownloadGroupTarget(List<String> urls, String targetName) {
this.mTargetName = targetName;
this.mUrls = urls;
mGroupName = CommonUtil.getMd5Code(urls);
mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", mGroupName);
init(CommonUtil.getMd5Code(urls));
}
private void init(String key) {
mGroupName = key;
mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key);
if (mTaskEntity == null) {
mTaskEntity = new DownloadGroupTaskEntity();
mTaskEntity.key = mGroupName;
mTaskEntity.key = key;
mTaskEntity.entity = getDownloadGroupEntity();
mTaskEntity.insert();
}
@ -57,18 +70,43 @@ public class DownloadGroupTarget
mEntity = mTaskEntity.entity;
}
/**
* 查询任务组实体,如果数据库不存在该实体,则新创建一个新的任务组实体
*/
private DownloadGroupEntity getDownloadGroupEntity() {
DownloadGroupEntity entity =
DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName);
if (entity == null) {
entity = new DownloadGroupEntity();
entity.setGroupName(mGroupName);
entity.setUrlmd5(mGroupName);
entity.setUrls(mUrls);
entity.insert();
}
return entity;
}
/**
* 设置任务组别名
*/
public DownloadGroupTarget setGroupAlias(String alias) {
if (TextUtils.isEmpty(alias)) return this;
mEntity.setAlias(alias);
mEntity.update();
return this;
}
/**
* 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址
*/
public DownloadGroupTarget setGroupUrl(List<String> urls) {
CheckUtil.checkDownloadUrls(urls);
mUrls.clear();
mUrls.addAll(urls);
mEntity.setGroupName(CommonUtil.getMd5Code(urls));
mEntity.update();
return this;
}
/**
* 设置任务组的文件夹路径在Aria中任务组的所有子任务都会下载到以任务组组名的文件夹中。
* 如groupDirPath = "/mnt/sdcard/download/group_test"

View File

@ -17,6 +17,7 @@ package com.arialyy.aria.core.download;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
@ -27,6 +28,7 @@ import com.arialyy.aria.core.upload.ProxyHelper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -49,9 +51,9 @@ public class DownloadReceiver extends AbsReceiver {
}
/**
* {@link #load(String)},请使用该方法
* 使用下载实体执行下载操作
*/
@Deprecated public DownloadTarget load(DownloadEntity entity) {
public DownloadTarget load(DownloadEntity entity) {
return new DownloadTarget(entity, targetName);
}
@ -71,6 +73,16 @@ public class DownloadReceiver extends AbsReceiver {
return new DownloadGroupTarget(urls, targetName);
}
/**
* 使用任务组实体执行任务组的实体执行任务组的下载操作
*
* @param groupEntity 如果加载的任务实体没有子项的下载地址,
* 那么你需要使用{@link DownloadGroupTarget#setGroupUrl(List)}设置子项的下载地址
*/
public DownloadGroupTarget load(DownloadGroupEntity groupEntity) {
return new DownloadGroupTarget(groupEntity, targetName);
}
/**
* 将当前类注册到Aria
*/
@ -144,7 +156,7 @@ public class DownloadReceiver extends AbsReceiver {
*/
public DownloadTaskEntity getDownloadTask(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'",
return DbEntity.findFirst(DownloadTaskEntity.class, "groupName=? and isGroupTask='false'",
downloadUrl);
}
@ -167,15 +179,31 @@ public class DownloadReceiver extends AbsReceiver {
/**
* 获取普通下载任务列表
*/
@Override public List<DownloadEntity> getTaskList() {
@Override public List<DownloadEntity> getSimpleTaskList() {
return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false");
}
/**
* 获取任务组列表
*/
public List<DownloadGroupTaskEntity> getGroupTaskList() {
return DownloadEntity.findAllData(DownloadGroupTaskEntity.class);
public List<DownloadGroupEntity> getGroupTaskList() {
return DownloadEntity.findAllData(DownloadGroupEntity.class);
}
/**
* 获取普通任务和任务组的任务列表
*/
public List<AbsEntity> getTotleTaskList() {
List<AbsEntity> list = new ArrayList<>();
List<DownloadEntity> simpleTask = getSimpleTaskList();
List<DownloadGroupEntity> groupTask = getGroupTaskList();
if (simpleTask != null && !simpleTask.isEmpty()) {
list.addAll(simpleTask);
}
if (groupTask != null && !groupTask.isEmpty()) {
list.addAll(groupTask);
}
return list;
}
/**

View File

@ -112,6 +112,7 @@ public class DownloadTarget
mEntity.setDownloadPath(downloadPath);
mEntity.setFileName(file.getName());
mTaskEntity.key = downloadPath;
mEntity.update();
mTaskEntity.update();
return this;
}

View File

@ -142,6 +142,11 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.completeTime = completeTime;
}
/**
* 实体唯一标识符
*/
public abstract String getKey();
public AbsEntity() {
}
@ -174,5 +179,4 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.completeTime = in.readLong();
this.isComplete = in.readByte() != 0;
}
}

View File

@ -24,30 +24,29 @@ import com.arialyy.aria.orm.Primary;
*/
public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
/**
* 组名
* 组名组名为任务地址相加的urlMd5
*/
@Primary
private String groupName = "";
@Primary protected String groupName = "";
/**
* 任务地址相加的urlmd5
* 任务组别名
*/
private String urlmd5 = "";
public String getUrlmd5() {
return urlmd5;
}
public void setUrlmd5(String urlmd5) {
this.urlmd5 = urlmd5;
}
private String alias = "";
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
public String getAlias() {
return alias;
}
@Override public String getKey() {
return groupName;
}
public void setAlias(String alias) {
this.alias = alias;
}
public AbsGroupEntity() {
@ -60,12 +59,12 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.groupName);
dest.writeString(this.urlmd5);
dest.writeString(this.alias);
}
protected AbsGroupEntity(Parcel in) {
super(in);
this.groupName = in.readString();
this.urlmd5 = in.readString();
this.alias = in.readString();
}
}

View File

@ -20,42 +20,41 @@ import com.arialyy.aria.orm.Ignore;
/**
* Created by lyy on 2017/2/23.
*/
public interface IEntity {
/**
* 其它状态
*/
@Ignore public static final int STATE_OTHER = -1;
@Ignore int STATE_OTHER = -1;
/**
* 失败状态
*/
@Ignore public static final int STATE_FAIL = 0;
@Ignore int STATE_FAIL = 0;
/**
* 完成状态
*/
@Ignore public static final int STATE_COMPLETE = 1;
@Ignore int STATE_COMPLETE = 1;
/**
* 停止状态
*/
@Ignore public static final int STATE_STOP = 2;
@Ignore int STATE_STOP = 2;
/**
* 等待状态
*/
@Ignore public static final int STATE_WAIT = 3;
@Ignore int STATE_WAIT = 3;
/**
* 下载中
*/
@Ignore public static final int STATE_RUNNING = 4;
@Ignore int STATE_RUNNING = 4;
/**
* 预处理
*/
@Ignore public static final int STATE_PRE = 5;
@Ignore int STATE_PRE = 5;
/**
* 预处理完成
*/
@Ignore public static final int STATE_POST_PRE = 6;
@Ignore int STATE_POST_PRE = 6;
/**
* 取消下载
*/
@Ignore public static final int STATE_CANCEL = 7;
@Ignore int STATE_CANCEL = 7;
}

View File

@ -56,5 +56,5 @@ public interface IReceiver<ENTITY extends IEntity> {
/**
* 获取任务列表
*/
public List<ENTITY> getTaskList();
public List<ENTITY> getSimpleTaskList();
}

View File

@ -102,7 +102,7 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
*
* @return 获取缓存的任务数
*/
@Override public int getCachePoolSize() {
@Override public int getCurrentCachePoolNum() {
return mCachePool.size();
}
@ -111,7 +111,7 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
*
* @return 当前正在执行的任务数
*/
@Override public int getExePoolSize() {
@Override public int getCurrentExePoolNum() {
return mExecutePool.size();
}

View File

@ -88,14 +88,14 @@ public interface ITaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
void reTryStart(TASK task);
/**
* 获取执行池中的任务数量
* 获取当前执行池中的任务数量
*/
int getExePoolSize();
int getCurrentExePoolNum();
/**
* 获取任务缓存池中的任务数量
* 获取当前任务缓存池中的任务数量
*/
int getCachePoolSize();
int getCurrentCachePoolNum();
/**
* 设置执行池可执行的最大任务数

View File

@ -16,7 +16,6 @@
package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
@ -140,7 +139,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
}
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.getExePoolSize() < AriaManager.getInstance(AriaManager.APP)
if (mQueue.getCurrentExePoolNum() < AriaManager.getInstance(AriaManager.APP)
.getUploadConfig()
.getMaxTaskNum()) {
startNextTask();
@ -206,11 +205,6 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
listener.onTaskCancel(task);
break;
case COMPLETE:
//new Handler().postDelayed(new Runnable() {
// @Override public void run() {
// listener.onTaskComplete(task);
// }
//}, 1000);
listener.onTaskComplete(task);
break;
case FAIL:
@ -280,7 +274,14 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
*
* @return {@code true} 有,{@code false} 无
*/
protected boolean hasNextTask() {
return mQueue.getCachePoolSize() > 0;
boolean hasNextTask() {
return mQueue.getCurrentCachePoolNum() > 0;
}
/**
* 获取正在执行的队列数
*/
int getExeTaskNum() {
return mQueue.getCurrentExePoolNum();
}
}

View File

@ -16,10 +16,11 @@
package com.arialyy.aria.core.scheduler;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by Aria.Lao on 2017/7/13.
@ -30,7 +31,7 @@ public class DQueueMapping {
public static final int QUEUE_TYPE_DOWNLOAD = 0xa1;
public static final int QUEUE_TYPE_DOWNLOAD_GROUP = 0xa2;
public static final int QUEUE_NONE = 0xab2;
LinkedHashMap<String, Integer> types = new LinkedHashMap<>();
private LinkedHashMap<String, Integer> types = new LinkedHashMap<>();
private static volatile DQueueMapping instance = null;
@ -79,4 +80,10 @@ public class DQueueMapping {
}
return QUEUE_NONE;
}
public boolean canStart() {
return DownloadTaskQueue.getInstance().getCurrentExePoolNum()
+ DownloadGroupTaskQueue.getInstance().getCurrentExePoolNum() >= AriaManager.getInstance(
AriaManager.APP).getDownloadConfig().getMaxTaskNum();
}
}

View File

@ -23,6 +23,7 @@ import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
/**
* Created by AriaL on 2017/7/2.
* 任务组调度器
*/
public class DownloadGroupSchedulers extends
AbsSchedulers<DownloadGroupTaskEntity, DownloadGroupEntity, DownloadGroupTask, DownloadGroupTaskQueue> {
@ -51,6 +52,10 @@ public class DownloadGroupSchedulers extends
}
@Override protected void startNextTask() {
if (getExeTaskNum() + DownloadSchedulers.getInstance().getExeTaskNum()
>= AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum()) {
return;
}
if (!DownloadSchedulers.getInstance().hasNextTask()) {
nextSelf();
} else {

View File

@ -16,6 +16,7 @@
package com.arialyy.aria.core.scheduler;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
@ -55,6 +56,10 @@ public class DownloadSchedulers
}
@Override protected void startNextTask() {
if (getExeTaskNum() + DownloadGroupSchedulers.getInstance().getExeTaskNum()
>= AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum()) {
return;
}
if (!DownloadGroupSchedulers.getInstance().hasNextTask()) {
nextSelf();
} else {

View File

@ -38,6 +38,10 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable {
this.filePath = filePath;
}
@Override public String getKey() {
return filePath;
}
public UploadEntity() {
}

View File

@ -62,7 +62,7 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath) != null;
}
@Override public List<UploadEntity> getTaskList() {
@Override public List<UploadEntity> getSimpleTaskList() {
return DbEntity.findAllData(UploadEntity.class);
}

View File

@ -28,7 +28,7 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
UploadTarget(String filePath, String targetName) {
this.mTargetName = targetName;
mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "key=?", filePath);
mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "groupName=?", filePath);
if (mTaskEntity == null) {
mTaskEntity = new UploadTaskEntity();
mTaskEntity.entity = new UploadEntity();

View File

@ -32,7 +32,7 @@ import java.util.Map;
public class DBConfig {
static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME;
static int VERSION = 9;
static int VERSION = 10;
static {
if (TextUtils.isEmpty(DB_NAME)) {

View File

@ -171,7 +171,11 @@ public class DbEntity {
if (SqlHelper.isOneToOne(field)) {
values.add(SqlHelper.getOneToOneParams(field));
} else if (type == List.class) {
values.add(SqlHelper.getListElementParams(field));
if (SqlHelper.isOneToMany(field)) {
values.add(SqlHelper.getOneToManyElementParams(field));
} else {
values.add(SqlHelper.list2Str(this, field));
}
} else if (type == Map.class) {
values.add(SqlHelper.map2Str((Map<String, String>) field.get(this)));
} else {

View File

@ -187,7 +187,7 @@ public class DbUtil {
Log.e(TAG, "请输入删除条件");
return -1;
} else if (wheres.length != values.length) {
Log.e(TAG, "key 和 vaule 长度不相等");
Log.e(TAG, "groupName 和 vaule 长度不相等");
return -1;
}
StringBuilder sb = new StringBuilder();

View File

@ -0,0 +1,32 @@
/*
* 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.
* 基本类型的List只能用于常见的数据类型如果是一对多的复杂数据结构需要使用{@link OneToMany}
*/
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface NormalList {
/**
* 数据类型
*/
Class clazz();
}

View File

@ -29,10 +29,10 @@ import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -220,7 +220,7 @@ final class SqlHelper extends SQLiteOpenHelper {
Log.e(TAG, "请输入查询条件");
return null;
} else if (wheres.length != values.length) {
Log.e(TAG, "key 和 vaule 长度不相等");
Log.e(TAG, "groupName 和 vaule 长度不相等");
return null;
}
StringBuilder sb = new StringBuilder();
@ -297,7 +297,11 @@ final class SqlHelper extends SQLiteOpenHelper {
if (type == Map.class) {
value = map2Str((Map<String, String>) field.get(dbEntity));
} else if (type == List.class) {
value = getListElementParams(field);
if (isOneToMany(field)) {
value = getOneToManyElementParams(field);
} else {
value = list2Str(dbEntity, field);
}
} else if (isOneToOne(field)) {
value = getOneToOneParams(field);
} else {
@ -354,7 +358,11 @@ final class SqlHelper extends SQLiteOpenHelper {
if (type == Map.class) {
sb.append(map2Str((Map<String, String>) field.get(dbEntity)));
} else if (type == List.class) {
sb.append(getListElementParams(field));
if (isOneToMany(field)) {
sb.append(getOneToManyElementParams(field));
} else {
sb.append(list2Str(dbEntity, field));
}
} else if (isOneToOne(field)) {
sb.append(getOneToOneParams(field));
} else {
@ -373,13 +381,6 @@ final class SqlHelper extends SQLiteOpenHelper {
close(db);
}
/**
* 保存一对一的数据
*/
private void saveOneToOneFile(Field field) {
}
/**
* 获取一对一参数
*/
@ -396,10 +397,10 @@ final class SqlHelper extends SQLiteOpenHelper {
*
* @param field list反射字段
*/
static String getListElementParams(Field field) {
static String getOneToManyElementParams(Field field) {
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
if (oneToMany == null) {
throw new IllegalArgumentException("List中元素必须被@OneToMany注解");
throw new IllegalArgumentException("一对多元素必须被@OneToMany注解");
}
//关联的表名
String tableName = oneToMany.table().getName();
@ -408,6 +409,63 @@ final class SqlHelper extends SQLiteOpenHelper {
return tableName + "$$" + key;
}
/**
* 列表数据转字符串
*
* @param field list反射字段
*/
static String list2Str(DbEntity dbEntity, Field field) throws IllegalAccessException {
NormalList normalList = field.getAnnotation(NormalList.class);
if (normalList == null) {
throw new IllegalArgumentException("List中元素必须被@NormalList注解");
}
List list = (List) field.get(dbEntity);
if (list == null || list.isEmpty()) return "";
StringBuilder sb = new StringBuilder();
for (Object aList : list) {
sb.append(aList).append("$$");
}
return sb.toString();
}
/**
* 字符串转列表
*
* @param str 数据库中的字段
* @return 如果str为null则返回null
*/
private static List str2List(String str, Field field) {
NormalList normalList = field.getAnnotation(NormalList.class);
if (normalList == null) {
throw new IllegalArgumentException("List中元素必须被@NormalList注解");
}
if (TextUtils.isEmpty(str)) return null;
String[] datas = str.split("$$");
List list = new ArrayList();
String type = normalList.clazz().getName();
for (String data : datas) {
list.add(checkData(data, type));
}
return list;
}
private static Object checkData(String type, String data) {
switch (type) {
case "String":
return data;
case "int":
case "Integer":
return Integer.parseInt(data);
case "double":
case "Double":
return Double.parseDouble(data);
case "float":
case "Float":
return Float.parseFloat(data);
}
return null;
}
/**
* 查找class的主键字段
*
@ -510,23 +568,6 @@ final class SqlHelper extends SQLiteOpenHelper {
close(db);
}
///**
// * 通过字段类型和获取保存在数据库表字段名
// */
//private static String getFieldName(Class<?> type, Field field) {
// String fieldName;
// if (type == Map.class) {
// fieldName = MAP_FIELD + field.getName();
// } else if (type == List.class) {
// fieldName = LIST_FIELD + field.getName();
// } else if (isGeneric(field)) {
// fieldName = GENERIC_FIELD + field.getName();
// } else {
// fieldName = field.getName();
// }
// return fieldName;
//}
/**
* 打印数据库日志
*
@ -598,19 +639,23 @@ final class SqlHelper extends SQLiteOpenHelper {
} else if (type == Map.class) {
field.set(entity, str2Map(cursor.getString(column)));
} else if (type == List.class) {
//主键字段
String primaryKey = getPrimaryName(clazz);
if (TextUtils.isEmpty(primaryKey)) {
throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段");
String value = cursor.getString(column);
if (isOneToMany(field)) {
//主键字段
String primaryKey = getPrimaryName(clazz);
if (TextUtils.isEmpty(primaryKey)) {
throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段");
}
//list字段保存的数据
int kc = cursor.getColumnIndex(primaryKey);
String primaryData = cursor.getString(kc);
if (TextUtils.isEmpty(primaryData)) continue;
List<T> list = findForeignData(db, primaryData, value);
if (list == null) continue;
field.set(entity, findForeignData(db, primaryData, value));
} else {
field.set(entity, str2List(value, field));
}
//list字段保存的数据
int kc = cursor.getColumnIndex(primaryKey);
String params = cursor.getString(column);
String primaryData = cursor.getString(kc);
if (TextUtils.isEmpty(primaryData)) continue;
List<T> list = findForeignData(db, primaryData, params);
if (list == null) continue;
field.set(entity, findForeignData(db, primaryData, params));
} else if (isOneToOne(field)) {
String primaryKey = getPrimaryName(clazz);
if (TextUtils.isEmpty(primaryKey)) {