1 /*
2  * Copyright (C) 2017 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 com.android.settings.search2;
18 
19 import android.app.LoaderManager;
20 import android.content.Context;
21 import android.content.Loader;
22 import android.os.Bundle;
23 
24 import com.android.settings.overlay.FeatureFactory;
25 
26 import java.util.List;
27 
28 public class SavedQueryController implements LoaderManager.LoaderCallbacks {
29 
30     // TODO: make a generic background task manager to handle one-off tasks like this one.
31 
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";
36 
37     private final Context mContext;
38     private final LoaderManager mLoaderManager;
39     private final SearchFeatureProvider mSearchFeatureProvider;
40     private final SearchResultsAdapter mResultAdapter;
41 
SavedQueryController(Context context, LoaderManager loaderManager, SearchResultsAdapter resultsAdapter)42     public SavedQueryController(Context context, LoaderManager loaderManager,
43             SearchResultsAdapter resultsAdapter) {
44         mContext = context;
45         mLoaderManager = loaderManager;
46         mResultAdapter = resultsAdapter;
47         mSearchFeatureProvider = FeatureFactory.getFactory(context)
48                 .getSearchFeatureProvider();
49     }
50 
51     @Override
onCreateLoader(int id, Bundle args)52     public Loader onCreateLoader(int id, Bundle args) {
53         switch (id) {
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);
60         }
61         return null;
62     }
63 
64     @Override
onLoadFinished(Loader loader, Object data)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);
69                 break;
70             case LOADER_ID_SAVED_QUERIES:
71                 mResultAdapter.displaySavedQuery((List<SearchResult>) data);
72                 break;
73         }
74     }
75 
76     @Override
onLoaderReset(Loader loader)77     public void onLoaderReset(Loader loader) {
78 
79     }
80 
saveQuery(String query)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);
85     }
86 
removeQuery(String query)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);
91     }
92 
loadSavedQueries()93     public void loadSavedQueries() {
94         mLoaderManager.restartLoader(LOADER_ID_SAVED_QUERIES, null, this);
95     }
96 }
97