Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 1 | package com.android.contacts.quickcontact; |
| 2 | |
| 3 | |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 4 | import android.content.Context; |
| 5 | import android.content.Intent; |
| 6 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 7 | |
| 8 | import com.android.contacts.ContactSaveService; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 9 | import com.android.contacts.group.GroupMetaData; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 10 | import com.android.contacts.model.AccountTypeManager; |
| 11 | import com.android.contacts.model.Contact; |
| 12 | import com.android.contacts.model.RawContact; |
| 13 | import com.android.contacts.model.RawContactDelta; |
| 14 | import com.android.contacts.model.RawContactDeltaList; |
| 15 | import com.android.contacts.model.RawContactModifier; |
| 16 | import com.android.contacts.model.ValuesDelta; |
| 17 | import com.android.contacts.model.account.AccountType; |
| 18 | import com.android.contacts.model.dataitem.DataItem; |
| 19 | import com.android.contacts.model.dataitem.DataKind; |
| 20 | import com.android.contacts.model.dataitem.GroupMembershipDataItem; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 21 | |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Iterables; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 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) { |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 73 | final RawContactDeltaList contactDeltaList = contactData.createRawContactDeltaList(); |
| 74 | if (markAddToDefaultGroup(contactData, contactDeltaList, context)) { |
| 75 | // Fire off the intent. we don't need a callback, as the database listener |
| 76 | // should update the ui |
| 77 | final Intent intent = ContactSaveService.createSaveContactIntent( |
| 78 | context, |
| 79 | contactDeltaList, "", 0, false, QuickContactActivity.class, |
| 80 | Intent.ACTION_VIEW, null, /* joinContactIdExtraKey =*/ null, |
| 81 | /* joinContactId =*/ null); |
| 82 | ContactSaveService.startService(context, intent); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public static boolean markAddToDefaultGroup(Contact contactData, |
| 87 | RawContactDeltaList rawContactDeltaList, Context context) { |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 88 | final long defaultGroupId = getDefaultGroupId(contactData.getGroupMetaData()); |
| 89 | // there should always be a default group (otherwise the button would be invisible), |
| 90 | // but let's be safe here |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 91 | if (defaultGroupId == -1) return false; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 92 | |
| 93 | // add the group membership to the current state |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 94 | final RawContactDelta rawContactEntityDelta = rawContactDeltaList.get(0); |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 95 | |
| 96 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance( |
| 97 | context); |
| 98 | final AccountType type = rawContactEntityDelta.getAccountType(accountTypes); |
| 99 | final DataKind groupMembershipKind = type.getKindForMimetype( |
| 100 | GroupMembership.CONTENT_ITEM_TYPE); |
| 101 | final ValuesDelta entry = RawContactModifier.insertChild(rawContactEntityDelta, |
| 102 | groupMembershipKind); |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 103 | if (entry == null) return false; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 104 | entry.setGroupRowId(defaultGroupId); |
Gary Mai | e487466 | 2016-09-26 11:42:54 -0700 | [diff] [blame] | 105 | return true; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** return default group id or -1 if no group or several groups are marked as default */ |
| 109 | private static long getDefaultGroupId(List<GroupMetaData> groups) { |
| 110 | long defaultGroupId = -1; |
| 111 | for (GroupMetaData group : groups) { |
Walter Jang | 428824e | 2016-09-09 13:18:35 -0700 | [diff] [blame] | 112 | if (group.defaultGroup) { |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 113 | // two default groups? return neither |
| 114 | if (defaultGroupId != -1) return -1; |
Walter Jang | 428824e | 2016-09-09 13:18:35 -0700 | [diff] [blame] | 115 | defaultGroupId = group.groupId; |
Brian Attwell | d28851f | 2014-06-10 13:25:07 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return defaultGroupId; |
| 119 | } |
| 120 | } |