Skip to content

Commit

Permalink
refactor transfer manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Logan676 committed Feb 9, 2015
1 parent 5b756f6 commit fb85567
Show file tree
Hide file tree
Showing 27 changed files with 894 additions and 1,244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*~
classes
*#
bin
gen
Expand Down
2 changes: 2 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,6 @@
<string name="transfer_list_cancel_all">Cancel all</string>
<string name="transfer_tabs_downloads">Download List</string>
<string name="transfer_tabs_uploads">Upload List</string>
<string name="transfer_download_no_task">All files has been downloaded</string>
<string name="transfer_download_started">Starting to download %d files</string>
</resources>
12 changes: 5 additions & 7 deletions src/com/seafile/seadroid2/cameraupload/CameraUploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
import com.seafile.seadroid2.SettingsManager;
import com.seafile.seadroid2.account.Account;
import com.seafile.seadroid2.data.SeafCachedPhoto;
import com.seafile.seadroid2.transfer.PendingUploadInfo;
import com.seafile.seadroid2.transfer.TransferService;
import com.seafile.seadroid2.transfer.*;
import com.seafile.seadroid2.transfer.TransferService.TransferBinder;
import com.seafile.seadroid2.transfer.UploadTaskInfo;
import com.seafile.seadroid2.util.CameraUploadUtil;

public class CameraUploadService extends Service {
Expand Down Expand Up @@ -75,13 +73,13 @@ public void onCreate() {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false,
cameraUploadObserver);
LocalBroadcastManager.getInstance(this).registerReceiver(transferReceiver,
new IntentFilter(TransferService.BROADCAST_ACTION));
new IntentFilter(TransferManager.BROADCAST_ACTION));
}

private void cancelUploadTasks(){

mTransferService.cancelAllCameraUploadTasks();
Intent localIntent = new Intent(TransferService.BROADCAST_ACTION).putExtra("type",
Intent localIntent = new Intent(TransferManager.BROADCAST_ACTION).putExtra("type",
BROADCAST_CAMERA_UPLOAD_SERVICE_STOPPED);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(localIntent);
}
Expand Down Expand Up @@ -241,7 +239,7 @@ protected void onPostExecute(List<File> result) {
// do nothing until network connection available
}
if (isNetworkAvailable && !isRemoteCameraUploadRepoValid) {
localIntent = new Intent(TransferService.BROADCAST_ACTION).putExtra("type",
localIntent = new Intent(TransferManager.BROADCAST_ACTION).putExtra("type",
BROADCAST_CAMERA_UPLOAD_LIBRARY_NOT_FOUND);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(localIntent);
}
Expand Down Expand Up @@ -295,7 +293,7 @@ public void onReceive(Context context, Intent intent) {
}
List<String> list = Lists.newArrayList();

if (type.equals(TransferService.BROADCAST_FILE_UPLOAD_SUCCESS)) {
if (type.equals(UploadTaskManager.BROADCAST_FILE_UPLOAD_SUCCESS)) {
int taskID = intent.getIntExtra("taskID", 0);
UploadTaskInfo info = mTransferService.getUploadTaskInfo(taskID);

Expand Down
12 changes: 5 additions & 7 deletions src/com/seafile/seadroid2/monitor/FileMonitorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

import com.seafile.seadroid2.ConcurrentAsyncTask;
import com.seafile.seadroid2.account.Account;
import com.seafile.seadroid2.transfer.DownloadTaskInfo;
import com.seafile.seadroid2.transfer.TransferService;
import com.seafile.seadroid2.transfer.UploadTaskInfo;
import com.seafile.seadroid2.transfer.*;

/**
* Monitor changes of local cached files, and upload them through TransferService if modified
Expand Down Expand Up @@ -67,7 +65,7 @@ public void onCreate() {
bindService(bindIntent, mTransferConnection, Context.BIND_AUTO_CREATE);

LocalBroadcastManager.getInstance(this).registerReceiver(transferReceiver,
new IntentFilter(TransferService.BROADCAST_ACTION));
new IntentFilter(TransferManager.BROADCAST_ACTION));
}

@Override
Expand Down Expand Up @@ -126,7 +124,7 @@ public void onReceive(Context context, Intent intent) {
return;
}

if (type.equals(TransferService.BROADCAST_FILE_DOWNLOAD_SUCCESS)) {
if (type.equals(DownloadTaskManager.BROADCAST_FILE_DOWNLOAD_SUCCESS)) {
int taskID = intent.getIntExtra("taskID", 0);
DownloadTaskInfo info = mTransferService.getDownloadTaskInfo(taskID);
if (info != null) {
Expand All @@ -135,15 +133,15 @@ public void onReceive(Context context, Intent intent) {
info.pathInRepo, info.localFilePath);
}
}
} else if (type.equals(TransferService.BROADCAST_FILE_UPLOAD_SUCCESS)) {
} else if (type.equals(UploadTaskManager.BROADCAST_FILE_UPLOAD_SUCCESS)) {
int taskID = intent.getIntExtra("taskID", 0);
UploadTaskInfo info = mTransferService.getUploadTaskInfo(taskID);

if (info != null && info.isUpdate) {
updateMgr.onFileUpdateSuccess(info.account, info.repoID, info.repoName,
info.parentDir, info.localFilePath);
}
} else if (type.equals(TransferService.BROADCAST_FILE_UPLOAD_FAILED)) {
} else if (type.equals(UploadTaskManager.BROADCAST_FILE_UPLOAD_FAILED)) {
int taskID = intent.getIntExtra("taskID", 0);
UploadTaskInfo info = mTransferService.getUploadTaskInfo(taskID);

Expand Down
12 changes: 12 additions & 0 deletions src/com/seafile/seadroid2/transfer/DownloadStateListener.java
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);
}
96 changes: 96 additions & 0 deletions src/com/seafile/seadroid2/transfer/DownloadTask.java
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;
}
}
5 changes: 3 additions & 2 deletions src/com/seafile/seadroid2/transfer/DownloadTaskInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.seafile.seadroid2.SeafException;
import com.seafile.seadroid2.account.Account;
import com.seafile.seadroid2.transfer.TransferManager.TaskState;

/**
* download task info
*/
public class DownloadTaskInfo extends TransferTaskInfo {

public final String pathInRepo;
Expand Down
126 changes: 126 additions & 0 deletions src/com/seafile/seadroid2/transfer/DownloadTaskManager.java
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);
}
}
8 changes: 8 additions & 0 deletions src/com/seafile/seadroid2/transfer/TaskState.java
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 }
Loading

0 comments on commit fb85567

Please sign in to comment.