Merge change 4797 into donut

* changes:
  Use new location of USE_LOCATION_FOR_SERVICES setting (Secure).
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2fedceb..9e28761 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1737,6 +1737,14 @@
     <string name="tts_default_lang_title">Language</string>
     <!-- On main TTS Settings screen, summary for default language for synthesized voice -->
     <string name="tts_default_lang_summary">Sets the language-specific voice for the spoken text</string>
+    <!-- On main TTS Settings screen, triggers an example of speech synthesis -->
+    <string name="tts_play_example_title">Listen to an example</string>
+    <!-- On main TTS Settings screen, summary for triggering an example of speech synthesis -->
+    <string name="tts_play_example_summary">Play a short demonstration of speech synthesis</string>
+    <!-- Text spoken by the TTS engine for demonstration purposes -->
+    <string name="tts_demo">This is an example of speech synthesis.</string>
+    <!-- Text spoken by the TTS engine when TTS settings (other than language) have been changed -->
+    <string name="tts_settings_changed_demo">Your settings have changed. This is an example of how they sound.</string>
 
     <!-- Power Control Widget -->
     <string name="gadget_title">Power Control</string>
diff --git a/res/xml/tts_settings.xml b/res/xml/tts_settings.xml
index 991c1f1..5a7aaa2 100644
--- a/res/xml/tts_settings.xml
+++ b/res/xml/tts_settings.xml
@@ -17,6 +17,12 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
         android:title="@string/tts_settings_title">
 
+        <Preference
+                android:key="tts_play_example"
+                android:persistent="false"
+                android:title="@string/tts_play_example_title" 
+                android:summary="@string/tts_play_example_summary" />
+
         <CheckBoxPreference
             android:key="toggle_use_default_tts_settings"
             android:title="@string/use_default_tts_settings_title"
diff --git a/src/com/android/settings/TextToSpeechSettings.java b/src/com/android/settings/TextToSpeechSettings.java
index 79a841b..ca58afd 100644
--- a/src/com/android/settings/TextToSpeechSettings.java
+++ b/src/com/android/settings/TextToSpeechSettings.java
@@ -22,44 +22,89 @@
 import static android.provider.Settings.Secure.TTS_DEFAULT_LANG;
 
 import android.content.ContentResolver;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.os.Bundle;
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.CheckBoxPreference;
 import android.provider.Settings;
+import android.speech.tts.TextToSpeech;
 import android.util.Log;
 
+import java.util.List;
+
 public class TextToSpeechSettings extends PreferenceActivity implements
-        Preference.OnPreferenceChangeListener {
+        Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
+        TextToSpeech.OnInitListener {
 
     private static final String TAG = "TextToSpeechSettings";
-    
-    /** If there is no setting in the provider, use this. */
-    private static final int FALLBACK_TTS_DEFAULT_RATE = 100; // 1x
-    private static final int FALLBACK_TTS_DEFAULT_PITCH = 100;// 1x
-    private static final int FALLBACK_TTS_USE_DEFAULTS = 1;
-    private static final String FALLBACK_TTS_DEFAULT_LANG = "en-rUS";
 
-    private static final String KEY_TTS_USE_DEFAULT =
-            "toggle_use_default_tts_settings";
+    private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example";
+    private static final String KEY_TTS_USE_DEFAULT = "toggle_use_default_tts_settings";
     private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate";
     private static final String KEY_TTS_DEFAULT_PITCH = "tts_default_pitch";
     private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang";
-    
+
+    // TODO move this to android.speech.tts.TextToSpeech.Engine
+    private static final String FALLBACK_TTS_DEFAULT_SYNTH = "com.svox.pico";
+
+    private Preference         mPlayExample = null;
     private CheckBoxPreference mUseDefaultPref = null;
     private ListPreference     mDefaultRatePref = null;
     private ListPreference     mDefaultPitchPref = null;
     private ListPreference     mDefaultLangPref = null;
-    
-    
+    private String             mDefaultEng = "";
+
+    private boolean mEnableDemo = false;
+
+    private TextToSpeech mTts = null;
+
+    /**
+     * Request code (arbitrary value) for voice data check through
+     * startActivityForResult.
+     */
+    private static final int VOICE_DATA_INTEGRITY_CHECK = 1977;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         addPreferencesFromResource(R.xml.tts_settings);
-        
+
+        initDemo();
         initDefaultSettings();
+
+        checkVoiceData();
+    }
+    
+    
+    @Override
+    protected void onResume() {
+        super.onResume();
+        // whenever we return to this screen, we don't know the state of the
+        // system, so we have to recheck that we can play the demo, or it must be disabled.
+        mEnableDemo = false;
+        initDemo();
+    }
+
+
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        if (mTts != null) {
+            mTts.shutdown();
+        }
+    }
+
+
+    private void initDemo() {
+        mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
+        mPlayExample.setEnabled(mEnableDemo);
+        mPlayExample.setOnPreferenceClickListener(this);
     }
 
 
@@ -71,30 +116,33 @@
             (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
         mUseDefaultPref.setChecked(Settings.Secure.getInt(resolver,
                 TTS_USE_DEFAULTS,
-                FALLBACK_TTS_USE_DEFAULTS) == 1 ? true : false);
+                TextToSpeech.Engine.FALLBACK_TTS_USE_DEFAULTS) == 1 ? true : false);
         mUseDefaultPref.setOnPreferenceChangeListener(this);
 
+        // Default engine
+        mDefaultEng = FALLBACK_TTS_DEFAULT_SYNTH;
+
         // Default rate
         mDefaultRatePref =
             (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
         mDefaultRatePref.setValue(String.valueOf(Settings.Secure.getInt(
-                resolver, TTS_DEFAULT_RATE, FALLBACK_TTS_DEFAULT_RATE)));
+                resolver, TTS_DEFAULT_RATE, TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_RATE)));
         mDefaultRatePref.setOnPreferenceChangeListener(this);
 
         // Default pitch
         mDefaultPitchPref =
             (ListPreference) findPreference(KEY_TTS_DEFAULT_PITCH);
         mDefaultPitchPref.setValue(String.valueOf(Settings.Secure.getInt(
-                resolver, TTS_DEFAULT_PITCH, FALLBACK_TTS_DEFAULT_PITCH)));
+                resolver, TTS_DEFAULT_PITCH, TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_PITCH)));
         mDefaultPitchPref.setOnPreferenceChangeListener(this);
-        
+
         // Default language
         mDefaultLangPref =
                 (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
         String defaultLang = String.valueOf(Settings.Secure.getString(resolver, 
                 TTS_DEFAULT_LANG));
         if (defaultLang.compareTo("null") == 0) {
-            mDefaultLangPref.setValue(FALLBACK_TTS_DEFAULT_LANG);
+            mDefaultLangPref.setValue(TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_LANG);
             Log.i(TAG, "TTS initDefaultSettings() default lang null ");
         } else {
             mDefaultLangPref.setValue(defaultLang);
@@ -104,6 +152,55 @@
     }
 
 
+    private void checkVoiceData() {
+        PackageManager pm = getPackageManager();
+        Intent intent = new Intent();
+        intent.setAction("android.intent.action.CHECK_TTS_DATA");
+        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
+        // query only the package that matches that of the default engine
+        for (int i = 0; i < resolveInfos.size(); i++) {
+            ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
+            if (mDefaultEng.equals(currentActivityInfo.packageName)) {
+                intent.setClassName(mDefaultEng, currentActivityInfo.name);
+                this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
+            }
+        }
+    }
+
+
+    /**
+     * Called when the TTS engine is initialized.
+     */
+    public void onInit(int status) {
+        if (status == TextToSpeech.TTS_SUCCESS) {
+            Log.v(TAG, "TTS engine for settings screen initialized.");
+            mEnableDemo = true;
+        } else {
+            Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
+            mEnableDemo = false;
+        }
+        mPlayExample.setEnabled(mEnableDemo);
+    }
+
+
+    /**
+     * Called when voice data integrity check returns
+     */
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
+            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
+                Log.v(TAG, "Voice data check passed");
+                if (mTts == null) {
+                    mTts = new TextToSpeech(this, this);
+                }
+            } else {
+                Log.v(TAG, "Voice data check failed");
+
+            }
+        }
+    }
+
+
     public boolean onPreferenceChange(Preference preference, Object objValue) {
         if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
             // "Use Defaults"
@@ -131,7 +228,7 @@
             } catch (NumberFormatException e) {
                 Log.e(TAG, "could not persist default TTS pitch setting", e);
             }
-        }else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
+        } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
             // Default language
             String value = (String) objValue;
             Settings.Secure.putString(getContentResolver(),
@@ -142,4 +239,16 @@
         return true;
     }
     
+
+    public boolean onPreferenceClick(Preference preference) {
+        if (preference == mPlayExample) {
+            if (mTts != null) {
+                mTts.speak(getResources().getString(R.string.tts_demo),
+                        TextToSpeech.TTS_QUEUE_FLUSH, null);
+            }
+            return true;
+        }
+        return false;
+    }
+
 }