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 android
.os
.Handler
;
22 import eu
.alefzero
.webdav
.WebdavClient
;
25 * Operation which execution involves one or several interactions with an ownCloud server.
27 * Provides methods to execute the operation both synchronously or asynchronously.
29 * @author David A. Velasco
31 public abstract class RemoteOperation
implements Runnable
{
33 /** Object to interact with the ownCloud server */
34 private WebdavClient mClient
= null
;
36 /** Callback object to notify about the execution of the remote operation */
37 private OnRemoteOperationListener mListener
= null
;
39 /** Handler to the thread where mListener methods will be called */
40 private Handler mListenerHandler
= null
;
44 * Abstract method to implement the operation in derived classes.
46 protected abstract RemoteOperationResult
run(WebdavClient client
);
50 * Synchronously executes the remote operation
52 * @param client Client object to reach an ownCloud server during the execution of the operation.
53 * @return Result of the operation.
55 public final RemoteOperationResult
execute(WebdavClient client
) {
57 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
64 * Asynchronously executes the remote operation
66 * @param client Client object to reach an ownCloud server during the execution of the operation.
67 * @param listener Listener to be notified about the execution of the operation.
68 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
69 * @return Thread were the remote operation is executed.
71 public final Thread
execute(WebdavClient client
, OnRemoteOperationListener listener
, Handler listenerHandler
) {
73 throw new IllegalArgumentException("Trying to execute a remote operation with a NULL WebdavClient");
77 if (listener
== null
) {
78 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
82 if (listenerHandler
== null
) {
83 throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
85 mListenerHandler
= listenerHandler
;
87 Thread runnerThread
= new Thread(this);
93 * Synchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient)}
95 * @param listener Listener to be notified about the execution of the operation.
96 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
97 * @return Thread were the remote operation is executed.
99 public final RemoteOperationResult
retry() {
100 return execute(mClient
);
104 * Asynchronously retries the remote operation using the same WebdavClient in the last call to {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}
106 * @param listener Listener to be notified about the execution of the operation.
107 * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
108 * @return Thread were the remote operation is executed.
110 public final Thread
retry(OnRemoteOperationListener listener
, Handler listenerHandler
) {
111 return execute(mClient
, listener
, listenerHandler
);
116 * Asynchronous execution of the operation
117 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
118 * and result posting.
121 public final void run() {
122 final RemoteOperationResult result
= execute(mClient
);
124 if (mListenerHandler
!= null
&& mListener
!= null
) {
125 mListenerHandler
.post(new Runnable() {
128 mListener
.onRemoteOperationFinish(RemoteOperation
.this, result
);