SyncOperation.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.operations.common;
  21. import android.content.Context;
  22. import android.os.Handler;
  23. import com.nextcloud.common.NextcloudClient;
  24. import com.owncloud.android.datamodel.FileDataStorageManager;
  25. import com.owncloud.android.lib.common.OwnCloudClient;
  26. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  27. import com.owncloud.android.lib.common.operations.RemoteOperation;
  28. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  29. import androidx.annotation.NonNull;
  30. /**
  31. * Operation which execution involves both interactions with an ownCloud server and with local data in the device.
  32. * <p>
  33. * Provides methods to execute the operation both synchronously or asynchronously.
  34. */
  35. public abstract class SyncOperation extends RemoteOperation {
  36. private final FileDataStorageManager storageManager;
  37. public SyncOperation(@NonNull FileDataStorageManager storageManager) {
  38. this.storageManager = storageManager;
  39. }
  40. /**
  41. * Synchronously executes the operation on the received ownCloud account.
  42. * <p>
  43. * Do not call this method from the main thread.
  44. * <p>
  45. * This method should be used whenever an ownCloud account is available, instead of {@link
  46. * #execute(OwnCloudClient)}.
  47. *
  48. * @param context Android context for the component calling the method.
  49. * @return Result of the operation.
  50. */
  51. public RemoteOperationResult execute(Context context) {
  52. if (storageManager.getAccount() == null) {
  53. throw new IllegalArgumentException("Trying to execute a sync operation with a " +
  54. "storage manager for a NULL account");
  55. }
  56. return super.execute(this.storageManager.getAccount(), context);
  57. }
  58. public RemoteOperationResult execute(@NonNull NextcloudClient client) {
  59. return run(client);
  60. }
  61. /**
  62. * Asynchronously executes the remote operation
  63. *
  64. * @param client Client object to reach an ownCloud server during the
  65. * execution of the operation.
  66. * @param listener Listener to be notified about the execution of the operation.
  67. * @param listenerHandler Handler associated to the thread where the methods of
  68. * the listener objects must be called.
  69. * @return Thread were the remote operation is executed.
  70. */
  71. public Thread execute(OwnCloudClient client,
  72. OnRemoteOperationListener listener,
  73. Handler listenerHandler) {
  74. return super.execute(client, listener, listenerHandler);
  75. }
  76. public FileDataStorageManager getStorageManager() {
  77. return this.storageManager;
  78. }
  79. }