diff options
3 files changed, 74 insertions, 3 deletions
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 076903b3c9db..ee7193b4b83e 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -22,6 +22,7 @@ import org.apache.commons.codec.binary.Base64; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; +import android.content.ComponentName; import android.content.ContentQueryMap; import android.content.ContentResolver; import android.content.ContentValues; @@ -2955,6 +2956,14 @@ public final class Settings { * @hide */ public static final String ANR_SHOW_BACKGROUND = "anr_show_background"; + + /** + * The {@link ComponentName} string of the service to be used as the voice recognition + * service. + * + * @hide + */ + public static final String VOICE_RECOGNITION_SERVICE = "voice_recognition_service"; /** * @hide diff --git a/core/java/android/speech/RecognitionManager.java b/core/java/android/speech/RecognitionManager.java index 7915208ef2c1..edb7067d984e 100644 --- a/core/java/android/speech/RecognitionManager.java +++ b/core/java/android/speech/RecognitionManager.java @@ -27,6 +27,8 @@ import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.RemoteException; +import android.provider.Settings; +import android.text.TextUtils; import android.util.Log; import java.util.LinkedList; @@ -218,8 +220,20 @@ public class RecognitionManager { checkIsCalledFromMainThread(); if (mConnection == null) { // first time connection mConnection = new Connection(); - if (!mContext.bindService(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), - mConnection, Context.BIND_AUTO_CREATE)) { + + Intent serviceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + String serviceComponent = Settings.Secure.getString(mContext.getContentResolver(), + Settings.Secure.VOICE_RECOGNITION_SERVICE); + + if (TextUtils.isEmpty(serviceComponent)) { + Log.e(TAG, "no selected voice recognition service"); + mListener.onError(ERROR_CLIENT); + return; + } + + serviceIntent.setComponent(ComponentName.unflattenFromString(serviceComponent)); + + if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { Log.e(TAG, "bind to recognition service failed"); mConnection = null; mService = null; diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java index 8b75b616a04f..4e1e0a2afc3d 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java @@ -23,6 +23,8 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.pm.ServiceInfo; import android.content.res.Resources; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; @@ -36,6 +38,7 @@ import android.net.ConnectivityManager; import android.os.Environment; import android.os.SystemProperties; import android.provider.Settings; +import android.speech.RecognizerIntent; import android.text.TextUtils; import android.util.Config; import android.util.Log; @@ -72,7 +75,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion' // is properly propagated through your change. Not doing so will result in a loss of user // settings. - private static final int DATABASE_VERSION = 48; + private static final int DATABASE_VERSION = 49; private Context mContext; @@ -611,6 +614,23 @@ public class DatabaseHelper extends SQLiteOpenHelper { } upgradeVersion = 48; } + + if (upgradeVersion == 48) { + /* + * Adding a new setting for which voice recognition service to use. + */ + db.beginTransaction(); + try { + SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)" + + " VALUES(?,?);"); + loadVoiceRecognitionServiceSetting(stmt); + stmt.close(); + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + upgradeVersion = 49; + } if (upgradeVersion != currentVersion) { Log.w(TAG, "Got stuck trying to upgrade from version " + upgradeVersion @@ -955,6 +975,8 @@ public class DatabaseHelper extends SQLiteOpenHelper { loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED, R.bool.def_mount_ums_notify_enabled); + + loadVoiceRecognitionServiceSetting(stmt); stmt.close(); } @@ -966,6 +988,32 @@ public class DatabaseHelper extends SQLiteOpenHelper { loadStringSetting(stmt, Settings.Secure.BACKUP_TRANSPORT, R.string.def_backup_transport); } + + /** + * Introduced in database version 49. + */ + private void loadVoiceRecognitionServiceSetting(SQLiteStatement stmt) { + String selectedService = null; + List<ResolveInfo> availableRecognitionServices = + mContext.getPackageManager().queryIntentServices( + new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); + int numAvailable = availableRecognitionServices.size(); + + if (numAvailable == 0) { + Log.w(TAG, "no available voice recognition services found"); + } else { + if (numAvailable > 1) { + Log.w(TAG, "more than one voice recognition service found, picking first"); + } + + ServiceInfo serviceInfo = availableRecognitionServices.get(0).serviceInfo; + selectedService = + new ComponentName(serviceInfo.packageName, serviceInfo.name).flattenToString(); + } + + loadSetting(stmt, Settings.Secure.VOICE_RECOGNITION_SERVICE, + selectedService == null ? "" : selectedService); + } private void loadSetting(SQLiteStatement stmt, String key, Object value) { stmt.bindString(1, key); |