Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed errors and crashes #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import io.flutter.plugin.common.PluginRegistry
import android.os.Build

class ImageDownloaderPermissionListener(private val activity: Activity) :
PluginRegistry.RequestPermissionsResultListener {

private val storagePermissions = mutableListOf<String>().apply {
val readStoragePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Manifest.permission.READ_MEDIA_IMAGES
} else {
Manifest.permission.READ_EXTERNAL_STORAGE
}

this.add(readStoragePermission)

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
this.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
}

private val permissionRequestId: Int = 2578166

var callback: Callback? = null
Expand All @@ -18,7 +33,7 @@ class ImageDownloaderPermissionListener(private val activity: Activity) :
requestCode: Int, permissions: Array<String>, grantResults: IntArray
): Boolean {

if (!isPermissionGranted(permissions)) {
if (!isStoragePermissionGranted()) {
// when select deny.
callback?.denied()
return false
Expand All @@ -37,21 +52,19 @@ class ImageDownloaderPermissionListener(private val activity: Activity) :
}

fun alreadyGranted(): Boolean {
val permissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)

if (!isPermissionGranted(permissions)) {
if (!isStoragePermissionGranted()) {
// Request authorization. User is not yet authorized.
ActivityCompat.requestPermissions(activity, permissions, permissionRequestId)
ActivityCompat.requestPermissions(activity, storagePermissions.toTypedArray(), permissionRequestId)
return false
}
// User already has authorization. Or below Android6.0
return true
}

private fun isPermissionGranted(permissions: Array<String>) = permissions.none {
ContextCompat.checkSelfPermission(
activity, it
) != PackageManager.PERMISSION_GRANTED
fun isStoragePermissionGranted(): Boolean {
return storagePermissions.all {
activity.checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED
}
}

interface Callback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import java.io.FileInputStream
import java.net.URLConnection
import java.text.SimpleDateFormat
import java.util.*
import kotlin.random.Random

class ImageDownloaderPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
companion object {
Expand Down Expand Up @@ -289,9 +290,12 @@ class ImageDownloaderPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
val inPublicDir = call.argument<Boolean>("inPublicDir") ?: true
val directoryType = call.argument<String>("directory") ?: "DIRECTORY_DOWNLOADS"
val subDirectory = call.argument<String>("subDirectory")
val tempSubDirectory = subDirectory ?: SimpleDateFormat(
"yyyy-MM-dd.HH.mm.sss", Locale.getDefault()
).format(Date())

// add a random number to avoid conflicts, because when i downloaded multiple files so fast, the last file overwrites the previous ones
val tempSubDirectory = subDirectory ?: "${SimpleDateFormat(
"yyyy-MM-dd.HH.mm.sss",
Locale.getDefault()
).format(Date())}${Random.nextInt(1000)}"

val directory = convertToDirectory(directoryType)

Expand Down Expand Up @@ -334,6 +338,10 @@ class ImageDownloaderPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
channel.invokeMethod("onProgressUpdate", args)
}
}
// fix error
// 'when' expression must be exhaustive, add necessary 'is Pending', 'is Successful' branches or 'else' branch instead
is Downloader.DownloadStatus.Successful -> Log.d(LOGGER_TAG, "Successful")
is Downloader.DownloadStatus.Pending -> Log.d(LOGGER_TAG, "Pending")
else -> throw AssertionError()
}

Expand All @@ -354,6 +362,15 @@ class ImageDownloaderPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
null
)
} else {
if (!file.canRead()) {
// it appears that the file is not readable, might the device is busy ( this happens when I try downloaded multiple files so fast)
result.error(
"read_failed",
"Couldn't read ${file.absolutePath ?: tempSubDirectory} ",
null
)
return@execute // return early
}
val stream = BufferedInputStream(FileInputStream(file))
val mimeType =
outputMimeType ?: URLConnection.guessContentTypeFromStream(stream)
Expand All @@ -377,6 +394,12 @@ class ImageDownloaderPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
.getMimeTypeFromExtension(newFile.extension) ?: ""
val imageId = saveToDatabase(newFile, mimeType ?: newMimeType, inPublicDir)

if (imageId == "") {
// in case the file was still downloaded but not saved to the gallery content provider, i don't know how to handle this case, someone might help :))

// result.error("save_to_database_failed", "Couldn't save the file to the database.", null)
// return@execute
}
result.success(imageId)
}
})
Expand Down Expand Up @@ -420,8 +443,12 @@ class ImageDownloaderPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
null
).use {
checkNotNull(it) { "${file.absolutePath} is not found." }
it.moveToFirst()
it.getString(it.getColumnIndex(MediaStore.Images.Media._ID))
if (it.moveToFirst()) {
it.getString(it.getColumnIndex(MediaStore.Images.Media._ID))
} else {
// it appears that the cursor is empty
""
}
}
} else {
val db = TemporaryDatabase(context)
Expand Down