From 9c7d212d52e34302386564ecc5435a7ed3255db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey=20Vilas?= Date: Tue, 21 Dec 2021 17:12:09 +0100 Subject: [PATCH] Fix for too many thumbnails in autoupload settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch is twofold: - Keep using SQL limit until android 11 (which is where it becomes mandatory to not use it) - Force MediaProvider to stop querying images after limit has been reached, even if cursor contains more. This handles the edge case of Android versions over 11 which don't properly implement the limit argument Signed-off-by: Álvaro Brey Vilas --- .../android/datamodel/ContentResolverHelper.kt | 4 ++-- .../com/owncloud/android/datamodel/MediaProvider.java | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/ContentResolverHelper.kt b/src/main/java/com/owncloud/android/datamodel/ContentResolverHelper.kt index c2fd5c8dc6..706fc5e707 100644 --- a/src/main/java/com/owncloud/android/datamodel/ContentResolverHelper.kt +++ b/src/main/java/com/owncloud/android/datamodel/ContentResolverHelper.kt @@ -58,7 +58,7 @@ object ContentResolverHelper { "Invalid sort direction" } return when { - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> { val queryArgs = getQueryArgsBundle(selection, sortColumn, sortDirection, limit) contentResolver.query(uri, projection, queryArgs, cancellationSignal) } @@ -90,7 +90,7 @@ object ContentResolverHelper { return sortOrderBuilder.toString() } - @RequiresApi(Build.VERSION_CODES.O) + @RequiresApi(Build.VERSION_CODES.R) private fun getQueryArgsBundle(selection: String?, sortColumn: String?, sortDirection: String?, limit: Int?): Bundle { return Bundle().apply { diff --git a/src/main/java/com/owncloud/android/datamodel/MediaProvider.java b/src/main/java/com/owncloud/android/datamodel/MediaProvider.java index 2df6176e0a..d72e7a6232 100644 --- a/src/main/java/com/owncloud/android/datamodel/MediaProvider.java +++ b/src/main/java/com/owncloud/android/datamodel/MediaProvider.java @@ -123,8 +123,8 @@ public final class MediaProvider { if (cursorImages != null) { String filePath; - - while (cursorImages.moveToNext()) { + int imageCount = 0; + while (cursorImages.moveToNext() && imageCount < itemLimit) { filePath = cursorImages.getString(cursorImages.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA)); @@ -133,6 +133,8 @@ public final class MediaProvider { mediaFolder.filePaths.add(filePath); mediaFolder.absolutePath = filePath.substring(0, filePath.lastIndexOf('/')); } + // ensure we don't go over the limit due to faulty android implementations + imageCount++; } cursorImages.close(); @@ -241,7 +243,8 @@ public final class MediaProvider { if (cursorVideos != null) { String filePath; - while (cursorVideos.moveToNext()) { + int videoCount = 0; + while (cursorVideos.moveToNext() && videoCount < itemLimit) { filePath = cursorVideos.getString(cursorVideos.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA)); @@ -249,6 +252,8 @@ public final class MediaProvider { mediaFolder.filePaths.add(filePath); mediaFolder.absolutePath = filePath.substring(0, filePath.lastIndexOf('/')); } + // ensure we don't go over the limit due to faulty android implementations + videoCount++; } cursorVideos.close();