public override void ViewDidLoad () { base.ViewDidLoad (); SetupUI(); var book = new AddressBook (); // We must request permission to access the user's address book // This will prompt the user on platforms that ask, or it will validate // manifest permissions on platforms that declare their required permissions. book.RequestPermission().ContinueWith (t => { if (!t.Result) { alert = new UIAlertView ("Permission denied", "User has denied this app access to their contacts", null, "Close"); alert.Show(); } else { //Add a new contact to the address book var contact = new Contact(); contact.FirstName = "Rene"; book.Save(contact); // Contacts can be selected and sorted using LINQ! // // In this sample, we'll just use LINQ to sort contacts by // their last name in reverse order. list = book.OrderByDescending (c => c.LastName).ToList(); tableView.Source = new TableViewDataSource (list); tableView.ReloadData(); } }, TaskScheduler.FromCurrentSynchronizationContext()); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // We must request permission to access the user's address book // This will prompt the user on platforms that ask, or it will validate // manifest permissions on platforms that declare their required permissions. AddressBook.RequestPermission().ContinueWith (t => { if (!t.Result) { Toast.MakeText (this, "Permission denied, check your manifest", ToastLength.Long).Show(); return; } //Create new Contact var newContact = new Contact(); newContact.FirstName = "Miguel"; newContact.LastName = "de EverybodyKnows"; newContact.Addresses = new List<Address>() { new Address() { StreetAddress = "Wili Street 1", PostalCode = "5331", City = "San Francisco" }}; newContact.Emails = new List<Email>() { new Email() { Label = "Work", Address = "*****@*****.**" }}; newContact.Phones = new List<Phone>() { new Phone() { Label = "Work", Number = "+1 (855) 926-2746" } }; newContact.Organizations = new List<Organization>() { new Organization() { Name = "Xamarin" } }; newContact.Websites = new List<Website>() { new Website() { Address = "http://www.xamarin.com" } }; AddressBook.Save (newContact); //set WriteContacts in your manifest // Contacts can be selected and sorted using LINQ! // // In this sample, we'll just use LINQ to sort contacts by // their last name in reverse order. foreach (Contact contact in AddressBook.Where (c => c.FirstName != null).OrderBy (c => c.FirstName)) { contacts.Add (contact.DisplayName); contactIDs.Add (contact.Id); // Save the ID in a parallel list } ListAdapter = new ArrayAdapter<string> (this, Resource.Layout.list_item, contacts.ToArray()); ListView.TextFilterEnabled = true; // When clicked, start a new activity to display more contact details ListView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) { // To show the contact on the details activity, we // need to send that activity the contacts ID string contactId = contactIDs[args.Position]; Intent showContactDetails = new Intent (this, typeof(ContactActivity)); showContactDetails.PutExtra ("contactID", contactId); StartActivity (showContactDetails); // Alternatively, show a toast with the name of the contact selected // //Toast.MakeText (Application, ((TextView)args.View).Text, ToastLength.Short).Show (); }; }, TaskScheduler.FromCurrentSynchronizationContext()); }
private static void FillContactWithRow (Resources resources, Contact contact, ICursor c) { string dataType = c.GetString (c.GetColumnIndex (ContactsContract.DataColumns.Mimetype)); switch (dataType) { case ContactsContract.CommonDataKinds.Nickname.ContentItemType: contact.Nickname = c.GetString (c.GetColumnIndex (ContactsContract.CommonDataKinds.Nickname.Name)); break; case StructuredName.ContentItemType: contact.Prefix = c.GetString (StructuredName.Prefix); contact.FirstName = c.GetString (StructuredName.GivenName); contact.MiddleName = c.GetString (StructuredName.MiddleName); contact.LastName = c.GetString (StructuredName.FamilyName); contact.Suffix = c.GetString (StructuredName.Suffix); break; case ContactsContract.CommonDataKinds.Phone.ContentItemType: contact.phones.Add (GetPhone (c, resources)); break; case ContactsContract.CommonDataKinds.Email.ContentItemType: contact.emails.Add (GetEmail (c, resources)); break; case ContactsContract.CommonDataKinds.Note.ContentItemType: contact.notes.Add (GetNote (c, resources)); break; case ContactsContract.CommonDataKinds.Organization.ContentItemType: contact.organizations.Add (GetOrganization (c, resources)); break; case StructuredPostal.ContentItemType: contact.addresses.Add (GetAddress (c, resources)); break; case InstantMessaging.ContentItemType: contact.instantMessagingAccounts.Add (GetImAccount (c, resources)); break; case WebsiteData.ContentItemType: contact.websites.Add (GetWebsite (c, resources)); break; case Relation.ContentItemType: contact.relationships.Add (GetRelationship (c, resources)); break; } }
internal static void FillContactExtras (bool rawContact, ContentResolver content, Resources resources, string recordId, Contact contact) { ICursor c = null; string column = (rawContact) ? ContactsContract.RawContactsColumns.ContactId : ContactsContract.ContactsColumns.LookupKey; try { c = content.Query (ContactsContract.Data.ContentUri, null, column + " = ?", new[] { recordId }, null); if (c == null) return; while (c.MoveToNext()) FillContactWithRow (resources, contact, c); } finally { if (c != null) c.Close(); } }
internal static Contact GetContact (bool rawContact, ContentResolver content, Resources resources, ICursor cursor) { string id = (rawContact) ? cursor.GetString (cursor.GetColumnIndex (ContactsContract.RawContactsColumns.ContactId)) : cursor.GetString (cursor.GetColumnIndex (ContactsContract.ContactsColumns.LookupKey)); Contact contact = new Contact (id, !rawContact, content); contact.DisplayName = GetString (cursor, ContactsContract.ContactsColumns.DisplayName); FillContactExtras (rawContact, content, resources, id, contact); return contact; }
internal static IEnumerable<Contact> GetContacts (bool rawContacts, ContentResolver content, Resources resources, string[] ids) { ICursor c = null; string column = (rawContacts) ? ContactsContract.RawContactsColumns.ContactId : ContactsContract.ContactsColumns.LookupKey; StringBuilder whereb = new StringBuilder(); for (int i = 0; i < ids.Length; i++) { if (i > 0) whereb.Append (" OR "); whereb.Append (column); whereb.Append ("=?"); } int x = 0; var map = new Dictionary<string, Contact> (ids.Length); try { Contact currentContact = null; c = content.Query (ContactsContract.Data.ContentUri, null, whereb.ToString(), ids, ContactsContract.ContactsColumns.LookupKey); if (c == null) yield break; int idIndex = c.GetColumnIndex (column); int dnIndex = c.GetColumnIndex (ContactsContract.ContactsColumns.DisplayName); while (c.MoveToNext()) { string id = c.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[x]) { yield return currentContact; x++; } else map.Add (currentContact.Id, currentContact); } currentContact = new Contact (id, !rawContacts, content); currentContact.DisplayName = c.GetString (dnIndex); } FillContactWithRow (resources, currentContact, c); } if (currentContact != null) map.Add (currentContact.Id, currentContact); for (; x < ids.Length; x++) yield return map[ids[x]]; } finally { if (c != null) c.Close(); } }
public DetailContactView (Contact c) { this.contact = c; }
internal static Contact GetContact (ABPerson person) { Contact contact = new Contact (person) { DisplayName = person.ToString(), Prefix = person.Prefix, FirstName = person.FirstName, MiddleName = person.MiddleName, LastName = person.LastName, Suffix = person.Suffix, Nickname = person.Nickname }; contact.Notes = (person.Note != null) ? new [] { new Note { Contents = person.Note } } : new Note[0]; contact.Emails = person.GetEmails().Select (e => new Email { Address = e.Value, Type = GetEmailType (e.Label), Label = (e.Label != null) ? GetLabel (e.Label) : GetLabel (ABLabel.Other) }); contact.Phones = person.GetPhones().Select (p => new Phone { Number = p.Value, Type = GetPhoneType (p.Label), Label = (p.Label != null) ? GetLabel (p.Label) : GetLabel (ABLabel.Other) }); Organization[] orgs; if (person.Organization != null) { orgs = new Organization[1]; orgs[0] = new Organization { Name = person.Organization, ContactTitle = person.JobTitle, Type = OrganizationType.Work, Label = GetLabel (ABLabel.Work) }; } else orgs = new Organization[0]; contact.Organizations = orgs; contact.InstantMessagingAccounts = person.GetInstantMessages().Select (ima => new InstantMessagingAccount() { Service = GetImService ((NSString)ima.Value[ABPersonInstantMessageKey.Service]), ServiceLabel = (NSString)ima.Value[ABPersonInstantMessageKey.Service], Account = (NSString)ima.Value[ABPersonInstantMessageKey.Username] }); contact.Addresses = person.GetAddresses().Select (a => new Address() { Type = GetAddressType (a.Label), Label = (a.Label != null) ? GetLabel (a.Label) : GetLabel (ABLabel.Other), StreetAddress = (NSString)a.Value[ABPersonAddressKey.Street], City = (NSString)a.Value[ABPersonAddressKey.City], Region = (NSString)a.Value[ABPersonAddressKey.State], Country = (NSString)a.Value[ABPersonAddressKey.Country], PostalCode = (NSString)a.Value[ABPersonAddressKey.Zip] }); contact.Websites = person.GetUrls().Select (url => new Website { Address = url.Value }); contact.Relationships = person.GetRelatedNames().Select (p => new Relationship { Name = p.Value, Type = GetRelationType (p.Label) }); return contact; }