1 /* 2 * Copyright (C) 2007 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 android.server.search; 18 19 import com.android.internal.content.PackageMonitor; 20 21 import android.app.ISearchManager; 22 import android.app.SearchManager; 23 import android.app.SearchableInfo; 24 import android.content.BroadcastReceiver; 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.os.Process; 30 import android.util.Log; 31 32 import java.util.List; 33 34 /** 35 * The search manager service handles the search UI, and maintains a registry of searchable 36 * activities. 37 */ 38 public class SearchManagerService extends ISearchManager.Stub { 39 40 // general debugging support 41 private static final String TAG = "SearchManagerService"; 42 43 // Context that the service is running in. 44 private final Context mContext; 45 46 // This field is initialized lazily in getSearchables(), and then never modified. 47 private Searchables mSearchables; 48 49 /** 50 * Initializes the Search Manager service in the provided system context. 51 * Only one instance of this object should be created! 52 * 53 * @param context to use for accessing DB, window manager, etc. 54 */ 55 public SearchManagerService(Context context) { 56 mContext = context; 57 mContext.registerReceiver(new BootCompletedReceiver(), 58 new IntentFilter(Intent.ACTION_BOOT_COMPLETED)); 59 } 60 61 private synchronized Searchables getSearchables() { 62 if (mSearchables == null) { 63 Log.i(TAG, "Building list of searchable activities"); 64 new MyPackageMonitor().register(mContext, true); 65 mSearchables = new Searchables(mContext); 66 mSearchables.buildSearchableList(); 67 } 68 return mSearchables; 69 } 70 71 /** 72 * Creates the initial searchables list after boot. 73 */ 74 private final class BootCompletedReceiver extends BroadcastReceiver { 75 @Override 76 public void onReceive(Context context, Intent intent) { 77 new Thread() { 78 @Override 79 public void run() { 80 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 81 mContext.unregisterReceiver(BootCompletedReceiver.this); 82 getSearchables(); 83 } 84 }.start(); 85 } 86 } 87 88 /** 89 * Refreshes the "searchables" list when packages are added/removed. 90 */ 91 class MyPackageMonitor extends PackageMonitor { 92 @Override 93 public void onSomePackagesChanged() { 94 // Update list of searchable activities 95 getSearchables().buildSearchableList(); 96 // Inform all listeners that the list of searchables has been updated. 97 Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); 98 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 99 mContext.sendBroadcast(intent); 100 } 101 } 102 103 // 104 // Searchable activities API 105 // 106 107 /** 108 * Returns the SearchableInfo for a given activity. 109 * 110 * @param launchActivity The activity from which we're launching this search. 111 * @return Returns a SearchableInfo record describing the parameters of the search, 112 * or null if no searchable metadata was available. 113 */ 114 public SearchableInfo getSearchableInfo(final ComponentName launchActivity) { 115 if (launchActivity == null) { 116 Log.e(TAG, "getSearchableInfo(), activity == null"); 117 return null; 118 } 119 return getSearchables().getSearchableInfo(launchActivity); 120 } 121 122 /** 123 * Returns a list of the searchable activities that can be included in global search. 124 */ 125 public List<SearchableInfo> getSearchablesInGlobalSearch() { 126 return getSearchables().getSearchablesInGlobalSearchList(); 127 } 128 129 /** 130 * Gets the name of the global search activity. 131 */ 132 public ComponentName getGlobalSearchActivity() { 133 return getSearchables().getGlobalSearchActivity(); 134 } 135 136 /** 137 * Gets the name of the web search activity. 138 */ 139 public ComponentName getWebSearchActivity() { 140 return getSearchables().getWebSearchActivity(); 141 } 142 143 } 144