1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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 2 of the License, or
7 * (at your option) any later version.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
20 import java
.io
.IOException
;
22 import org
.apache
.commons
.httpclient
.Credentials
;
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
;
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
;
37 import eu
.alefzero
.webdav
.WebdavClient
;
40 * Operation which execution involves one or several interactions with an ownCloud server.
42 * Provides methods to execute the operation both synchronously or asynchronously.
44 * @author David A. Velasco
46 public abstract class RemoteOperation
implements Runnable
{
48 private static final String TAG
= RemoteOperation
.class.getSimpleName();
50 /** ownCloud account in the remote ownCloud server to operate */
51 private Account mAccount
= null
;
53 /** Android Application context */
54 private Context mContext
= null
;
56 /** Object to interact with the remote server */
57 private WebdavClient mClient
= null
;
59 /** Callback object to notify about the execution of the remote operation */
60 private OnRemoteOperationListener mListener
= null
;
62 /** Handler to the thread where mListener methods will be called */
63 private Handler mListenerHandler
= null
;
66 private Activity mCallerActivity
;
70 * Abstract method to implement the operation in derived classes.
72 protected abstract RemoteOperationResult
run(WebdavClient client
);
76 * Synchronously executes the remote operation on the received ownCloud account.
78 * Do not call this method from the main thread.
80 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
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.
86 public final RemoteOperationResult
execute(Account account
, Context context
) {
88 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
90 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
92 mContext
= context
.getApplicationContext();
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
);
104 * Synchronously executes the remote operation
106 * Do not call this method from the main thread.
108 * @param client Client object to reach an ownCloud server during the execution of the operation.
109 * @return Result of the operation.
111 public final RemoteOperationResult
execute(WebdavClient client
) {
113 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
120 * Asynchronously executes the remote operation
122 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
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.
130 public final Thread
execute(Account account
, Context context
, OnRemoteOperationListener listener
, Handler listenerHandler
, Activity callerActivity
) {
132 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
134 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
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
140 if (listener
== null
) {
141 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
143 mListener
= listener
;
145 if (listenerHandler
== null
) {
146 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
148 mListenerHandler
= listenerHandler
;
150 Thread runnerThread
= new Thread(this);
151 runnerThread
.start();
157 * Asynchronously executes the remote operation
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.
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");
170 if (listener
== null
) {
171 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
173 mListener
= listener
;
175 if (listenerHandler
== null
) {
176 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
178 mListenerHandler
= listenerHandler
;
180 Thread runnerThread
= new Thread(this);
181 runnerThread
.start();
186 * Synchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient)}
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.
192 public final RemoteOperationResult
retry() {
193 return execute(mClient
);
197 * Asynchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}
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.
203 public final Thread
retry(OnRemoteOperationListener listener
, Handler listenerHandler
) {
204 return execute(mClient
, listener
, listenerHandler
);
209 * Asynchronous execution of the operation
210 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
211 * and result posting.
213 * TODO refactor && clean the code; now it's a mess
216 public final void run() {
217 RemoteOperationResult result
= null
;
218 boolean repeat
= false
;
221 if (mClient
== null
) {
222 if (mAccount
!= null
&& mContext
!= null
) {
223 if (mCallerActivity
!= null
) {
224 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, mCallerActivity
);
226 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
);
229 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
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
);
237 } catch (AccountsException e
) {
238 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
239 result
= new RemoteOperationResult(e
);
243 result
= run(mClient
);
246 if (mCallerActivity
!= null
&& mAccount
!= null
&& mContext
!= null
&& !result
.isSuccess() && result
.getCode() == ResultCode
.UNAUTHORIZED
) {
247 /// fail due to lack of authorization in an operation performed in foreground
248 AccountManager am
= AccountManager
.get(mContext
);
249 Credentials cred
= mClient
.getCredentials();
250 if (cred
instanceof BearerCredentials
) {
251 am
.invalidateAuthToken(AccountAuthenticator
.ACCOUNT_TYPE
, ((BearerCredentials
)cred
).getAccessToken());
253 am
.clearPassword(mAccount
);
256 repeat
= true
; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
261 final RemoteOperationResult resultToSend
= result
;
262 if (mListenerHandler
!= null
&& mListener
!= null
) {
263 mListenerHandler
.post(new Runnable() {
266 mListener
.onRemoteOperationFinish(RemoteOperation
.this, resultToSend
);
274 * Returns the current client instance to access the remote server.
276 * @return Current client instance to access the remote server.
278 public final WebdavClient
getClient() {