public void search(string searchCriteria)
        {
            string[] args = JSON.JsonHelper.Deserialize <string[]>(searchCriteria);

            ContactSearchParams searchParams = new ContactSearchParams();

            try
            {
                searchParams.fields  = JSON.JsonHelper.Deserialize <string[]>(args[0]);
                searchParams.options = JSON.JsonHelper.Deserialize <SearchOptions>(args[1]);
            }
            catch (Exception)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, INVALID_ARGUMENT_ERROR));
                return;
            }
            if (searchParams.options == null)
            {
                searchParams.options          = new SearchOptions();
                searchParams.options.filter   = "";
                searchParams.options.multiple = true;
            }
            else if (searchParams.options.filter == null)
            {
                searchParams.options.filter = "";
            }

            DeviceContacts deviceContacts = new DeviceContacts();

            deviceContacts.SearchCompleted += new EventHandler <ContactsSearchEventArgs>(contacts_SearchCompleted);

            // default is to search all fields
            FilterKind filterKind = FilterKind.None;

            // if only one field is specified, we will try the 3 available DeviceContact search filters
            if (searchParams.fields.Count() == 1)
            {
                if (searchParams.fields.Contains("name"))
                {
                    filterKind = FilterKind.DisplayName;
                }
                else if (searchParams.fields.Contains("emails"))
                {
                    filterKind = FilterKind.EmailAddress;
                }
                else if (searchParams.fields.Contains("phoneNumbers"))
                {
                    filterKind = FilterKind.PhoneNumber;
                }
            }

            try
            {
                deviceContacts.SearchAsync(searchParams.options.filter, filterKind, searchParams);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("search contacts exception :: " + ex.Message);
            }
        }
Exemple #2
0
        public void search(string searchCriteria)
        {
            string[] args = JSON.JsonHelper.Deserialize<string[]>(searchCriteria);

            ContactSearchParams searchParams = new ContactSearchParams();
            try
            {
                searchParams.fields = JSON.JsonHelper.Deserialize<string[]>(args[0]);
                searchParams.options = JSON.JsonHelper.Deserialize<SearchOptions>(args[1]);
            }
            catch (Exception)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, INVALID_ARGUMENT_ERROR));
                return;
            }

            if (searchParams.options == null)
            {
                searchParams.options = new SearchOptions();
                searchParams.options.filter = "";
                searchParams.options.multiple = true;
            }
            else if (searchParams.options.filter == null)
            {
                searchParams.options.filter = "";
            }

            DeviceContacts deviceContacts = new DeviceContacts();
            deviceContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);

            // default is to search all fields
            FilterKind filterKind = FilterKind.None;
            // if only one field is specified, we will try the 3 available DeviceContact search filters
            if (searchParams.fields.Count() == 1)
            {
                if (searchParams.fields.Contains("name"))
                {
                    filterKind = FilterKind.DisplayName;
                }
                else if (searchParams.fields.Contains("emails"))
                {
                    filterKind = FilterKind.EmailAddress;
                }
                else if (searchParams.fields.Contains("phoneNumbers"))
                {
                    filterKind = FilterKind.PhoneNumber;
                }
            }

            try
            {

                deviceContacts.SearchAsync(searchParams.options.filter, filterKind, searchParams);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("search contacts exception :: " + ex.Message);
            }
        }
Exemple #3
0
        private void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            ContactSearchParams searchParams = (ContactSearchParams)e.State;

            List <Contact> foundContacts = null;

            // if we have multiple search fields
            if (searchParams.options.filter.Length > 0 && searchParams.fields.Count() > 1)
            {
                foundContacts = new List <Contact>();
                if (searchParams.fields.Contains("emails"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           from ContactEmailAddress a in con.EmailAddresses
                                           where a.EmailAddress.Contains(searchParams.options.filter)
                                           select con);
                }
                if (searchParams.fields.Contains("displayName"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           where con.DisplayName.Contains(searchParams.options.filter)
                                           select con);
                }
                if (searchParams.fields.Contains("name"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           where con.CompleteName != null && con.CompleteName.ToString().Contains(searchParams.options.filter)
                                           select con);
                }
                if (searchParams.fields.Contains("phoneNumbers"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           from ContactPhoneNumber a in con.PhoneNumbers
                                           where a.PhoneNumber.Contains(searchParams.options.filter)
                                           select con);
                }
                if (searchParams.fields.Contains("urls"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           from string a in con.Websites
                                           where a.Contains(searchParams.options.filter)
                                           select con);
                }
            }
            else
            {
                foundContacts = new List <Contact>(e.Results);
            }

            //List<string> contactList = new List<string>();

            string strResult = "";

            IEnumerable <Contact> distinctContacts = foundContacts.Distinct();

            foreach (Contact contact in distinctContacts)
            {
                strResult += FormatJSONContact(contact, null) + ",";
                //contactList.Add(FormatJSONContact(contact, null));
                if (!searchParams.options.multiple)
                {
                    break; // just return the first item
                }
            }
            PluginResult result = new PluginResult(PluginResult.Status.OK);

            result.Message = "[" + strResult.TrimEnd(',') + "]";
            DispatchCommandResult(result);
        }
        private void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            ContactSearchParams searchParams = (ContactSearchParams)e.State;

            List <Contact> foundContacts = null;
            // used for comparing strings, ""  instantiates with InvariantCulture
            CultureInfo culture = new CultureInfo("");
            // make the search comparisons case insensitive.
            CompareOptions compare_option = CompareOptions.IgnoreCase;

            // if we have multiple search fields

            if (!String.IsNullOrEmpty(searchParams.options.filter) && searchParams.fields.Count() > 1)
            {
                foundContacts = new List <Contact>();
                if (searchParams.fields.Contains("emails"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           from ContactEmailAddress a in con.EmailAddresses
                                           where culture.CompareInfo.IndexOf(a.EmailAddress, searchParams.options.filter, compare_option) >= 0
                                           select con);
                }
                if (searchParams.fields.Contains("displayName"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           where culture.CompareInfo.IndexOf(con.DisplayName, searchParams.options.filter, compare_option) >= 0
                                           select con);
                }
                if (searchParams.fields.Contains("name"))
                {
                    foundContacts.AddRange(
                        from Contact con in e.Results
                        where con.CompleteName != null && (
                            (con.CompleteName.FirstName != null && culture.CompareInfo.IndexOf(con.CompleteName.FirstName, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.LastName != null && culture.CompareInfo.IndexOf(con.CompleteName.LastName, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.MiddleName != null && culture.CompareInfo.IndexOf(con.CompleteName.MiddleName, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.Nickname != null && culture.CompareInfo.IndexOf(con.CompleteName.Nickname, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.Suffix != null && culture.CompareInfo.IndexOf(con.CompleteName.Suffix, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.Title != null && culture.CompareInfo.IndexOf(con.CompleteName.Title, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.YomiFirstName != null && culture.CompareInfo.IndexOf(con.CompleteName.YomiFirstName, searchParams.options.filter, compare_option) >= 0) ||
                            (con.CompleteName.YomiLastName != null && culture.CompareInfo.IndexOf(con.CompleteName.YomiLastName, searchParams.options.filter, compare_option) >= 0))
                        select con);
                }
                if (searchParams.fields.Contains("phoneNumbers"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           from ContactPhoneNumber a in con.PhoneNumbers
                                           where culture.CompareInfo.IndexOf(a.PhoneNumber, searchParams.options.filter, compare_option) >= 0
                                           select con);
                }
                if (searchParams.fields.Contains("urls"))
                {
                    foundContacts.AddRange(from Contact con in e.Results
                                           from string a in con.Websites
                                           where culture.CompareInfo.IndexOf(a, searchParams.options.filter, compare_option) >= 0
                                           select con);
                }
            }
            else
            {
                foundContacts = new List <Contact>(e.Results);
            }

            //List<string> contactList = new List<string>();

            string strResult = "";

            IEnumerable <Contact> distinctContacts = foundContacts.Distinct();

            foreach (Contact contact in distinctContacts)
            {
                strResult += FormatJSONContact(contact, null) + ",";
                //contactList.Add(FormatJSONContact(contact, null));
                if (!searchParams.options.multiple)
                {
                    break; // just return the first item
                }
            }
            PluginResult result = new PluginResult(PluginResult.Status.OK);

            result.Message = "[" + strResult.TrimEnd(',') + "]";
            DispatchCommandResult(result);
        }