public class DownloadAssist {
public static final int IMAGE_DOWNLOADED_FINISHED = 0xFF000001;
private static final int MAX_TASK_NUM = 8;
private ArrayList<DownLoadMsg> mMsgQueue = null;
private Object mLock = new Object();
private final static int MAX_MSG_QUEUE_COUNT = 200;
private ImageLoader mImageLoader = null;
private Handler mHandler = null;
private DownLoadTask[] mDownLoadTasks = null;
private Context mApplication = null;
public DownloadAssist(Context context, String type, Handler handler){
mApplication = context;
mHandler = handler;
mImageLoader = new ImageLoader(context, type);
mMsgQueue = new ArrayList<DownLoadMsg>();
mDownLoadTasks = new DownLoadTask[MAX_TASK_NUM];
startDownLoadTask();
}
public boolean isOnline() {
ConnectivityManager cmAll = (ConnectivityManager) mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mActiveNetworkInfo = cmAll.getActiveNetworkInfo();
if (mActiveNetworkInfo != null && mActiveNetworkInfo.isConnected()) {
return true;
}
return false;
}
private void startDownLoadTask(){
stopDownLoadTask();
for(int i = 0; i < mDownLoadTasks.length; i++){
mDownLoadTasks[i] = new DownLoadTask();
try{
mDownLoadTasks[i].start();
}catch(Exception e){
}
}
}
private void stopDownLoadTask(){
for(int i = 0; i < mDownLoadTasks.length; i++){
if(mDownLoadTasks[i] != null){
try{
mDownLoadTasks[i].mEnableRunning = false;
}catch(Exception e){
}
}
}
}
public void destroyInstance(){
clearMsgQueue();
stopDownLoadTask();
}
public void removeTaskByCategoryId(int categoryId){
synchronized(mLock){
for(int i = 0; i < mMsgQueue.size(); i++){
DownLoadMsg msg = mMsgQueue.get(i);
if(msg.mCategoryId == categoryId){
mMsgQueue.remove(i);
i--;
}
}
}
}
public void downLoad(int categoryId, int medidId, String url){
downLoadImage(categoryId, medidId, url, false);
}
public void downLoad(int categoryId, int medidId, String url, boolean bFront){
DownLoadMsg msg = getDownLoadMsg(categoryId, medidId, url);
if(msg != null){
pushMsg(msg, bFront);
}
}
private void notifyDownloadFinished(int categoryId, int mediaId, Bitmap bitmap){
if(mHandler != null){
Message msg = new Message();
msg.what = IMAGE_DOWNLOADED_FINISHED;
msg.arg1 = categoryId;
msg.arg2 = mediaId;
msg.obj = bitmap;
mHandler.sendMessage(msg);
}
}
private void pushMsg(DownLoadMsg msg, boolean bFront){
synchronized(mLock){
while(mMsgQueue.size() >= MAX_MSG_QUEUE_COUNT){
mMsgQueue.remove(MAX_MSG_QUEUE_COUNT - 1);
}
for(int i = 0; i < mMsgQueue.size(); i++){
if((msg.mMediaId == mMsgQueue.get(i).mMediaId) &&
(msg.mCategoryId == mMsgQueue.get(i).mCategoryId)){
mMsgQueue.remove(i);
i--;
}
}
if(bFront){
mMsgQueue.add(0, msg);
}else{
mMsgQueue.add(mMsgQueue.size(), msg);
}
}
}
private void pushMsg(DownLoadMsg msg){
pushMsg(msg, false);
}
private DownLoadMsg popMsg(){
synchronized(mLock){
if(mMsgQueue.size() > 0){
DownLoadMsg msg = mMsgQueue.get(0);
mMsgQueue.remove(0);
return msg;
}
return null;
}
}
private void clearMsgQueue(){
synchronized(mLock){
mMsgQueue.clear();
}
}
private DownLoadMsg getDownLoadMsg(int categoryId, int mediaId, String url){
if(url == null){
return null;
}
DownLoadMsg msg = new DownLoadMsg();
msg.mCategoryId = categoryId;
msg.mMediaId = mediaId;
msg.mUrl = url;
return msg;
}
private class DownLoadMsg{
protected int mCategoryId;
protected int mMediaId;
protected String mUrl = null;
protected int mRepeatDownLoadCount = 0;
}
private class DownLoadTask extends Thread{
protected volatile boolean mEnableRunning = true;
private boolean mBSuccess = true;
@Override
public void run(){
while(true){
if(mEnableRunning == false){
break;
}
mBSuccess = true;
DownLoadMsg msg = popMsg();
if(msg != null){
try{
if((msg.mUrl != null) && (msg.mUrl.isEmpty() == false)){
Bitmap bitmap = mImageLoader.getBitmap(msg.mUrl);
notifyDownloadFinished(msg.mCategoryId, msg.mMediaId, bitmap);
if(bitmap == null){
mBSuccess = false;
}
}
}catch(Exception e){
mBSuccess = false;
}
}
try {
if(mBSuccess == false){
if(msg.mRepeatDownLoadCount < 5){
msg.mRepeatDownLoadCount++;
pushMsg(msg, true);
}else{
notifyDownloadFinished(msg.mCategoryId, msg.mMediaId, null);
}
}
sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
多线程下载
最新推荐文章于 2021-11-23 19:12:32 发布