Skip to content

Commit

Permalink
Fix Crash on Android 11 OpenSooq#23
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazan Tarifi committed Apr 29, 2021
1 parent 1f9b624 commit d8d692b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.opensooq:gligar:1.1.1'
implementation project(":gligar")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.opensooq.supernova.gligar.dataSource

import android.content.ContentResolver
import android.database.Cursor
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import androidx.annotation.RequiresApi
import androidx.paging.PositionalDataSource
import com.opensooq.OpenSooq.ui.imagePicker.model.AlbumItem
import com.opensooq.OpenSooq.ui.imagePicker.model.ImageItem
Expand Down Expand Up @@ -55,40 +58,61 @@ internal class ImagesDataSource(private val contentResolver: ContentResolver){
return list
}

private fun getCurserQuery(albumItem: AlbumItem?, offset: Int): Cursor? {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val queryBundle = Bundle().apply {
putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE)
putInt(ContentResolver.QUERY_ARG_OFFSET, offset)
putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, ORDER_BY)
}

if (!(albumItem == null || albumItem.isAll)) {
queryBundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, "${MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME} =?")
queryBundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, arrayOf(albumItem.name))
}

return contentResolver.query(
cursorUri,
getSelectionProjection(),
queryBundle,
null
)
} else {
return if (albumItem == null || albumItem.isAll) {
contentResolver.query(cursorUri, getSelectionProjection(), null, null, ORDER_BY + " LIMIT $PAGE_SIZE" + " OFFSET $offset")
} else {
contentResolver.query(cursorUri, getSelectionProjection(), "${MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME} =?", arrayOf(albumItem.name), ORDER_BY + " LIMIT $PAGE_SIZE" + " OFFSET $offset")
}
}
} catch (ex: Exception) {
print("III :: ${ex.message}")
ex.printStackTrace()
return null
}
}

private fun getSelectionProjection(): Array<String> {
return arrayOf(ID_COLUMN, PATH_COLUMN)
}

fun loadAlbumImages(
albumItem: AlbumItem?,
page: Int,
supportedImages: String? = null,
preSelectedImages: Array<out String?>? = null
): ArrayList<ImageItem> {
val offset = page * PAGE_SIZE
val list: ArrayList<ImageItem> = arrayListOf()
var photoCursor: Cursor? = null
val list: ArrayList<ImageItem> = ArrayList()
try {
if (albumItem == null || albumItem.isAll) {
photoCursor = contentResolver.query(
cursorUri,
arrayOf(
ID_COLUMN,
PATH_COLUMN
),
null,
null,
"$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset"
)
} else {
photoCursor = contentResolver.query(
cursorUri,
arrayOf(
ID_COLUMN,
PATH_COLUMN
),
"${MediaStore.Images.ImageColumns.BUCKET_ID} =?",
arrayOf(albumItem.bucketId),
"$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset"
)
val offset = page * PAGE_SIZE
photoCursor = getCurserQuery(albumItem, offset)

photoCursor?.isAfterLast
if (photoCursor == null) {
return list
}
photoCursor?.isAfterLast ?: return list

while(photoCursor.moveToNext()) {
val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN)))
if (supportedImages != null) {
Expand All @@ -108,6 +132,9 @@ internal class ImagesDataSource(private val contentResolver: ContentResolver){
}
}
}
} catch (ex: Exception) {
println("III :: ${ex.message}")
ex.printStackTrace()
} finally {
if (photoCursor != null && !photoCursor.isClosed()) {
photoCursor.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) :
}
}

private suspend fun getImages() = withContext(Dispatchers.Default) {
if (!TextUtils.equals(supportedImages, ALL_TYPES)) {
mImageDataSource.loadAlbumImages(mSelectedAlbum, mPage, supportedImages, preSelectedImages)
} else {
mImageDataSource.loadAlbumImages(mSelectedAlbum, mPage, null, preSelectedImages)
private suspend fun getImages(): ArrayList<ImageItem> {
return withContext(Dispatchers.IO) {
if (!TextUtils.equals(supportedImages, ALL_TYPES)) {
mImageDataSource.loadAlbumImages(mSelectedAlbum, mPage, supportedImages, preSelectedImages)
} else {
mImageDataSource.loadAlbumImages(mSelectedAlbum, mPage, null, preSelectedImages)
}
}
}


private suspend fun getAlbums() = withContext(Dispatchers.Default) {
mImageDataSource.loadAlbums()
}
Expand Down

0 comments on commit d8d692b

Please sign in to comment.