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
.oc_framework
.operations
;
19 import java
.io
.IOException
;
21 import org
.apache
.commons
.httpclient
.Credentials
;
23 import com
.owncloud
.android
.oc_framework
.network
.BearerCredentials
;
24 import com
.owncloud
.android
.oc_framework
.network
.OwnCloudClientUtils
;
25 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
26 import com
.owncloud
.android
.oc_framework
.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
;
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
;
53 private String mAuthorities
;
55 /** Android Application context */
56 private Context mContext
= null
;
58 /** Object to interact with the remote server */
59 private WebdavClient mClient
= null
;
61 /** Callback object to notify about the execution of the remote operation */
62 private OnRemoteOperationListener mListener
= null
;
64 /** Handler to the thread where mListener methods will be called */
65 private Handler mListenerHandler
= null
;
68 private Activity mCallerActivity
;
72 * Abstract method to implement the operation in derived classes.
74 protected abstract RemoteOperationResult
run(WebdavClient client
);
78 * Synchronously executes the remote operation on the received ownCloud account.
80 * Do not call this method from the main thread.
82 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
84 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
85 * @param context Android context for the component calling the method.
86 * @return Result of the operation.
88 public final RemoteOperationResult
execute(Account account
, Context context
, String authorities
) {
90 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
92 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
94 mContext
= context
.getApplicationContext();
96 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, authorities
);
97 } catch (Exception e
) {
98 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
99 return new RemoteOperationResult(e
);
101 mAuthorities
= authorities
;
107 * Synchronously executes the remote operation
109 * Do not call this method from the main thread.
111 * @param client Client object to reach an ownCloud server during the execution of the operation.
112 * @return Result of the operation.
114 public final RemoteOperationResult
execute(WebdavClient client
) {
116 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
123 * Asynchronously executes the remote operation
125 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
127 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
128 * @param context Android context for the component calling the method.
129 * @param listener Listener to be notified about the execution of the operation.
130 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
131 * @return Thread were the remote operation is executed.
133 public final Thread
execute(Account account
, Context context
, OnRemoteOperationListener listener
, Handler listenerHandler
, Activity callerActivity
) {
135 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
137 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
139 mContext
= context
.getApplicationContext();
140 mCallerActivity
= callerActivity
;
141 mClient
= null
; // the client instance will be created from mAccount and mContext in the runnerThread to create below
143 mListener
= listener
;
145 mListenerHandler
= listenerHandler
;
147 Thread runnerThread
= new Thread(this);
148 runnerThread
.start();
154 * Asynchronously executes the remote operation
156 * @param client Client object to reach an ownCloud server during the execution of the operation.
157 * @param listener Listener to be notified about the execution of the operation.
158 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
159 * @return Thread were the remote operation is executed.
161 public final Thread
execute(WebdavClient client
, OnRemoteOperationListener listener
, Handler listenerHandler
) {
162 if (client
== null
) {
163 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
167 if (listener
== null
) {
168 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
170 mListener
= listener
;
172 if (listenerHandler
== null
) {
173 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
175 mListenerHandler
= listenerHandler
;
177 Thread runnerThread
= new Thread(this);
178 runnerThread
.start();
183 * Synchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient)}
185 * @param listener Listener to be notified about the execution of the operation.
186 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
187 * @return Thread were the remote operation is executed.
189 public final RemoteOperationResult
retry() {
190 return execute(mClient
);
194 * Asynchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}
196 * @param listener Listener to be notified about the execution of the operation.
197 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
198 * @return Thread were the remote operation is executed.
200 public final Thread
retry(OnRemoteOperationListener listener
, Handler listenerHandler
) {
201 return execute(mClient
, listener
, listenerHandler
);
206 * Asynchronous execution of the operation
207 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
208 * and result posting.
210 * TODO refactor && clean the code; now it's a mess
213 public final void run() {
214 RemoteOperationResult result
= null
;
215 boolean repeat
= false
;
218 if (mClient
== null
) {
219 if (mAccount
!= null
&& mContext
!= null
) {
220 if (mCallerActivity
!= null
) {
221 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, mCallerActivity
, mAuthorities
);
223 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, mAuthorities
);
226 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
230 } catch (IOException e
) {
231 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, new AccountsException("I/O exception while trying to authorize the account", e
));
232 result
= new RemoteOperationResult(e
);
234 } catch (AccountsException e
) {
235 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
236 result
= new RemoteOperationResult(e
);
240 result
= run(mClient
);
243 if (mCallerActivity
!= null
&& mAccount
!= null
&& mContext
!= null
&& !result
.isSuccess() &&
244 // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
245 (result
.getCode() == ResultCode
.UNAUTHORIZED
|| result
.isIdPRedirection())) {
246 /// possible fail due to lack of authorization in an operation performed in foreground
247 Credentials cred
= mClient
.getCredentials();
248 String ssoSessionCookie
= mClient
.getSsoSessionCookie();
249 if (cred
!= null
|| ssoSessionCookie
!= null
) {
250 /// confirmed : unauthorized operation
251 AccountManager am
= AccountManager
.get(mContext
);
252 boolean bearerAuthorization
= (cred
!= null
&& cred
instanceof BearerCredentials
);
253 boolean samlBasedSsoAuthorization
= (cred
== null
&& ssoSessionCookie
!= null
);
254 if (bearerAuthorization
) {
255 am
.invalidateAuthToken(mAccount
.type
, ((BearerCredentials
)cred
).getAccessToken());
256 } else if (samlBasedSsoAuthorization
) {
257 am
.invalidateAuthToken(mAccount
.type
, ssoSessionCookie
);
259 am
.clearPassword(mAccount
);
262 repeat
= true
; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
268 final RemoteOperationResult resultToSend
= result
;
269 if (mListenerHandler
!= null
&& mListener
!= null
) {
270 mListenerHandler
.post(new Runnable() {
273 mListener
.onRemoteOperationFinish(RemoteOperation
.this, resultToSend
);
281 * Returns the current client instance to access the remote server.
283 * @return Current client instance to access the remote server.
285 public final WebdavClient
getClient() {