Guard all ContentResolver accesses for SecurityException
This commit is contained in:
1 parent
fde4f74c98
commit
649fac327e
2 files changed
+24
-8
No files matched your search
@@ -8,6 +8,7 @@ import android.provider.DocumentsContract
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import androidx.core.net.toUri
|
||||
import eu.depau.etchdroid.plugins.telemetry.Telemetry
|
||||
|
||||
/**
|
||||
* Get a file path from a Uri. This will get the the path for Storage Access
|
||||
@@ -111,11 +112,15 @@ fun Uri.getDataColumn(
|
||||
val column = "_data"
|
||||
val projection = arrayOf(column)
|
||||
|
||||
context.contentResolver.query(this, projection, selection, selectionArgs, null)?.use {
|
||||
if (it.moveToFirst()) {
|
||||
val columnIndex = it.getColumnIndexOrThrow(column)
|
||||
return it.getString(columnIndex)
|
||||
try {
|
||||
context.contentResolver.query(this, projection, selection, selectionArgs, null)?.use {
|
||||
if (it.moveToFirst()) {
|
||||
val columnIndex = it.getColumnIndexOrThrow(column)
|
||||
return it.getString(columnIndex)
|
||||
}
|
||||
}
|
||||
} catch (e: SecurityException) {
|
||||
Telemetry.captureException("getDataColumn: Failed to query content resolver", e)
|
||||
}
|
||||
|
||||
return null
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.database.Cursor
|
||||
import android.database.SQLException
|
||||
import android.net.Uri
|
||||
import android.provider.OpenableColumns
|
||||
import eu.depau.etchdroid.plugins.telemetry.Telemetry
|
||||
import java.io.File
|
||||
|
||||
// https://github.com/android-rcs/rcsjta/blob/master/RI/src/com/gsma/rcs/ri/utils/FileUtils.java#L214
|
||||
@@ -18,18 +19,28 @@ fun Uri.getFileSize(context: Context): Long {
|
||||
}
|
||||
|
||||
ContentResolver.SCHEME_CONTENT -> {
|
||||
val cursor: Cursor? = context.contentResolver.query(this, null, null, null, null)
|
||||
var cursor: Cursor? = null
|
||||
|
||||
try {
|
||||
cursor = context.contentResolver.query(this, null, null, null, null)
|
||||
} catch (e: Exception) {
|
||||
Telemetry.captureException("getFileSize: Failed to query content resolver", e)
|
||||
}
|
||||
|
||||
cursor.use {
|
||||
if (it == null) {
|
||||
throw SQLException("Failed to query file $this")
|
||||
}
|
||||
return if (it.moveToFirst()) {
|
||||
java.lang.Long.valueOf(it.getString(it
|
||||
.getColumnIndexOrThrow(OpenableColumns.SIZE)))
|
||||
java.lang.Long.valueOf(
|
||||
it.getString(
|
||||
it.getColumnIndexOrThrow(OpenableColumns.SIZE)
|
||||
)
|
||||
)
|
||||
} else {
|
||||
throw IllegalArgumentException(
|
||||
"Error in retrieving this size form the URI")
|
||||
"Error in retrieving this size form the URI"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user