fix: PackageInfo compat

This commit is contained in:
J-Jamet 2023-06-18 11:00:22 +02:00
parent c3e4504a1a
commit 59794557b3
17 changed files with 95 additions and 92 deletions

View file

@ -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)

View file

@ -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

View file

@ -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 {

View file

@ -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) {

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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(

View file

@ -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

View file

@ -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"

View file

@ -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
)

View file

@ -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) {

View file

@ -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) {

View file

@ -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

View file

@ -22,33 +22,32 @@ 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? {
fun String.parseUri(): Uri? {
return if (this.isNotEmpty()) Uri.parse(this) else null
}
}
fun String.decodeUri(): String {
fun String.decodeUri(): String {
return Uri.decode(this) ?: ""
}
}
fun Context.getBinaryDir(): File {
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? {
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriInputStream(fileUri: Uri?): InputStream? {
if (fileUri == null)
return null
return when {
@ -56,11 +55,11 @@ object UriHelper {
fileUri.withContentScheme() -> this.openInputStream(fileUri)
else -> null
}
}
}
@SuppressLint("Recycle")
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriOutputStream(fileUri: Uri?): OutputStream? {
@SuppressLint("Recycle")
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriOutputStream(fileUri: Uri?): OutputStream? {
if (fileUri == null)
return null
return when {
@ -79,23 +78,29 @@ object UriHelper {
}
else -> null
}
}
}
fun Uri.withFileScheme(): Boolean {
fun Uri.withFileScheme(): Boolean {
val scheme = this.scheme
if (scheme.isNullOrEmpty() || scheme.lowercase(Locale.ENGLISH) == "file") {
return true
}
return false
}
}
fun Uri.withContentScheme(): Boolean {
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"
}
private const val TAG = "UriHelper"