Skip to content

Commit

Permalink
Merge pull request #224 from CaiJingLong/thumb-quality
Browse files Browse the repository at this point in the history
Add quality for thumb method.
  • Loading branch information
CaiJingLong authored Mar 22, 2020
2 parents f66815d + 9b465ca commit 7117d21
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 275 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Feature:

- Add `quality` for `AssetEntity.thumbDataWithSize`.
- Add `orientation` for `AssetEntity`.
- Add `onlyAll` for `getAssetPathList`.
- Support audio type(Only android, iOS Photos have no audio)
Expand Down
65 changes: 33 additions & 32 deletions android/src/main/kotlin/top/kikt/imagescanner/core/PhotoManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ import java.io.FileInputStream
/// create 2019-09-05 by cai
/// Do some business logic assembly
class PhotoManager(private val context: Context) {

companion object {
const val ALL_ID = "isAll"
}

var useOldApi: Boolean = false

private val dbUtils: IDBUtils
get() = if (useOldApi || Build.VERSION.SDK_INT < 29) {
DBUtils
} else {
AndroidQDBUtils
}

fun getGalleryList(type: Int, timeStamp: Long, hasAll: Boolean, onlyAll: Boolean, option: FilterOption): List<GalleryEntity> {
if (onlyAll) {
return dbUtils.getOnlyGalleryList(context, type, timeStamp, option)
}

val fromDb = dbUtils.getGalleryList(context, type, timeStamp, option)

if (!hasAll) {
return fromDb
}

// make is all to the gallery list
val entity = fromDb.run {
var count = 0
Expand All @@ -52,37 +52,38 @@ class PhotoManager(private val context: Context) {
}
GalleryEntity(ALL_ID, "Recent", count, type, true)
}

return listOf(entity) + fromDb
}

fun getAssetList(galleryId: String, page: Int, pageCount: Int, typeInt: Int = 0, timestamp: Long, option: FilterOption): List<AssetEntity> {
val gId = if (galleryId == ALL_ID) "" else galleryId
return dbUtils.getAssetFromGalleryId(context, gId, page, pageCount, typeInt, timestamp, option)
}


fun getAssetListWithRange(galleryId: String, type: Int, start: Int, end: Int, timestamp: Long, option: FilterOption): List<AssetEntity> {
val gId = if (galleryId == ALL_ID) "" else galleryId
return dbUtils.getAssetFromGalleryIdRange(context, gId, start, end, type, timestamp, option)
}
fun getThumb(id: String, width: Int, height: Int, format: Int, resultHandler: ResultHandler) {

fun getThumb(id: String, width: Int, height: Int, format: Int, quality: Int, resultHandler: ResultHandler) {
try {
if (!isAndroidQ) {
val asset = dbUtils.getAssetEntity(context, id)
if (asset == null) {
resultHandler.replyError("The asset not found!")
return
}
ThumbnailUtil.getThumbnailByGlide(context, asset.path, width, height, format, resultHandler.result)
ThumbnailUtil.getThumbnailByGlide(context, asset.path, width, height, format, quality, resultHandler.result)
} else {
// need use android Q MediaStore thumbnail api

val asset = dbUtils.getAssetEntity(context, id)
val type = asset?.type
val uri = dbUtils.getThumbUri(context, id, width, height, type) ?: throw RuntimeException("Cannot load uri of $id.")
ThumbnailUtil.getThumbOfUri(context, uri, width, height, format) {
val uri = dbUtils.getThumbUri(context, id, width, height, type)
?: throw RuntimeException("Cannot load uri of $id.")
ThumbnailUtil.getThumbOfUri(context, uri, width, height, format, quality) {
resultHandler.reply(it)
}
}
Expand All @@ -92,10 +93,10 @@ class PhotoManager(private val context: Context) {
resultHandler.replyError("201", "get thumb error", e)
}
}

fun getOriginBytes(id: String, cacheOriginBytes: Boolean, haveLocationPermission: Boolean, resultHandler: ResultHandler) {
val asset = dbUtils.getAssetEntity(context, id)

if (asset == null) {
resultHandler.replyError("The asset not found")
return
Expand All @@ -116,11 +117,11 @@ class PhotoManager(private val context: Context) {
resultHandler.replyError("202", "get origin Bytes error", e)
}
}

fun clearCache() {
dbUtils.clearCache()
}

fun getPathEntity(id: String, type: Int, timestamp: Long, option: FilterOption): GalleryEntity? {
if (id == ALL_ID) {
val allGalleryList = dbUtils.getGalleryList(context, type, timestamp, option)
Expand All @@ -139,48 +140,48 @@ class PhotoManager(private val context: Context) {
}
return dbUtils.getGalleryEntity(context, id, type, timestamp, option)
}

fun getFile(id: String, isOrigin: Boolean, resultHandler: ResultHandler) {
val path = dbUtils.getFilePath(context, id, isOrigin)
resultHandler.reply(path)
}

fun deleteAssetWithIds(ids: List<String>): List<String> {
return dbUtils.deleteWithIds(context, ids)
}

fun saveImage(image: ByteArray, title: String, description: String): AssetEntity? {
return dbUtils.saveImage(context, image, title, description)
}

fun saveVideo(path: String, title: String, desc: String): AssetEntity? {
if (!File(path).exists()) {
return null
}
return dbUtils.saveVideo(context, FileInputStream(path), title, desc)
}

fun assetExists(id: String, resultHandler: ResultHandler) {
val exists: Boolean = dbUtils.exists(context, id)
resultHandler.reply(exists)
}

fun getLocation(id: String): Map<String, Double> {
val exifInfo = dbUtils.getExif(context, id)
val latLong = exifInfo?.latLong
return if (latLong == null) {
mapOf(
"lat" to 0.0,
"lng" to 0.0
"lat" to 0.0,
"lng" to 0.0
)
} else {
mapOf(
"lat" to latLong[0],
"lng" to latLong[1]
"lat" to latLong[0],
"lng" to latLong[1]
)
}
}

fun getMediaUri(id: String, type: Int): String {
return dbUtils.getMediaUri(context, id, type)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ import java.util.concurrent.TimeUnit

class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : MethodChannel.MethodCallHandler {

companion object {
private const val poolSize = 8
private val threadPool: ThreadPoolExecutor = ThreadPoolExecutor(
poolSize + 3,
1000,
200,
TimeUnit.MINUTES,
ArrayBlockingQueue<Runnable>(poolSize + 3)
poolSize + 3,
1000,
200,
TimeUnit.MINUTES,
ArrayBlockingQueue<Runnable>(poolSize + 3)
)

fun runOnBackground(runnable: () -> Unit) {
threadPool.execute(runnable)
}

var cacheOriginBytes = true
}

private val permissionsUtils = PermissionsUtils()
private val notifyChannel = PhotoManagerNotifyChannel(registrar, Handler())

init {
registrar.addRequestPermissionsResultListener { i, strings, ints ->
permissionsUtils.dealResult(i, strings, ints)
Expand All @@ -50,19 +50,19 @@ class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : Meth
permissionsUtils.permissionsListener = object : PermissionsListener {
override fun onDenied(deniedPermissions: MutableList<String>, grantedPermissions: MutableList<String>) {
}

override fun onGranted() {
}
}
}

private val photoManager = PhotoManager(registrar.context().applicationContext)

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
val resultHandler = ResultHandler(result)

var needLocationPermissions = false

val handleResult = when (call.method) {
"releaseMemCache" -> {
photoManager.clearCache()
Expand Down Expand Up @@ -112,11 +112,11 @@ class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : Meth
}
else -> false
}

if (handleResult) {
return
}

val utils = permissionsUtils.apply {
withActivity(registrar.activity())
permissionsListener = object : PermissionsListener {
Expand All @@ -132,26 +132,26 @@ class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : Meth
}
}
}

override fun onGranted() {
onHandlePermissionResult(call, resultHandler, true)
}
}
}

val permissions = arrayListOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)

if (needLocationPermissions && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
permissions.add(Manifest.permission.ACCESS_MEDIA_LOCATION)
}

utils.getPermissions(registrar.activity(), 3001, permissions)
}

private fun replyPermissionError(resultHandler: ResultHandler) {
resultHandler.replyError("Request for permission failed.", "User denied permission.", null)
}

private fun onHandlePermissionResult(call: MethodCall, resultHandler: ResultHandler, haveLocationPermission: Boolean) {
LogUtils.info("onGranted call.method = ${call.method}")
when (call.method) {
Expand All @@ -166,7 +166,7 @@ class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : Meth
val hasAll = call.argument<Boolean>("hasAll")!!
val option = call.getOption()
val onlyAll = call.argument<Boolean>("onlyAll")!!

val list = photoManager.getGalleryList(type, timeStamp, hasAll, onlyAll, option)
resultHandler.reply(ConvertUtils.convertToGalleryResult(list))
}
Expand Down Expand Up @@ -201,7 +201,8 @@ class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : Meth
val width = call.argument<Int>("width")!!
val height = call.argument<Int>("height")!!
val format = call.argument<Int>("format")!!
photoManager.getThumb(id, width, height, format, resultHandler)
val quality = call.argument<Int>("quality")!!
photoManager.getThumb(id, width, height, format, quality, resultHandler)
}
}
"assetExists" -> {
Expand Down Expand Up @@ -312,19 +313,19 @@ class PhotoManagerPlugin(private val registrar: PluginRegistry.Registrar) : Meth
else -> resultHandler.notImplemented()
}
}

fun MethodCall.getTimeStamp(): Long {
return this.argument<Long>("timestamp")!!
}

fun MethodCall.getString(key: String): String {
return this.argument<String>(key)!!
}

fun MethodCall.getInt(key: String): Int {
return this.argument<Int>(key)!!
}

fun MethodCall.getOption(): FilterOption {
val arguments = argument<Map<*, *>>("option")!!
return ConvertUtils.convertFilterOptionsFromMap(arguments)
Expand Down
Loading

0 comments on commit 7117d21

Please sign in to comment.