Downloads through temporal file and better OCFile.isDownloading() and .isDown() imple...
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 23 Jul 2012 11:55:58 +0000 (13:55 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 23 Jul 2012 11:55:58 +0000 (13:55 +0200)
AndroidManifest.xml
src/eu/alefzero/owncloud/datamodel/OCFile.java
src/eu/alefzero/owncloud/files/services/FileDownloader.java
src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java

index c0082bf..003f31f 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.174B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.175B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index 32d36de..7284604 100644 (file)
@@ -22,6 +22,8 @@ import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
 
+import eu.alefzero.owncloud.files.services.FileDownloader;
+
 import android.net.Uri;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -144,14 +146,12 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     /**
      * Use this to check if this file is available locally
      * 
-     * TODO use a better condition not dependent upon mLenght being synchronized; to change when downloads are done through a temporal file
-     * 
      * @return true if it is
      */
     public boolean isDown() {
         if (mLocalPath != null && mLocalPath.length() > 0) {
             File file = new File(mLocalPath);
-            return (file.exists() && file.length() == mLength);
+            return (file.exists());
         }
         return false;
     }
@@ -159,14 +159,13 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     /**
      * Use this to check if this file is downloading
      * 
-     * TODO use a better condition not dependent upon mLenght being synchronized; to change when downloads are done through a temporal file
-     * 
      * @return true if it is in a download in progress
      */
     public boolean isDownloading() {
         if (mLocalPath != null && mLocalPath.length() > 0) {
-            File file = new File(mLocalPath);
-            return (file.exists() && file.length() < mLength);  
+            String savePath = FileDownloader.getSavePath();
+            File file = new File(FileDownloader.getTemporalPath() + mLocalPath.substring(savePath.length()));
+            return (file.exists());  
         }
         return false;
     }
index d08e727..cb5d0c8 100644 (file)
@@ -60,6 +60,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,29 +138,40 @@ 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
 \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
+        if (!download_result) {\r
+            tmpFile.delete();\r
         }\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_FILE_PATH, newFile.getAbsolutePath());\r
         end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);\r
         end.putExtra(ACCOUNT_NAME, mAccount.name);\r
         sendBroadcast(end);\r
index 32cbb16..9c79603 100644 (file)
@@ -111,12 +111,12 @@ public class FileListListAdapter implements ListAdapter {
             }\r
             ImageView downloaded = (ImageView) view.findViewById(R.id.imageView2);\r
             ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);\r
-            if (file.isDown()) {\r
-                 downloaded.setVisibility(View.VISIBLE);\r
-                 downloading.setVisibility(View.INVISIBLE);\r
-            } else if (file.isDownloading()) {\r
+            if (file.isDownloading()) {\r
                 downloaded.setVisibility(View.INVISIBLE);\r
                 downloading.setVisibility(View.VISIBLE);\r
+            } else if (file.isDown()) {\r
+                 downloaded.setVisibility(View.VISIBLE);\r
+                 downloading.setVisibility(View.INVISIBLE);\r
             } else {\r
                 downloaded.setVisibility(View.INVISIBLE);\r
                 downloading.setVisibility(View.INVISIBLE);\r