Пример #1
0
        private void searchByVoice(object sender, EventArgs e)
        {
            MainActivity act        = (MainActivity)this.Activity;
            SearchView   searchView = (SearchView)act.FindViewById(Resource.Id.searchView1);

            //If coming from the text search focus, get rid of it.
            if (flagSearch)
            {
                searchView.Focusable = false;
                searchView.SetIconifiedByDefault(true);
                searchView.OnActionViewCollapsed();
            }

            Intent intent = new Intent(RecognizerIntent.ActionRecognizeSpeech);

            try
            {
                intent.PutExtra(RecognizerIntent.ActionRecognizeSpeech, "en-US");
                intent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
                intent.PutExtra(RecognizerIntent.ExtraLanguage, LocaleList.Default);
                intent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 8000);

                act.StartActivityForResult(intent, 100);
            }catch (ActivityNotFoundException)
            {
                Toast t = Toast.MakeText(this.Activity, "Your device doesn't support Speech to Text", ToastLength.Short);
                t.Show();
            }
        }
        private void ConnectSearchView()
        {
            var ctx = (Activity)context;

            if (CoreSettings.SearchView != 0)
            {
                _searchView = ctx?.FindViewById <SearchView>(CoreSettings.SearchView);
            }

            if (_searchView == null)
            {
                return;
            }

            var contentPage = Element as ContentPage;

            if (contentPage == null)
            {
                _searchView.Visibility = ViewStates.Gone;
                return;
            }

            _searchProvider = contentPage.BindingContext as ISearchProvider;

            if (_searchProvider == null)
            {
                _searchView.Visibility = ViewStates.Gone;
                return;
            }

            if (!string.IsNullOrEmpty(_searchProvider.QueryHint))
            {
                _searchView.SetQueryHint(_searchProvider.QueryHint);
            }

            _searchView.QueryTextSubmit += HandleQueryTextSubmit;

            if (_searchProvider.SearchIsDefaultAction)
            {
                _searchView.OnActionViewExpanded();
            }
            else
            {
                _searchView.OnActionViewCollapsed();
            }

            _searchView.Visibility = ViewStates.Visible;
        }
Пример #3
0
        private void searchByText(object sender, EventArgs e)
        {
            //change focus off button
            Button mButton = (Button)sender;

            mButton.Focusable = false;

            //Get activity and the searchView. Set Listener on it
            MainActivity act     = (MainActivity)this.Activity;
            SearchView   searchV = (SearchView)act.FindViewById(Resource.Id.searchView1);

            //loses focus on search view if it already given focus before ((allows for backout without querying and pressing the button again))
            if (flagSearch)
            {
                searchV.Focusable = false;
                searchV.SetIconifiedByDefault(true);
                searchV.OnActionViewCollapsed();
            }

            searchV.RequestFocus();
            searchV.SetIconifiedByDefault(false);
            searchV.OnActionViewExpanded();
            flagSearch = true;
        }
Пример #4
0
        private async void submitQueryListener(object sender, SearchView.QueryTextSubmitEventArgs e)
        {
            MainActivity act        = (MainActivity)this.Activity;
            SearchView   searchView = (SearchView)act.FindViewById(Resource.Id.searchView1);
            string       input      = e.Query;

            searchView.SetIconifiedByDefault(true);
            searchView.OnActionViewCollapsed();
            searchView.Focusable = false;

            //Ping database by name
            string data = GetData();

            JArray        jsonArray    = JArray.Parse(data);
            List <string> searched_Loc = new List <string>();
            List <string> similar_Loc  = new List <string>();

            string debugMe    = "";
            string tempinput  = input;
            string inputBlock = tempinput;

            tempinput = RemoveSpecialCharacters(tempinput);
            tempinput = tempinput.Replace(" ", System.String.Empty);

            AddressLocator        tempAddress;
            List <AddressLocator> mAddresses = new List <AddressLocator>();

            for (int i = 0; i < jsonArray.Count; i++)
            {
                JToken json = jsonArray[i];
                string temp = (string)json["name"];
                temp = RemoveSpecialCharacters(temp);
                temp = temp.Replace(" ", System.String.Empty);
                if (((string)json["name"]).Equals(input, StringComparison.InvariantCultureIgnoreCase) || temp.Equals(tempinput, StringComparison.InvariantCultureIgnoreCase))
                {
                    //Location stuff - make sure user location permissions were give
                    if (act.CheckSelfPermission(Android.Manifest.Permission.AccessCoarseLocation) == (int)Android.Content.PM.Permission.Granted)
                    {
                        Geocoder        coder   = new Geocoder(act);
                        IList <Address> address = new List <Address>();


                        address = coder.GetFromLocationName(((string)json["street"]) + " " + ((string)json["city"]) + " " + ((string)json["state"]), 5);

                        float lon = (float)address.ElementAt(0).Longitude;
                        float lat = (float)address.ElementAt(0).Latitude;

                        tempAddress = new AddressLocator((string)json["name"], ((string)json["street"]) + " " + ((string)json["city"]) + " " + ((string)json["state"]), lon, lat);
                        mAddresses.Add(tempAddress);


                        userLocationObtained = true;
                    }
                    else // if not, grab list like before.
                    {
                        searched_Loc.Add(((string)json["name"]) + ": " + ((string)json["street"]) + " " + ((string)json["city"]) + " " + ((string)json["state"]));
                    }
                }
            }

            if (userLocationObtained)
            {
                //calculate distances
                CalculateAddressDistance(mAddresses);
                //sort distances
                mAddresses.Sort();

                for (int x = 0; x < mAddresses.Count; x++)
                {
                    searched_Loc.Add(mAddresses.ElementAt(x).Name + ": " + mAddresses.ElementAt(x).Address + " (" + mAddresses.ElementAt(x).Distance.ToString("n2") + " miles)");
                }
            }

            for (int j = 0; j < searched_Loc.Count; j++)
            {
                debugMe += searched_Loc[j];
                debugMe += "\n";
            }

            if (searched_Loc.Count == 0)
            {
                for (int i = 0; i < jsonArray.Count; i++)
                {
                    JToken json      = jsonArray[i];
                    bool   wordMatch = false;
                    string temp      = (string)json["name"];

                    string[] outputBlock = temp.Split(" ");

                    temp = RemoveSpecialCharacters(temp);
                    temp = temp.Replace(" ", System.String.Empty);

                    wordMatch = InputsMatch(inputBlock.ToLower(), outputBlock);

                    if ((tempinput.StartsWith(temp.Substring(0, 2), StringComparison.InvariantCultureIgnoreCase) || wordMatch) && !userLocationObtained)
                    {
                        similar_Loc.Add(((string)json["name"]) + ": " + ((string)json["street"]) + " " + ((string)json["city"]) + " " + ((string)json["state"]));
                    }
                    else if ((tempinput.StartsWith(temp.Substring(0, 2), StringComparison.InvariantCultureIgnoreCase) || wordMatch) && userLocationObtained)
                    {
                        Geocoder        coder   = new Geocoder(act);
                        IList <Address> address = new List <Address>();


                        address = coder.GetFromLocationName(((string)json["street"]) + " " + ((string)json["city"]) + " " + ((string)json["state"]), 5);

                        float lon = (float)address.ElementAt(0).Longitude;
                        float lat = (float)address.ElementAt(0).Latitude;

                        tempAddress = new AddressLocator((string)json["name"], ((string)json["street"]) + " " + ((string)json["city"]) + " " + ((string)json["state"]), lon, lat);
                        mAddresses.Add(tempAddress);
                    }
                }
                if (userLocationObtained)
                {
                    //calculate distances
                    CalculateAddressDistance(mAddresses);
                    //sort distances
                    mAddresses.Sort();

                    for (int x = 0; x < mAddresses.Count; x++)
                    {
                        similar_Loc.Add(mAddresses.ElementAt(x).Name + ": " + mAddresses.ElementAt(x).Address + " (" + mAddresses.ElementAt(x).Distance.ToString("n2") + " miles)");
                    }
                }
            }

            mTv = act.FindViewById <ListView>(Resource.Id.searchResults);
            ArrayAdapter <string> arrayAdapter;

            if (searched_Loc.Count == 0 && similar_Loc.Count == 0)
            {
                Toast.MakeText(MainActivity.activity, "There were no results for that Location.", ToastLength.Long).Show();
                arrayAdapter = new ArrayAdapter <string>(act, Android.Resource.Layout.SimpleListItem1, similar_Loc);
            }
            else if (searched_Loc.Count == 0)
            {
                arrayAdapter = new ArrayAdapter <string>(act, Android.Resource.Layout.SimpleListItem1, similar_Loc);
            }
            else
            {
                arrayAdapter = new ArrayAdapter <string>(act, Android.Resource.Layout.SimpleListItem1, searched_Loc);
            }

            mTv.Adapter = arrayAdapter;
            mTv.SetFooterDividersEnabled(true);
            mTv.SetHeaderDividersEnabled(true);
        }