1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package com
.owncloud
.android
.lib
.operations
.common
;
27 import java
.io
.IOException
;
29 import org
.apache
.commons
.httpclient
.Credentials
;
31 import com
.owncloud
.android
.lib
.network
.BearerCredentials
;
32 import com
.owncloud
.android
.lib
.network
.OwnCloudClient
;
33 import com
.owncloud
.android
.lib
.network
.OwnCloudClientFactory
;
34 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
38 import android
.accounts
.Account
;
39 import android
.accounts
.AccountManager
;
40 import android
.accounts
.AccountsException
;
41 import android
.app
.Activity
;
42 import android
.content
.Context
;
43 import android
.os
.Handler
;
44 import android
.util
.Log
;
48 * Operation which execution involves one or several interactions with an ownCloud server.
50 * Provides methods to execute the operation both synchronously or asynchronously.
52 * @author David A. Velasco
54 public abstract class RemoteOperation
implements Runnable
{
56 private static final String TAG
= RemoteOperation
.class.getSimpleName();
58 /** ownCloud account in the remote ownCloud server to operate */
59 private Account mAccount
= null
;
61 /** Android Application context */
62 private Context mContext
= null
;
64 /** Object to interact with the remote server */
65 private OwnCloudClient mClient
= null
;
67 /** Callback object to notify about the execution of the remote operation */
68 private OnRemoteOperationListener mListener
= null
;
70 /** Handler to the thread where mListener methods will be called */
71 private Handler mListenerHandler
= null
;
74 private Activity mCallerActivity
;
78 * Abstract method to implement the operation in derived classes.
80 protected abstract RemoteOperationResult
run(OwnCloudClient client
);
84 * Synchronously executes the remote operation on the received ownCloud account.
86 * Do not call this method from the main thread.
88 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
90 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
91 * @param context Android context for the component calling the method.
92 * @return Result of the operation.
94 public final RemoteOperationResult
execute(Account account
, Context context
) {
96 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
98 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
100 mContext
= context
.getApplicationContext();
102 mClient
= OwnCloudClientFactory
.createOwnCloudClient(mAccount
, mContext
);
103 } catch (Exception e
) {
104 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
105 return new RemoteOperationResult(e
);
112 * Synchronously executes the remote operation
114 * Do not call this method from the main thread.
116 * @param client Client object to reach an ownCloud server during the execution of the operation.
117 * @return Result of the operation.
119 public final RemoteOperationResult
execute(OwnCloudClient client
) {
121 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient");
128 * Asynchronously executes the remote operation
130 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
132 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
133 * @param context Android context for the component calling the method.
134 * @param listener Listener to be notified about the execution of the operation.
135 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
136 * @return Thread were the remote operation is executed.
138 public final Thread
execute(Account account
, Context context
, OnRemoteOperationListener listener
, Handler listenerHandler
, Activity callerActivity
) {
140 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
142 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
144 mContext
= context
.getApplicationContext();
145 mCallerActivity
= callerActivity
;
146 mClient
= null
; // the client instance will be created from mAccount and mContext in the runnerThread to create below
148 mListener
= listener
;
150 mListenerHandler
= listenerHandler
;
152 Thread runnerThread
= new Thread(this);
153 runnerThread
.start();
159 * Asynchronously executes the remote operation
161 * @param client Client object to reach an ownCloud server during the execution of the operation.
162 * @param listener Listener to be notified about the execution of the operation.
163 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
164 * @return Thread were the remote operation is executed.
166 public final Thread
execute(OwnCloudClient client
, OnRemoteOperationListener listener
, Handler listenerHandler
) {
167 if (client
== null
) {
168 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient");
172 if (listener
== null
) {
173 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
175 mListener
= listener
;
177 if (listenerHandler
== null
) {
178 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
180 mListenerHandler
= listenerHandler
;
182 Thread runnerThread
= new Thread(this);
183 runnerThread
.start();
188 * Synchronously retries the remote operation using the same OwnCloudClient in the last call to {@link RemoteOperation#execute(OwnCloudClient)}
190 * @param listener Listener to be notified about the execution of the operation.
191 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
192 * @return Thread were the remote operation is executed.
194 public final RemoteOperationResult
retry() {
195 return execute(mClient
);
199 * Asynchronously retries the remote operation using the same OwnCloudClient in the last call to {@link RemoteOperation#execute(OwnCloudClient, OnRemoteOperationListener, Handler)}
201 * @param listener Listener to be notified about the execution of the operation.
202 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
203 * @return Thread were the remote operation is executed.
205 public final Thread
retry(OnRemoteOperationListener listener
, Handler listenerHandler
) {
206 return execute(mClient
, listener
, listenerHandler
);
211 * Asynchronous execution of the operation
212 * started by {@link RemoteOperation#execute(OwnCloudClient, OnRemoteOperationListener, Handler)},
213 * and result posting.
215 * TODO refactor && clean the code; now it's a mess
218 public final void run() {
219 RemoteOperationResult result
= null
;
220 boolean repeat
= false
;
223 if (mClient
== null
) {
224 if (mAccount
!= null
&& mContext
!= null
) {
225 if (mCallerActivity
!= null
) {
226 mClient
= OwnCloudClientFactory
.createOwnCloudClient(mAccount
, mContext
, mCallerActivity
);
228 mClient
= OwnCloudClientFactory
.createOwnCloudClient(mAccount
, mContext
);
231 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
235 } catch (IOException e
) {
236 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, new AccountsException("I/O exception while trying to authorize the account", e
));
237 result
= new RemoteOperationResult(e
);
239 } catch (AccountsException e
) {
240 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
241 result
= new RemoteOperationResult(e
);
245 result
= run(mClient
);
248 if (mCallerActivity
!= null
&& mAccount
!= null
&& mContext
!= null
&& !result
.isSuccess() &&
249 // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
250 (result
.getCode() == ResultCode
.UNAUTHORIZED
|| result
.isIdPRedirection())) {
251 /// possible fail due to lack of authorization in an operation performed in foreground
252 Credentials cred
= mClient
.getCredentials();
253 String ssoSessionCookie
= mClient
.getSsoSessionCookie();
254 if (cred
!= null
|| ssoSessionCookie
!= null
) {
255 /// confirmed : unauthorized operation
256 AccountManager am
= AccountManager
.get(mContext
);
257 boolean bearerAuthorization
= (cred
!= null
&& cred
instanceof BearerCredentials
);
258 boolean samlBasedSsoAuthorization
= (cred
== null
&& ssoSessionCookie
!= null
);
259 if (bearerAuthorization
) {
260 am
.invalidateAuthToken(mAccount
.type
, ((BearerCredentials
)cred
).getAccessToken());
261 } else if (samlBasedSsoAuthorization
) {
262 am
.invalidateAuthToken(mAccount
.type
, ssoSessionCookie
);
264 am
.clearPassword(mAccount
);
267 repeat
= true
; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
273 final RemoteOperationResult resultToSend
= result
;
274 if (mListenerHandler
!= null
&& mListener
!= null
) {
275 mListenerHandler
.post(new Runnable() {
278 mListener
.onRemoteOperationFinish(RemoteOperation
.this, resultToSend
);
286 * Returns the current client instance to access the remote server.
288 * @return Current client instance to access the remote server.
290 public final OwnCloudClient
getClient() {