\r
import java.io.File;\r
import java.util.AbstractList;\r
+import java.util.HashMap;\r
import java.util.Iterator;\r
+import java.util.Map;\r
import java.util.Vector;\r
import java.util.concurrent.ConcurrentHashMap;\r
import java.util.concurrent.ConcurrentMap;\r
DownloadFileOperation newDownload = new DownloadFileOperation(account, file); \r
mPendingDownloads.putIfAbsent(downloadKey, newDownload);\r
newDownload.addDatatransferProgressListener(this);\r
+ newDownload.addDatatransferProgressListener((FileDownloaderBinder)mBinder);\r
requestedDownloads.add(downloadKey);\r
sendBroadcastNewDownload(newDownload);\r
\r
return mBinder;\r
}\r
\r
+\r
+ /**\r
+ * Called when ALL the bound clients were onbound.\r
+ */\r
+ @Override\r
+ public boolean onUnbind(Intent intent) {\r
+ ((FileDownloaderBinder)mBinder).clearListeners();\r
+ return false; // not accepting rebinding (default behaviour)\r
+ }\r
+\r
\r
/**\r
* Binder to let client components to perform operations on the queue of downloads.\r
* \r
* It provides by itself the available operations.\r
*/\r
- public class FileDownloaderBinder extends Binder {\r
+ public class FileDownloaderBinder extends Binder implements OnDatatransferProgressListener {\r
+ \r
+ /** \r
+ * Map of listeners that will be reported about progress of downloads from a {@link FileDownloaderBinder} instance \r
+ */\r
+ private Map<String, OnDatatransferProgressListener> mBoundListeners = new HashMap<String, OnDatatransferProgressListener>();\r
+ \r
\r
/**\r
* Cancels a pending or current download of a remote file.\r
}\r
\r
\r
+ public void clearListeners() {\r
+ mBoundListeners.clear();\r
+ }\r
+\r
+\r
/**\r
* Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download.\r
* \r
* @param file A file that could be in the queue of downloads.\r
*/\r
public boolean isDownloading(Account account, OCFile file) {\r
+ if (account == null || file == null) return false;\r
String targetKey = buildRemoteName(account, file);\r
synchronized (mPendingDownloads) {\r
if (file.isDirectory()) {\r
}\r
}\r
}\r
+\r
+ \r
+ /**\r
+ * Adds a listener interested in the progress of the download for a concrete file.\r
+ * \r
+ * @param listener Object to notify about progress of transfer. \r
+ * @param account ownCloud account holding the file of interest.\r
+ * @param file {@link OCfile} of interest for listener. \r
+ */\r
+ public void addDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {\r
+ if (account == null || file == null || listener == null) return;\r
+ String targetKey = buildRemoteName(account, file);\r
+ mBoundListeners.put(targetKey, listener);\r
+ }\r
+ \r
+ \r
+ \r
+ /**\r
+ * Removes a listener interested in the progress of the download for a concrete file.\r
+ * \r
+ * @param listener Object to notify about progress of transfer. \r
+ * @param account ownCloud account holding the file of interest.\r
+ * @param file {@link OCfile} of interest for listener. \r
+ */\r
+ public void removeDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {\r
+ if (account == null || file == null || listener == null) return;\r
+ String targetKey = buildRemoteName(account, file);\r
+ if (mBoundListeners.get(targetKey) == listener) {\r
+ mBoundListeners.remove(targetKey);\r
+ }\r
+ }\r
+\r
+\r
+ @Override\r
+ public void onTransferProgress(long progressRate) {\r
+ // old way, should not be in use any more\r
+ }\r
+\r
+\r
+ @Override\r
+ public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer,\r
+ String fileName) {\r
+ String key = buildRemoteName(mCurrentDownload.getAccount(), mCurrentDownload.getFile());\r
+ OnDatatransferProgressListener boundListener = mBoundListeners.get(key);\r
+ if (boundListener != null) {\r
+ boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName);\r
+ }\r
+ }\r
+ \r
}\r
\r
\r
file.setLastSyncDateForProperties(syncDate);\r
file.setLastSyncDateForData(syncDate);\r
file.setModificationTimestamp(mCurrentDownload.getModificationTimestamp());\r
+ file.setModificationTimestampAtLastSyncForData(mCurrentDownload.getModificationTimestamp());\r
// file.setEtag(mCurrentDownload.getEtag()); // TODO Etag, where available\r
file.setMimetype(mCurrentDownload.getMimeType());\r
file.setStoragePath(mCurrentDownload.getSavePath());\r
*/\r
private void sendBroadcastNewDownload(DownloadFileOperation download) {\r
Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE);\r
- /*added.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
- added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());*/\r
+ added.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
+ added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());\r
added.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
sendStickyBroadcast(added);\r
}\r