1 package com.android.contacts.quickcontact; 2 3 4 import com.google.common.collect.Iterables; 5 6 import com.android.contacts.ContactSaveService; 7 import com.android.contacts.common.GroupMetaData; 8 import com.android.contacts.common.model.AccountTypeManager; 9 import com.android.contacts.common.model.Contact; 10 import com.android.contacts.common.model.RawContact; 11 import com.android.contacts.common.model.RawContactDelta; 12 import com.android.contacts.common.model.RawContactDeltaList; 13 import com.android.contacts.common.model.RawContactModifier; 14 import com.android.contacts.common.model.ValuesDelta; 15 import com.android.contacts.common.model.account.AccountType; 16 import com.android.contacts.common.model.dataitem.DataItem; 17 import com.android.contacts.common.model.dataitem.DataKind; 18 import com.android.contacts.common.model.dataitem.GroupMembershipDataItem; 19 20 import android.content.Context; 21 import android.content.Intent; 22 import android.provider.ContactsContract.CommonDataKinds.GroupMembership; 23 24 import java.util.List; 25 26 /** 27 * Utility class to support adding invisible contacts. Ie, contacts that don't belong to the 28 * default group. 29 */ 30 public class InvisibleContactUtil { 31 32 public static boolean isInvisibleAndAddable(Contact contactData, Context context) { 33 // Only local contacts 34 if (contactData == null || contactData.isDirectoryEntry()) return false; 35 36 // User profile cannot be added to contacts 37 if (contactData.isUserProfile()) return false; 38 39 // Only if exactly one raw contact 40 if (contactData.getRawContacts().size() != 1) return false; 41 42 // test if the default group is assigned 43 final List<GroupMetaData> groups = contactData.getGroupMetaData(); 44 45 // For accounts without group support, groups is null 46 if (groups == null) return false; 47 48 // remember the default group id. no default group? bail out early 49 final long defaultGroupId = getDefaultGroupId(groups); 50 if (defaultGroupId == -1) return false; 51 52 final RawContact rawContact = (RawContact) contactData.getRawContacts().get(0); 53 final AccountType type = rawContact.getAccountType(context); 54 // Offline or non-writeable account? Nothing to fix 55 if (type == null || !type.areContactsWritable()) return false; 56 57 // Check whether the contact is in the default group 58 boolean isInDefaultGroup = false; 59 for (DataItem dataItem : Iterables.filter( 60 rawContact.getDataItems(), GroupMembershipDataItem.class)) { 61 GroupMembershipDataItem groupMembership = (GroupMembershipDataItem) dataItem; 62 final Long groupId = groupMembership.getGroupRowId(); 63 if (groupId != null && groupId == defaultGroupId) { 64 isInDefaultGroup = true; 65 break; 66 } 67 } 68 69 return !isInDefaultGroup; 70 } 71 72 public static void addToDefaultGroup(Contact contactData, Context context) { 73 final long defaultGroupId = getDefaultGroupId(contactData.getGroupMetaData()); 74 // there should always be a default group (otherwise the button would be invisible), 75 // but let's be safe here 76 if (defaultGroupId == -1) return; 77 78 // add the group membership to the current state 79 final RawContactDeltaList contactDeltaList = contactData.createRawContactDeltaList(); 80 final RawContactDelta rawContactEntityDelta = contactDeltaList.get(0); 81 82 final AccountTypeManager accountTypes = AccountTypeManager.getInstance( 83 context); 84 final AccountType type = rawContactEntityDelta.getAccountType(accountTypes); 85 final DataKind groupMembershipKind = type.getKindForMimetype( 86 GroupMembership.CONTENT_ITEM_TYPE); 87 final ValuesDelta entry = RawContactModifier.insertChild(rawContactEntityDelta, 88 groupMembershipKind); 89 if (entry == null) return; 90 entry.setGroupRowId(defaultGroupId); 91 92 // and fire off the intent. we don't need a callback, as the database listener 93 // should update the ui 94 final Intent intent = ContactSaveService.createSaveContactIntent( 95 context, 96 contactDeltaList, "", 0, false, QuickContactActivity.class, 97 Intent.ACTION_VIEW, null, /* joinContactIdExtraKey =*/ null, 98 /* joinContactId =*/ null); 99 ContactSaveService.startService(context, intent); 100 } 101 102 /** return default group id or -1 if no group or several groups are marked as default */ 103 private static long getDefaultGroupId(List<GroupMetaData> groups) { 104 long defaultGroupId = -1; 105 for (GroupMetaData group : groups) { 106 if (group.isDefaultGroup()) { 107 // two default groups? return neither 108 if (defaultGroupId != -1) return -1; 109 defaultGroupId = group.getGroupId(); 110 } 111 } 112 return defaultGroupId; 113 } 114 } 115