1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/SearchLoader.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.mozilla.gecko.db.BrowserDB; 1.12 + 1.13 +import android.content.Context; 1.14 +import android.database.Cursor; 1.15 +import android.os.Bundle; 1.16 +import android.support.v4.app.LoaderManager; 1.17 +import android.support.v4.app.LoaderManager.LoaderCallbacks; 1.18 +import android.support.v4.content.Loader; 1.19 + 1.20 +/** 1.21 + * Encapsulates the implementation of the search cursor loader. 1.22 + */ 1.23 +class SearchLoader { 1.24 + // Key for search terms 1.25 + private static final String KEY_SEARCH_TERM = "search_term"; 1.26 + 1.27 + private SearchLoader() { 1.28 + } 1.29 + 1.30 + public static Loader<Cursor> createInstance(Context context, Bundle args) { 1.31 + if (args != null) { 1.32 + final String searchTerm = args.getString(KEY_SEARCH_TERM); 1.33 + return new SearchCursorLoader(context, searchTerm); 1.34 + } else { 1.35 + return new SearchCursorLoader(context, ""); 1.36 + } 1.37 + } 1.38 + 1.39 + private static Bundle createArgs(String searchTerm) { 1.40 + Bundle args = new Bundle(); 1.41 + args.putString(SearchLoader.KEY_SEARCH_TERM, searchTerm); 1.42 + 1.43 + return args; 1.44 + } 1.45 + 1.46 + public static void init(LoaderManager manager, int loaderId, 1.47 + LoaderCallbacks<Cursor> callbacks, String searchTerm) { 1.48 + final Bundle args = createArgs(searchTerm); 1.49 + manager.initLoader(loaderId, args, callbacks); 1.50 + } 1.51 + 1.52 + public static void restart(LoaderManager manager, int loaderId, 1.53 + LoaderCallbacks<Cursor> callbacks, String searchTerm) { 1.54 + final Bundle args = createArgs(searchTerm); 1.55 + manager.restartLoader(loaderId, args, callbacks); 1.56 + } 1.57 + 1.58 + public static class SearchCursorLoader extends SimpleCursorLoader { 1.59 + // Max number of search results 1.60 + private static final int SEARCH_LIMIT = 100; 1.61 + 1.62 + // The target search term associated with the loader 1.63 + private final String mSearchTerm; 1.64 + 1.65 + public SearchCursorLoader(Context context, String searchTerm) { 1.66 + super(context); 1.67 + mSearchTerm = searchTerm; 1.68 + } 1.69 + 1.70 + @Override 1.71 + public Cursor loadCursor() { 1.72 + return BrowserDB.filter(getContext().getContentResolver(), mSearchTerm, SEARCH_LIMIT); 1.73 + } 1.74 + 1.75 + public String getSearchTerm() { 1.76 + return mSearchTerm; 1.77 + } 1.78 + } 1.79 + 1.80 +}