Search in sources :

Example 1 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class DownloadLaunchRunnableTest method run_noWifiButRequired_callbackNetworkError.

@Test
public void run_noWifiButRequired_callbackNetworkError() {
    // init context
    final Application spyApplication = spy(application);
    when(spyApplication.getApplicationContext()).thenReturn(spyApplication);
    FileDownloader.setupOnApplicationOnCreate(spyApplication).database(getMockNonOptDatabaseMaker()).commit();
    // no wifi state
    mockContextNoWifiState(spyApplication);
    // pending model
    final FileDownloadModel model = mock(FileDownloadModel.class);
    when(model.getId()).thenReturn(1);
    when(model.getStatus()).thenReturn(FileDownloadStatus.pending);
    // mock launch runnable.
    final DownloadStatusCallback callback = mock(DownloadStatusCallback.class);
    final DownloadLaunchRunnable launchRunnable = DownloadLaunchRunnable.createForTest(callback, model, mock(FileDownloadHeader.class), mock(IThreadPoolMonitor.class), 1000, 100, false, true, 0);
    launchRunnable.run();
    verify(callback).onErrorDirectly(any(FileDownloadNetworkPolicyException.class));
}
Also used : IThreadPoolMonitor(com.liulishuo.filedownloader.IThreadPoolMonitor) FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) Application(android.app.Application) FileDownloadHeader(com.liulishuo.filedownloader.model.FileDownloadHeader) FileDownloadNetworkPolicyException(com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException) Test(org.junit.Test)

Example 2 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class DownloadLaunchRunnableTest method getMockNonOptDatabaseMaker.

private static FileDownloadHelper.DatabaseCustomMaker getMockNonOptDatabaseMaker() {
    final FileDownloadDatabase database = mock(FileDownloadDatabase.class);
    when(database.maintainer()).thenReturn(new FileDownloadDatabase.Maintainer() {

        @Override
        public void onFinishMaintain() {
        }

        @Override
        public void onRemovedInvalidData(FileDownloadModel model) {
        }

        @Override
        public void onRefreshedValidData(FileDownloadModel model) {
        }

        @Override
        public void changeFileDownloadModelId(int oldId, FileDownloadModel modelWithNewId) {
        }

        @Override
        public Iterator<FileDownloadModel> iterator() {
            return new Iterator<FileDownloadModel>() {

                @Override
                public boolean hasNext() {
                    return false;
                }

                @Override
                public FileDownloadModel next() {
                    return null;
                }
            };
        }
    });
    return new FileDownloadHelper.DatabaseCustomMaker() {

        @Override
        public FileDownloadDatabase customMake() {
            return database;
        }
    };
}
Also used : FileDownloadDatabase(com.liulishuo.filedownloader.database.FileDownloadDatabase) FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) Iterator(java.util.Iterator)

Example 3 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class FileDownloadManager method start.

// synchronize for safe: check downloading, check resume, update data, execute runnable
public synchronized void start(final String url, final String path, final boolean pathAsDirectory, final int callbackProgressTimes, final int callbackProgressMinIntervalMillis, final int autoRetryTimes, final boolean forceReDownload, final FileDownloadHeader header, final boolean isWifiRequired) {
    if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(this, "request start the task with url(%s) path(%s) isDirectory(%B)", url, path, pathAsDirectory);
    }
    final int id = FileDownloadUtils.generateId(url, path, pathAsDirectory);
    FileDownloadModel model = mDatabase.find(id);
    List<ConnectionModel> dirConnectionModelList = null;
    if (!pathAsDirectory && model == null) {
        // try dir data.
        final int dirCaseId = FileDownloadUtils.generateId(url, FileDownloadUtils.getParent(path), true);
        model = mDatabase.find(dirCaseId);
        if (model != null && path.equals(model.getTargetFilePath())) {
            if (FileDownloadLog.NEED_LOG) {
                FileDownloadLog.d(this, "task[%d] find model by dirCaseId[%d]", id, dirCaseId);
            }
            dirConnectionModelList = mDatabase.findConnectionModel(dirCaseId);
        }
    }
    if (FileDownloadHelper.inspectAndInflowDownloading(id, model, this, true)) {
        if (FileDownloadLog.NEED_LOG) {
            FileDownloadLog.d(this, "has already started download %d", id);
        }
        return;
    }
    final String targetFilePath = model != null ? model.getTargetFilePath() : FileDownloadUtils.getTargetFilePath(path, pathAsDirectory, null);
    if (FileDownloadHelper.inspectAndInflowDownloaded(id, targetFilePath, forceReDownload, true)) {
        if (FileDownloadLog.NEED_LOG) {
            FileDownloadLog.d(this, "has already completed downloading %d", id);
        }
        return;
    }
    final long sofar = model != null ? model.getSoFar() : 0;
    final String tempFilePath = model != null ? model.getTempFilePath() : FileDownloadUtils.getTempPath(targetFilePath);
    if (FileDownloadHelper.inspectAndInflowConflictPath(id, sofar, tempFilePath, targetFilePath, this)) {
        if (FileDownloadLog.NEED_LOG) {
            FileDownloadLog.d(this, "there is an another task with the same target-file-path %d %s", id, targetFilePath);
        }
        // because of the file is dirty for this task.
        if (model != null) {
            mDatabase.remove(id);
            mDatabase.removeConnections(id);
        }
        return;
    }
    // real start
    // - create model
    boolean needUpdate2DB;
    if (model != null && (model.getStatus() == FileDownloadStatus.paused || model.getStatus() == FileDownloadStatus.error || model.getStatus() == FileDownloadStatus.pending || model.getStatus() == FileDownloadStatus.started || // FileDownloadRunnable
    model.getStatus() == FileDownloadStatus.connected)) // invoke #isBreakpointAvailable to determine whether it is really invalid.
    {
        if (model.getId() != id) {
            // in try dir case.
            mDatabase.remove(model.getId());
            mDatabase.removeConnections(model.getId());
            model.setId(id);
            model.setPath(path, pathAsDirectory);
            if (dirConnectionModelList != null) {
                for (ConnectionModel connectionModel : dirConnectionModelList) {
                    connectionModel.setId(id);
                    mDatabase.insertConnectionModel(connectionModel);
                }
            }
            needUpdate2DB = true;
        } else {
            if (!TextUtils.equals(url, model.getUrl())) {
                // for cover the case of reusing the downloaded processing with the different
                // url( using with idGenerator ).
                model.setUrl(url);
                needUpdate2DB = true;
            } else {
                needUpdate2DB = false;
            }
        }
    } else {
        if (model == null) {
            model = new FileDownloadModel();
        }
        model.setUrl(url);
        model.setPath(path, pathAsDirectory);
        model.setId(id);
        model.setSoFar(0);
        model.setTotal(0);
        model.setStatus(FileDownloadStatus.pending);
        model.setConnectionCount(1);
        needUpdate2DB = true;
    }
    // - update model to db
    if (needUpdate2DB) {
        mDatabase.update(model);
    }
    final DownloadLaunchRunnable.Builder builder = new DownloadLaunchRunnable.Builder();
    final DownloadLaunchRunnable runnable = builder.setModel(model).setHeader(header).setThreadPoolMonitor(this).setMinIntervalMillis(callbackProgressMinIntervalMillis).setCallbackProgressMaxCount(callbackProgressTimes).setForceReDownload(forceReDownload).setWifiRequired(isWifiRequired).setMaxRetryTimes(autoRetryTimes).build();
    // - execute
    mThreadPool.execute(runnable);
}
Also used : FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) ConnectionModel(com.liulishuo.filedownloader.model.ConnectionModel) DownloadLaunchRunnable(com.liulishuo.filedownloader.download.DownloadLaunchRunnable)

Example 4 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class FileDownloadManager method pause.

public boolean pause(final int id) {
    if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(this, "request pause the task %d", id);
    }
    final FileDownloadModel model = mDatabase.find(id);
    if (model == null) {
        return false;
    }
    model.setStatus(FileDownloadStatus.paused);
    mThreadPool.cancel(id);
    return true;
}
Also used : FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel)

Example 5 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class FileDownloadManager method getSoFar.

public long getSoFar(final int id) {
    final FileDownloadModel model = mDatabase.find(id);
    if (model == null) {
        return 0;
    }
    final int connectionCount = model.getConnectionCount();
    if (connectionCount <= 1) {
        return model.getSoFar();
    } else {
        final List<ConnectionModel> modelList = mDatabase.findConnectionModel(id);
        if (modelList == null || modelList.size() != connectionCount) {
            return 0;
        } else {
            return ConnectionModel.getTotalOffset(modelList);
        }
    }
}
Also used : FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) ConnectionModel(com.liulishuo.filedownloader.model.ConnectionModel)

Aggregations

FileDownloadModel (com.liulishuo.filedownloader.model.FileDownloadModel)13 ConnectionModel (com.liulishuo.filedownloader.model.ConnectionModel)3 File (java.io.File)2 Application (android.app.Application)1 Cursor (android.database.Cursor)1 IThreadPoolMonitor (com.liulishuo.filedownloader.IThreadPoolMonitor)1 FileDownloadConnection (com.liulishuo.filedownloader.connection.FileDownloadConnection)1 FileDownloadDatabase (com.liulishuo.filedownloader.database.FileDownloadDatabase)1 DownloadLaunchRunnable (com.liulishuo.filedownloader.download.DownloadLaunchRunnable)1 FileDownloadGiveUpRetryException (com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException)1 FileDownloadHttpException (com.liulishuo.filedownloader.exception.FileDownloadHttpException)1 FileDownloadNetworkPolicyException (com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException)1 FileDownloadHeader (com.liulishuo.filedownloader.model.FileDownloadHeader)1 FileDownloadHelper (com.liulishuo.filedownloader.util.FileDownloadHelper)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Test (org.junit.Test)1