mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-29 21:33:07 -08:00
Fix check file ANR #494
This commit is contained in:
1 parent
0baa7bcbf1
commit
01960e74c1
6 files changed
+75
-123
No files matched your search
@@ -278,9 +278,8 @@ class FileDatabaseSelectActivity : StylishActivity(),
|
||||
// Show only uri accessible
|
||||
historyList.filter {
|
||||
if (hideBrokenLocations) {
|
||||
UriUtil.parse(it.databaseUri)?.let { historyUri ->
|
||||
UriUtil.isUriAccessible(contentResolver, historyUri)
|
||||
} ?: false
|
||||
FileDatabaseInfo(this@FileDatabaseSelectActivity,
|
||||
it.databaseUri).exists
|
||||
} else
|
||||
true
|
||||
})
|
||||
|
||||
@@ -313,7 +313,13 @@ open class PasswordActivity : StylishActivity() {
|
||||
}
|
||||
|
||||
private fun initUriFromIntent() {
|
||||
mForceReadOnly = !UriUtil.isUriWritable(mDatabaseFileUri)
|
||||
/*
|
||||
// "canXrite" doesn't work with Google Drive, don't really know why?
|
||||
mForceReadOnly = mDatabaseFileUri?.let {
|
||||
!FileDatabaseInfo(this, it).canWrite
|
||||
} ?: false
|
||||
*/
|
||||
mForceReadOnly = false
|
||||
|
||||
// Post init uri with KeyFile if needed
|
||||
if (mRememberKeyFile && (mDatabaseKeyFileUri == null || mDatabaseKeyFileUri.toString().isEmpty())) {
|
||||
|
||||
@@ -84,24 +84,25 @@ class FileDatabaseHistoryAdapter(private val context: Context)
|
||||
// File path
|
||||
holder.filePath.text = UriUtil.decode(fileDatabaseInfo.fileUri?.toString())
|
||||
|
||||
if (fileDatabaseInfo.dataAccessible()) {
|
||||
if (fileDatabaseInfo.exists) {
|
||||
holder.fileInformation.clearColorFilter()
|
||||
} else {
|
||||
holder.fileInformation.setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY)
|
||||
}
|
||||
|
||||
// Modification
|
||||
if (fileDatabaseInfo.lastModificationAccessible()) {
|
||||
holder.fileModification.text = fileDatabaseInfo.getModificationString()
|
||||
fileDatabaseInfo.getModificationString()?.let {
|
||||
holder.fileModification.text = it
|
||||
holder.fileModification.visibility = View.VISIBLE
|
||||
} else {
|
||||
} ?: run {
|
||||
holder.fileModification.visibility = View.GONE
|
||||
}
|
||||
|
||||
// Size
|
||||
if (fileDatabaseInfo.sizeAccessible()) {
|
||||
holder.fileSize.text = fileDatabaseInfo.getSizeString()
|
||||
fileDatabaseInfo.getSizeString()?.let {
|
||||
holder.fileSize.text = it
|
||||
holder.fileSize.visibility = View.VISIBLE
|
||||
} else {
|
||||
} ?: run {
|
||||
holder.fileSize.visibility = View.GONE
|
||||
}
|
||||
|
||||
|
||||
@@ -21,25 +21,77 @@ package com.kunzisoft.keepass.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.text.format.Formatter
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import java.io.Serializable
|
||||
import java.text.DateFormat
|
||||
import java.util.*
|
||||
|
||||
class FileDatabaseInfo : FileInfo {
|
||||
class FileDatabaseInfo : Serializable {
|
||||
|
||||
constructor(context: Context, fileUri: Uri): super(context, fileUri)
|
||||
private var context: Context
|
||||
private var documentFile: DocumentFile? = null
|
||||
var fileUri: Uri?
|
||||
private set
|
||||
|
||||
constructor(context: Context, filePath: String): super(context, filePath)
|
||||
constructor(context: Context, fileUri: Uri) {
|
||||
this.context = context
|
||||
this.fileUri = fileUri
|
||||
init()
|
||||
}
|
||||
|
||||
constructor(context: Context, filePath: String) {
|
||||
this.context = context
|
||||
this.fileUri = UriUtil.parse(filePath)
|
||||
init()
|
||||
}
|
||||
|
||||
fun init() {
|
||||
documentFile = UriUtil.getFileData(context, fileUri)
|
||||
}
|
||||
|
||||
var exists: Boolean = false
|
||||
get() {
|
||||
return documentFile?.exists() ?: field
|
||||
}
|
||||
private set
|
||||
|
||||
var canRead: Boolean = false
|
||||
get() {
|
||||
return documentFile?.canRead() ?: field
|
||||
}
|
||||
private set
|
||||
|
||||
var canWrite: Boolean = false
|
||||
get() {
|
||||
return documentFile?.canWrite() ?: field
|
||||
}
|
||||
private set
|
||||
|
||||
fun getModificationString(): String? {
|
||||
return documentFile?.lastModified()?.let {
|
||||
DateFormat.getDateTimeInstance()
|
||||
.format(Date(it))
|
||||
}
|
||||
}
|
||||
|
||||
fun getSizeString(): String? {
|
||||
return documentFile?.let {
|
||||
Formatter.formatFileSize(context, it.length())
|
||||
}
|
||||
}
|
||||
|
||||
fun retrieveDatabaseAlias(alias: String): String {
|
||||
return when {
|
||||
alias.isNotEmpty() -> alias
|
||||
PreferencesUtil.isFullFilePathEnable(context) -> filePath ?: ""
|
||||
else -> fileName ?: ""
|
||||
PreferencesUtil.isFullFilePathEnable(context) -> fileUri?.path ?: ""
|
||||
else -> if (exists) documentFile?.name ?: "" else fileUri?.path ?: ""
|
||||
}
|
||||
}
|
||||
|
||||
fun retrieveDatabaseTitle(titleCallback: (String)->Unit) {
|
||||
|
||||
fileUri?.let { fileUri ->
|
||||
FileDatabaseHistoryAction.getInstance(context.applicationContext)
|
||||
.getFileDatabaseHistory(fileUri) { fileDatabaseHistoryEntity ->
|
||||
@@ -48,5 +100,4 @@ class FileDatabaseInfo : FileInfo {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.text.format.Formatter
|
||||
import java.io.Serializable
|
||||
import java.text.DateFormat
|
||||
import java.util.*
|
||||
|
||||
open class FileInfo : Serializable {
|
||||
|
||||
var context: Context
|
||||
var fileUri: Uri?
|
||||
var filePath: String? = null
|
||||
var fileName: String? = ""
|
||||
var lastModification = Date(0L)
|
||||
var size: Long = 0L
|
||||
|
||||
constructor(context: Context, fileUri: Uri) {
|
||||
this.context = context
|
||||
this.fileUri = fileUri
|
||||
init()
|
||||
}
|
||||
|
||||
constructor(context: Context, filePath: String) {
|
||||
this.context = context
|
||||
this.fileUri = UriUtil.parse(filePath)
|
||||
init()
|
||||
}
|
||||
|
||||
fun init() {
|
||||
this.filePath = fileUri?.path
|
||||
|
||||
UriUtil.getFileData(context, fileUri)?.let { file ->
|
||||
size = file.length()
|
||||
fileName = file.name
|
||||
lastModification = Date(file.lastModified())
|
||||
}
|
||||
|
||||
if (fileName == null || fileName!!.isEmpty()) {
|
||||
fileName = filePath
|
||||
}
|
||||
}
|
||||
|
||||
fun lastModificationAccessible(): Boolean {
|
||||
return lastModification.after(Date(0L))
|
||||
}
|
||||
|
||||
fun sizeAccessible(): Boolean {
|
||||
return size != 0L
|
||||
}
|
||||
|
||||
fun dataAccessible(): Boolean {
|
||||
return UriUtil.isUriAccessible(context.contentResolver, fileUri)
|
||||
}
|
||||
|
||||
fun getModificationString(): String {
|
||||
return DateFormat.getDateTimeInstance()
|
||||
.format(lastModification)
|
||||
}
|
||||
|
||||
fun getSizeString(): String {
|
||||
return Formatter.formatFileSize(context, size)
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.kunzisoft.keepass.R
|
||||
@@ -37,26 +36,6 @@ import java.util.*
|
||||
|
||||
object UriUtil {
|
||||
|
||||
fun isUriAccessible(contentResolver: ContentResolver, fileUri: Uri?): Boolean {
|
||||
if (fileUri == null)
|
||||
return false
|
||||
return try {
|
||||
//https://developer.android.com/reference/android/content/res/AssetFileDescriptor
|
||||
contentResolver.openInputStream(fileUri)?.close()
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.e(UriUtil.javaClass.name, "Unable to access uri $fileUri : ${e.message}")
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun isUriWritable(fileUri: Uri?): Boolean {
|
||||
if (fileUri == null)
|
||||
return false
|
||||
// TODO Uri writeable detection
|
||||
return true
|
||||
}
|
||||
|
||||
fun getFileData(context: Context, fileUri: Uri?): DocumentFile? {
|
||||
if (fileUri == null)
|
||||
return null
|
||||
|
||||
Reference in new issue
Block a user