From 3dbbf172e6f890483e9018397770b1aa55e55f16 Mon Sep 17 00:00:00 2001 From: Aayushi Date: Fri, 1 Mar 2024 17:28:53 +0530 Subject: [PATCH] Fixed FileChooser'S Security Exception. --- app/src/main/AndroidManifest.xml | 2 + .../org/merakilearn/FileChooserActivity.kt | 92 +++++++++++++++++-- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7a9554bbe..e03eb456d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,8 @@ + + { - compareToCommon(uri) + compareToCommon(uri) } scheme.compareTo("http") == 0 -> { compareToCommon(uri) @@ -96,7 +105,51 @@ class FileChooserActivity : AppCompatActivity() { } } } + } + + private fun checkAndRequestPermissions() { + val permissionsToRequest = mutableListOf() + if (ContextCompat.checkSelfPermission( + this, + Manifest.permission.READ_CONTACTS + ) != PackageManager.PERMISSION_GRANTED + ) { + permissionsToRequest.add(Manifest.permission.READ_CONTACTS) + } + if (ContextCompat.checkSelfPermission( + this, + Manifest.permission.WRITE_CONTACTS + ) != PackageManager.PERMISSION_GRANTED + ) { + permissionsToRequest.add(Manifest.permission.WRITE_CONTACTS) + } + + if (permissionsToRequest.isNotEmpty()) { + ActivityCompat.requestPermissions( + this, + permissionsToRequest.toTypedArray(), + PERMISSION_REQUEST_CODE + ) + } + } + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) { + if (requestCode == PERMISSION_REQUEST_CODE) { + if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { + recreate() + } else { + Toast.makeText( + this, + "Permission denied. Some functionality may not work properly.", + Toast.LENGTH_SHORT + ).show() + } + } + super.onRequestPermissionsResult(requestCode, permissions, grantResults) } private fun compareToCommon(uri : Uri?) { @@ -108,6 +161,7 @@ class FileChooserActivity : AppCompatActivity() { finish() } + @SuppressLint("Recycle") private fun fileFromContentUri(context: Context, contentUri: Uri): File { val fileExtension = getFileExtension(context, contentUri) @@ -164,15 +218,37 @@ class FileChooserActivity : AppCompatActivity() { } private fun getNameFromContentUri(context: Context, contentUri: Uri): String { - val returnCursor: Cursor = - context.contentResolver.query(contentUri, null, null, null, null)!! - val nameColumnIndex: Int = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) - returnCursor.moveToFirst() - val name = returnCursor.getString(nameColumnIndex) - returnCursor.close() - return name + return try { + if (ContextCompat.checkSelfPermission( + context, + Manifest.permission.READ_CONTACTS + ) == PackageManager.PERMISSION_GRANTED + ) { + val returnCursor: Cursor? = + context.contentResolver.query(contentUri, null, null, null, null) + + returnCursor?.use { + if (it.moveToFirst()) { + val nameColumnIndex: Int = it.getColumnIndex(OpenableColumns.DISPLAY_NAME) + if (nameColumnIndex != -1) { + return it.getString(nameColumnIndex) + } else { + return "" + } + } else { + "" + } + } ?: "" + } else { + "" + } + } catch (e: RuntimeException) { + e.printStackTrace() + "" + } } + private fun getFileExtension(context: Context, contentUri: Uri): String? { val fileType: String? = context.contentResolver.getType(contentUri) return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType)