bug 修复,代码优化

pull/2/head
AriaLyy 8 years ago
parent cd2e27d55f
commit 32e7c7f235
  1. 13
      Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java
  2. 38
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  3. 16
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  4. 1
      Aria/src/main/java/com/arialyy/aria/core/task/DownloadUtil.java
  5. 26
      Aria/src/main/java/com/arialyy/aria/core/task/Task.java
  6. 36
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  7. 51
      Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  9. 37
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  10. 2
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  11. 55
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  12. 1
      app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java
  13. 8
      app/src/main/res/layout/content_single.xml

@ -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);
}

@ -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;
// }
//
//
//}
}

@ -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;
}
/**

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

@ -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);

@ -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());

@ -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);
}

@ -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;

@ -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");
}
/**
* 检测下载实体是否合法
*

@ -132,7 +132,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
}
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
private class MySchedulerListener extends Aria.SimpleSchedulerListener{
@Override public void onTaskPre(Task task) {
super.onTaskPre(task);
L.d(TAG, "download pre");

@ -19,6 +19,8 @@ package com.arialyy.simple.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@ -26,6 +28,7 @@ import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
@ -53,6 +56,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_RUNNING = 0x07;
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
//private static final String DOWNLOAD_URL =
// "http://img13.poco.cn/mypoco/myphoto/20120828/15/55689209201208281549023849547194135_001.jpg";
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@ -60,6 +65,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Bind(R.id.size) TextView mSize;
@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.speed) TextView mSpeed;
@Bind(R.id.img) ImageView mImg;
private DownloadEntity mEntity;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
@ -76,14 +82,14 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
switch (msg.what) {
case DOWNLOAD_RUNNING:
Task task = (Task) msg.obj;
long current = task.getDownloadEntity().getCurrentProgress();
long len = task.getDownloadEntity().getFileSize();
long current = task.getCurrentProgress();
long len = task.getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
mSpeed.setText(CommonUtil.formatFileSize(task.getDownloadEntity().getSpeed()) + "/s");
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
break;
case DOWNLOAD_PRE:
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
@ -117,6 +123,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mStart.setText("重新开始?");
mCancel.setEnabled(false);
setBtState(true);
//String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg";
//Bitmap bm = BitmapFactory.decodeFile(path);
//mImg.setImageBitmap(bm);
break;
}
}
@ -159,21 +170,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void init() {
mEntity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
if (mEntity != null) {
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize()));
if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
setBtState(false);
} else if (mEntity.isDownloadComplete()) {
mStart.setText("重新开始?");
setBtState(true);
}
} else {
mEntity = new DownloadEntity();
mEntity.setFileName("test.apk");
mEntity.setDownloadUrl(DOWNLOAD_URL);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.whit(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}
}
@ -197,22 +197,28 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void resume() {
Aria.whit(this).load(mEntity).resume();
Aria.whit(this).load(DOWNLOAD_URL).resume();
}
private void start() {
Aria.whit(this).load(mEntity).start();
Aria.whit(this)
.load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
.setDownloadName("test.apk")
//.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.jpg")
//.setDownloadName("test.jpg")
.start();
}
private void stop() {
Aria.whit(this).load(mEntity).stop();
Aria.whit(this).load(DOWNLOAD_URL).stop();
}
private void cancel() {
Aria.whit(this).load(mEntity).cancel();
Aria.whit(this).load(DOWNLOAD_URL).cancel();
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener {
private class MySchedulerListener extends Aria.SimpleSchedulerListener {
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
@ -220,8 +226,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Override public void onTaskResume(Task task) {
super.onTaskResume(task);
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getFileSize()).sendToTarget();
}
@Override public void onTaskStop(Task task) {

@ -19,6 +19,7 @@ package com.arialyy.simple.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

@ -35,6 +35,7 @@
<LinearLayout
android:id="@+id/handle_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
@ -79,4 +80,11 @@
style="?buttonBarButtonStyle"
/>
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/handle_bar"
/>
</RelativeLayout>

Loading…
Cancel
Save