Merge remote-tracking branch 'remotes/upstream/cancelUploadOnWlanExit' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / common / SyncOperation.java
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
21 package com.owncloud.android.operations.common;
22
23 import android.content.Context;
24 import android.os.Handler;
25
26 import com.owncloud.android.datamodel.FileDataStorageManager;
27 import com.owncloud.android.lib.common.OwnCloudClient;
28 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
29 import com.owncloud.android.lib.common.operations.RemoteOperation;
30 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
31
32
33 /**
34 * Operation which execution involves both interactions with an ownCloud server and
35 * with local data in the device.
36 *
37 * Provides methods to execute the operation both synchronously or asynchronously.
38 */
39 public abstract class SyncOperation extends RemoteOperation {
40
41 //private static final String TAG = SyncOperation.class.getSimpleName();
42
43 private FileDataStorageManager mStorageManager;
44
45 public FileDataStorageManager getStorageManager() {
46 return mStorageManager;
47 }
48
49
50 /**
51 * Synchronously executes the operation on the received ownCloud account.
52 *
53 * Do not call this method from the main thread.
54 *
55 * This method should be used whenever an ownCloud account is available, instead of
56 * {@link #execute(OwnCloudClient, com.owncloud.android.datamodel.FileDataStorageManager)}.
57 *
58 * @param storageManager
59 * @param context Android context for the component calling the method.
60 * @return Result of the operation.
61 */
62 public RemoteOperationResult execute(FileDataStorageManager storageManager, Context context) {
63 if (storageManager == null) {
64 throw new IllegalArgumentException("Trying to execute a sync operation with a " +
65 "NULL storage manager");
66 }
67 if (storageManager.getAccount() == null) {
68 throw new IllegalArgumentException("Trying to execute a sync operation with a " +
69 "storage manager for a NULL account");
70 }
71 mStorageManager = storageManager;
72 return super.execute(mStorageManager.getAccount(), context);
73 }
74
75
76 /**
77 * Synchronously executes the remote operation
78 *
79 * Do not call this method from the main thread.
80 *
81 * @param client Client object to reach an ownCloud server during the execution of the o
82 * peration.
83 * @param storageManager
84 * @return Result of the operation.
85 */
86 public RemoteOperationResult execute(OwnCloudClient client,
87 FileDataStorageManager storageManager) {
88 if (storageManager == null)
89 throw new IllegalArgumentException("Trying to execute a sync operation with a " +
90 "NULL storage manager");
91 mStorageManager = storageManager;
92 return super.execute(client);
93 }
94
95
96 /**
97 * Asynchronously executes the remote operation
98 *
99 * This method should be used whenever an ownCloud account is available, instead of
100 * {@link #execute(OwnCloudClient)}.
101 *
102 * @param account ownCloud account in remote ownCloud server to reach during the
103 * execution of the operation.
104 * @param context Android context for the component calling the method.
105 * @param listener Listener to be notified about the execution of the operation.
106 * @param listenerHandler Handler associated to the thread where the methods of the listener
107 * objects must be called.
108 * @return Thread were the remote operation is executed.
109 */
110 /*
111 public Thread execute(FileDataStorageManager storageManager,
112 Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) {
113 if (storageManager == null) {
114 throw new IllegalArgumentException("Trying to execute a sync operation
115 with a NULL storage manager");
116 }
117 if (storageManager.getAccount() == null) {
118 throw new IllegalArgumentException("Trying to execute a sync operation with a
119 storage manager for a NULL account");
120 }
121 mStorageManager = storageManager;
122 return super.execute(storageManager.getAccount(), context, listener, listenerHandler,
123 callerActivity);
124 }
125 */
126
127
128 /**
129 * Asynchronously executes the remote operation
130 *
131 * @param client Client object to reach an ownCloud server during the
132 * execution of the operation.
133 * @param listener Listener to be notified about the execution of the operation.
134 * @param listenerHandler Handler associated to the thread where the methods of
135 * the listener objects must be called.
136 * @return Thread were the remote operation is executed.
137 */
138 public Thread execute(OwnCloudClient client, FileDataStorageManager storageManager,
139 OnRemoteOperationListener listener, Handler listenerHandler) {
140 if (storageManager == null) {
141 throw new IllegalArgumentException("Trying to execute a sync operation " +
142 "with a NULL storage manager");
143 }
144 mStorageManager = storageManager;
145 return super.execute(client, listener, listenerHandler);
146 }
147
148
149 }