• 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.query;
18 
19 import android.content.Context;
20 import androidx.annotation.NonNull;
21 
22 import com.android.settings.intelligence.overlay.FeatureFactory;
23 import com.android.settings.intelligence.search.SearchResult;
24 import com.android.settings.intelligence.search.sitemap.SiteMapManager;
25 
26 import java.util.List;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.FutureTask;
29 
30 public class SearchQueryTask extends FutureTask<List<? extends SearchResult>> {
31 
32     private final int mId;
33 
SearchQueryTask(@onNull QueryWorker queryWorker)34     public SearchQueryTask(@NonNull QueryWorker queryWorker) {
35         super(queryWorker);
36         mId = queryWorker.getQueryWorkerId();
37     }
38 
getTaskId()39     public int getTaskId() {
40         return mId;
41     }
42 
43     public static abstract class QueryWorker implements Callable<List<? extends SearchResult>> {
44 
45         protected final Context mContext;
46         protected final SiteMapManager mSiteMapManager;
47         protected final String mQuery;
48 
QueryWorker(Context context, SiteMapManager siteMapManager, String query)49         public QueryWorker(Context context, SiteMapManager siteMapManager, String query) {
50             mContext = context;
51             mSiteMapManager = siteMapManager;
52             mQuery = query;
53         }
54 
55         @Override
call()56         public List<? extends SearchResult> call() throws Exception {
57             final long startTime = System.currentTimeMillis();
58             try {
59                 return query();
60             } finally {
61                 final long endTime = System.currentTimeMillis();
62                 FeatureFactory.get(mContext).metricsFeatureProvider(mContext)
63                         .logEvent(getQueryWorkerId(), endTime - startTime);
64             }
65         }
66 
getQueryWorkerId()67         protected abstract int getQueryWorkerId();
68 
query()69         protected abstract List<? extends SearchResult> query();
70     }
71 }
72