-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
894 additions
and
1,244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*~ | ||
classes | ||
*# | ||
bin | ||
gen | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/com/seafile/seadroid2/transfer/DownloadStateListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.seafile.seadroid2.transfer; | ||
|
||
/** | ||
* Download state listener | ||
* | ||
* Created by Logan on 15/2/7. | ||
*/ | ||
public interface DownloadStateListener { | ||
void onFileDownloadProgress(int taskID); | ||
void onFileDownloaded(int taskID); | ||
void onFileDownloadFailed(int taskID); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.seafile.seadroid2.transfer; | ||
|
||
import com.seafile.seadroid2.SeafException; | ||
import com.seafile.seadroid2.account.Account; | ||
import com.seafile.seadroid2.data.DataManager; | ||
import com.seafile.seadroid2.data.ProgressMonitor; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Download task | ||
* | ||
* Created by Logan on 15/2/3. | ||
*/ | ||
public class DownloadTask extends TransferTask { | ||
|
||
private String localPath; | ||
private DownloadStateListener downloadStateListener; | ||
|
||
public DownloadTask(int taskID, Account account, String repoName, String repoID, String path, | ||
DownloadStateListener downloadStateListener) { | ||
super(taskID, account, repoName, repoID, path); | ||
this.downloadStateListener = downloadStateListener; | ||
} | ||
|
||
/** | ||
* When downloading a file, we don't know the file size in advance, so | ||
* we make use of the first progress update to return the file size. | ||
*/ | ||
@Override | ||
protected void onProgressUpdate(Long... values) { | ||
if (totalSize == -1) { | ||
totalSize = values[0]; | ||
state = TaskState.TRANSFERRING; | ||
return; | ||
} | ||
finished = values[0]; | ||
downloadStateListener.onFileDownloadProgress(taskID); | ||
} | ||
|
||
@Override | ||
protected File doInBackground(Void... params) { | ||
try { | ||
DataManager dataManager = new DataManager(account); | ||
return dataManager.getFile(repoName, repoID, path, | ||
new ProgressMonitor() { | ||
|
||
@Override | ||
public void onProgressNotify(long total) { | ||
publishProgress(total); | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() { | ||
return DownloadTask.this.isCancelled(); | ||
} | ||
} | ||
); | ||
} catch (SeafException e) { | ||
err = e; | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(File file) { | ||
if (downloadStateListener != null) { | ||
if (file != null) { | ||
state = TaskState.FINISHED; | ||
localPath = file.getPath(); | ||
downloadStateListener.onFileDownloaded(taskID); | ||
} else { | ||
state = TaskState.FAILED; | ||
if (err == null) | ||
err = SeafException.unknownException; | ||
downloadStateListener.onFileDownloadFailed(taskID); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void onCancelled() { | ||
state = TaskState.CANCELLED; | ||
} | ||
|
||
@Override | ||
public DownloadTaskInfo getTaskInfo() { | ||
DownloadTaskInfo info = new DownloadTaskInfo(account, taskID, state, repoID, | ||
repoName, path, localPath, totalSize, finished, err); | ||
return info; | ||
} | ||
|
||
public String getLocalPath() { | ||
return localPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/com/seafile/seadroid2/transfer/DownloadTaskManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.seafile.seadroid2.transfer; | ||
|
||
import android.content.Intent; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
import com.google.common.collect.Lists; | ||
import com.seafile.seadroid2.ConcurrentAsyncTask; | ||
import com.seafile.seadroid2.SeadroidApplication; | ||
import com.seafile.seadroid2.account.Account; | ||
import com.seafile.seadroid2.util.Utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Download task manager | ||
* <p/> | ||
* Created by Logan on 15/2/4. | ||
*/ | ||
public class DownloadTaskManager extends TransferManager implements DownloadStateListener { | ||
private static final String DEBUG_TAG = "DownloadTaskManager"; | ||
|
||
public static final String BROADCAST_FILE_DOWNLOAD_SUCCESS = "downloaded"; | ||
public static final String BROADCAST_FILE_DOWNLOAD_FAILED = "downloadFailed"; | ||
public static final String BROADCAST_FILE_DOWNLOAD_PROGRESS = "downloadProgress"; | ||
|
||
/** | ||
* Add a new download task. | ||
* call this method to execute a task immediately. | ||
*/ | ||
public int addTask(Account account, String repoName, String repoID, String path) { | ||
TransferTask task = new DownloadTask(++notificationID, account, | ||
repoName, repoID, path, this); | ||
if (allTaskList.contains(task)) { | ||
if (task.getState().equals(TaskState.CANCELLED) | ||
|| task.getState().equals(TaskState.FAILED) | ||
|| task.getState().equals(TaskState.FINISHED)) { | ||
allTaskList.remove(task); | ||
} else { | ||
// return taskID of old task | ||
return allTaskList.get(allTaskList.indexOf(task)).getTaskID(); | ||
} | ||
} | ||
allTaskList.add(task); | ||
ConcurrentAsyncTask.execute(task); | ||
return task.getTaskID(); | ||
} | ||
|
||
public void addTaskToQue(Account account, String repoName, String repoID, String path) { | ||
// create a new one to avoid IllegalStateException | ||
DownloadTask downloadTask = new DownloadTask(++notificationID, account, repoName, repoID, path, this); | ||
addTaskToQue(downloadTask); | ||
} | ||
|
||
public int getDownloadingFileCountByPath(String repoID, String dir) { | ||
List<DownloadTaskInfo> downloadTaskInfos = getTaskInfoListByPath(repoID, dir); | ||
int count = 0; | ||
for (DownloadTaskInfo downloadTaskInfo : downloadTaskInfos) { | ||
if (downloadTaskInfo.state.equals(TaskState.INIT) | ||
|| downloadTaskInfo.state.equals(TaskState.TRANSFERRING)) | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
/** | ||
* get all download task info under a specific directory. | ||
* | ||
* @param repoID | ||
* @param dir valid dir should be something like this "/DIRNAME/", instead of "/DIRNAME", | ||
* in order to ensure the param being consistent with its caller | ||
* @return List<DownloadTaskInfo> | ||
*/ | ||
public List<DownloadTaskInfo> getTaskInfoListByPath(String repoID, String dir) { | ||
ArrayList<DownloadTaskInfo> infos = Lists.newArrayList(); | ||
for (TransferTask task : allTaskList) { | ||
if (!task.getRepoID().equals(repoID)) | ||
continue; | ||
|
||
String parentDir = Utils.getParentPath(task.getPath()); | ||
String validDir; | ||
|
||
if (!parentDir.equals("/")) | ||
validDir = parentDir + "/"; | ||
else | ||
validDir = parentDir; | ||
|
||
if (validDir.equals(dir)) | ||
infos.add(((DownloadTask) task).getTaskInfo()); | ||
} | ||
|
||
return infos; | ||
} | ||
|
||
public void retry(int taskID) { | ||
DownloadTask task = (DownloadTask) getTask(taskID); | ||
if (task == null || !task.canRetry()) | ||
return; | ||
addTaskToQue(task.getAccount(), task.getRepoName(), task.getRepoID(), task.getPath()); | ||
} | ||
|
||
// -------------------------- listener method --------------------// | ||
@Override | ||
public void onFileDownloadProgress(int taskID) { | ||
Intent localIntent = new Intent(BROADCAST_ACTION).putExtra("type", | ||
BROADCAST_FILE_DOWNLOAD_PROGRESS).putExtra("taskID", taskID); | ||
LocalBroadcastManager.getInstance(SeadroidApplication.getAppContext()).sendBroadcast(localIntent); | ||
} | ||
|
||
@Override | ||
public void onFileDownloaded(int taskID) { | ||
remove(taskID); | ||
doNext(); | ||
Intent localIntent = new Intent(BROADCAST_ACTION).putExtra("type", | ||
BROADCAST_FILE_DOWNLOAD_SUCCESS).putExtra("taskID", taskID); | ||
LocalBroadcastManager.getInstance(SeadroidApplication.getAppContext()).sendBroadcast(localIntent); | ||
} | ||
|
||
@Override | ||
public void onFileDownloadFailed(int taskID) { | ||
remove(taskID); | ||
doNext(); | ||
Intent localIntent = new Intent(BROADCAST_ACTION).putExtra("type", | ||
BROADCAST_FILE_DOWNLOAD_FAILED).putExtra("taskID", taskID); | ||
LocalBroadcastManager.getInstance(SeadroidApplication.getAppContext()).sendBroadcast(localIntent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.seafile.seadroid2.transfer; | ||
|
||
/** | ||
* Task state | ||
* | ||
* Created by Logan on 15/2/3. | ||
*/ | ||
public enum TaskState { INIT, TRANSFERRING, FINISHED, CANCELLED, TaskState, FAILED } |
Oops, something went wrong.