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