Search in sources :

Example 1 with DownloadMessage

use of com.justwayward.reader.bean.support.DownloadMessage in project BookReader by JustWayward.

the class DownloadBookService method addToDownloadQueue.

@Subscribe(threadMode = ThreadMode.MAIN)
public synchronized void addToDownloadQueue(DownloadQueue queue) {
    if (!TextUtils.isEmpty(queue.bookId)) {
        boolean exists = false;
        // 判断当前书籍缓存任务是否存在
        for (int i = 0; i < downloadQueues.size(); i++) {
            if (downloadQueues.get(i).bookId.equals(queue.bookId)) {
                LogUtils.e("addToDownloadQueue:exists");
                exists = true;
                break;
            }
        }
        if (exists) {
            post(new DownloadMessage(queue.bookId, "当前缓存任务已存在", false));
            return;
        }
        // 添加到下载队列
        downloadQueues.add(queue);
        LogUtils.e("addToDownloadQueue:" + queue.bookId);
        post(new DownloadMessage(queue.bookId, "成功加入缓存队列", false));
    }
    // 从队列顺序取出第一条下载
    if (downloadQueues.size() > 0 && !isBusy) {
        isBusy = true;
        DownloadQueue downloadQueue = downloadQueues.get(0);
        downloadBook(downloadQueue);
    }
}
Also used : DownloadMessage(com.justwayward.reader.bean.support.DownloadMessage) DownloadQueue(com.justwayward.reader.bean.support.DownloadQueue) SyncOnSubscribe(rx.observables.SyncOnSubscribe) Subscribe(org.greenrobot.eventbus.Subscribe)

Example 2 with DownloadMessage

use of com.justwayward.reader.bean.support.DownloadMessage in project BookReader by JustWayward.

the class DownloadBookService method downloadBook.

public synchronized void downloadBook(final DownloadQueue downloadQueue) {
    AsyncTask<Integer, Integer, Integer> downloadTask = new AsyncTask<Integer, Integer, Integer>() {

        List<BookMixAToc.mixToc.Chapters> list = downloadQueue.list;

        String bookId = downloadQueue.bookId;

        // 起始章节
        int start = downloadQueue.start;

        // 结束章节
        int end = downloadQueue.end;

        @Override
        protected Integer doInBackground(Integer... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            int failureCount = 0;
            for (int i = start; i <= end && i <= list.size(); i++) {
                if (canceled) {
                    break;
                }
                // 网络异常,取消下载
                if (!NetworkUtils.isAvailable(AppUtils.getAppContext())) {
                    downloadQueue.isCancel = true;
                    post(new DownloadMessage(bookId, getString(R.string.book_read_download_error), true));
                    failureCount = -1;
                    break;
                }
                if (!downloadQueue.isFinish && !downloadQueue.isCancel) {
                    // 章节文件不存在,则下载,否则跳过
                    if (CacheManager.getInstance().getChapterFile(bookId, i) == null) {
                        BookMixAToc.mixToc.Chapters chapters = list.get(i - 1);
                        String url = chapters.link;
                        int ret = download(url, bookId, chapters.title, i, list.size());
                        if (ret != 1) {
                            failureCount++;
                        }
                    } else {
                        post(new DownloadProgress(bookId, String.format(getString(R.string.book_read_alreday_download), list.get(i - 1).title, i, list.size()), true));
                    }
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
            return failureCount;
        }

        @Override
        protected void onPostExecute(Integer failureCount) {
            super.onPostExecute(failureCount);
            downloadQueue.isFinish = true;
            if (failureCount > -1) {
                // 完成通知
                post(new DownloadMessage(bookId, String.format(getString(R.string.book_read_download_complete), failureCount), true));
            }
            // 下载完成,从队列里移除
            downloadQueues.remove(downloadQueue);
            // 释放 空闲状态
            isBusy = false;
            if (!canceled) {
                // post一个空事件,通知继续执行下一个任务
                post(new DownloadQueue());
            } else {
                downloadQueues.clear();
            }
            canceled = false;
            LogUtils.i(bookId + "缓存完成,失败" + failureCount + "章");
        }
    };
    downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : DownloadProgress(com.justwayward.reader.bean.support.DownloadProgress) DownloadMessage(com.justwayward.reader.bean.support.DownloadMessage) AsyncTask(android.os.AsyncTask) DownloadQueue(com.justwayward.reader.bean.support.DownloadQueue) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DownloadMessage (com.justwayward.reader.bean.support.DownloadMessage)2 DownloadQueue (com.justwayward.reader.bean.support.DownloadQueue)2 AsyncTask (android.os.AsyncTask)1 DownloadProgress (com.justwayward.reader.bean.support.DownloadProgress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Subscribe (org.greenrobot.eventbus.Subscribe)1 SyncOnSubscribe (rx.observables.SyncOnSubscribe)1