Fixed cancel button in landscape account activity
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileDownloader.java
index 1b510cb..b0dd2b2 100644 (file)
@@ -13,6 +13,7 @@ import android.app.PendingIntent;
 import android.app.Service;\r
 import android.content.ContentValues;\r
 import android.content.Intent;\r
 import android.app.Service;\r
 import android.content.ContentValues;\r
 import android.content.Intent;\r
+import android.net.Uri;\r
 import android.os.Environment;\r
 import android.os.Handler;\r
 import android.os.HandlerThread;\r
 import android.os.Environment;\r
 import android.os.Handler;\r
 import android.os.HandlerThread;\r
@@ -35,7 +36,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     public static final String EXTRA_FILE_PATH = "FILE_PATH";\r
     public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";\r
     public static final String EXTRA_FILE_SIZE = "FILE_SIZE";\r
     public static final String EXTRA_FILE_PATH = "FILE_PATH";\r
     public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";\r
     public static final String EXTRA_FILE_SIZE = "FILE_SIZE";\r
-    public static final String ACCOUNT_NAME = "ACCOUNT_NAME";    \r
+    public static final String ACCOUNT_NAME = "ACCOUNT_NAME";\r
+    \r
     private static final String TAG = "FileDownloader";\r
 \r
     private NotificationManager mNotificationMngr;\r
     private static final String TAG = "FileDownloader";\r
 \r
     private NotificationManager mNotificationMngr;\r
@@ -81,14 +83,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         }\r
     }\r
     \r
         }\r
     }\r
     \r
-    public static final String getSavePath() {\r
+    public static final String getSavePath(String accountName) {\r
         File sdCard = Environment.getExternalStorageDirectory();\r
         File sdCard = Environment.getExternalStorageDirectory();\r
-        return sdCard.getAbsolutePath() + "/owncloud/";\r
+        return sdCard.getAbsolutePath() + "/owncloud/" + Uri.encode(accountName, "@");   \r
+            // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B\r
     }\r
     \r
     }\r
     \r
-    public static final String getTemporalPath() {\r
+    public static final String getTemporalPath(String accountName) {\r
         File sdCard = Environment.getExternalStorageDirectory();\r
         File sdCard = Environment.getExternalStorageDirectory();\r
-        return sdCard.getAbsolutePath() + "/owncloud.tmp/";\r
+        return sdCard.getAbsolutePath() + "/owncloud/tmp/" + Uri.encode(accountName, "@");\r
+            // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B\r
     }\r
 \r
     @Override\r
     }\r
 \r
     @Override\r
@@ -154,7 +158,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 \r
         \r
         /// download will be in a temporal file\r
 \r
         \r
         /// download will be in a temporal file\r
-        File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath);\r
+        File tmpFile = new File(getTemporalPath(mAccount.name) + mFilePath);\r
         \r
         /// create status notification to show the download progress\r
         mNotification = new Notification(R.drawable.icon, getString(R.string.downloader_download_in_progress_ticker), System.currentTimeMillis());\r
         \r
         /// create status notification to show the download progress\r
         mNotification = new Notification(R.drawable.icon, getString(R.string.downloader_download_in_progress_ticker), System.currentTimeMillis());\r
@@ -172,26 +176,29 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         tmpFile.getParentFile().mkdirs();\r
         mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath());\r
         File newFile = null;\r
         tmpFile.getParentFile().mkdirs();\r
         mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath());\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
+        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
             \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
+                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
             }\r
+        } finally {\r
+            mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath));\r
         }\r
         }\r
-        mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath));\r
 \r
         \r
         /// notify result\r
 \r
         \r
         /// notify result\r