private void UseSelected()
 {
     if (Intent != null && _selectedEngine != null && _selectedVoice > -1)
     {
         String eng = _selectedEngine.Name();
         String voi = _selectedEngine.Voices[_selectedVoice];
         Intent.PutExtra(SELECTED_ENGINE, eng);
         Intent.PutExtra(SELECTED_VOICE, voi);
         LangSupport.SetSelectedTtsEng(eng);
         LangSupport.SetPrefferedVoice(_selectedEngine.Iso3ln[_selectedVoice],
                                       voi + "|" + eng);
         SetResult(Result.Ok, Intent);
     }
     Finish();
 }
        private async Task TestVoice(String pckName, String langVoice)
        {
            if (_testVoiceStarted)
            {
                return;
            }
            _testVoiceStarted = true;
            Log.Debug(TAG, "TestVoice: " + pckName + ", " + langVoice);
            if (_tts != null)
            {
                try {
                    _tts.Shutdown();
                } catch { /* don't care */ }
            }

            _tts = await CreateTtsAsync(this, pckName);

            if (_tts != null)
            {
                Locale loc = LangSupport.LocaleFromString(langVoice);
                _tts.SetLanguage(loc);
                String[] samples = Resources.GetStringArray(Resource.Array.tts_samples);
                String   sample  = "[" + loc.ISO3Language.ToLower() + "]";
                foreach (String s in samples)
                {
                    if (s.Contains(sample))
                    {
                        sample = s.Substring(s.LastIndexOf(']') + 1);
                        break;
                    }
                }
                if (sample.StartsWith("["))
                {
                    var intent = new Intent("android.speech.tts.engine.GET_SAMPLE_TEXT");
                    intent = intent.SetPackage(pckName);
                    intent.PutExtra("language", loc.Language);
                    intent.PutExtra("country", loc.Country);
                    intent.PutExtra("variant", loc.Variant);
                    try
                    {
                        _ttsStillNeeded = true;
                        StartActivityForResult(intent, SAMP_REQUEST); // goes to onActivityResult()
                    }
                    catch
                    {
                        _ttsStillNeeded = false;
                    } // ActivityNotFoundException, possibly others
                }
                else
                {
                    try
                    {
                        Log.Debug(TAG, "Speak: " + sample);
                        _tts.Speak(sample, QueueMode.Flush, _paramMap);
                    }
                    catch (Exception e)
                    {
                        Log.Debug(TAG, "Exception in Speak: " + e.ToString());
                    }
                }
            }
            _testVoiceStarted = false;
        }
        private void langSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            // fill voices
            String lang   = _langCodes[e.Position].Code;
            var    voices = new List <String>();

            _eli = new List <EngInt>();
            String initVoice    = Intent.GetStringExtra(INIT_LANG);
            String initLangIso3 = null;

            if (initVoice != null)
            {
                initVoice    = initVoice.ToLower();
                initLangIso3 = new Locale(initVoice).ISO3Language.ToLower();
            }
            String initEngine = Intent.GetStringExtra(INIT_ENGINE);
            int    selPos = -1, selQual = -1, maxQual = -100, n = 0;

            foreach (EngLang el in _allEngines)
            {
                String ttsEngName = el.Label().Replace(" TTS", "");
                bool   useThis    = initVoice != null && initEngine != null && initEngine == el.Name();
                for (int i = 0; i < el.Voices.Count; i++)
                {
                    if (lang == el.Iso3ln[i])
                    {
                        Locale loc    = LangSupport.LocaleFromString(el.Voices[i]);
                        String locStr = loc.ToString().ToLower().Replace('_', '-');
                        if (useThis && locStr == initVoice)
                        {
                            selPos    = n;
                            useThis   = false;
                            initVoice = null; // to stop looking for more
                        }
                        if (maxQual < el.Quality)
                        {
                            maxQual = el.Quality;
                            selQual = n;
                        }
                        String country = loc.GetDisplayCountry(loc);
                        String variant = loc.Variant;
                        String s       = ttsEngName;
                        if ("" != country)
                        {
                            s += ", " + country;
                        }
                        if ("" != variant)
                        {
                            s += ", " + variant;
                        }
                        voices.Add(s);
                        _eli.Add(new EngInt(el, i));
                        n++;
                    }
                }
            }

            var adapter = new ArrayAdapter <String>(this, Android.Resource.Layout.SimpleSpinnerItem, voices);

            adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            _voiceSpinner.Adapter = adapter;
            if (selPos < 0)
            {
                selPos = selQual;
            }
            if (selPos > -1)
            {
                _selectedEngine = _eli[selPos].El;
                _selectedVoice  = _eli[selPos].Pos;
                bool autoSel = (Intent != null && Intent.GetBooleanExtra(INIT_AUTOSEL, false)) ? true : false;
                if (autoSel && _selectedEngine != null && _selectedVoice > -1)
                {
                    if (initLangIso3 == _selectedEngine.Iso3ln[_selectedVoice])
                    {
                        UseSelected();
                        return;
                    }
                    else
                    {
                        Intent.PutExtra(SELECTED_VOICE, initLangIso3 + "_n/a");
                        SetResult(Result.Ok, Intent);
                        Finish();
                        return;
                    }
                }
                _voiceSpinner.SetSelection(selPos); // fix this, use previous selection or best quality...
            }
        }