Procházet zdrojové kódy

Downloads through temporal file and better OCFile.isDownloading() and .isDown() implementation

David A. Velasco před 12 roky
rodič
revize
6f189bffe1

+ 1 - 1
AndroidManifest.xml

@@ -18,7 +18,7 @@
  -->
 <manifest package="eu.alefzero.owncloud"
     android:versionCode="1"
-    android:versionName="0.1.174B" xmlns:android="http://schemas.android.com/apk/res/android">
+    android:versionName="0.1.175B" xmlns:android="http://schemas.android.com/apk/res/android">
 
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />

+ 6 - 7
src/eu/alefzero/owncloud/datamodel/OCFile.java

@@ -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;
     }

+ 31 - 10
src/eu/alefzero/owncloud/files/services/FileDownloader.java

@@ -60,6 +60,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             stopSelf(msg.arg1);
         }
     }
+    
+    public static final String getSavePath() {
+        File sdCard = Environment.getExternalStorageDirectory();
+        return sdCard.getAbsolutePath() + "/owncloud/";
+    }
+    
+    public static final String getTemporalPath() {
+        File sdCard = Environment.getExternalStorageDirectory();
+        return sdCard.getAbsolutePath() + "/owncloud.tmp/";
+    }
 
     @Override
     public void onCreate() {
@@ -128,29 +138,40 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         
         mNotificationMngr.notify(1, mNotification);
 
-        File sdCard = Environment.getExternalStorageDirectory();
-        File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);
-        file.getParentFile().mkdirs();
+        // download in a temporal file
+        File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath);
+        tmpFile.getParentFile().mkdirs();
 
         boolean download_result = false;
-        if (wdc.downloadFile(mRemotePath, file)) {
-            ContentValues cv = new ContentValues();
-            cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());
-            getContentResolver().update(
+        File newFile = null;
+        if (wdc.downloadFile(mRemotePath, tmpFile)) {
+            newFile = new File(getSavePath() + mAccount.name + mFilePath);
+            newFile.getParentFile().mkdirs();
+            boolean moved = tmpFile.renameTo(newFile);
+            
+            if (moved) {
+                ContentValues cv = new ContentValues();
+                cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newFile.getAbsolutePath());
+                getContentResolver().update(
                     ProviderTableMeta.CONTENT_URI,
                     cv,
                     ProviderTableMeta.FILE_NAME + "=? AND "
                             + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
                     new String[] {
                             mFilePath.substring(mFilePath.lastIndexOf('/') + 1),
-                            mAccount.name });            
-            download_result = true;
+                            mAccount.name });
+                download_result = true;
+            }
+        }
+        
+        if (!download_result) {
+            tmpFile.delete();
         }
         
         mNotificationMngr.cancel(1);
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);
         end.putExtra(EXTRA_REMOTE_PATH, mRemotePath);
-        end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());
+        end.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath());
         end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);
         end.putExtra(ACCOUNT_NAME, mAccount.name);
         sendBroadcast(end);

+ 4 - 4
src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java

@@ -111,12 +111,12 @@ public class FileListListAdapter implements ListAdapter {
             }
             ImageView downloaded = (ImageView) view.findViewById(R.id.imageView2);
             ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);
-            if (file.isDown()) {
-                 downloaded.setVisibility(View.VISIBLE);
-                 downloading.setVisibility(View.INVISIBLE);
-            } else if (file.isDownloading()) {
+            if (file.isDownloading()) {
                 downloaded.setVisibility(View.INVISIBLE);
                 downloading.setVisibility(View.VISIBLE);
+            } else if (file.isDown()) {
+                 downloaded.setVisibility(View.VISIBLE);
+                 downloading.setVisibility(View.INVISIBLE);
             } else {
                 downloaded.setVisibility(View.INVISIBLE);
                 downloading.setVisibility(View.INVISIBLE);