fix bug
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
package com.arialyy.simple.adapter;
|
package com.arialyy.simple.adapter;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.SparseIntArray;
|
import android.content.res.Resources;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
|
||||||
@ -11,7 +11,6 @@ import com.arialyy.downloadutil.core.DownloadManager;
|
|||||||
import com.arialyy.downloadutil.core.command.CommandFactory;
|
import com.arialyy.downloadutil.core.command.CommandFactory;
|
||||||
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
||||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||||
import com.arialyy.downloadutil.util.Util;
|
|
||||||
import com.arialyy.simple.R;
|
import com.arialyy.simple.R;
|
||||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
||||||
|
|
||||||
@ -19,6 +18,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
|
|
||||||
@ -30,15 +30,15 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
private static final String TAG = "DownloadAdapter";
|
private static final String TAG = "DownloadAdapter";
|
||||||
private DownloadManager mManager;
|
private DownloadManager mManager;
|
||||||
private CommandFactory mFactory;
|
private CommandFactory mFactory;
|
||||||
private Map<String, Long> mProgress = new HashMap<>();
|
private Map<String, Long> mProgress = new HashMap<>();
|
||||||
private SparseIntArray mPositions = new SparseIntArray();
|
private Map<String, Integer> mPositions = new HashMap<>();
|
||||||
|
|
||||||
public DownloadAdapter(Context context, List<DownloadEntity> data) {
|
public DownloadAdapter(Context context, List<DownloadEntity> data) {
|
||||||
super(context, data);
|
super(context, data);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (DownloadEntity entity : data) {
|
for (DownloadEntity entity : data) {
|
||||||
mProgress.put(entity.getDownloadUrl(), entity.getCurrentProgress());
|
mProgress.put(entity.getDownloadUrl(), entity.getCurrentProgress());
|
||||||
mPositions.append(i, Util.keyToHashCode(entity.getDownloadUrl()));
|
mPositions.put(entity.getDownloadUrl(), i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
mFactory = CommandFactory.getInstance();
|
mFactory = CommandFactory.getInstance();
|
||||||
@ -59,11 +59,21 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
|
|
||||||
public synchronized void setProgress(String url, long currentPosition) {
|
public synchronized void setProgress(String url, long currentPosition) {
|
||||||
mProgress.put(url, currentPosition);
|
mProgress.put(url, currentPosition);
|
||||||
|
// int index = indexItem(url);
|
||||||
|
// L.d(TAG, "index ==> " + index);
|
||||||
|
// notifyItemChanged(index);
|
||||||
notifyItemChanged(indexItem(url));
|
notifyItemChanged(indexItem(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
private int indexItem(String url) {
|
private synchronized int indexItem(String url) {
|
||||||
return mPositions.indexOfValue(Util.keyToHashCode(url));
|
Set set = mPositions.entrySet();
|
||||||
|
for (Object aSet : set) {
|
||||||
|
Map.Entry entry = (Map.Entry) aSet;
|
||||||
|
if (entry.getKey().equals(url)) {
|
||||||
|
return (int) entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected void bindData(MyHolder holder, int position, DownloadEntity item) {
|
@Override protected void bindData(MyHolder holder, int position, DownloadEntity item) {
|
||||||
@ -81,7 +91,8 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
holder.bt.setTag(holder.bt.getId(), listener);
|
holder.bt.setTag(holder.bt.getId(), listener);
|
||||||
}
|
}
|
||||||
holder.bt.setOnClickListener(listener);
|
holder.bt.setOnClickListener(listener);
|
||||||
String str = "";
|
String str = "";
|
||||||
|
int color = android.R.color.holo_green_light;
|
||||||
switch (item.getState()) {
|
switch (item.getState()) {
|
||||||
case DownloadEntity.STATE_WAIT:
|
case DownloadEntity.STATE_WAIT:
|
||||||
case DownloadEntity.STATE_OTHER:
|
case DownloadEntity.STATE_OTHER:
|
||||||
@ -90,12 +101,23 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
break;
|
break;
|
||||||
case DownloadEntity.STATE_STOP:
|
case DownloadEntity.STATE_STOP:
|
||||||
str = "恢复";
|
str = "恢复";
|
||||||
|
color = android.R.color.holo_blue_light;
|
||||||
break;
|
break;
|
||||||
case DownloadEntity.STATE_DOWNLOAD_ING:
|
case DownloadEntity.STATE_DOWNLOAD_ING:
|
||||||
str = "暂停";
|
str = "暂停";
|
||||||
|
color = android.R.color.holo_red_light;
|
||||||
|
break;
|
||||||
|
case DownloadEntity.STATE_COMPLETE:
|
||||||
|
str = "重新开始?";
|
||||||
|
holder.progress.setProgress(100);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
holder.bt.setText(str);
|
holder.bt.setText(str);
|
||||||
|
holder.bt.setTextColor(getColor(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getColor(int color) {
|
||||||
|
return Resources.getSystem().getColor(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BtClickListener implements View.OnClickListener {
|
private class BtClickListener implements View.OnClickListener {
|
||||||
@ -111,6 +133,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
case DownloadEntity.STATE_OTHER:
|
case DownloadEntity.STATE_OTHER:
|
||||||
case DownloadEntity.STATE_FAIL:
|
case DownloadEntity.STATE_FAIL:
|
||||||
case DownloadEntity.STATE_STOP:
|
case DownloadEntity.STATE_STOP:
|
||||||
|
case DownloadEntity.STATE_COMPLETE:
|
||||||
start(entity);
|
start(entity);
|
||||||
break;
|
break;
|
||||||
case DownloadEntity.STATE_DOWNLOAD_ING:
|
case DownloadEntity.STATE_DOWNLOAD_ING:
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
<string-array name="test_apk_download_url">
|
<string-array name="test_apk_download_url">
|
||||||
<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item> <!--300M的文件-->
|
<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item> <!--300M的文件-->
|
||||||
<!--<item>http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk</item>-->
|
<item>http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk</item>
|
||||||
<!--<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk</item>-->
|
<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk</item>
|
||||||
<!--<item>http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk</item>-->
|
<item>http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk</item>
|
||||||
<!--<item>http://static.gaoshouyou.com/d/18/cf/ba18113bc6cf56c1c5863e761e717003.apk</item>-->
|
<!--<item>http://static.gaoshouyou.com/d/18/cf/ba18113bc6cf56c1c5863e761e717003.apk</item>-->
|
||||||
<!--<item>http://static.gaoshouyou.com/d/60/17/2460921367173ea7145f11194a6f2587.apk</item>-->
|
<!--<item>http://static.gaoshouyou.com/d/60/17/2460921367173ea7145f11194a6f2587.apk</item>-->
|
||||||
<!--<item>http://static.gaoshouyou.com/d/32/e0/1a32123ecbe0ee010d35479df248f90f.apk</item>-->
|
<!--<item>http://static.gaoshouyou.com/d/32/e0/1a32123ecbe0ee010d35479df248f90f.apk</item>-->
|
||||||
|
@ -100,9 +100,10 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取任务执行池
|
* 获取任务执行池
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public ExecutePool getExecutePool(){
|
public ExecutePool getExecutePool() {
|
||||||
return mExecutePool;
|
return mExecutePool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,11 +153,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
|||||||
}
|
}
|
||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case STOP:
|
case STOP:
|
||||||
startNextTask(entity);
|
|
||||||
break;
|
|
||||||
case CANCEL:
|
case CANCEL:
|
||||||
startNextTask(entity);
|
|
||||||
break;
|
|
||||||
case COMPLETE:
|
case COMPLETE:
|
||||||
startNextTask(entity);
|
startNextTask(entity);
|
||||||
break;
|
break;
|
||||||
@ -187,7 +184,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
|||||||
target.mTargetListener.onTaskCancel(task);
|
target.mTargetListener.onTaskCancel(task);
|
||||||
break;
|
break;
|
||||||
case COMPLETE:
|
case COMPLETE:
|
||||||
target.mTargetListener.onTaskCancel(task);
|
target.mTargetListener.onTaskComplete(task);
|
||||||
break;
|
break;
|
||||||
case FAIL:
|
case FAIL:
|
||||||
target.mTargetListener.onTaskFail(task);
|
target.mTargetListener.onTaskFail(task);
|
||||||
@ -222,7 +219,9 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
|||||||
Log.w(TAG, "没有下一任务");
|
Log.w(TAG, "没有下一任务");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
target.startTask(newTask);
|
if (newTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) {
|
||||||
|
target.startTask(newTask);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,25 +3,25 @@ package com.arialyy.downloadutil.core.pool;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.arialyy.downloadutil.util.Task;
|
|
||||||
import com.arialyy.downloadutil.core.inf.IPool;
|
import com.arialyy.downloadutil.core.inf.IPool;
|
||||||
|
import com.arialyy.downloadutil.util.Task;
|
||||||
import com.arialyy.downloadutil.util.Util;
|
import com.arialyy.downloadutil.util.Util;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.PriorityQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.Queue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2016/8/14.
|
* Created by lyy on 2016/8/14.
|
||||||
* 任务缓存池,所有下载任务最先缓存在这个池中
|
* 任务缓存池,所有下载任务最先缓存在这个池中
|
||||||
*/
|
*/
|
||||||
public class CachePool implements IPool {
|
public class CachePool implements IPool {
|
||||||
private static final String TAG = "CachePool";
|
private static final String TAG = "CachePool";
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
private static volatile CachePool INSTANCE = null;
|
private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数
|
||||||
private Map<String, Task> mCacheArray;
|
private static volatile CachePool INSTANCE = null;
|
||||||
private Queue<Task> mCacheQueue;
|
private Map<String, Task> mCacheArray;
|
||||||
|
private LinkedBlockingQueue<Task> mCacheQueue;
|
||||||
|
|
||||||
public static CachePool getInstance() {
|
public static CachePool getInstance() {
|
||||||
if (INSTANCE == null) {
|
if (INSTANCE == null) {
|
||||||
@ -33,7 +33,7 @@ public class CachePool implements IPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CachePool() {
|
private CachePool() {
|
||||||
mCacheQueue = new PriorityQueue<>();
|
mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM);
|
||||||
mCacheArray = new HashMap<>();
|
mCacheArray = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ public class CachePool implements IPool {
|
|||||||
Log.w(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "】");
|
Log.w(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "】");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
boolean s = mCacheQueue.offer(task);
|
boolean s = mCacheQueue.offer(task);
|
||||||
Log.d(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】"));
|
Log.d(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】"));
|
||||||
if (s) {
|
if (s) {
|
||||||
mCacheArray.put(Util.keyToHashKey(url), task);
|
mCacheArray.put(Util.keyToHashKey(url), task);
|
||||||
|
@ -89,7 +89,7 @@ public class ExecutePool implements IPool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
oldTask.stop();
|
oldTask.stop();
|
||||||
wait(200);
|
// wait(200);
|
||||||
String key = Util.keyToHashKey(oldTask.getDownloadEntity().getDownloadUrl());
|
String key = Util.keyToHashKey(oldTask.getDownloadEntity().getDownloadUrl());
|
||||||
mExecuteArray.remove(key);
|
mExecuteArray.remove(key);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
@ -59,7 +59,7 @@ public class DbEntity {
|
|||||||
/**
|
/**
|
||||||
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
||||||
*/
|
*/
|
||||||
public synchronized void save() {
|
public void save() {
|
||||||
if (mUtil.tableExists(getClass()) && thisIsExist()) {
|
if (mUtil.tableExists(getClass()) && thisIsExist()) {
|
||||||
update();
|
update();
|
||||||
} else {
|
} else {
|
||||||
|
@ -62,7 +62,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 删除某条数据
|
* 删除某条数据
|
||||||
*/
|
*/
|
||||||
<T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
|
synchronized <T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
|
||||||
@NonNull Object[] values) {
|
@NonNull Object[] values) {
|
||||||
mDb = mHelper.getWritableDatabase();
|
mDb = mHelper.getWritableDatabase();
|
||||||
if (wheres.length <= 0 || values.length <= 0) {
|
if (wheres.length <= 0 || values.length <= 0) {
|
||||||
@ -88,7 +88,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 修改某行数据
|
* 修改某行数据
|
||||||
*/
|
*/
|
||||||
void modifyData(DbEntity dbEntity) {
|
synchronized void modifyData(DbEntity dbEntity) {
|
||||||
mDb = mHelper.getWritableDatabase();
|
mDb = mHelper.getWritableDatabase();
|
||||||
Class<?> clazz = dbEntity.getClass();
|
Class<?> clazz = dbEntity.getClass();
|
||||||
Field[] fields = Util.getFields(clazz);
|
Field[] fields = Util.getFields(clazz);
|
||||||
@ -123,7 +123,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 遍历所有数据
|
* 遍历所有数据
|
||||||
*/
|
*/
|
||||||
<T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
synchronized <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||||
if (!tableExists(clazz)) {
|
if (!tableExists(clazz)) {
|
||||||
createTable(clazz);
|
createTable(clazz);
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 条件查寻数据
|
* 条件查寻数据
|
||||||
*/
|
*/
|
||||||
<T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
|
synchronized <T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
|
||||||
@NonNull String[] values) {
|
@NonNull String[] values) {
|
||||||
if (!tableExists(clazz)) {
|
if (!tableExists(clazz)) {
|
||||||
createTable(clazz);
|
createTable(clazz);
|
||||||
@ -167,7 +167,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 插入数据
|
* 插入数据
|
||||||
*/
|
*/
|
||||||
void insertData(DbEntity dbEntity) {
|
synchronized void insertData(DbEntity dbEntity) {
|
||||||
Class<?> clazz = dbEntity.getClass();
|
Class<?> clazz = dbEntity.getClass();
|
||||||
if (!tableExists(clazz)) {
|
if (!tableExists(clazz)) {
|
||||||
createTable(clazz);
|
createTable(clazz);
|
||||||
@ -245,7 +245,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 创建表
|
* 创建表
|
||||||
*/
|
*/
|
||||||
private void createTable(Class clazz) {
|
private synchronized void createTable(Class clazz) {
|
||||||
if (mDb == null || !mDb.isOpen()) {
|
if (mDb == null || !mDb.isOpen()) {
|
||||||
mDb = mHelper.getWritableDatabase();
|
mDb = mHelper.getWritableDatabase();
|
||||||
}
|
}
|
||||||
@ -321,7 +321,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 关闭数据库
|
* 关闭数据库
|
||||||
*/
|
*/
|
||||||
private void close() {
|
private synchronized void close() {
|
||||||
if (mDb != null) {
|
if (mDb != null) {
|
||||||
mDb.close();
|
mDb.close();
|
||||||
}
|
}
|
||||||
@ -330,7 +330,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取所在行Id
|
* 获取所在行Id
|
||||||
*/
|
*/
|
||||||
int[] getRowId(Class clazz) {
|
synchronized int[] getRowId(Class clazz) {
|
||||||
mDb = mHelper.getReadableDatabase();
|
mDb = mHelper.getReadableDatabase();
|
||||||
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(clazz), null);
|
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(clazz), null);
|
||||||
int[] ids = new int[cursor.getCount()];
|
int[] ids = new int[cursor.getCount()];
|
||||||
@ -347,7 +347,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取行Id
|
* 获取行Id
|
||||||
*/
|
*/
|
||||||
int getRowId(Class clazz, Object[] wheres, Object[] values) {
|
synchronized int getRowId(Class clazz, Object[] wheres, Object[] values) {
|
||||||
mDb = mHelper.getReadableDatabase();
|
mDb = mHelper.getReadableDatabase();
|
||||||
if (wheres.length <= 0 || values.length <= 0) {
|
if (wheres.length <= 0 || values.length <= 0) {
|
||||||
Log.e(TAG, "请输入删除条件");
|
Log.e(TAG, "请输入删除条件");
|
||||||
@ -375,7 +375,7 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 根据数据游标创建一个具体的对象
|
* 根据数据游标创建一个具体的对象
|
||||||
*/
|
*/
|
||||||
private <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz, Cursor cursor) {
|
private synchronized <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz, Cursor cursor) {
|
||||||
Field[] fields = Util.getFields(clazz);
|
Field[] fields = Util.getFields(clazz);
|
||||||
List<T> entitys = new ArrayList<>();
|
List<T> entitys = new ArrayList<>();
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
|
@ -65,7 +65,7 @@ public class Util {
|
|||||||
*/
|
*/
|
||||||
public static int keyToHashCode(String str) {
|
public static int keyToHashCode(String str) {
|
||||||
int total = 0;
|
int total = 0;
|
||||||
for (int i = 0; i < str.length() && i < 6; i++) {
|
for (int i = 0; i < str.length(); i++) {
|
||||||
char ch = str.charAt(i);
|
char ch = str.charAt(i);
|
||||||
if (ch == '-') ch = (char) 28; // does not contain the same last 5 bits as any letter
|
if (ch == '-') ch = (char) 28; // does not contain the same last 5 bits as any letter
|
||||||
if (ch == '\'') ch = (char) 29; // nor this
|
if (ch == '\'') ch = (char) 29; // nor this
|
||||||
|
Reference in New Issue
Block a user