From 9c7c995251ec1c19a7746d8ad7dbeab1a409ae23 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 28 Jul 2019 19:30:17 +0200 Subject: [PATCH] moving whole new photo fetcher handling on a background thread --- .../gallery/pro/jobs/NewPhotoFetcher.kt | 77 +++++++++---------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt index e00e2eb4f..26095afdf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt @@ -56,46 +56,45 @@ class NewPhotoFetcher : JobService() { override fun onStartJob(params: JobParameters): Boolean { mRunningParams = params - - val affectedFolderPaths = HashSet() - if (params.triggeredContentAuthorities != null && params.triggeredContentUris != null) { - val ids = arrayListOf() - for (uri in params.triggeredContentUris!!) { - val path = uri.pathSegments - if (path != null && (path.size == PHOTO_PATH_SEGMENTS.size + 1 || path.size == VIDEO_PATH_SEGMENTS.size + 1)) { - ids.add(path[path.size - 1]) - } - } - - if (ids.isNotEmpty()) { - val selection = StringBuilder() - for (id in ids) { - if (selection.isNotEmpty()) { - selection.append(" OR ") - } - selection.append("${MediaStore.Images.ImageColumns._ID} = '$id'") - } - - var cursor: Cursor? = null - try { - val projection = arrayOf(MediaStore.Images.ImageColumns.DATA) - val uris = arrayListOf(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI) - uris.forEach { - cursor = contentResolver.query(it, projection, selection.toString(), null, null) - while (cursor!!.moveToNext()) { - val path = cursor!!.getStringValue(MediaStore.Images.ImageColumns.DATA) - affectedFolderPaths.add(path.getParentPath()) - addPathToDB(path) - } - } - } catch (ignored: Exception) { - } finally { - cursor?.close() - } - } - } - ensureBackgroundThread { + val affectedFolderPaths = HashSet() + if (params.triggeredContentAuthorities != null && params.triggeredContentUris != null) { + val ids = arrayListOf() + for (uri in params.triggeredContentUris!!) { + val path = uri.pathSegments + if (path != null && (path.size == PHOTO_PATH_SEGMENTS.size + 1 || path.size == VIDEO_PATH_SEGMENTS.size + 1)) { + ids.add(path[path.size - 1]) + } + } + + if (ids.isNotEmpty()) { + val selection = StringBuilder() + for (id in ids) { + if (selection.isNotEmpty()) { + selection.append(" OR ") + } + selection.append("${MediaStore.Images.ImageColumns._ID} = '$id'") + } + + var cursor: Cursor? = null + try { + val projection = arrayOf(MediaStore.Images.ImageColumns.DATA) + val uris = arrayListOf(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI) + uris.forEach { + cursor = contentResolver.query(it, projection, selection.toString(), null, null) + while (cursor!!.moveToNext()) { + val path = cursor!!.getStringValue(MediaStore.Images.ImageColumns.DATA) + affectedFolderPaths.add(path.getParentPath()) + addPathToDB(path) + } + } + } catch (ignored: Exception) { + } finally { + cursor?.close() + } + } + } + affectedFolderPaths.forEach { updateDirectoryPath(it) }