public class DownloadTask extends java.lang.Object implements BaseDownloadTask, BaseDownloadTask.IRunningTask
BaseDownloadTask.FinishListener, BaseDownloadTask.InQueueTask, BaseDownloadTask.IRunningTask, BaseDownloadTask.LifeCycleCallback| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_CALLBACK_PROGRESS_MIN_INTERVAL_MILLIS |
| Modifier and Type | Method and Description |
|---|---|
BaseDownloadTask |
addFinishListener(BaseDownloadTask.FinishListener finishListener)
Add the finish listener to listen when the task is finished.
|
BaseDownloadTask |
addHeader(java.lang.String line)
Add a field with the specified value to the request header.
|
BaseDownloadTask |
addHeader(java.lang.String name,
java.lang.String value)
Add the params to the request header.
|
BaseDownloadTask.InQueueTask |
asInQueueTask()
Declare the task will be assembled by a queue which makes up of the same listener task.
|
boolean |
cancel()
The
BaseDownloadTask.pause() also clear all data relate with this task in the memory, so please use
BaseDownloadTask.pause() instead. |
void |
free()
Free the task.
|
int |
getAttachKey()
When the task is running, it must attach a key.
|
int |
getAutoRetryTimes() |
int |
getCallbackProgressMinInterval() |
int |
getCallbackProgressTimes() |
int |
getDownloadId()
Deprecated.
Used
getId() instead. |
java.lang.Throwable |
getErrorCause() |
java.lang.String |
getEtag() |
java.lang.Throwable |
getEx() |
java.lang.String |
getFilename() |
java.util.ArrayList<BaseDownloadTask.FinishListener> |
getFinishListenerList() |
FileDownloadHeader |
getHeader() |
int |
getId()
The downloading identify of this task, what is generated by
BaseDownloadTask.getUrl() and
BaseDownloadTask.getPath() and BaseDownloadTask.isPathAsDirectory() from
FileDownloadUtils.generateId(String, String). |
long |
getLargeFileSoFarBytes()
This method will be used when the length of target file is more than 1.99G.
|
long |
getLargeFileTotalBytes()
This method will be used when the length of target file is more than 1.99G.
|
FileDownloadListener |
getListener() |
ITaskHunter.IMessageHandler |
getMessageHandler() |
BaseDownloadTask |
getOrigin() |
java.lang.String |
getPath() |
java.lang.Object |
getPauseLock()
Get the object as a lock for synchronized with the pause area.
|
int |
getRetryingTimes() |
BaseDownloadTask.IRunningTask |
getRunningTask() |
int |
getSmallFileSoFarBytes()
This method will be used when the length of target file is less than or equal to 1.99G.
|
int |
getSmallFileTotalBytes()
This method will be used when the length of target file is less than or equal to 1.99G.
|
int |
getSoFarBytes() |
int |
getSpeed()
Get the downloading speed.
|
byte |
getStatus() |
java.lang.Object |
getTag() |
java.lang.Object |
getTag(int key)
Returns the tag associated with this task and the specified key.
|
java.lang.String |
getTargetFilePath() |
int |
getTotalBytes() |
java.lang.String |
getUrl() |
boolean |
is(FileDownloadListener listener) |
boolean |
is(int id) |
boolean |
isAttached()
Whether this task has already attached to a listener / a serial-queue.
|
boolean |
isContainFinishListener()
Whether contain finish listener or not.
|
boolean |
isContinue()
Deprecated.
Use
isResuming() instead. |
boolean |
isForceReDownload() |
boolean |
isLargeFile() |
boolean |
isMarkedAdded2List() |
boolean |
isOver() |
boolean |
isPathAsDirectory() |
boolean |
isResuming() |
boolean |
isReusedOldFile() |
boolean |
isRunning() |
boolean |
isSyncCallback() |
boolean |
isUsing() |
boolean |
isWifiRequired() |
void |
markAdded2List()
Mark the task has already added to the downloading list.
|
boolean |
pause()
Why pause? not stop/cancel? because invoke this method(pause) will clear all data about this
task in memory, and stop the total processing about this task.
|
int |
ready()
Ready this task(For the task in a queue).
|
BaseDownloadTask |
removeAllHeaders(java.lang.String name)
Remove all fields in the request header.
|
boolean |
removeFinishListener(BaseDownloadTask.FinishListener finishListener)
Remove the finish listener from this task.
|
boolean |
reuse()
Reuse this task withhold request params: path、url、header、isForceReDownloader、etc.
|
void |
setAttachKeyByQueue(int key)
Set this task attach to the
key by the queue. |
void |
setAttachKeyDefault()
Set this task to the default key.
|
BaseDownloadTask |
setAutoRetryTimes(int autoRetryTimes)
Set the number of times to retry when encounter any error, except
FileDownloadGiveUpRetryException. |
BaseDownloadTask |
setCallbackProgressIgnored()
Ignore all callbacks of
FileDownloadListener.progress(BaseDownloadTask, int, int)
during the entire process of downloading. |
BaseDownloadTask |
setCallbackProgressMinInterval(int minIntervalMillis)
Set the minimum time interval between each callback of
FileDownloadListener.progress(BaseDownloadTask, int, int). |
BaseDownloadTask |
setCallbackProgressTimes(int callbackProgressCount)
Set the maximum callback count of
FileDownloadListener.progress(BaseDownloadTask, int, int) during the entire process
of downloading. |
void |
setFileName(java.lang.String fileName) |
BaseDownloadTask |
setFinishListener(BaseDownloadTask.FinishListener finishListener) |
BaseDownloadTask |
setForceReDownload(boolean isForceReDownload)
Force re-downloading the file regardless the target file is exist.
|
BaseDownloadTask |
setListener(FileDownloadListener listener) |
BaseDownloadTask |
setMinIntervalUpdateSpeed(int minIntervalUpdateSpeedMs) |
BaseDownloadTask |
setPath(java.lang.String path) |
BaseDownloadTask |
setPath(java.lang.String path,
boolean pathAsDirectory) |
BaseDownloadTask |
setSyncCallback(boolean syncCallback) |
BaseDownloadTask |
setTag(int key,
java.lang.Object tag)
Set a tag associated with this task, not be used by internal.
|
BaseDownloadTask |
setTag(java.lang.Object tag)
Sets the tag associated with this task, not be used by internal.
|
BaseDownloadTask |
setWifiRequired(boolean isWifiRequired)
Set whether this task only allows downloading on the wifi network type.
|
int |
start()
Start this task in parallel.
|
void |
startTaskByQueue()
Start the task by the queue handler.
|
void |
startTaskByRescue()
Start the task just because this task can't started by pass, and now, we try to rescue
this task and start it.
|
java.lang.String |
toString() |
public static final int DEFAULT_CALLBACK_PROGRESS_MIN_INTERVAL_MILLIS
public BaseDownloadTask setMinIntervalUpdateSpeed(int minIntervalUpdateSpeedMs)
setMinIntervalUpdateSpeed in interface BaseDownloadTaskminIntervalUpdateSpeedMs - The minimum interval millisecond for updating the downloading
speed in downloading process(Status equal to progress).
Default: 5 ms.
If the value is less than or equal to 0, will not calculate the download speed in process.
public BaseDownloadTask setPath(java.lang.String path)
setPath in interface BaseDownloadTaskpath - path = (absolute directory/filename); and BaseDownloadTask.isPathAsDirectory()
assign to false.public BaseDownloadTask setPath(java.lang.String path, boolean pathAsDirectory)
setPath in interface BaseDownloadTaskpath - The absolute path for saving the download file.pathAsDirectory - true: if the path is absolute directory to store the
downloading file, and the filename will be found in
contentDisposition from the response as default, if can't find
contentDisposition,the filename will be generated by
FileDownloadUtils.generateFileName(String) with url.
false: if the path = (absolute directory/filename).BaseDownloadTask.isPathAsDirectory(),
BaseDownloadTask.getFilename()public BaseDownloadTask setListener(FileDownloadListener listener)
setListener in interface BaseDownloadTasklistener - For callback download status(pending,connected,progress,
blockComplete,retry,error,paused,completed,warn)public BaseDownloadTask setCallbackProgressTimes(int callbackProgressCount)
BaseDownloadTaskFileDownloadListener.progress(BaseDownloadTask, int, int) during the entire process
of downloading.
Note: this function will not work if the URL is refer to 'chucked' resource.
setCallbackProgressTimes in interface BaseDownloadTaskcallbackProgressCount - The maximum callback count of
FileDownloadListener.progress(com.liulishuo.filedownloader.BaseDownloadTask, int, int)
during the entire process of downloading.
Default value is 100, If the value less than or equal to 0, you
will not receive any callback of
FileDownloadListener.progress(com.liulishuo.filedownloader.BaseDownloadTask, int, int)
.
BaseDownloadTask.setCallbackProgressMinInterval(int)public BaseDownloadTask setCallbackProgressMinInterval(int minIntervalMillis)
BaseDownloadTaskFileDownloadListener.progress(BaseDownloadTask, int, int).setCallbackProgressMinInterval in interface BaseDownloadTaskminIntervalMillis - The minimum interval between each callback of
FileDownloadListener.progress(BaseDownloadTask, int, int).
Unit: millisecond.
Default value is
BaseDownloadTask.DEFAULT_CALLBACK_PROGRESS_MIN_INTERVAL_MILLIS.
Scope: [5, Integer.MAX_VALUE.
BaseDownloadTask.setCallbackProgressTimes(int)public BaseDownloadTask setCallbackProgressIgnored()
BaseDownloadTaskFileDownloadListener.progress(BaseDownloadTask, int, int)
during the entire process of downloading. This will optimize the performance.setCallbackProgressIgnored in interface BaseDownloadTaskpublic BaseDownloadTask setTag(java.lang.Object tag)
BaseDownloadTasksetTag in interface BaseDownloadTaskpublic BaseDownloadTask setTag(int key, java.lang.Object tag)
BaseDownloadTasksetTag in interface BaseDownloadTaskkey - The key of identifying the tag.
If the key already exists, the old data will be replaced.tag - An Object to tag the task withpublic BaseDownloadTask setForceReDownload(boolean isForceReDownload)
BaseDownloadTasksetForceReDownload in interface BaseDownloadTaskisForceReDownload - If set to true, will not check whether the target file is exist.
Default: false.
public BaseDownloadTask setFinishListener(BaseDownloadTask.FinishListener finishListener)
setFinishListener in interface BaseDownloadTaskpublic BaseDownloadTask addFinishListener(BaseDownloadTask.FinishListener finishListener)
BaseDownloadTask
This listener's method BaseDownloadTask.FinishListener.over(BaseDownloadTask) will be invoked in
Internal-Flow-Thread directly, which is controlled by MessageSnapshotThreadPool.
addFinishListener in interface BaseDownloadTaskfinishListener - The finish listener.FileDownloadStatus.isOver(int)public boolean removeFinishListener(BaseDownloadTask.FinishListener finishListener)
BaseDownloadTaskremoveFinishListener in interface BaseDownloadTaskfinishListener - The finish listener.true if remove the finishListener successfully.
false otherwise.public BaseDownloadTask setAutoRetryTimes(int autoRetryTimes)
BaseDownloadTaskFileDownloadGiveUpRetryException.setAutoRetryTimes in interface BaseDownloadTaskautoRetryTimes - The retry times, default 0.public BaseDownloadTask addHeader(java.lang.String name, java.lang.String value)
BaseDownloadTaskNote: We have already handled Etag internal for guaranteeing tasks resuming from the breakpoint, in other words, if the task has downloaded and got Etag, we will add the 'If-Match' and the 'Range' K-V to its request header automatically.
addHeader in interface BaseDownloadTaskpublic BaseDownloadTask addHeader(java.lang.String line)
BaseDownloadTaskaddHeader in interface BaseDownloadTaskpublic BaseDownloadTask removeAllHeaders(java.lang.String name)
BaseDownloadTaskremoveAllHeaders in interface BaseDownloadTaskpublic BaseDownloadTask setSyncCallback(boolean syncCallback)
setSyncCallback in interface BaseDownloadTasksyncCallback - true FileDownloader will invoke methods of
FileDownloadListener directly on the download thread(isn't in the
main thread).public BaseDownloadTask setWifiRequired(boolean isWifiRequired)
BaseDownloadTaskfalse
Note: If isWifiRequired is true, FileDownloader will check
the network type every time after fetch less than or equal to 4096 bytes data from the
network, what will result in slowing the download speed slightly.
Permission: If isWifiRequired is true, You need declare the
permission Manifest.permission.ACCESS_NETWORK_STATE in your manifest, let
FileDownloader has permission to check the network type in downloading, if you start this
task without this permission you will receive a FileDownloadGiveUpRetryException.
setWifiRequired in interface BaseDownloadTaskisWifiRequired - true This task only allow to download on the wifi network type.public int ready()
BaseDownloadTask
Note: If this task doesn't belong to a queue, what is just an isolated task,
you just need to invoke BaseDownloadTask.start() to start this task, that's all. In other words, If
this task doesn't belong to a queue, you must not invoke this method or
BaseDownloadTask.InQueueTask.enqueue() method before invoke BaseDownloadTask.start(), If you do that and if
there is the same listener object to start a queue in another thread, this task may be
assembled by the queue, in that case, when you invoke BaseDownloadTask.start() manually to start this
task or this task is started by the queue, there is an exception buried in there, because
this task object is started two times without declare BaseDownloadTask.reuse() : 1. you invoke
BaseDownloadTask.start() manually; 2. the queue start this task automatically.
ready in interface BaseDownloadTaskFileDownloader.start(FileDownloadListener, boolean)public BaseDownloadTask.InQueueTask asInQueueTask()
BaseDownloadTask
Note: If you use FileDownloadQueueSet to start this task in a queue,
you don't need to invoke this method manually, it has been handled by
FileDownloadQueueSet.
asInQueueTask in interface BaseDownloadTaskBaseDownloadTask.InQueueTask.enqueue() to
enqueue this task to the global queue to ready for being assembled by the queue.public boolean reuse()
BaseDownloadTask
Note:If the task has been over(FileDownloadStatus.isOver(int)), but
the over-message has not been handover to the listener, since the callback is asynchronous,
once your invoke this 'reuse' method, that message would be discard, for free the messenger.
reuse in interface BaseDownloadTasktrue if reuse this task successfully. false otherwise.public boolean isUsing()
isUsing in interface BaseDownloadTasktrue if the this task already has downloading data, it means that this task
is running or has ran.BaseDownloadTask.isRunning(),
BaseDownloadTask.start(),
BaseDownloadTask.reuse()public boolean isRunning()
isRunning in interface BaseDownloadTasktrue if this task is running, in this case, this task isn't allow
to BaseDownloadTask.start() again for this task object, and even not allow to BaseDownloadTask.reuse().
false this task maybe BaseDownloadTask.isUsing() or in idle.BaseDownloadTask.isUsing(),
BaseDownloadTask.start()public boolean isAttached()
BaseDownloadTaskisAttached in interface BaseDownloadTasktrue if this task is running, and it has already attached to the listener or
has already assembled to a serial-queue and would be started automatically when it is come to
its turn.IQueuesHandler.startQueueSerial(FileDownloadListener),
IQueuesHandler.startQueueParallel(FileDownloadListener)public int start()
BaseDownloadTaskstart in interface BaseDownloadTaskpublic boolean pause()
BaseDownloadTaskpause in interface BaseDownloadTasktrue, if pause this task successfully, false otherwise this task has
already in over status before invoke this method(this case maybe occurred in high concurrent
situation).FileDownloader.pause(int),
FileDownloader.pause(FileDownloadListener),
FileDownloader.pauseAll()public boolean cancel()
BaseDownloadTaskBaseDownloadTask.pause() also clear all data relate with this task in the memory, so please use
BaseDownloadTask.pause() instead.cancel in interface BaseDownloadTasktrue if cancel this task successfully.public int getId()
BaseDownloadTaskBaseDownloadTask.getUrl() and
BaseDownloadTask.getPath() and BaseDownloadTask.isPathAsDirectory() from
FileDownloadUtils.generateId(String, String).getId in interface BaseDownloadTaskFileDownloader.pause(int),
FileDownloader.getStatus(String, String),
FileDownloader.getTotal(int),
FileDownloader.getSoFar(int)public int getDownloadId()
getId() instead.getDownloadId in interface BaseDownloadTaskpublic java.lang.String getUrl()
getUrl in interface BaseDownloadTaskpublic int getCallbackProgressTimes()
getCallbackProgressTimes in interface BaseDownloadTaskFileDownloadListener.progress(BaseDownloadTask, int, int) during the entire process
of downloading.public int getCallbackProgressMinInterval()
getCallbackProgressMinInterval in interface BaseDownloadTaskFileDownloadListener.progress(BaseDownloadTask, int, int) .public java.lang.String getPath()
getPath in interface BaseDownloadTaskBaseDownloadTask.isPathAsDirectory() is true: path is a absolute directory
to store the downloading file, and the filename will be found in contentDisposition
from the response as default, if can't find contentDisposition, the filename will be
generated by FileDownloadUtils.generateFileName(String) with url. otherwise
path is the absolute path of the target file.public boolean isPathAsDirectory()
isPathAsDirectory in interface BaseDownloadTaskBaseDownloadTask.getPath() is a directory.BaseDownloadTask.getPath()public java.lang.String getFilename()
getFilename in interface BaseDownloadTaskBaseDownloadTask.isPathAsDirectory() is true, the filename will be found
in contentDisposition from the response as default, if can't find contentDisposition, the
filename will be generated by FileDownloadUtils.generateFileName(String) with
url. It will be found before the callback of
FileDownloadListener.connected(BaseDownloadTask, String, boolean, int, int).
If BaseDownloadTask.isPathAsDirectory() is false, the filename will be found
immediately when you invoke BaseDownloadTask.setPath(String, boolean) .public java.lang.String getTargetFilePath()
getTargetFilePath in interface BaseDownloadTaskpublic FileDownloadListener getListener()
getListener in interface BaseDownloadTaskpublic int getSoFarBytes()
getSoFarBytes in interface BaseDownloadTaskpublic int getSmallFileSoFarBytes()
BaseDownloadTaskgetSmallFileSoFarBytes in interface BaseDownloadTaskpublic long getLargeFileSoFarBytes()
BaseDownloadTaskgetLargeFileSoFarBytes in interface BaseDownloadTaskpublic int getTotalBytes()
getTotalBytes in interface BaseDownloadTask
Note: this value will be valid
after FileDownloadListener.connected(BaseDownloadTask, String, boolean, int, int) or
it has already have in the database.
public int getSmallFileTotalBytes()
BaseDownloadTaskgetSmallFileTotalBytes in interface BaseDownloadTaskpublic long getLargeFileTotalBytes()
BaseDownloadTaskgetLargeFileTotalBytes in interface BaseDownloadTaskpublic int getSpeed()
BaseDownloadTask
If the task is in the downloading process(status equal FileDownloadStatus.progress) :
The value is a real-time speed. it is calculated when the interval from the last calculation
more than BaseDownloadTask.setMinIntervalUpdateSpeed(int) before each
FileDownloadListener.progress(BaseDownloadTask, int, int) call-back method.
FileDownloadStatus.isOver(int)): The value is a average
speed. it is calculated from the entire downloading travel(connected, over).getSpeed in interface BaseDownloadTaskBaseDownloadTask.setMinIntervalUpdateSpeed(int)public byte getStatus()
getStatus in interface BaseDownloadTaskFileDownloadStatuspublic boolean isForceReDownload()
isForceReDownload in interface BaseDownloadTasktrue if this task force re-download regard less the target file has already
exist.public java.lang.Throwable getEx()
getEx in interface BaseDownloadTaskpublic java.lang.Throwable getErrorCause()
getErrorCause in interface BaseDownloadTaskpublic boolean isReusedOldFile()
isReusedOldFile in interface BaseDownloadTasktrue if this task didn't start downloading really, because the target file
has already exist. false otherwise.BaseDownloadTask.isForceReDownload()public java.lang.Object getTag()
getTag in interface BaseDownloadTaskpublic java.lang.Object getTag(int key)
BaseDownloadTaskgetTag in interface BaseDownloadTaskkey - The key identifying the tag.null if not
set.BaseDownloadTask.setTag(int, Object),
BaseDownloadTask.getTag()public boolean isContinue()
isResuming() instead.isContinue in interface BaseDownloadTaskpublic boolean isResuming()
isResuming in interface BaseDownloadTasktrue if this task is resuming from the breakpoint, this value is valid
after FileDownloadListener.connected(BaseDownloadTask, String, boolean, int, int).public java.lang.String getEtag()
getEtag in interface BaseDownloadTaskFileDownloadListener.connected(BaseDownloadTask, String, boolean, int, int)public int getAutoRetryTimes()
getAutoRetryTimes in interface BaseDownloadTaskBaseDownloadTask.setAutoRetryTimes(int)public int getRetryingTimes()
getRetryingTimes in interface BaseDownloadTaskFileDownloadListener.retry(BaseDownloadTask, Throwable, int, int)public boolean isSyncCallback()
isSyncCallback in interface BaseDownloadTasktrue if the methods of FileDownloadListener will be invoked directly
in message-thread for this task, false all methods of FileDownloadListener
will
be post to the UI thread for this task.BaseDownloadTask.setSyncCallback(boolean)public boolean isLargeFile()
isLargeFile in interface BaseDownloadTasktrue if the length of target file is more than or equal to 2G.BaseDownloadTask.getLargeFileSoFarBytes(),
BaseDownloadTask.getLargeFileTotalBytes()public boolean isWifiRequired()
isWifiRequired in interface BaseDownloadTasktrue if this task has been set only allows downloading on the wifi network
type.public FileDownloadHeader getHeader()
public void markAdded2List()
BaseDownloadTask.IRunningTaskmarkAdded2List in interface BaseDownloadTask.IRunningTaskpublic void free()
BaseDownloadTask.IRunningTaskfree in interface BaseDownloadTask.IRunningTaskpublic void startTaskByQueue()
BaseDownloadTask.IRunningTaskstartTaskByQueue in interface BaseDownloadTask.IRunningTaskpublic void startTaskByRescue()
BaseDownloadTask.IRunningTaskCurrently, this rescue is occurred when the filedownloader service connected.
startTaskByRescue in interface BaseDownloadTask.IRunningTaskpublic java.lang.Object getPauseLock()
BaseDownloadTask.IRunningTaskgetPauseLock in interface BaseDownloadTask.IRunningTaskpublic boolean isContainFinishListener()
BaseDownloadTask.IRunningTaskisContainFinishListener in interface BaseDownloadTask.IRunningTasktrue if there is finish listener on the task.public boolean isMarkedAdded2List()
isMarkedAdded2List in interface BaseDownloadTask.IRunningTasktrue the task has already added to the downloading list.public BaseDownloadTask.IRunningTask getRunningTask()
public void setFileName(java.lang.String fileName)
public java.util.ArrayList<BaseDownloadTask.FinishListener> getFinishListenerList()
public BaseDownloadTask getOrigin()
getOrigin in interface BaseDownloadTask.IRunningTaskpublic ITaskHunter.IMessageHandler getMessageHandler()
getMessageHandler in interface BaseDownloadTask.IRunningTaskpublic boolean is(int id)
is in interface BaseDownloadTask.IRunningTasktrue the id of the task is equal to the id.public boolean is(FileDownloadListener listener)
is in interface BaseDownloadTask.IRunningTasktrue the listener of the task is equal to the listener.public boolean isOver()
isOver in interface BaseDownloadTask.IRunningTasktrue if the task has already finished.public int getAttachKey()
BaseDownloadTask.IRunningTaskgetAttachKey in interface BaseDownloadTask.IRunningTaskpublic void setAttachKeyByQueue(int key)
BaseDownloadTask.IRunningTaskkey by the queue. In this case, this task must be a
task which belong to a queue, and will be started automatically by the queue.setAttachKeyByQueue in interface BaseDownloadTask.IRunningTaskkey - The attached key for this task.public void setAttachKeyDefault()
BaseDownloadTask.IRunningTasksetAttachKeyDefault in interface BaseDownloadTask.IRunningTaskpublic java.lang.String toString()
toString in class java.lang.Object