Search in sources :

Example 1 with GroupMetaData

use of com.android.contacts.common.GroupMetaData in project android_packages_apps_Dialer by LineageOS.

the class ContactLoader method loadGroupMetaData.

/**
 * Loads groups meta-data for all groups associated with all constituent raw contacts' accounts.
 */
private void loadGroupMetaData(Contact result) {
    StringBuilder selection = new StringBuilder();
    ArrayList<String> selectionArgs = new ArrayList<String>();
    final HashSet<AccountKey> accountsSeen = new HashSet<>();
    for (RawContact rawContact : result.getRawContacts()) {
        final String accountName = rawContact.getAccountName();
        final String accountType = rawContact.getAccountTypeString();
        final String dataSet = rawContact.getDataSet();
        final AccountKey accountKey = new AccountKey(accountName, accountType, dataSet);
        if (accountName != null && accountType != null && !accountsSeen.contains(accountKey)) {
            accountsSeen.add(accountKey);
            if (selection.length() != 0) {
                selection.append(" OR ");
            }
            selection.append("(" + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?");
            selectionArgs.add(accountName);
            selectionArgs.add(accountType);
            if (dataSet != null) {
                selection.append(" AND " + Groups.DATA_SET + "=?");
                selectionArgs.add(dataSet);
            } else {
                selection.append(" AND " + Groups.DATA_SET + " IS NULL");
            }
            selection.append(")");
        }
    }
    final ImmutableList.Builder<GroupMetaData> groupListBuilder = new ImmutableList.Builder<GroupMetaData>();
    final Cursor cursor = getContext().getContentResolver().query(Groups.CONTENT_URI, GroupQuery.COLUMNS, selection.toString(), selectionArgs.toArray(new String[0]), null);
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME);
                final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE);
                final String dataSet = cursor.getString(GroupQuery.DATA_SET);
                final long groupId = cursor.getLong(GroupQuery.ID);
                final String title = cursor.getString(GroupQuery.TITLE);
                final boolean defaultGroup = !cursor.isNull(GroupQuery.AUTO_ADD) && cursor.getInt(GroupQuery.AUTO_ADD) != 0;
                final boolean favorites = !cursor.isNull(GroupQuery.FAVORITES) && cursor.getInt(GroupQuery.FAVORITES) != 0;
                groupListBuilder.add(new GroupMetaData(accountName, accountType, dataSet, groupId, title, defaultGroup, favorites));
            }
        } finally {
            cursor.close();
        }
    }
    result.setGroupMetaData(groupListBuilder.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) GroupMetaData(com.android.contacts.common.GroupMetaData) HashSet(java.util.HashSet)

Example 2 with GroupMetaData

use of com.android.contacts.common.GroupMetaData in project packages_apps_Contacts by AOKP.

the class InvisibleContactUtil method isInvisibleAndAddable.

public static boolean isInvisibleAndAddable(Contact contactData, Context context) {
    // Only local contacts
    if (contactData == null || contactData.isDirectoryEntry())
        return false;
    // User profile cannot be added to contacts
    if (contactData.isUserProfile())
        return false;
    // Only if exactly one raw contact
    if (contactData.getRawContacts().size() != 1)
        return false;
    // test if the default group is assigned
    final List<GroupMetaData> groups = contactData.getGroupMetaData();
    // For accounts without group support, groups is null
    if (groups == null)
        return false;
    // remember the default group id. no default group? bail out early
    final long defaultGroupId = getDefaultGroupId(groups);
    if (defaultGroupId == -1)
        return false;
    final RawContact rawContact = (RawContact) contactData.getRawContacts().get(0);
    final AccountType type = rawContact.getAccountType(context);
    // Offline or non-writeable account? Nothing to fix
    if (type == null || !type.areContactsWritable())
        return false;
    // Check whether the contact is in the default group
    boolean isInDefaultGroup = false;
    for (DataItem dataItem : Iterables.filter(rawContact.getDataItems(), GroupMembershipDataItem.class)) {
        GroupMembershipDataItem groupMembership = (GroupMembershipDataItem) dataItem;
        final Long groupId = groupMembership.getGroupRowId();
        if (groupId != null && groupId == defaultGroupId) {
            isInDefaultGroup = true;
            break;
        }
    }
    return !isInDefaultGroup;
}
Also used : DataItem(com.android.contacts.common.model.dataitem.DataItem) GroupMembershipDataItem(com.android.contacts.common.model.dataitem.GroupMembershipDataItem) RawContact(com.android.contacts.common.model.RawContact) GroupMetaData(com.android.contacts.common.GroupMetaData) GroupMembershipDataItem(com.android.contacts.common.model.dataitem.GroupMembershipDataItem) AccountType(com.android.contacts.common.model.account.AccountType)

Aggregations

GroupMetaData (com.android.contacts.common.GroupMetaData)2 Cursor (android.database.Cursor)1 RawContact (com.android.contacts.common.model.RawContact)1 AccountType (com.android.contacts.common.model.account.AccountType)1 DataItem (com.android.contacts.common.model.dataitem.DataItem)1 GroupMembershipDataItem (com.android.contacts.common.model.dataitem.GroupMembershipDataItem)1 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1