Home | History | Annotate | Download | only in quicksearchbox
      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 
     39     public VoiceSearch(Context context) {
     40         mContext = context;
     41     }
     42 
     43     protected Context getContext() {
     44         return mContext;
     45     }
     46 
     47     public boolean shouldShowVoiceSearch() {
     48         return isVoiceSearchAvailable();
     49     }
     50 
     51     protected Intent createVoiceSearchIntent() {
     52         return new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
     53     }
     54 
     55     private ResolveInfo getResolveInfo() {
     56         Intent intent = createVoiceSearchIntent();
     57         ResolveInfo ri = mContext.getPackageManager().
     58                 resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
     59         return ri;
     60     }
     61 
     62     public boolean isVoiceSearchAvailable() {
     63         return getResolveInfo() != null;
     64     }
     65 
     66     public Intent createVoiceWebSearchIntent(Bundle appData) {
     67         if (!isVoiceSearchAvailable()) return null;
     68         Intent intent = createVoiceSearchIntent();
     69         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     70         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
     71                 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
     72         if (appData != null) {
     73             intent.putExtra(SearchManager.APP_DATA, appData);
     74         }
     75         return intent;
     76     }
     77 
     78     /**
     79      * Create an intent to launch the voice search help screen, if any exists.
     80      * @return The intent, or null.
     81      */
     82     public Intent createVoiceSearchHelpIntent() {
     83         return null;
     84     }
     85 
     86     /**
     87      * Gets the {@code versionCode} of the currently installed voice search package.
     88      *
     89      * @return The {@code versionCode} of voiceSearch, or 0 if none is installed.
     90      */
     91     public int getVersion() {
     92         ResolveInfo ri = getResolveInfo();
     93         if (ri == null) return 0;
     94         ComponentInfo ci = ri.activityInfo != null ? ri.activityInfo : ri.serviceInfo;
     95         try {
     96             return getContext().getPackageManager().getPackageInfo(ci.packageName, 0).versionCode;
     97         } catch (NameNotFoundException e) {
     98             Log.e(TAG, "Cannot find voice search package " + ci.packageName, e);
     99             return 0;
    100         }
    101     }
    102 
    103     public ComponentName getComponent() {
    104         return createVoiceSearchIntent().resolveActivity(getContext().getPackageManager());
    105     }
    106 }
    107