Skip to content

Commit

Permalink
Merge pull request #351 from Logan676/review_notification
Browse files Browse the repository at this point in the history
Review notification
  • Loading branch information
Logan676 committed May 12, 2015
2 parents 49995f9 + 34d7224 commit 772e062
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ public abstract class BaseNotificationProvider {

protected TransferManager txMgr;
protected TransferService txService;
protected long totalSize;

public BaseNotificationProvider(TransferManager transferManager,
TransferService transferService,
long totalSize) {
TransferService transferService) {
this.txMgr = transferManager;
this.txService = transferService;
this.totalSize = totalSize;
}

/**
Expand Down Expand Up @@ -82,7 +79,7 @@ public void updateNotification() {
String progressInfo = getProgressInfo();
String notifTitle = getNotificationTitle();
int notifId = getNotificationID();
int progress = getNotificationProgress();
int progress = getProgress();

if (getState().equals(NotificationState.NOTIFICATION_STATE_PROGRESS)) {
notifyProgress(notifId, notifTitle, progressInfo, progress);
Expand Down Expand Up @@ -196,30 +193,13 @@ protected void notifyCompletedWithErrors(int notificationID, String title, Strin
*/
protected abstract String getProgressInfo();

private int getNotificationProgress() {
// avoid ArithmeticException
if (totalSize == 0l)
return 0;

return (int) (getFinishedSize() * 100 / totalSize);
}

/**
* get summary size of transferred files
* get progress of transferred files
*
* @return
* summary size
*/
protected abstract long getFinishedSize();

/**
* update data
*
* @param size
* progress
*/
public void updateTotalSize(long size) {
totalSize += size;
}
protected abstract int getProgress();

/**
* Clear notification from notification area
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@
public class DownloadNotificationProvider extends BaseNotificationProvider {

public DownloadNotificationProvider(DownloadTaskManager downloadTaskManager,
TransferService transferService,
long totalSize) {
super(downloadTaskManager, transferService, totalSize);
TransferService transferService) {
super(downloadTaskManager, transferService);
}

@Override
protected String getProgressInfo() {
String progressStatus = "";

// avoid ArithmeticException
if (totalSize == 0
|| txService == null)
if (txService == null)
return progressStatus;

// failed or cancelled tasks won`t be shown in notification state
Expand All @@ -53,7 +50,7 @@ else if (getState().equals(NotificationState.NOTIFICATION_STATE_PROGRESS)) {
getQuantityString(R.plurals.notification_download_info,
downloadingCount,
downloadingCount,
getFinishedSize() * 100 / totalSize);
getProgress());
}
return progressStatus;
}
Expand Down Expand Up @@ -121,18 +118,24 @@ public void notifyStarted() {
}

@Override
protected long getFinishedSize() {
protected int getProgress() {
long downloadedSize = 0l;
long totalSize = 0l;
if (txService == null)
return downloadedSize;
return 0;

List<DownloadTaskInfo> infos = txService.getAllDownloadTaskInfos();
for (DownloadTaskInfo info : infos) {
if (info == null)
continue;
downloadedSize += info.finished;
totalSize += info.fileSize;
}
return downloadedSize;

// avoid ArithmeticException
if (totalSize == 0l)
return 0;
return (int) (downloadedSize * 100 / totalSize);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@
public class UploadNotificationProvider extends BaseNotificationProvider {

public UploadNotificationProvider(UploadTaskManager uploadTaskManager,
TransferService transferService,
long totalSize) {
super(uploadTaskManager, transferService, totalSize);
TransferService transferService) {
super(uploadTaskManager, transferService);

}

@Override
protected String getProgressInfo() {
String progressStatus = "";

// avoid ArithmeticException
if (totalSize == 0
|| txService == null)
if (txService == null)
return progressStatus;

// failed or cancelled tasks won`t be shown in notification state
Expand All @@ -51,10 +48,11 @@ else if (getState().equals(NotificationState.NOTIFICATION_STATE_PROGRESS)) {
}

if (uploadingCount != 0)
progressStatus = String.format(SeadroidApplication.getAppContext().getResources().
getQuantityString(R.plurals.notification_upload_info, uploadingCount),
uploadingCount,
getFinishedSize() * 100 / totalSize);
progressStatus = SeadroidApplication.getAppContext().getResources().
getQuantityString(R.plurals.notification_upload_info,
uploadingCount,
uploadingCount,
getProgress());
}
return progressStatus;
}
Expand Down Expand Up @@ -83,18 +81,24 @@ protected void notifyStarted() {
}

@Override
protected long getFinishedSize() {
protected int getProgress() {
long uploadedSize = 0l;
long totalSize = 0l;
if (txService == null)
return uploadedSize;
return 0;

List<UploadTaskInfo> infos = txService.getNoneCameraUploadTaskInfos();
for (UploadTaskInfo info : infos) {
if (info == null)
continue;
uploadedSize += info.uploadedSize;
totalSize += info.totalSize;
}
return uploadedSize;

if (totalSize == 0)
return 0;

return (int) (uploadedSize * 100 / totalSize);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/com/seafile/seadroid2/transfer/TransferService.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ public void cancelDownloadTask(int taskID) {
cancelDownloadTaskInQue(taskID);
}

public void cancelNotification() {
downloadTaskManager.cancelAllDownloadNotification();
}

public void cancelDownloadTaskInQue(int taskID) {
downloadTaskManager.cancel(taskID);
downloadTaskManager.doNext();
Expand Down
30 changes: 8 additions & 22 deletions src/com/seafile/seadroid2/ui/activity/BrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String[] paths = data.getStringArrayExtra(MultiFileChooserActivity.MULTI_FILES_PATHS);
if (paths == null)
return;
// ToastUtils.show(this, getString(R.string.added_to_upload_tasks));
ToastUtils.show(this, getString(R.string.added_to_upload_tasks));
for (String path : paths) {
addUploadTask(navContext.getRepoID(), navContext.getRepoName(), navContext.getDirPath(), path);
}
Expand All @@ -971,7 +971,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ArrayList<String> paths = data.getStringArrayListExtra("photos");
if (paths == null)
return;
// ToastUtils.show(this, getString(R.string.added_to_upload_tasks));
ToastUtils.show(this, getString(R.string.added_to_upload_tasks));
for (String path : paths) {
addUploadTask(navContext.getRepoID(), navContext.getRepoName(), navContext.getDirPath(), path);
}
Expand Down Expand Up @@ -1024,7 +1024,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(DEBUG_TAG, "Pick file request did not return a path");
return;
}
// ToastUtils.show(this, getString(R.string.added_to_upload_tasks));
ToastUtils.show(this, getString(R.string.added_to_upload_tasks));
addUploadTask(navContext.getRepoID(), navContext.getRepoName(), navContext.getDirPath(), strImgPath);

}
Expand Down Expand Up @@ -1079,7 +1079,6 @@ protected File[] doInBackground(Uri... uriList) {

@Override
protected void onPostExecute(File... fileList) {
long totalSize = 0l;
ArrayList<Integer> taskIDList = Lists.newArrayList();

for (File file: fileList) {
Expand All @@ -1088,7 +1087,6 @@ protected void onPostExecute(File... fileList) {
} else {
int taskID = addUploadTask(navContext.getRepoID(),
navContext.getRepoName(), navContext.getDirPath(), file.getAbsolutePath());
totalSize += file.length();
taskIDList.add(taskID);
}
}
Expand All @@ -1099,15 +1097,9 @@ protected void onPostExecute(File... fileList) {
if (!txService.hasUploadNotifProvider()) {
UploadNotificationProvider provider = new UploadNotificationProvider(
txService.getUploadTaskManager(),
txService,
totalSize);
txService);
txService.saveUploadNotifProvider(provider);
} else {
// if the notificationManager mapping the repoID exist, update its data set
txService.getUploadNotifProvider().updateTotalSize(totalSize);
}


}
}

Expand Down Expand Up @@ -1147,7 +1139,6 @@ private class DownloadDirTask extends AsyncTask<String, Void, List<SeafDirent> >
private String repoID;
private String dirPath;
private int fileCount;
private long fileTotalSize;
private boolean recurse;
private ArrayList<String> dirPaths = Lists.newArrayList();
private SeafException err = null;
Expand Down Expand Up @@ -1198,8 +1189,6 @@ protected List<SeafDirent> doInBackground(String... params) {
continue;
}

fileTotalSize += seafDirent.size;

// txService maybe null if layout orientation has changed
// e.g. landscape and portrait switch
if (txService == null)
Expand Down Expand Up @@ -1233,14 +1222,11 @@ protected void onPostExecute(List<SeafDirent> dirents) {
if (fileCount == 0)
ToastUtils.show(BrowserActivity.this, R.string.transfer_download_no_task);
else {
ToastUtils.show(BrowserActivity.this, getResources().getQuantityString(R.plurals.transfer_download_started, fileCount, fileCount));
if (!txService.hasDownloadNotifProvider()) {
DownloadNotificationProvider provider = new DownloadNotificationProvider(txService.getDownloadTaskManager(),
txService,
fileTotalSize);
txService);
txService.saveDownloadNotifProvider(provider);
} else {
// if the notificationManager mapping the repoID exist, update its data set
txService.getDownloadNotifProvider().updateTotalSize(fileTotalSize);
}

}
Expand Down Expand Up @@ -1687,8 +1673,8 @@ private void onFileUploaded(int taskID) {
&& repoID.equals(navContext.getRepoID())
&& dir.equals(navContext.getDirPath())) {
getReposFragment().refreshView(true);
// String verb = getString(info.isUpdate ? R.string.updated : R.string.uploaded);
// ToastUtils.show(this, verb + " " + Utils.fileNameFromPath(info.localFilePath));
String verb = getString(info.isUpdate ? R.string.updated : R.string.uploaded);
ToastUtils.show(this, verb + " " + Utils.fileNameFromPath(info.localFilePath));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/com/seafile/seadroid2/ui/activity/FileActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private void initWidgets() {
public void onClick(View view) {
if (mTaskID > 0) {
mTransferService.cancelDownloadTask(mTaskID);
mTransferService.cancelNotification();
}
finish();
}
Expand Down Expand Up @@ -178,8 +179,7 @@ private void onFileDownloadProgress(DownloadTaskInfo info) {
if (!mTransferService.hasDownloadNotifProvider()) {
DownloadNotificationProvider provider = new DownloadNotificationProvider(
mTransferService.getDownloadTaskManager(),
mTransferService,
fileSize);
mTransferService);
mTransferService.saveDownloadNotifProvider(provider);
} else {
// if the notificationManager mapping the repoID exist, update its data set
Expand Down

0 comments on commit 772e062

Please sign in to comment.