a9f89d41fa50f7aaba493c4fdf8a994284c3a77d
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / common / SyncOperation.java
1 /* ownCloud Android client application
2 *
3 * @author David A. Velasco
4 * Copyright (C) 2012-2014 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.operations.common;
21
22 import com.owncloud.android.datamodel.FileDataStorageManager;
23 import com.owncloud.android.lib.common.OwnCloudClient;
24 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
25 import com.owncloud.android.lib.common.operations.RemoteOperation;
26 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
27
28 import android.content.Context;
29 import android.os.Handler;
30
31
32 /**
33 * Operation which execution involves both interactions with an ownCloud server and
34 * with local data in the device.
35 *
36 * Provides methods to execute the operation both synchronously or asynchronously.
37 */
38 public abstract class SyncOperation extends RemoteOperation {
39
40 //private static final String TAG = SyncOperation.class.getSimpleName();
41
42 private FileDataStorageManager mStorageManager;
43
44 public FileDataStorageManager getStorageManager() {
45 return mStorageManager;
46 }
47
48
49 /**
50 * Synchronously executes the operation on the received ownCloud account.
51 *
52 * Do not call this method from the main thread.
53 *
54 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
55 *
56 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
57 * @param context Android context for the component calling the method.
58 * @return Result of the operation.
59 */
60 public RemoteOperationResult execute(FileDataStorageManager storageManager, Context context) {
61 if (storageManager == null) {
62 throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
63 }
64 if (storageManager.getAccount() == null) {
65 throw new IllegalArgumentException("Trying to execute a sync operation with a storage manager for a NULL account");
66 }
67 mStorageManager = storageManager;
68 return super.execute(mStorageManager.getAccount(), context);
69 }
70
71
72 /**
73 * Synchronously executes the remote operation
74 *
75 * Do not call this method from the main thread.
76 *
77 * @param client Client object to reach an ownCloud server during the execution of the operation.
78 * @return Result of the operation.
79 */
80 public RemoteOperationResult execute(OwnCloudClient client, FileDataStorageManager storageManager) {
81 if (storageManager == null)
82 throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
83 mStorageManager = storageManager;
84 return super.execute(client);
85 }
86
87
88 /**
89 * Asynchronously executes the remote operation
90 *
91 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
92 *
93 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
94 * @param context Android context for the component calling the method.
95 * @param listener Listener to be notified about the execution of the operation.
96 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
97 * @return Thread were the remote operation is executed.
98 */
99 /*
100 public Thread execute(FileDataStorageManager storageManager, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) {
101 if (storageManager == null) {
102 throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
103 }
104 if (storageManager.getAccount() == null) {
105 throw new IllegalArgumentException("Trying to execute a sync operation with a storage manager for a NULL account");
106 }
107 mStorageManager = storageManager;
108 return super.execute(storageManager.getAccount(), context, listener, listenerHandler, callerActivity);
109 }
110 */
111
112
113 /**
114 * Asynchronously executes the remote operation
115 *
116 * @param client Client object to reach an ownCloud server during the execution of the operation.
117 * @param listener Listener to be notified about the execution of the operation.
118 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
119 * @return Thread were the remote operation is executed.
120 */
121 public Thread execute(OwnCloudClient client, FileDataStorageManager storageManager, OnRemoteOperationListener listener, Handler listenerHandler) {
122 if (storageManager == null) {
123 throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
124 }
125 mStorageManager = storageManager;
126 return super.execute(client, listener, listenerHandler);
127 }
128
129
130 }