Fixed DownloadRemoteOperation to use real total transfer size and to simplify constructor
authorDavid A. Velasco <dvelasco@solidgear.es>
Wed, 15 Jan 2014 10:48:10 +0000 (11:48 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Wed, 15 Jan 2014 10:48:10 +0000 (11:48 +0100)
oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java
oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/MainActivity.java
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/DownloadRemoteFileOperation.java
src/com/owncloud/android/operations/DownloadFileOperation.java

index 2030a8f..8270fad 100644 (file)
@@ -144,7 +144,7 @@ public class TestActivity extends Activity {
                File folder = new File(sdCard.getAbsolutePath() + "/" + path);
                folder.mkdirs();
                
-               DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile, folder.getAbsolutePath());
+               DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile.getRemotePath(), folder.getAbsolutePath());
                RemoteOperationResult result = downloadOperation.execute(mClient);
 
                return result;
index 6f7d216..ad2b4dd 100644 (file)
@@ -146,9 +146,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
        File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
        File fileToUpload = upFolder.listFiles()[0];
        String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
-       RemoteFile rFileToDownload = new RemoteFile(remotePath);
-       rFileToDownload.setLength(fileToUpload.length());
-       DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(rFileToDownload, downFolder.getAbsolutePath());
+       DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath());
        downloadOperation.addDatatransferProgressListener(this);
        downloadOperation.execute(mClient, this, mHandler);
     }
index e3e7ee0..5103c56 100644 (file)
@@ -21,7 +21,6 @@ import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.Date;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
@@ -38,7 +37,6 @@ import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressLi
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
 import com.owncloud.android.oc_framework.operations.OperationCancelledException;
-import com.owncloud.android.oc_framework.operations.RemoteFile;
 import com.owncloud.android.oc_framework.operations.RemoteOperation;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 
@@ -55,15 +53,15 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
     
        private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
     private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
-    private long mModificationTimestamp = 0;
+    //private long mModificationTimestamp = 0;
     private GetMethod mGet;
     
-    private RemoteFile mRemoteFile;
-    private String mTemporalFolder;
+    private String mRemotePath;
+    private String mDownloadFolderPath;
        
-       public DownloadRemoteFileOperation(RemoteFile remoteFile, String temporalFolder) {
-               mRemoteFile = remoteFile;
-               mTemporalFolder = temporalFolder;
+       public DownloadRemoteFileOperation(String remotePath, String downloadFolderPath) {
+               mRemotePath = remotePath;
+               mDownloadFolderPath = downloadFolderPath;
        }
 
        @Override
@@ -78,11 +76,11 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
                tmpFile.getParentFile().mkdirs();
                int status = downloadFile(client, tmpFile);
                result = new RemoteOperationResult(isSuccess(status), status, (mGet != null ? mGet.getResponseHeaders() : null));
-               Log.i(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage());
+               Log.i(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + result.getLogMessage());
 
         } catch (Exception e) {
             result = new RemoteOperationResult(e);
-            Log.e(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage(), e);
+            Log.e(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + result.getLogMessage(), e);
         }
         
         return result;
@@ -92,7 +90,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
     protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
         int status = -1;
         boolean savedFile = false;
-        mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemoteFile.getRemotePath()));
+        mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
         Iterator<OnDatatransferProgressListener> it = null;
         
         FileOutputStream fos = null;
@@ -103,6 +101,9 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
                 BufferedInputStream bis = new BufferedInputStream(mGet.getResponseBodyAsStream());
                 fos = new FileOutputStream(targetFile);
                 long transferred = 0;
+                
+                Header contentLength = mGet.getResponseHeader("Content-Length");
+                long totalToTransfer = (contentLength != null && contentLength.getValue().length() >0) ? Long.parseLong(contentLength.getValue()) : 0;
 
                 byte[] bytes = new byte[4096];
                 int readResult = 0;
@@ -118,16 +119,18 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
                     synchronized (mDataTransferListeners) {
                         it = mDataTransferListeners.iterator();
                         while (it.hasNext()) {
-                            it.next().onTransferProgress(readResult, transferred, mRemoteFile.getLength(), targetFile.getName());
+                            it.next().onTransferProgress(readResult, transferred, totalToTransfer, targetFile.getName());
                         }
                     }
                 }
                 savedFile = true;
+                /*
                 Header modificationTime = mGet.getResponseHeader("Last-Modified");
                 if (modificationTime != null) {
                     Date d = WebdavUtils.parseResponseDate((String) modificationTime.getValue());
                     mModificationTimestamp = (d != null) ? d.getTime() : 0;
                 }
+                */
                 
             } else {
                 client.exhaustResponse(mGet.getResponseBodyAsStream());
@@ -148,7 +151,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
     }
     
     private String getTmpPath() {
-        return mTemporalFolder + mRemoteFile.getRemotePath();
+        return mDownloadFolderPath + mRemotePath;
     }
     
     public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
index 8d89f63..d9be6f0 100644 (file)
@@ -25,7 +25,6 @@ import java.util.Set;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
-import com.owncloud.android.oc_framework.operations.RemoteFile;
 import com.owncloud.android.oc_framework.operations.RemoteOperation;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.remote.DownloadRemoteFileOperation;
@@ -128,10 +127,9 @@ public class DownloadFileOperation extends RemoteOperation {
         File tmpFile = new File(getTmpPath());
         
         String tmpFolder =  getTmpFolder();
-        RemoteFile remoteFile = FileStorageUtils.fillRemoteFile(mFile);
         
         /// perform the download
-        mDownloadOperation = new DownloadRemoteFileOperation(remoteFile, tmpFolder);
+        mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
         Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
         while (listener.hasNext()) {
             mDownloadOperation.addDatatransferProgressListener(listener.next());