1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
21 import java
.util
.HashSet
;
22 import java
.util
.Iterator
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.lib
.network
.OnDatatransferProgressListener
;
27 import com
.owncloud
.android
.lib
.network
.OwnCloudClient
;
28 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
29 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
30 import com
.owncloud
.android
.lib
.operations
.remote
.DownloadRemoteFileOperation
;
31 import com
.owncloud
.android
.utils
.FileStorageUtils
;
32 import com
.owncloud
.android
.utils
.Log_OC
;
34 import android
.accounts
.Account
;
35 import android
.webkit
.MimeTypeMap
;
38 * Remote mDownloadOperation performing the download of a file to an ownCloud server
40 * @author David A. Velasco
43 public class DownloadFileOperation
extends RemoteOperation
{
45 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
47 private Account mAccount
;
49 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
50 private long mModificationTimestamp
= 0;
52 private DownloadRemoteFileOperation mDownloadOperation
;
55 public DownloadFileOperation(Account account
, OCFile file
) {
57 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
59 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
67 public Account
getAccount() {
71 public OCFile
getFile() {
75 public String
getSavePath() {
76 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
77 if (path
!= null
&& path
.length() > 0) {
80 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
83 public String
getTmpPath() {
84 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
87 public String
getTmpFolder() {
88 return FileStorageUtils
.getTemporalPath(mAccount
.name
);
91 public String
getRemotePath() {
92 return mFile
.getRemotePath();
95 public String
getMimeType() {
96 String mimeType
= mFile
.getMimetype();
97 if (mimeType
== null
|| mimeType
.length() <= 0) {
99 mimeType
= MimeTypeMap
.getSingleton()
100 .getMimeTypeFromExtension(
101 mFile
.getRemotePath().substring(mFile
.getRemotePath().lastIndexOf('.') + 1));
102 } catch (IndexOutOfBoundsException e
) {
103 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mFile
.getRemotePath());
106 if (mimeType
== null
) {
107 mimeType
= "application/octet-stream";
112 public long getSize() {
113 return mFile
.getFileLength();
116 public long getModificationTimestamp() {
117 return (mModificationTimestamp
> 0) ? mModificationTimestamp
: mFile
.getModificationTimestamp();
121 protected RemoteOperationResult
run(OwnCloudClient client
) {
122 RemoteOperationResult result
= null
;
124 boolean moved
= true
;
126 /// download will be performed to a temporal file, then moved to the final location
127 File tmpFile
= new File(getTmpPath());
129 String tmpFolder
= getTmpFolder();
131 /// perform the download
132 mDownloadOperation
= new DownloadRemoteFileOperation(mFile
.getRemotePath(), tmpFolder
);
133 Iterator
<OnDatatransferProgressListener
> listener
= mDataTransferListeners
.iterator();
134 while (listener
.hasNext()) {
135 mDownloadOperation
.addDatatransferProgressListener(listener
.next());
137 result
= mDownloadOperation
.execute(client
);
139 if (result
.isSuccess()) {
140 mModificationTimestamp
= mDownloadOperation
.getModificationTimestamp();
141 newFile
= new File(getSavePath());
142 newFile
.getParentFile().mkdirs();
143 moved
= tmpFile
.renameTo(newFile
);
146 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
148 Log_OC
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
154 public void cancel() {
155 mDownloadOperation
.cancel();
159 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
160 synchronized (mDataTransferListeners
) {
161 mDataTransferListeners
.add(listener
);
165 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
166 synchronized (mDataTransferListeners
) {
167 mDataTransferListeners
.remove(listener
);