bug 修复,代码优化

This commit is contained in:
AriaLyy
2017-01-02 15:29:58 +08:00
parent cd2e27d55f
commit 32e7c7f235
13 changed files with 217 additions and 69 deletions

View File

@ -20,6 +20,7 @@ import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.util.CheckUtil;
/**
* Created by lyy on 2016/12/5.
@ -30,6 +31,9 @@ public class AMReceiver {
OnSchedulerListener listener;
DownloadEntity entity;
/**
* {@link #load(String)},请使用该方法
*/
@Deprecated public AMTarget load(DownloadEntity entity) {
this.entity = entity;
return new AMTarget(this);
@ -39,10 +43,13 @@ public class AMReceiver {
* 读取下载链接
*/
public AMTarget load(@NonNull String downloadUrl) {
if (TextUtils.isEmpty(downloadUrl)) {
throw new IllegalArgumentException("下载链接不能为null");
CheckUtil.checkDownloadUrl(downloadUrl);
if (entity == null) {
entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
}
if (entity == null) {
entity = new DownloadEntity();
}
entity = new DownloadEntity();
entity.setDownloadUrl(downloadUrl);
return new AMTarget(this);
}

View File

@ -19,6 +19,7 @@ import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
@ -56,6 +57,33 @@ public class AMTarget {
return this;
}
/**
* 获取下载文件大小
*/
public long getFileSize() {
DownloadEntity entity = getDownloadEntity(mReceiver.entity.getDownloadUrl());
if (entity == null) {
throw new NullPointerException("下载管理器中没有改任务");
}
return entity.getFileSize();
}
/**
* 获取当前下载进度,如果下載实体存在,则返回当前进度
*/
public long getCurrentProgress() {
DownloadEntity entity = getDownloadEntity(mReceiver.entity.getDownloadUrl());
if (entity == null) {
throw new NullPointerException("下载管理器中没有改任务");
}
return entity.getCurrentProgress();
}
private DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
}
/**
* 添加任务
*/
@ -117,14 +145,4 @@ public class AMTarget {
cancel();
start();
}
//static class Build {
// DownloadEntity entity;
//
// Build(DownloadEntity entity) {
// this.entity = entity;
// }
//
//
//}
}

View File

@ -29,9 +29,11 @@ import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.util.Configuration;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
@ -81,11 +83,15 @@ import java.util.Set;
* 通过下载链接获取下载实体
*/
public DownloadEntity getDownloadEntity(String downloadUrl) {
if (TextUtils.isEmpty(downloadUrl)) {
throw new IllegalArgumentException("下载链接不能为null");
}
return DownloadEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { downloadUrl });
CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
}
/**
* 下载任务是否存在
*/
public boolean taskExists(String downloadUrl) {
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
}
/**

View File

@ -524,6 +524,7 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
*/
private void progress(long len) {
synchronized (LOCK) {
currentLocation += len;
mCurrentLocation += len;
mListener.onProgress(mCurrentLocation);
}

View File

@ -44,7 +44,6 @@ public class Task {
private Handler mOutHandler;
private Context mContext;
private IDownloadUtil mUtil;
private long mSpeed;
private Task(Context context, DownloadEntity entity, Handler outHandler) {
mContext = context.getApplicationContext();
@ -62,7 +61,21 @@ public class Task {
* 获取下载速度
*/
public long getSpeed() {
return mSpeed;
return mEntity.getSpeed();
}
/**
* 获取文件大小
*/
public long getFileSize(){
return mEntity.getFileSize();
}
/**
* 获取当前下载进度
*/
public long getCurrentProgress(){
return mEntity.getCurrentProgress();
}
/**
@ -111,13 +124,6 @@ public class Task {
}
}
/**
* 获取下载工具
*/
public IDownloadUtil getDownloadUtil() {
return mUtil;
}
/**
* 任务下载状态
*/
@ -221,7 +227,7 @@ public class Task {
this.context = context;
this.outHandler = new WeakReference<>(outHandler);
this.wTask = new WeakReference<>(task);
task = wTask.get();
this.task = wTask.get();
this.downloadEntity = this.task.getDownloadEntity();
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, downloadEntity);

View File

@ -55,24 +55,28 @@ public class DbEntity {
/**
* 查询一组数据
* <code>
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
* </code>
*
* @return 没有数据返回null
*/
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, @NonNull String[] wheres,
@NonNull String[] values) {
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, String... expression) {
DbUtil util = DbUtil.getInstance();
return util.findData(clazz, wheres, values);
return util.findData(clazz, expression);
}
/**
* 查询一行数据
* <code>
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
* </code>
*
* @return 没有数据返回null
*/
public static <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
@NonNull String[] values) {
public static <T extends DbEntity> T findData(Class<T> clazz, String... expression) {
DbUtil util = DbUtil.getInstance();
List<T> datas = util.findData(clazz, wheres, values);
List<T> datas = util.findData(clazz, expression);
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
}
@ -94,14 +98,19 @@ public class DbEntity {
* 删除当前数据
*/
public void deleteData() {
mUtil.delData(getClass(), new Object[] { "rowid" }, new Object[] { rowID });
//mUtil.delData(getClass(), new Object[] { "rowid" }, new Object[] { rowID });
deleteData(getClass(), "rowid=?", rowID + "");
}
/**
* 根据条件删除数据
* <code>
* DownloadEntity.deleteData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
* </code>
*/
public void deleteData(@NonNull Object[] wheres, @NonNull Object[] values) {
mUtil.delData(getClass(), wheres, values);
public static <T extends DbEntity> void deleteData(Class<T> clazz, String... expression) {
DbUtil util = DbUtil.getInstance();
util.delData(clazz, expression);
}
/**
@ -128,7 +137,7 @@ public class DbEntity {
* 查找数据在表中是否存在
*/
private boolean thisIsExist() {
return findData(getClass(), new String[] { "rowid" }, new String[] { rowID + "" }) != null;
return findData(getClass(), "rowid=?", rowID + "") != null;
}
/**
@ -139,6 +148,13 @@ public class DbEntity {
updateRowID();
}
private <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
@NonNull String[] values) {
DbUtil util = DbUtil.getInstance();
List<T> list = util.findData(clazz, wheres, values);
return list == null ? null : list.get(0);
}
private void updateRowID() {
try {
Field[] fields = CommonUtil.getFields(getClass());

View File

@ -22,6 +22,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.util.Log;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
@ -76,8 +77,8 @@ public class DbUtil {
/**
* 删除某条数据
*/
synchronized <T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
@NonNull Object[] values) {
@Deprecated private synchronized <T extends DbEntity> void delData(Class<T> clazz,
@NonNull Object[] wheres, @NonNull Object[] values) {
mDb = mHelper.getWritableDatabase();
if (wheres.length <= 0 || values.length <= 0) {
Log.e(TAG, "输入删除条件");
@ -99,6 +100,25 @@ public class DbUtil {
close();
}
/**
* 删除某条数据
*/
synchronized <T extends DbEntity> void delData(Class<T> clazz, String... expression) {
CheckUtil.checkSqlExpression(expression);
mDb = mHelper.getWritableDatabase();
String sql = "DELETE 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] = "'" + expression[i + 1] + "'";
}
sql = String.format(sql, params);
Log.d(TAG, sql);
print(DEL_DATA, sql);
mDb.execSQL(sql);
close();
}
/**
* 修改某行数据
*/
@ -151,8 +171,31 @@ public class DbUtil {
/**
* 条件查寻数据
*/
synchronized <T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
@NonNull String[] values) {
synchronized <T extends DbEntity> List<T> findData(Class<T> clazz, String... expression) {
if (!tableExists(clazz)) {
createTable(clazz);
}
mDb = mHelper.getReadableDatabase();
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] = "'" + expression[i + 1] + "'";
}
sql = String.format(sql, params);
Log.d(TAG, sql);
print(FIND_DATA, sql);
Cursor cursor = mDb.rawQuery(sql, null);
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
}
/**
* 条件查寻数据
*/
@Deprecated synchronized <T extends DbEntity> List<T> findData(Class<T> clazz,
@NonNull String[] wheres, @NonNull String[] values) {
if (!tableExists(clazz)) {
createTable(clazz);
}

View File

@ -26,7 +26,7 @@ import android.text.TextUtils;
* Created by lyy on 2015/11/2.
* sql帮助类
*/
public class SqlHelper extends SQLiteOpenHelper {
final class SqlHelper extends SQLiteOpenHelper {
protected static String DB_NAME;
protected static int VERSION = -1;

View File

@ -20,6 +20,8 @@ import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.DownloadEntity;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Lyy on 2016/9/23.
@ -28,6 +30,41 @@ import java.io.File;
public class CheckUtil {
private static final String TAG = "CheckUtil";
/**
* 检查sql的expression是否合法
*/
public static void checkSqlExpression(String... expression) {
if (expression.length == 0) {
throw new IllegalArgumentException("sql语句表达式不能为null");
}
if (expression.length == 1) {
throw new IllegalArgumentException("表达式需要写入参数");
}
String where = expression[0];
if (!where.contains("?")) {
throw new IllegalArgumentException("请在where语句的'='后编写?");
}
Pattern pattern = Pattern.compile("\\?");
Matcher matcher = pattern.matcher(where);
int count = 0;
while (matcher.find()) {
count++;
}
if (count < expression.length - 1){
throw new IllegalArgumentException("条件语句的?个数不能小于参数个数");
}
if (count > expression.length - 1){
throw new IllegalArgumentException("条件语句的?个数不能大于参数个数");
}
}
/**
* 检测下载链接是否为null
*/
public static void checkDownloadUrl(String downloadUrl) {
if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null");
}
/**
* 检测下载实体是否合法
*