From: David A. Velasco Date: Wed, 15 Jan 2014 10:48:10 +0000 (+0100) Subject: Fixed DownloadRemoteOperation to use real total transfer size and to simplify constructor X-Git-Tag: oc-android-1.5.5~69^2~15 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/af6815a14bc0b10392c861d418e9ec5d748e5fd6?ds=inline;hp=-c Fixed DownloadRemoteOperation to use real total transfer size and to simplify constructor --- af6815a14bc0b10392c861d418e9ec5d748e5fd6 diff --git a/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java b/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java index 2030a8f2..8270fad8 100644 --- a/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java +++ b/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java @@ -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; diff --git a/oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/MainActivity.java b/oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/MainActivity.java index 6f7d2168..ad2b4dd5 100644 --- a/oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/MainActivity.java +++ b/oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/MainActivity.java @@ -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); } diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/DownloadRemoteFileOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/DownloadRemoteFileOperation.java index e3e7ee07..5103c562 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/DownloadRemoteFileOperation.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/DownloadRemoteFileOperation.java @@ -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 mDataTransferListeners = new HashSet(); 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 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) { diff --git a/src/com/owncloud/android/operations/DownloadFileOperation.java b/src/com/owncloud/android/operations/DownloadFileOperation.java index 8d89f636..d9be6f04 100644 --- a/src/com/owncloud/android/operations/DownloadFileOperation.java +++ b/src/com/owncloud/android/operations/DownloadFileOperation.java @@ -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 listener = mDataTransferListeners.iterator(); while (listener.hasNext()) { mDownloadOperation.addDatatransferProgressListener(listener.next());