fix bug
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -37,7 +37,7 @@
|
|||||||
<ConfirmationsSetting value="0" id="Add" />
|
<ConfirmationsSetting value="0" id="Add" />
|
||||||
<ConfirmationsSetting value="0" id="Remove" />
|
<ConfirmationsSetting value="0" id="Remove" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
@ -14,43 +14,36 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
/**
|
/**
|
||||||
* 其它状态
|
* 其它状态
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_OTHER = -1;
|
||||||
public static final int STATE_OTHER = -1;
|
|
||||||
/**
|
/**
|
||||||
* 失败状态
|
* 失败状态
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_FAIL = 0;
|
||||||
public static final int STATE_FAIL = 0;
|
|
||||||
/**
|
/**
|
||||||
* 完成状态
|
* 完成状态
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_COMPLETE = 1;
|
||||||
public static final int STATE_COMPLETE = 1;
|
|
||||||
/**
|
/**
|
||||||
* 停止状态
|
* 停止状态
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_STOP = 2;
|
||||||
public static final int STATE_STOP = 2;
|
|
||||||
/**
|
/**
|
||||||
* 未开始状态
|
* 未开始状态
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_WAIT = 3;
|
||||||
public static final int STATE_WAIT = 3;
|
|
||||||
/**
|
/**
|
||||||
* 下载中
|
* 下载中
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_DOWNLOAD_ING = 4;
|
||||||
public static final int STATE_DOWNLOAD_ING = 4;
|
|
||||||
/**
|
/**
|
||||||
* 取消下载
|
* 取消下载
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore public static final int STATE_CANCEL = 5;
|
||||||
public static final int STATE_CANCEL = 5;
|
|
||||||
|
|
||||||
private String downloadUrl; //下载路径
|
private String downloadUrl = ""; //下载路径
|
||||||
private String downloadPath; //保存路径
|
private String downloadPath = ""; //保存路径
|
||||||
private String fileName; //文件名
|
private String fileName = ""; //文件名
|
||||||
private String str; //其它字段
|
private String str = ""; //其它字段
|
||||||
private long completeTime; //完成时间
|
private long completeTime; //完成时间
|
||||||
private long fileSize = 1;
|
private long fileSize = 1;
|
||||||
private int state = STATE_WAIT;
|
private int state = STATE_WAIT;
|
||||||
@ -58,7 +51,8 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
private long currentProgress = 0; //当前下载进度
|
private long currentProgress = 0; //当前下载进度
|
||||||
private int failNum = 0;
|
private int failNum = 0;
|
||||||
|
|
||||||
public DownloadEntity(){}
|
public DownloadEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
public String getStr() {
|
public String getStr() {
|
||||||
return str;
|
return str;
|
||||||
@ -154,9 +148,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
dest.writeLong(this.completeTime);
|
dest.writeLong(this.completeTime);
|
||||||
dest.writeLong(this.fileSize);
|
dest.writeLong(this.fileSize);
|
||||||
dest.writeInt(this.state);
|
dest.writeInt(this.state);
|
||||||
dest.writeByte(this.isDownloadComplete ?
|
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
|
||||||
(byte) 1 :
|
|
||||||
(byte) 0);
|
|
||||||
dest.writeLong(this.currentProgress);
|
dest.writeLong(this.currentProgress);
|
||||||
dest.writeInt(this.failNum);
|
dest.writeInt(this.failNum);
|
||||||
}
|
}
|
||||||
@ -172,8 +164,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
this.failNum = in.readInt();
|
this.failNum = in.readInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Ignore public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
||||||
public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
|
||||||
@Override public DownloadEntity createFromParcel(Parcel source) {
|
@Override public DownloadEntity createFromParcel(Parcel source) {
|
||||||
return new DownloadEntity(source);
|
return new DownloadEntity(source);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package com.arialyy.downloadutil.orm;
|
|||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.arialyy.downloadutil.util.Util;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -57,8 +59,8 @@ public class DbEntity {
|
|||||||
/**
|
/**
|
||||||
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
||||||
*/
|
*/
|
||||||
public void save() {
|
public synchronized void save() {
|
||||||
if (thisIsExist()) {
|
if (mUtil.tableExists(this) && thisIsExist()) {
|
||||||
update();
|
update();
|
||||||
} else {
|
} else {
|
||||||
insert();
|
insert();
|
||||||
@ -70,7 +72,7 @@ public class DbEntity {
|
|||||||
*/
|
*/
|
||||||
private boolean thisIsExist() {
|
private boolean thisIsExist() {
|
||||||
try {
|
try {
|
||||||
Field[] fields = getClass().getFields();
|
Field[] fields = Util.getFields(getClass());
|
||||||
List<String> where = new ArrayList<>();
|
List<String> where = new ArrayList<>();
|
||||||
List<String> values = new ArrayList<>();
|
List<String> values = new ArrayList<>();
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
@ -80,10 +82,10 @@ public class DbEntity {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
where.add(field.getName());
|
where.add(field.getName());
|
||||||
values.add((String) field.get(getClass()));
|
values.add(field.get(this) + "");
|
||||||
}
|
}
|
||||||
return findData(getClass(), (String[]) where.toArray(),
|
return findData(getClass(), where.toArray(new String[where.size()]),
|
||||||
(String[]) values.toArray()) != null;
|
values.toArray(new String[values.size()])) != null;
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -124,6 +126,6 @@ public class DbEntity {
|
|||||||
public <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
public <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
||||||
@NonNull String[] values) {
|
@NonNull String[] values) {
|
||||||
List<T> datas = mUtil.findData(clazz, this, wheres, values);
|
List<T> datas = mUtil.findData(clazz, this, wheres, values);
|
||||||
return datas.size() > 0 ? datas.get(0) : null;
|
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,7 +149,7 @@ public class DbUtil {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object where : wheres) {
|
for (Object where : wheres) {
|
||||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||||
sb.append(i >= wheres.length - 1 ? "" : ", ");
|
sb.append(i >= wheres.length - 1 ? "" : " AND ");
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
print(FIND_DATA, sb.toString());
|
print(FIND_DATA, sb.toString());
|
||||||
@ -208,7 +208,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 查找某张表是否存在
|
* 查找某张表是否存在
|
||||||
*/
|
*/
|
||||||
public boolean tableExists(DbEntity dbEntity) {
|
public synchronized boolean tableExists(DbEntity dbEntity) {
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
@ -26,15 +26,15 @@ public class DownLoadUtil {
|
|||||||
* 线程数
|
* 线程数
|
||||||
*/
|
*/
|
||||||
private static final int THREAD_NUM = 3;
|
private static final int THREAD_NUM = 3;
|
||||||
|
private static final int TIME_OUT = 5000; //超时时间
|
||||||
/**
|
/**
|
||||||
* 已经完成下载任务的线程数量
|
* 已经完成下载任务的线程数量
|
||||||
*/
|
*/
|
||||||
private int mCompleteThreadNum = 0;
|
private int mCompleteThreadNum = 0;
|
||||||
private long mCurrentLocation;
|
|
||||||
private boolean isDownloading = false;
|
private boolean isDownloading = false;
|
||||||
private boolean isStop = false;
|
private boolean isStop = false;
|
||||||
private boolean isCancel = false;
|
private boolean isCancel = false;
|
||||||
private static final int TIME_OUT = 5000; //超时时间
|
private long mCurrentLocation;
|
||||||
boolean isNewTask = true;
|
boolean isNewTask = true;
|
||||||
private int mCancelNum = 0;
|
private int mCancelNum = 0;
|
||||||
private int mStopNum = 0;
|
private int mStopNum = 0;
|
||||||
@ -197,7 +197,8 @@ public class DownLoadUtil {
|
|||||||
endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
||||||
}
|
}
|
||||||
DownloadEntity entity = new DownloadEntity(context, fileLength,
|
DownloadEntity entity = new DownloadEntity(context, fileLength,
|
||||||
downloadUrl, dFile, i, startL, endL);
|
downloadUrl, dFile, i,
|
||||||
|
startL, endL);
|
||||||
DownLoadTask task = new DownLoadTask(entity);
|
DownLoadTask task = new DownLoadTask(entity);
|
||||||
tasks.put(i, new Thread(task));
|
tasks.put(i, new Thread(task));
|
||||||
}
|
}
|
||||||
@ -230,7 +231,6 @@ public class DownLoadUtil {
|
|||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
stopDownload();
|
stopDownload();
|
||||||
mListener.onFail();
|
mListener.onFail();
|
||||||
System.gc();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -306,7 +306,6 @@ public class DownLoadUtil {
|
|||||||
Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++");
|
Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++");
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
mListener.onCancel();
|
mListener.onCancel();
|
||||||
System.gc();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -324,7 +323,6 @@ public class DownLoadUtil {
|
|||||||
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++");
|
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++");
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
mListener.onStop(mCurrentLocation);
|
mListener.onStop(mCurrentLocation);
|
||||||
System.gc();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -340,7 +338,6 @@ public class DownLoadUtil {
|
|||||||
}
|
}
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
mListener.onComplete();
|
mListener.onComplete();
|
||||||
System.gc();
|
|
||||||
}
|
}
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -18,6 +18,45 @@ import java.util.Properties;
|
|||||||
public class Util {
|
public class Util {
|
||||||
private static final String TAG = "util";
|
private static final String TAG = "util";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取类里面的所在字段
|
||||||
|
*/
|
||||||
|
public static Field[] getFields(Class clazz) {
|
||||||
|
Field[] fields = null;
|
||||||
|
fields = clazz.getDeclaredFields();
|
||||||
|
if (fields == null || fields.length == 0) {
|
||||||
|
Class superClazz = clazz.getSuperclass();
|
||||||
|
if (superClazz != null) {
|
||||||
|
fields = getFields(superClazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取类里面的指定对象,如果该类没有则从父类查询
|
||||||
|
*/
|
||||||
|
public static Field getField(Class clazz, String name) {
|
||||||
|
Field field = null;
|
||||||
|
try {
|
||||||
|
field = clazz.getDeclaredField(name);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
try {
|
||||||
|
field = clazz.getField(name);
|
||||||
|
} catch (NoSuchFieldException e1) {
|
||||||
|
if (clazz.getSuperclass() == null) {
|
||||||
|
return field;
|
||||||
|
} else {
|
||||||
|
field = getField(clazz.getSuperclass(), name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (field != null) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
}
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将缓存的key转换为hash码
|
* 将缓存的key转换为hash码
|
||||||
*
|
*
|
||||||
@ -53,21 +92,6 @@ public class Util {
|
|||||||
return stringBuilder.toString();
|
return stringBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取类里面的所在字段
|
|
||||||
*/
|
|
||||||
public static Field[] getFields(Class clazz) {
|
|
||||||
Field[] fields = null;
|
|
||||||
fields = clazz.getDeclaredFields();
|
|
||||||
if (fields == null || fields.length == 0) {
|
|
||||||
Class superClazz = clazz.getSuperclass();
|
|
||||||
if (superClazz != null) {
|
|
||||||
fields = getFields(superClazz);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取对象名
|
* 获取对象名
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user