Example usage for android.accounts AccountManager blockingGetAuthToken

List of usage examples for android.accounts AccountManager blockingGetAuthToken

Introduction

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

Prototype

public String blockingGetAuthToken(Account account, String authTokenType, boolean notifyAuthFailure)
        throws OperationCanceledException, IOException, AuthenticatorException 

Source Link

Document

This convenience helper synchronously gets an auth token with #getAuthToken(Account,String,boolean,AccountManagerCallback,Handler) .

Usage

From source file:org.klnusbaum.udj.PlaylistLoader.java

private PlaylistResult attemptLoad(boolean attemptReauth) {
    AccountManager am = AccountManager.get(getContext());
    String authToken = "";
    try {//from   w ww .j a v  a2 s. c o  m
        authToken = am.blockingGetAuthToken(account, "", true);
    } catch (IOException e) {
        //TODO this might actually be an auth error
        return new PlaylistResult(null, PlaylistLoadError.AUTHENTICATION_ERROR);
    } catch (AuthenticatorException e) {
        return new PlaylistResult(null, PlaylistLoadError.AUTHENTICATION_ERROR);
    } catch (OperationCanceledException e) {
        return new PlaylistResult(null, PlaylistLoadError.AUTHENTICATION_ERROR);
    }

    try {
        String playerId = am.getUserData(account, Constants.LAST_PLAYER_ID_DATA);
        JSONObject serverResult = ServerConnection.getActivePlaylist(playerId, authToken);
        List<ActivePlaylistEntry> toReturn = RESTProcessor.processActivePlaylist(serverResult, am, account,
                context);
        return new PlaylistResult(toReturn);
    } catch (JSONException e) {
        return new PlaylistResult(null, PlaylistLoadError.SERVER_ERROR);
    } catch (ParseException e) {
        return new PlaylistResult(null, PlaylistLoadError.SERVER_ERROR);
    } catch (IOException e) {
        return new PlaylistResult(null, PlaylistLoadError.SERVER_ERROR);
    } catch (AuthenticationException e) {
        if (attemptReauth) {
            Log.d(TAG, "soft auth failure");
            am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
            return attemptLoad(false);
        } else {
            Log.d(TAG, "hard auth failure");
            return new PlaylistResult(null, PlaylistLoadError.AUTHENTICATION_ERROR);
        }
    } catch (PlayerInactiveException e) {
        return new PlaylistResult(null, PlaylistLoadError.PLAYER_INACTIVE_ERROR);
    } catch (NoLongerInPlayerException e) {
        return new PlaylistResult(null, PlaylistLoadError.NO_LONGER_IN_PLAYER_ERROR);
    } catch (KickedException e) {
        return new PlaylistResult(null, PlaylistLoadError.KICKED_ERROR);
    }
}

From source file:org.klnusbaum.udj.MusicSearchLoader.java

private MusicSearchResult attemptSearch(boolean attemptReauth) {
    AccountManager am = AccountManager.get(getContext());
    String authToken = "";
    try {// w  w w.  j av  a2 s  .  c  om
        authToken = am.blockingGetAuthToken(account, "", true);
    } catch (IOException e) {
        //TODO this might actually be an auth error
        return new MusicSearchResult(null, MusicSearchError.AUTHENTICATION_ERROR);
    } catch (AuthenticatorException e) {
        return new MusicSearchResult(null, MusicSearchError.AUTHENTICATION_ERROR);
    } catch (OperationCanceledException e) {
        return new MusicSearchResult(null, MusicSearchError.AUTHENTICATION_ERROR);
    }

    try {
        String playerId = am.getUserData(account, Constants.LAST_PLAYER_ID_DATA);
        return doSearch(playerId, authToken);
    } catch (JSONException e) {
        return new MusicSearchResult(null, MusicSearchError.SERVER_ERROR);
    } catch (ParseException e) {
        return new MusicSearchResult(null, MusicSearchError.SERVER_ERROR);
    } catch (IOException e) {
        return new MusicSearchResult(null, MusicSearchError.SERVER_ERROR);
    } catch (AuthenticationException e) {
        if (attemptReauth) {
            Log.d(TAG, "soft auth failure");
            am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
            return attemptSearch(false);
        } else {
            Log.d(TAG, "hard auth failure");
            return new MusicSearchResult(null, MusicSearchError.AUTHENTICATION_ERROR);
        }
    } catch (PlayerInactiveException e) {
        return new MusicSearchResult(null, MusicSearchError.PLAYER_INACTIVE_ERROR);
    } catch (NoLongerInPlayerException e) {
        return new MusicSearchResult(null, MusicSearchError.NO_LONGER_IN_PLAYER_ERROR);
    } catch (KickedException e) {
        return new MusicSearchResult(null, MusicSearchError.KICKED_ERROR);
    }
}

From source file:com.mrcaps.taskswidget.TasksHelper.java

public static void refreshAuthTokenImpl5(final Context context, final boolean invalidate,
        final Runnable continuation) {
    final Handler handl = getHandler();

    new Thread(new Runnable() {
        public void run() {
            AccountManager mgr = AccountManager.get(context);
            Account[] accts = mgr.getAccountsByType(ACCOUNT_TYPE);
            if (accts.length < 1) {
                handl.post(new Runnable() {
                    public void run() {
                        Toast.makeText(context, "Error: could not find a Google account on this phone",
                                Toast.LENGTH_SHORT).show();
                    }//w w w .j  a  va2s .  c o m
                });
                return;
            }

            Account acct = accts[0];

            if (invalidate) {
                long now = System.currentTimeMillis();
                Log.v(TAG, "I5 Auth Token Invalidation Requested");
                if (now - lastInvalidation > invalidationDelta) {
                    Log.v(TAG, "I5 Invalidating Auth Token");
                    try {
                        String token = mgr.blockingGetAuthToken(acct, SERVICE_NAME, true);
                        mgr.invalidateAuthToken(ACCOUNT_TYPE, token);
                    } catch (Exception e) {
                        Log.e(TAG, "I5 couldn't invalidate token", e);
                    }
                }
            }

            try {
                String token = mgr.blockingGetAuthToken(acct, SERVICE_NAME, true);
                Log.v(TAG, "I5 got auth token: " + token);
                authToken = token;
            } catch (Exception e) {
                Log.e(TAG, "I5 couldn't authenticate", e);
                handl.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "Error: I5 could not authenticate with Google account",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

            continuation.run();
        }
    }).start();
}

From source file:net.translatewiki.app.TranslateWikiApp.java

public Boolean revalidateAuthToken() {
    AccountManager accountManager = AccountManager.get(this);
    Account curAccount = getCurrentAccount();

    if (curAccount == null) {
        return false; // This should never happen
    }//w w w . j  a  va2  s  .com

    accountManager.invalidateAuthToken(getString(R.string.account_type_identifier), api.getAuthCookie());
    try {
        String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
        api.setAuthCookie(authCookie);
        return true;
    } catch (OperationCanceledException e) {
        e.printStackTrace();
        return false;
    } catch (AuthenticatorException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:net.vleu.par.android.rpc.Transceiver.java

/**
 * This method may block while a network request completes, and must never
 * be made from the main thread. It requests a new GoogleAuthToken.
 * //from w  w w.  ja va  2 s .  c o  m
 * @param account
 *            The account to fetch an auth token for
 * @return A token associated with this account
 * @throws OperationCanceledException
 *             if the request was canceled for any reason, including the
 *             user canceling a credential request
 * @throws AuthenticatorException
 *             if the authenticator failed to respond
 * @throws IOException
 *             if the authenticator experienced an I/O problem creating a
 *             new auth token, usually because of network trouble
 */
private GoogleAuthToken blockingGetNewAuthToken()
        throws OperationCanceledException, AuthenticatorException, IOException {
    final AccountManager am = AccountManager.get(this.context);
    final String authTokenStr = am.blockingGetAuthToken(this.account, APPENGINE_TOKEN_TYPE, true);
    if (Log.isLoggable(TAG, Log.INFO))
        Log.i(TAG, "Got a new GoogleAuthToken for account: " + this.account.name + ": " + authTokenStr);
    if (authTokenStr == null)
        throw new AuthenticatorException("Could not get an auth token");
    else
        return new GoogleAuthToken(authTokenStr);
}

From source file:dev.drsoran.moloko.sync.SyncAdapter.java

private String checkAccount(Account account) {
    final AccountManager accountManager = AccountManager.get(context);

    final String authToken;
    try {/*from w  ww .  j  a  v a  2s . c  om*/
        authToken = accountManager.blockingGetAuthToken(account, Constants.AUTH_TOKEN_TYPE,
                true /* notifyAuthFailure */);
    } catch (OperationCanceledException e) {
        throw new SyncException(e);
    } catch (AuthenticatorException e) {
        ++syncResult.stats.numAuthExceptions;
        throw new SyncException(e);
    } catch (IOException e) {
        ++syncResult.stats.numIoExceptions;
        throw new SyncException(e);
    }

    return authToken;
}

From source file:org.klnusbaum.udj.network.PlaylistSyncService.java

private void setCurrentSong(Account account, String playerId, String libId, boolean attemptReauth,
        Intent originalIntent) {//from ww w.  j  a v a2s  . co  m
    String authToken = "";
    AccountManager am = AccountManager.get(this);
    try {
        authToken = am.blockingGetAuthToken(account, "", true);
    } catch (AuthenticatorException e) {
        alertSetSongException(account, originalIntent);
        Log.e(TAG, "Authentication exception when setting song");
    } catch (OperationCanceledException e) {
        alertSetSongException(account, originalIntent);
        Log.e(TAG, "Op Canceled exception when setting song");
    } catch (IOException e) {
        alertSetSongException(account, originalIntent);
        Log.e(TAG, "IO exception when geting authtoken for setting song");
        Log.e(TAG, e.getMessage());
    }

    try {
        ServerConnection.setCurrentSong(playerId, libId, authToken);
        Intent setCurrentComplete = new Intent(Constants.BROADCAST_SET_CURRENT_COMPLETE);
        this.sendBroadcast(setCurrentComplete);
    } catch (IOException e) {
        alertSetSongException(account, originalIntent);
        Log.e(TAG, "IO exception when setting song");
        Log.e(TAG, e.getMessage());
    } catch (AuthenticationException e) {
        if (attemptReauth) {
            am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
            addSongToPlaylist(account, playerId, libId, false, originalIntent);
            Log.e(TAG, "Soft Authentication exception when setting song");
        } else {
            alertSetSongException(account, originalIntent);
            Log.e(TAG, "Hard Authentication exception when setting song");
        }
    } catch (PlayerInactiveException e) {
        Log.e(TAG, "Event over exceptoin when setting song");
        Utils.handleInactivePlayer(this, account);
    } catch (NoLongerInPlayerException e) {
        Utils.handleNoLongerInPlayer(this, account);
    } catch (KickedException e) {
        Utils.handleKickedFromPlayer(this, account);
    }
}

From source file:org.klnusbaum.udj.network.PlaylistSyncService.java

private void removeSongFromPlaylist(Account account, String playerId, String libId, boolean attemptReauth,
        Intent originalIntent) {//  w  w  w. j  a va2 s. co  m
    String authToken = "";
    AccountManager am = AccountManager.get(this);
    try {
        authToken = am.blockingGetAuthToken(account, "", true);
    } catch (AuthenticatorException e) {
        alertRemoveSongException(account, originalIntent);
        Log.e(TAG, "Authentication exception when removing from playist");
    } catch (OperationCanceledException e) {
        alertRemoveSongException(account, originalIntent);
        Log.e(TAG, "Op Canceled exception when removing from playist");
    } catch (IOException e) {
        alertRemoveSongException(account, originalIntent);
        Log.e(TAG, "IO exception when removing from playist/getting authtoken");
        Log.e(TAG, e.getMessage());
    }

    try {
        Log.d(TAG, "Actually removing song");
        ServerConnection.removeSongFromActivePlaylist(playerId, libId, authToken);
        Intent removeSongComplete = new Intent(Constants.BROADCAST_REMOVE_SONG_COMPLETE);
        this.sendBroadcast(removeSongComplete);
    } catch (ParseException e) {
        alertRemoveSongException(account, originalIntent);
        Log.e(TAG, "Parse exception when removing from playist");
    } catch (IOException e) {
        alertRemoveSongException(account, originalIntent);
        Log.e(TAG, "IO exception when removing from playist");
        Log.e(TAG, e.getMessage());
    } catch (AuthenticationException e) {
        if (attemptReauth) {
            am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
            removeSongFromPlaylist(account, playerId, libId, false, originalIntent);
            Log.e(TAG, "Soft Authentication exception when removing from playist");
        } else {
            alertRemoveSongException(account, originalIntent);
            Log.e(TAG, "Hard Authentication exception when removing from playist");
        }
    } catch (PlayerInactiveException e) {
        Log.e(TAG, "Event over exceptoin when removing from playlist");
        Utils.handleInactivePlayer(this, account);
    } catch (NoLongerInPlayerException e) {
        Utils.handleNoLongerInPlayer(this, account);
    } catch (KickedException e) {
        Utils.handleKickedFromPlayer(this, account);
    }
}

From source file:org.klnusbaum.udj.network.PlaylistSyncService.java

private void addSongToPlaylist(Account account, String playerId, String libId, boolean attemptReauth,
        Intent originalIntent) {/* ww w  . j  a v  a 2s.  c  o  m*/
    String authToken = "";
    AccountManager am = AccountManager.get(this);
    try {
        authToken = am.blockingGetAuthToken(account, "", true);
    } catch (AuthenticatorException e) {
        alertAddSongException(account, originalIntent);
        Log.e(TAG, "Authentication exception when adding to playist");
    } catch (OperationCanceledException e) {
        alertAddSongException(account, originalIntent);
        Log.e(TAG, "Op Canceled exception when adding to playist");
    } catch (IOException e) {
        alertAddSongException(account, originalIntent);
        Log.e(TAG, "IO exception when geting authtoken for adding to playist");
        Log.e(TAG, e.getMessage());
    }

    try {
        ServerConnection.addSongToActivePlaylist(playerId, libId, authToken);
    } catch (JSONException e) {
        alertAddSongException(account, originalIntent);
        Log.e(TAG, "JSON exception when adding to playist");
    } catch (ParseException e) {
        alertAddSongException(account, originalIntent);
        Log.e(TAG, "Parse exception when adding to playist");
    } catch (IOException e) {
        alertAddSongException(account, originalIntent);
        Log.e(TAG, "IO exception when adding to playist");
        Log.e(TAG, e.getMessage());
    } catch (AuthenticationException e) {
        if (attemptReauth) {
            am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
            addSongToPlaylist(account, playerId, libId, false, originalIntent);
            Log.e(TAG, "Soft Authentication exception when adding to playist");
        } else {
            alertAddSongException(account, originalIntent);
            Log.e(TAG, "Hard Authentication exception when adding to playist");
        }
    } catch (PlayerInactiveException e) {
        Log.e(TAG, "Event over exceptoin when retreiving playlist");
        Utils.handleInactivePlayer(this, account);
    } catch (NoLongerInPlayerException e) {
        Utils.handleNoLongerInPlayer(this, account);
    } catch (KickedException e) {
        Utils.handleKickedFromPlayer(this, account);
    }

}