2 * ownCloud Android client application
4 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.operations
;
25 import java
.util
.HashSet
;
26 import java
.util
.Iterator
;
28 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
31 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
32 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
33 import com
.owncloud
.android
.lib
.common
.operations
.OperationCancelledException
;
34 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
35 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
36 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
37 import com
.owncloud
.android
.lib
.resources
.files
.DownloadRemoteFileOperation
;
38 import com
.owncloud
.android
.utils
.FileStorageUtils
;
40 import android
.accounts
.Account
;
41 import android
.webkit
.MimeTypeMap
;
44 * Remote mDownloadOperation performing the download of a file to an ownCloud server
46 public class DownloadFileOperation
extends RemoteOperation
{
48 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
50 private Account mAccount
;
52 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
53 private long mModificationTimestamp
= 0;
54 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
56 private DownloadRemoteFileOperation mDownloadOperation
;
59 public DownloadFileOperation(Account account
, OCFile file
) {
61 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
63 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
71 public Account
getAccount() {
75 public OCFile
getFile() {
79 public String
getSavePath() {
80 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
81 if (path
!= null
&& path
.length() > 0) {
84 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
87 public String
getTmpPath() {
88 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
91 public String
getTmpFolder() {
92 return FileStorageUtils
.getTemporalPath(mAccount
.name
);
95 public String
getRemotePath() {
96 return mFile
.getRemotePath();
99 public String
getMimeType() {
100 String mimeType
= mFile
.getMimetype();
101 if (mimeType
== null
|| mimeType
.length() <= 0) {
103 mimeType
= MimeTypeMap
.getSingleton()
104 .getMimeTypeFromExtension(
105 mFile
.getRemotePath().substring(mFile
.getRemotePath().lastIndexOf('.') + 1));
106 } catch (IndexOutOfBoundsException e
) {
107 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mFile
.getRemotePath());
110 if (mimeType
== null
) {
111 mimeType
= "application/octet-stream";
116 public long getSize() {
117 return mFile
.getFileLength();
120 public long getModificationTimestamp() {
121 return (mModificationTimestamp
> 0) ? mModificationTimestamp
: mFile
.getModificationTimestamp();
125 protected RemoteOperationResult
run(OwnCloudClient client
) {
126 RemoteOperationResult result
= null
;
128 boolean moved
= true
;
130 /// download will be performed to a temporal file, then moved to the final location
131 File tmpFile
= new File(getTmpPath());
133 String tmpFolder
= getTmpFolder();
135 /// perform the download
136 synchronized(mCancellationRequested
) {
137 if (mCancellationRequested
.get()) {
138 return new RemoteOperationResult(new OperationCancelledException());
142 mDownloadOperation
= new DownloadRemoteFileOperation(mFile
.getRemotePath(), tmpFolder
);
143 Iterator
<OnDatatransferProgressListener
> listener
= mDataTransferListeners
.iterator();
144 while (listener
.hasNext()) {
145 mDownloadOperation
.addDatatransferProgressListener(listener
.next());
147 result
= mDownloadOperation
.execute(client
);
149 if (result
.isSuccess()) {
150 mModificationTimestamp
= mDownloadOperation
.getModificationTimestamp();
151 newFile
= new File(getSavePath());
152 newFile
.getParentFile().mkdirs();
153 moved
= tmpFile
.renameTo(newFile
);
155 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
157 Log_OC
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
162 public void cancel() {
163 mCancellationRequested
.set(true
); // atomic set; there is no need of synchronizing it
164 if (mDownloadOperation
!= null
) {
165 mDownloadOperation
.cancel();
170 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
171 synchronized (mDataTransferListeners
) {
172 mDataTransferListeners
.add(listener
);
176 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
177 synchronized (mDataTransferListeners
) {
178 mDataTransferListeners
.remove(listener
);