Merge "A few TTS bug fixes"
diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java
index 990d493..6823b73 100755
--- a/core/java/android/speech/tts/TextToSpeech.java
+++ b/core/java/android/speech/tts/TextToSpeech.java
@@ -505,12 +505,14 @@
 
         // Try requested engine
         if (connectToEngine(engine)) {
+            mCurrentEngine = engine;
             return SUCCESS;
         }
 
         // Fall back to user's default engine if different from the already tested one
         if (!engine.equals(defaultEngine)) {
             if (connectToEngine(defaultEngine)) {
+                mCurrentEngine = engine;
                 return SUCCESS;
             }
         }
@@ -520,10 +522,12 @@
         if (!defaultEngine.equals(highestRanked)
                 && !engine.equals(highestRanked)) {
             if (connectToEngine(highestRanked)) {
+                mCurrentEngine = engine;
                 return SUCCESS;
             }
         }
 
+        dispatchOnInit(ERROR);
         return ERROR;
     }
 
@@ -534,10 +538,9 @@
         boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
         if (!bound) {
             Log.e(TAG, "Failed to bind to " + engine);
-            dispatchOnInit(ERROR);
             return false;
         } else {
-            mCurrentEngine = engine;
+            Log.i(TAG, "Sucessfully bound to " + engine);
             return true;
         }
     }
diff --git a/core/java/android/speech/tts/TtsEngines.java b/core/java/android/speech/tts/TtsEngines.java
index e8d74eb..715894f 100644
--- a/core/java/android/speech/tts/TtsEngines.java
+++ b/core/java/android/speech/tts/TtsEngines.java
@@ -48,14 +48,14 @@
     }
 
     /**
-     * @return the default TTS engine. If the user has set a default, that
-     *         value is returned else the highest ranked engine is returned,
-     *         as per {@link EngineInfoComparator}.
+     * @return the default TTS engine. If the user has set a default, and the engine
+     *         is available on the device, the default is returned. Otherwise,
+     *         the highest ranked engine is returned as per {@link EngineInfoComparator}.
      */
     public String getDefaultEngine() {
         String engine = Settings.Secure.getString(mContext.getContentResolver(),
                 Settings.Secure.TTS_DEFAULT_SYNTH);
-        return engine != null ? engine : getHighestRankedEngineName();
+        return isEngineInstalled(engine) ? engine : getHighestRankedEngineName();
     }
 
     /**
@@ -124,7 +124,14 @@
     public boolean isEngineEnabled(String engine) {
         // System engines are enabled by default always.
         EngineInfo info = getEngineInfo(engine);
-        if (info != null && info.system) {
+        if (info == null) {
+            // The engine is not installed, and therefore cannot
+            // be enabled.
+            return false;
+        }
+
+        if (info.system) {
+            // All system engines are enabled by default.
             return true;
         }
 
@@ -141,6 +148,25 @@
         return appInfo != null && (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
     }
 
+    /**
+     * @return true if a given engine is installed on the system. Useful to deal
+     *         with cases where an engine has been uninstalled by the user or removed
+     *         for any other reason.
+     */
+    private boolean isEngineInstalled(String engine) {
+        if (engine == null) {
+            return false;
+        }
+
+        for (EngineInfo info : getEngines()) {
+            if (engine.equals(info.name)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     private EngineInfo getEngineInfo(ResolveInfo resolve, PackageManager pm) {
         ServiceInfo service = resolve.serviceInfo;
         if (service != null) {