public async Task<List<string>> GetContacts() { List<string> contactList = null; try { await Task.Run(async () => { contactList = new List<string>(); var book = new AddressBook(MainActivity.GetMainActivity()); if (!await book.RequestPermission()) { Toast.MakeText(MainActivity.GetMainActivity(), "Permission denied.", ToastLength.Short); return; } //foreach (Xamarin.Contacts.Contact contact in book.OrderBy(c => c.LastName)) try { book.OrderBy(c => c.DisplayName); } catch (Exception ex) { var test = ex.Message; } foreach (Xamarin.Contacts.Contact contact in book) { if (contact.FirstName != null && contact.FirstName.Trim().Length > 0 && contact.Phones != null && contact.Phones.Count() > 0) contactList.Add(contact.FirstName); } }); } catch (Exception ex) { var test = ex.Message; } return contactList; }
private void AddContactsData(String contactFilter) { // Figure out where the SQLite database will be. bool showAll = contactFilter != "Securecom users"; var conn = new SQLite.SQLiteConnection(AppDelegate._dbPath); List<PushContact> pc = conn.Query<PushContact>("select * from PushContact"); conn.Close(); List<String> registeredContacts = new List<String>(); List<String> groups = new List<String>(); foreach (PushContact c in pc) registeredContacts.Add(c.Number); var phoneUtil = PhoneNumberUtil.GetInstance(); if (!showAll) { Dictionary<String, List<String>> map = new Dictionary<String, List<String>>(); foreach (PushContact c in pc) { String n = c.Name ?? c.Number; if (!map.ContainsKey(n)) map[n] = new List<String>(); map[n].Add(c.Number); } foreach (KeyValuePair<String, List<String>> entry in map.OrderBy(c => c.Key)) { String group = entry.Key.Substring(0, 1).ToUpper(); bool newGroup = !groups.Contains(group); foreach (CustomCellGroup ccg in cellGroups) { if (ccg.Name.Equals(group)) { newGroup = false; tableCellGroup = ccg; } } ContactListCell cell = ContactListCell.Create(); cell.SetName(entry.Key); foreach (String number in entry.Value) { if (number.Contains("@")) cell.SetEmail(number); else cell.SetPhone(number); } if (newGroup) { tableCellGroup = new CustomCellGroup { Name = group }; cellGroups.Add(tableCellGroup); } tableCellGroup.Cells.Add(cell); } return; } AddressBook book = new AddressBook(); book.RequestPermission().ContinueWith(t => { if (!t.Result) { Console.WriteLine("Permission denied by user or manifest"); return; } }, TaskScheduler.FromCurrentSynchronizationContext()); foreach (Contact contact in book.OrderBy(c => c.DisplayName)) { if (!showAll && registeredContacts.Count == 0) break; if (String.IsNullOrEmpty(contact.DisplayName)) continue; String group = contact.DisplayName.Substring(0, 1).ToUpper(); bool newGroup = !groups.Contains(group); foreach (CustomCellGroup ccg in cellGroups) { if (ccg.Name.Equals(group)) { newGroup = false; tableCellGroup = ccg; } } ContactListCell cell = ContactListCell.Create(); cell.SetName(contact.DisplayName); cell.SetEmail(null); cell.SetPhone(null); if (contact.Phones.Any()) { foreach (Phone p in contact.Phones) { if (showAll) { cell.SetPhone(p.Number); cell.registeredState = ContactListCell.STATE_PENDING; break; } if (p.Number.Contains("*") || p.Number.Contains("#")) continue; String number; try { number = phoneUtil.Format(phoneUtil.Parse(p.Number, AppDelegate.GetCountryCode()), PhoneNumberFormat.E164); } catch (Exception e) { continue; } if (!registeredContacts.Contains(number)) continue; registeredContacts.Remove(number); cell.SetPhone(p.Number); cell.registeredState = ContactListCell.STATE_REGISTERED; //conn.Execute("UPDATE PushContact Set Name = ? WHERE Number = ?", contact.DisplayName, number); break; } } if (contact.Emails.Any()) { foreach (Email e in contact.Emails) { if (showAll) { cell.SetEmail(e.Address); cell.registeredState = ContactListCell.STATE_PENDING; break; } if (!registeredContacts.Contains(e.Address)) continue; registeredContacts.Remove(e.Address); cell.SetEmail(e.Address); cell.registeredState = ContactListCell.STATE_REGISTERED; //conn.Execute("UPDATE PushContact Set Name = ? WHERE Number = ?", contact.DisplayName, e.Address); break; } } if (cell._email == null && cell.mobile == null) continue; if (newGroup) { tableCellGroup = new CustomCellGroup { Name = group }; cellGroups.Add(tableCellGroup); } tableCellGroup.Cells.Add(cell); } //conn.Close(); }
private void ProcessSearchOnAllContacts(string searchText) { List<CustomCellGroup> mCellGroups = new List<CustomCellGroup>(); CustomCellGroup mTableCellGroup = new CustomCellGroup(); AddressBook book = new AddressBook(); foreach (Contact contact in book.OrderBy(c => c.DisplayName)) { if (String.IsNullOrEmpty(contact.DisplayName)) continue; bool found = false; //Decide contact group based on the first character String group = contact.DisplayName.Substring(0, 1).ToUpper(); if (contact.DisplayName.ToLower().Contains(searchText)) { found = true; } else if (contact.Phones.Any()) { foreach (Phone p in contact.Phones) { String temp = p.Number .Replace("(", string.Empty) .Replace(")", string.Empty) .Replace("-", string.Empty) .Replace(" ", string.Empty); found |= temp.Contains(searchText); } } else if (contact.Emails.Any()) { foreach (Email e in contact.Emails) { found |= e.Address.ToLower().Contains(searchText); } } if (!found) continue; ContactListCell cell = ContactListCell.Create(); cell.SetName(contact.DisplayName); foreach (Phone p in contact.Phones) { cell.SetPhone(p.Number); break; } foreach (Email e in contact.Emails) { cell.SetEmail(e.Address); break; } mTableCellGroup.Cells.Add(cell); } mTableCellGroup.Name = mTableCellGroup.Cells.Count == 0 ? "No Results" : "Search Results"; mCellGroups.Add(mTableCellGroup); source = new CustomCellTableSource(mCellGroups); source.RowSelectedAction = RowSelected; table.Source = source; }