From fe8e139fffd13cf5854bcd3a63f40a2e764142b6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 7 Apr 2020 12:39:48 +0200 Subject: [PATCH] adding a helper extension for querying cursors --- build.gradle | 2 +- .../commons/extensions/Context.kt | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index a62e167bd..5ba943679 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { propMinSdkVersion = 21 propTargetSdkVersion = propCompileSdkVersion propVersionCode = 1 - propVersionName = '5.24.18' + propVersionName = '5.24.19' kotlin_version = '1.3.71' } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt index 5996c87d5..79df3ea3c 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt @@ -306,6 +306,31 @@ fun Context.getMediaContent(path: String, uri: Uri): Uri? { return null } +fun Context.queryCursor( + uri: Uri, + projection: Array, + selection: String? = null, + selectionArgs: Array? = null, + sortOrder: String? = null, + showErrors: Boolean = false, + callback: (cursor: Cursor) -> Unit +) { + try { + val cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) + cursor?.use { + if (cursor.moveToFirst()) { + do { + callback(cursor) + } while (cursor.moveToNext()) + } + } + } catch (e: Exception) { + if (showErrors) { + showErrorToast(e) + } + } +} + fun Context.getFilenameFromUri(uri: Uri): String { return if (uri.scheme == "file") { File(uri.toString()).name