Example usage for android.accounts NetworkErrorException NetworkErrorException

List of usage examples for android.accounts NetworkErrorException NetworkErrorException

Introduction

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

Prototype

public NetworkErrorException() 

Source Link

Usage

From source file:org.wheelmap.android.net.TotalNodeCountExecutor.java

@Override
public void execute(long id) throws RestServiceException {
    TotalNodeCountRequestBuilder requestBuilder = null;

    requestBuilder = new TotalNodeCountRequestBuilder(getServer(), getApiKey(), AcceptType.JSON);

    try {/*  www  .  j a  v  a2s . c o m*/
        String request = UriUtils.encodeQuery(requestBuilder.buildRequestUri(), "utf-8");

        long countTotal = requestNodeCountForURL(request);
        long countUnknown = requestNodeCountForURL(request + "&wheelchair=unknown");

        if (countTotal != -1 && countUnknown != -1 && countTotal > countUnknown) {
            long count = countTotal - countUnknown;
            WheelmapApp.getDefaultPrefs().edit().putLong("ItemCountTotal", count).commit();
        }

    } catch (Exception e) {
        e.printStackTrace();
        processException(RestServiceException.ERROR_NETWORK_FAILURE, new NetworkErrorException(), false);
    }
}

From source file:pt.up.mobile.authenticator.Authenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");

    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(Constants.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }/* www . ja va2s  . c om*/
    try {
        final AccountManager am = AccountManager.get(mContext);
        final String peek = am.peekAuthToken(account, authTokenType);
        if (peek != null) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            result.putString(AccountManager.KEY_AUTHTOKEN, peek);
            return result;
        }
        // Extract the username and password from the Account Manager, and
        // ask
        // the server for an appropriate AuthToken.
        final String password = am.getPassword(account);
        if (password != null) {
            String[] reply;

            reply = SifeupAPI.authenticate(account.name, password, mContext);
            final String authToken = reply[1];
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        }

    } catch (AuthenticationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        throw new NetworkErrorException();
    }
    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity panel.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true);
    intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:org.ohmage.auth.Authenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {

    // Extract the email and refresh_token from the Account Manager, and ask
    // the server for a new refresh_token.
    String authToken = am.peekAuthToken(account, authTokenType);

    // Lets give another try to authenticate the user
    AccessToken token = null;//from   www  . j  a va  2s. c  o  m
    UserRecoverableAuthException userRecoverableAuthException = null;

    if (TextUtils.isEmpty(authToken)) {
        final String refreshToken = am.getPassword(account);
        Log.i(TAG, "Auth token is null");
        if (refreshToken != null) {
            Log.i(TAG, "Refresh Token");
            try {
                // If the account credentials have not gone to the server yet we saved the
                // password for the user
                if (Boolean.parseBoolean(am.getUserData(account, USE_PASSWORD))) {
                    token = ohmageService.getAccessToken(account.name, refreshToken);
                    if (token != null) {
                        am.setUserData(account, Authenticator.USE_PASSWORD, String.valueOf(false));
                        am.setUserData(account, Authenticator.USER_ID, token.getUserId());
                    }
                } else {
                    // refresh token
                    if (Ohmage.USE_DSU_DATAPOINTS_API) {

                        Log.i(TAG, "Refresh Token with DSU");
                        token = ohmageService.refreshAccessToken(refreshToken, "refresh_token");
                    } else {
                        token = ohmageService.getAccessToken(refreshToken);
                    }
                }
            } catch (AuthenticationException e) {
                // This will happen if the refresh token was already used, or it was
                // invalidated or something

                // We can try getting the token from google
                String googleAccount = am.getUserData(account, USER_DATA_GOOGLE_ACCOUNT);
                if (googleAccount != null) {
                    try {
                        token = getTokenFromGoogle(googleAccount);
                        Log.i(TAG, token.toString());
                    } catch (UserRecoverableAuthException e1) {
                        userRecoverableAuthException = e1;
                    }
                }
            } catch (RetrofitError e) {
                if (e.getResponse() != null && e.getResponse().getStatus() == 409) {
                    // The user hasn't activated their account by clicking the link
                    final Bundle bundle = new Bundle();
                    bundle.putString(AccountManager.KEY_AUTH_FAILED_MESSAGE,
                            Ohmage.app().getString(R.string.account_not_activated));
                    bundle.putParcelable(AccountManager.KEY_INTENT,
                            new Intent(mContext, AccountNotActivatedDialog.class));
                    return bundle;
                } else {
                    throw new NetworkErrorException();
                }
            } catch (Exception e) {
                Log.e(TAG, "", e);
            }
        }
    }

    // If we get an authToken - we return it
    if (token != null) {
        am.setPassword(account, token.getRefreshToken());
        authToken = token.getAccessToken();
    }

    if (!TextUtils.isEmpty(authToken)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        return result;
    }

    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    intent.putExtra(AuthenticatorActivity.EXTRA_FROM_AUTHENTICATOR, true);
    intent.putExtra(AuthenticatorActivity.EXTRA_HANDLE_USER_RECOVERABLE_ERROR, userRecoverableAuthException);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:org.ohmage.auth.Authenticator.java

private AccessToken getTokenFromGoogle(String googleAccount)
        throws NetworkErrorException, UserRecoverableAuthException {
    // We can check for a google account to automatically update the refresh token

    try {/*from   w  w  w.  java  2 s  .  c om*/
        String googleToken = authHelper.googleAuthGetAccessToken(googleAccount);
        return ohmageService.getAccessTokenWithGoogleAccessToken(AuthUtil.OMH_CLIENT_ID,
                AuthUtil.OMH_CLIENT_SECRET, googleToken);

    } catch (UserRecoverableAuthException userAuthEx) {
        throw userAuthEx;
    } catch (GoogleAuthException authEx) {
        // We can't really deal with this.. hopefully it doesn't happen
    } catch (RetrofitError e) {
        if (e.isNetworkError()) {
            throw new NetworkErrorException();
        }
        //TODO: what should we catch for retrofit exceptions?
    } catch (IOException transientEx) {
        throw new NetworkErrorException();
    } catch (AuthenticationException e) {
        //TODO: what should we do if the token is invalid?
    }
    return null;
}

From source file:no.firestorm.weathernotificatonservice.WeatherNotificationService.java

/**
 * Update the temperature/* w w w. j a  v a2  s .  c  om*/
 */
void showTemp() {
    // Stops
    if (!CheckInternetStatus.canConnectToInternet(this)) {
        addConnectionChangedReceiver();
        makeNotification(new NetworkErrorException());
    }

    try {
        final WeatherElement returnValue = getWeatherData();
        makeNotification(returnValue);
    } catch (final NetworkErrorException e) {
        makeNotification(e);
    } catch (final HttpException e) {
        makeNotification(e);
    } catch (final NoLocationException e) {
        makeNotification(e);
    }

}