X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/c86d4d63cc4a00ebecb712da671e8f876ffcfeaa..00482b6dcb1e98da39df54374f7098b51d66bee5:/src/com/owncloud/android/operations/DownloadFileOperation.java?ds=inline diff --git a/src/com/owncloud/android/operations/DownloadFileOperation.java b/src/com/owncloud/android/operations/DownloadFileOperation.java index 04011c10..0cb303ce 100644 --- a/src/com/owncloud/android/operations/DownloadFileOperation.java +++ b/src/com/owncloud/android/operations/DownloadFileOperation.java @@ -1,5 +1,9 @@ -/* ownCloud Android client application - * Copyright (C) 2012-2013 ownCloud Inc. +/** + * ownCloud Android client application + * + * @author David A. Velasco + * @author masensio + * Copyright (C) 2015 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -21,24 +25,24 @@ import java.io.File; import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import com.owncloud.android.MainApp; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.lib.network.OnDatatransferProgressListener; -import com.owncloud.android.lib.network.OwnCloudClient; -import com.owncloud.android.lib.operations.common.RemoteOperation; -import com.owncloud.android.lib.operations.common.RemoteOperationResult; -import com.owncloud.android.lib.operations.remote.DownloadRemoteFileOperation; +import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.operations.OperationCancelledException; +import com.owncloud.android.lib.common.operations.RemoteOperation; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.utils.FileStorageUtils; -import com.owncloud.android.utils.Log_OC; import android.accounts.Account; import android.webkit.MimeTypeMap; /** * Remote mDownloadOperation performing the download of a file to an ownCloud server - * - * @author David A. Velasco - * @author masensio */ public class DownloadFileOperation extends RemoteOperation { @@ -48,15 +52,18 @@ public class DownloadFileOperation extends RemoteOperation { private OCFile mFile; private Set mDataTransferListeners = new HashSet(); private long mModificationTimestamp = 0; + private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false); private DownloadRemoteFileOperation mDownloadOperation; public DownloadFileOperation(Account account, OCFile file) { if (account == null) - throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation"); + throw new IllegalArgumentException("Illegal null account in DownloadFileOperation " + + "creation"); if (file == null) - throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation"); + throw new IllegalArgumentException("Illegal null file in DownloadFileOperation " + + "creation"); mAccount = account; mFile = file; @@ -73,7 +80,7 @@ public class DownloadFileOperation extends RemoteOperation { } public String getSavePath() { - String path = mFile.getStoragePath(); // re-downloads should be done over the original file + String path = mFile.getStoragePath(); // re-downloads should be done over the original file if (path != null && path.length() > 0) { return path; } @@ -98,9 +105,11 @@ public class DownloadFileOperation extends RemoteOperation { try { mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension( - mFile.getRemotePath().substring(mFile.getRemotePath().lastIndexOf('.') + 1)); + mFile.getRemotePath().substring( + mFile.getRemotePath().lastIndexOf('.') + 1)); } catch (IndexOutOfBoundsException e) { - Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + mFile.getRemotePath()); + Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + + mFile.getRemotePath()); } } if (mimeType == null) { @@ -114,7 +123,8 @@ public class DownloadFileOperation extends RemoteOperation { } public long getModificationTimestamp() { - return (mModificationTimestamp > 0) ? mModificationTimestamp : mFile.getModificationTimestamp(); + return (mModificationTimestamp > 0) ? mModificationTimestamp : + mFile.getModificationTimestamp(); } @Override @@ -129,6 +139,12 @@ public class DownloadFileOperation extends RemoteOperation { String tmpFolder = getTmpFolder(); /// perform the download + synchronized(mCancellationRequested) { + if (mCancellationRequested.get()) { + return new RemoteOperationResult(new OperationCancelledException()); + } + } + mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder); Iterator listener = mDataTransferListeners.iterator(); while (listener.hasNext()) { @@ -141,18 +157,21 @@ public class DownloadFileOperation extends RemoteOperation { newFile = new File(getSavePath()); newFile.getParentFile().mkdirs(); moved = tmpFile.renameTo(newFile); - if (!moved) - result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED); + result = new RemoteOperationResult( + RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED); } - Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage()); - + Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + + result.getLogMessage()); return result; } public void cancel() { - mDownloadOperation.cancel(); + mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it + if (mDownloadOperation != null) { + mDownloadOperation.cancel(); + } } @@ -167,5 +186,4 @@ public class DownloadFileOperation extends RemoteOperation { mDataTransferListeners.remove(listener); } } - }