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 17 package com.android.quicksearchbox; 18 19 20 import com.android.quicksearchbox.util.Util; 21 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.ActivityInfo; 26 import android.graphics.drawable.Drawable; 27 import android.net.Uri; 28 import android.os.Bundle; 29 import android.text.TextUtils; 30 import android.util.Log; 31 32 /** 33 * The apps search source. 34 */ 35 public class AppsCorpus extends SingleSourceCorpus { 36 37 private static final String TAG = "QSB.AppsCorpus"; 38 39 private static final String APPS_CORPUS_NAME = "apps"; 40 41 public AppsCorpus(Context context, Config config, Source appsSource) { 42 super(context, config, appsSource); 43 } 44 45 @Override 46 public CharSequence getLabel() { 47 return getContext().getText(R.string.corpus_label_apps); 48 } 49 50 @Override 51 public CharSequence getHint() { 52 return getContext().getText(R.string.corpus_hint_apps); 53 } 54 55 @Override 56 public Drawable getCorpusIcon() { 57 return getContext().getResources().getDrawable(R.drawable.corpus_icon_apps); 58 } 59 60 @Override 61 public Uri getCorpusIconUri() { 62 return Util.getResourceUri(getContext(), R.drawable.corpus_icon_apps); 63 } 64 65 @Override 66 public String getName() { 67 return APPS_CORPUS_NAME; 68 } 69 70 @Override 71 public CharSequence getSettingsDescription() { 72 return getContext().getText(R.string.corpus_description_apps); 73 } 74 75 @Override 76 public Intent createSearchIntent(String query, Bundle appData) { 77 Intent appSearchIntent = createAppSearchIntent(query, appData); 78 if (appSearchIntent != null) { 79 return appSearchIntent; 80 } else { 81 // Fall back to sending the intent to ApplicationsProvider 82 return super.createSearchIntent(query, appData); 83 } 84 } 85 86 /** 87 * Creates an intent that starts the search activity specified in 88 * R.string.apps_search_activity. 89 * 90 * @return An intent, or {@code null} if the search activity is not set or can't be found. 91 */ 92 private Intent createAppSearchIntent(String query, Bundle appData) { 93 ComponentName name = getComponentName(getContext(), R.string.apps_search_activity); 94 if (name == null) return null; 95 Intent intent = AbstractSource.createSourceSearchIntent(name, query, appData); 96 if (intent == null) return null; 97 ActivityInfo ai = intent.resolveActivityInfo(getContext().getPackageManager(), 0); 98 if (ai != null) { 99 return intent; 100 } else { 101 Log.w(TAG, "Can't find app search activity " + name); 102 return null; 103 } 104 } 105 106 private static ComponentName getComponentName(Context context, int res) { 107 String nameStr = context.getString(res); 108 if (TextUtils.isEmpty(nameStr)) { 109 return null; 110 } else { 111 return ComponentName.unflattenFromString(nameStr); 112 } 113 } 114 } 115