Exemplo n.º 1
0
        /// <summary>
        /// Load contacts asynchronously, fact reading method of address book.
        /// </summary>
        /// <returns></returns>
        async Task <IList <Contact> > LoadContactsAsync()
        {
            IList <Contact> contacts      = new List <Contact>();
            var             hasPermission = await RequestPermissionAsync();

            if (hasPermission)
            {
                NSError error       = null;
                var     keysToFetch = new[] { CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.ImageDataAvailable, CNContactKey.ThumbnailImageData };

                var request = new CNContactFetchRequest(keysToFetch: keysToFetch);
                request.SortOrder = CNContactSortOrder.GivenName;

                using (var store = new CNContactStore())
                {
                    var result = store.EnumerateContacts(request, out error, new CNContactStoreListContactsHandler((CNContact c, ref bool stop) =>
                    {
                        string path = null;
                        if (c.ImageDataAvailable)
                        {
                            path = path = Path.Combine(Path.GetTempPath(), $"{ThumbnailPrefix}-{Guid.NewGuid()}");

                            if (!File.Exists(path))
                            {
                                var imageData = c.ThumbnailImageData;
                                imageData?.Save(path, true);
                            }
                        }

                        var contact = new Contact()
                        {
                            Name         = string.IsNullOrEmpty(c.FamilyName) ? c.GivenName : $"{c.GivenName} {c.FamilyName}",
                            Image        = path,
                            PhoneNumbers = c.PhoneNumbers?.Select(p => p?.Value?.StringValue).ToArray(),
                            Emails       = c.EmailAddresses?.Select(p => p?.Value?.ToString()).ToArray(),
                        };

                        if (!string.IsNullOrWhiteSpace(contact.Name))
                        {
                            OnContactLoaded?.Invoke(this, new ContactEventArgs(contact));

                            contacts.Add(contact);
                        }

                        stop = requestStop;
                    }));
                }
            }

            return(contacts);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load contacts asynchronously, fact reading method of address book.
        /// </summary>
        /// <returns></returns>
        async Task <IList <Contact> > LoadContactsAsync()
        {
            IList <Contact> contacts      = new List <Contact>();
            var             hasPermission = await RequestPermissionAsync();

            if (!hasPermission)
            {
                return(contacts);
            }

            var uri = ContactsContract.Contacts.ContentUri;
            var ctx = Application.Context;
            await Task.Run(() =>
            {
                // Only Contact ID, DisplayName and PhotoThumbnailUri are requested temporarily, which can be extended
                var cursor = ctx.ApplicationContext.ContentResolver.Query(uri, new string[]
                {
                    ContactsContract.Contacts.InterfaceConsts.Id,
                    ContactsContract.Contacts.InterfaceConsts.DisplayName,
                    ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri
                }, null, null, $"{ContactsContract.Contacts.InterfaceConsts.DisplayName} ASC");
                if (cursor.Count > 0)
                {
                    while (cursor.MoveToNext())
                    {
                        var contact = CreateContact(cursor, ctx);

                        if (!string.IsNullOrWhiteSpace(contact.Name))
                        {
                            // Read out a contact record, notify the shared library.
                            OnContactLoaded?.Invoke(this, new ContactEventArgs(contact));
                            contacts.Add(contact);
                        }

                        if (stopLoad)
                        {
                            break;
                        }
                    }
                }
            });

            return(contacts);
        }
Exemplo n.º 3
0
        async Task <IList <ContactModel> > LoadContactsAsync()
        {
            IList <ContactModel> contacts = new List <ContactModel>();
            var hasPermission             = await RequestPermissionAsync();

            if (hasPermission)
            {
                var uri = ContactsContract.Contacts.ContentUri;
                var ctx = Application.Context;
                await Task.Run(() =>
                {
                    var cursor = ctx.ApplicationContext.ContentResolver.Query(uri, new string[]
                    {
                        ContactsContract.Contacts.InterfaceConsts.Id,
                        ContactsContract.Contacts.InterfaceConsts.DisplayName,
                        ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri
                    }, null, null, $"{ContactsContract.Contacts.InterfaceConsts.DisplayName} ASC");
                    if (cursor.Count > 0)
                    {
                        while (cursor.MoveToNext())
                        {
                            var contact = CreateContact(cursor, ctx);

                            if (!string.IsNullOrWhiteSpace(contact.Name))
                            {
                                OnContactLoaded?.Invoke(this, new ContactEventArgs(contact));
                                contacts.Add(contact);
                            }

                            if (stopLoad)
                            {
                                break;
                            }
                        }
                    }
                });
            }
            return(contacts);
        }
Exemplo n.º 4
0
        async Task ReadContactsAsync()
        {
            var uri = ContactsContract.CommonDataKinds.Phone.ContentUri;

            string[] projection =
            {
                ContactsContract.Contacts.InterfaceConsts.Id,
                ContactsContract.Contacts.InterfaceConsts.DisplayName,
                ContactsContract.CommonDataKinds.Phone.Number
            };
            await Task.Run(() =>
            {
                var cursor = Application.Context.ContentResolver.Query(uri, projection, null, null, null);
                if (cursor.MoveToFirst())
                {
                    do
                    {
                        var contact         = new Contact();
                        contact.Id          = cursor.GetString(cursor.GetColumnIndex(projection[0]));
                        contact.Name        = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                        var number          = cursor.GetString(cursor.GetColumnIndex(projection[2]));
                        number              = number.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "");
                        number              = Regex.Replace(number, @".{1,}(?=\d{10}$)", string.Empty);
                        contact.PhoneNumber = number;
                        if (number.Length == 10)
                        {
                            OnContactLoaded?.Invoke(this, new ContactEventArgs(contact));
                        }
                        if (stopLoad)
                        {
                            break;
                        }
                    } while (cursor.MoveToNext());
                }
            });
        }