Skip to content

Commit

Permalink
缓存任务网络异常则结束任务
Browse files Browse the repository at this point in the history
  • Loading branch information
smuyyh committed Sep 22, 2016
1 parent 300c959 commit 1f41884
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.justwayward.reader.bean.support;

/**
* @author yuyh.
* @date 16/8/14.
*/
public class DownloadError {

public String bookId;

public DownloadError(String bookId) {
this.bookId = bookId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import com.justwayward.reader.bean.BookToc;
import com.justwayward.reader.bean.ChapterRead;
import com.justwayward.reader.bean.support.DownloadComplete;
import com.justwayward.reader.bean.support.DownloadError;
import com.justwayward.reader.bean.support.DownloadProgress;
import com.justwayward.reader.bean.support.DownloadQueue;
import com.justwayward.reader.module.BookApiModule;
import com.justwayward.reader.utils.AppUtils;
import com.justwayward.reader.utils.BookPageFactory;
import com.justwayward.reader.utils.LogUtils;
import com.justwayward.reader.utils.NetworkUtils;
import com.justwayward.reader.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
Expand All @@ -44,6 +47,7 @@ public class DownloadBookService extends Service {
public BookApi bookApi;

public boolean isBusy = false; // 当前是否有下载任务在进行

@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -82,17 +86,21 @@ public static void post(DownloadQueue downloadQueue) {
EventBus.getDefault().post(downloadQueue);
}

public static void post(DownloadProgress progress){
public static void post(DownloadProgress progress) {
EventBus.getDefault().post(progress);
}

private void post(DownloadComplete complete) {
EventBus.getDefault().post(complete);
}

private void post(DownloadError error) {
EventBus.getDefault().post(error);
}

@Subscribe(threadMode = ThreadMode.MAIN)
public synchronized void addToDownloadQueue(DownloadQueue downloadQueue) {
if(!TextUtils.isEmpty(downloadQueue.bookId)) {
if (!TextUtils.isEmpty(downloadQueue.bookId)) {
LogUtils.e("addToDownloadQueue:" + downloadQueue.bookId);
boolean exists = false;
// 判断当前书籍缓存任务是否存在
Expand All @@ -111,11 +119,11 @@ public synchronized void addToDownloadQueue(DownloadQueue downloadQueue) {
// 添加到下载队列
downloadQueues.add(downloadQueue);
// 当前队列里有其他任务,才提示
if(downloadQueues.size() >1)
if (downloadQueues.size() > 1)
ToastUtils.showSingleToast("成功加入缓存队列");
}
// 从队列顺序取出第一条下载
if(downloadQueues.size() >0 && !isBusy){
if (downloadQueues.size() > 0 && !isBusy) {
isBusy = true;
downloadBook(downloadQueues.get(0));
}
Expand All @@ -133,12 +141,20 @@ public synchronized void downloadBook(final DownloadQueue downloadQueue) {
@Override
protected Integer doInBackground(Integer... params) {
int failureCount = 0;
for (int i = start; i <=end && i <= list.size(); i++) {
for (int i = start; i <= end && i <= list.size(); i++) {
// 网络异常,取消下载
if (!NetworkUtils.isAvailable(AppUtils.getAppContext())) {
downloadQueue.isCancel = true;
post(new DownloadError(bookId));
failureCount = -1;
break;
}
if (!downloadQueue.isFinish && !downloadQueue.isCancel) {
if (factory.getBookFile(i).length() < 50) { // 认为章节文件不存在,则下载
// 章节文件不存在,则下载,否则跳过
if (factory.getBookFile(i).length() < 50) {
BookToc.mixToc.Chapters chapters = list.get(i - 1);
String url = chapters.link;
int ret = download(factory, url, bookId, i);
int ret = download(url, bookId, i);
if (ret != 1) {
failureCount++;
}
Expand All @@ -155,10 +171,12 @@ protected Integer doInBackground(Integer... params) {
protected void onPostExecute(Integer failureCount) {
super.onPostExecute(failureCount);
downloadQueue.isFinish = true;
if (failureCount > -1) {
// 通知
post(new DownloadComplete(bookId));
}
// 下载完成,从队列里移除
downloadQueues.remove(downloadQueue);
// 通知
post(new DownloadComplete(bookId));
// 释放 空闲状态
isBusy = false;
// post一个空事件,通知继续执行下一个任务
Expand All @@ -169,7 +187,7 @@ protected void onPostExecute(Integer failureCount) {
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

private int download(final BookPageFactory factory, String url, final String bookId, final int chapter) {
private int download(String url, final String bookId, final int chapter) {

final int[] result = {-1};

Expand All @@ -179,7 +197,6 @@ private int download(final BookPageFactory factory, String url, final String boo
@Override
public void onNext(ChapterRead data) {
if (data.chapter != null) {
//factory.append(data.chapter, chapter);
post(new DownloadProgress(bookId, chapter));
result[0] = 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.justwayward.reader.bean.BookToc;
import com.justwayward.reader.bean.ChapterRead;
import com.justwayward.reader.bean.support.DownloadComplete;
import com.justwayward.reader.bean.support.DownloadError;
import com.justwayward.reader.bean.support.DownloadProgress;
import com.justwayward.reader.bean.support.DownloadQueue;
import com.justwayward.reader.component.AppComponent;
Expand Down Expand Up @@ -342,6 +343,24 @@ public void run() {
}
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void downloadError(DownloadError error) {
if (bookId.equals(error.bookId)) {
mTvDownloadProgress.postDelayed(new Runnable() {
@Override
public void run() {
mTvDownloadProgress.setText("网络异常,已取消下载");
}
}, 500);
mTvDownloadProgress.postDelayed(new Runnable() {
@Override
public void run() {
gone(mTvDownloadProgress);
}
}, 2500);
}
}

private void hideReadBar() { // 隐藏工具栏
gone(mTvDownloadProgress, mLlBookReadBottom, mLlBookReadTop);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,4 @@ public boolean onTouchEvent(MotionEvent event) {
}
return false;
}

}

0 comments on commit 1f41884

Please sign in to comment.