Automatic download of previewable files in download mode
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index f58e563..d3317d8 100644 (file)
@@ -20,7 +20,9 @@ package com.owncloud.android.files.services;
 \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
@@ -135,6 +137,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             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
@@ -164,13 +167,29 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         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
@@ -189,17 +208,85 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         }\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
+         * 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
-                return (mPendingDownloads.containsKey(buildRemoteName(account, file)));\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
@@ -288,6 +375,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         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
@@ -391,8 +479,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      */\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