From a38d7cd8e2b39a1d57ccf7da10e60c3f333b34b5 Mon Sep 17 00:00:00 2001 From: Omkar Tenkale <39808155+omkar-tenkale@users.noreply.github.com> Date: Fri, 27 Aug 2021 16:49:19 +0530 Subject: [PATCH] Fix crash " Invalid token LIMIT " on android 11 https://github.com/OpenSooq/Gligar/issues/23 --- .../gligar/dataSource/ImagesDataSource.kt | 109 +++++++++++++----- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt index 1e2334a..ac41258 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt @@ -17,52 +17,90 @@ import kotlin.coroutines.CoroutineContext /** * Created by Hani AlMomani on 24,September,2019 + Modified by Omkar Tenkale to apply fix https://github.com/OpenSooq/Gligar/issues/23#issuecomment-798877787 by Shay-BH for issue https://github.com/OpenSooq/Gligar/issues/23 */ internal class ImagesDataSource(private val contentResolver: ContentResolver){ - fun loadAlbums(): ArrayList { - val albumCursor = contentResolver.query( - cursorUri, - arrayOf(DISPLAY_NAME_COLUMN,MediaStore.Images.ImageColumns.BUCKET_ID), +fun getCursorUri(): Uri { + + val collection: Uri + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + collection = newCursorUri; + } else { + collection = cursorUri; + } + + return collection; +} + +fun loadAlbums(): ArrayList { + val albumCursor = contentResolver.query( + getCursorUri(), + arrayOf(DISPLAY_NAME_COLUMN, MediaStore.Images.ImageColumns.BUCKET_ID), null, null, ORDER_BY - ) - val list = arrayListOf() - try { - list.add(AlbumItem("All", true,"0")) - if (albumCursor == null) { - return list - } - albumCursor.doWhile { + ) + val list = arrayListOf() + try { + list.add(AlbumItem("All", true, "0")) + if (albumCursor == null) { + return list + } + albumCursor.doWhile { + try { val bucketId = albumCursor.getString(albumCursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)) - val name = albumCursor.getString(albumCursor.getColumnIndex(DISPLAY_NAME_COLUMN)) ?: bucketId + val name = albumCursor.getString(albumCursor.getColumnIndex(DISPLAY_NAME_COLUMN)) + ?: bucketId var albumItem = AlbumItem(name, false, bucketId) if (!list.contains(albumItem)) { list.add(albumItem) } } - } finally { - if (albumCursor != null && !albumCursor.isClosed) { - albumCursor.close() + catch (ex: Exception) { + ex.printStackTrace() } } - return list + } finally { + if (albumCursor != null && !albumCursor.isClosed) { + albumCursor.close() + } } + return list +} - fun loadAlbumImages( +fun loadAlbumImages( albumItem: AlbumItem?, page: Int - ): ArrayList { - val offset = page * PAGE_SIZE - val list: ArrayList = arrayListOf() - var photoCursor: Cursor? = null - try { +): ArrayList { + val offset = page * PAGE_SIZE + val list: ArrayList = arrayListOf() + var photoCursor: Cursor? = null + try { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { + + val bundle = Bundle().apply { + putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE) + putInt(ContentResolver.QUERY_ARG_OFFSET, offset) + putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, "${MediaStore.MediaColumns.DATE_MODIFIED} DESC") + } + + photoCursor = contentResolver.query( + getCursorUri(), + arrayOf( + ID_COLUMN, + PATH_COLUMN + ), + bundle, + null + ) + } + else { if (albumItem == null || albumItem.isAll) { photoCursor = contentResolver.query( - cursorUri, + getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN @@ -73,7 +111,7 @@ internal class ImagesDataSource(private val contentResolver: ContentResolver){ ) } else { photoCursor = contentResolver.query( - cursorUri, + getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN @@ -83,16 +121,25 @@ internal class ImagesDataSource(private val contentResolver: ContentResolver){ "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } - photoCursor?.isAfterLast ?: return list - photoCursor.doWhile { + } + photoCursor?.isAfterLast ?: return list + photoCursor.doWhile { + try { val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN))) list.add(ImageItem(image, ImageSource.GALLERY, 0)) } - } finally { - if (photoCursor != null && !photoCursor.isClosed()) { + catch (ex: Exception) { + ex.printStackTrace() + } + } + } finally { + if (photoCursor != null) { + if (!photoCursor.isClosed()) { photoCursor.close() } } - return list } -} \ No newline at end of file + return list +} + +}