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
.MainApp
;
31 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
33 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
34 import com
.owncloud
.android
.lib
.common
.operations
.OperationCancelledException
;
35 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
36 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
37 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
38 import com
.owncloud
.android
.lib
.resources
.files
.DownloadRemoteFileOperation
;
39 import com
.owncloud
.android
.utils
.FileStorageUtils
;
41 import android
.accounts
.Account
;
42 import android
.webkit
.MimeTypeMap
;
45 * Remote mDownloadOperation performing the download of a file to an ownCloud server
47 public class DownloadFileOperation
extends RemoteOperation
{
49 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
51 private Account mAccount
;
53 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
54 private long mModificationTimestamp
= 0;
55 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
57 private DownloadRemoteFileOperation mDownloadOperation
;
60 public DownloadFileOperation(Account account
, OCFile file
) {
62 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation " +
65 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation " +
74 public Account
getAccount() {
78 public OCFile
getFile() {
82 public String
getSavePath() {
83 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
84 if (path
!= null
&& path
.length() > 0) {
87 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
90 public String
getTmpPath() {
91 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
94 public String
getTmpFolder() {
95 return FileStorageUtils
.getTemporalPath(mAccount
.name
);
98 public String
getRemotePath() {
99 return mFile
.getRemotePath();
102 public String
getMimeType() {
103 String mimeType
= mFile
.getMimetype();
104 if (mimeType
== null
|| mimeType
.length() <= 0) {
106 mimeType
= MimeTypeMap
.getSingleton()
107 .getMimeTypeFromExtension(
108 mFile
.getRemotePath().substring(
109 mFile
.getRemotePath().lastIndexOf('.') + 1));
110 } catch (IndexOutOfBoundsException e
) {
111 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " +
112 mFile
.getRemotePath());
115 if (mimeType
== null
) {
116 mimeType
= "application/octet-stream";
121 public long getSize() {
122 return mFile
.getFileLength();
125 public long getModificationTimestamp() {
126 return (mModificationTimestamp
> 0) ? mModificationTimestamp
:
127 mFile
.getModificationTimestamp();
131 protected RemoteOperationResult
run(OwnCloudClient client
) {
132 RemoteOperationResult result
= null
;
134 boolean moved
= true
;
136 /// download will be performed to a temporal file, then moved to the final location
137 File tmpFile
= new File(getTmpPath());
139 String tmpFolder
= getTmpFolder();
141 /// perform the download
142 synchronized(mCancellationRequested
) {
143 if (mCancellationRequested
.get()) {
144 return new RemoteOperationResult(new OperationCancelledException());
148 mDownloadOperation
= new DownloadRemoteFileOperation(mFile
.getRemotePath(), tmpFolder
);
149 Iterator
<OnDatatransferProgressListener
> listener
= mDataTransferListeners
.iterator();
150 while (listener
.hasNext()) {
151 mDownloadOperation
.addDatatransferProgressListener(listener
.next());
153 result
= mDownloadOperation
.execute(client
);
155 if (result
.isSuccess()) {
156 mModificationTimestamp
= mDownloadOperation
.getModificationTimestamp();
157 newFile
= new File(getSavePath());
158 newFile
.getParentFile().mkdirs();
159 moved
= tmpFile
.renameTo(newFile
);
161 result
= new RemoteOperationResult(
162 RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
164 Log_OC
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " +
165 result
.getLogMessage());
170 public void cancel() {
171 mCancellationRequested
.set(true
); // atomic set; there is no need of synchronizing it
172 if (mDownloadOperation
!= null
) {
173 mDownloadOperation
.cancel();
178 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
179 synchronized (mDataTransferListeners
) {
180 mDataTransferListeners
.add(listener
);
184 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
185 synchronized (mDataTransferListeners
) {
186 mDataTransferListeners
.remove(listener
);