import java.io.IOException;
+import org.apache.commons.httpclient.Credentials;
+
+import com.owncloud.android.authenticator.AccountAuthenticator;
+import com.owncloud.android.network.BearerCredentials;
import com.owncloud.android.network.OwnCloudClientUtils;
+import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
import android.accounts.Account;
+import android.accounts.AccountManager;
import android.accounts.AccountsException;
import android.app.Activity;
import android.content.Context;
* Asynchronous execution of the operation
* started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)},
* and result posting.
+ *
+ * TODO refactor && clean the code; now it's a mess
*/
@Override
public final void run() {
RemoteOperationResult result = null;
- try{
- if (mClient == null) {
- if (mAccount != null && mContext != null) {
- if (mCallerActivity != null) {
- mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity);
+ boolean repeat = false;
+ do {
+ try{
+ if (mClient == null) {
+ if (mAccount != null && mContext != null) {
+ if (mCallerActivity != null) {
+ mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity);
+ } else {
+ mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
+ }
} else {
- mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
+ throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
}
- } else {
- throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
}
- }
- } catch (IOException e) {
- Log.e(TAG, "Error while trying to access to " + mAccount.name, new AccountsException("I/O exception while trying to authorize the account", e));
- result = new RemoteOperationResult(e);
+ } catch (IOException e) {
+ Log.e(TAG, "Error while trying to access to " + mAccount.name, new AccountsException("I/O exception while trying to authorize the account", e));
+ result = new RemoteOperationResult(e);
- } catch (AccountsException e) {
- Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
- result = new RemoteOperationResult(e);
- }
+ } catch (AccountsException e) {
+ Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
+ result = new RemoteOperationResult(e);
+ }
- if (result == null)
- result = run(mClient);
+ if (result == null)
+ result = run(mClient);
+
+ repeat = false;
+ if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && result.getCode() == ResultCode.UNAUTHORIZED) {
+ AccountManager am = AccountManager.get(mContext);
+ Credentials cred = mClient.getCredentials();
+ if (cred instanceof BearerCredentials) {
+ am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ((BearerCredentials)cred).getAccessToken());
+ } else {
+ am.clearPassword(mAccount);
+ }
+ mClient = null;
+ repeat = true;
+ }
+ } while (repeat);
final RemoteOperationResult resultToSend = result;
if (mListenerHandler != null && mListener != null) {
*/\r
@Override\r
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
- if (!result.isSuccess() && result.getCode() == ResultCode.UNAUTHORIZED) {\r
- AccountManager am = AccountManager.get(getSherlockActivity());\r
- //am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, OwnCloudClientUtils.getAuthorizationTokenType(operation.getClient().getCredentials()));\r
- Credentials cred = operation.getClient().getCredentials();\r
- if (cred instanceof BearerCredentials) {\r
- am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ((BearerCredentials)cred).getAccessToken());\r
- } else {\r
- am.clearPassword(mAccount);\r
- }\r
- operation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); // need a new client instance, so avoid retry()\r
- // TODO si el usuario no se autoriza de nuevo, esto genera un bucle infinito; o un error en la creación del objecto cliente\r
- \r
- } else {\r
- if (operation instanceof RemoveFileOperation) {\r
- onRemoveFileOperationFinish((RemoveFileOperation)operation, result);\r
+ if (operation instanceof RemoveFileOperation) {\r
+ onRemoveFileOperationFinish((RemoveFileOperation)operation, result);\r
\r
- } else if (operation instanceof RenameFileOperation) {\r
- onRenameFileOperationFinish((RenameFileOperation)operation, result);\r
+ } else if (operation instanceof RenameFileOperation) {\r
+ onRenameFileOperationFinish((RenameFileOperation)operation, result);\r
\r
- } else if (operation instanceof SynchronizeFileOperation) {\r
- onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
- }\r
+ } else if (operation instanceof SynchronizeFileOperation) {\r
+ onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
}\r
}\r
\r
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
-import com.owncloud.android.network.OwnCloudClientUtils;
import com.owncloud.android.operations.OnRemoteOperationListener;
import com.owncloud.android.operations.RemoteOperation;
import com.owncloud.android.operations.RemoveFileOperation;
import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
-import eu.alefzero.webdav.WebdavClient;
import eu.alefzero.webdav.WebdavUtils;
import android.accounts.Account;