Search in sources :

Example 81 with AccountManager

use of android.accounts.AccountManager in project Sunshine-Version-2 by udacity.

the class SunshineSyncAdapter method getSyncAccount.

/**
     * Helper method to get the fake account to be used with SyncAdapter, or make a new one
     * if the fake account doesn't exist yet.  If we make a new account, we call the
     * onAccountCreated method so we can initialize things.
     *
     * @param context The context used to access the account service
     * @return a fake account.
     */
public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name), context.getString(R.string.sync_account_type));
    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {
        /*
         * 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)) {
            return null;
        }
        /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
             * here.
             */
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager)

Example 82 with AccountManager

use of android.accounts.AccountManager in project cordova-android-chromeview by thedracle.

the class ContactAccessorSdk5 method save.

@Override
public /**
     * 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.
     */
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);
    } else // Modify existing contact
    {
        return modifyContact(id, contact, accountType, accountName);
    }
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager)

Example 83 with AccountManager

use of android.accounts.AccountManager in project mobile-android by photo.

the class GoogleLoginFragment method getAccountNames.

private String[] getAccountNames() {
    AccountManager mAccountManager = AccountManager.get(getActivity());
    Account[] accounts = mAccountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    String[] names = new String[accounts.length];
    for (int i = 0; i < names.length; i++) {
        names[i] = accounts[i].name;
    }
    return names;
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager)

Example 84 with AccountManager

use of android.accounts.AccountManager in project android_frameworks_base by DirtyUnicorns.

the class DevicePolicyManagerService method hasIncompatibleAccountsOrNonAdbNoLock.

/**
     * Return true if a given user has any accounts that'll prevent installing a device or profile
     * owner {@code owner}.
     * - If the user has no accounts, then return false.
     * - Otherwise, if the owner is unknown (== null), or is not test-only, then return true.
     * - Otherwise, if there's any account that does not have ..._ALLOWED, or does have
     *   ..._DISALLOWED, return true.
     * - Otherwise return false.
     *
     * If the caller is *not* ADB, it also returns true.  The returned value shouldn't be used
     * when the caller is not ADB.
     *
     * DO NOT CALL IT WITH THE DPMS LOCK HELD.
     */
private boolean hasIncompatibleAccountsOrNonAdbNoLock(int userId, @Nullable ComponentName owner) {
    final boolean isAdb = (mInjector.binderGetCallingUid() == Process.SHELL_UID) || (mInjector.binderGetCallingUid() == Process.ROOT_UID);
    if (!isAdb) {
        return true;
    }
    if (Thread.holdsLock(this)) {
        Slog.wtf(LOG_TAG, "hasIncompatibleAccountsNoLock() called with the DPMS lock held.");
        return true;
    }
    final long token = mInjector.binderClearCallingIdentity();
    try {
        final AccountManager am = AccountManager.get(mContext);
        final Account[] accounts = am.getAccountsAsUser(userId);
        if (accounts.length == 0) {
            return false;
        }
        synchronized (this) {
            if (owner == null || !isAdminTestOnlyLocked(owner, userId)) {
                Log.w(LOG_TAG, "Non test-only owner can't be installed with existing accounts.");
                return true;
            }
        }
        final String[] feature_allow = { DevicePolicyManager.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED };
        final String[] feature_disallow = { DevicePolicyManager.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_DISALLOWED };
        boolean compatible = true;
        for (Account account : accounts) {
            if (hasAccountFeatures(am, account, feature_disallow)) {
                Log.e(LOG_TAG, account + " has " + feature_disallow[0]);
                compatible = false;
                break;
            }
            if (!hasAccountFeatures(am, account, feature_allow)) {
                Log.e(LOG_TAG, account + " doesn't have " + feature_allow[0]);
                compatible = false;
                break;
            }
        }
        if (compatible) {
            Log.w(LOG_TAG, "All accounts are compatible");
        } else {
            Log.e(LOG_TAG, "Found incompatible accounts");
        }
        return !compatible;
    } finally {
        mInjector.binderRestoreCallingIdentity(token);
    }
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager) ParcelableString(com.android.internal.util.ParcelableString)

Example 85 with AccountManager

use of android.accounts.AccountManager in project android_frameworks_base by DirtyUnicorns.

the class AppLaunch method checkAccountSignIn.

private void checkAccountSignIn() {
    // for Gmail
    if (mRequiredAccounts == null || mRequiredAccounts.isEmpty()) {
        return;
    }
    final AccountManager am = (AccountManager) getInstrumentation().getTargetContext().getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = am.getAccounts();
    // use set here in case device has multiple accounts of the same type
    Set<String> foundAccounts = new HashSet<String>();
    for (Account account : accounts) {
        if (mRequiredAccounts.contains(account.type)) {
            foundAccounts.add(account.type);
        }
    }
    // are missing
    if (mRequiredAccounts.size() != foundAccounts.size()) {
        mRequiredAccounts.removeAll(foundAccounts);
        StringBuilder sb = new StringBuilder("Device missing these accounts:");
        for (String account : mRequiredAccounts) {
            sb.append(' ');
            sb.append(account);
        }
        fail(sb.toString());
    }
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager) HashSet(java.util.HashSet)

Aggregations

AccountManager (android.accounts.AccountManager)105 Account (android.accounts.Account)76 Bundle (android.os.Bundle)14 IOException (java.io.IOException)11 Intent (android.content.Intent)10 OperationCanceledException (android.accounts.OperationCanceledException)7 AuthenticatorException (android.accounts.AuthenticatorException)5 View (android.view.View)5 TextView (android.widget.TextView)5 HashSet (java.util.HashSet)5 SharedPreferences (android.content.SharedPreferences)4 AccountManagerCallback (android.accounts.AccountManagerCallback)3 Paint (android.graphics.Paint)3 JSONException (org.json.JSONException)3 AccountsException (android.accounts.AccountsException)2 TargetApi (android.annotation.TargetApi)2 AlertDialog (android.app.AlertDialog)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 OperationApplicationException (android.content.OperationApplicationException)2