Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.example.android.tvleanback.ui;
     16 
     17 import android.app.Activity;
     18 import android.content.Intent;
     19 import android.os.Bundle;
     20 import android.support.v17.leanback.widget.SpeechRecognitionCallback;
     21 import android.util.Log;
     22 
     23 import com.example.android.tvleanback.R;
     24 
     25 /*
     26  * SearchActivity for SearchFragment
     27  */
     28 public class SearchActivity extends Activity {
     29     /**
     30      * Called when the activity is first created.
     31      */
     32 
     33     private static final String TAG = "SearchActivity";
     34     private static boolean DEBUG = true;
     35     /**
     36      * SpeechRecognitionCallback is not required and if not provided recognition will be handled
     37      * using internal speech recognizer, in which case you must have RECORD_AUDIO permission
     38      */
     39     private static final int REQUEST_SPEECH = 1;
     40     private SearchFragment mFragment;
     41     private SpeechRecognitionCallback mSpeechRecognitionCallback;
     42 
     43     @Override
     44     public void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         setContentView(R.layout.search);
     47 
     48         mFragment = (SearchFragment) getFragmentManager().findFragmentById(R.id.search_fragment);
     49 
     50         mSpeechRecognitionCallback = new SpeechRecognitionCallback() {
     51             @Override
     52             public void recognizeSpeech() {
     53                 if (DEBUG) Log.v(TAG, "recognizeSpeech");
     54                 startActivityForResult(mFragment.getRecognizerIntent(), REQUEST_SPEECH);
     55             }
     56         };
     57         mFragment.setSpeechRecognitionCallback(mSpeechRecognitionCallback);
     58     }
     59 
     60     @Override
     61     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     62         if (DEBUG) Log.v(TAG, "onActivityResult requestCode=" + requestCode +
     63                 " resultCode=" + resultCode +
     64                 " data=" + data);
     65         if (requestCode == REQUEST_SPEECH && resultCode == RESULT_OK) {
     66             mFragment.setSearchQuery(data, true);
     67         }
     68     }
     69 
     70     @Override
     71     public boolean onSearchRequested() {
     72         startActivity(new Intent(this, SearchActivity.class));
     73         return true;
     74     }
     75 }
     76