blob: 073b3d0f3ae5b5cef8fb0ee2213e4bd65dd166b1 [file] [log] [blame]
Ivan Chiang282baa42018-08-08 18:54:14 +08001/*
2 * Copyright (C) 2018 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
17package com.android.documentsui;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.net.Uri;
22import android.os.Bundle;
23import android.provider.DocumentsContract;
24
25import androidx.annotation.NonNull;
26
27import com.android.documentsui.base.Lookup;
28import com.android.documentsui.base.RootInfo;
29import com.android.documentsui.base.State;
30import com.android.documentsui.roots.ProvidersAccess;
31import com.android.documentsui.roots.RootCursorWrapper;
32
33import java.util.List;
34import java.util.concurrent.Executor;
35
36/*
37 * The class to query multiple roots support {@link DocumentsContract.Root#FLAG_LOCAL_ONLY}
38 * and {@link DocumentsContract.Root#FLAG_SUPPORTS_SEARCH} from
39 * {@link android.provider.DocumentsProvider}.
40 */
41public class GlobalSearchLoader extends MultiRootDocumentsLoader {
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +080042 private final Bundle mQueryArgs;
Ivan Chiang282baa42018-08-08 18:54:14 +080043
44 /*
45 * Create the loader to query multiple roots support
46 * {@link DocumentsContract.Root#FLAG_LOCAL_ONLY} and
47 * {@link DocumentsContract.Root#FLAG_SUPPORTS_SEARCH} from
48 * {@link android.provider.DocumentsProvider}.
49 *
50 * @param context the context
51 * @param providers the providers
52 * @param state current state
53 * @param features the feature flags
54 * @param executors the executors of authorities
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +080055 * @param fileTypeMap the map of mime types and file types
56 * @param queryArgs the bundle of query arguments
Ivan Chiang282baa42018-08-08 18:54:14 +080057 */
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +080058 GlobalSearchLoader(Context context, ProvidersAccess providers, State state,
Ivan Chiang282baa42018-08-08 18:54:14 +080059 Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap,
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +080060 @NonNull Bundle queryArgs) {
Ivan Chiang282baa42018-08-08 18:54:14 +080061 super(context, providers, state, executors, fileTypeMap);
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +080062 mQueryArgs = queryArgs;
Ivan Chiang282baa42018-08-08 18:54:14 +080063 }
64
65 @Override
66 protected boolean shouldIgnoreRoot(RootInfo root) {
67 // Only support local search in GlobalSearchLoader
68 if (!root.isLocalOnly() || !root.supportsSearch()) {
69 return true;
70 }
71
72 // If the value of showAdvanced is true,
73 // don't query media roots and downloads root to avoid showing
74 // duplicated files.
75 if (mState.showAdvanced && (root.isLibrary() || root.isDownloads())) {
76 return true;
77 }
78 return false;
79 }
80
81 @Override
82 protected QueryTask getQueryTask(String authority, List<RootInfo> rootInfos) {
83 return new SearchTask(authority, rootInfos);
84 }
85
86 private class SearchTask extends QueryTask {
87
88 public SearchTask(String authority, List<RootInfo> rootInfos) {
89 super(authority, rootInfos);
90 }
91
92 @Override
93 protected void addQueryArgs(@NonNull Bundle queryArgs) {
Ivan Chiang282baa42018-08-08 18:54:14 +080094 queryArgs.putBoolean(DocumentsContract.QUERY_ARG_EXCLUDE_MEDIA, true);
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +080095 queryArgs.putAll(mQueryArgs);
Ivan Chiang282baa42018-08-08 18:54:14 +080096 }
97
98 @Override
99 protected Uri getQueryUri(RootInfo rootInfo) {
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +0800100 // For the new querySearchDocuments, we put the query string into queryArgs.
101 // Use the empty string to build the query uri.
Ivan Chiang282baa42018-08-08 18:54:14 +0800102 return DocumentsContract.buildSearchDocumentsUri(authority,
Ivan Chiangf0ea0ed2018-12-12 11:10:01 +0800103 rootInfo.rootId, "" /* query */);
Ivan Chiang282baa42018-08-08 18:54:14 +0800104 }
105
106 @Override
107 protected RootCursorWrapper generateResultCursor(RootInfo rootInfo, Cursor oriCursor) {
108 return new RootCursorWrapper(authority, rootInfo.rootId, oriCursor, -1 /* maxCount */);
109 }
110 }
111}