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