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:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

@Override
/**/*from www.jav a2  s.c o m*/
 * This method will save a contact object into the devices contacts database.
 *
 * @param contact the contact to be saved.
 * @returns the id if the contact is successfully saved, null otherwise.
 */
public String save(JSONObject contact) {
    AccountManager mgr = AccountManager.get(getContext());
    Account[] accounts = mgr.getAccounts();
    String accountName = null;
    String accountType = null;

    if (accounts.length == 1) {
        accountName = accounts[0].name;
        accountType = accounts[0].type;
    } else if (accounts.length > 1) {
        for (Account a : accounts) {
            if (a.type.contains("eas") && a.name.matches(EMAIL_REGEXP)) /*
                                                                        * Exchange ActiveSync
                                                                        */ {
                accountName = a.name;
                accountType = a.type;
                break;
            }
        }
        if (accountName == null) {
            for (Account a : accounts) {
                if (a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*
                                                                                   * Google sync provider
                                                                                   */ {
                    accountName = a.name;
                    accountType = a.type;
                    break;
                }
            }
        }
        if (accountName == null) {
            for (Account a : accounts) {
                if (a.name.matches(EMAIL_REGEXP)) /*
                                                  * Last resort, just look for an email address...
                                                  */ {
                    accountName = a.name;
                    accountType = a.type;
                    break;
                }
            }
        }
    }

    String id = getJsonString(contact, "id");
    // Create new contact
    if (id == null) {
        return createNewContact(contact, accountType, accountName);
    }
    // Modify existing contact
    else {
        return modifyContact(id, contact, accountType, accountName);
    }
}

From source file:org.apache.cordova.ContactAccessorSdk5.java

@Override
/**//from  w  ww.ja v a 2s  . c  o  m
 * This method will save a contact object into the devices contacts database.
 *
 * @param contact the contact to be saved.
 * @returns the id if the contact is successfully saved, null otherwise.
 */
public String save(JSONObject contact) {
    AccountManager mgr = AccountManager.get(mApp.getActivity());
    Account[] accounts = mgr.getAccounts();
    String accountName = null;
    String accountType = null;

    if (accounts.length == 1) {
        accountName = accounts[0].name;
        accountType = accounts[0].type;
    } else if (accounts.length > 1) {
        for (Account a : accounts) {
            if (a.type.contains("eas") && a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/ {
                accountName = a.name;
                accountType = a.type;
                break;
            }
        }
        if (accountName == null) {
            for (Account a : accounts) {
                if (a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google sync provider*/ {
                    accountName = a.name;
                    accountType = a.type;
                    break;
                }
            }
        }
        if (accountName == null) {
            for (Account a : accounts) {
                if (a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/ {
                    accountName = a.name;
                    accountType = a.type;
                    break;
                }
            }
        }
    }

    String id = getJsonString(contact, "id");
    // Create new contact
    if (id == null) {
        return createNewContact(contact, accountType, accountName);
    }
    // Modify existing contact
    else {
        return modifyContact(id, contact, accountType, accountName);
    }
}

From source file:com.remobile.contacts.ContactAccessorSdk5.java

@Override
/**//from  w w w. ja va  2 s. c  o m
 * This method will save a contact object into the devices contacts database.
 *
 * @param contact the contact to be saved.
 * @returns the id if the contact is successfully saved, null otherwise.
 */
public String save(JSONObject contact) {
    AccountManager mgr = AccountManager.get(mApp.getActivity());
    Account[] accounts = mgr.getAccounts();
    String accountName = null;
    String accountType = null;

    if (accounts.length == 1) {
        accountName = accounts[0].name;
        accountType = accounts[0].type;
    } else if (accounts.length > 1) {
        for (Account a : accounts) {
            if (a.type.contains("eas") && a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/ {
                accountName = a.name;
                accountType = a.type;
                break;
            }
        }
        if (accountName == null) {
            for (Account a : accounts) {
                if (a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google sync provider*/ {
                    accountName = a.name;
                    accountType = a.type;
                    break;
                }
            }
        }
        if (accountName == null) {
            for (Account a : accounts) {
                if (a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/ {
                    accountName = a.name;
                    accountType = a.type;
                    break;
                }
            }
        }
    }

    String id = getJsonString(contact, "id");
    if (id == null) {
        // Create new contact
        return createNewContact(contact, accountType, accountName);
    } else {
        // Modify existing contact
        return modifyContact(id, contact, accountType, accountName);
    }
}