com.liulishuo.filedownloader.services.FileDownloadMgr.java Source code

Java tutorial

Introduction

Here is the source code for com.liulishuo.filedownloader.services.FileDownloadMgr.java

Source

/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liulishuo.filedownloader.services;

import com.liulishuo.filedownloader.model.FileDownloadModel;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.liulishuo.filedownloader.model.FileDownloadTransferModel;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.liulishuo.filedownloader.util.FileDownloadUtils;
import com.squareup.okhttp.OkHttpClient;

import java.io.File;

/**
 * Created by Jacksgong on 9/24/15.
 */
class FileDownloadMgr {
    private final IFileDownloadDBHelper mHelper;

    // TODO OkHttpClient?OkHttpClient?
    private final OkHttpClient client;

    private final FileDownloadThreadPool mThreadPool = new FileDownloadThreadPool();

    public FileDownloadMgr() {
        mHelper = new FileDownloadDBHelper();

        // init client
        client = new OkHttpClient();
        // TODO 
    }

    public synchronized int start(String url, String path, int callbackProgressTimes, int autoRetryTimes) {
        final int id = FileDownloadUtils.generateId(url, path);

        if (checkResume(id, autoRetryTimes)) {
            FileDownloadLog.d(this, "resume %d", id);
            return id;
        }

        final FileDownloadModel model = new FileDownloadModel();
        model.setUrl(url);
        model.setPath(path);
        model.setCallbackProgressTimes(callbackProgressTimes);

        model.setId(id);
        model.setSoFar(0);
        model.setTotal(0);
        model.setStatus(FileDownloadStatus.pending);
        model.setIsCancel(false);

        mHelper.update(model);

        mThreadPool.execute(new FileDownloadRunnable(client, model, mHelper, autoRetryTimes));

        return id;
    }

    public boolean checkDownloading(String url, String path) {
        final int downloadId = FileDownloadUtils.generateId(url, path);
        final FileDownloadModel model = mHelper.find(downloadId);
        final boolean isInPool = mThreadPool.isInThreadPool(downloadId);

        boolean isDownloading;
        do {
            if (model == null || (model.getStatus() != FileDownloadStatus.pending
                    && model.getStatus() != FileDownloadStatus.progress)) {

                if (isInPool) {
                    // status ?pending/processing & ???status?complete/error/pause
                    // TODO ?????DownloadIdRunnable?
                    isDownloading = true;
                } else {
                    // status ?pending/processing & ?
                    isDownloading = false;

                }
            } else {
                //model != null && status pending/progress
                if (isInPool) {
                    // status pending/processing & 
                    isDownloading = true;
                } else {
                    // status pending/processing & ???elog?
                    FileDownloadLog.e(this, "status is[%s] & thread is not has %d", model.getStatus(), downloadId);
                    isDownloading = false;

                }
            }
        } while (false);

        return isDownloading;

    }

    /**
     * @return Already succeed & exists
     */
    public FileDownloadTransferModel checkReuse(String url, String path) {
        final int downloadId = FileDownloadUtils.generateId(url, path);
        final FileDownloadModel model = mHelper.find(downloadId);
        FileDownloadTransferModel transferModel = null;
        // checkDownloading? ???, ??DownloadIdRunnable?
        do {
            if (model == null) {
                // ??
                FileDownloadLog.w(this, "model not exist %s", url);
                break;
            }

            if (model.getStatus() != FileDownloadStatus.completed) {
                // ???
                FileDownloadLog.w(this, "status not completed %s %s", model.getStatus(), url);
                break;
            }

            final File file = new File(path);
            if (!file.exists() || !file.isFile()) {
                // ?
                FileDownloadLog.w(this, "file not exists %s", url);
                break;
            }

            if (model.getSoFar() != model.getTotal()) {
                // ??
                FileDownloadLog.w(this, "soFar[%d] not equal total[%d] %s", model.getSoFar(), model.getTotal(),
                        url);
                break;
            }

            if (file.length() != model.getTotal()) {
                // 
                FileDownloadLog.w(this, "file length[%d] not equal total[%d] %s", file.length(), model.getTotal(),
                        url);
                break;
            }

            transferModel = new FileDownloadTransferModel(model);
        } while (false);

        FileDownloadLog.d(this, "check reuse %d enable(%B)", downloadId, transferModel != null);
        return transferModel;
    }

    public boolean checkResume(final int id, final int autoRetryTimes) {
        final FileDownloadModel model = mHelper.find(id);
        if (model == null) {
            return false;
        }

        if (model.getStatus() != FileDownloadStatus.paused) {
            return false;
        }

        model.setIsCancel(false);

        FileDownloadLog.d(this, "start resume %d %d %d", id, model.getSoFar(), model.getTotal());
        mThreadPool.execute(new FileDownloadRunnable(client, model, mHelper, autoRetryTimes));

        return true;
    }

    public boolean pause(final int id) {
        final FileDownloadModel model = mHelper.find(id);
        if (model == null) {
            return false;
        }

        FileDownloadLog.d(this, "paused %d", id);
        model.setIsCancel(true);
        /**
         * ? by {@link FileDownloadRunnable#run()}  {@link com.squareup.okhttp.Request.Builder#tag(Object)}
         * ?okHttp??
         */
        // ?errorokHttp?okHttpcancelerror
        //        client.cancel(id);
        return true;
    }

    public long getSoFar(final int id) {
        final FileDownloadModel model = mHelper.find(id);
        if (model == null) {
            return 0;
        }

        return model.getSoFar();
    }

    public long getTotal(final int id) {
        final FileDownloadModel model = mHelper.find(id);
        if (model == null) {
            return 0;
        }

        return model.getTotal();
    }
}