mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-04-04 21:37:37 +03:00
fix: PackageInfo compat
This commit is contained in:
parent
c3e4504a1a
commit
59794557b3
17 changed files with 95 additions and 92 deletions
|
@ -31,6 +31,7 @@ import com.kunzisoft.keepass.BuildConfig
|
|||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.stylish.StylishActivity
|
||||
import com.kunzisoft.keepass.utils.UriUtil.isContributingUser
|
||||
import com.kunzisoft.keepass.utils.getPackageInfoCompat
|
||||
import org.joda.time.DateTime
|
||||
|
||||
class AboutActivity : StylishActivity() {
|
||||
|
@ -55,7 +56,7 @@ class AboutActivity : StylishActivity() {
|
|||
var version: String
|
||||
var build: String
|
||||
try {
|
||||
version = packageManager.getPackageInfo(packageName, 0).versionName
|
||||
version = packageManager.getPackageInfoCompat(packageName).versionName
|
||||
build = BuildConfig.BUILD_VERSION
|
||||
} catch (e: NameNotFoundException) {
|
||||
Log.w(javaClass.simpleName, "Unable to get the app or the build version", e)
|
||||
|
|
|
@ -69,7 +69,7 @@ import com.kunzisoft.keepass.utils.DexUtil
|
|||
import com.kunzisoft.keepass.utils.MagikeyboardUtil
|
||||
import com.kunzisoft.keepass.utils.MenuUtil
|
||||
import com.kunzisoft.keepass.utils.getParcelableCompat
|
||||
import com.kunzisoft.keepass.utils.UriHelper.parseUri
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import com.kunzisoft.keepass.utils.UriUtil.isContributingUser
|
||||
import com.kunzisoft.keepass.utils.UriUtil.openUrl
|
||||
import com.kunzisoft.keepass.view.asError
|
||||
|
|
|
@ -9,7 +9,7 @@ import com.kunzisoft.keepass.database.MainCredential
|
|||
import com.kunzisoft.keepass.database.DatabaseTaskProvider
|
||||
import com.kunzisoft.keepass.model.CipherEncryptDatabase
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.getBinaryDir
|
||||
import com.kunzisoft.keepass.viewmodels.DatabaseViewModel
|
||||
|
||||
abstract class DatabaseActivity: StylishActivity(), DatabaseRetrieval {
|
||||
|
|
|
@ -27,8 +27,8 @@ import com.kunzisoft.keepass.model.DatabaseFile
|
|||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.SingletonHolderParameter
|
||||
import com.kunzisoft.keepass.utils.UriHelper.decodeUri
|
||||
import com.kunzisoft.keepass.utils.UriHelper.parseUri
|
||||
import com.kunzisoft.keepass.utils.decodeUri
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import com.kunzisoft.keepass.viewmodels.FileDatabaseInfo
|
||||
|
||||
class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
|
|
|
@ -25,7 +25,7 @@ import android.os.Parcelable
|
|||
import com.kunzisoft.keepass.database.element.MasterCredential
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.utils.readParcelableCompat
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.readEnum
|
||||
import com.kunzisoft.keepass.utils.writeEnum
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ import android.net.Uri
|
|||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.MainCredential
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.getBinaryDir
|
||||
|
||||
class CreateDatabaseRunnable(
|
||||
context: Context,
|
||||
|
|
|
@ -29,8 +29,8 @@ import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
|
|||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.getUriInputStream
|
||||
|
||||
class LoadDatabaseRunnable(
|
||||
private val context: Context,
|
||||
|
|
|
@ -28,7 +28,7 @@ import com.kunzisoft.keepass.database.exception.DatabaseException
|
|||
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.getUriInputStream
|
||||
|
||||
class MergeDatabaseRunnable(
|
||||
context: Context,
|
||||
|
|
|
@ -26,8 +26,8 @@ import com.kunzisoft.keepass.database.exception.DatabaseException
|
|||
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.getUriInputStream
|
||||
|
||||
class ReloadDatabaseRunnable(
|
||||
private val context: Context,
|
||||
|
|
|
@ -26,7 +26,7 @@ import com.kunzisoft.keepass.database.MainCredential
|
|||
import com.kunzisoft.keepass.database.exception.DatabaseException
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriOutputStream
|
||||
import com.kunzisoft.keepass.utils.getUriOutputStream
|
||||
import java.io.File
|
||||
|
||||
open class SaveDatabaseRunnable(
|
||||
|
|
|
@ -8,8 +8,8 @@ import android.util.Log
|
|||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryCache
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getUriOutputStream
|
||||
import com.kunzisoft.keepass.utils.getUriInputStream
|
||||
import com.kunzisoft.keepass.utils.getUriOutputStream
|
||||
import com.kunzisoft.keepass.utils.readAllBytes
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.ByteArrayInputStream
|
||||
|
|
|
@ -36,7 +36,6 @@ import com.kunzisoft.keepass.services.ClipboardEntryNotificationService
|
|||
import com.kunzisoft.keepass.services.KeyboardEntryNotificationService
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
||||
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir
|
||||
import com.kunzisoft.keepass.utils.UriUtil.releaseAllUnnecessaryPermissionUris
|
||||
|
||||
const val DATABASE_START_TASK_ACTION = "com.kunzisoft.keepass.DATABASE_START_TASK_ACTION"
|
||||
|
|
|
@ -32,8 +32,6 @@ import com.kunzisoft.keepass.BuildConfig
|
|||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.education.Education
|
||||
import com.kunzisoft.keepass.utils.UriHelper.withContentScheme
|
||||
import com.kunzisoft.keepass.utils.UriHelper.withFileScheme
|
||||
import java.io.File
|
||||
|
||||
|
||||
|
@ -216,7 +214,7 @@ object UriUtil {
|
|||
|
||||
fun Context.isExternalAppInstalled(packageName: String, showError: Boolean = true): Boolean {
|
||||
try {
|
||||
this.applicationContext.packageManager.getPackageInfo(
|
||||
this.applicationContext.packageManager.getPackageInfoCompat(
|
||||
packageName,
|
||||
PackageManager.GET_ACTIVITIES
|
||||
)
|
||||
|
|
|
@ -9,7 +9,7 @@ import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
|||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.UriHelper.parseUri
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
|
||||
class DatabaseFileViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import com.kunzisoft.keepass.hardware.HardwareKey
|
|||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.UriHelper.parseUri
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import com.kunzisoft.keepass.utils.UriUtil.releaseUriPermission
|
||||
|
||||
class DatabaseFilesViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
|
|
@ -23,7 +23,7 @@ import android.content.Context
|
|||
import android.net.Uri
|
||||
import android.text.format.Formatter
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.kunzisoft.keepass.utils.UriHelper.parseUri
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import com.kunzisoft.keepass.utils.UriUtil.getDocumentFile
|
||||
import com.kunzisoft.keepass.utils.UriUtil.takeUriPermission
|
||||
import java.io.Serializable
|
||||
|
|
|
@ -22,80 +22,85 @@ package com.kunzisoft.keepass.utils
|
|||
import android.annotation.SuppressLint
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
|
||||
object UriHelper {
|
||||
|
||||
fun String.parseUri(): Uri? {
|
||||
return if (this.isNotEmpty()) Uri.parse(this) else null
|
||||
}
|
||||
|
||||
fun String.decodeUri(): String {
|
||||
return Uri.decode(this) ?: ""
|
||||
}
|
||||
|
||||
fun Context.getBinaryDir(): File {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
this.applicationContext.noBackupFilesDir
|
||||
} else {
|
||||
this.applicationContext.filesDir
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun ContentResolver.getUriInputStream(fileUri: Uri?): InputStream? {
|
||||
if (fileUri == null)
|
||||
return null
|
||||
return when {
|
||||
fileUri.withFileScheme() -> fileUri.path?.let { FileInputStream(it) }
|
||||
fileUri.withContentScheme() -> this.openInputStream(fileUri)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("Recycle")
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun ContentResolver.getUriOutputStream(fileUri: Uri?): OutputStream? {
|
||||
if (fileUri == null)
|
||||
return null
|
||||
return when {
|
||||
fileUri.withFileScheme() -> fileUri.path?.let { FileOutputStream(it) }
|
||||
fileUri.withContentScheme() -> {
|
||||
try {
|
||||
this.openOutputStream(fileUri, "wt")
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.e(TAG, "Unable to open stream in `wt` mode, retry in `rwt` mode.", e)
|
||||
// https://issuetracker.google.com/issues/180526528
|
||||
// Try with rwt to fix content provider issue
|
||||
val outStream = this.openOutputStream(fileUri, "rwt")
|
||||
Log.w(TAG, "`rwt` mode used.")
|
||||
outStream
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun Uri.withFileScheme(): Boolean {
|
||||
val scheme = this.scheme
|
||||
if (scheme.isNullOrEmpty() || scheme.lowercase(Locale.ENGLISH) == "file") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun Uri.withContentScheme(): Boolean {
|
||||
val scheme = this.scheme
|
||||
if (scheme != null && scheme.lowercase(Locale.ENGLISH) == "content") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private const val TAG = "UriHelper"
|
||||
fun String.parseUri(): Uri? {
|
||||
return if (this.isNotEmpty()) Uri.parse(this) else null
|
||||
}
|
||||
|
||||
fun String.decodeUri(): String {
|
||||
return Uri.decode(this) ?: ""
|
||||
}
|
||||
|
||||
fun Context.getBinaryDir(): File {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
this.applicationContext.noBackupFilesDir
|
||||
} else {
|
||||
this.applicationContext.filesDir
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun ContentResolver.getUriInputStream(fileUri: Uri?): InputStream? {
|
||||
if (fileUri == null)
|
||||
return null
|
||||
return when {
|
||||
fileUri.withFileScheme() -> fileUri.path?.let { FileInputStream(it) }
|
||||
fileUri.withContentScheme() -> this.openInputStream(fileUri)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("Recycle")
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun ContentResolver.getUriOutputStream(fileUri: Uri?): OutputStream? {
|
||||
if (fileUri == null)
|
||||
return null
|
||||
return when {
|
||||
fileUri.withFileScheme() -> fileUri.path?.let { FileOutputStream(it) }
|
||||
fileUri.withContentScheme() -> {
|
||||
try {
|
||||
this.openOutputStream(fileUri, "wt")
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.e(TAG, "Unable to open stream in `wt` mode, retry in `rwt` mode.", e)
|
||||
// https://issuetracker.google.com/issues/180526528
|
||||
// Try with rwt to fix content provider issue
|
||||
val outStream = this.openOutputStream(fileUri, "rwt")
|
||||
Log.w(TAG, "`rwt` mode used.")
|
||||
outStream
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun Uri.withFileScheme(): Boolean {
|
||||
val scheme = this.scheme
|
||||
if (scheme.isNullOrEmpty() || scheme.lowercase(Locale.ENGLISH) == "file") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun Uri.withContentScheme(): Boolean {
|
||||
val scheme = this.scheme
|
||||
if (scheme != null && scheme.lowercase(Locale.ENGLISH) == "content") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int = 0): PackageInfo =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
|
||||
} else {
|
||||
@Suppress("DEPRECATION") getPackageInfo(packageName, flags)
|
||||
}
|
||||
|
||||
|
||||
private const val TAG = "UriHelper"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue