|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import org.mozilla.gecko.db.BrowserDB; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.database.Cursor; |
|
12 import android.os.Bundle; |
|
13 import android.support.v4.app.LoaderManager; |
|
14 import android.support.v4.app.LoaderManager.LoaderCallbacks; |
|
15 import android.support.v4.content.Loader; |
|
16 |
|
17 /** |
|
18 * Encapsulates the implementation of the search cursor loader. |
|
19 */ |
|
20 class SearchLoader { |
|
21 // Key for search terms |
|
22 private static final String KEY_SEARCH_TERM = "search_term"; |
|
23 |
|
24 private SearchLoader() { |
|
25 } |
|
26 |
|
27 public static Loader<Cursor> createInstance(Context context, Bundle args) { |
|
28 if (args != null) { |
|
29 final String searchTerm = args.getString(KEY_SEARCH_TERM); |
|
30 return new SearchCursorLoader(context, searchTerm); |
|
31 } else { |
|
32 return new SearchCursorLoader(context, ""); |
|
33 } |
|
34 } |
|
35 |
|
36 private static Bundle createArgs(String searchTerm) { |
|
37 Bundle args = new Bundle(); |
|
38 args.putString(SearchLoader.KEY_SEARCH_TERM, searchTerm); |
|
39 |
|
40 return args; |
|
41 } |
|
42 |
|
43 public static void init(LoaderManager manager, int loaderId, |
|
44 LoaderCallbacks<Cursor> callbacks, String searchTerm) { |
|
45 final Bundle args = createArgs(searchTerm); |
|
46 manager.initLoader(loaderId, args, callbacks); |
|
47 } |
|
48 |
|
49 public static void restart(LoaderManager manager, int loaderId, |
|
50 LoaderCallbacks<Cursor> callbacks, String searchTerm) { |
|
51 final Bundle args = createArgs(searchTerm); |
|
52 manager.restartLoader(loaderId, args, callbacks); |
|
53 } |
|
54 |
|
55 public static class SearchCursorLoader extends SimpleCursorLoader { |
|
56 // Max number of search results |
|
57 private static final int SEARCH_LIMIT = 100; |
|
58 |
|
59 // The target search term associated with the loader |
|
60 private final String mSearchTerm; |
|
61 |
|
62 public SearchCursorLoader(Context context, String searchTerm) { |
|
63 super(context); |
|
64 mSearchTerm = searchTerm; |
|
65 } |
|
66 |
|
67 @Override |
|
68 public Cursor loadCursor() { |
|
69 return BrowserDB.filter(getContext().getContentResolver(), mSearchTerm, SEARCH_LIMIT); |
|
70 } |
|
71 |
|
72 public String getSearchTerm() { |
|
73 return mSearchTerm; |
|
74 } |
|
75 } |
|
76 |
|
77 } |