• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.intelligence.search.savedqueries;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import androidx.loader.app.LoaderManager;
25 import androidx.loader.content.Loader;
26 
27 import com.android.settings.intelligence.R;
28 import com.android.settings.intelligence.overlay.FeatureFactory;
29 import com.android.settings.intelligence.search.SearchCommon;
30 import com.android.settings.intelligence.search.SearchFeatureProvider;
31 import com.android.settings.intelligence.search.SearchResult;
32 import com.android.settings.intelligence.search.SearchResultsAdapter;
33 
34 import java.util.List;
35 
36 public class SavedQueryController implements LoaderManager.LoaderCallbacks,
37         MenuItem.OnMenuItemClickListener {
38 
39     // TODO: make a generic background task manager to handle one-off tasks like this one.
40     private static final String ARG_QUERY = "remove_query";
41     private static final String TAG = "SearchSavedQueryCtrl";
42 
43     private static final int MENU_SEARCH_HISTORY = 1000;
44 
45     private final Context mContext;
46     private final LoaderManager mLoaderManager;
47     private final SearchFeatureProvider mSearchFeatureProvider;
48     private final SearchResultsAdapter mResultAdapter;
49 
SavedQueryController(Context context, LoaderManager loaderManager, SearchResultsAdapter resultsAdapter)50     public SavedQueryController(Context context, LoaderManager loaderManager,
51             SearchResultsAdapter resultsAdapter) {
52         mContext = context;
53         mLoaderManager = loaderManager;
54         mResultAdapter = resultsAdapter;
55         mSearchFeatureProvider = FeatureFactory.get(context)
56                 .searchFeatureProvider();
57     }
58 
59     @Override
onCreateLoader(int id, Bundle args)60     public Loader onCreateLoader(int id, Bundle args) {
61         switch (id) {
62             case SearchCommon.SearchLoaderId.SAVE_QUERY_TASK:
63                 return new SavedQueryRecorder(mContext, args.getString(ARG_QUERY));
64             case SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK:
65                 return new SavedQueryRemover(mContext);
66             case SearchCommon.SearchLoaderId.SAVED_QUERIES:
67                 return mSearchFeatureProvider.getSavedQueryLoader(mContext);
68         }
69         return null;
70     }
71 
72     @Override
onLoadFinished(Loader loader, Object data)73     public void onLoadFinished(Loader loader, Object data) {
74         switch (loader.getId()) {
75             case SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK:
76                 mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVED_QUERIES,
77                         null /* args */, this /* callback */);
78                 break;
79             case SearchCommon.SearchLoaderId.SAVED_QUERIES:
80                 if (SearchFeatureProvider.DEBUG) {
81                     Log.d(TAG, "Saved queries loaded");
82                 }
83                 mResultAdapter.displaySavedQuery((List<SearchResult>) data);
84                 break;
85         }
86     }
87 
88     @Override
onLoaderReset(Loader loader)89     public void onLoaderReset(Loader loader) {
90     }
91 
92     @Override
onMenuItemClick(MenuItem item)93     public boolean onMenuItemClick(MenuItem item) {
94         if (item.getItemId() != MENU_SEARCH_HISTORY) {
95             return false;
96         }
97         removeQueries();
98         return true;
99     }
100 
buildMenuItem(Menu menu)101     public void buildMenuItem(Menu menu) {
102         final MenuItem item =
103                 menu.add(Menu.NONE, MENU_SEARCH_HISTORY, Menu.NONE, R.string.search_clear_history);
104         item.setOnMenuItemClickListener(this);
105     }
106 
saveQuery(String query)107     public void saveQuery(String query) {
108         final Bundle args = new Bundle();
109         args.putString(ARG_QUERY, query);
110         mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVE_QUERY_TASK, args,
111                 this /* callback */);
112     }
113 
114     /**
115      * Remove all saved queries from DB
116      */
removeQueries()117     public void removeQueries() {
118         final Bundle args = new Bundle();
119         mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK, args,
120                 this /* callback */);
121     }
122 
loadSavedQueries()123     public void loadSavedQueries() {
124         if (SearchFeatureProvider.DEBUG) {
125             Log.d(TAG, "loading saved queries");
126         }
127         mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVED_QUERIES, null /* args */,
128                 this /* callback */);
129     }
130 }
131