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
.MainApp
;
25 import com
.owncloud
.android
.network
.BearerCredentials
;
26 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
27 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
30 import android
.accounts
.Account
;
31 import android
.accounts
.AccountManager
;
32 import android
.accounts
.AccountsException
;
33 import android
.app
.Activity
;
34 import android
.content
.Context
;
35 import android
.os
.Handler
;
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_OC
.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 mListener
= listener
;
142 mListenerHandler
= listenerHandler
;
144 Thread runnerThread
= new Thread(this);
145 runnerThread
.start();
151 * Asynchronously executes the remote operation
153 * @param client Client object to reach an ownCloud server during the execution of the operation.
154 * @param listener Listener to be notified about the execution of the operation.
155 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
156 * @return Thread were the remote operation is executed.
158 public final Thread
execute(WebdavClient client
, OnRemoteOperationListener listener
, Handler listenerHandler
) {
159 if (client
== null
) {
160 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
164 if (listener
== null
) {
165 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
167 mListener
= listener
;
169 if (listenerHandler
== null
) {
170 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
172 mListenerHandler
= listenerHandler
;
174 Thread runnerThread
= new Thread(this);
175 runnerThread
.start();
180 * Synchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient)}
182 * @param listener Listener to be notified about the execution of the operation.
183 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
184 * @return Thread were the remote operation is executed.
186 public final RemoteOperationResult
retry() {
187 return execute(mClient
);
191 * Asynchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}
193 * @param listener Listener to be notified about the execution of the operation.
194 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
195 * @return Thread were the remote operation is executed.
197 public final Thread
retry(OnRemoteOperationListener listener
, Handler listenerHandler
) {
198 return execute(mClient
, listener
, listenerHandler
);
203 * Asynchronous execution of the operation
204 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
205 * and result posting.
207 * TODO refactor && clean the code; now it's a mess
210 public final void run() {
211 RemoteOperationResult result
= null
;
212 boolean repeat
= false
;
215 if (mClient
== null
) {
216 if (mAccount
!= null
&& mContext
!= null
) {
217 if (mCallerActivity
!= null
) {
218 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, mCallerActivity
);
220 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
);
223 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
227 } catch (IOException e
) {
228 Log_OC
.e(TAG
, "Error while trying to access to " + mAccount
.name
, new AccountsException("I/O exception while trying to authorize the account", e
));
229 result
= new RemoteOperationResult(e
);
231 } catch (AccountsException e
) {
232 Log_OC
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
233 result
= new RemoteOperationResult(e
);
237 result
= run(mClient
);
240 if (mCallerActivity
!= null
&& mAccount
!= null
&& mContext
!= null
&& !result
.isSuccess() &&
241 // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
242 (result
.getCode() == ResultCode
.UNAUTHORIZED
|| result
.isIdPRedirection())) {
243 /// possible fail due to lack of authorization in an operation performed in foreground
244 Credentials cred
= mClient
.getCredentials();
245 String ssoSessionCookie
= mClient
.getSsoSessionCookie();
246 if (cred
!= null
|| ssoSessionCookie
!= null
) {
247 /// confirmed : unauthorized operation
248 AccountManager am
= AccountManager
.get(mContext
);
249 boolean bearerAuthorization
= (cred
!= null
&& cred
instanceof BearerCredentials
);
250 boolean samlBasedSsoAuthorization
= (cred
== null
&& ssoSessionCookie
!= null
);
251 if (bearerAuthorization
) {
252 am
.invalidateAuthToken(MainApp
.getAccountType(), ((BearerCredentials
)cred
).getAccessToken());
253 } else if (samlBasedSsoAuthorization
) {
254 am
.invalidateAuthToken(MainApp
.getAccountType(), ssoSessionCookie
);
256 am
.clearPassword(mAccount
);
259 repeat
= true
; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
265 final RemoteOperationResult resultToSend
= result
;
266 if (mListenerHandler
!= null
&& mListener
!= null
) {
267 mListenerHandler
.post(new Runnable() {
270 mListener
.onRemoteOperationFinish(RemoteOperation
.this, resultToSend
);
278 * Returns the current client instance to access the remote server.
280 * @return Current client instance to access the remote server.
282 public final WebdavClient
getClient() {