SyncOperation.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2014 ownCloud Inc.
  3. *
  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.
  7. *
  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.
  12. *
  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/>.
  15. *
  16. */
  17. package com.owncloud.android.operations.common;
  18. import com.owncloud.android.datamodel.FileDataStorageManager;
  19. import com.owncloud.android.lib.common.OwnCloudClient;
  20. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  21. import com.owncloud.android.lib.common.operations.RemoteOperation;
  22. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  23. import android.content.Context;
  24. import android.os.Handler;
  25. /**
  26. * Operation which execution involves both interactions with an ownCloud server and
  27. * with local data in the device.
  28. *
  29. * Provides methods to execute the operation both synchronously or asynchronously.
  30. *
  31. * @author David A. Velasco
  32. */
  33. public abstract class SyncOperation extends RemoteOperation {
  34. //private static final String TAG = SyncOperation.class.getSimpleName();
  35. private FileDataStorageManager mStorageManager;
  36. public FileDataStorageManager getStorageManager() {
  37. return mStorageManager;
  38. }
  39. /**
  40. * Synchronously executes the operation on the received ownCloud account.
  41. *
  42. * Do not call this method from the main thread.
  43. *
  44. * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
  45. *
  46. * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
  47. * @param context Android context for the component calling the method.
  48. * @return Result of the operation.
  49. */
  50. public RemoteOperationResult execute(FileDataStorageManager storageManager, Context context) {
  51. if (storageManager == null) {
  52. throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
  53. }
  54. if (storageManager.getAccount() == null) {
  55. throw new IllegalArgumentException("Trying to execute a sync operation with a storage manager for a NULL account");
  56. }
  57. mStorageManager = storageManager;
  58. return super.execute(mStorageManager.getAccount(), context);
  59. }
  60. /**
  61. * Synchronously executes the remote operation
  62. *
  63. * Do not call this method from the main thread.
  64. *
  65. * @param client Client object to reach an ownCloud server during the execution of the operation.
  66. * @return Result of the operation.
  67. */
  68. public RemoteOperationResult execute(OwnCloudClient client, FileDataStorageManager storageManager) {
  69. if (storageManager == null)
  70. throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
  71. mStorageManager = storageManager;
  72. return super.execute(client);
  73. }
  74. /**
  75. * Asynchronously executes the remote operation
  76. *
  77. * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
  78. *
  79. * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
  80. * @param context Android context for the component calling the method.
  81. * @param listener Listener to be notified about the execution of the operation.
  82. * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
  83. * @return Thread were the remote operation is executed.
  84. */
  85. /*
  86. public Thread execute(FileDataStorageManager storageManager, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) {
  87. if (storageManager == null) {
  88. throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
  89. }
  90. if (storageManager.getAccount() == null) {
  91. throw new IllegalArgumentException("Trying to execute a sync operation with a storage manager for a NULL account");
  92. }
  93. mStorageManager = storageManager;
  94. return super.execute(storageManager.getAccount(), context, listener, listenerHandler, callerActivity);
  95. }
  96. */
  97. /**
  98. * Asynchronously executes the remote operation
  99. *
  100. * @param client Client object to reach an ownCloud server during the execution of the operation.
  101. * @param listener Listener to be notified about the execution of the operation.
  102. * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
  103. * @return Thread were the remote operation is executed.
  104. */
  105. public Thread execute(OwnCloudClient client, FileDataStorageManager storageManager, OnRemoteOperationListener listener, Handler listenerHandler) {
  106. if (storageManager == null) {
  107. throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
  108. }
  109. mStorageManager = storageManager;
  110. return super.execute(client, listener, listenerHandler);
  111. }
  112. }