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:com.android.settings.HWSettings.java

private int insertAccountsHeaders(List<Header> target, int headerIndex) {
    String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
    List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
    for (String accountType : accountTypes) {
        /**/*w w w  .ja v  a2  s  .  c  o  m*/
         * SPRD:
         * hide sprd account in settings.
         * @{
         */
        if (accountType.startsWith("sprd")) {
            continue;
        }
        /**
         * @}
         */
        CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
        if (label == null) {
            continue;
        }

        Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
        boolean skipToAccount = accounts.length == 1
                && !mAuthenticatorHelper.hasAccountPreferences(accountType);
        Header accHeader = new Header();
        accHeader.title = label;
        if (accHeader.extras == null) {
            accHeader.extras = new Bundle();
        }
        if (skipToAccount) {
            accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
            accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
            accHeader.fragment = AccountSyncSettings.class.getName();
            accHeader.fragmentArguments = new Bundle();
            // Need this for the icon
            accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
            accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
            accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
        } else {
            accHeader.breadCrumbTitle = label;
            accHeader.breadCrumbShortTitle = label;
            accHeader.fragment = ManageAccountsSettings.class.getName();
            accHeader.fragmentArguments = new Bundle();
            accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
            accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
            if (!isMultiPane()) {
                accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
                        label.toString());
            }
        }
        accountHeaders.add(accHeader);
        mAuthenticatorHelper.preloadDrawableForType(this, accountType);
    }

    // Sort by label
    Collections.sort(accountHeaders, new Comparator<Header>() {
        @Override
        public int compare(Header h1, Header h2) {
            return h1.title.toString().compareTo(h2.title.toString());
        }
    });

    for (Header header : accountHeaders) {
        target.add(headerIndex++, header);
    }
    if (!mListeningToAccountUpdates) {
        AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
        mListeningToAccountUpdates = true;
    }
    return headerIndex;
}

From source file:edu.mit.mobile.android.locast.net.NetworkClient.java

/**
 * Perform an offline check to see if there is a pairing stored for this client. Does not block
 * on network connection./*from w  ww.j  a  v a 2 s.  c  om*/
 *
 * @return true if the client is paired with the server.
 */
@Deprecated
public boolean isPaired() {
    final AccountManager am = AccountManager.get(mContext);
    final Account[] accounts = am.getAccountsByType(AuthenticationService.ACCOUNT_TYPE);
    return accounts.length >= 1;
}

From source file:co.beem.project.beem.FbTextService.java

/**
 * Get the specified Android account./*from  w  w w .j  av a 2s. com*/
 * 
 * @param accountName
 *            the account name
 * @param accountType
 *            the account type
 * 
 * @return the account or null if it does not exist
 */
private Account getAccount(String accountName, String accountType) {
    AccountManager am = AccountManager.get(this);
    for (Account a : am.getAccountsByType(accountType)) {
        if (a.name.equals(accountName)) {
            return a;
        }
    }
    return null;
}

From source file:com.nononsenseapps.notepad.MainActivity.java

private void requestSync(String accountName) {
    if (accountName != null && !"".equals(accountName)) {
        Account account = SyncPrefs.getAccount(AccountManager.get(this), accountName);
        // Don't start a new sync if one is already going
        if (!ContentResolver.isSyncActive(account, NotePad.AUTHORITY)) {
            Bundle options = new Bundle();
            // This will force a sync regardless of what the setting is
            // in accounts manager. Only use it here where the user has
            // manually desired a sync to happen NOW.
            options.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
            ContentResolver.requestSync(account, NotePad.AUTHORITY, options);
        }//from ww w. j  a  v a 2s. c  o m
    }
}

From source file:com.google.samples.apps.sergio.ui.BaseActivity.java

/**
 * Returns the default account on the device. We use the rule that the first account
 * should be the default. It's arbitrary, but the alternative would be showing an account
 * chooser popup which wouldn't be a smooth first experience with the app. Since the user
 * can easily switch the account with the nav drawer, we opted for this implementation.
 *///from   w ww  .  j av a 2s. co m
private String getDefaultAccount() {
    // Choose first account on device.
    LOGD(TAG, "Choosing default account (first account on device)");
    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    if (accounts.length == 0) {
        // No Google accounts on device.
        LOGW(TAG, "No Google accounts on device; not setting default account.");
        return null;
    }

    LOGD(TAG, "Default account is: " + accounts[0].name);
    return accounts[0].name;
}

From source file:com.mwebster.exchange.SyncManager.java

/**
 * Non-blocking call to run the account reconciler.
 * Launches a worker thread, so it may be called from UI thread.
 *//*from  ww  w  .j a v a2s  .  c o m*/
private void runAccountReconciler() {
    final SyncManager syncManager = this;
    new Thread() {
        @Override
        public void run() {
            android.accounts.Account[] accountMgrList = AccountManager.get(syncManager)
                    .getAccountsByType(Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
            synchronized (sAccountList) {
                // Make sure we have an up-to-date sAccountList.  If not (for example, if the
                // service has been destroyed), we would be reconciling against an empty account
                // list, which would cause the deletion of all of our accounts
                if (mAccountObserver != null) {
                    mAccountObserver.onAccountChanged();
                    reconcileAccountsWithAccountManager(syncManager, sAccountList, accountMgrList, false,
                            mResolver);
                }
            }
        }
    }.start();
}

From source file:com.android.exchange.SyncManager.java

/**
 * Non-blocking call to run the account reconciler.
 * Launches a worker thread, so it may be called from UI thread.
 *///from   w  w w.j a  v a2s .c o m
private void runAccountReconciler() {
    final SyncManager syncManager = this;
    new Thread() {
        @Override
        public void run() {
            android.accounts.Account[] accountMgrList = AccountManager.get(syncManager)
                    .getAccountsByType(Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
            synchronized (mAccountList) {
                // Make sure we have an up-to-date sAccountList.  If not (for example, if the
                // service has been destroyed), we would be reconciling against an empty account
                // list, which would cause the deletion of all of our accounts
                if (mAccountObserver != null) {
                    mAccountObserver.onAccountChanged();
                    reconcileAccountsWithAccountManager(syncManager, mAccountList, accountMgrList, false,
                            mResolver);
                }
            }
        }
    }.start();
}

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

@Override
/**//from   w w w  .j  a v a 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);
    }
}

From source file:com.saarang.samples.apps.iosched.ui.BaseActivity.java

/**
 * Returns the default account on the device. We use the rule that the first account
 * should be the default. It's arbitrary, but the alternative would be showing an account
 * chooser popup which wouldn't be a smooth first experience with the app. Since the user
 * can easily switch the account with the nav drawer, we opted for this implementation.
 *//*from  w w w. ja va 2  s .  c o m*/
private String getDefaultAccount() {
    // Choose first account on device.
    LogUtils.LOGD(TAG, "Choosing default account (first account on device)");
    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    if (accounts.length == 0) {
        // No Google accounts on device.
        LogUtils.LOGW(TAG, "No Google accounts on device; not setting default account.");
        return null;
    }

    LogUtils.LOGD(TAG, "Default account is: " + accounts[0].name);
    return accounts[0].name;
}