SynchronizeFileOperation.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author masensio
  6. * Copyright (C) 2012 Bartek Przybylski
  7. * Copyright (C) 2015 ownCloud Inc.
  8. *
  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.
  12. *
  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.
  17. *
  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/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import android.accounts.Account;
  23. import android.content.Context;
  24. import android.content.Intent;
  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.common.operations.RemoteOperationResult;
  30. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  31. import com.owncloud.android.lib.common.utils.Log_OC;
  32. import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
  33. import com.owncloud.android.lib.resources.files.RemoteFile;
  34. import com.owncloud.android.operations.common.SyncOperation;
  35. import com.owncloud.android.utils.FileStorageUtils;
  36. /**
  37. * Remote operation performing the read of remote file in the ownCloud server.
  38. */
  39. public class SynchronizeFileOperation extends SyncOperation {
  40. private static final String TAG = SynchronizeFileOperation.class.getSimpleName();
  41. private OCFile mLocalFile;
  42. private String mRemotePath;
  43. private OCFile mServerFile;
  44. private Account mAccount;
  45. private boolean mSyncFileContents;
  46. private Context mContext;
  47. private boolean mTransferWasRequested = false;
  48. /**
  49. * When 'false', uploads to the server are not done; only downloads or conflict detection.
  50. * This is a temporal field.
  51. * TODO Remove when 'folder synchronization' replaces 'folder download'.
  52. */
  53. private boolean mAllowUploads;
  54. /**
  55. * Constructor for "full synchronization mode".
  56. * <p/>
  57. * Uses remotePath to retrieve all the data both in local cache and in the remote OC server
  58. * when the operation is executed, instead of reusing {@link OCFile} instances.
  59. * <p/>
  60. * Useful for direct synchronization of a single file.
  61. *
  62. * @param remotePath remote path of the file
  63. * @param account ownCloud account holding the file.
  64. * @param syncFileContents When 'true', transference of data will be started by the
  65. * operation if needed and no conflict is detected.
  66. * @param context Android context; needed to start transfers.
  67. */
  68. public SynchronizeFileOperation(
  69. String remotePath,
  70. Account account,
  71. boolean syncFileContents,
  72. Context context) {
  73. mRemotePath = remotePath;
  74. mLocalFile = null;
  75. mServerFile = null;
  76. mAccount = account;
  77. mSyncFileContents = syncFileContents;
  78. mContext = context;
  79. mAllowUploads = true;
  80. }
  81. /**
  82. * Constructor allowing to reuse {@link OCFile} instances just queried from local cache or
  83. * from remote OC server.
  84. *
  85. * Useful to include this operation as part of the synchronization of a folder
  86. * (or a full account), avoiding the repetition of fetch operations (both in local database
  87. * or remote server).
  88. *
  89. * At least one of localFile or serverFile MUST NOT BE NULL. If you don't have none of them,
  90. * use the other constructor.
  91. *
  92. * @param localFile Data of file (just) retrieved from local cache/database.
  93. * @param serverFile Data of file (just) retrieved from a remote server. If null,
  94. * will be retrieved from network by the operation when executed.
  95. * @param account ownCloud account holding the file.
  96. * @param syncFileContents When 'true', transference of data will be started by the
  97. * operation if needed and no conflict is detected.
  98. * @param context Android context; needed to start transfers.
  99. */
  100. public SynchronizeFileOperation(
  101. OCFile localFile,
  102. OCFile serverFile,
  103. Account account,
  104. boolean syncFileContents,
  105. Context context) {
  106. mLocalFile = localFile;
  107. mServerFile = serverFile;
  108. if (mLocalFile != null) {
  109. mRemotePath = mLocalFile.getRemotePath();
  110. if (mServerFile != null && !mServerFile.getRemotePath().equals(mRemotePath)) {
  111. throw new IllegalArgumentException("serverFile and localFile do not correspond" +
  112. " to the same OC file");
  113. }
  114. } else if (mServerFile != null) {
  115. mRemotePath = mServerFile.getRemotePath();
  116. } else {
  117. throw new IllegalArgumentException("Both serverFile and localFile are NULL");
  118. }
  119. mAccount = account;
  120. mSyncFileContents = syncFileContents;
  121. mContext = context;
  122. mAllowUploads = true;
  123. }
  124. /**
  125. * Temporal constructor.
  126. *
  127. * Extends the previous one to allow constrained synchronizations where uploads are never
  128. * performed - only downloads or conflict detection.
  129. *
  130. * Do not use unless you are involved in 'folder synchronization' or 'folder download' work
  131. * in progress.
  132. *
  133. * TODO Remove when 'folder synchronization' replaces 'folder download'.
  134. *
  135. * @param localFile Data of file (just) retrieved from local cache/database.
  136. * MUSTN't be null.
  137. * @param serverFile Data of file (just) retrieved from a remote server.
  138. * If null, will be retrieved from network by the operation
  139. * when executed.
  140. * @param account ownCloud account holding the file.
  141. * @param syncFileContents When 'true', transference of data will be started by the
  142. * operation if needed and no conflict is detected.
  143. * @param allowUploads When 'false', uploads to the server are not done;
  144. * only downloads or conflict detection.
  145. * @param context Android context; needed to start transfers.
  146. */
  147. public SynchronizeFileOperation(
  148. OCFile localFile,
  149. OCFile serverFile,
  150. Account account,
  151. boolean syncFileContents,
  152. boolean allowUploads,
  153. Context context) {
  154. this(localFile, serverFile, account, syncFileContents, context);
  155. mAllowUploads = allowUploads;
  156. }
  157. @Override
  158. protected RemoteOperationResult run(OwnCloudClient client) {
  159. RemoteOperationResult result = null;
  160. mTransferWasRequested = false;
  161. if (mLocalFile == null) {
  162. // Get local file from the DB
  163. mLocalFile = getStorageManager().getFileByPath(mRemotePath);
  164. }
  165. if (!mLocalFile.isDown()) {
  166. /// easy decision
  167. requestForDownload(mLocalFile);
  168. result = new RemoteOperationResult(ResultCode.OK);
  169. } else {
  170. /// local copy in the device -> need to think a bit more before do anything
  171. if (mServerFile == null) {
  172. ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
  173. result = operation.execute(client);
  174. if (result.isSuccess()) {
  175. mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
  176. mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
  177. }
  178. }
  179. if (mServerFile != null) {
  180. /// check changes in server and local file
  181. boolean serverChanged;
  182. if (mLocalFile.getEtag() == null || mLocalFile.getEtag().length() == 0) {
  183. // file uploaded (null) or downloaded ("") before upgrade to version 1.8.0; check the old condition
  184. serverChanged = mServerFile.getModificationTimestamp() !=
  185. mLocalFile.getModificationTimestampAtLastSyncForData();
  186. } else {
  187. serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag()));
  188. }
  189. boolean localChanged = (
  190. mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData()
  191. );
  192. /// decide action to perform depending upon changes
  193. //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
  194. if (localChanged && serverChanged) {
  195. result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
  196. getStorageManager().saveConflict(mLocalFile, mServerFile.getEtag());
  197. } else if (localChanged) {
  198. if (mSyncFileContents && mAllowUploads) {
  199. requestForUpload(mLocalFile);
  200. // the local update of file properties will be done by the FileUploader
  201. // service when the upload finishes
  202. } else {
  203. // NOTHING TO DO HERE: updating the properties of the file in the server
  204. // without uploading the contents would be stupid;
  205. // So, an instance of SynchronizeFileOperation created with
  206. // syncFileContents == false is completely useless when we suspect
  207. // that an upload is necessary (for instance, in FileObserverService).
  208. Log_OC.d(TAG, "Nothing to do here");
  209. }
  210. result = new RemoteOperationResult(ResultCode.OK);
  211. } else if (serverChanged) {
  212. mLocalFile.setRemoteId(mServerFile.getRemoteId());
  213. if (mSyncFileContents) {
  214. requestForDownload(mLocalFile); // local, not server; we won't to keep
  215. // the value of favorite!
  216. // the update of local data will be done later by the FileUploader
  217. // service when the upload finishes
  218. } else {
  219. // TODO CHECK: is this really useful in some point in the code?
  220. mServerFile.setFavorite(mLocalFile.isFavorite());
  221. mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
  222. mServerFile.setStoragePath(mLocalFile.getStoragePath());
  223. mServerFile.setParentId(mLocalFile.getParentId());
  224. mServerFile.setEtag(mLocalFile.getEtag());
  225. getStorageManager().saveFile(mServerFile);
  226. }
  227. result = new RemoteOperationResult(ResultCode.OK);
  228. } else {
  229. // nothing changed, nothing to do
  230. result = new RemoteOperationResult(ResultCode.OK);
  231. }
  232. // safe blanket: sync'ing a not in-conflict file will clean wrong conflict markers in ancestors
  233. if (result.getCode() != ResultCode.SYNC_CONFLICT) {
  234. getStorageManager().saveConflict(mLocalFile, null);
  235. }
  236. }
  237. }
  238. Log_OC.i(TAG, "Synchronizing " + mAccount.name + ", file " + mLocalFile.getRemotePath() +
  239. ": " + result.getLogMessage());
  240. return result;
  241. }
  242. /**
  243. * Requests for an upload to the FileUploader service
  244. *
  245. * @param file OCFile object representing the file to upload
  246. */
  247. private void requestForUpload(OCFile file) {
  248. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  249. requester.uploadUpdate(mContext, mAccount, file, FileUploader.LOCAL_BEHAVIOUR_MOVE, true);
  250. mTransferWasRequested = true;
  251. }
  252. /**
  253. * Requests for a download to the FileDownloader service
  254. *
  255. * @param file OCFile object representing the file to download
  256. */
  257. private void requestForDownload(OCFile file) {
  258. Intent i = new Intent(mContext, FileDownloader.class);
  259. i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
  260. i.putExtra(FileDownloader.EXTRA_FILE, file);
  261. mContext.startService(i);
  262. mTransferWasRequested = true;
  263. }
  264. public boolean transferWasRequested() {
  265. return mTransferWasRequested;
  266. }
  267. public OCFile getLocalFile() {
  268. return mLocalFile;
  269. }
  270. }