Example usage for android.accounts AccountManager getAccounts

List of usage examples for android.accounts AccountManager getAccounts

Introduction

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

Prototype

@NonNull
public Account[] getAccounts() 

Source Link

Document

Lists all accounts visible to the caller regardless of type.

Usage

From source file:Main.java

public static Account getAccountFromAccountName(AccountManager accountManager, String accountName) {
    Account accounts[] = accountManager.getAccounts();
    for (int i = 0; i < accounts.length; ++i)
        if (accountName.equals(accounts[i].name))
            return accounts[i];
    return null;//w ww .j  av  a 2 s.c  o  m
}

From source file:Main.java

public static Account[] getAccounts(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = null;/*  w ww.  ja va2 s . c om*/
    try {
        accounts = accountManager.getAccounts();
    } catch (Exception e) {
    }
    return accounts;
}

From source file:Main.java

/**
 * @param context//from   w w w .  j  av a 2 s . c  o m
 * this method is used for  retrieving  email accounts of device
 * @return
 */
public static String[] getAccount(Context context) {
    final AccountManager accountManager = AccountManager.get(context);
    final Account[] accounts = accountManager.getAccounts();
    final Vector<String> accountVector = new Vector<String>();
    for (int i = 0; i < accounts.length; i++) {
        if (!accountVector.contains(accounts[i].name) && isValidEmail(accounts[i].name)) {
            accountVector.addElement(accounts[i].name);
        }
    }

    final String accountArray[] = new String[accountVector.size()];
    return accountVector.toArray(accountArray);
}

From source file:org.qeo.deviceregistration.service.RegisterService.java

/**
 * Get the default username for registering.
 *
 * @param ctx The android context./* w ww  .ja  v a2s  .com*/
 * @return A default username to be used for registering.
 */
public static String getUserName(Context ctx) {
    AccountManager manager = (AccountManager) ctx.getSystemService(ContextWrapper.ACCOUNT_SERVICE);
    Account[] list = manager.getAccounts();
    String account = "";

    for (Account acc : list) {
        if (acc.type.equalsIgnoreCase("com.google")) {
            account = acc.name;
            break;
        }
    }
    if (account.isEmpty()) {
        account = "User_" + DeviceInfoAndroid.getInstance().getDeviceInfo().userFriendlyName;
    }

    return account;
}

From source file:com.example.jumpnote.android.SyncAdapter.java

public static void clearSyncData(Context context) {
    AccountManager am = AccountManager.get(context);
    Account[] accounts = am.getAccounts();
    for (Account account : accounts) {
        final SharedPreferences syncMeta = context.getSharedPreferences("sync:" + account.name, 0);
        syncMeta.edit().clear().commit();
    }/*from  ww  w. j a v a2 s.co m*/
}

From source file:ir.keloud.android.lib.common.accounts.AccountUtils.java

/**
 * Restore the client cookies from accountName
 * @param accountName/*from w ww.  java 2s.c om*/
 * @param client
 * @param context
 */
public static void restoreCookies(String accountName, KeloudClient client, Context context) {
    Log_OC.d(TAG, "Restoring cookies for " + accountName);

    // Account Manager
    AccountManager am = AccountManager.get(context.getApplicationContext());

    // Get account
    Account account = null;
    Account accounts[] = am.getAccounts();
    for (Account a : accounts) {
        if (a.name.equals(accountName)) {
            account = a;
            break;
        }
    }

    // Restoring cookies
    if (account != null) {
        restoreCookies(account, client, context);
    }
}

From source file:com.cerema.cloud2.lib.common.accounts.AccountUtils.java

/**
 * Restore the client cookies from accountName
 * @param accountName/*from   w  w  w.j av a2s .  co m*/
 * @param client
 * @param context
 */
public static void restoreCookies(String accountName, OwnCloudClient client, Context context) {
    Log_OC.d(TAG, "Restoring cookies for " + accountName);

    // Account Manager
    AccountManager am = AccountManager.get(context.getApplicationContext());

    // Get account
    Account account = null;
    Account accounts[] = am.getAccounts();
    for (Account a : accounts) {
        if (a.name.equals(accountName)) {
            account = a;
            break;
        }
    }

    // Restoring cookies
    if (account != null) {
        restoreCookies(account, client, context);
    }
}

From source file:com.android.shell.BugreportReceiver.java

/**
 * Find the best matching {@link Account} based on build properties.
 *///from ww  w. j  a  va2s.c o m
private static Account findSendToAccount(Context context) {
    final AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    String preferredDomain = SystemProperties.get("sendbug.preferred.domain");
    if (!preferredDomain.startsWith("@")) {
        preferredDomain = "@" + preferredDomain;
    }

    final Account[] accounts = am.getAccounts();
    Account foundAccount = null;
    for (Account account : accounts) {
        if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
            if (!preferredDomain.isEmpty()) {
                // if we have a preferred domain and it matches, return; otherwise keep
                // looking
                if (account.name.endsWith(preferredDomain)) {
                    return account;
                } else {
                    foundAccount = account;
                }
                // if we don't have a preferred domain, just return since it looks like
                // an email address
            } else {
                return account;
            }
        }
    }
    return foundAccount;
}

From source file:Main.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 *//*from w  w  w  . j a  v  a2s. c  om*/
public static Account CreateSyncAccount(Context context) {

    // Create the account type and default account
    Account newAccount = new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(context.ACCOUNT_SERVICE);

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        Log.d(TAG, "Account created: " + newAccount.name);

        return newAccount;
    } else {
        /*
         * The account exists or some other error occurred.
         * Try and get account first. Then log or report the error and return null.
         */
        Account[] accounts = accountManager.getAccounts();
        for (Account account : accounts) {
            if (account.name.equals(ACCOUNT_NAME)) {
                Log.d(TAG, "Account exists: " + account.name);
                return account;
            }
        }
        Log.d(TAG, "Error occured. The account is null");
        return null;
    }

}

From source file:com.mikecorrigan.bohrium.pubsub.RegistrationClient.java

public static List<String> getAccounts(final Context context, final String accountType) {
    Log.v(TAG, "getAccountsByType: accountType=" + accountType);

    final AccountManager mgr = AccountManager.get(context);
    ArrayList<String> result = new ArrayList<>();
    final Account[] accounts = mgr.getAccounts();
    for (final Account account : accounts) {
        Log.v(TAG, "accountName=" + account.name);
        if (account.type.equals(accountType)) {
            result.add(account.name);//from www  . j  ava2  s  .com
        }
    }

    return result;
}