1 /* 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 17 package com.android.documentsui; 18 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.provider.DocumentsContract; 24 25 import androidx.annotation.NonNull; 26 27 import com.android.documentsui.base.Lookup; 28 import com.android.documentsui.base.RootInfo; 29 import com.android.documentsui.base.State; 30 import com.android.documentsui.roots.ProvidersAccess; 31 import com.android.documentsui.roots.RootCursorWrapper; 32 33 import java.util.List; 34 import 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 */ 41 public class GlobalSearchLoader extends MultiRootDocumentsLoader { 42 private final Bundle mQueryArgs; 43 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 55 * @param fileTypeMap the map of mime types and file types 56 * @param queryArgs the bundle of query arguments 57 */ GlobalSearchLoader(Context context, ProvidersAccess providers, State state, Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, @NonNull Bundle queryArgs)58 GlobalSearchLoader(Context context, ProvidersAccess providers, State state, 59 Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, 60 @NonNull Bundle queryArgs) { 61 super(context, providers, state, executors, fileTypeMap); 62 mQueryArgs = queryArgs; 63 } 64 65 @Override shouldIgnoreRoot(RootInfo root)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 getQueryTask(String authority, List<RootInfo> rootInfos)82 protected QueryTask getQueryTask(String authority, List<RootInfo> rootInfos) { 83 return new SearchTask(authority, rootInfos); 84 } 85 86 private class SearchTask extends QueryTask { 87 SearchTask(String authority, List<RootInfo> rootInfos)88 public SearchTask(String authority, List<RootInfo> rootInfos) { 89 super(authority, rootInfos); 90 } 91 92 @Override addQueryArgs(@onNull Bundle queryArgs)93 protected void addQueryArgs(@NonNull Bundle queryArgs) { 94 queryArgs.putBoolean(DocumentsContract.QUERY_ARG_EXCLUDE_MEDIA, true); 95 queryArgs.putAll(mQueryArgs); 96 } 97 98 @Override getQueryUri(RootInfo rootInfo)99 protected Uri getQueryUri(RootInfo rootInfo) { 100 // For the new querySearchDocuments, we put the query string into queryArgs. 101 // Use the empty string to build the query uri. 102 return DocumentsContract.buildSearchDocumentsUri(authority, 103 rootInfo.rootId, "" /* query */); 104 } 105 106 @Override generateResultCursor(RootInfo rootInfo, Cursor oriCursor)107 protected RootCursorWrapper generateResultCursor(RootInfo rootInfo, Cursor oriCursor) { 108 return new RootCursorWrapper(authority, rootInfo.rootId, oriCursor, -1 /* maxCount */); 109 } 110 } 111 } 112