1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-07-17 15:05:42 +00:00

Create new method to retry synchronous and asynchronous remote operations

This commit is contained in:
davigonz 2017-08-02 12:25:38 +02:00
parent 7f98b3804c
commit c04f6e3463

View File

@ -134,7 +134,8 @@ public abstract class RemoteOperation implements Runnable {
Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e); Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
return new RemoteOperationResult(e); return new RemoteOperationResult(e);
} }
return run(mClient);
return runOperationRetryingItIfNeeded();
} }
@ -152,7 +153,8 @@ public abstract class RemoteOperation implements Runnable {
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " +
"OwnCloudClient"); "OwnCloudClient");
mClient = client; mClient = client;
return run(client);
return runOperationRetryingItIfNeeded();
} }
@ -223,7 +225,6 @@ public abstract class RemoteOperation implements Runnable {
mListenerHandler = listenerHandler; mListenerHandler = listenerHandler;
} }
Thread runnerThread = new Thread(this); Thread runnerThread = new Thread(this);
runnerThread.start(); runnerThread.start();
return runnerThread; return runnerThread;
@ -238,9 +239,37 @@ public abstract class RemoteOperation implements Runnable {
*/ */
@Override @Override
public final void run() { public final void run() {
if (mAccount != null && mContext != null) {
// Save Client Cookies
AccountUtils.saveClient(mClient, mAccount, mContext);
}
final RemoteOperationResult resultToSend = runOperationRetryingItIfNeeded();;
if (mListenerHandler != null && mListener != null) {
mListenerHandler.post(new Runnable() {
@Override
public void run() {
mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
});
} else if (mListener != null) {
mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
}
/**
* Run operation after asynchronous or synchronous executions. If the account credentials are
* invalidated, the operation will be retried with new valid credentials
*
* @return remote operation result
*/
private RemoteOperationResult runOperationRetryingItIfNeeded () {
RemoteOperationResult result; RemoteOperationResult result;
boolean repeat; boolean repeat;
int repeatCounter = 0; int repeatCounter = 0;
do { do {
repeat = false; repeat = false;
@ -256,7 +285,7 @@ public abstract class RemoteOperation implements Runnable {
if (shouldInvalidateAccountCredentials(result)) { if (shouldInvalidateAccountCredentials(result)) {
boolean invalidated = invalidateAccountCredentials(); boolean invalidated = invalidateAccountCredentials();
if (invalidated && if (invalidated &&
mClient.getCredentials().authTokenCanBeRefreshed() && mClient.getCredentials().authTokenCanBeRefreshed() &&
repeatCounter < MAX_REPEAT_COUNTER) { repeatCounter < MAX_REPEAT_COUNTER) {
mClient = null; mClient = null;
@ -274,24 +303,10 @@ public abstract class RemoteOperation implements Runnable {
} while (repeat); } while (repeat);
if (mAccount != null && mContext != null) { return result;
// Save Client Cookies
AccountUtils.saveClient(mClient, mAccount, mContext);
}
final RemoteOperationResult resultToSend = result;
if (mListenerHandler != null && mListener != null) {
mListenerHandler.post(new Runnable() {
@Override
public void run() {
mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
});
} else if (mListener != null) {
mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
} }
private void grantOwnCloudClient() throws private void grantOwnCloudClient() throws
AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
if (mClient == null) { if (mClient == null) {