Skip to content

Commit

Permalink
feat[android]: Add log for query cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiJingLong committed Aug 23, 2023
1 parent db972a1 commit 57a265d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object AndroidQDBUtils : IDBUtils {
val selections =
"$BUCKET_ID IS NOT NULL $where"

val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
IDBUtils.storeBucketKeys,
selections,
Expand Down Expand Up @@ -90,7 +90,7 @@ object AndroidQDBUtils : IDBUtils {
val selections =
"$BUCKET_ID IS NOT NULL $where"

val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
IDBUtils.storeBucketKeys,
selections,
Expand Down Expand Up @@ -154,7 +154,7 @@ object AndroidQDBUtils : IDBUtils {
"$BUCKET_ID = ? $where"
}
val sortOrder = getSortOrder(page * size, size, option)
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys(),
selection,
Expand Down Expand Up @@ -194,7 +194,7 @@ object AndroidQDBUtils : IDBUtils {
}
val pageSize = end - start
val sortOrder = getSortOrder(start, pageSize, option)
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys(),
selection,
Expand Down Expand Up @@ -226,7 +226,7 @@ object AndroidQDBUtils : IDBUtils {
): AssetEntity? {
val selection = "$_ID = ?"
val args = arrayOf(id)
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys(),
selection,
Expand Down Expand Up @@ -260,7 +260,7 @@ object AndroidQDBUtils : IDBUtils {

val selection =
"$BUCKET_ID IS NOT NULL $where $idSelection"
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
IDBUtils.storeBucketKeys,
selection, args.toTypedArray(),
Expand Down Expand Up @@ -351,7 +351,7 @@ object AndroidQDBUtils : IDBUtils {
}

val cr = context.contentResolver
val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
copyKeys.toTypedArray() + arrayOf(RELATIVE_PATH),
idSelection,
Expand Down Expand Up @@ -421,7 +421,7 @@ object AndroidQDBUtils : IDBUtils {
Log.i(TAG, "The removeAllExistsAssets is starting.")
val removedList = ArrayList<String>()
val cr = context.contentResolver
val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
arrayOf(
BaseColumns._ID,
Expand Down Expand Up @@ -479,7 +479,7 @@ object AndroidQDBUtils : IDBUtils {

private fun getRelativePath(context: Context, galleryId: String): String? {
val cr = context.contentResolver
val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
arrayOf(BUCKET_ID, RELATIVE_PATH),
"$BUCKET_ID = ?",
Expand All @@ -496,7 +496,7 @@ object AndroidQDBUtils : IDBUtils {

override fun getSomeInfo(context: Context, assetId: String): Pair<String, String?>? {
val cr = context.contentResolver
val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
arrayOf(BUCKET_ID, RELATIVE_PATH),
"$_ID = ?",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object DBUtils : IDBUtils {
// val where = makeWhere(requestType, option, args)
val selection =
"${MediaStore.MediaColumns.BUCKET_ID} IS NOT NULL $where) GROUP BY (${MediaStore.MediaColumns.BUCKET_ID}"
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
IDBUtils.storeBucketKeys + arrayOf("count(1)"),
selection,
Expand Down Expand Up @@ -72,7 +72,7 @@ object DBUtils : IDBUtils {
val selections =
"${MediaStore.MediaColumns.BUCKET_ID} IS NOT NULL $where"

val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
projection,
selections,
Expand Down Expand Up @@ -113,7 +113,7 @@ object DBUtils : IDBUtils {
val where = option.makeWhere(type, args)
val selection =
"${MediaStore.MediaColumns.BUCKET_ID} IS NOT NULL $where $idSelection) GROUP BY (${MediaStore.MediaColumns.BUCKET_ID}"
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
IDBUtils.storeBucketKeys + arrayOf("count(1)"),
selection,
Expand Down Expand Up @@ -154,7 +154,7 @@ object DBUtils : IDBUtils {
"${MediaStore.MediaColumns.BUCKET_ID} = ? $where"
}
val sortOrder = getSortOrder(page * size, size, option)
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys,
selection,
Expand Down Expand Up @@ -195,7 +195,7 @@ object DBUtils : IDBUtils {
}
val pageSize = end - start
val sortOrder = getSortOrder(start, pageSize, option)
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys,
selection,
Expand Down Expand Up @@ -223,7 +223,7 @@ object DBUtils : IDBUtils {
val selection = "${MediaStore.MediaColumns._ID} = ?"
val args = arrayOf(id)

val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys,
selection,
Expand Down Expand Up @@ -284,7 +284,7 @@ object DBUtils : IDBUtils {
copyKeys.add(MediaStore.Video.VideoColumns.DESCRIPTION)
}

val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
copyKeys.toTypedArray() + arrayOf(MediaStore.MediaColumns.DATA),
idSelection,
Expand Down Expand Up @@ -334,7 +334,7 @@ object DBUtils : IDBUtils {
}

val cr = context.contentResolver
val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
arrayOf(MediaStore.MediaColumns.DATA),
idSelection,
Expand Down Expand Up @@ -374,7 +374,7 @@ object DBUtils : IDBUtils {
deleteLock.withLock {
val removedList = ArrayList<String>()
val cr = context.contentResolver
val cursor = cr.query(
val cursor = cr.logQuery(
allUri,
arrayOf(_ID, MediaStore.MediaColumns.DATA),
null,
Expand Down Expand Up @@ -409,7 +409,7 @@ object DBUtils : IDBUtils {
* 1 : current asset parent path
*/
override fun getSomeInfo(context: Context, assetId: String): Pair<String, String?>? {
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
arrayOf(MediaStore.MediaColumns.BUCKET_ID, MediaStore.MediaColumns.DATA),
"${MediaStore.MediaColumns._ID} = ?",
Expand All @@ -432,7 +432,7 @@ object DBUtils : IDBUtils {
MediaStore.MediaColumns.BUCKET_DISPLAY_NAME,
MediaStore.MediaColumns.DATA
)
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
keys,
"${MediaStore.MediaColumns.BUCKET_ID} = ?",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fluttercandies.photo_manager.core.utils

import android.content.ContentResolver
import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
Expand Down Expand Up @@ -68,8 +69,8 @@ interface IDBUtils {
}

val typeKeys = arrayOf(
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Images.Media.DISPLAY_NAME
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Images.Media.DISPLAY_NAME
)

val storeBucketKeys = arrayOf(BUCKET_ID, BUCKET_DISPLAY_NAME)
Expand All @@ -88,9 +89,9 @@ interface IDBUtils {
get() = IDBUtils.allUri

fun getAssetPathList(
context: Context,
requestType: Int = 0,
option: FilterOption
context: Context,
requestType: Int = 0,
option: FilterOption
): List<AssetPathEntity>

fun getAssetListPaged(
Expand Down Expand Up @@ -194,10 +195,14 @@ interface IDBUtils {
} else if (type == MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) {
val mmr = MediaMetadataRetriever()
mmr.setDataSource(path)
width = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: 0
height = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: 0
orientation = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)?.toInt()
?: orientation
width = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
?.toInt() ?: 0
height = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
?.toInt() ?: 0
orientation =
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)
?.toInt()
?: orientation
if (isAboveAndroidQ) mmr.close() else mmr.release()
}
} catch (e: Throwable) {
Expand Down Expand Up @@ -459,7 +464,8 @@ interface IDBUtils {
shouldKeepPath: Boolean = false,
): AssetEntity? {
val cr = context.contentResolver
val uri = cr.insert(contentUri, values) ?: throw RuntimeException("Cannot insert the new asset.")
val uri =
cr.insert(contentUri, values) ?: throw RuntimeException("Cannot insert the new asset.")
val id = ContentUris.parseId(uri)
if (!shouldKeepPath) {
val outputStream = cr.openOutputStream(uri)
Expand All @@ -472,7 +478,7 @@ interface IDBUtils {

fun assetExists(context: Context, id: String): Boolean {
val columns = arrayOf(_ID)
context.contentResolver.query(allUri, columns, "$_ID = ?", arrayOf(id), null).use {
context.contentResolver.logQuery(allUri, columns, "$_ID = ?", arrayOf(id), null).use {
if (it == null) {
return false
}
Expand All @@ -492,7 +498,8 @@ interface IDBUtils {
if (LogUtils.isLog) {
val splitter = "".padStart(40, '-')
LogUtils.info("log error row $id start $splitter")
val cursor = context.contentResolver.query(allUri, null, "$_ID = ?", arrayOf(id), null)
val cursor =
context.contentResolver.logQuery(allUri, null, "$_ID = ?", arrayOf(id), null)
cursor?.use {
val names = it.columnNames
if (it.moveToNext()) {
Expand Down Expand Up @@ -574,7 +581,7 @@ interface IDBUtils {
val key = arrayOf(_ID, MediaStore.Files.FileColumns.MEDIA_TYPE, DATA)
val idSelection = ids.joinToString(",") { "?" }
val selection = "$_ID in ($idSelection)"
val cursor = context.contentResolver.query(
val cursor = context.contentResolver.logQuery(
allUri,
key,
selection,
Expand Down Expand Up @@ -608,9 +615,9 @@ interface IDBUtils {
val columns = arrayOf(DATE_MODIFIED)
val sortOrder = "$DATE_MODIFIED desc"
val cursor = if (pathId == PhotoManager.ALL_ID) {
context.contentResolver.query(allUri, columns, null, null, sortOrder)
context.contentResolver.logQuery(allUri, columns, null, null, sortOrder)
} else {
context.contentResolver.query(
context.contentResolver.logQuery(
allUri,
columns,
"$BUCKET_ID = ?",
Expand All @@ -628,28 +635,64 @@ interface IDBUtils {

fun getColumnNames(context: Context): List<String> {
val cr = context.contentResolver
cr.query(allUri, null, null, null, null)?.use {
cr.logQuery(allUri, null, null, null, null)?.use {
return it.columnNames.toList()
}
return emptyList()
}

fun ContentResolver.logQuery(
uri: Uri,
projection: Array<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? {
fun log(logFunc: (log: String) -> Unit) {
if (LogUtils.isLog) {
val sb = StringBuilder()
sb.appendLine("uri: $uri")
sb.appendLine("projection: ${projection?.joinToString(", ")}")
sb.appendLine("selection: $selection")
sb.appendLine("selectionArgs: ${selectionArgs?.joinToString(", ")}")
sb.appendLine("sortOrder: $sortOrder")
logFunc(sb.toString())
}
}

try {
val cursor = query(uri, projection, selection, selectionArgs, sortOrder)
log(LogUtils::info)
return cursor
} catch (e: Exception) {
log(LogUtils::error)
LogUtils.error("happen query error", e)
throw e
}
}

fun getAssetCount(context: Context, option: FilterOption, requestType: Int): Int {
val cr = context.contentResolver
val args = ArrayList<String>()
val where = option.makeWhere(requestType, args, false)
val order = option.orderByCondString()
cr.query(allUri, arrayOf(_ID), where, args.toTypedArray(), order).use {
cr.logQuery(allUri, arrayOf(_ID), where, args.toTypedArray(), order).use {
return it?.count ?: 0
}
}

fun getAssetsByRange(context: Context, option: FilterOption, start: Int, end: Int, requestType: Int): List<AssetEntity> {
fun getAssetsByRange(
context: Context,
option: FilterOption,
start: Int,
end: Int,
requestType: Int
): List<AssetEntity> {
val cr = context.contentResolver
val args = ArrayList<String>()
val where = option.makeWhere(requestType, args, false)
val order = option.orderByCondString()
cr.query(allUri, keys(), where, args.toTypedArray(), order)?.use {
cr.logQuery(allUri, keys(), where, args.toTypedArray(), order)?.use {
val result = ArrayList<AssetEntity>()
it.moveToPosition(start - 1)
while (it.moveToNext()) {
Expand Down

0 comments on commit 57a265d

Please sign in to comment.