public class FileDownloader extends Object
| Constructor and Description |
|---|
FileDownloader() |
| Modifier and Type | Method and Description |
|---|---|
void |
addServiceConnectListener(FileDownloadConnectListener listener)
Add the listener for listening when the status of connection with the downloader service is
changed.
|
void |
bindService()
Start and bind the FileDownloader service.
|
void |
bindService(Runnable runnable)
Start and bind the FileDownloader service and run
runnable as soon as the binding is
successful. |
boolean |
clear(int id,
String targetFilePath)
Clear the data with the provided
id. |
void |
clearAllTaskData()
Clear all data in the filedownloader database.
|
BaseDownloadTask |
create(String url)
Create a download task.
|
static void |
disableAvoidDropFrame()
Disable avoiding missing screen frames, let all callbacks in
FileDownloadListener
can be invoked at once when it achieve. |
static void |
enableAvoidDropFrame()
Avoid missing screen frames, this leads to all callbacks in
FileDownloadListener do
not be invoked at once when it has already achieved to ensure callbacks don't be too frequent |
static FileDownloader |
getImpl() |
long |
getSoFar(int downloadId)
Get downloaded bytes so far by the downloadId.
|
byte |
getStatus(int id,
String path) |
byte |
getStatus(String url,
String path) |
byte |
getStatusIgnoreCompleted(int id) |
long |
getTotal(int id)
Get the total bytes of the target file for the task with the {code id}.
|
static void |
init(Context context)
Deprecated.
please use
setup(Context) instead. |
static void |
init(Context context,
DownloadMgrInitialParams.InitCustomMaker maker)
Deprecated.
please using
setupOnApplicationOnCreate(Application) instead. |
FileDownloadLine |
insureServiceBind()
If the FileDownloader service is not started and connected, FileDownloader will try to start
it and try to bind with it.
|
FileDownloadLineAsync |
insureServiceBindAsync()
If the FileDownloader service is not started and connected will return
false
immediately, and meanwhile FileDownloader will try to start FileDownloader service and try to
bind with it, and after it is bound successfully the request you invoke in
FileDownloadLineAsync will be executed automatically. |
static boolean |
isEnabledAvoidDropFrame() |
boolean |
isServiceConnected() |
void |
pause(FileDownloadListener listener)
Pause the download queue by the same
listener. |
int |
pause(int id)
Pause downloading tasks with the
id. |
void |
pauseAll()
Pause all tasks running in FileDownloader.
|
void |
removeServiceConnectListener(FileDownloadConnectListener listener)
Remove the listener for listening when the status of connection with the downloader service
is changed.
|
int |
replaceListener(int id,
FileDownloadListener listener)
Find the running task by
id, and replace its listener width the new one
listener. |
int |
replaceListener(String url,
FileDownloadListener listener)
Find the running task by
url and default path, and replace its listener with
the new one listener. |
int |
replaceListener(String url,
String path,
FileDownloadListener listener)
Find the running task by
url and path, and replace its listener with
the new one listener. |
static void |
setGlobalHandleSubPackageSize(int packageSize)
For avoiding missing screen frames.
|
static void |
setGlobalPost2UIInterval(int intervalMillisecond)
For avoiding missing screen frames.
|
boolean |
setMaxNetworkThreadCount(int count)
Set the maximum count of the network thread, what is the number of simultaneous downloads in
FileDownloader.
|
boolean |
setTaskCompleted(List<FileDownloadTaskAtom> taskAtomList)
Deprecated.
If you invoked this method, please remove the code directly feel free, it doesn't
need any longer. In new mechanism(filedownloader 0.3.3 or higher), FileDownloader doesn't
store completed tasks in Database anymore, because all downloading files have temp a file
name.
|
boolean |
setTaskCompleted(String url,
String path,
long totalBytes)
Deprecated.
If you invoked this method, please remove the code directly feel free, it doesn't
need any longer. In new mechanism(filedownloader 0.3.3 or higher), FileDownloader doesn't
store completed tasks in Database anymore, because all downloading files have temp a file
name.
|
static void |
setup(Context context)
You can invoke this method anytime before you using the FileDownloader.
|
static DownloadMgrInitialParams.InitCustomMaker |
setupOnApplicationOnCreate(Application application)
Using this method to setup the FileDownloader only you want to register your own customize
components for Filedownloader, otherwise just using
setup(Context) instead. |
boolean |
start(FileDownloadListener listener,
boolean isSerial)
Start the download queue by the same listener.
|
void |
startForeground(int id,
Notification notification)
Start the
notification with the id. |
void |
stopForeground(boolean removeNotification)
Remove the downloader service from the foreground state, allowing it to be killed if
more memory is needed.
|
void |
unBindService()
Unbind and stop the downloader service.
|
boolean |
unBindServiceIfIdle()
Unbind and stop the downloader service when there is no task running in the FileDownloader.
|
public static void setup(Context context)
If you want to register your own customize components please using
setupOnApplicationOnCreate(Application) on the Application.onCreate()
instead.
context - the context of Application or Activity etc..public static DownloadMgrInitialParams.InitCustomMaker setupOnApplicationOnCreate(Application application)
setup(Context) instead.
Please invoke this method on the Application.onCreate() because of the customize
components must be assigned before FileDownloader is running.
Such as:
class MyApplication extends Application {
...
public void onCreate() {
...
FileDownloader.setupOnApplicationOnCreate(this)
.idGenerator(new MyIdGenerator())
.database(new MyDatabase())
...
.commit();
...
}
...
}application - the application.public static void init(Context context)
setup(Context) instead.public static void init(Context context, DownloadMgrInitialParams.InitCustomMaker maker)
setupOnApplicationOnCreate(Application) instead.public static FileDownloader getImpl()
public static void setGlobalPost2UIInterval(int intervalMillisecond)
FileDownloadListener is invoked too
frequent in result the system missing screen frames in the main thread.
We wrap the message package which size is FileDownloadMessageStation.SUB_PACKAGE_SIZE
and post the package to the main thread with the interval:
FileDownloadMessageStation.INTERVAL milliseconds.
intervalMillisecond equal to or less than 0, each
callback in FileDownloadListener will be posted to the main thread immediately.intervalMillisecond - The time interval between posting two message packages.enableAvoidDropFrame(),
disableAvoidDropFrame(),
setGlobalHandleSubPackageSize(int)public static void setGlobalHandleSubPackageSize(int packageSize)
FileDownloadListener is invoked too
frequent in result the system missing screen frames in the main thread.
We wrap the message package which size is FileDownloadMessageStation.SUB_PACKAGE_SIZE
and post the package to the main thread with the interval:
FileDownloadMessageStation.INTERVAL milliseconds.
The default count of message for a message package is 5.
packageSize - The count of message for a message package.setGlobalPost2UIInterval(int)public static void enableAvoidDropFrame()
FileDownloadListener do
not be invoked at once when it has already achieved to ensure callbacks don't be too frequentpublic static void disableAvoidDropFrame()
FileDownloadListener
can be invoked at once when it achieve.public static boolean isEnabledAvoidDropFrame()
true if enabled the function of avoiding missing screen frames.enableAvoidDropFrame(),
disableAvoidDropFrame(),
setGlobalPost2UIInterval(int)public BaseDownloadTask create(String url)
public boolean start(FileDownloadListener listener, boolean isSerial)
listener - Used to assemble tasks which is bound by the same listenerisSerial - Whether start tasks one by one rather than parallel.true if start tasks successfully.public void pause(FileDownloadListener listener)
listener.listener - the listener.pause(int)public void pauseAll()
public int pause(int id)
id.id - the id .pause(FileDownloadListener)public boolean clear(int id,
String targetFilePath)
id.
Normally used to deleting the data in filedownloader database, when it is paused or in
downloading status. If you want to re-download it clearly.
Note: YOU NO NEED to clear the data when it is already completed downloading
because the data would be deleted when it completed downloading automatically by
FileDownloader.
If there are tasks with the id in downloading, will be paused first;
If delete the data with the id in the filedownloader database successfully, will try
to delete its intermediate downloading file and downloaded file.
id - the download id.targetFilePath - the target path.true if the data with the id in filedownloader database was deleted,
and tasks with the id was paused; false otherwise.public void clearAllTaskData()
Note: Normally, YOU NO NEED to clearAllTaskData manually, because the FileDownloader will maintain those data to ensure only if the data available for resuming can be kept automatically.
clear(int, String)public long getSoFar(int downloadId)
public long getTotal(int id)
public byte getStatusIgnoreCompleted(int id)
id - The downloadId.FileDownloadStatus.INVALID_STATUS ).getStatus(String, String),
getStatus(int, String)public byte getStatus(String url, String path)
url - The downloading URL.path - The downloading file's path.getStatus(int, String),
getStatusIgnoreCompleted(int)public byte getStatus(int id,
String path)
id - The downloadId.path - The target file path.FileDownloadStatus,
getStatus(String, String),
getStatusIgnoreCompleted(int)public int replaceListener(String url, FileDownloadListener listener)
url and default path, and replace its listener with
the new one listener.replaceListener(int, FileDownloadListener),
replaceListener(String, String, FileDownloadListener)public int replaceListener(String url, String path, FileDownloadListener listener)
url and path, and replace its listener with
the new one listener.replaceListener(String, FileDownloadListener),
replaceListener(int, FileDownloadListener)public int replaceListener(int id,
FileDownloadListener listener)
id, and replace its listener width the new one
listener.replaceListener(String, FileDownloadListener),
replaceListener(String, String, FileDownloadListener)public void bindService()
Tips: The FileDownloader service will start and bind automatically when any task is request to start.
public void bindService(Runnable runnable)
runnable as soon as the binding is
successful.
Tips: The FileDownloader service will start and bind automatically when any task is request to start.
runnable - the command will be executed as soon as the FileDownloader Service is
successfully bound.isServiceConnected(),
bindService(),
addServiceConnectListener(FileDownloadConnectListener)public void unBindService()
public boolean unBindServiceIfIdle()
true if unbind and stop the downloader service successfully, false
there are some tasks running in the FileDownloader.public boolean isServiceConnected()
true if the downloader service has been started and connected.public void addServiceConnectListener(FileDownloadConnectListener listener)
listener - The downloader service connection listener.removeServiceConnectListener(FileDownloadConnectListener)public void removeServiceConnectListener(FileDownloadConnectListener listener)
listener - The downloader service connection listener.addServiceConnectListener(FileDownloadConnectListener)public void startForeground(int id,
Notification notification)
notification with the id. This will let the downloader service
change to a foreground service.
In foreground status, will save the FileDownloader alive, even user kill the application from recent apps.
Make FileDownloader service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default FileDownloader services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background downloading, so the user would notice if their app stopped downloading.id - The identifier for this notification as per
NotificationManager.notify(int, Notification); must not be 0.notification - The notification to be displayed.stopForeground(boolean)public void stopForeground(boolean removeNotification)
removeNotification - true if the notification previously provided
to startForeground(int, android.app.Notification) will be removed. false it will
be remained until a later call removes it (or the service is
destroyed).startForeground(int, Notification)public boolean setTaskCompleted(String url, String path, long totalBytes)
url - The url of the completed task.path - The absolute path of the completed task's save file.totalBytes - The content-length of the completed task, the length of the file in the
path must be equal to this value.path not exist will
be false; If the length of the file in path is not equal to totalBytes will
be false; If the task with url and path is downloading will be false.
Otherwise will be true.
Recommend used to telling the FileDownloader Engine that the task with the {@code url} and
the {@code path} has already completed downloading, in case of your task has already
downloaded by other ways(not by FileDownloader Engine), and after success to set the task
completed, FileDownloader will check the task with {@code url} and the {@code path} whether
completed by {@code totalBytes}.
Otherwise, If FileDownloader Engine isn't know your task's status, whatever your task with
the {@code url} and the {@code path} has already downloaded in other way, FileDownloader
Engine will ignore the exist file and redownload it, because FileDownloader Engine don't know
the exist file whether it is valid.,
setTaskCompleted(List)public boolean setTaskCompleted(List<FileDownloadTaskAtom> taskAtomList)
taskAtomList - The bulk of tasks.setTaskCompleted(String, String, long)public boolean setMaxNetworkThreadCount(int count)
count - the number of simultaneous downloads, scope: [1, 12].public FileDownloadLine insureServiceBind()
FileDownloadLine will be executed.
If the FileDownloader service has been started and connected, the request you invoke in
FileDownloadLine will be executed immediately.
Note: FileDownloader can not block the main thread, because the system is
also call-backs the ServiceConnection.onServiceConnected(ComponentName, IBinder)
method in the main thread.
Tips: The FileDownloader service will start and bind automatically when any task is request to start.
FileDownloadLine,
bindService(Runnable)public FileDownloadLineAsync insureServiceBindAsync()
false
immediately, and meanwhile FileDownloader will try to start FileDownloader service and try to
bind with it, and after it is bound successfully the request you invoke in
FileDownloadLineAsync will be executed automatically.
If the FileDownloader service has been started and connected, the request you invoke in
FileDownloadLineAsync will be executed immediately.
FileDownloadLineAsync,
bindService(Runnable)