Added a static list of downloads in progress to have a reliable way to check if a...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileDownloader.java
index d08e727..c147c9d 100644 (file)
@@ -2,6 +2,9 @@ package eu.alefzero.owncloud.files.services;
 \r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -48,7 +51,27 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     private long mTotalDownloadSize;\r
     private long mCurrentDownlodSize;\r
     private Notification mNotification;\r
+    \r
+    /**\r
+     * Static map with the files being download and the path to the temporal file were are download\r
+     */\r
+    private static Map<String, String> mDownloadsInProgress = Collections.synchronizedMap(new HashMap<String, String>());\r
+    \r
+    /**\r
+     * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading\r
+     */\r
+    public static boolean isDownloading(Account account, String remotePath) {\r
+        return (mDownloadsInProgress.get(buildRemoteName(account.name, remotePath)) != null);\r
+    }\r
+    \r
+    /**\r
+     * Builds a key for mDownloadsInProgress from the accountName and remotePath\r
+     */\r
+    private static String buildRemoteName(String accountName, String remotePath) {\r
+        return accountName + remotePath;\r
+    }\r
 \r
+    \r
     private final class ServiceHandler extends Handler {\r
         public ServiceHandler(Looper looper) {\r
             super(looper);\r
@@ -60,6 +83,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             stopSelf(msg.arg1);\r
         }\r
     }\r
+    \r
+    public static final String getSavePath() {\r
+        File sdCard = Environment.getExternalStorageDirectory();\r
+        return sdCard.getAbsolutePath() + "/owncloud/";\r
+    }\r
+    \r
+    public static final String getTemporalPath() {\r
+        File sdCard = Environment.getExternalStorageDirectory();\r
+        return sdCard.getAbsolutePath() + "/owncloud.tmp/";\r
+    }\r
 \r
     @Override\r
     public void onCreate() {\r
@@ -128,31 +161,43 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         \r
         mNotificationMngr.notify(1, mNotification);\r
 \r
-        File sdCard = Environment.getExternalStorageDirectory();\r
-        File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);\r
-        file.getParentFile().mkdirs();\r
+        // download in a temporal file\r
+        File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath);\r
+        tmpFile.getParentFile().mkdirs();\r
+        mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath());\r
 \r
         boolean download_result = false;\r
-        if (wdc.downloadFile(mRemotePath, file)) {\r
-            ContentValues cv = new ContentValues();\r
-            cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());\r
-            getContentResolver().update(\r
+        File newFile = null;\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
-            download_result = true;\r
+                            mAccount.name });\r
+                download_result = true;\r
+            }\r
         }\r
         \r
+        mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath));\r
+        \r
         mNotificationMngr.cancel(1);\r
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
-        end.putExtra(EXTRA_REMOTE_PATH, mRemotePath);\r
-        end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());\r
         end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);\r
         end.putExtra(ACCOUNT_NAME, mAccount.name);\r
+        end.putExtra(EXTRA_REMOTE_PATH, mRemotePath);\r
+        if (download_result) {\r
+            end.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath());\r
+        }\r
         sendBroadcast(end);\r
 \r
         if (download_result) {\r