- /// perform the download\r
- tmpFile.getParentFile().mkdirs();\r
- mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath());\r
- File newFile = null;\r
- try {\r
- if (wdc.downloadFile(mRemotePath, tmpFile)) {\r
- newFile = new File(getSavePath(mAccount.name) + mFilePath);\r
- newFile.getParentFile().mkdirs();\r
- boolean moved = tmpFile.renameTo(newFile);\r
- \r
- if (moved) {\r
- ContentValues cv = new ContentValues();\r
- cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newFile.getAbsolutePath());\r
- getContentResolver().update(\r
- ProviderTableMeta.CONTENT_URI,\r
- cv,\r
- ProviderTableMeta.FILE_NAME + "=? AND "\r
- + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",\r
- new String[] {\r
- mFilePath.substring(mFilePath.lastIndexOf('/') + 1),\r
- mAccount.name });\r
- downloadResult = true;\r
+\r
+ /**\r
+ * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download.\r
+ * \r
+ * If 'file' is a directory, returns 'true' if some of its descendant files is downloading or waiting to download. \r
+ * \r
+ * @param account Owncloud account where the remote file is stored.\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
+ // this can be slow if there are many downloads :(\r
+ Iterator<String> it = mPendingDownloads.keySet().iterator();\r
+ boolean found = false;\r
+ while (it.hasNext() && !found) {\r
+ found = it.next().startsWith(targetKey);\r
+ }\r
+ return found;\r
+ } else {\r
+ return (mPendingDownloads.containsKey(targetKey));\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
+ /** \r
+ * Download worker. Performs the pending downloads in the order they were requested. \r
+ * \r
+ * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}. \r
+ */\r
+ private static class ServiceHandler extends Handler {\r
+ // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak\r
+ FileDownloader mService;\r
+ public ServiceHandler(Looper looper, FileDownloader service) {\r
+ super(looper);\r
+ if (service == null)\r
+ throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");\r
+ mService = service;\r
+ }\r
+\r
+ @Override\r
+ public void handleMessage(Message msg) {\r
+ @SuppressWarnings("unchecked")\r
+ AbstractList<String> requestedDownloads = (AbstractList<String>) msg.obj;\r
+ if (msg.obj != null) {\r
+ Iterator<String> it = requestedDownloads.iterator();\r
+ while (it.hasNext()) {\r
+ mService.downloadFile(it.next());\r