Upload really in progress always and only in top of the list of current uploads

This commit is contained in:
David A. Velasco 2016-03-15 18:44:53 +01:00
parent 259c94a45b
commit 26d748a2d3
2 changed files with 66 additions and 25 deletions

View file

@ -721,6 +721,17 @@ public class FileUploader extends Service
}
public boolean isUploadingNow(OCUpload upload) {
return (
upload != null &&
mCurrentAccount != null &&
mCurrentUpload != null &&
upload.getAccountName().equals(mCurrentAccount.name) &&
upload.getRemotePath().equals(mCurrentUpload.getRemotePath())
);
}
/**
* Adds a listener interested in the progress of the upload for a concrete file.
*
@ -743,16 +754,14 @@ public class FileUploader extends Service
* Adds a listener interested in the progress of the upload for a concrete file.
*
* @param listener Object to notify about progress of transfer.
* @param account ownCloud account holding the file of interest.
* @param ocUpload {@link OCUpload} of interest for listener.
*/
public void addDatatransferProgressListener(
OnDatatransferProgressListener listener,
Account account,
OCUpload ocUpload
) {
if (account == null || ocUpload == null || listener == null) return;
String targetKey = buildRemoteName(account.name, ocUpload.getRemotePath());
if (ocUpload == null || listener == null) return;
String targetKey = buildRemoteName(ocUpload.getAccountName(), ocUpload.getRemotePath());
mBoundListeners.put(targetKey, listener);
}
@ -781,16 +790,14 @@ public class FileUploader extends Service
* Removes a listener interested in the progress of the upload for a concrete file.
*
* @param listener Object to notify about progress of transfer.
* @param account ownCloud account holding the file of interest.
* @param ocUpload Stored upload of interest
*/
public void removeDatatransferProgressListener(
OnDatatransferProgressListener listener,
Account account,
OCUpload ocUpload
) {
if (account == null || ocUpload == null || listener == null) return;
String targetKey = buildRemoteName(account.name, ocUpload.getRemotePath());
if (ocUpload == null || listener == null) return;
String targetKey = buildRemoteName(ocUpload.getAccountName(), ocUpload.getRemotePath());
if (mBoundListeners.get(targetKey) == listener) {
mBoundListeners.remove(targetKey);
}

View file

@ -91,8 +91,19 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
}
public Comparator<OCUpload> comparator = new Comparator<OCUpload>() {
@Override
public int compare(OCUpload upload1, OCUpload upload2) {
if (upload1.getUploadStatus().equals(UploadStatus.UPLOAD_IN_PROGRESS)) {
FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
if (binder != null) {
if (binder.isUploadingNow(upload1)) {
return -1;
} else if (binder.isUploadingNow(upload2)) {
return -1;
}
}
}
if (upload1.getUploadEndTimestamp() == 0) {
return compareUploadId(upload1, upload2);
} else {
@ -262,17 +273,34 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.upload_progress_bar);
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
mProgressListener = new ProgressListener(progressBar);
if (mParentActivity.getFileUploaderBinder() != null) {
mParentActivity.getFileUploaderBinder().addDatatransferProgressListener(
FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
if (binder != null) {
if (binder.isUploadingNow(upload)) {
/// really uploading, bind the progress bar to listen for progess updates
mProgressListener = new ProgressListener(upload, progressBar);
binder.addDatatransferProgressListener(
mProgressListener,
mParentActivity.getAccount(),
upload
);
);
} else {
/// not really uploading; stop listening progress if view is reused!
if (convertView != null &&
mProgressListener != null &&
mProgressListener.isWrapping(progressBar)) {
binder.removeDatatransferProgressListener(
mProgressListener,
mProgressListener.getUpload() // the one that was added
);
mProgressListener = null;
}
}
} else {
Log_OC.e(TAG, "UploadBinder == null. It should have been created on creating mParentActivity"
+ " which inherits from FileActivity. Fix that!");
Log_OC.e(TAG, "PENDING BINDING for upload = " + upload.getLocalPath());
Log_OC.w(
TAG,
"FileUploaderBinder not ready yet for upload " + upload.getRemotePath()
);
}
uploadDateTextView.setVisibility(View.GONE);
pathTextView.setVisibility(View.GONE);
@ -356,14 +384,6 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
if (upload.getUploadStatus() != UploadStatus.UPLOAD_IN_PROGRESS) {
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.upload_progress_bar);
progressBar.setVisibility(View.GONE);
if (mParentActivity.getFileUploaderBinder() != null && mProgressListener != null) {
mParentActivity.getFileUploaderBinder().removeDatatransferProgressListener(
mProgressListener,
upload.getAccount(mParentActivity),
upload
);
mProgressListener = null;
}
}
statusTextView.setText(status);
@ -616,9 +636,11 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
public class ProgressListener implements OnDatatransferProgressListener {
int mLastPercent = 0;
OCUpload mUpload = null;
WeakReference<ProgressBar> mProgressBar = null;
ProgressListener(ProgressBar progressBar) {
public ProgressListener(OCUpload upload, ProgressBar progressBar) {
mUpload = upload;
mProgressBar = new WeakReference<ProgressBar>(progressBar);
}
@ -636,6 +658,18 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
mLastPercent = percent;
}
public boolean isWrapping(ProgressBar progressBar) {
ProgressBar wrappedProgressBar = mProgressBar.get();
return (
wrappedProgressBar != null &&
wrappedProgressBar == progressBar // on purpose; don't replace with equals
);
}
public OCUpload getUpload() {
return mUpload;
}
}
;