+
+ /**
+ * Binder to let client components to perform operations on the queue of
+ * uploads.
+ *
+ * It provides by itself the available operations.
+ */
+ public class FileUploaderBinder extends Binder implements OnDatatransferProgressListener {
+
+ /**
+ * Map of listeners that will be reported about progress of uploads from a {@link FileUploaderBinder} instance
+ */
+ private Map<String, OnDatatransferProgressListener> mBoundListeners = new HashMap<String, OnDatatransferProgressListener>();
+
+ /**
+ * Cancels a pending or current upload of a remote file.
+ *
+ * @param account Owncloud account where the remote file will be stored.
+ * @param file A file in the queue of pending uploads
+ */
+ public void cancel(Account account, OCFile file) {
+ UploadFileOperation upload = null;
+ synchronized (mPendingUploads) {
+ upload = mPendingUploads.remove(buildRemoteName(account, file));
+ }
+ if (upload != null) {
+ upload.cancel();
+ }
+ }
+
+
+
+ public void clearListeners() {
+ mBoundListeners.clear();
+ }
+
+
+
+
+ /**
+ * Returns True when the file described by 'file' is being uploaded to
+ * the ownCloud account 'account' or waiting for it
+ *
+ * If 'file' is a directory, returns 'true' if some of its descendant files is uploading or waiting to upload.
+ *
+ * @param account Owncloud account where the remote file will be stored.
+ * @param file A file that could be in the queue of pending uploads
+ */
+ public boolean isUploading(Account account, OCFile file) {
+ if (account == null || file == null)
+ return false;
+ String targetKey = buildRemoteName(account, file);
+ synchronized (mPendingUploads) {
+ if (file.isDirectory()) {
+ // this can be slow if there are many uploads :(
+ Iterator<String> it = mPendingUploads.keySet().iterator();
+ boolean found = false;
+ while (it.hasNext() && !found) {
+ found = it.next().startsWith(targetKey);
+ }
+ return found;
+ } else {
+ return (mPendingUploads.containsKey(targetKey));
+ }
+ }
+ }
+
+
+ /**
+ * 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 file {@link OCfile} of interest for listener.
+ */
+ public void addDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {
+ if (account == null || file == null || listener == null) return;
+ String targetKey = buildRemoteName(account, file);
+ mBoundListeners.put(targetKey, listener);
+ }
+
+
+
+ /**
+ * 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 file {@link OCfile} of interest for listener.
+ */
+ public void removeDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {
+ if (account == null || file == null || listener == null) return;
+ String targetKey = buildRemoteName(account, file);
+ if (mBoundListeners.get(targetKey) == listener) {
+ mBoundListeners.remove(targetKey);
+ }
+ }
+
+
+ @Override
+ public void onTransferProgress(long progressRate) {
+ // old way, should not be in use any more
+ }
+
+
+ @Override
+ public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer,
+ String fileName) {
+ String key = buildRemoteName(mCurrentUpload.getAccount(), mCurrentUpload.getFile());
+ OnDatatransferProgressListener boundListener = mBoundListeners.get(key);
+ if (boundListener != null) {
+ boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName);
+ }
+ }
+
+ }
+
+ /**
+ * Upload worker. Performs the pending uploads in the order they were
+ * requested.