Example usage for android.accounts AuthenticatorException getLocalizedMessage

List of usage examples for android.accounts AuthenticatorException getLocalizedMessage

Introduction

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

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:com.ntsync.android.sync.syncadapter.SyncAdapter.java

private void performSync(Account account, Bundle extras, SyncResult syncResult,
        AccountSyncResult appSyncResult) {
    String authtoken = null;/*w  w  w  . j av a  2s.com*/
    try {

        long contactSyncMarker = getContactSyncMarker(account);
        long contactGroupMarker = getContactGroupSyncMarker(account);
        SyncAnchor syncAnchor = new SyncAnchor();

        if (contactSyncMarker == 0) {
            // By default, contacts from a 3rd party provider are hidden
            // in the contacts list.
            ContactManager.setAccountContactsVisibility(mContext, account, true);

            // Add Default Group, otherwise group selection is not
            // visible
            // in the default editor.
            ContactManager.ensureSampleGroupExists(mContext, account);
        }

        authtoken = NetworkUtilities.blockingGetAuthToken(mAccountManager, account, null);
        if (authtoken == null) {
            syncResult.stats.numIoExceptions++;
            appSyncResult.setState(SyncResultState.AUTH_FAILED);
            return;
        }

        boolean getRestrictions = extras != null ? extras.containsKey(Constants.PARAM_GETRESTRICTIONS) : false;
        Restrictions restr = SyncUtils.getRestrictions(account, mAccountManager);
        if (restr == null || getRestrictions) {
            Restrictions oldRestr = restr;
            restr = NetworkUtilities.getRestrictions(mContext, account, authtoken, mAccountManager);
            processRestrictions(oldRestr, restr, account);
            // Return-Value is not processed because we don't have to
            // restart for a full sync because all relevant data values will
            // be read after here.
        }

        PrivateKeyState readyState = ClientKeyHelper.isReadyForSync(mContext, account, mAccountManager,
                authtoken);
        boolean retryLater = false;
        switch (readyState) {
        case AUTH_FAILED:
            retryLater = true;
            appSyncResult.setState(SyncResultState.AUTH_FAILED);
            break;
        case CHECK_FAILED:
            // Retry later -> delay sync
            retryLater = true;
            appSyncResult.setState(SyncResultState.SERVER_ERROR);
            break;
        case NETWORK_ERROR:
            appSyncResult.setState(SyncResultState.NETWORK_ERROR);
            appSyncResult.setErrorMsg(readyState.getErrorMsg());
            retryLater = true;
            break;
        case MISSING_KEY:
            sendMissingKey(account, authtoken, readyState.getCurrSalt());
            appSyncResult.setState(SyncResultState.MISSING_KEY);
            return;
        default:
            clearMissingKeyNotification(account);
            break;
        }
        if (retryLater) {
            syncResult.delayUntil = SYNC_RETRY_DELAY;
            syncResult.fullSyncRequested = true;
            return;
        }

        SecretKey privKey = ClientKeyHelper.getOrCreatePrivateKey(account, mAccountManager);

        authtoken = checkIfSaltSaved(account, authtoken);
        if (authtoken == null) {
            syncResult.stats.numIoExceptions++;
            appSyncResult.setState(SyncResultState.AUTH_FAILED);
            return;
        }

        // Update ClientMod if not already set.
        ContactManager.updateClientModDate(mContext, account);
        // Get local Dirty Groups
        List<ContactGroup> dirtyGroups = ContactManager.getDirtyGroups(mContext, account, restr);
        boolean syncContacts = true;
        if (!dirtyGroups.isEmpty()) {
            // sync only Groups if some new groups are available, to
            // make
            // sure, that ids are available
            for (ContactGroup contactGroup : dirtyGroups) {
                if (contactGroup.getSourceId() == null) {
                    syncContacts = false;
                    break;
                }
            }
        }
        syncAnchor.setAnchor(ContactConstants.TYPE_CONTACTGROUP, contactGroupMarker);

        List<RawContact> dirtyContacts = null;
        Map<Long, String> newIdMap = null;
        if (syncContacts) {

            // Get local Dirty contacts
            dirtyContacts = ContactManager.getDirtyContacts(mContext, account, restr,
                    new SyncRestConflictHandler(account.name));

            newIdMap = ContactManager.getNewIdMap(mContext, account);
            syncAnchor.setAnchor(ContactConstants.TYPE_CONTACT, contactSyncMarker);
        }

        // Send the dirty contacts to the server, and retrieve the
        // server-side changes
        String saltStr = ClientKeyHelper.getSalt(account, mAccountManager);
        boolean explizitPhotoSave = getExplicitSavePhoto(account);
        SyncResponse result = NetworkUtilities.syncContacts(account, authtoken, syncAnchor, dirtyContacts,
                dirtyGroups, privKey, mAccountManager, mContext, syncResult, saltStr, newIdMap, restr,
                explizitPhotoSave);

        if (result.newGroupIdMap != null && !result.newGroupIdMap.isEmpty()) {
            if (Log.isLoggable(TAG, Log.INFO)) {
                Log.i(TAG, "Calling contactManager's set new GroupIds. Count Updates:"
                        + result.newGroupIdMap.size());
            }

            ContactManager.saveGroupIds(mContext, account.name, result.newGroupIdMap);
        }
        if (result.newContactIdMap != null && !result.newContactIdMap.isEmpty()) {
            if (Log.isLoggable(TAG, Log.INFO)) {
                Log.i(TAG, "Calling contactManager's set new ContactIds. Count Updates:"
                        + result.newContactIdMap.size());
            }

            ContactManager.saveContactIds(mContext, account.name, result.newContactIdMap);
        }

        Set<Long> updatedGroupIds = null;
        if (result.serverGroups != null && !result.serverGroups.isEmpty()) {
            // Update the local groups database with the changes.
            if (Log.isLoggable(TAG, Log.INFO)) {
                Log.i(TAG,
                        "Calling contactManager's update groups. Count Updates:" + result.serverGroups.size());
            }
            updatedGroupIds = ContactManager.updateGroups(mContext, account.name, result.serverGroups);
        }
        Set<Long> updatedContactIds = null;
        if (result.serverContacts != null && !result.serverContacts.isEmpty()) {
            // Update the local contacts database with the changes.
            if (Log.isLoggable(TAG, Log.INFO)) {
                Log.i(TAG, "Calling contactManager's update contacts. Count Updates:"
                        + result.serverContacts.size());
            }
            updatedContactIds = ContactManager.updateContacts(mContext, account.name, result.serverContacts,
                    true, restr);
        }

        SyncAnchor newSyncAnchor = result.newServerAnchor;
        if (newSyncAnchor != null) {
            setContactSyncMarker(account, newSyncAnchor.getAnchor(ContactConstants.TYPE_CONTACT));
            setContactGroupSyncMarker(account, newSyncAnchor.getAnchor(ContactConstants.TYPE_CONTACTGROUP));
        }

        if (result.syncstate != null) {
            switch (result.syncstate) {
            case INVALID_KEY:
                // Reset Key-Info && do FullSync
                mAccountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authtoken);
                ClientKeyHelper.clearPrivateKeyData(account, mAccountManager);
                ContactManager.setDirtyFlag(mContext, account);
                setContactSyncMarker(account, FULLSYNC_MARKER);
                setContactGroupSyncMarker(account, FULLSYNC_MARKER);
                syncResult.fullSyncRequested = true;
                appSyncResult.setState(SyncResultState.SUCCESS);
                return;
            case FORCE_FULLSYNC:
                ContactManager.setDirtyFlag(mContext, account);
                setContactSyncMarker(account, FULLSYNC_MARKER);
                setContactGroupSyncMarker(account, FULLSYNC_MARKER);
                syncResult.fullSyncRequested = true;
                appSyncResult.setState(SyncResultState.SUCCESS);
                return;
            default:
                LogHelper.logI(TAG, "Ignoring unknown SyncState:" + result.syncstate);
                break;
            }
        }

        boolean resync = processRestrictions(restr, result.restrictions, account);
        if (resync) {
            ContactManager.setDirtyFlag(mContext, account);
            syncResult.fullSyncRequested = true;
            appSyncResult.setState(SyncResultState.SUCCESS);
            return;
        }

        if (!dirtyGroups.isEmpty()) {
            ContactManager.clearGroupSyncFlags(mContext, dirtyGroups, result.newGroupIdMap, updatedGroupIds);
        }

        if (dirtyContacts != null && !dirtyContacts.isEmpty()) {
            ContactManager.clearSyncFlags(mContext, dirtyContacts, account.name, result.newContactIdMap,
                    updatedContactIds);
        }

        if (newIdMap != null && !newIdMap.isEmpty()) {
            ContactManager.clearServerId(mContext, newIdMap);
        }

        if (!syncContacts) {
            // if only groups were synced, restart for syncing contacts.
            syncResult.fullSyncRequested = true;
        }

        if (explizitPhotoSave) {
            // Clear Explizit Flag
            setExplicitSavePhoto(account, false);
        }

        appSyncResult.setState(SyncResultState.SUCCESS);
    } catch (final AuthenticatorException e) {
        LogHelper.logE(TAG, "AuthenticatorException", e);
        syncResult.stats.numParseExceptions++;
        setPrgErrorMsg(appSyncResult, e);
    } catch (final OperationCanceledException e) {
        LogHelper.logI(TAG, "OperationCanceledExcetpion", e);
        appSyncResult.setState(SyncResultState.AUTH_FAILED);
    } catch (final IOException e) {
        LogHelper.logE(TAG, "IOException", e);
        syncResult.stats.numIoExceptions++;
        setPrgErrorMsg(appSyncResult, e);
    } catch (final AuthenticationException e) {
        LogHelper.logI(TAG, "AuthenticationException", e);
        syncResult.stats.numAuthExceptions++;
        appSyncResult.setState(SyncResultState.AUTH_FAILED);
    } catch (final ParseException e) {
        LogHelper.logE(TAG, "ParseException", e);
        syncResult.stats.numParseExceptions++;
        setPrgErrorMsg(appSyncResult, e);
    } catch (InvalidKeyException e) {
        mAccountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authtoken);
        ClientKeyHelper.clearPrivateKeyData(account, mAccountManager);
        LogHelper.logW(TAG, "InvalidKeyException", e);
        syncResult.stats.numAuthExceptions++;
    } catch (NetworkErrorException e) {
        syncResult.stats.numIoExceptions++;
        LogHelper.logWCause(TAG, "Sync failed because of a NetworkException", e);
        appSyncResult.setState(SyncResultState.NETWORK_ERROR);
        appSyncResult.setErrorMsg(String.format(getText(R.string.sync_network_error), e.getLocalizedMessage()));
    } catch (ServerException e) {
        syncResult.stats.numIoExceptions++;
        LogHelper.logWCause(TAG, "Sync failed because of a ServerException", e);
        appSyncResult.setState(SyncResultState.SERVER_ERROR);
        appSyncResult.setErrorMsg(getText(R.string.sync_server_error));
    } catch (OperationApplicationException e) {
        LogHelper.logW(TAG, "Sync failed because a DB-Operation failed", e);
        syncResult.databaseError = true;
        setPrgErrorMsg(appSyncResult, e);
    } catch (HeaderParseException e) {
        syncResult.stats.numIoExceptions++;
        LogHelper.logWCause(TAG, "Sync failed because of server reponse could not be parsed", e);
        setPrgErrorMsg(appSyncResult, e);
    } catch (HeaderCreateException e) {
        syncResult.databaseError = true;
        LogHelper.logE(TAG, "Sync failed because header could not be created", e);
        setPrgErrorMsg(appSyncResult, e);
    }
}