2 * ownCloud Android client application
4 * @author David A. Velasco
6 * Copyright (C) 2012 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com
.owncloud
.android
.operations
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.files
.services
.FileDownloader
;
27 import com
.owncloud
.android
.files
.services
.FileUploader
;
28 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
29 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
32 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
33 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
34 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
35 import com
.owncloud
.android
.utils
.FileStorageUtils
;
37 import android
.accounts
.Account
;
38 import android
.content
.Context
;
39 import android
.content
.Intent
;
42 * Remote operation performing the read of remote file in the ownCloud server.
45 public class SynchronizeFileOperation
extends SyncOperation
{
47 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
49 private OCFile mLocalFile
;
50 private String mRemotePath
;
51 private OCFile mServerFile
;
52 private Account mAccount
;
53 private boolean mSyncFileContents
;
54 private Context mContext
;
56 private boolean mTransferWasRequested
= false
;
59 * When 'false', uploads to the server are not done; only downloads or conflict detection.
60 * This is a temporal field.
61 * TODO Remove when 'folder synchronization' replaces 'folder download'.
63 private boolean mAllowUploads
;
67 * Constructor for "full synchronization mode".
69 * Uses remotePath to retrieve all the data both in local cache and in the remote OC server when the operation
70 * is executed, instead of reusing {@link OCFile} instances.
72 * Useful for direct synchronization of a single file.
75 * @param account ownCloud account holding the file.
76 * @param syncFileContents When 'true', transference of data will be started by the
77 * operation if needed and no conflict is detected.
78 * @param context Android context; needed to start transfers.
80 public SynchronizeFileOperation(
83 boolean syncFileContents
,
86 mRemotePath
= remotePath
;
90 mSyncFileContents
= syncFileContents
;
97 * Constructor allowing to reuse {@link OCFile} instances just queried from local cache or from remote OC server.
99 * Useful to include this operation as part of the synchronization of a folder (or a full account), avoiding the
100 * repetition of fetch operations (both in local database or remote server).
102 * At least one of localFile or serverFile MUST NOT BE NULL. If you don't have none of them, use the other
105 * @param localFile Data of file (just) retrieved from local cache/database.
106 * @param serverFile Data of file (just) retrieved from a remote server. If null, will be
107 * retrieved from network by the operation when executed.
108 * @param account ownCloud account holding the file.
109 * @param syncFileContents When 'true', transference of data will be started by the
110 * operation if needed and no conflict is detected.
111 * @param context Android context; needed to start transfers.
113 public SynchronizeFileOperation(
117 boolean syncFileContents
,
120 mLocalFile
= localFile
;
121 mServerFile
= serverFile
;
122 if (mLocalFile
!= null
) {
123 mRemotePath
= mLocalFile
.getRemotePath();
124 if (mServerFile
!= null
&& !mServerFile
.getRemotePath().equals(mRemotePath
)) {
125 throw new IllegalArgumentException("serverFile and localFile do not correspond to the same OC file");
127 } else if (mServerFile
!= null
) {
128 mRemotePath
= mServerFile
.getRemotePath();
130 throw new IllegalArgumentException("Both serverFile and localFile are NULL");
133 mSyncFileContents
= syncFileContents
;
135 mAllowUploads
= true
;
140 * Temporal constructor.
142 * Extends the previous one to allow constrained synchronizations where uploads are never performed - only
143 * downloads or conflict detection.
145 * Do not use unless you are involved in 'folder synchronization' or 'folder download' work in progress.
147 * TODO Remove when 'folder synchronization' replaces 'folder download'.
149 * @param localFile Data of file (just) retrieved from local cache/database. MUSTN't be null.
150 * @param serverFile Data of file (just) retrieved from a remote server. If null, will be
151 * retrieved from network by the operation when executed.
152 * @param account ownCloud account holding the file.
153 * @param syncFileContents When 'true', transference of data will be started by the
154 * operation if needed and no conflict is detected.
155 * @param allowUploads When 'false', uploads to the server are not done; only downloads or conflict
157 * @param context Android context; needed to start transfers.
159 public SynchronizeFileOperation(
163 boolean syncFileContents
,
164 boolean allowUploads
,
167 this(localFile
, serverFile
, account
, syncFileContents
, context
);
168 mAllowUploads
= allowUploads
;
173 protected RemoteOperationResult
run(OwnCloudClient client
) {
175 RemoteOperationResult result
= null
;
176 mTransferWasRequested
= false
;
178 if (mLocalFile
== null
) {
179 // Get local file from the DB
180 mLocalFile
= getStorageManager().getFileByPath(mRemotePath
);
183 if (!mLocalFile
.isDown()) {
185 requestForDownload(mLocalFile
);
186 result
= new RemoteOperationResult(ResultCode
.OK
);
189 /// local copy in the device -> need to think a bit more before do anything
191 if (mServerFile
== null
) {
192 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(mRemotePath
);
193 result
= operation
.execute(client
);
194 if (result
.isSuccess()){
195 mServerFile
= FileStorageUtils
.fillOCFile((RemoteFile
) result
.getData().get(0));
196 mServerFile
.setLastSyncDateForProperties(System
.currentTimeMillis());
200 if (mServerFile
!= null
) {
202 /// check changes in server and local file
203 boolean serverChanged
= false
;
204 /* time for eTag is coming, but not yet
205 if (mServerFile.getEtag() != null) {
206 serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag()));
209 mServerFile
.getModificationTimestamp() != mLocalFile
.getModificationTimestampAtLastSyncForData()
212 boolean localChanged
= (
213 mLocalFile
.getLocalModificationTimestamp() > mLocalFile
.getLastSyncDateForData()
216 /// decide action to perform depending upon changes
217 //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
218 if (localChanged
&& serverChanged
) {
219 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
221 } else if (localChanged
) {
222 if (mSyncFileContents
&& mAllowUploads
) {
223 requestForUpload(mLocalFile
);
224 // the local update of file properties will be done by the FileUploader service when the upload finishes
226 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
227 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
228 // that an upload is necessary (for instance, in FileObserverService).
230 result
= new RemoteOperationResult(ResultCode
.OK
);
232 } else if (serverChanged
) {
233 mLocalFile
.setRemoteId(mServerFile
.getRemoteId());
235 if (mSyncFileContents
) {
236 requestForDownload(mLocalFile
); // local, not server; we won't to keep the value of keepInSync!
237 // the update of local data will be done later by the FileUploader service when the upload finishes
239 // TODO CHECK: is this really useful in some point in the code?
240 mServerFile
.setKeepInSync(mLocalFile
.keepInSync());
241 mServerFile
.setLastSyncDateForData(mLocalFile
.getLastSyncDateForData());
242 mServerFile
.setStoragePath(mLocalFile
.getStoragePath());
243 mServerFile
.setParentId(mLocalFile
.getParentId());
244 getStorageManager().saveFile(mServerFile
);
247 result
= new RemoteOperationResult(ResultCode
.OK
);
250 // nothing changed, nothing to do
251 result
= new RemoteOperationResult(ResultCode
.OK
);
258 Log_OC
.i(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + mLocalFile
.getRemotePath() + ": "
259 + result
.getLogMessage());
266 * Requests for an upload to the FileUploader service
268 * @param file OCFile object representing the file to upload
270 private void requestForUpload(OCFile file
) {
271 Intent i
= new Intent(mContext
, FileUploader
.class);
272 i
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
273 i
.putExtra(FileUploader
.KEY_FILE
, file
);
274 /*i.putExtra(FileUploader.KEY_REMOTE_FILE, mRemotePath); // doing this we would lose the value of keepInSync in the road, and maybe it's not updated in the database when the FileUploader service gets it!
275 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
276 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
277 i
.putExtra(FileUploader
.KEY_FORCE_OVERWRITE
, true
);
278 mContext
.startService(i
);
279 mTransferWasRequested
= true
;
284 * Requests for a download to the FileDownloader service
286 * @param file OCFile object representing the file to download
288 private void requestForDownload(OCFile file
) {
289 Intent i
= new Intent(mContext
, FileDownloader
.class);
290 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
291 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
292 mContext
.startService(i
);
293 mTransferWasRequested
= true
;
297 public boolean transferWasRequested() {
298 return mTransferWasRequested
;
302 public OCFile
getLocalFile() {