bf635a8da99e2691eea3115a7953de47ccb1d3b5
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoteOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.operations;
19
20 import java.io.IOException;
21
22 import org.apache.commons.httpclient.Credentials;
23
24 import com.owncloud.android.authenticator.AccountAuthenticator;
25 import com.owncloud.android.network.BearerCredentials;
26 import com.owncloud.android.network.OwnCloudClientUtils;
27 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
28
29 import android.accounts.Account;
30 import android.accounts.AccountManager;
31 import android.accounts.AccountsException;
32 import android.app.Activity;
33 import android.content.Context;
34 import android.os.Handler;
35 import android.util.Log;
36
37 import eu.alefzero.webdav.WebdavClient;
38
39 /**
40 * Operation which execution involves one or several interactions with an ownCloud server.
41 *
42 * Provides methods to execute the operation both synchronously or asynchronously.
43 *
44 * @author David A. Velasco
45 */
46 public abstract class RemoteOperation implements Runnable {
47
48 private static final String TAG = RemoteOperation.class.getSimpleName();
49
50 /** ownCloud account in the remote ownCloud server to operate */
51 private Account mAccount = null;
52
53 /** Android Application context */
54 private Context mContext = null;
55
56 /** Object to interact with the remote server */
57 private WebdavClient mClient = null;
58
59 /** Callback object to notify about the execution of the remote operation */
60 private OnRemoteOperationListener mListener = null;
61
62 /** Handler to the thread where mListener methods will be called */
63 private Handler mListenerHandler = null;
64
65 /** Activity */
66 private Activity mCallerActivity;
67
68
69 /**
70 * Abstract method to implement the operation in derived classes.
71 */
72 protected abstract RemoteOperationResult run(WebdavClient client);
73
74
75 /**
76 * Synchronously executes the remote operation on the received ownCloud account.
77 *
78 * Do not call this method from the main thread.
79 *
80 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
81 *
82 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
83 * @param context Android context for the component calling the method.
84 * @return Result of the operation.
85 */
86 public final RemoteOperationResult execute(Account account, Context context) {
87 if (account == null)
88 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
89 if (context == null)
90 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
91 mAccount = account;
92 mContext = context.getApplicationContext();
93 try {
94 mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
95 } catch (Exception e) {
96 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
97 return new RemoteOperationResult(e);
98 }
99 return run(mClient);
100 }
101
102
103 /**
104 * Synchronously executes the remote operation
105 *
106 * Do not call this method from the main thread.
107 *
108 * @param client Client object to reach an ownCloud server during the execution of the operation.
109 * @return Result of the operation.
110 */
111 public final RemoteOperationResult execute(WebdavClient client) {
112 if (client == null)
113 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
114 mClient = client;
115 return run(client);
116 }
117
118
119 /**
120 * Asynchronously executes the remote operation
121 *
122 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
123 *
124 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
125 * @param context Android context for the component calling the method.
126 * @param listener Listener to be notified about the execution of the operation.
127 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
128 * @return Thread were the remote operation is executed.
129 */
130 public final Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) {
131 if (account == null)
132 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
133 if (context == null)
134 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
135 mAccount = account;
136 mContext = context.getApplicationContext();
137 mCallerActivity = callerActivity;
138 mClient = null; // the client instance will be created from mAccount and mContext in the runnerThread to create below
139
140 if (listener == null) {
141 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
142 }
143 mListener = listener;
144
145 if (listenerHandler == null) {
146 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
147 }
148 mListenerHandler = listenerHandler;
149
150 Thread runnerThread = new Thread(this);
151 runnerThread.start();
152 return runnerThread;
153 }
154
155
156 /**
157 * Asynchronously executes the remote operation
158 *
159 * @param client Client object to reach an ownCloud server during the execution of the operation.
160 * @param listener Listener to be notified about the execution of the operation.
161 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
162 * @return Thread were the remote operation is executed.
163 */
164 public final Thread execute(WebdavClient client, OnRemoteOperationListener listener, Handler listenerHandler) {
165 if (client == null) {
166 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
167 }
168 mClient = client;
169
170 if (listener == null) {
171 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
172 }
173 mListener = listener;
174
175 if (listenerHandler == null) {
176 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
177 }
178 mListenerHandler = listenerHandler;
179
180 Thread runnerThread = new Thread(this);
181 runnerThread.start();
182 return runnerThread;
183 }
184
185 /**
186 * Synchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient)}
187 *
188 * @param listener Listener to be notified about the execution of the operation.
189 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
190 * @return Thread were the remote operation is executed.
191 */
192 public final RemoteOperationResult retry() {
193 return execute(mClient);
194 }
195
196 /**
197 * Asynchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}
198 *
199 * @param listener Listener to be notified about the execution of the operation.
200 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
201 * @return Thread were the remote operation is executed.
202 */
203 public final Thread retry(OnRemoteOperationListener listener, Handler listenerHandler) {
204 return execute(mClient, listener, listenerHandler);
205 }
206
207
208 /**
209 * Asynchronous execution of the operation
210 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
211 * and result posting.
212 *
213 * TODO refactor && clean the code; now it's a mess
214 */
215 @Override
216 public final void run() {
217 RemoteOperationResult result = null;
218 boolean repeat = false;
219 do {
220 try{
221 if (mClient == null) {
222 if (mAccount != null && mContext != null) {
223 if (mCallerActivity != null) {
224 mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity);
225 } else {
226 mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
227 }
228 } else {
229 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
230 }
231 }
232
233 } catch (IOException e) {
234 Log.e(TAG, "Error while trying to access to " + mAccount.name, new AccountsException("I/O exception while trying to authorize the account", e));
235 result = new RemoteOperationResult(e);
236
237 } catch (AccountsException e) {
238 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
239 result = new RemoteOperationResult(e);
240 }
241
242 if (result == null)
243 result = run(mClient);
244
245 repeat = false;
246 if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && result.getCode() == ResultCode.UNAUTHORIZED) {
247 AccountManager am = AccountManager.get(mContext);
248 Credentials cred = mClient.getCredentials();
249 if (cred instanceof BearerCredentials) {
250 am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ((BearerCredentials)cred).getAccessToken());
251 } else {
252 am.clearPassword(mAccount);
253 }
254 mClient = null;
255 repeat = true;
256 result = null;
257 }
258 } while (repeat);
259
260 final RemoteOperationResult resultToSend = result;
261 if (mListenerHandler != null && mListener != null) {
262 mListenerHandler.post(new Runnable() {
263 @Override
264 public void run() {
265 mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
266 }
267 });
268 }
269 }
270
271
272 /**
273 * Returns the current client instance to access the remote server.
274 *
275 * @return Current client instance to access the remote server.
276 */
277 public final WebdavClient getClient() {
278 return mClient;
279 }
280
281 }