SearchableSource.java revision 0484fb4d652bfa9d5c7fb238a7cec1a6f2244e44
1/* 2 * Copyright (C) 2009 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 17package com.android.quicksearchbox; 18 19import android.app.SearchManager; 20import android.app.SearchableInfo; 21import android.content.ComponentName; 22import android.content.ContentResolver; 23import android.content.Context; 24import android.content.pm.ActivityInfo; 25import android.content.pm.PackageManager; 26import android.content.pm.PackageManager.NameNotFoundException; 27import android.database.Cursor; 28import android.graphics.drawable.Drawable; 29import android.net.Uri; 30import android.util.Log; 31 32import java.util.Arrays; 33 34/** 35 * Represents a single suggestion source, e.g. Contacts. 36 * 37 */ 38public class SearchableSource implements Source { 39 40 private static final boolean DBG = true; 41 private static final String TAG = "QSB.SearchableSource"; 42 43 private final Context mContext; 44 45 private final SearchableInfo mSearchable; 46 47 private final ActivityInfo mActivityInfo; 48 49 // Cached label for the activity 50 private CharSequence mLabel = null; 51 52 // Cached icon for the activity 53 private Drawable.ConstantState mSourceIcon = null; 54 55 private final boolean mIsWebSuggestionSource; 56 57 private final IconLoader mIconLoader; 58 59 public SearchableSource(Context context, SearchableInfo searchable) 60 throws NameNotFoundException { 61 this(context, searchable, false); 62 } 63 64 public SearchableSource(Context context, SearchableInfo searchable, 65 boolean isWebSuggestionSource) throws NameNotFoundException { 66 ComponentName componentName = searchable.getSearchActivity(); 67 mContext = context; 68 mSearchable = searchable; 69 mActivityInfo = context.getPackageManager().getActivityInfo(componentName, 0); 70 mIsWebSuggestionSource = isWebSuggestionSource; 71 72 Context activityContext = searchable.getActivityContext(context); 73 Context providerContext = searchable.getProviderContext(context, activityContext); 74 mIconLoader = new CachingIconLoader(new PackageIconLoader(providerContext)); 75 } 76 77 public ComponentName getComponentName() { 78 return mSearchable.getSearchActivity(); 79 } 80 81 public Drawable getIcon(String drawableId) { 82 return mIconLoader.getIcon(drawableId); 83 } 84 85 public Uri getIconUri(String drawableId) { 86 return mIconLoader.getIconUri(drawableId); 87 } 88 89 public CharSequence getLabel() { 90 if (mLabel == null) { 91 // Load label lazily 92 mLabel = mActivityInfo.loadLabel(mContext.getPackageManager()); 93 } 94 return mLabel; 95 } 96 97 public int getQueryThreshold() { 98 return mSearchable.getSuggestThreshold(); 99 } 100 101 public String getSettingsDescription() { 102 return mSearchable.getSettingsDescription(); 103 } 104 105 public Drawable getSourceIcon() { 106 if (mSourceIcon == null) { 107 // Load icon lazily 108 int iconRes = getSourceIconResource(); 109 PackageManager pm = mContext.getPackageManager(); 110 Drawable icon = pm.getDrawable(mActivityInfo.packageName, iconRes, 111 mActivityInfo.applicationInfo); 112 // Can't share Drawable instances, save constant state instead. 113 mSourceIcon = (icon != null) ? icon.getConstantState() : null; 114 // Optimization, return the Drawable the first time 115 return icon; 116 } 117 return (mSourceIcon != null) ? mSourceIcon.newDrawable() : null; 118 } 119 120 public Uri getSourceIconUri() { 121 return mIconLoader.getIconUri(String.valueOf(getSourceIconResource())); 122 } 123 124 private int getSourceIconResource() { 125 int icon = mActivityInfo.getIconResource(); 126 return (icon != 0) ? icon : android.R.drawable.sym_def_app_icon; 127 } 128 129 public SuggestionCursor getSuggestions(String query, int queryLimit) { 130 try { 131 Cursor cursor = getSuggestions(mContext, mSearchable, query, queryLimit); 132 if (DBG) Log.d(TAG, toString() + "[" + query + "] returned."); 133 return new SourceResult(this, query, cursor); 134 } catch (RuntimeException ex) { 135 Log.e(TAG, toString() + "[" + query + "] failed", ex); 136 return new SourceResult(this, query); 137 } 138 } 139 140 /** 141 * This is a copy of {@link SearchManager#getSuggestions(SearchableInfo, String)}. 142 */ 143 private static Cursor getSuggestions(Context context, SearchableInfo searchable, String query, 144 int queryLimit) { 145 if (searchable == null) { 146 return null; 147 } 148 149 String authority = searchable.getSuggestAuthority(); 150 if (authority == null) { 151 return null; 152 } 153 154 Uri.Builder uriBuilder = new Uri.Builder() 155 .scheme(ContentResolver.SCHEME_CONTENT) 156 .authority(authority); 157 158 // if content path provided, insert it now 159 final String contentPath = searchable.getSuggestPath(); 160 if (contentPath != null) { 161 uriBuilder.appendEncodedPath(contentPath); 162 } 163 164 // append standard suggestion query path 165 uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY); 166 167 // get the query selection, may be null 168 String selection = searchable.getSuggestSelection(); 169 // inject query, either as selection args or inline 170 String[] selArgs = null; 171 if (selection != null) { // use selection if provided 172 selArgs = new String[] { query }; 173 } else { // no selection, use REST pattern 174 uriBuilder.appendPath(query); 175 } 176 177 uriBuilder.appendQueryParameter("limit", String.valueOf(queryLimit)); 178 179 Uri uri = uriBuilder.build(); 180 181 // finally, make the query 182 if (DBG) { 183 Log.d(TAG, "query(" + uri + ",null," + selection + "," 184 + Arrays.toString(selArgs) + ",null)"); 185 } 186 return context.getContentResolver().query(uri, null, selection, selArgs, null); 187 } 188 189 public boolean isWebSuggestionSource() { 190 return mIsWebSuggestionSource; 191 } 192 193 public boolean queryAfterZeroResults() { 194 return mSearchable.queryAfterZeroResults(); 195 } 196 197 public boolean shouldRewriteQueryFromData() { 198 return mSearchable.shouldRewriteQueryFromData(); 199 } 200 201 public boolean shouldRewriteQueryFromText() { 202 return mSearchable.shouldRewriteQueryFromText(); 203 } 204 205 @Override 206 public String toString() { 207 return "SearchableSource{component=" + getComponentName().flattenToShortString() + "}"; 208 } 209 210 public String getDefaultIntentAction() { 211 return mSearchable.getSuggestIntentAction(); 212 } 213 214 public String getDefaultIntentData() { 215 return mSearchable.getSuggestIntentData(); 216 } 217 218 public ComponentName getSearchActivity() { 219 return mSearchable.getSearchActivity(); 220 } 221 222 public String getSuggestActionMsg(int keyCode) { 223 SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); 224 if (actionKey == null) return null; 225 return actionKey.getSuggestActionMsg(); 226 } 227 228 public String getSuggestActionMsgColumn(int keyCode) { 229 SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); 230 if (actionKey == null) return null; 231 return actionKey.getSuggestActionMsgColumn(); 232 } 233 234} 235