Workaround to show hidden accents in options menu
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index 1ac3a0a..f58e563 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
@@ -62,6 +59,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
     public static final String EXTRA_FILE = "FILE";\r
     \r
+    public static final String DOWNLOAD_ADDED_MESSAGE = "DOWNLOAD_ADDED";\r
     public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";\r
     public static final String EXTRA_DOWNLOAD_RESULT = "RESULT";    \r
     public static final String EXTRA_FILE_PATH = "FILE_PATH";\r
@@ -75,6 +73,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 +92,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
@@ -149,6 +136,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             mPendingDownloads.putIfAbsent(downloadKey, newDownload);\r
             newDownload.addDatatransferProgressListener(this);\r
             requestedDownloads.add(downloadKey);\r
+            sendBroadcastNewDownload(newDownload);\r
             \r
         } catch (IllegalArgumentException e) {\r
             Log.e(TAG, "Not enough information provided in intent: " + e.getMessage());\r
@@ -264,6 +252,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 +261,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
@@ -294,11 +274,28 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             /// notify result\r
             notifyDownloadResult(mCurrentDownload, downloadResult);\r
             \r
-            sendFinalBroadcast(mCurrentDownload, downloadResult);\r
+            sendBroadcastDownloadFinished(mCurrentDownload, downloadResult);\r
         }\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
+        long syncDate = System.currentTimeMillis();\r
+        file.setLastSyncDateForProperties(syncDate);\r
+        file.setLastSyncDateForData(syncDate);\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
@@ -372,20 +369,32 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     \r
     \r
     /**\r
-     * Sends a broadcast in order to the interested activities can update their view\r
+     * Sends a broadcast when a download finishes in order to the interested activities can update their view\r
      * \r
      * @param download          Finished download operation\r
      * @param downloadResult    Result of the download operation\r
      */\r
-    private void sendFinalBroadcast(DownloadFileOperation download, RemoteOperationResult downloadResult) {\r
+    private void sendBroadcastDownloadFinished(DownloadFileOperation download, RemoteOperationResult downloadResult) {\r
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
         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
-        sendBroadcast(end);\r
+        end.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
+        sendStickyBroadcast(end);\r
+    }\r
+    \r
+    \r
+    /**\r
+     * Sends a broadcast when a new download is added to the queue.\r
+     * \r
+     * @param download          Added download operation\r
+     */\r
+    private void sendBroadcastNewDownload(DownloadFileOperation download) {\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