Search in sources :

Example 31 with AccountManager

use of android.accounts.AccountManager in project Applozic-Android-SDK by AppLozic.

the class ApplozicBridge method getUserEmailId.

/**
 * Method to get the  Email Id  from google primary account
 *
 * @param context
 * @return
 */
public static String getUserEmailId(Context context) {
    String userEmailId = "";
    try {
        AccountManager manager = AccountManager.get(context);
        Account[] accounts = manager.getAccountsByType("com.google");
        List<String> possibleEmails = new LinkedList<String>();
        for (Account account : accounts) {
            // TODO: Check possibleEmail against an email regex or treat
            // account.name as an email address only for certain
            // account.type values.
            possibleEmails.add(account.name);
        }
        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            userEmailId = possibleEmails.get(0);
        }
    } catch (Exception e) {
    }
    return userEmailId;
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager) LinkedList(java.util.LinkedList)

Example 32 with AccountManager

use of android.accounts.AccountManager in project android_packages_apps_Dialer by LineageOS.

the class AccountTypeManagerImpl method loadAccountsInBackground.

/**
 * Loads account list and corresponding account types (potentially with data sets). Always called
 * on a background thread.
 */
protected void loadAccountsInBackground() {
    if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
        Log.d(Constants.PERFORMANCE_TAG, "AccountTypeManager.loadAccountsInBackground start");
    }
    TimingLogger timings = new TimingLogger(TAG, "loadAccountsInBackground");
    final long startTime = SystemClock.currentThreadTimeMillis();
    final long startTimeWall = SystemClock.elapsedRealtime();
    // Account types, keyed off the account type and data set concatenation.
    final Map<AccountTypeWithDataSet, AccountType> accountTypesByTypeAndDataSet = new ArrayMap<>();
    // The same AccountTypes, but keyed off {@link RawContacts#ACCOUNT_TYPE}.  Since there can
    // be multiple account types (with different data sets) for the same type of account, each
    // type string may have multiple AccountType entries.
    final Map<String, List<AccountType>> accountTypesByType = new ArrayMap<>();
    final List<AccountWithDataSet> allAccounts = new ArrayList<>();
    final List<AccountWithDataSet> contactWritableAccounts = new ArrayList<>();
    final List<AccountWithDataSet> groupWritableAccounts = new ArrayList<>();
    final Set<String> extensionPackages = new HashSet<>();
    final AccountManager am = mAccountManager;
    final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
    final AuthenticatorDescription[] auths = am.getAuthenticatorTypes();
    // First process sync adapters to find any that provide contact data.
    for (SyncAdapterType sync : syncs) {
        if (!ContactsContract.AUTHORITY.equals(sync.authority)) {
            // Skip sync adapters that don't provide contact data.
            continue;
        }
        // Look for the formatting details provided by each sync
        // adapter, using the authenticator to find general resources.
        final String type = sync.accountType;
        final AuthenticatorDescription auth = findAuthenticator(auths, type);
        if (auth == null) {
            Log.w(TAG, "No authenticator found for type=" + type + ", ignoring it.");
            continue;
        }
        AccountType accountType;
        if (GoogleAccountType.ACCOUNT_TYPE.equals(type)) {
            accountType = new GoogleAccountType(mContext, auth.packageName);
        } else if (ExchangeAccountType.isExchangeType(type)) {
            accountType = new ExchangeAccountType(mContext, auth.packageName, type);
        } else if (SamsungAccountType.isSamsungAccountType(mContext, type, auth.packageName)) {
            accountType = new SamsungAccountType(mContext, auth.packageName, type);
        } else {
            Log.d(TAG, "Registering external account type=" + type + ", packageName=" + auth.packageName);
            accountType = new ExternalAccountType(mContext, auth.packageName, false);
        }
        if (!accountType.isInitialized()) {
            if (accountType.isEmbedded()) {
                throw new IllegalStateException("Problem initializing embedded type " + accountType.getClass().getCanonicalName());
            } else {
                // Skip external account types that couldn't be initialized.
                continue;
            }
        }
        accountType.accountType = auth.type;
        accountType.titleRes = auth.labelId;
        accountType.iconRes = auth.iconId;
        addAccountType(accountType, accountTypesByTypeAndDataSet, accountTypesByType);
        // Check to see if the account type knows of any other non-sync-adapter packages
        // that may provide other data sets of contact data.
        extensionPackages.addAll(accountType.getExtensionPackageNames());
    }
    // If any extension packages were specified, process them as well.
    if (!extensionPackages.isEmpty()) {
        Log.d(TAG, "Registering " + extensionPackages.size() + " extension packages");
        for (String extensionPackage : extensionPackages) {
            ExternalAccountType accountType = new ExternalAccountType(mContext, extensionPackage, true);
            if (!accountType.isInitialized()) {
                // Skip external account types that couldn't be initialized.
                continue;
            }
            if (!accountType.hasContactsMetadata()) {
                Log.w(TAG, "Skipping extension package " + extensionPackage + " because" + " it doesn't have the CONTACTS_STRUCTURE metadata");
                continue;
            }
            if (TextUtils.isEmpty(accountType.accountType)) {
                Log.w(TAG, "Skipping extension package " + extensionPackage + " because" + " the CONTACTS_STRUCTURE metadata doesn't have the accountType" + " attribute");
                continue;
            }
            Log.d(TAG, "Registering extension package account type=" + accountType.accountType + ", dataSet=" + accountType.dataSet + ", packageName=" + extensionPackage);
            addAccountType(accountType, accountTypesByTypeAndDataSet, accountTypesByType);
        }
    }
    timings.addSplit("Loaded account types");
    // Map in accounts to associate the account names with each account type entry.
    Account[] accounts = mAccountManager.getAccounts();
    for (Account account : accounts) {
        boolean syncable = ContentResolver.getIsSyncable(account, ContactsContract.AUTHORITY) > 0;
        if (syncable) {
            List<AccountType> accountTypes = accountTypesByType.get(account.type);
            if (accountTypes != null) {
                // authenticated by this account.
                for (AccountType accountType : accountTypes) {
                    AccountWithDataSet accountWithDataSet = new AccountWithDataSet(account.name, account.type, accountType.dataSet);
                    allAccounts.add(accountWithDataSet);
                    if (accountType.areContactsWritable()) {
                        contactWritableAccounts.add(accountWithDataSet);
                    }
                    if (accountType.isGroupMembershipEditable()) {
                        groupWritableAccounts.add(accountWithDataSet);
                    }
                }
            }
        }
    }
    Collections.sort(allAccounts, ACCOUNT_COMPARATOR);
    Collections.sort(contactWritableAccounts, ACCOUNT_COMPARATOR);
    Collections.sort(groupWritableAccounts, ACCOUNT_COMPARATOR);
    timings.addSplit("Loaded accounts");
    synchronized (this) {
        mAccountTypesWithDataSets = accountTypesByTypeAndDataSet;
        mAccounts = allAccounts;
        mContactWritableAccounts = contactWritableAccounts;
        mGroupWritableAccounts = groupWritableAccounts;
        mInvitableAccountTypes = findAllInvitableAccountTypes(mContext, allAccounts, accountTypesByTypeAndDataSet);
    }
    timings.dumpToLog();
    final long endTimeWall = SystemClock.elapsedRealtime();
    final long endTime = SystemClock.currentThreadTimeMillis();
    Log.i(TAG, "Loaded meta-data for " + mAccountTypesWithDataSets.size() + " account types, " + mAccounts.size() + " accounts in " + (endTimeWall - startTimeWall) + "ms(wall) " + (endTime - startTime) + "ms(cpu)");
    if (mInitializationLatch != null) {
        mInitializationLatch.countDown();
        mInitializationLatch = null;
    }
    if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
        Log.d(Constants.PERFORMANCE_TAG, "AccountTypeManager.loadAccountsInBackground finish");
    }
    // Check filter validity since filter may become obsolete after account update. It must be
    // done from UI thread.
    mMainThreadHandler.post(mCheckFilterValidityRunnable);
}
Also used : Account(android.accounts.Account) ArrayList(java.util.ArrayList) GoogleAccountType(com.android.contacts.common.model.account.GoogleAccountType) ExchangeAccountType(com.android.contacts.common.model.account.ExchangeAccountType) SamsungAccountType(com.android.contacts.common.model.account.SamsungAccountType) AuthenticatorDescription(android.accounts.AuthenticatorDescription) TimingLogger(android.util.TimingLogger) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AccountWithDataSet(com.android.contacts.common.model.account.AccountWithDataSet) AccountTypeWithDataSet(com.android.contacts.common.model.account.AccountTypeWithDataSet) ArrayMap(android.util.ArrayMap) ExternalAccountType(com.android.contacts.common.model.account.ExternalAccountType) SyncAdapterType(android.content.SyncAdapterType) AccountType(com.android.contacts.common.model.account.AccountType) FallbackAccountType(com.android.contacts.common.model.account.FallbackAccountType) ExchangeAccountType(com.android.contacts.common.model.account.ExchangeAccountType) SamsungAccountType(com.android.contacts.common.model.account.SamsungAccountType) ExternalAccountType(com.android.contacts.common.model.account.ExternalAccountType) GoogleAccountType(com.android.contacts.common.model.account.GoogleAccountType) AccountManager(android.accounts.AccountManager)

Example 33 with AccountManager

use of android.accounts.AccountManager in project Signal-Android by signalapp.

the class DirectoryHelper method getOrCreateAccount.

private static Optional<AccountHolder> getOrCreateAccount(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType("org.thoughtcrime.securesms");
    Optional<AccountHolder> account;
    if (accounts.length == 0)
        account = createAccount(context);
    else
        account = Optional.of(new AccountHolder(accounts[0], false));
    if (account.isPresent() && !ContentResolver.getSyncAutomatically(account.get().getAccount(), ContactsContract.AUTHORITY)) {
        ContentResolver.setSyncAutomatically(account.get().getAccount(), ContactsContract.AUTHORITY, true);
    }
    return account;
}
Also used : Account(android.accounts.Account) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) AccountManager(android.accounts.AccountManager)

Example 34 with AccountManager

use of android.accounts.AccountManager in project Signal-Android by signalapp.

the class DirectoryHelper method createAccount.

private static Optional<AccountHolder> createAccount(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account account = new Account(context.getString(R.string.app_name), "org.thoughtcrime.securesms");
    if (accountManager.addAccountExplicitly(account, null, null)) {
        Log.w(TAG, "Created new account...");
        ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
        return Optional.of(new AccountHolder(account, true));
    } else {
        Log.w(TAG, "Failed to create account!");
        return Optional.absent();
    }
}
Also used : Account(android.accounts.Account) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) AccountManager(android.accounts.AccountManager)

Example 35 with AccountManager

use of android.accounts.AccountManager in project android by nextcloud.

the class FileActivity method performCredentialsUpdate.

public void performCredentialsUpdate(Account account, Context context) {
    try {
        // / step 1 - invalidate credentials of current account
        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
        OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount);
        if (client != null) {
            OwnCloudCredentials credentials = client.getCredentials();
            if (credentials != null) {
                AccountManager accountManager = AccountManager.get(context);
                if (credentials.authTokenExpires()) {
                    accountManager.invalidateAuthToken(account.type, credentials.getAuthToken());
                } else {
                    accountManager.clearPassword(account);
                }
            }
        }
        // / step 2 - request credentials to user
        Intent updateAccountCredentials = new Intent(context, AuthenticatorActivity.class);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivityForResult(updateAccountCredentials, REQUEST_CODE__UPDATE_CREDENTIALS);
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        DisplayUtils.showSnackMessage(this, R.string.auth_account_does_not_exist);
    }
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) AccountManager(android.accounts.AccountManager) Intent(android.content.Intent) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Aggregations

AccountManager (android.accounts.AccountManager)180 Account (android.accounts.Account)121 Bundle (android.os.Bundle)28 Intent (android.content.Intent)18 PackageManager (android.content.pm.PackageManager)16 View (android.view.View)16 TextView (android.widget.TextView)16 Context (android.content.Context)15 IOException (java.io.IOException)14 UserAccountManager (com.nextcloud.client.account.UserAccountManager)13 OperationCanceledException (android.accounts.OperationCanceledException)11 ImageView (android.widget.ImageView)9 AuthenticatorDescription (android.accounts.AuthenticatorDescription)8 UserHandle (android.os.UserHandle)8 LayoutInflater (android.view.LayoutInflater)8 AuthenticatorException (android.accounts.AuthenticatorException)7 UserInfo (android.content.pm.UserInfo)7 Resources (android.content.res.Resources)7 Drawable (android.graphics.drawable.Drawable)7 LinearLayout (android.widget.LinearLayout)7