• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.documentsui.loaders
17 
18 import android.content.Context
19 import android.provider.DocumentsContract
20 import com.android.documentsui.ContentLock
21 import com.android.documentsui.DirectoryResult
22 import com.android.documentsui.LockingContentObserver
23 import com.android.documentsui.base.DocumentInfo
24 import com.android.documentsui.base.FilteringCursorWrapper
25 import com.android.documentsui.base.Lookup
26 import com.android.documentsui.base.RootInfo
27 import com.android.documentsui.base.UserId
28 import com.android.documentsui.sorting.SortModel
29 
30 /**
31  * A specialization of the BaseFileLoader that loads the children of a single folder. To list
32  * a directory you need to provide:
33  *
34  *  - The current application context
35  *  - A content lock for which a locking content observer is built
36  *  - A list of user IDs on behalf of which the search is conducted
37  *  - The root info of the listed directory
38  *  - The document info of the listed directory
39  *  - a lookup from file extension to file type
40  *  - The model capable of sorting results
41  */
42 class FolderLoader(
43     context: Context,
44     userIdList: List<UserId>,
45     mimeTypeLookup: Lookup<String, String>,
46     contentLock: ContentLock,
47     private val mRoot: RootInfo,
48     private val mListedDir: DocumentInfo,
49     private val mOptions: QueryOptions,
50     private val mSortModel: SortModel,
51 ) : BaseFileLoader(context, userIdList, mimeTypeLookup) {
52 
53     // An observer registered on the cursor to force a reload if the cursor reports a change.
54     private val mObserver = LockingContentObserver(contentLock, this::onContentChanged)
55 
56     // Creates a directory result object corresponding to the current parameters of the loader.
loadInBackgroundnull57     override fun loadInBackground(): DirectoryResult? {
58         val rejectBeforeTimestamp = mOptions.getRejectBeforeTimestamp()
59         val folderChildrenUri = DocumentsContract.buildChildDocumentsUri(
60             mListedDir.authority,
61             mListedDir.documentId
62         )
63         val cursor =
64             queryLocation(mRoot.rootId, folderChildrenUri, mOptions.otherQueryArgs, ALL_RESULTS)
65                 ?: emptyCursor()
66         cursor.registerContentObserver(mObserver)
67 
68         val filteredCursor = FilteringCursorWrapper(cursor)
69         filteredCursor.filterHiddenFiles(mOptions.showHidden)
70         filteredCursor.filterMimes(mOptions.acceptableMimeTypes, null)
71         if (rejectBeforeTimestamp > 0L) {
72             filteredCursor.filterLastModified(rejectBeforeTimestamp)
73         }
74         // TODO(b:380945065): Add filtering by category, such as images, audio, video.
75         val sortedCursor = mSortModel.sortCursor(filteredCursor, mMimeTypeLookup)
76 
77         val result = DirectoryResult()
78         result.doc = mListedDir
79         result.cursor = sortedCursor
80         return result
81     }
82 }
83