Granted update of modificationTimestamp with the end of downloads and uploads
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index 1ac3a0a..0ca4248 100644 (file)
@@ -25,8 +25,8 @@ import java.util.Vector;
 import java.util.concurrent.ConcurrentHashMap;\r
 import java.util.concurrent.ConcurrentMap;\r
 \r
+import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
-import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;\r
 import eu.alefzero.webdav.OnDatatransferProgressListener;\r
 \r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
@@ -40,11 +40,8 @@ import android.app.Notification;
 import android.app.NotificationManager;\r
 import android.app.PendingIntent;\r
 import android.app.Service;\r
-import android.content.ContentValues;\r
 import android.content.Intent;\r
-import android.net.Uri;\r
 import android.os.Binder;\r
-import android.os.Environment;\r
 import android.os.Handler;\r
 import android.os.HandlerThread;\r
 import android.os.IBinder;\r
@@ -75,6 +72,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     private IBinder mBinder;\r
     private WebdavClient mDownloadClient = null;\r
     private Account mLastAccount = null;\r
+    private FileDataStorageManager mStorageManager;\r
     \r
     private ConcurrentMap<String, DownloadFileOperation> mPendingDownloads = new ConcurrentHashMap<String, DownloadFileOperation>();\r
     private DownloadFileOperation mCurrentDownload = null;\r
@@ -93,18 +91,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     private String buildRemoteName(Account account, OCFile file) {\r
         return account.name + file.getRemotePath();\r
     }\r
-    \r
-    public static final String getSavePath(String accountName) {\r
-        File sdCard = Environment.getExternalStorageDirectory();\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
-    public static final String getTemporalPath(String accountName) {\r
-        File sdCard = Environment.getExternalStorageDirectory();\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
     \r
     /**\r
@@ -264,6 +250,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             /// prepare client object to send the request to the ownCloud server\r
             if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {\r
                 mLastAccount = mCurrentDownload.getAccount();\r
+                mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());\r
                 mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());\r
             }\r
 \r
@@ -272,16 +259,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             try {\r
                 downloadResult = mCurrentDownload.execute(mDownloadClient);\r
                 if (downloadResult.isSuccess()) {\r
-                    ContentValues cv = new ContentValues();\r
-                    cv.put(ProviderTableMeta.FILE_STORAGE_PATH, mCurrentDownload.getSavePath());\r
-                    getContentResolver().update(\r
-                            ProviderTableMeta.CONTENT_URI,\r
-                            cv,\r
-                            ProviderTableMeta.FILE_NAME + "=? AND "\r
-                                    + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",\r
-                                    new String[] {\r
-                                    mCurrentDownload.getSavePath().substring(mCurrentDownload.getSavePath().lastIndexOf('/') + 1),\r
-                                    mLastAccount.name });\r
+                    saveDownloadedFile();\r
                 }\r
             \r
             } finally {\r
@@ -298,7 +276,22 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         }\r
     }\r
 \r
-    \r
+\r
+    /**\r
+     * Updates the OC File after a successful download.\r
+     */\r
+    private void saveDownloadedFile() {\r
+        OCFile file = mCurrentDownload.getFile();\r
+        file.setLastSyncDate(System.currentTimeMillis());\r
+        file.setModificationTimestamp(mCurrentDownload.getModificationTimestamp());\r
+        // file.setEtag(mCurrentDownload.getEtag());    // TODO Etag, where available\r
+        file.setMimetype(mCurrentDownload.getMimeType());\r
+        file.setStoragePath(mCurrentDownload.getSavePath());\r
+        file.setFileLength((new File(mCurrentDownload.getSavePath()).length()));\r
+        mStorageManager.saveFile(file);\r
+    }\r
+\r
+\r
     /**\r
      * Creates a status notification to show the download progress\r
      * \r
@@ -382,9 +375,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         end.putExtra(EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess());\r
         end.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
         end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());\r
-        if (downloadResult.isSuccess()) {\r
-            end.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
-        }\r
+        end.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
         sendBroadcast(end);\r
     }\r
 \r