From 8d1538237847baf381787b881141f8c0478bef5b Mon Sep 17 00:00:00 2001 From: Bjorn Bringert Date: Mon, 22 Jun 2009 10:31:44 +0100 Subject: [PATCH] Make all static methods in SearchManager non-static. They were only static because of a now removed restriction that only activity contexts could instantiate SearchManager. This only changes hidden APIs, but all users of the changed methods must be updated to use them non-statically before this is submitted. --- core/java/android/app/SearchDialog.java | 8 ++- core/java/android/app/SearchManager.java | 61 +++++++------------ core/java/android/app/SuggestionsAdapter.java | 4 +- .../android/unit_tests/SearchManagerTest.java | 14 +++-- 4 files changed, 38 insertions(+), 49 deletions(-) diff --git a/core/java/android/app/SearchDialog.java b/core/java/android/app/SearchDialog.java index 3a3a983cb4ba5..5d1af52152fed 100644 --- a/core/java/android/app/SearchDialog.java +++ b/core/java/android/app/SearchDialog.java @@ -310,15 +310,17 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS + appSearchData + ", " + globalSearch + ")"); } + SearchManager searchManager = (SearchManager) + mContext.getSystemService(Context.SEARCH_SERVICE); // Try to get the searchable info for the provided component (or for global search, // if globalSearch == true). - mSearchable = SearchManager.getSearchableInfo(componentName, globalSearch); + mSearchable = searchManager.getSearchableInfo(componentName, globalSearch); // If we got back nothing, and it wasn't a request for global search, then try again // for global search, as we'll try to launch that in lieu of any component-specific search. if (!globalSearch && mSearchable == null) { globalSearch = true; - mSearchable = SearchManager.getSearchableInfo(componentName, globalSearch); + mSearchable = searchManager.getSearchableInfo(componentName, globalSearch); // If we still get back null (i.e., there's not even a searchable info available // for global search), then really give up. @@ -333,7 +335,7 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS mAppSearchData = appSearchData; // Using globalSearch here is just an optimization, just calling // isDefaultSearchable() should always give the same result. - mGlobalSearchMode = globalSearch || SearchManager.isDefaultSearchable(mSearchable); + mGlobalSearchMode = globalSearch || searchManager.isDefaultSearchable(mSearchable); mActivityContext = mSearchable.getActivityContext(getContext()); // show the dialog. this will call onStart(). diff --git a/core/java/android/app/SearchManager.java b/core/java/android/app/SearchManager.java index 4c868d44739d5..fd69ba461a04a 100644 --- a/core/java/android/app/SearchManager.java +++ b/core/java/android/app/SearchManager.java @@ -1514,7 +1514,7 @@ public class SearchManager /** * Reference to the shared system search service. */ - private static ISearchManager sService = getSearchManagerService(); + private static ISearchManager mService; private final Context mContext; @@ -1529,6 +1529,8 @@ public class SearchManager /*package*/ SearchManager(Context context, Handler handler) { mContext = context; mHandler = handler; + mService = ISearchManager.Stub.asInterface( + ServiceManager.getService(Context.SEARCH_SERVICE)); } /** @@ -1581,7 +1583,7 @@ public class SearchManager try { mIsShowing = true; // activate the search manager and start it up! - sService.startSearch(initialQuery, selectInitialQuery, launchActivity, appSearchData, + mService.startSearch(initialQuery, selectInitialQuery, launchActivity, appSearchData, globalSearch, mSearchManagerCallback); } catch (RemoteException ex) { Log.e(TAG, "startSearch() failed: " + ex); @@ -1603,7 +1605,7 @@ public class SearchManager if (DBG) debug("stopSearch(), mIsShowing=" + mIsShowing); if (!mIsShowing) return; try { - sService.stopSearch(); + mService.stopSearch(); // onDismiss will also clear this, but we do it here too since onDismiss() is // called asynchronously. mIsShowing = false; @@ -1725,7 +1727,7 @@ public class SearchManager if (DBG) debug("saveSearchDialog(), mIsShowing=" + mIsShowing); if (!mIsShowing) return null; try { - return sService.onSaveInstanceState(); + return mService.onSaveInstanceState(); } catch (RemoteException ex) { Log.e(TAG, "onSaveInstanceState() failed: " + ex); return null; @@ -1743,7 +1745,7 @@ public class SearchManager if (DBG) debug("restoreSearchDialog(" + searchDialogState + ")"); if (searchDialogState == null) return; try { - sService.onRestoreInstanceState(searchDialogState); + mService.onRestoreInstanceState(searchDialogState); } catch (RemoteException ex) { Log.e(TAG, "onRestoreInstanceState() failed: " + ex); } @@ -1760,17 +1762,12 @@ public class SearchManager if (DBG) debug("onConfigurationChanged(" + newConfig + "), mIsShowing=" + mIsShowing); if (!mIsShowing) return; try { - sService.onConfigurationChanged(newConfig); + mService.onConfigurationChanged(newConfig); } catch (RemoteException ex) { Log.e(TAG, "onConfigurationChanged() failed:" + ex); } } - private static ISearchManager getSearchManagerService() { - return ISearchManager.Stub.asInterface( - ServiceManager.getService(Context.SEARCH_SERVICE)); - } - /** * Gets information about a searchable activity. This method is static so that it can * be used from non-Activity contexts. @@ -1782,10 +1779,10 @@ public class SearchManager * * @hide because SearchableInfo is not part of the API. */ - public static SearchableInfo getSearchableInfo(ComponentName componentName, + public SearchableInfo getSearchableInfo(ComponentName componentName, boolean globalSearch) { try { - return sService.getSearchableInfo(componentName, globalSearch); + return mService.getSearchableInfo(componentName, globalSearch); } catch (RemoteException ex) { Log.e(TAG, "getSearchableInfo() failed: " + ex); return null; @@ -1797,12 +1794,12 @@ public class SearchManager * * @hide because SearchableInfo is not part of the API. */ - public static boolean isDefaultSearchable(SearchableInfo searchable) { - SearchableInfo defaultSearchable = SearchManager.getSearchableInfo(null, true); + public boolean isDefaultSearchable(SearchableInfo searchable) { + SearchableInfo defaultSearchable = getSearchableInfo(null, true); return defaultSearchable != null && defaultSearchable.getSearchActivity().equals(searchable.getSearchActivity()); } - + /** * Gets a cursor with search suggestions. * @@ -1813,20 +1810,6 @@ public class SearchManager * @hide because SearchableInfo is not part of the API. */ public Cursor getSuggestions(SearchableInfo searchable, String query) { - return getSuggestions(mContext, searchable, query); - } - - /** - * Gets a cursor with search suggestions. This method is static so that it can - * be used from non-Activity context. - * - * @param searchable Information about how to get the suggestions. - * @param query The search text entered (so far). - * @return a cursor with suggestions, or null the suggestion query failed. - * - * @hide because SearchableInfo is not part of the API. - */ - public static Cursor getSuggestions(Context context, SearchableInfo searchable, String query) { if (searchable == null) { return null; } @@ -1865,7 +1848,7 @@ public class SearchManager .build(); // finally, make the query - return context.getContentResolver().query(uri, null, selection, selArgs, null); + return mContext.getContentResolver().query(uri, null, selection, selArgs, null); } /** @@ -1877,9 +1860,9 @@ public class SearchManager * * @hide because SearchableInfo is not part of the API. */ - public static List getSearchablesInGlobalSearch() { + public List getSearchablesInGlobalSearch() { try { - return sService.getSearchablesInGlobalSearch(); + return mService.getSearchablesInGlobalSearch(); } catch (RemoteException e) { Log.e(TAG, "getSearchablesInGlobalSearch() failed: " + e); return null; @@ -1894,9 +1877,9 @@ public class SearchManager * * @hide because SearchableInfo is not part of the API. */ - public static List getSearchablesForWebSearch() { + public List getSearchablesForWebSearch() { try { - return sService.getSearchablesForWebSearch(); + return mService.getSearchablesForWebSearch(); } catch (RemoteException e) { Log.e(TAG, "getSearchablesForWebSearch() failed: " + e); return null; @@ -1910,9 +1893,9 @@ public class SearchManager * * @hide because SearchableInfo is not part of the API. */ - public static SearchableInfo getDefaultSearchableForWebSearch() { + public SearchableInfo getDefaultSearchableForWebSearch() { try { - return sService.getDefaultSearchableForWebSearch(); + return mService.getDefaultSearchableForWebSearch(); } catch (RemoteException e) { Log.e(TAG, "getDefaultSearchableForWebSearch() failed: " + e); return null; @@ -1926,9 +1909,9 @@ public class SearchManager * * @hide */ - public static void setDefaultWebSearch(ComponentName component) { + public void setDefaultWebSearch(ComponentName component) { try { - sService.setDefaultWebSearch(component); + mService.setDefaultWebSearch(component); } catch (RemoteException e) { Log.e(TAG, "setDefaultWebSearch() failed: " + e); } diff --git a/core/java/android/app/SuggestionsAdapter.java b/core/java/android/app/SuggestionsAdapter.java index ed76f4e5c693b..0bdb10b505635 100644 --- a/core/java/android/app/SuggestionsAdapter.java +++ b/core/java/android/app/SuggestionsAdapter.java @@ -54,6 +54,7 @@ class SuggestionsAdapter extends ResourceCursorAdapter { private static final boolean DBG = false; private static final String LOG_TAG = "SuggestionsAdapter"; + private SearchManager mSearchManager; private SearchDialog mSearchDialog; private SearchableInfo mSearchable; private Context mProviderContext; @@ -92,6 +93,7 @@ class SuggestionsAdapter extends ResourceCursorAdapter { com.android.internal.R.layout.search_dropdown_item_icons_2line, null, // no initial cursor true); // auto-requery + mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); mSearchDialog = searchDialog; mSearchable = searchable; @@ -142,7 +144,7 @@ class SuggestionsAdapter extends ResourceCursorAdapter { mSearchDialog.getWindow().getDecorView().post(mStartSpinnerRunnable); } try { - final Cursor cursor = SearchManager.getSuggestions(mContext, mSearchable, query); + final Cursor cursor = mSearchManager.getSuggestions(mSearchable, query); // trigger fill window so the spinner stays up until the results are copied over and // closer to being ready if (!mGlobalSearchMode) cursor.getCount(); diff --git a/tests/AndroidTests/src/com/android/unit_tests/SearchManagerTest.java b/tests/AndroidTests/src/com/android/unit_tests/SearchManagerTest.java index f03a779b2d014..4c6da767500ee 100644 --- a/tests/AndroidTests/src/com/android/unit_tests/SearchManagerTest.java +++ b/tests/AndroidTests/src/com/android/unit_tests/SearchManagerTest.java @@ -163,17 +163,19 @@ public class SearchManagerTest extends ActivityInstrumentationTestCase2