Skip to content

Commit

Permalink
Fix crash " Invalid token LIMIT " on android 11
Browse files Browse the repository at this point in the history
  • Loading branch information
omkar-tenkale authored Aug 27, 2021
1 parent 081f699 commit a38d7cd
Showing 1 changed file with 78 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlbumItem> {
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<AlbumItem> {
val albumCursor = contentResolver.query(
getCursorUri(),
arrayOf(DISPLAY_NAME_COLUMN, MediaStore.Images.ImageColumns.BUCKET_ID),
null,
null,
ORDER_BY
)
val list = arrayListOf<AlbumItem>()
try {
list.add(AlbumItem("All", true,"0"))
if (albumCursor == null) {
return list
}
albumCursor.doWhile {
)
val list = arrayListOf<AlbumItem>()
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<ImageItem> {
val offset = page * PAGE_SIZE
val list: ArrayList<ImageItem> = arrayListOf()
var photoCursor: Cursor? = null
try {
): ArrayList<ImageItem> {
val offset = page * PAGE_SIZE
val list: ArrayList<ImageItem> = 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
Expand All @@ -73,7 +111,7 @@ internal class ImagesDataSource(private val contentResolver: ContentResolver){
)
} else {
photoCursor = contentResolver.query(
cursorUri,
getCursorUri(),
arrayOf(
ID_COLUMN,
PATH_COLUMN
Expand All @@ -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
}
}
return list
}

}

0 comments on commit a38d7cd

Please sign in to comment.