Granted update of modificationTimestamp with the end of downloads and uploads
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index 8488294..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,7 +40,6 @@ 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.os.Binder;\r
 import android.os.Handler;\r
@@ -73,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
@@ -250,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
@@ -258,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
@@ -284,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
@@ -368,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