diff options
| author | Mike LeBeau <mlebeau@android.com> | 2010-02-11 22:58:29 -0800 |
|---|---|---|
| committer | Mike LeBeau <mlebeau@android.com> | 2010-02-11 22:58:29 -0800 |
| commit | 6774fcffa45e06d92993fa4381d90939495da431 (patch) | |
| tree | b10a51b967b137713a8442dd2b6392677c53883b /samples/VoiceRecognitionService | |
| parent | 8789080d4f31cef83c170969ece8ed2df9b70950 (diff) | |
| download | android_development-6774fcffa45e06d92993fa4381d90939495da431.tar.gz android_development-6774fcffa45e06d92993fa4381d90939495da431.tar.bz2 android_development-6774fcffa45e06d92993fa4381d90939495da431.zip | |
First stab at a sample voice recognition service to show the skeleton for how
to set up such a thing and expose its settings activity, etc.
Diffstat (limited to 'samples/VoiceRecognitionService')
8 files changed, 318 insertions, 0 deletions
diff --git a/samples/VoiceRecognitionService/Android.mk b/samples/VoiceRecognitionService/Android.mk new file mode 100755 index 000000000..6155a0f49 --- /dev/null +++ b/samples/VoiceRecognitionService/Android.mk @@ -0,0 +1,12 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := samples + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_SDK_VERSION := current + +LOCAL_PACKAGE_NAME := VoiceRecognitionService + +include $(BUILD_PACKAGE) diff --git a/samples/VoiceRecognitionService/AndroidManifest.xml b/samples/VoiceRecognitionService/AndroidManifest.xml new file mode 100644 index 000000000..f6128fad1 --- /dev/null +++ b/samples/VoiceRecognitionService/AndroidManifest.xml @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** Copyright 2010, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.example.android.voicerecognitionservice"> + + <uses-sdk android:minSdkVersion="Froyo" /> + + <application> + + <!-- The service that implements voice recognition. Note that a label + for the service is important to provide, as this is what will show + to users if they have to choose between multiple recognizers in + system settings. --> + <service android:name="VoiceRecognitionService" + android:label="@string/service_name"> + + <intent-filter> + <!-- Here we identify that we are a RecognitionService by specifying that + we satisfy RecognitionService's interface intent. + + The constant value is defined at RecognitionService.SERVICE_INTERFACE. --> + <action android:name="android.speech.RecognitionService" /> + <category android:name="android.intent.category.DEFAULT" /> + </intent-filter> + + <!-- This points to a metadata xml file that contains information about this + RecognitionService - specifically, the name of the settings activity to + expose in system settings. + + The constant value is defined at RecognitionService.SERVICE_META_DATA. --> + <meta-data android:name="android.speech" android:resource="@xml/recognizer" /> + + </service> + + <!-- The settings activity for this sample voice recognizer. --> + <activity android:name=".VoiceRecognitionSettings" + android:label="@string/settings_name" + android:exported="true" /> + + </application> +</manifest> diff --git a/samples/VoiceRecognitionService/_index.html b/samples/VoiceRecognitionService/_index.html new file mode 100644 index 000000000..377b10c20 --- /dev/null +++ b/samples/VoiceRecognitionService/_index.html @@ -0,0 +1,14 @@ +<p>A sample application that demonstrates Android's pluggable voice recognition framework.</p> + +<p>This application includes a sample voice recognition service, and a settings activity +for that service. It shows the basic skeleton for setting up a recognition service and +exposing its settings activity to settings.</p> + +<p>The behavior is extremely simple - it does no real voice recognition, and just returns a +fixed set of results immediately. The results can be either a set of letters or numbers, as +chosen by the user in the simplistic settings activity.</p> + +<p>See also:</p> +<ul> + <li><a href="../../../reference/android/speech/RecognitionService.html">RecognitionService</a></li> +</ul>
\ No newline at end of file diff --git a/samples/VoiceRecognitionService/res/values/strings.xml b/samples/VoiceRecognitionService/res/values/strings.xml new file mode 100644 index 000000000..d8720190f --- /dev/null +++ b/samples/VoiceRecognitionService/res/values/strings.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** Copyright 2010, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ +--> +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + + <!-- The name for the recognizer - to be shown in system settings. --> + <string name="service_name">Sample Recognizer</string> + + <!-- The name of the settings activity. --> + <string name="settings_name">Voice Recognition Settings</string> + + <!-- The title of the preference for the type of results to return (letters or numbers). --> + <string name="results_type_title">Results type</string> + + <!-- The entry names of the preference for the type of results to return + (letters or numbers). --> + <string-array name="results_type_entries"> + <item>Letters</item> + <item>Numbers</item> + </string-array> + + <!-- The entry values of the preference for the type of results to return + (letters or numbers). --> + <string-array name="results_type_values"> + <item>0</item> + <item>1</item> + </string-array> + + <!-- The default value of the preference for the type of results to return + (letters or numbers). --> + <string name="results_type_default_value">0</string> + +</resources> diff --git a/samples/VoiceRecognitionService/res/xml/preferences.xml b/samples/VoiceRecognitionService/res/xml/preferences.xml new file mode 100644 index 000000000..2d9e08ce3 --- /dev/null +++ b/samples/VoiceRecognitionService/res/xml/preferences.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2010, The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" + android:title="@string/settings_name"> + + <!-- This setting lets you choose what kind of fake results you get from + this sample recognizer. If you choose "letters", you'll get letters + like "a", "b", "c" in the list of results. If you choose "numbers", + you'll get numbers like "1", "2", "3" in the list of results. --> + <ListPreference + android:key="results_type" + android:title="@string/results_type_title" + android:entries="@array/results_type_entries" + android:entryValues="@array/results_type_values" + android:dialogTitle="@string/results_type_title" + android:defaultValue="@string/results_type_default_value" + /> + +</PreferenceScreen> diff --git a/samples/VoiceRecognitionService/res/xml/recognizer.xml b/samples/VoiceRecognitionService/res/xml/recognizer.xml new file mode 100644 index 000000000..37b5c8ab8 --- /dev/null +++ b/samples/VoiceRecognitionService/res/xml/recognizer.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/** + * Copyright (c) 2010, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +--> + +<!-- The attributes in this XML file provide configuration information + for the sample voice recognizer. --> + +<recognition-service xmlns:android="http://schemas.android.com/apk/res/android" + android:settingsActivity="com.example.android.voicerecognitionservice.VoiceRecognitionSettings" +/> diff --git a/samples/VoiceRecognitionService/src/com/example/android/voicerecognitionservice/VoiceRecognitionService.java b/samples/VoiceRecognitionService/src/com/example/android/voicerecognitionservice/VoiceRecognitionService.java new file mode 100644 index 000000000..f0d167640 --- /dev/null +++ b/samples/VoiceRecognitionService/src/com/example/android/voicerecognitionservice/VoiceRecognitionService.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.voicerecognitionservice; + +import java.util.ArrayList; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.os.RemoteException; +import android.speech.RecognitionManager; +import android.speech.RecognitionService; + +/** + * A sample implementation of a {@link RecognitionService}. This very simple implementation does + * no actual voice recognition. It just immediately returns fake recognition results. + * Depending on the setting chosen in {@link VoiceRecognitionSettings}, it either returns a + * list of letters ("a", "b", "c"), or a list of numbers ("1", "2", "3"). + */ +public class VoiceRecognitionService extends RecognitionService { + + @Override + protected void onCancel(Callback listener) { + // A real recognizer would do something to shut down recognition here. + } + + @Override + protected void onStartListening(Intent recognizerIntent, Callback listener) { + // A real recognizer would probably utilize a lot of the other listener callback + // methods. But we'll just skip all that and pretend we've got a result. + ArrayList<String> results = new ArrayList<String>(); + + SharedPreferences prefs = getSharedPreferences( + VoiceRecognitionSettings.SHARED_PREFERENCES_NAME, + Context.MODE_PRIVATE); + + String resultType = prefs.getString( + VoiceRecognitionSettings.PREF_KEY_RESULTS_TYPE, + String.valueOf(VoiceRecognitionSettings.RESULT_TYPE_LETTERS)); + int resultTypeInt = Integer.parseInt(resultType); + + if (resultTypeInt == VoiceRecognitionSettings.RESULT_TYPE_LETTERS) { + results.add("a"); + results.add("b"); + results.add("c"); + } else if (resultTypeInt == VoiceRecognitionSettings.RESULT_TYPE_NUMBERS) { + results.add("1"); + results.add("2"); + results.add("3"); + } + + Bundle bundle = new Bundle(); + bundle.putStringArrayList(RecognitionManager.RESULTS_RECOGNITION, results); + + try { + listener.results(bundle); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void onStopListening(Callback listener) { + // Not implemented - in this sample we assume recognition would be endpointed + // automatically, though certain applications may wish to expose an affordance + // for stopping recording manually. + } + +} diff --git a/samples/VoiceRecognitionService/src/com/example/android/voicerecognitionservice/VoiceRecognitionSettings.java b/samples/VoiceRecognitionService/src/com/example/android/voicerecognitionservice/VoiceRecognitionSettings.java new file mode 100644 index 000000000..1360ce392 --- /dev/null +++ b/samples/VoiceRecognitionService/src/com/example/android/voicerecognitionservice/VoiceRecognitionSettings.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.example.android.voicerecognitionservice; + +import android.os.Bundle; +import android.preference.PreferenceActivity; + +/** + * A settings activity for the sample voice recognizer. + */ +public class VoiceRecognitionSettings extends PreferenceActivity { + + // The name of the SharedPreferences file we'll store preferences in. + public static final String SHARED_PREFERENCES_NAME = "VoiceRecognitionService"; + + // The key to the preference for the type of results to show (letters or numbers). + // Identical to the value specified in res/values/strings.xml. + public static final String PREF_KEY_RESULTS_TYPE = "results_type"; + + // The values of the preferences for the type of results to show (letters or numbers). + // Identical to the values specified in res/values/strings.xml. + public static final int RESULT_TYPE_LETTERS = 0; + public static final int RESULT_TYPE_NUMBERS = 1; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getPreferenceManager().setSharedPreferencesName(SHARED_PREFERENCES_NAME); + addPreferencesFromResource(R.xml.preferences); + } +} |
