static Contact Extract(Windows.ApplicationModel.Contacts.Contact contact, ContactSearchParams searchParams) { var phones = contact.Phones.Select(p => new Contact.Phone { Number = p.Number, Type = p.Kind.ToString() }).ToList(); var result = new Contact { Id = contact.Id, FirstName = contact.FirstName, MiddleName = contact.MiddleName, LastName = contact.LastName, DisplayName = contact.DisplayName, PhoneNumbers = phones, Emails = contact.Emails.Select(e => e.Address).ToList(), Prefix = searchParams.IncludePrefix ? contact.HonorificNamePrefix : null, Suffix = searchParams.IncludeSuffix ? contact.HonorificNameSuffix : null, Nickname = searchParams.IncludeNickName ? contact.Nickname : null, Notes = searchParams.IncludeNotes ? contact.Notes : null, Relationships = searchParams.IncludeRelationships ? contact.SignificantOthers.Select(r => r.Name).ToList() : null, Websites = searchParams.IncludeWebsites ? contact.Websites.Select(w => w.Uri?.ToString()).ToList() : null, }; if (searchParams.IncludeOrganizations) { result.Organizations = contact.JobInfo.Select(og => new Contact.Organization { JobTitle = og.Title, CompanyName = og.CompanyName }).ToList(); } if (searchParams.IncludeImAccounts) { result.InstantMessagingAccounts = contact.ConnectedServiceAccounts.Select(ims => new Contact.InstantMessagingAccount { Service = ims.ServiceName, UserId = ims.Id }).ToList(); } if (searchParams.IncludeAddresses) { result.Addresses = contact.Addresses.Select(ad => new Contact.Address { City = ad.Locality, Country = ad.Country, PostalCode = ad.PostalCode, Region = ad.Region, StreetAddress = ad.StreetAddress }).ToList(); } return(result); }
static Task <List <Contact> > DoReadContacts(ContactSearchParams searchParams) { if (searchParams.NameKeywords.HasValue()) { return(Task.FromResult(SelectContacts(ContentResolver, new string[] { searchParams.NameKeywords }, searchParams).ToList())); } return(Task.FromResult(SelectContacts(ContentResolver, searchParams).ToList())); }
static async Task <List <Contact> > DoReadContacts(ContactSearchParams searchParams) { var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); IReadOnlyList <Windows.ApplicationModel.Contacts.Contact> contacts; if (searchParams.NameKeywords.HasValue()) { contacts = await store.FindContactsAsync(searchParams.NameKeywords); } else { contacts = await store.FindContactsAsync(); } return(contacts.Select(x => Extract(x, searchParams)).ToList()); }
/// <summary> /// Returns the contacts in the device operating system which match the specified search query. /// </summary> public static async Task <IEnumerable <Contact> > Search(ContactSearchParams searchParams, OnError errorAction = OnError.Alert) { try { if (await Permissions.Check(Permission.Contacts) != PermissionResult.Granted) { if (await Permissions.Request(Permission.Contacts) != PermissionResult.Granted) { await errorAction.Apply("Permission for reading device contacts not granted"); return(null); } } return(await DoReadContacts(searchParams)); } catch (Exception ex) { await errorAction.Apply(ex, "Failed to read contacts: " + ex.Message); return(null); } }
static IEnumerable <Contact> SelectContacts(ContentResolver content, ContactSearchParams searchParams) { var uri = ContactsContract.Contacts.ContentUri; ICursor cursor = null; try { cursor = content.Query(uri, null, null, null, null); if (cursor == null) { yield break; } foreach (var contact in SelectContacts(cursor, content, BATCH_SIZE, searchParams)) { yield return(contact); } } finally { cursor?.Close(); } }
static IEnumerable <Contact> SelectContacts(ICursor cursor, ContentResolver content, int batchSize, ContactSearchParams searchParams) { if (cursor == null) { yield break; } var column = ContactsContract.ContactsColumns.LookupKey; var ids = new string[batchSize]; var columnIndex = cursor.GetColumnIndex(column); var uniques = new HashSet <string>(); var counter = 0; while (cursor.MoveToNext()) { if (counter == batchSize) { counter = 0; foreach (var c in SelectContacts(content, ids, searchParams)) { yield return(c); } } var id = cursor.GetString(columnIndex); if (id == null || uniques.Contains(id)) { continue; } uniques.Add(id); ids[counter++] = id; } if (counter > 0) { foreach (var c in SelectContacts(content, ids.Take(counter).ToArray(), searchParams)) { yield return(c); } } }
static void FillContactWithRow(Contact contact, ICursor cursor, ContactSearchParams searchParams) { var dataType = cursor.GetString(cursor.GetColumnIndex(ContactsContract.DataColumns.Mimetype)); switch (dataType) { case ContactsContract.CommonDataKinds.Nickname.ContentItemType: if (searchParams.IncludeNickName) { contact.Nickname = cursor.GetString(cursor.GetColumnIndex(ContactsContract.CommonDataKinds.Nickname.Name)); } break; case StructuredName.ContentItemType: contact.FirstName = cursor.GetString(cursor.GetColumnIndex(StructuredName.GivenName)); contact.MiddleName = cursor.GetString(cursor.GetColumnIndex(StructuredName.MiddleName)); contact.LastName = cursor.GetString(cursor.GetColumnIndex(StructuredName.FamilyName)); if (searchParams.IncludeSuffix) { contact.Suffix = cursor.GetString(cursor.GetColumnIndex(StructuredName.Suffix)); } if (searchParams.IncludePrefix) { contact.Prefix = cursor.GetString(cursor.GetColumnIndex(StructuredName.Prefix)); } break; case ContactsContract.CommonDataKinds.Phone.ContentItemType: contact.PhoneNumbers.Add(GetPhone(cursor)); break; case ContactsContract.CommonDataKinds.Email.ContentItemType: contact.Emails.Add(GetEmail(cursor)); break; case ContactsContract.CommonDataKinds.Note.ContentItemType: if (searchParams.IncludeNotes) { contact.Notes = GetNote(cursor); } break; case ContactsContract.CommonDataKinds.Organization.ContentItemType: if (searchParams.IncludeOrganizations) { contact.Organizations.Add(GetOrganization(cursor)); } break; case StructuredPostal.ContentItemType: if (searchParams.IncludeAddresses) { contact.Addresses.Add(GetAddress(cursor)); } break; case InstantMessaging.ContentItemType: if (searchParams.IncludeImAccounts) { contact.InstantMessagingAccounts.Add(GetImAccount(cursor)); } break; case WebsiteData.ContentItemType: if (searchParams.IncludeWebsites) { contact.Websites.Add(GetWebsite(cursor)); } break; case Relation.ContentItemType: if (searchParams.IncludeRelationships) { contact.Relationships.Add(GetRelationship(cursor)); } break; default: break; } }
static void FillContactExtras(ContentResolver content, string recordId, Contact contact, ContactSearchParams searchParams) { if (recordId.IsEmpty()) { return; } var column = ContactsContract.ContactsColumns.LookupKey; ICursor cursor = null; try { cursor = content.Query(ContactsContract.Data.ContentUri, null, column + " = ?", new[] { recordId }, null); if (cursor == null) { return; } while (cursor.MoveToNext()) { FillContactWithRow(contact, cursor, searchParams); } } finally { cursor?.Close(); } }
static IEnumerable <Contact> SelectContacts(ContentResolver content, string[] ids, ContactSearchParams searchParams) { ICursor cursor = null; var column = ContactsContract.ContactsColumns.LookupKey; var whereb = new StringBuilder(); for (var i = 0; i < ids.Length; i++) { if (i > 0) { whereb.Append(" OR "); } whereb.Append(column); whereb.Append("=?"); } var xCounter = 0; var map = new Dictionary <string, Contact>(ids.Length); try { Contact currentContact = null; cursor = content.Query(ContactsContract.Data.ContentUri, null, whereb.ToString(), ids, ContactsContract.ContactsColumns.LookupKey); if (cursor == null) { yield break; } var idIndex = cursor.GetColumnIndex(column); var dnIndex = cursor.GetColumnIndex(ContactsContract.ContactsColumns.DisplayName); while (cursor.MoveToNext()) { var id = cursor.GetString(idIndex); if (currentContact == null || currentContact.Id != id) { // We need to yield these in the original ID order if (currentContact != null) { if (currentContact.Id == ids[xCounter]) { yield return(currentContact); xCounter++; } else { map.Add(currentContact.Id, currentContact); } } currentContact = new Contact { Id = id, IsAggregate = true, Tag = content, DisplayName = cursor.GetString(dnIndex) }; if (searchParams.IncludeImage) { try { currentContact.PhotoData = ContentUriConverter.GetFileData(GetString(cursor, ContactsContract.Contacts.InterfaceConsts.PhotoUri)); currentContact.PhotoDataThumbnail = ContentUriConverter.GetFileData(GetString(cursor, ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri)); } catch { } } } FillContactWithRow(currentContact, cursor, searchParams); } if (currentContact != null) { map.Add(currentContact.Id, currentContact); } for (; xCounter < ids.Length; xCounter++) { if (map.TryGetValue(ids[xCounter], out Contact tContact)) { yield return(tContact); } } } finally { cursor?.Close(); } }
static Contact Extract(ABPerson data, ContactSearchParams searchParams) { var result = new Contact { Id = data.Id.ToString(), FirstName = data.FirstName, MiddleName = data.MiddleName, LastName = data.LastName, DisplayName = data.ToString(), PhoneNumbers = data.GetPhones().Select(ph => new Contact.Phone { Number = ph.Value }).ToList(), Emails = data.GetEmails().Select(e => e.Value).Trim().ToList(), Prefix = searchParams.IncludePrefix ? data.Prefix : null, Suffix = searchParams.IncludeSuffix ? data.Suffix : null, Nickname = searchParams.IncludeNickName ? data.Nickname : null, Notes = searchParams.IncludeNotes ? data.Note : null, Relationships = searchParams.IncludeRelationships ? data.GetRelatedNames().Select(r => r.Value).ToList() : null, Websites = searchParams.IncludeWebsites ? data.GetUrls().Select(w => w.Value).ToList() : null }; if (searchParams.IncludeOrganizations) { result.Organizations = new List <Contact.Organization> { new Contact.Organization { CompanyName = data.Organization, JobTitle = data.JobTitle } } } ; if (searchParams.IncludeImAccounts) { result.InstantMessagingAccounts = data.GetInstantMessageServices().Select(im => new Contact.InstantMessagingAccount { Service = im.Value.ServiceName, UserId = im.Value.Username }).ToList(); } if (searchParams.IncludeAddresses) { result.Addresses = data.GetAllAddresses().Select(ad => new Contact.Address { City = ad.Value.City, Country = ad.Value.Country, PostalCode = ad.Value.Zip, Region = ad.Value.State, StreetAddress = ad.Value.Street }).ToList(); } try { if (searchParams.IncludeImage && data.HasImage) { result.PhotoData = data.GetImage(ABPersonImageFormat.OriginalSize).ToArray(); result.PhotoDataThumbnail = data.GetImage(ABPersonImageFormat.Thumbnail).ToArray(); } } catch { } return(result); } }
static Task <List <Contact> > DoReadContacts(ContactSearchParams searchParams) { var people = searchParams.NameKeywords.HasValue() ? Addressbook.GetPeopleWithName(searchParams.NameKeywords) : Addressbook.GetPeople(); return(Task.FromResult(people.Select(x => Extract(x, searchParams)).ToList())); }