Example usage for android.accounts AccountsException AccountsException

List of usage examples for android.accounts AccountsException AccountsException

Introduction

In this page you can find the example usage for android.accounts AccountsException AccountsException.

Prototype

public AccountsException(String message, Throwable cause) 

Source Link

Usage

From source file:com.owncloud.android.oc_framework.operations.RemoteOperation.java

/**
 * Asynchronous execution of the operation 
 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}, 
 * and result posting./*from  ww w.j a  v a2s.c  o  m*/
 * 
 * TODO refactor && clean the code; now it's a mess
 */
@Override
public final void run() {
    RemoteOperationResult result = null;
    boolean repeat = false;
    do {
        try {
            if (mClient == null) {
                if (mAccount != null && mContext != null) {
                    if (mCallerActivity != null) {
                        mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext,
                                mCallerActivity);
                    } else {
                        mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext);
                    }
                } 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 (AccountsException e) {
            Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
            result = new RemoteOperationResult(e);
        }

        if (result == null)
            result = run(mClient);

        repeat = false;
        if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() &&
        //                    (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
                (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) {
            /// possible fail due to lack of authorization in an operation performed in foreground
            Credentials cred = mClient.getCredentials();
            String ssoSessionCookie = mClient.getSsoSessionCookie();
            if (cred != null || ssoSessionCookie != null) {
                /// confirmed : unauthorized operation
                AccountManager am = AccountManager.get(mContext);
                boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials);
                boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null);
                if (bearerAuthorization) {
                    am.invalidateAuthToken(mAccount.type, ((BearerCredentials) cred).getAccessToken());
                } else if (samlBasedSsoAuthorization) {
                    am.invalidateAuthToken(mAccount.type, ssoSessionCookie);
                } else {
                    am.clearPassword(mAccount);
                }
                mClient = null;
                repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
                result = null;
            }
        }
    } while (repeat);

    final RemoteOperationResult resultToSend = result;
    if (mListenerHandler != null && mListener != null) {
        mListenerHandler.post(new Runnable() {
            @Override
            public void run() {
                mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
            }
        });
    }
}

From source file:com.owncloud.android.operations.RemoteOperation.java

/**
 * Asynchronous execution of the operation 
 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}, 
 * and result posting.//from   ww w.  ja v  a  2  s  .c om
 * 
 * TODO refactor && clean the code; now it's a mess
 */
@Override
public final void run() {
    RemoteOperationResult result = null;
    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 {
                    throw new IllegalStateException(
                            "Trying to run a remote operation asynchronously with no client instance or account");
                }
            }

        } catch (IOException e) {
            Log_OC.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_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
            result = new RemoteOperationResult(e);
        }

        if (result == null)
            result = run(mClient);

        repeat = false;
        if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() &&
        //                    (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
                (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) {
            /// possible fail due to lack of authorization in an operation performed in foreground
            Credentials cred = mClient.getCredentials();
            String ssoSessionCookie = mClient.getSsoSessionCookie();
            if (cred != null || ssoSessionCookie != null) {
                /// confirmed : unauthorized operation
                AccountManager am = AccountManager.get(mContext);
                boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials);
                boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null);
                if (bearerAuthorization) {
                    am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE,
                            ((BearerCredentials) cred).getAccessToken());
                } else if (samlBasedSsoAuthorization) {
                    am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ssoSessionCookie);
                } else {
                    am.clearPassword(mAccount);
                }
                mClient = null;
                repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
                result = null;
            }
        }
    } while (repeat);

    final RemoteOperationResult resultToSend = result;
    if (mListenerHandler != null && mListener != null) {
        mListenerHandler.post(new Runnable() {
            @Override
            public void run() {
                mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
            }
        });
    }
}