Home | History | Annotate | Download | only in voicerecognitionservice
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.voicerecognitionservice;
     18 
     19 import java.util.ArrayList;
     20 
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.os.Bundle;
     25 import android.os.RemoteException;
     26 import android.speech.SpeechRecognizer;
     27 import android.speech.RecognitionService;
     28 
     29 /**
     30  * A sample implementation of a {@link RecognitionService}. This very simple implementation does
     31  * no actual voice recognition. It just immediately returns fake recognition results.
     32  * Depending on the setting chosen in {@link VoiceRecognitionSettings}, it either returns a
     33  * list of letters ("a", "b", "c"), or a list of numbers ("1", "2", "3").
     34  */
     35 public class VoiceRecognitionService extends RecognitionService {
     36 
     37     @Override
     38     protected void onCancel(Callback listener) {
     39         // A real recognizer would do something to shut down recognition here.
     40     }
     41 
     42     @Override
     43     protected void onStartListening(Intent recognizerIntent, Callback listener) {
     44         // A real recognizer would probably utilize a lot of the other listener callback
     45         // methods. But we'll just skip all that and pretend we've got a result.
     46         ArrayList<String> results = new ArrayList<String>();
     47 
     48         SharedPreferences prefs = getSharedPreferences(
     49                 VoiceRecognitionSettings.SHARED_PREFERENCES_NAME,
     50                 Context.MODE_PRIVATE);
     51 
     52         String resultType = prefs.getString(
     53                 VoiceRecognitionSettings.PREF_KEY_RESULTS_TYPE,
     54                 String.valueOf(VoiceRecognitionSettings.RESULT_TYPE_LETTERS));
     55         int resultTypeInt = Integer.parseInt(resultType);
     56 
     57         if (resultTypeInt == VoiceRecognitionSettings.RESULT_TYPE_LETTERS) {
     58             results.add("a");
     59             results.add("b");
     60             results.add("c");
     61         } else if (resultTypeInt == VoiceRecognitionSettings.RESULT_TYPE_NUMBERS) {
     62             results.add("1");
     63             results.add("2");
     64             results.add("3");
     65         }
     66 
     67         Bundle bundle = new Bundle();
     68         bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, results);
     69 
     70         try {
     71             listener.results(bundle);
     72         } catch (RemoteException e) {
     73             throw new RuntimeException(e);
     74         }
     75     }
     76 
     77     @Override
     78     protected void onStopListening(Callback listener) {
     79         // Not implemented - in this sample we assume recognition would be endpointed
     80         // automatically, though certain applications may wish to expose an affordance
     81         // for stopping recording manually.
     82     }
     83 
     84 }
     85