Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 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 { |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 42 | private final Bundle mQueryArgs; |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 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 |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 55 | * @param fileTypeMap the map of mime types and file types |
| 56 | * @param queryArgs the bundle of query arguments |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 57 | */ |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 58 | GlobalSearchLoader(Context context, ProvidersAccess providers, State state, |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 59 | Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap, |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 60 | @NonNull Bundle queryArgs) { |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 61 | super(context, providers, state, executors, fileTypeMap); |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 62 | mQueryArgs = queryArgs; |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 63 | } |
| 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 Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 94 | queryArgs.putBoolean(DocumentsContract.QUERY_ARG_EXCLUDE_MEDIA, true); |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 95 | queryArgs.putAll(mQueryArgs); |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected Uri getQueryUri(RootInfo rootInfo) { |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 100 | // For the new querySearchDocuments, we put the query string into queryArgs. |
| 101 | // Use the empty string to build the query uri. |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 102 | return DocumentsContract.buildSearchDocumentsUri(authority, |
Ivan Chiang | f0ea0ed | 2018-12-12 11:10:01 +0800 | [diff] [blame] | 103 | rootInfo.rootId, "" /* query */); |
Ivan Chiang | 282baa4 | 2018-08-08 18:54:14 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected RootCursorWrapper generateResultCursor(RootInfo rootInfo, Cursor oriCursor) { |
| 108 | return new RootCursorWrapper(authority, rootInfo.rootId, oriCursor, -1 /* maxCount */); |
| 109 | } |
| 110 | } |
| 111 | } |