Skip to content

Commit 3f8be30

Browse files
committed
clean code
1 parent 9474302 commit 3f8be30

File tree

1 file changed

+13
-20
lines changed
  • android/src/main/kotlin/com/mr/flutter/plugin/filepicker

1 file changed

+13
-20
lines changed

android/src/main/kotlin/com/mr/flutter/plugin/filepicker/FileUtils.kt

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.os.Bundle
1111
import android.os.Environment
1212
import android.os.Parcelable
1313
import android.provider.DocumentsContract
14-
import android.provider.MediaStore
1514
import android.provider.OpenableColumns
1615
import android.util.Log
1716
import android.webkit.MimeTypeMap
@@ -68,8 +67,7 @@ object FileUtils {
6867
}
6968

7069
data.data != null -> {
71-
var uri = data.data!!
72-
uri = processUri(activity, uri, compressionQuality)
70+
var uri = processUri(activity, data.data!!, compressionQuality)
7371

7472
if (type == "dir") {
7573
uri = DocumentsContract.buildDocumentUriUsingTree(
@@ -118,7 +116,7 @@ object FileUtils {
118116
return uri
119117
}
120118

121-
fun FilePickerDelegate.handleFileResult(files: List<FileInfo>) {
119+
private fun FilePickerDelegate.handleFileResult(files: List<FileInfo>) {
122120
if (files.isNotEmpty()) {
123121
finishWithSuccess(files)
124122
} else {
@@ -138,7 +136,7 @@ object FileUtils {
138136
intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
139137
} else {
140138
if (type == "image/*") {
141-
intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
139+
intent = Intent(Intent.ACTION_PICK)
142140
val uri = (Environment.getExternalStorageDirectory().path + File.separator).toUri()
143141
intent.setDataAndType(uri, type)
144142
intent.type = this.type
@@ -213,7 +211,7 @@ object FileUtils {
213211
return mimeType.substringAfter("/")
214212
}
215213

216-
fun getMimeTypeForBytes(fileName: String?, bytes: ByteArray?): String {
214+
private fun getMimeTypeForBytes(fileName: String?, bytes: ByteArray?): String {
217215
val tika = Tika()
218216

219217
if (fileName.isNullOrEmpty()) {
@@ -263,15 +261,15 @@ object FileUtils {
263261
}
264262
}
265263

266-
fun processUri(activity: Activity, uri: Uri, compressionQuality: Int): Uri {
264+
private fun processUri(activity: Activity, uri: Uri, compressionQuality: Int): Uri {
267265
return if (compressionQuality > 0 && isImage(activity.applicationContext, uri)) {
268266
compressImage(uri, compressionQuality, activity.applicationContext)
269267
} else {
270268
uri
271269
}
272270
}
273271

274-
fun addFile(
272+
private fun addFile(
275273
activity: Activity,
276274
uri: Uri,
277275
loadDataToMemory: Boolean,
@@ -283,7 +281,7 @@ object FileUtils {
283281
}
284282

285283
@Suppress("deprecation")
286-
fun getSelectedItems(bundle: Bundle): ArrayList<Parcelable>? {
284+
private fun getSelectedItems(bundle: Bundle): ArrayList<Parcelable>? {
287285
if (Build.VERSION.SDK_INT >= 33) {
288286
return bundle.getParcelableArrayList("selectedItems", Parcelable::class.java)
289287
}
@@ -349,11 +347,7 @@ object FileUtils {
349347

350348
@JvmStatic
351349
fun isImage(context: Context, uri: Uri): Boolean {
352-
val extension = getFileExtension(context, uri)
353-
354-
if (extension == null) {
355-
return false
356-
}
350+
val extension = getFileExtension(context, uri) ?: return false
357351

358352
return extension.contentEquals("jpg") || extension.contentEquals("jpeg")
359353
|| extension.contentEquals("png") || extension.contentEquals("webp")
@@ -410,7 +404,7 @@ object FileUtils {
410404
* @param uri The Uri to check.
411405
* @return Whether the Uri authority is DownloadsProvider.
412406
*/
413-
fun isDownloadsDocument(uri: Uri): Boolean {
407+
private fun isDownloadsDocument(uri: Uri): Boolean {
414408
return uri.authority == "com.android.providers.downloads.documents"
415409
}
416410

@@ -429,7 +423,7 @@ object FileUtils {
429423
return true
430424
}
431425

432-
fun loadData(file: File, fileInfo: FileInfo.Builder) {
426+
private fun loadData(file: File, fileInfo: FileInfo.Builder) {
433427
try {
434428
val size = file.length().toInt()
435429
val bytes = ByteArray(size)
@@ -506,7 +500,7 @@ object FileUtils {
506500
return fileInfo.build()
507501
}
508502

509-
fun getPathFromTreeUri(uri: Uri): String? {
503+
private fun getPathFromTreeUri(uri: Uri): String {
510504
val docId = DocumentsContract.getTreeDocumentId(uri)
511505
val parts = docId.split(":")
512506
return "${Environment.getExternalStorageDirectory()}/${parts.last()}"
@@ -525,7 +519,7 @@ object FileUtils {
525519
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path
526520
if (docId == "downloads") {
527521
return extPath
528-
} else if (docId.matches("^ms[df]\\:.*".toRegex())) {
522+
} else if (docId.matches("^ms[df]:.*".toRegex())) {
529523
val fileName = getFileName(treeUri, con)
530524
return "$extPath/$fileName"
531525
} else if (docId.startsWith("raw:")) {
@@ -536,7 +530,6 @@ object FileUtils {
536530
}
537531
}
538532
var volumePath = getPathFromTreeUri(treeUri)
539-
?: return File.separator
540533

541534
if (volumePath.endsWith(File.separator)) {
542535
volumePath = volumePath.substring(0, volumePath.length - 1)
@@ -547,7 +540,7 @@ object FileUtils {
547540
if (documentPath.endsWith(File.separator)) {
548541
documentPath = documentPath.substring(0, documentPath.length - 1)
549542
}
550-
return if (!documentPath.isEmpty()) {
543+
return if (documentPath.isNotEmpty()) {
551544
if(volumePath.endsWith(documentPath)){
552545
volumePath
553546
}else {

0 commit comments

Comments
 (0)