Version up: 1.3.22
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index 51b8406..ac70e39 100644 (file)
@@ -1,9 +1,10 @@
 /* ownCloud Android client application\r
  *   Copyright (C) 2012 Bartek Przybylski\r
+ *   Copyright (C) 2012-2013 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
+ *   the Free Software Foundation, either version 2 of the License, or\r
  *   (at your option) any later version.\r
  *\r
  *   This program is distributed in the hope that it will be useful,\r
@@ -190,14 +191,28 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         \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
@@ -288,6 +303,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
@@ -380,7 +396,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         end.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
         end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());\r
         end.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
-        sendBroadcast(end);\r
+        sendStickyBroadcast(end);\r
     }\r
     \r
     \r
@@ -390,11 +406,11 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      * @param download          Added download operation\r
      */\r
     private void sendBroadcastNewDownload(DownloadFileOperation download) {\r
-        Intent end = new Intent(DOWNLOAD_ADDED_MESSAGE);\r
-        /*end.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
-        end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());*/\r
-        end.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
-        sendBroadcast(end);\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(EXTRA_FILE_PATH, download.getSavePath());\r
+        sendStickyBroadcast(added);\r
     }\r
 \r
 }\r