Android-Download-Manager
Android/Java download manager library help you to download files in parallel mechanism in some chunks.
Under MIT License
By majidgolshadi
Android/Java download manager library help you to download files in parallel mechanism in some chunks.
Under MIT License
By majidgolshadi
Android/Java download manager library help you to download files in parallel mechanism in some chunks.
This library is a download manager android/java library which developers can use in their apps and allow you to download files in parallel mechanism in some chunks and notify developers about tasks status (any download file process is a task). Each download task cross 6 stats in its lifetime.
In the first stage, you need to include these permissions in your AndroidManifest.xml
filexml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
After that, import com.golshadi.downloadManager package in your packages folder. So now everything is ready to start.
One of the important benefits of this lib is that you don't need to initialize object completely before getting any reports.java
DownloadManagerPro dm = new DownloadManagerPro(Context);
to get report about tasks you can use these methods that will be introduced later on this doc:java
public ReportStructure singleDownloadStatus(int token);
public List<ReportStructure> downloadTasksInSameState(int state);
public List<ReportStructure> lastCompletedDownloads();
public boolean unNotifiedChecked();
public boolean delete(int token, boolean deleteTaskFile);
Attention: in this documentation dm stands for DownloadManagerPro object
in order to download with this lib you need to set its basic configurations and give him a listener to poke you about tasks status.java
void DownloadManagerPro.init(String saveFilePath, int maxChunk, DownloadManagerListener class)
Example:java
public class MyActivity extends Activity implements DownloadManagerListener {
...
public void methodName() {
...
// you can only pass this for context but here i want to show clearly
DownloadManagerPro dm = new DownloadManagerPro(this.getApplicationContext());
dm.init("downloadManager/", 12, this);
...
}
...
}
there are three ways to define your download task, so you can define it any way you want. for example If you didn't set maximum chunks number or sd card folder address it uses your initialized values. these methods return you a task id that you can call to start or pause that task using this token.
```java
int DownloadManagerPro.addTask(String saveName, String url, int chunk, String sdCardFolderAddress, boolean overwrite, boolean priority)
int DownloadManagerPro.addTask(String saveName, String url, String sdCardFolderAddress, boolean overwrite, boolean priority)
int DownloadManagerPro.addTask(String saveName, String url, boolean overwrite, boolean priority)
```
* String saveName: defining te name of desired download file.
* String url : Location of desired downlaod file.
* int chunk : Number of chunks which download file has been divided into.
* String sdCardFolder : Location of where user want to save the file.
* boolean overwrite : Overwrite if exists another file with the same name. If true, overwrite and replace the file. If false, find new name and save it with new name.
* boolean priority : Grant priority to more desired files to be downloaded.
Example:java
int taskToken = dm.addTask("save_name", "http://www.site.com/video/ss.mp4", false, false);
this method usage is to start a download task. If download task doesn't get started since this task is in downloading state, it throw you an IOException. When download task start to download this lib notify you with OnDownloadStarted interfacejava
void DownloadManagerPro.startDownload(int token) throws IOException
* int token: It is an assigned token to each new download which is considered as download task id.
Example:
```java
try {
dm.startDownload(taskToekn);
} catch (IOException e) {
e.printStackTrace();
}
```
pause a download tasks that you mention and when that task paused this lib notify you with OnDownloadPaused interfacejava
void DownloadManagerPro.pauseDownload(int token)
* int token: It is an assigned token to each new download which is considered as download task id.
Example:java
dm.pauseDownload(taskToekn);
StartQueueDownload method create a queue sort on what you want and start download queue tasks with downloadTaskPerTime number simultaneously. If download tasks are running in queue and you try to start it again it throws a QueueDownloadInProgressException exception.java
void DownloadManagerPro.StartQueueDownload(int downloadTaskPerTime, int sortType) throws QueueDownloadInProgressException
* int downloadTaskPerTime: the number of task that can be downloaded simultaneously
* int sortType: Grant priority to more desired files to be downloaded.
Example:
``` java
try {
dm.startQueueDownload(3, QueueSort.oldestFirst);
} catch (QueueDownloadInProgressException e) {
e.printStackTrace();
}
```
this method pauses queue download and if no queue download was started it throws a QueueDownloadNotStartedException exception.java
void DownloadManagerPro.pauseQueueDownload()throws QueueDownloadNotStartedException
Example:
```java
try {
dm.pauseQueueDownload();
} catch (QueueDownloadNotStartedException e){
e.printStackTrace();
}
```
In this section we are working with reports since we need to get tasks status and some useful information about those status.
It reports task download information in "ReportStructure" style using a token (download task id) and finally returns the statue of that token.java
ReportStruct DownloadManagerPro.SingleDownloadStatus(int token)
int token: task token
return ReportStructure object and it has a method to convert these info to json
int id: task token
Example:java
ReportStructure report = dm.singleDownloadStatus(taskToken);
It's a report method for returning the list of download task in same state that developers want.java
List DownloadManagerPro.downloadTasksInSameState(int state)
Example:java
List<ReportStructure> report = dm.downloadTasksInSameState(TaskState.INIT);
This method returns list of last completed Download tasks in "ReportStructure" style, developers can use it for notifying whether the task is completed or not.java
List DownloadManagerPro.lastCompletedTasks()
Example:java
List<ReportStructure> completedDownloadTasks = dm.lastCompletedTasks();
This method checks all un notified tasks, so in another "lastCompletedDownloads" call ,completed task does not show up again. “lastCompletedDownloads”: Shows the list of latest completed downloads. Calling this method, all of the tasks that were shown in the previous report, will be eliminated from "lastCompletedDownloads"java
void DownloadManagerPro.unNotifiedCheck()
Example:java
dm.unNotifiedCheck()
this method delete download taskjava
boolean DownloadManagerPro.delete(int token, boolean deleteTaskFile)
* int token: download task token
* boolean deleteTaskFile: deletes download task from database and set deleteTaskFile as true, then it goes to saved folder and delete that file.
return boolean* : if delete is successfully it returns true otherwise false
Example:java
dm.delete(12, false);
This method closes database connection.
java
void DownloadManagerPro.disConnectDB()
Example:java
dm.disConnectDb();