VoiceSearch.java revision 5d8047bba2e05cc0cc1ca6684a53f26656a60584
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.ComponentInfo;
22import android.content.pm.PackageManager;
23import android.content.pm.PackageManager.NameNotFoundException;
24import android.content.pm.ResolveInfo;
25import android.os.Bundle;
26import android.speech.RecognizerIntent;
27import android.util.Log;
28
29/**
30 * Voice Search integration.
31 */
32public class VoiceSearch {
33
34    private static final String TAG = "QSB.VoiceSearch";
35
36    private final Context mContext;
37
38    public VoiceSearch(Context context) {
39        mContext = context;
40    }
41
42    protected Context getContext() {
43        return mContext;
44    }
45
46    public boolean shouldShowVoiceSearch(Corpus corpus) {
47        return corpusSupportsVoiceSearch(corpus) && isVoiceSearchAvailable();
48    }
49
50    protected boolean corpusSupportsVoiceSearch(Corpus corpus) {
51        return (corpus == null || corpus.voiceSearchEnabled());
52    }
53
54    protected Intent createVoiceSearchIntent() {
55        return new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
56    }
57
58    private ResolveInfo getResolveInfo() {
59        Intent intent = createVoiceSearchIntent();
60        ResolveInfo ri = mContext.getPackageManager().
61                resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
62        return ri;
63    }
64
65    public boolean isVoiceSearchAvailable() {
66        return getResolveInfo() != null;
67    }
68
69    public Intent createVoiceWebSearchIntent(Bundle appData) {
70        if (!isVoiceSearchAvailable()) return null;
71        Intent intent = createVoiceSearchIntent();
72        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
73        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
74                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
75        if (appData != null) {
76            intent.putExtra(SearchManager.APP_DATA, appData);
77        }
78        return intent;
79    }
80
81    /**
82     * Create an intent to launch the voice search help screen, if any exists.
83     * @return The intent, or null.
84     */
85    public Intent createVoiceSearchHelpIntent() {
86        return null;
87    }
88
89    /**
90     * Gets the {@code versionCode} of the currently installed voice search package.
91     *
92     * @return The {@code versionCode} of voiceSearch, or 0 if none is installed.
93     */
94    public int getVersion() {
95        ResolveInfo ri = getResolveInfo();
96        if (ri == null) return 0;
97        ComponentInfo ci = ri.activityInfo != null ? ri.activityInfo : ri.serviceInfo;
98        try {
99            return getContext().getPackageManager().getPackageInfo(ci.packageName, 0).versionCode;
100        } catch (NameNotFoundException e) {
101            Log.e(TAG, "Cannot find voice search package " + ci.packageName, e);
102            return 0;
103        }
104    }
105
106}
107