diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/HiddenFoldersActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/HiddenFoldersActivity.kt index 7caa96d6b..79ce030f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/HiddenFoldersActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/HiddenFoldersActivity.kt @@ -21,16 +21,19 @@ class HiddenFoldersActivity : SimpleActivity(), RefreshRecyclerViewListener { } private fun updateFolders() { - val folders = getNoMediaFolders() - manage_folders_placeholder.apply { - text = getString(R.string.hidden_folders_placeholder) - beVisibleIf(folders.isEmpty()) - setTextColor(config.textColor) - } + getNoMediaFolders { + runOnUiThread { + manage_folders_placeholder.apply { + text = getString(R.string.hidden_folders_placeholder) + beVisibleIf(it.isEmpty()) + setTextColor(config.textColor) + } - val adapter = ManageHiddenFoldersAdapter(this, folders, this, manage_folders_list) {} - adapter.setupDragListener(true) - manage_folders_list.adapter = adapter + val adapter = ManageHiddenFoldersAdapter(this, it, this, manage_folders_list) {} + adapter.setupDragListener(true) + manage_folders_list.adapter = adapter + } + } } override fun onCreateOptionsMenu(menu: Menu?): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 58516723d..62c8f95c7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -79,31 +79,33 @@ fun Context.getSortedDirectories(source: ArrayList): ArrayList { - val folders = ArrayList() +fun Context.getNoMediaFolders(callback: (folders: ArrayList) -> Unit) { + Thread { + val folders = ArrayList() - val uri = MediaStore.Files.getContentUri("external") - val projection = arrayOf(MediaStore.Files.FileColumns.DATA) - val selection = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? AND ${MediaStore.Files.FileColumns.TITLE} LIKE ?" - val selectionArgs = arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_NONE.toString(), "%$NOMEDIA%") - val sortOrder = MediaStore.Files.FileColumns.DATA + val uri = MediaStore.Files.getContentUri("external") + val projection = arrayOf(MediaStore.Files.FileColumns.DATA) + val selection = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? AND ${MediaStore.Files.FileColumns.TITLE} LIKE ?" + val selectionArgs = arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_NONE.toString(), "%$NOMEDIA%") + val sortOrder = "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC" - var cursor: Cursor? = null + var cursor: Cursor? = null - try { - cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) - if (cursor?.moveToFirst() == true) { - do { - val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA) ?: continue - val noMediaFile = File(path) - if (noMediaFile.exists()) { - folders.add("${noMediaFile.parent}/") - } - } while (cursor.moveToNext()) + try { + cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) + if (cursor?.moveToFirst() == true) { + do { + val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA) ?: continue + val noMediaFile = File(path) + if (noMediaFile.exists()) { + folders.add("${noMediaFile.parent}/") + } + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() } - } finally { - cursor?.close() - } - return folders + callback(folders) + }.start() }