Fix race condition with fileExists(); this should also be faster
This commit is contained in:
parent
0fce663975
commit
f37ddb80a8
1 changed files with 16 additions and 18 deletions
|
@ -7,7 +7,6 @@ import android.net.Uri
|
||||||
import android.provider.OpenableColumns
|
import android.provider.OpenableColumns
|
||||||
import android.view.Window
|
import android.view.Window
|
||||||
import io.heckel.ntfy.data.Notification
|
import io.heckel.ntfy.data.Notification
|
||||||
import io.heckel.ntfy.data.PROGRESS_NONE
|
|
||||||
import io.heckel.ntfy.data.Subscription
|
import io.heckel.ntfy.data.Subscription
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
import java.text.DateFormat
|
import java.text.DateFormat
|
||||||
|
@ -112,12 +111,9 @@ fun formatTitle(notification: Notification): String {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks in the most horrible way if a content URI exists; I couldn't find a better way
|
// Checks in the most horrible way if a content URI exists; I couldn't find a better way
|
||||||
fun fileExists(context: Context, uri: String?): Boolean {
|
fun fileExists(context: Context, contentUri: String?): Boolean {
|
||||||
if (uri == null) return false
|
|
||||||
val resolver = context.applicationContext.contentResolver
|
|
||||||
return try {
|
return try {
|
||||||
val fileIS = resolver.openInputStream(Uri.parse(uri))
|
queryFilenameInternal(context, contentUri) // Throws if the file does not exist
|
||||||
fileIS?.close()
|
|
||||||
true
|
true
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
false
|
false
|
||||||
|
@ -126,20 +122,22 @@ fun fileExists(context: Context, uri: String?): Boolean {
|
||||||
|
|
||||||
// Queries the filename of a content URI
|
// Queries the filename of a content URI
|
||||||
fun queryFilename(context: Context, contentUri: String?, fallbackName: String): String {
|
fun queryFilename(context: Context, contentUri: String?, fallbackName: String): String {
|
||||||
if (contentUri == null) {
|
return try {
|
||||||
return fallbackName
|
queryFilenameInternal(context, contentUri)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
fallbackName
|
||||||
}
|
}
|
||||||
try {
|
}
|
||||||
|
|
||||||
|
fun queryFilenameInternal(context: Context, contentUri: String?): String {
|
||||||
|
if (contentUri == null) throw Exception("URI is null")
|
||||||
val resolver = context.applicationContext.contentResolver
|
val resolver = context.applicationContext.contentResolver
|
||||||
val cursor = resolver.query(Uri.parse(contentUri), null, null, null, null) ?: return fallbackName
|
val cursor = resolver.query(Uri.parse(contentUri), null, null, null, null) ?: throw Exception("Query returned null")
|
||||||
return cursor.use { c ->
|
return cursor.use { c ->
|
||||||
val nameIndex = c.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)
|
val nameIndex = c.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)
|
||||||
c.moveToFirst()
|
c.moveToFirst()
|
||||||
c.getString(nameIndex)
|
c.getString(nameIndex)
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {
|
|
||||||
return fallbackName
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status bar color fading to match action bar, see https://stackoverflow.com/q/51150077/1440785
|
// Status bar color fading to match action bar, see https://stackoverflow.com/q/51150077/1440785
|
||||||
|
|
Loading…
Add table
Reference in a new issue