Search in sources :

Example 1 with AccountTypeWithDataSet

use of com.android.contacts.common.model.account.AccountTypeWithDataSet 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 2 with AccountTypeWithDataSet

use of com.android.contacts.common.model.account.AccountTypeWithDataSet in project android_packages_apps_Dialer by LineageOS.

the class ContactLoader method loadInvitableAccountTypes.

/**
 * Sets the "invitable" account types to {@link Contact#mInvitableAccountTypes}.
 */
private void loadInvitableAccountTypes(Contact contactData) {
    final ImmutableList.Builder<AccountType> resultListBuilder = new ImmutableList.Builder<AccountType>();
    if (!contactData.isUserProfile()) {
        Map<AccountTypeWithDataSet, AccountType> invitables = AccountTypeManager.getInstance(getContext()).getUsableInvitableAccountTypes();
        if (!invitables.isEmpty()) {
            final Map<AccountTypeWithDataSet, AccountType> resultMap = Maps.newHashMap(invitables);
            // Remove the ones that already have a raw contact in the current contact
            for (RawContact rawContact : contactData.getRawContacts()) {
                final AccountTypeWithDataSet type = AccountTypeWithDataSet.get(rawContact.getAccountTypeString(), rawContact.getDataSet());
                resultMap.remove(type);
            }
            resultListBuilder.addAll(resultMap.values());
        }
    }
    // Set to mInvitableAccountTypes
    contactData.setInvitableAccountTypes(resultListBuilder.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AccountTypeWithDataSet(com.android.contacts.common.model.account.AccountTypeWithDataSet) AccountType(com.android.contacts.common.model.account.AccountType)

Example 3 with AccountTypeWithDataSet

use of com.android.contacts.common.model.account.AccountTypeWithDataSet in project android_packages_apps_Dialer by LineageOS.

the class AccountTypeManagerImpl method findAllInvitableAccountTypes.

/**
 * Return all {@link AccountType}s with at least one account which supports "invite", i.e. its
 * {@link AccountType#getInviteContactActivityClassName()} is not empty.
 */
@VisibleForTesting
static Map<AccountTypeWithDataSet, AccountType> findAllInvitableAccountTypes(Context context, Collection<AccountWithDataSet> accounts, Map<AccountTypeWithDataSet, AccountType> accountTypesByTypeAndDataSet) {
    Map<AccountTypeWithDataSet, AccountType> result = new ArrayMap<>();
    for (AccountWithDataSet account : accounts) {
        AccountTypeWithDataSet accountTypeWithDataSet = account.getAccountTypeWithDataSet();
        AccountType type = accountTypesByTypeAndDataSet.get(accountTypeWithDataSet);
        if (type == null) {
            // just in case
            continue;
        }
        if (result.containsKey(accountTypeWithDataSet)) {
            continue;
        }
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Type " + accountTypeWithDataSet + " inviteClass=" + type.getInviteContactActivityClassName());
        }
        if (!TextUtils.isEmpty(type.getInviteContactActivityClassName())) {
            result.put(accountTypeWithDataSet, type);
        }
    }
    return Collections.unmodifiableMap(result);
}
Also used : AccountWithDataSet(com.android.contacts.common.model.account.AccountWithDataSet) AccountTypeWithDataSet(com.android.contacts.common.model.account.AccountTypeWithDataSet) ArrayMap(android.util.ArrayMap) 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) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 4 with AccountTypeWithDataSet

use of com.android.contacts.common.model.account.AccountTypeWithDataSet in project android_packages_apps_Dialer by LineageOS.

the class AccountTypeManagerImpl method findUsableInvitableAccountTypes.

/**
 * Return all usable {@link AccountType}s that support the "invite" feature from the list of all
 * potential invitable account types (retrieved from {@link #getAllInvitableAccountTypes}). A
 * usable invitable account type means: (1) there is at least 1 raw contact in the database with
 * that account type, and (2) the app contributing the account type is not disabled.
 *
 * <p>Warning: Don't use on the UI thread because this can scan the database.
 */
private Map<AccountTypeWithDataSet, AccountType> findUsableInvitableAccountTypes(Context context) {
    Map<AccountTypeWithDataSet, AccountType> allInvitables = getAllInvitableAccountTypes();
    if (allInvitables.isEmpty()) {
        return EMPTY_UNMODIFIABLE_ACCOUNT_TYPE_MAP;
    }
    final Map<AccountTypeWithDataSet, AccountType> result = new ArrayMap<>();
    result.putAll(allInvitables);
    final PackageManager packageManager = context.getPackageManager();
    for (AccountTypeWithDataSet accountTypeWithDataSet : allInvitables.keySet()) {
        AccountType accountType = allInvitables.get(accountTypeWithDataSet);
        // Make sure that account types don't come from apps that are disabled.
        Intent invitableIntent = MoreContactUtils.getInvitableIntent(accountType, SAMPLE_CONTACT_URI);
        if (invitableIntent == null) {
            result.remove(accountTypeWithDataSet);
            continue;
        }
        ResolveInfo resolveInfo = packageManager.resolveActivity(invitableIntent, PackageManager.MATCH_DEFAULT_ONLY);
        if (resolveInfo == null) {
            // If we can't find an activity to start for this intent, then there's no point in
            // showing this option to the user.
            result.remove(accountTypeWithDataSet);
            continue;
        }
        // is non-trivial and should not be done on the UI thread.
        if (!accountTypeWithDataSet.hasData(context)) {
            result.remove(accountTypeWithDataSet);
        }
    }
    return Collections.unmodifiableMap(result);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) AccountTypeWithDataSet(com.android.contacts.common.model.account.AccountTypeWithDataSet) ArrayMap(android.util.ArrayMap) Intent(android.content.Intent) 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)

Aggregations

AccountType (com.android.contacts.common.model.account.AccountType)4 AccountTypeWithDataSet (com.android.contacts.common.model.account.AccountTypeWithDataSet)4 ArrayMap (android.util.ArrayMap)3 ExchangeAccountType (com.android.contacts.common.model.account.ExchangeAccountType)3 ExternalAccountType (com.android.contacts.common.model.account.ExternalAccountType)3 FallbackAccountType (com.android.contacts.common.model.account.FallbackAccountType)3 GoogleAccountType (com.android.contacts.common.model.account.GoogleAccountType)3 SamsungAccountType (com.android.contacts.common.model.account.SamsungAccountType)3 AccountWithDataSet (com.android.contacts.common.model.account.AccountWithDataSet)2 Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 AuthenticatorDescription (android.accounts.AuthenticatorDescription)1 Intent (android.content.Intent)1 SyncAdapterType (android.content.SyncAdapterType)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 TimingLogger (android.util.TimingLogger)1 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1