2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.search2;
19 import android.app.LoaderManager;
20 import android.content.Context;
21 import android.content.Loader;
22 import android.os.Bundle;
24 import com.android.settings.overlay.FeatureFactory;
26 import java.util.List;
28 public class SavedQueryController implements LoaderManager.LoaderCallbacks {
30 // TODO: make a generic background task manager to handle one-off tasks like this one.
32 private static final int LOADER_ID_SAVE_QUERY_TASK = 0;
33 private static final int LOADER_ID_REMOVE_QUERY_TASK = 1;
34 private static final int LOADER_ID_SAVED_QUERIES = 2;
35 private static final String ARG_QUERY = "remove_query";
37 private final Context mContext;
38 private final LoaderManager mLoaderManager;
39 private final SearchFeatureProvider mSearchFeatureProvider;
40 private final SearchResultsAdapter mResultAdapter;
42 public SavedQueryController(Context context, LoaderManager loaderManager,
43 SearchResultsAdapter resultsAdapter) {
45 mLoaderManager = loaderManager;
46 mResultAdapter = resultsAdapter;
47 mSearchFeatureProvider = FeatureFactory.getFactory(context)
48 .getSearchFeatureProvider();
52 public Loader onCreateLoader(int id, Bundle args) {
54 case LOADER_ID_SAVE_QUERY_TASK:
55 return new SavedQueryRecorder(mContext, args.getString(ARG_QUERY));
56 case LOADER_ID_REMOVE_QUERY_TASK:
57 return new SavedQueryRemover(mContext, args.getString(ARG_QUERY));
58 case LOADER_ID_SAVED_QUERIES:
59 return mSearchFeatureProvider.getSavedQueryLoader(mContext);
65 public void onLoadFinished(Loader loader, Object data) {
66 switch (loader.getId()) {
67 case LOADER_ID_REMOVE_QUERY_TASK:
68 mLoaderManager.restartLoader(LOADER_ID_SAVED_QUERIES, null, this);
70 case LOADER_ID_SAVED_QUERIES:
71 mResultAdapter.displaySavedQuery((List<SearchResult>) data);
77 public void onLoaderReset(Loader loader) {
81 public void saveQuery(String query) {
82 final Bundle args = new Bundle();
83 args.putString(ARG_QUERY, query);
84 mLoaderManager.restartLoader(LOADER_ID_SAVE_QUERY_TASK, args, this);
87 public void removeQuery(String query) {
88 final Bundle args = new Bundle();
89 args.putString(ARG_QUERY, query);
90 mLoaderManager.restartLoader(LOADER_ID_REMOVE_QUERY_TASK, args, this);
93 public void loadSavedQueries() {
94 mLoaderManager.restartLoader(LOADER_ID_SAVED_QUERIES, null, this);