File tree Expand file tree Collapse file tree
src/main/java/com/lazygeniouz/dfc/resolver Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,11 +22,11 @@ android {
2222 }
2323
2424 compileOptions {
25- sourceCompatibility JavaVersion . VERSION_1_8
26- targetCompatibility JavaVersion . VERSION_1_8
25+ sourceCompatibility JavaVersion . VERSION_17
26+ targetCompatibility JavaVersion . VERSION_17
2727 }
2828
2929 kotlinOptions {
30- jvmTarget = ' 1.8 '
30+ jvmTarget = JavaVersion . VERSION_17 . toString()
3131 }
3232}
Original file line number Diff line number Diff line change @@ -81,8 +81,11 @@ internal object ResolverCompat {
8181 */
8282 internal fun count (context : Context , uri : Uri ): Int {
8383 val childrenUri = createChildrenUri(uri)
84- getCursor(context, childrenUri, iconProjection)?.use { cursor -> return cursor.count }
85- return 0
84+ return getCursor(
85+ context,
86+ childrenUri,
87+ iconProjection
88+ )?.use { cursor -> return cursor.count } ? : 0
8689 }
8790
8891 /* *
@@ -108,6 +111,17 @@ internal object ResolverCompat {
108111 val listOfDocuments = arrayListOf<DocumentFileCompat >()
109112
110113 getCursor(context, childrenUri, fullProjection)?.use { cursor ->
114+ val itemCount = cursor.count
115+ /* *
116+ * Pre-sizing the list to avoid resizing overhead.
117+ * This is especially beneficial for directories with a large number of files.
118+ *
119+ * Memory comparison for 8192 files:
120+ * 1. With pre-sizing: 3.10 MB
121+ * 2. Without pre-sizing: 9.60 MB
122+ */
123+ if (itemCount > 10 ) listOfDocuments.ensureCapacity(itemCount)
124+
111125 while (cursor.moveToNext()) {
112126 val documentId: String = cursor.getString(0 )
113127 val documentUri = DocumentsContract .buildDocumentUriUsingTree(uri, documentId)
You can’t perform that action at this time.
0 commit comments