Example usage for android.accounts AccountManager get

List of usage examples for android.accounts AccountManager get

Introduction

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

Prototype

public static AccountManager get(Context context) 

Source Link

Document

Gets an AccountManager instance associated with a Context.

Usage

From source file:Main.java

public static void invalidateAuthToken(final Context context) {
    AccountManager am = AccountManager.get(context);
    am.invalidateAuthToken(ACCOUNT_TYPE, getAuthToken(context));
    setAuthToken(context, null);/* www.  ja  v  a 2s. co m*/
}

From source file:Main.java

@SuppressLint({ "MissingPermission" })
public static void addOnAccountsUpdatedListener(Context context, final OnAccountsUpdateListener listener,
        Handler handler, boolean updateImmediately) {
    AccountManager accountManager = AccountManager.get(context);
    accountManager.addOnAccountsUpdatedListener(listener, handler, updateImmediately);
}

From source file:Main.java

/**
 * Gets an instance of an AccountManager to use for authorizing the app
 *
 * @param applicationContext//from  w  ww.  j  a  va  2 s .  co  m
 * @return
 */
public static AccountManager GetAccountManager(Context applicationContext) {
    return AccountManager.get(applicationContext);
}

From source file:Main.java

@SuppressWarnings("MissingPermission")
public static String[] getGoogleAccounts(Context ctx) {
    if (ctx.checkCallingOrSelfPermission(
            Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
        Account[] accounts = AccountManager.get(ctx).getAccountsByType("com.google");
        String[] result = new String[accounts.length];
        for (int i = 0; i < accounts.length; i++) {
            result[i] = accounts[i].name;
        }//from  ww  w.j a v  a 2 s.  c o m
        return result;
    }
    return null;
}

From source file:Main.java

/**
 * Invalidate auth token for specified account
 * @param account account to invalidate auth token
 * @param authTokenType auth token type//ww w  . j av a  2s  . c  om
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void invalidateAuthToken(Account account, String authTokenType, String[] requiredFeatures,
        Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    if (account == null) {
        throw new IllegalArgumentException("account cannot be null");
    }
    Context context = activity.getApplicationContext();
    final AccountManager am = AccountManager.get(context);
    String authToken = am.peekAuthToken(account, authTokenType);
    if (!TextUtils.isEmpty(authToken)) {
        am.invalidateAuthToken(account.type, authToken);
    }
    am.addAccount(account.type, authTokenType, requiredFeatures, options, activity, null, null);
}

From source file:Main.java

/**
 * Retrieves a list of e-mails on the device
 *
 * @return A list of emails//from  w  ww .j a v  a 2 s  . c  om
 */
public static List<String> getAccountEmails(Context context) {

    final Set<String> emailSet = new HashSet<String>();
    final Account[] accounts = AccountManager.get(context).getAccounts();

    for (Account account : accounts) {
        if (isEmail(account.name)) {
            emailSet.add(account.name);
        }
    }

    return new ArrayList<String>(emailSet);
}

From source file:Main.java

public static String getUserEmail(Context context) {
    if (context == null) {
        return null;
    }//from w ww .  java 2  s  .c  om
    Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");
    if (accounts.length > 0) {
        return accounts[0].name;
    }
    return null;
}

From source file:Main.java

public static String refreshAuthToken(Activity activity, String token, String name, String googleApi) {
    String authToken = null;//from  w w  w.j a v  a2  s  .  c  o  m
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    try {
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, token);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

From source file:Main.java

/**
 * Check if the specified account exist//  w  w w .  j a  va 2s .c o m
 * @param context context
 * @param account account to check
 * @return <code>true</code> if account exist
 */
public static boolean isAccountExist(Context context, Account account) {
    if (account == null)
        return false;
    final AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(account.type);
    if (accounts == null || accounts.length == 0) {
        return false;
    } else {
        for (Account a : accounts) {
            if (a.equals(account))
                return true;
        }
        return false;
    }
}

From source file:Main.java

private static Account[] getAccounts(final Context context) {
    return AccountManager.get(context).getAccounts();
}