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 version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.operations
;
19 import java
.io
.IOException
;
21 import org
.apache
.commons
.httpclient
.Credentials
;
23 import com
.owncloud
.android
.Log_OC
;
24 import com
.owncloud
.android
.authentication
.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
;
36 import eu
.alefzero
.webdav
.WebdavClient
;
39 * Operation which execution involves one or several interactions with an ownCloud server.
41 * Provides methods to execute the operation both synchronously or asynchronously.
43 * @author David A. Velasco
45 public abstract class RemoteOperation
implements Runnable
{
47 private static final String TAG
= RemoteOperation
.class.getSimpleName();
49 /** ownCloud account in the remote ownCloud server to operate */
50 private Account mAccount
= null
;
52 /** Android Application context */
53 private Context mContext
= null
;
55 /** Object to interact with the remote server */
56 private WebdavClient mClient
= null
;
58 /** Callback object to notify about the execution of the remote operation */
59 private OnRemoteOperationListener mListener
= null
;
61 /** Handler to the thread where mListener methods will be called */
62 private Handler mListenerHandler
= null
;
65 private Activity mCallerActivity
;
69 * Abstract method to implement the operation in derived classes.
71 protected abstract RemoteOperationResult
run(WebdavClient client
);
75 * Synchronously executes the remote operation on the received ownCloud account.
77 * Do not call this method from the main thread.
79 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
81 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
82 * @param context Android context for the component calling the method.
83 * @return Result of the operation.
85 public final RemoteOperationResult
execute(Account account
, Context context
) {
87 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
89 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
91 mContext
= context
.getApplicationContext();
93 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
);
94 } catch (Exception e
) {
95 Log_OC
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
96 return new RemoteOperationResult(e
);
103 * Synchronously executes the remote operation
105 * Do not call this method from the main thread.
107 * @param client Client object to reach an ownCloud server during the execution of the operation.
108 * @return Result of the operation.
110 public final RemoteOperationResult
execute(WebdavClient client
) {
112 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
119 * Asynchronously executes the remote operation
121 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
123 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
124 * @param context Android context for the component calling the method.
125 * @param listener Listener to be notified about the execution of the operation.
126 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
127 * @return Thread were the remote operation is executed.
129 public final Thread
execute(Account account
, Context context
, OnRemoteOperationListener listener
, Handler listenerHandler
, Activity callerActivity
) {
131 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
133 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
135 mContext
= context
.getApplicationContext();
136 mCallerActivity
= callerActivity
;
137 mClient
= null
; // the client instance will be created from mAccount and mContext in the runnerThread to create below
139 if (listener
== null
) {
140 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
142 mListener
= listener
;
144 if (listenerHandler
== null
) {
145 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
147 mListenerHandler
= listenerHandler
;
149 Thread runnerThread
= new Thread(this);
150 runnerThread
.start();
156 * Asynchronously executes the remote operation
158 * @param client Client object to reach an ownCloud server during the execution of the operation.
159 * @param listener Listener to be notified about the execution of the operation.
160 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
161 * @return Thread were the remote operation is executed.
163 public final Thread
execute(WebdavClient client
, OnRemoteOperationListener listener
, Handler listenerHandler
) {
164 if (client
== null
) {
165 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
169 if (listener
== null
) {
170 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
172 mListener
= listener
;
174 if (listenerHandler
== null
) {
175 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
177 mListenerHandler
= listenerHandler
;
179 Thread runnerThread
= new Thread(this);
180 runnerThread
.start();
185 * Synchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient)}
187 * @param listener Listener to be notified about the execution of the operation.
188 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
189 * @return Thread were the remote operation is executed.
191 public final RemoteOperationResult
retry() {
192 return execute(mClient
);
196 * Asynchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}
198 * @param listener Listener to be notified about the execution of the operation.
199 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
200 * @return Thread were the remote operation is executed.
202 public final Thread
retry(OnRemoteOperationListener listener
, Handler listenerHandler
) {
203 return execute(mClient
, listener
, listenerHandler
);
208 * Asynchronous execution of the operation
209 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
210 * and result posting.
212 * TODO refactor && clean the code; now it's a mess
215 public final void run() {
216 RemoteOperationResult result
= null
;
217 boolean repeat
= false
;
220 if (mClient
== null
) {
221 if (mAccount
!= null
&& mContext
!= null
) {
222 if (mCallerActivity
!= null
) {
223 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, mCallerActivity
);
225 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
);
228 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
232 } catch (IOException e
) {
233 Log_OC
.e(TAG
, "Error while trying to access to " + mAccount
.name
, new AccountsException("I/O exception while trying to authorize the account", e
));
234 result
= new RemoteOperationResult(e
);
236 } catch (AccountsException e
) {
237 Log_OC
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
238 result
= new RemoteOperationResult(e
);
242 result
= run(mClient
);
245 if (mCallerActivity
!= null
&& mAccount
!= null
&& mContext
!= null
&& !result
.isSuccess() &&
246 (result
.getCode() == ResultCode
.UNAUTHORIZED
|| result
.isTemporalRedirection())) {
247 /// possible fail due to lack of authorization in an operation performed in foreground
248 Credentials cred
= mClient
.getCredentials();
249 String ssoSessionCookie
= mClient
.getSsoSessionCookie();
250 if (cred
!= null
|| ssoSessionCookie
!= null
) {
251 /// confirmed : unauthorized operation
252 AccountManager am
= AccountManager
.get(mContext
);
253 boolean bearerAuthorization
= (cred
!= null
&& cred
instanceof BearerCredentials
);
254 boolean samlBasedSsoAuthorization
= (cred
== null
&& ssoSessionCookie
!= null
);
255 if (bearerAuthorization
) {
256 am
.invalidateAuthToken(AccountAuthenticator
.ACCOUNT_TYPE
, ((BearerCredentials
)cred
).getAccessToken());
257 } else if (samlBasedSsoAuthorization
) {
258 am
.invalidateAuthToken(AccountAuthenticator
.ACCOUNT_TYPE
, ssoSessionCookie
);
260 am
.clearPassword(mAccount
);
263 repeat
= true
; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
269 final RemoteOperationResult resultToSend
= result
;
270 if (mListenerHandler
!= null
&& mListener
!= null
) {
271 mListenerHandler
.post(new Runnable() {
274 mListener
.onRemoteOperationFinish(RemoteOperation
.this, resultToSend
);
282 * Returns the current client instance to access the remote server.
284 * @return Current client instance to access the remote server.
286 public final WebdavClient
getClient() {