fix bug
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
package com.arialyy.simple.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.SparseIntArray;
|
||||
import android.content.res.Resources;
|
||||
import android.view.View;
|
||||
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.IDownloadCommand;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
||||
|
||||
@ -19,6 +18,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import butterknife.Bind;
|
||||
|
||||
@ -31,14 +31,14 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
private DownloadManager mManager;
|
||||
private CommandFactory mFactory;
|
||||
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) {
|
||||
super(context, data);
|
||||
int i = 0;
|
||||
for (DownloadEntity entity : data) {
|
||||
mProgress.put(entity.getDownloadUrl(), entity.getCurrentProgress());
|
||||
mPositions.append(i, Util.keyToHashCode(entity.getDownloadUrl()));
|
||||
mPositions.put(entity.getDownloadUrl(), i);
|
||||
i++;
|
||||
}
|
||||
mFactory = CommandFactory.getInstance();
|
||||
@ -59,11 +59,21 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
|
||||
public synchronized void setProgress(String url, long currentPosition) {
|
||||
mProgress.put(url, currentPosition);
|
||||
// int index = indexItem(url);
|
||||
// L.d(TAG, "index ==> " + index);
|
||||
// notifyItemChanged(index);
|
||||
notifyItemChanged(indexItem(url));
|
||||
}
|
||||
|
||||
private int indexItem(String url) {
|
||||
return mPositions.indexOfValue(Util.keyToHashCode(url));
|
||||
private synchronized int indexItem(String 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) {
|
||||
@ -82,6 +92,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
}
|
||||
holder.bt.setOnClickListener(listener);
|
||||
String str = "";
|
||||
int color = android.R.color.holo_green_light;
|
||||
switch (item.getState()) {
|
||||
case DownloadEntity.STATE_WAIT:
|
||||
case DownloadEntity.STATE_OTHER:
|
||||
@ -90,12 +101,23 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
break;
|
||||
case DownloadEntity.STATE_STOP:
|
||||
str = "恢复";
|
||||
color = android.R.color.holo_blue_light;
|
||||
break;
|
||||
case DownloadEntity.STATE_DOWNLOAD_ING:
|
||||
str = "暂停";
|
||||
color = android.R.color.holo_red_light;
|
||||
break;
|
||||
case DownloadEntity.STATE_COMPLETE:
|
||||
str = "重新开始?";
|
||||
holder.progress.setProgress(100);
|
||||
break;
|
||||
}
|
||||
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 {
|
||||
@ -111,6 +133,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
case DownloadEntity.STATE_OTHER:
|
||||
case DownloadEntity.STATE_FAIL:
|
||||
case DownloadEntity.STATE_STOP:
|
||||
case DownloadEntity.STATE_COMPLETE:
|
||||
start(entity);
|
||||
break;
|
||||
case DownloadEntity.STATE_DOWNLOAD_ING:
|
||||
|
@ -4,9 +4,9 @@
|
||||
|
||||
<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/21/e8/61218d78d0e8b79df68dbc18dd484c97.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/21/e8/61218d78d0e8b79df68dbc18dd484c97.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/18/cf/ba18113bc6cf56c1c5863e761e717003.apk</item>-->
|
||||
<!--<item>http://static.gaoshouyou.com/d/60/17/2460921367173ea7145f11194a6f2587.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
|
||||
*/
|
||||
public ExecutePool getExecutePool(){
|
||||
public ExecutePool getExecutePool() {
|
||||
return mExecutePool;
|
||||
}
|
||||
|
||||
@ -152,11 +153,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
||||
}
|
||||
switch (msg.what) {
|
||||
case STOP:
|
||||
startNextTask(entity);
|
||||
break;
|
||||
case CANCEL:
|
||||
startNextTask(entity);
|
||||
break;
|
||||
case COMPLETE:
|
||||
startNextTask(entity);
|
||||
break;
|
||||
@ -187,7 +184,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
||||
target.mTargetListener.onTaskCancel(task);
|
||||
break;
|
||||
case COMPLETE:
|
||||
target.mTargetListener.onTaskCancel(task);
|
||||
target.mTargetListener.onTaskComplete(task);
|
||||
break;
|
||||
case FAIL:
|
||||
target.mTargetListener.onTaskFail(task);
|
||||
@ -222,7 +219,9 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
||||
Log.w(TAG, "没有下一任务");
|
||||
return;
|
||||
}
|
||||
if (newTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) {
|
||||
target.startTask(newTask);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,14 +3,13 @@ package com.arialyy.downloadutil.core.pool;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.core.inf.IPool;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/14.
|
||||
@ -19,9 +18,10 @@ import java.util.Queue;
|
||||
public class CachePool implements IPool {
|
||||
private static final String TAG = "CachePool";
|
||||
private static final Object LOCK = new Object();
|
||||
private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数
|
||||
private static volatile CachePool INSTANCE = null;
|
||||
private Map<String, Task> mCacheArray;
|
||||
private Queue<Task> mCacheQueue;
|
||||
private LinkedBlockingQueue<Task> mCacheQueue;
|
||||
|
||||
public static CachePool getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
@ -33,7 +33,7 @@ public class CachePool implements IPool {
|
||||
}
|
||||
|
||||
private CachePool() {
|
||||
mCacheQueue = new PriorityQueue<>();
|
||||
mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM);
|
||||
mCacheArray = new HashMap<>();
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ public class ExecutePool implements IPool {
|
||||
return false;
|
||||
}
|
||||
oldTask.stop();
|
||||
wait(200);
|
||||
// wait(200);
|
||||
String key = Util.keyToHashKey(oldTask.getDownloadEntity().getDownloadUrl());
|
||||
mExecuteArray.remove(key);
|
||||
} catch (InterruptedException e) {
|
||||
|
@ -59,7 +59,7 @@ public class DbEntity {
|
||||
/**
|
||||
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
||||
*/
|
||||
public synchronized void save() {
|
||||
public void save() {
|
||||
if (mUtil.tableExists(getClass()) && thisIsExist()) {
|
||||
update();
|
||||
} 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) {
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
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();
|
||||
Class<?> clazz = dbEntity.getClass();
|
||||
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)) {
|
||||
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) {
|
||||
if (!tableExists(clazz)) {
|
||||
createTable(clazz);
|
||||
@ -167,7 +167,7 @@ public class DbUtil {
|
||||
/**
|
||||
* 插入数据
|
||||
*/
|
||||
void insertData(DbEntity dbEntity) {
|
||||
synchronized void insertData(DbEntity dbEntity) {
|
||||
Class<?> clazz = dbEntity.getClass();
|
||||
if (!tableExists(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()) {
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
}
|
||||
@ -321,7 +321,7 @@ public class DbUtil {
|
||||
/**
|
||||
* 关闭数据库
|
||||
*/
|
||||
private void close() {
|
||||
private synchronized void close() {
|
||||
if (mDb != null) {
|
||||
mDb.close();
|
||||
}
|
||||
@ -330,7 +330,7 @@ public class DbUtil {
|
||||
/**
|
||||
* 获取所在行Id
|
||||
*/
|
||||
int[] getRowId(Class clazz) {
|
||||
synchronized int[] getRowId(Class clazz) {
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(clazz), null);
|
||||
int[] ids = new int[cursor.getCount()];
|
||||
@ -347,7 +347,7 @@ public class DbUtil {
|
||||
/**
|
||||
* 获取行Id
|
||||
*/
|
||||
int getRowId(Class clazz, Object[] wheres, Object[] values) {
|
||||
synchronized int getRowId(Class clazz, Object[] wheres, Object[] values) {
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
if (wheres.length <= 0 || values.length <= 0) {
|
||||
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);
|
||||
List<T> entitys = new ArrayList<>();
|
||||
if (fields != null && fields.length > 0) {
|
||||
|
@ -65,7 +65,7 @@ public class Util {
|
||||
*/
|
||||
public static int keyToHashCode(String str) {
|
||||
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);
|
||||
if (ch == '-') ch = (char) 28; // does not contain the same last 5 bits as any letter
|
||||
if (ch == '\'') ch = (char) 29; // nor this
|
||||
|
Reference in New Issue
Block a user