1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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 3 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 com
.owncloud
.android
.network
.OwnCloudClientUtils
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountsException
;
26 import android
.app
.Activity
;
27 import android
.content
.Context
;
28 import android
.os
.Handler
;
29 import android
.util
.Log
;
31 import eu
.alefzero
.webdav
.WebdavClient
;
34 * Operation which execution involves one or several interactions with an ownCloud server.
36 * Provides methods to execute the operation both synchronously or asynchronously.
38 * @author David A. Velasco
40 public abstract class RemoteOperation
implements Runnable
{
42 private static final String TAG
= RemoteOperation
.class.getSimpleName();
44 /** ownCloud account in the remote ownCloud server to operate */
45 private Account mAccount
= null
;
47 /** Android Application context */
48 private Context mContext
= null
;
50 /** Object to interact with the remote server */
51 private WebdavClient mClient
= null
;
53 /** Callback object to notify about the execution of the remote operation */
54 private OnRemoteOperationListener mListener
= null
;
56 /** Handler to the thread where mListener methods will be called */
57 private Handler mListenerHandler
= null
;
60 private Activity mCallerActivity
;
64 * Abstract method to implement the operation in derived classes.
66 protected abstract RemoteOperationResult
run(WebdavClient client
);
70 * Synchronously executes the remote operation on the received ownCloud account.
72 * Do not call this method from the main thread.
74 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
76 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
77 * @param context Android context for the component calling the method.
78 * @return Result of the operation.
80 public final RemoteOperationResult
execute(Account account
, Context context
) {
82 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
84 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
86 mContext
= context
.getApplicationContext();
88 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
);
89 } catch (Exception e
) {
90 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
91 return new RemoteOperationResult(e
);
98 * Synchronously executes the remote operation
100 * Do not call this method from the main thread.
102 * @param client Client object to reach an ownCloud server during the execution of the operation.
103 * @return Result of the operation.
105 public final RemoteOperationResult
execute(WebdavClient client
) {
107 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
114 * Asynchronously executes the remote operation
116 * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
118 * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
119 * @param context Android context for the component calling the method.
120 * @param listener Listener to be notified about the execution of the operation.
121 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
122 * @return Thread were the remote operation is executed.
124 public final Thread
execute(Account account
, Context context
, OnRemoteOperationListener listener
, Handler listenerHandler
, Activity callerActivity
) {
126 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
128 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
130 mContext
= context
.getApplicationContext();
131 mCallerActivity
= callerActivity
;
132 mClient
= null
; // the client instance will be created from mAccount and mContext in the runnerThread to create below
134 if (listener
== null
) {
135 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
137 mListener
= listener
;
139 if (listenerHandler
== null
) {
140 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
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.
208 public final void run() {
209 RemoteOperationResult result
= null
;
211 if (mClient
== null
) {
212 if (mAccount
!= null
&& mContext
!= null
) {
213 if (mCallerActivity
!= null
) {
214 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
, mCallerActivity
);
216 mClient
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, mContext
);
219 throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
223 } catch (IOException e
) {
224 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, new AccountsException("I/O exception while trying to authorize the account", e
));
225 result
= new RemoteOperationResult(e
);
227 } catch (AccountsException e
) {
228 Log
.e(TAG
, "Error while trying to access to " + mAccount
.name
, e
);
229 result
= new RemoteOperationResult(e
);
233 result
= run(mClient
);
235 final RemoteOperationResult resultToSend
= result
;
236 if (mListenerHandler
!= null
&& mListener
!= null
) {
237 mListenerHandler
.post(new Runnable() {
240 mListener
.onRemoteOperationFinish(RemoteOperation
.this, resultToSend
);
248 * Returns the current client instance to access the remote server.
250 * @return Current client instance to access the remote server.
252 public final WebdavClient
getClient() {