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 FileChooser'S Security Exception. #323

Open
wants to merge 1 commit into
base: development_inapp
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
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.MICROPHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

<application
android:name=".MerakiApp"
Expand Down
92 changes: 84 additions & 8 deletions app/src/main/java/org/merakilearn/FileChooserActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.merakilearn

import android.Manifest
import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.os.Bundle
Expand All @@ -13,6 +16,8 @@ import android.webkit.MimeTypeMap
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import org.koin.android.ext.android.inject
import org.merakilearn.core.navigator.MerakiNavigator
import org.merakilearn.ui.ScratchActivity
Expand All @@ -23,9 +28,13 @@ import java.io.OutputStreamWriter


class FileChooserActivity : AppCompatActivity() {

private val PERMISSION_REQUEST_CODE = 1001

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_file_chooser)
checkAndRequestPermissions()

val progressBar: ProgressBar = findViewById(R.id.progressBar3)
progressBar.visibility = View.VISIBLE
Expand Down Expand Up @@ -85,7 +94,7 @@ class FileChooserActivity : AppCompatActivity() {
}
}
scheme.compareTo("https") == 0 -> {
compareToCommon(uri)
compareToCommon(uri)
}
scheme.compareTo("http") == 0 -> {
compareToCommon(uri)
Expand All @@ -96,7 +105,51 @@ class FileChooserActivity : AppCompatActivity() {
}
}
}
}

private fun checkAndRequestPermissions() {
val permissionsToRequest = mutableListOf<String>()
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<out String>,
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?) {
Expand All @@ -108,6 +161,7 @@ class FileChooserActivity : AppCompatActivity() {
finish()
}

@SuppressLint("Recycle")
private fun fileFromContentUri(context: Context, contentUri: Uri): File {

val fileExtension = getFileExtension(context, contentUri)
Expand Down Expand Up @@ -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)
Expand Down