bug 修复,代码优化
This commit is contained in:
@ -20,6 +20,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||||
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
|
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
|
||||||
|
import com.arialyy.aria.util.CheckUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2016/12/5.
|
* Created by lyy on 2016/12/5.
|
||||||
@ -30,6 +31,9 @@ public class AMReceiver {
|
|||||||
OnSchedulerListener listener;
|
OnSchedulerListener listener;
|
||||||
DownloadEntity entity;
|
DownloadEntity entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link #load(String)},请使用该方法
|
||||||
|
*/
|
||||||
@Deprecated public AMTarget load(DownloadEntity entity) {
|
@Deprecated public AMTarget load(DownloadEntity entity) {
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
return new AMTarget(this);
|
return new AMTarget(this);
|
||||||
@ -39,10 +43,13 @@ public class AMReceiver {
|
|||||||
* 读取下载链接
|
* 读取下载链接
|
||||||
*/
|
*/
|
||||||
public AMTarget load(@NonNull String downloadUrl) {
|
public AMTarget load(@NonNull String downloadUrl) {
|
||||||
if (TextUtils.isEmpty(downloadUrl)) {
|
CheckUtil.checkDownloadUrl(downloadUrl);
|
||||||
throw new IllegalArgumentException("下载链接不能为null");
|
if (entity == null) {
|
||||||
|
entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||||
|
}
|
||||||
|
if (entity == null) {
|
||||||
|
entity = new DownloadEntity();
|
||||||
}
|
}
|
||||||
entity = new DownloadEntity();
|
|
||||||
entity.setDownloadUrl(downloadUrl);
|
entity.setDownloadUrl(downloadUrl);
|
||||||
return new AMTarget(this);
|
return new AMTarget(this);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import com.arialyy.aria.core.command.CmdFactory;
|
import com.arialyy.aria.core.command.CmdFactory;
|
||||||
import com.arialyy.aria.core.command.IDownloadCmd;
|
import com.arialyy.aria.core.command.IDownloadCmd;
|
||||||
|
import com.arialyy.aria.util.CheckUtil;
|
||||||
import com.arialyy.aria.util.CommonUtil;
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -56,6 +57,33 @@ public class AMTarget {
|
|||||||
return this;
|
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();
|
cancel();
|
||||||
start();
|
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.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.arialyy.aria.core.command.CmdFactory;
|
import com.arialyy.aria.core.command.CmdFactory;
|
||||||
|
import com.arialyy.aria.util.CheckUtil;
|
||||||
import com.arialyy.aria.util.CommonUtil;
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
import com.arialyy.aria.core.command.IDownloadCmd;
|
import com.arialyy.aria.core.command.IDownloadCmd;
|
||||||
import com.arialyy.aria.util.Configuration;
|
import com.arialyy.aria.util.Configuration;
|
||||||
|
import java.io.File;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -81,11 +83,15 @@ import java.util.Set;
|
|||||||
* 通过下载链接获取下载实体
|
* 通过下载链接获取下载实体
|
||||||
*/
|
*/
|
||||||
public DownloadEntity getDownloadEntity(String downloadUrl) {
|
public DownloadEntity getDownloadEntity(String downloadUrl) {
|
||||||
if (TextUtils.isEmpty(downloadUrl)) {
|
CheckUtil.checkDownloadUrl(downloadUrl);
|
||||||
throw new IllegalArgumentException("下载链接不能为null");
|
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||||
}
|
}
|
||||||
return DownloadEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
|
|
||||||
new String[] { 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) {
|
private void progress(long len) {
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
|
currentLocation += len;
|
||||||
mCurrentLocation += len;
|
mCurrentLocation += len;
|
||||||
mListener.onProgress(mCurrentLocation);
|
mListener.onProgress(mCurrentLocation);
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,6 @@ public class Task {
|
|||||||
private Handler mOutHandler;
|
private Handler mOutHandler;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private IDownloadUtil mUtil;
|
private IDownloadUtil mUtil;
|
||||||
private long mSpeed;
|
|
||||||
|
|
||||||
private Task(Context context, DownloadEntity entity, Handler outHandler) {
|
private Task(Context context, DownloadEntity entity, Handler outHandler) {
|
||||||
mContext = context.getApplicationContext();
|
mContext = context.getApplicationContext();
|
||||||
@ -62,7 +61,21 @@ public class Task {
|
|||||||
* 获取下载速度
|
* 获取下载速度
|
||||||
*/
|
*/
|
||||||
public long getSpeed() {
|
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.context = context;
|
||||||
this.outHandler = new WeakReference<>(outHandler);
|
this.outHandler = new WeakReference<>(outHandler);
|
||||||
this.wTask = new WeakReference<>(task);
|
this.wTask = new WeakReference<>(task);
|
||||||
task = wTask.get();
|
this.task = wTask.get();
|
||||||
this.downloadEntity = this.task.getDownloadEntity();
|
this.downloadEntity = this.task.getDownloadEntity();
|
||||||
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
|
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
|
||||||
sendIntent.putExtra(Aria.ENTITY, downloadEntity);
|
sendIntent.putExtra(Aria.ENTITY, downloadEntity);
|
||||||
|
@ -55,24 +55,28 @@ public class DbEntity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询一组数据
|
* 查询一组数据
|
||||||
|
* <code>
|
||||||
|
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||||
|
* </code>
|
||||||
*
|
*
|
||||||
* @return 没有数据返回null
|
* @return 没有数据返回null
|
||||||
*/
|
*/
|
||||||
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, @NonNull String[] wheres,
|
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, String... expression) {
|
||||||
@NonNull String[] values) {
|
|
||||||
DbUtil util = DbUtil.getInstance();
|
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
|
* @return 没有数据返回null
|
||||||
*/
|
*/
|
||||||
public static <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
public static <T extends DbEntity> T findData(Class<T> clazz, String... expression) {
|
||||||
@NonNull String[] values) {
|
|
||||||
DbUtil util = DbUtil.getInstance();
|
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;
|
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,14 +98,19 @@ public class DbEntity {
|
|||||||
* 删除当前数据
|
* 删除当前数据
|
||||||
*/
|
*/
|
||||||
public void deleteData() {
|
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) {
|
public static <T extends DbEntity> void deleteData(Class<T> clazz, String... expression) {
|
||||||
mUtil.delData(getClass(), wheres, values);
|
DbUtil util = DbUtil.getInstance();
|
||||||
|
util.delData(clazz, expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -128,7 +137,7 @@ public class DbEntity {
|
|||||||
* 查找数据在表中是否存在
|
* 查找数据在表中是否存在
|
||||||
*/
|
*/
|
||||||
private boolean thisIsExist() {
|
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();
|
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() {
|
private void updateRowID() {
|
||||||
try {
|
try {
|
||||||
Field[] fields = CommonUtil.getFields(getClass());
|
Field[] fields = CommonUtil.getFields(getClass());
|
||||||
|
@ -22,6 +22,7 @@ import android.database.Cursor;
|
|||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import com.arialyy.aria.util.CheckUtil;
|
||||||
import com.arialyy.aria.util.CommonUtil;
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -76,8 +77,8 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 删除某条数据
|
* 删除某条数据
|
||||||
*/
|
*/
|
||||||
synchronized <T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
|
@Deprecated private synchronized <T extends DbEntity> void delData(Class<T> clazz,
|
||||||
@NonNull Object[] values) {
|
@NonNull Object[] wheres, @NonNull Object[] values) {
|
||||||
mDb = mHelper.getWritableDatabase();
|
mDb = mHelper.getWritableDatabase();
|
||||||
if (wheres.length <= 0 || values.length <= 0) {
|
if (wheres.length <= 0 || values.length <= 0) {
|
||||||
Log.e(TAG, "输入删除条件");
|
Log.e(TAG, "输入删除条件");
|
||||||
@ -99,6 +100,25 @@ public class DbUtil {
|
|||||||
close();
|
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,
|
synchronized <T extends DbEntity> List<T> findData(Class<T> clazz, String... expression) {
|
||||||
@NonNull String[] values) {
|
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)) {
|
if (!tableExists(clazz)) {
|
||||||
createTable(clazz);
|
createTable(clazz);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import android.text.TextUtils;
|
|||||||
* Created by lyy on 2015/11/2.
|
* Created by lyy on 2015/11/2.
|
||||||
* sql帮助类
|
* sql帮助类
|
||||||
*/
|
*/
|
||||||
public class SqlHelper extends SQLiteOpenHelper {
|
final class SqlHelper extends SQLiteOpenHelper {
|
||||||
protected static String DB_NAME;
|
protected static String DB_NAME;
|
||||||
protected static int VERSION = -1;
|
protected static int VERSION = -1;
|
||||||
|
|
||||||
|
@ -20,6 +20,8 @@ import android.text.TextUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.arialyy.aria.core.DownloadEntity;
|
import com.arialyy.aria.core.DownloadEntity;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Lyy on 2016/9/23.
|
* Created by Lyy on 2016/9/23.
|
||||||
@ -28,6 +30,41 @@ import java.io.File;
|
|||||||
public class CheckUtil {
|
public class CheckUtil {
|
||||||
private static final String TAG = "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) {
|
@Override public void onTaskPre(Task task) {
|
||||||
super.onTaskPre(task);
|
super.onTaskPre(task);
|
||||||
L.d(TAG, "download pre");
|
L.d(TAG, "download pre");
|
||||||
|
@ -19,6 +19,8 @@ package com.arialyy.simple.activity;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@ -26,6 +28,7 @@ import android.os.Message;
|
|||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
@ -53,6 +56,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
public static final int DOWNLOAD_RUNNING = 0x07;
|
public static final int DOWNLOAD_RUNNING = 0x07;
|
||||||
private static final String DOWNLOAD_URL =
|
private static final String DOWNLOAD_URL =
|
||||||
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
|
"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.progressBar) HorizontalProgressBarWithNumber mPb;
|
||||||
@Bind(R.id.start) Button mStart;
|
@Bind(R.id.start) Button mStart;
|
||||||
@Bind(R.id.stop) Button mStop;
|
@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.size) TextView mSize;
|
||||||
@Bind(R.id.toolbar) Toolbar toolbar;
|
@Bind(R.id.toolbar) Toolbar toolbar;
|
||||||
@Bind(R.id.speed) TextView mSpeed;
|
@Bind(R.id.speed) TextView mSpeed;
|
||||||
|
@Bind(R.id.img) ImageView mImg;
|
||||||
private DownloadEntity mEntity;
|
private DownloadEntity mEntity;
|
||||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
@Override public void onReceive(Context context, Intent intent) {
|
@Override public void onReceive(Context context, Intent intent) {
|
||||||
@ -76,14 +82,14 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case DOWNLOAD_RUNNING:
|
case DOWNLOAD_RUNNING:
|
||||||
Task task = (Task) msg.obj;
|
Task task = (Task) msg.obj;
|
||||||
long current = task.getDownloadEntity().getCurrentProgress();
|
long current = task.getCurrentProgress();
|
||||||
long len = task.getDownloadEntity().getFileSize();
|
long len = task.getFileSize();
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
mPb.setProgress(0);
|
mPb.setProgress(0);
|
||||||
} else {
|
} else {
|
||||||
mPb.setProgress((int) ((current * 100) / len));
|
mPb.setProgress((int) ((current * 100) / len));
|
||||||
}
|
}
|
||||||
mSpeed.setText(CommonUtil.formatFileSize(task.getDownloadEntity().getSpeed()) + "/s");
|
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
|
||||||
break;
|
break;
|
||||||
case DOWNLOAD_PRE:
|
case DOWNLOAD_PRE:
|
||||||
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
|
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
|
||||||
@ -117,6 +123,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
mStart.setText("重新开始?");
|
mStart.setText("重新开始?");
|
||||||
mCancel.setEnabled(false);
|
mCancel.setEnabled(false);
|
||||||
setBtState(true);
|
setBtState(true);
|
||||||
|
|
||||||
|
//String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg";
|
||||||
|
//Bitmap bm = BitmapFactory.decodeFile(path);
|
||||||
|
//mImg.setImageBitmap(bm);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,21 +170,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
mEntity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
|
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
|
||||||
if (mEntity != null) {
|
AMTarget target = Aria.whit(this).load(DOWNLOAD_URL);
|
||||||
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
|
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
|
||||||
mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize()));
|
mPb.setProgress(p);
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,22 +197,28 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void resume() {
|
private void resume() {
|
||||||
Aria.whit(this).load(mEntity).resume();
|
Aria.whit(this).load(DOWNLOAD_URL).resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void start() {
|
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() {
|
private void stop() {
|
||||||
Aria.whit(this).load(mEntity).stop();
|
Aria.whit(this).load(DOWNLOAD_URL).stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cancel() {
|
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) {
|
@Override public void onTaskStart(Task task) {
|
||||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
||||||
.sendToTarget();
|
.sendToTarget();
|
||||||
@ -220,8 +226,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
|
|
||||||
@Override public void onTaskResume(Task task) {
|
@Override public void onTaskResume(Task task) {
|
||||||
super.onTaskResume(task);
|
super.onTaskResume(task);
|
||||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getFileSize()).sendToTarget();
|
||||||
.sendToTarget();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onTaskStop(Task task) {
|
@Override public void onTaskStop(Task task) {
|
||||||
|
@ -19,6 +19,7 @@ package com.arialyy.simple.adapter;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
android:id="@+id/handle_bar"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_below="@+id/progressBar"
|
android:layout_below="@+id/progressBar"
|
||||||
@ -79,4 +80,11 @@
|
|||||||
style="?buttonBarButtonStyle"
|
style="?buttonBarButtonStyle"
|
||||||
/>
|
/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/img"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_below="@+id/handle_bar"
|
||||||
|
/>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
Reference in New Issue
Block a user