VoiceSearch.java revision 81a0897ff9685f3313c58294bf7973700c468b2b
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 */
16package com.android.quicksearchbox;
17
18import android.app.SearchManager;
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.os.Bundle;
24import android.speech.RecognizerIntent;
25
26/**
27 * Voice Search integration.
28 */
29public class VoiceSearch {
30
31    private final Context mContext;
32
33    public VoiceSearch(Context context) {
34        mContext = context;
35    }
36
37    public boolean shouldShowVoiceSearch(Corpus corpus) {
38        if (corpus != null && !corpus.voiceSearchEnabled()) {
39            return false;
40        }
41        return isVoiceSearchAvailable();
42    }
43
44    private boolean isVoiceSearchAvailable() {
45        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
46        ResolveInfo ri = mContext.getPackageManager().
47                resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
48        return ri != null;
49    }
50
51    public Intent createVoiceWebSearchIntent(Bundle appData) {
52        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
53        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
54        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
55                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
56        if (appData != null) {
57            intent.putExtra(SearchManager.APP_DATA, appData);
58        }
59        return intent;
60    }
61
62}
63