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 package com.example.android.leanback;
15 
16 import android.app.Activity;
17 import android.content.Intent;
18 import android.os.Bundle;
19 import android.util.Log;
20 
21 import androidx.leanback.app.SearchFragment;
22 import androidx.leanback.widget.SpeechRecognitionCallback;
23 
24 public class SearchActivity extends Activity
25 {
26     private static final String TAG = "SearchActivity";
27     private static boolean DEBUG = true;
28 
29     /** If using internal speech recognizer, you must have RECORD_AUDIO permission */
30     private static final boolean USE_INTERNAL_SPEECH_RECOGNIZER = true;
31     private static final int REQUEST_SPEECH = 1;
32 
33     private SearchFragment mFragment;
34     private SpeechRecognitionCallback mSpeechRecognitionCallback;
35 
36     /** Called when the activity is first created. */
37     @Override
onCreate(Bundle savedInstanceState)38     public void onCreate(Bundle savedInstanceState)
39     {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.search);
42 
43         mFragment = (SearchFragment) getFragmentManager().findFragmentById(R.id.search_fragment);
44 
45         if (!USE_INTERNAL_SPEECH_RECOGNIZER) {
46             mSpeechRecognitionCallback = new SpeechRecognitionCallback() {
47                 @Override
48                 public void recognizeSpeech() {
49                     if (DEBUG) Log.v(TAG, "recognizeSpeech");
50                     startActivityForResult(mFragment.getRecognizerIntent(), REQUEST_SPEECH);
51                 }
52             };
53             mFragment.setSpeechRecognitionCallback(mSpeechRecognitionCallback);
54         }
55     }
56 
57     @Override
onActivityResult(int requestCode, int resultCode, Intent data)58     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
59         if (DEBUG) Log.v(TAG, "onActivityResult requestCode=" + requestCode +
60                 " resultCode=" + resultCode +
61                 " data=" + data);
62         super.onActivityResult(requestCode, resultCode, data);
63         if (requestCode == REQUEST_SPEECH && resultCode == RESULT_OK) {
64             mFragment.setSearchQuery(data, true);
65         }
66     }
67 
68 }
69