upgrade deprecated code

This commit is contained in:
Wenqi Li 2023-05-09 21:08:38 +08:00
parent f8842c89a8
commit a5e5543b3d

View File

@ -2,6 +2,14 @@ package com.morbit.photogallery
import android.content.ContentResolver import android.content.ContentResolver
import android.content.ContentUris import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.database.Cursor.FIELD_TYPE_INTEGER
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.os.Build
import android.provider.MediaStore
import android.util.Size
import androidx.annotation.NonNull import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
@ -9,28 +17,14 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar import io.flutter.plugin.common.PluginRegistry.Registrar
import android.graphics.Bitmap
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import android.provider.MediaStore
import android.content.Context
import android.database.Cursor
import android.database.Cursor.FIELD_TYPE_INTEGER
import android.graphics.ImageDecoder
import android.os.AsyncTask
import android.os.Build
import android.util.Size
import java.io.FileOutputStream import java.io.FileOutputStream
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/** PhotoGalleryPlugin */ /** PhotoGalleryPlugin */
class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val channel = MethodChannel(flutterPluginBinding.binaryMessenger, "photo_gallery")
val plugin = PhotoGalleryPlugin()
plugin.context = flutterPluginBinding.applicationContext
channel.setMethodCallHandler(plugin)
}
companion object { companion object {
// This static function is optional and equivalent to onAttachedToEngine. It supports the old // This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
@ -82,17 +76,31 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
) )
} }
private var context: Context? = null private lateinit var channel: MethodChannel
private lateinit var context: Context
private val executor: ExecutorService = Executors.newSingleThreadExecutor()
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "photo_gallery")
val plugin = PhotoGalleryPlugin()
plugin.context = flutterPluginBinding.applicationContext
channel.setMethodCallHandler(plugin)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) { when (call.method) {
"listAlbums" -> { "listAlbums" -> {
val mediumType = call.argument<String>("mediumType") val mediumType = call.argument<String>("mediumType")
BackgroundAsyncTask({ executor.submit {
listAlbums(mediumType!!) result.success(
}, { v -> listAlbums(mediumType!!)
result.success(v) )
}) }
} }
"listMedia" -> { "listMedia" -> {
val albumId = call.argument<String>("albumId") val albumId = call.argument<String>("albumId")
@ -101,24 +109,24 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val total = call.argument<Int>("total") val total = call.argument<Int>("total")
val skip = call.argument<Int>("skip") val skip = call.argument<Int>("skip")
val take = call.argument<Int>("take") val take = call.argument<Int>("take")
BackgroundAsyncTask({ executor.submit {
when (mediumType) { result.success(
imageType -> listImages(albumId!!, newest!!, total!!, skip, take) when (mediumType) {
videoType -> listVideos(albumId!!, newest!!, total!!, skip, take) imageType -> listImages(albumId!!, newest!!, total!!, skip, take)
else -> null videoType -> listVideos(albumId!!, newest!!, total!!, skip, take)
} else -> null
}, { v -> }
result.success(v) )
}) }
} }
"getMedium" -> { "getMedium" -> {
val mediumId = call.argument<String>("mediumId") val mediumId = call.argument<String>("mediumId")
val mediumType = call.argument<String>("mediumType") val mediumType = call.argument<String>("mediumType")
BackgroundAsyncTask({ executor.submit {
getMedium(mediumId!!, mediumType) result.success(
}, { v -> getMedium(mediumId!!, mediumType)
result.success(v) )
}) }
} }
"getThumbnail" -> { "getThumbnail" -> {
val mediumId = call.argument<String>("mediumId") val mediumId = call.argument<String>("mediumId")
@ -126,11 +134,11 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val width = call.argument<Int>("width") val width = call.argument<Int>("width")
val height = call.argument<Int>("height") val height = call.argument<Int>("height")
val highQuality = call.argument<Boolean>("highQuality") val highQuality = call.argument<Boolean>("highQuality")
BackgroundAsyncTask({ executor.submit {
getThumbnail(mediumId!!, mediumType, width, height, highQuality) result.success(
}, { v -> getThumbnail(mediumId!!, mediumType, width, height, highQuality)
result.success(v) )
}) }
} }
"getAlbumThumbnail" -> { "getAlbumThumbnail" -> {
val albumId = call.argument<String>("albumId") val albumId = call.argument<String>("albumId")
@ -138,34 +146,33 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val width = call.argument<Int>("width") val width = call.argument<Int>("width")
val height = call.argument<Int>("height") val height = call.argument<Int>("height")
val highQuality = call.argument<Boolean>("highQuality") val highQuality = call.argument<Boolean>("highQuality")
BackgroundAsyncTask({ executor.submit {
getAlbumThumbnail(albumId!!, mediumType, width, height, highQuality) result.success(
}, { v -> getAlbumThumbnail(albumId!!, mediumType, width, height, highQuality)
result.success(v) )
}) }
} }
"getFile" -> { "getFile" -> {
val mediumId = call.argument<String>("mediumId") val mediumId = call.argument<String>("mediumId")
val mediumType = call.argument<String>("mediumType") val mediumType = call.argument<String>("mediumType")
val mimeType = call.argument<String>("mimeType") val mimeType = call.argument<String>("mimeType")
BackgroundAsyncTask({ executor.submit {
getFile(mediumId!!, mediumType, mimeType) result.success(
}, { v -> getFile(mediumId!!, mediumType, mimeType)
result.success(v) )
}) }
} }
"cleanCache" -> { "cleanCache" -> {
cleanCache() executor.submit {
result.success(null) result.success(
cleanCache()
)
}
} }
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}
private fun listAlbums(mediumType: String): List<Map<String, Any>> { private fun listAlbums(mediumType: String): List<Map<String, Any>> {
return when (mediumType) { return when (mediumType) {
imageType -> { imageType -> {
@ -181,7 +188,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun listImageAlbums(): List<Map<String, Any>> { private fun listImageAlbums(): List<Map<String, Any>> {
this.context?.run { this.context.run {
var total = 0 var total = 0
val albumHashMap = mutableMapOf<String, MutableMap<String, Any>>() val albumHashMap = mutableMapOf<String, MutableMap<String, Any>>()
@ -237,7 +244,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun listVideoAlbums(): List<Map<String, Any>> { private fun listVideoAlbums(): List<Map<String, Any>> {
this.context?.run { this.context.run {
var total = 0 var total = 0
val albumHashMap = mutableMapOf<String, MutableMap<String, Any>>() val albumHashMap = mutableMapOf<String, MutableMap<String, Any>>()
@ -294,7 +301,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val offset = skip ?: 0 val offset = skip ?: 0
val limit = take ?: (total - offset) val limit = take ?: (total - offset)
this.context?.run { this.context.run {
val isSelection = albumId != allAlbumId val isSelection = albumId != allAlbumId
val selection = if (isSelection) "${MediaStore.Images.Media.BUCKET_ID} = ?" else null val selection = if (isSelection) "${MediaStore.Images.Media.BUCKET_ID} = ?" else null
val selectionArgs = if (isSelection) arrayOf(albumId) else null val selectionArgs = if (isSelection) arrayOf(albumId) else null
@ -352,7 +359,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val offset = skip ?: 0 val offset = skip ?: 0
val limit = take ?: (total - offset) val limit = take ?: (total - offset)
this.context?.run { this.context.run {
val isSelection = albumId != allAlbumId val isSelection = albumId != allAlbumId
val selection = if (isSelection) "${MediaStore.Video.Media.BUCKET_ID} = ?" else null val selection = if (isSelection) "${MediaStore.Video.Media.BUCKET_ID} = ?" else null
val selectionArgs = if (isSelection) arrayOf(albumId) else null val selectionArgs = if (isSelection) arrayOf(albumId) else null
@ -422,7 +429,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
private fun getImageMedia(mediumId: String): Map<String, Any?>? { private fun getImageMedia(mediumId: String): Map<String, Any?>? {
var imageMetadata: Map<String, Any?>? = null var imageMetadata: Map<String, Any?>? = null
this.context?.run { this.context.run {
val imageCursor = this.contentResolver.query( val imageCursor = this.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageMetadataProjection, imageMetadataProjection,
@ -444,7 +451,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
private fun getVideoMedia(mediumId: String): Map<String, Any?>? { private fun getVideoMedia(mediumId: String): Map<String, Any?>? {
var videoMetadata: Map<String, Any?>? = null var videoMetadata: Map<String, Any?>? = null
this.context?.run { this.context.run {
val videoCursor = this.contentResolver.query( val videoCursor = this.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
videoMetadataProjection, videoMetadataProjection,
@ -481,7 +488,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
private fun getImageThumbnail(mediumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? { private fun getImageThumbnail(mediumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
var byteArray: ByteArray? = null var byteArray: ByteArray? = null
val bitmap: Bitmap? = this.context?.run { val bitmap: Bitmap? = this.context.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try { try {
val widthSize = width ?: if (highQuality == true) 512 else 96 val widthSize = width ?: if (highQuality == true) 512 else 96
@ -515,7 +522,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
private fun getVideoThumbnail(mediumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? { private fun getVideoThumbnail(mediumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
var byteArray: ByteArray? = null var byteArray: ByteArray? = null
val bitmap: Bitmap? = this.context?.run { val bitmap: Bitmap? = this.context.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try { try {
val widthSize = width ?: if (highQuality == true) 512 else 96 val widthSize = width ?: if (highQuality == true) 512 else 96
@ -563,7 +570,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun getImageAlbumThumbnail(albumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? { private fun getImageAlbumThumbnail(albumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
return this.context?.run { return this.context.run {
val isSelection = albumId != allAlbumId val isSelection = albumId != allAlbumId
val selection = if (isSelection) "${MediaStore.Images.Media.BUCKET_ID} = ?" else null val selection = if (isSelection) "${MediaStore.Images.Media.BUCKET_ID} = ?" else null
val selectionArgs = if (isSelection) arrayOf(albumId) else null val selectionArgs = if (isSelection) arrayOf(albumId) else null
@ -610,7 +617,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun getVideoAlbumThumbnail(albumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? { private fun getVideoAlbumThumbnail(albumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
return this.context?.run { return this.context.run {
val isSelection = albumId != allAlbumId val isSelection = albumId != allAlbumId
val selection = if (isSelection) "${MediaStore.Video.Media.BUCKET_ID} = ?" else null val selection = if (isSelection) "${MediaStore.Video.Media.BUCKET_ID} = ?" else null
val selectionArgs = if (isSelection) arrayOf(albumId) else null val selectionArgs = if (isSelection) arrayOf(albumId) else null
@ -671,7 +678,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun getImageFile(mediumId: String, mimeType: String? = null): String? { private fun getImageFile(mediumId: String, mimeType: String? = null): String? {
this.context?.run { this.context.run {
mimeType?.let { mimeType?.let {
val type = this.contentResolver.getType( val type = this.contentResolver.getType(
ContentUris.withAppendedId( ContentUris.withAppendedId(
@ -706,7 +713,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
private fun getVideoFile(mediumId: String): String? { private fun getVideoFile(mediumId: String): String? {
var path: String? = null var path: String? = null
this.context?.run { this.context.run {
val videoCursor = this.contentResolver.query( val videoCursor = this.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
arrayOf(MediaStore.Video.Media.DATA), arrayOf(MediaStore.Video.Media.DATA),
@ -727,7 +734,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun cacheImage(mediumId: String, mimeType: String): String? { private fun cacheImage(mediumId: String, mimeType: String): String? {
val bitmap: Bitmap? = this.context?.run { val bitmap: Bitmap? = this.context.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try { try {
ImageDecoder.decodeBitmap(ImageDecoder.createSource( ImageDecoder.decodeBitmap(ImageDecoder.createSource(
@ -762,7 +769,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} else if (mimeType == "image/webp") { } else if (mimeType == "image/webp") {
val path = File(getCachePath(), "$mediumId.webp") val path = File(getCachePath(), "$mediumId.webp")
val out = FileOutputStream(path) val out = FileOutputStream(path)
compressFormat = Bitmap.CompressFormat.WEBP compressFormat = Bitmap.CompressFormat.WEBP_LOSSLESS
it.compress(compressFormat, 100, out) it.compress(compressFormat, 100, out)
return path.absolutePath return path.absolutePath
} }
@ -870,7 +877,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun getCachePath(): File? { private fun getCachePath(): File? {
return this.context?.run { return this.context.run {
val cachePath = File(this.cacheDir, "photo_gallery") val cachePath = File(this.cacheDir, "photo_gallery")
if (!cachePath.exists()) { if (!cachePath.exists()) {
cachePath.mkdirs() cachePath.mkdirs()
@ -884,19 +891,3 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
cachePath?.deleteRecursively() cachePath?.deleteRecursively()
} }
} }
class BackgroundAsyncTask<T>(val handler: () -> T, val post: (result: T) -> Unit) : AsyncTask<Void, Void, T>() {
init {
execute()
}
override fun doInBackground(vararg params: Void?): T {
return handler()
}
override fun onPostExecute(result: T) {
super.onPostExecute(result)
post(result)
return
}
}